1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
|
/* jshint -W041 */
/* jslint browser: true*/
/* global cordova,StatusBar,angular,console, Masonry, URI */
angular.module('zmApp.controllers').controller('zmApp.WizardCtrl', ['$scope', '$rootScope', '$ionicModal', 'NVR', '$ionicSideMenuDelegate', '$ionicHistory', '$state', '$ionicPopup', 'SecuredPopups', '$http', '$q', 'zm', '$ionicLoading', 'WizardHandler', '$translate', '$cookies', function ($scope, $rootScope, $ionicModal, NVR, $ionicSideMenuDelegate, $ionicHistory, $state, $ionicPopup, SecuredPopups, $http, $q, zm, $ionicLoading, WizardHandler, $translate, $cookies) {
$scope.openMenu = function () {
$ionicSideMenuDelegate.toggleLeft();
};
//--------------------------------------------------------------------------
// logs into ZM
//--------------------------------------------------------------------------
function login(u, zmu, zmp) {
var d = $q.defer();
$http({
method: 'post',
//withCredentials: true,
url: u,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
},
transformRequest: function (obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" +
encodeURIComponent(obj[p]));
var params = str.join("&");
//console.log ("PARAMS in login:"+params);
return params;
},
data: {
username: zmu,
password: zmp,
action: "login",
view: "console"
}
})
.then(function (data, status, headers) {
data = data.data;
//console.log("LOOKING FOR " + zm.loginScreenString);
//console.log("DATA RECEIVED " + JSON.stringify(data));
if (data.indexOf(zm.loginScreenString1) == -1 &&
data.indexOf(zm.loginScreenString2) == -1 ) {
$scope.wizard.loginURL = $scope.wizard.fqportal;
$scope.wizard.portalValidText = $translate.instant('kPortal') + ": " + $scope.wizard.loginURL;
$scope.wizard.portalColor = "#16a085";
d.resolve(true);
return d.promise;
} else {
//console.log("************ERROR");
$scope.wizard.portalValidText = $translate.instant('kPortalDetectionFailed');
$scope.wizard.portalColor = "#e74c3c";
// NVR.debug("Login response form was invalid,I am going to try JSON login");
d.reject(false);
return d.promise;
}
},
function (error) {
// console.log("************ERROR:"+ JSON.stringify(error));
NVR.debug ("Login error returned: "+JSON.stringify(error));
$scope.wizard.portalValidText = $translate.instant('kPortalDetectionFailed');
$scope.wizard.portalColor = "#e74c3c";
d.reject(false);
return d.promise;
});
return d.promise;
}
//--------------------------------------------------------------------------
// we need a monitor ID to do cgi-bin detection - if you don't have
// monitors configured, cgi-bin won't work
//--------------------------------------------------------------------------
function getFirstMonitor() {
var d = $q.defer();
$http.get($scope.wizard.apiURL + "/monitors.json")
.then(function (success) {
// console.log("getfirst monitor success: " + JSON.stringify(success));
if (success.data.monitors.length > 0) {
var foundMid = -1;
for (var i = 0; i < success.data.monitors.length; i++) {
if (success.data.monitors[i].Monitor.Function != 'None' &&
success.data.monitors[i].Monitor.Enabled == '1') {
foundMid = success.data.monitors[i].Monitor.Id;
break;
}
}
if (foundMid != -1) {
NVR.debug("zmWizard - getFirstMonitor returned " + foundMid);
d.resolve(foundMid);
return d.promise;
} else {
d.reject(false);
return d.promise;
}
} else {
d.reject(false);
return d.promise;
}
},
function (error) {
//console.log("getfirst monitor error: " + JSON.stringify(error));
d.reject(false);
return d.promise;
});
return d.promise;
}
//--------------------------------------------------------------------------
// Utility function - iterates through a list of URLs
// Don't put loginData.reachability here --> we are using this to iterate
// through multiple options - not the same as fallback
//--------------------------------------------------------------------------
function findFirstReachableUrl(urls, tail) {
var d = $q.defer();
if (urls.length > 0) {
var t = "";
if (tail) t = tail;
//$ionicLoading.show({template: 'trying ' + urls[0].server});
NVR.log("zmWizard test.." + urls[0] + t);
return $http.get(urls[0] + t).then(function () {
NVR.log("Success: on " + urls[0] + t);
//$ionicLoading.hide();
d.resolve(urls[0]);
return d.promise;
//return urls[0];
}, function (err) {
NVR.log("zmWizard:Failed on " + urls[0] + t + " with error " + JSON.stringify(err));
// this is actually a success - I might get empty status
// or something
if (err.status < 300) {
NVR.log("A 2xx is a success, I think - " + urls[0]);
d.resolve(urls[0]);
return d.promise;
}
return findFirstReachableUrl(urls.slice(1), tail);
});
} else {
// $ionicLoading.hide();
NVR.log("zmWizard: findFirst returned no success");
d.reject("No reachable URL");
return d.promise;
}
return d.promise;
}
//--------------------------------------------------------------------------
// removes proto scheme from string
//--------------------------------------------------------------------------
function stripProto(u) {
if (u.indexOf('://') != -1)
return u.substr(u.indexOf('://') + 3);
else
return u;
}
//--------------------------------------------------------------------------
// tries to detect cgi-bin
//--------------------------------------------------------------------------
function detectcgi() {
var d = $q.defer();
var c = URI.parse($scope.wizard.loginURL);
var p1, p2;
p1 = "";
p2 = "";
if (c.userinfo)
p1 = c.userinfo + "@";
if (c.port)
p2 = ":" + c.port;
var baseUri = c.scheme + "://" + p1 + c.host + p2;
NVR.log("zmWizard CGI: baseURL is " + baseUri);
var a5 = baseUri + "/zmcgi"; // mageia
var a4 = baseUri + "/cgi-bin/zm"; // another one I found with a CentOS 6 guy
var a1 = baseUri + "/zm/cgi-bin"; // ubuntu/debian
var a2 = baseUri + "/cgi-bin-zm"; //fedora/centos/rhel
var a3 = baseUri + "/cgi-bin"; // doofus
var urls = [a1, a2, a3, a4, a5];
// can't use getPathZms as loginData is not inited yet
$http.get($scope.wizard.apiURL + "/configs/viewByName/ZM_PATH_ZMS.json")
//NVR.getPathZms() // what does ZM have stored in PATH_ZMS?
.then(function (data) {
// remove zms or nph-zms
var str = data.data.config.Value;
var path = str.trim();
path = path.replace("/nph-zms", "");
path = path.replace("/zms", "");
urls.push(baseUri.trim() + path);
NVR.log("zmWizard: getPathZMS succeeded, adding " + baseUri + path + " to things to try");
continueCgi(urls);
},
function (error) {
NVR.log("zmWizard: getPathZMS failed, but continuing...");
continueCgi(urls);
});
// Well, PATH_ZMS or not, lets call this function and brute force it
function continueCgi(urls) {
$ionicLoading.show({
template: $translate.instant('kDiscovering') + "...",
noBackdrop: true,
duration: zm.httpTimeout
});
getFirstMonitor()
.then(function (success) {
$ionicLoading.hide();
var tail = "/nph-zms?mode=single&monitor=" + success;
if ($scope.wizard.useauth && $scope.wizard.usezmauth) {
var ck = Math.floor(Math.random() * (50000 - 10000 + 1)) + 10000;
NVR.getAuthKey(success, ck)
.then(function (success) {
if (success == "") {
NVR.log("getAuthKey returned null, so going user=&pwd= way");
tail += "&user=" + $scope.wizard.zmuser + "&pass=" + $scope.wizard.zmpassword;
} else {
tail += success;
}
NVR.log("auth computed is : " + tail);
proceedwithCgiAfterAuth(urls, tail);
},
function (error) {
NVR.log("Should never come here, getAuthKey doesn't return error");
});
//console.log ("****CDING " + tail);
} else // no auth case
{
proceedwithCgiAfterAuth(urls, tail);
}
function proceedwithCgiAfterAuth(urls, tail) {
$ionicLoading.show({
template: $translate.instant('kDiscovering') + "...",
noBackdrop: true,
duration: zm.httpTimeout
});
findFirstReachableUrl(urls, tail)
.then(function (success) {
$ionicLoading.hide();
NVR.log("Valid cgi-bin found with: " + success);
$scope.wizard.streamingURL = success;
$scope.wizard.streamingValidText = "cgi-bin: " + $scope.wizard.streamingURL;
$scope.wizard.streamingColor = "#16a085";
d.resolve(true);
return d.promise;
},
function (error) {
$ionicLoading.hide();
NVR.debug("No cgi-bin found: " + JSON.stringify(error));
$scope.wizard.streamingValidText = $translate.instant('kPortalCgiBinFailed');
$scope.wizard.streamingColor = "#e74c3c";
d.reject(false);
return (d.promise);
});
}
},
function (error) {
$ionicLoading.hide();
$scope.wizard.streamingValidText = $translate.instant('kPortalCgiBinFailed') + " -" + $translate.instant('kPortalNoMonitorFound');
$scope.wizard.streamingColor = "#e74c3c";
d.reject(false);
return (d.promise);
});
}
// https://server/zm/cgi-bin/nph-zms?mode=single&monitor=1&user=admin&pass=cc
return d.promise;
}
//--------------------------------------------------------------------------
// Finds an appropriate API to use
//--------------------------------------------------------------------------
function detectapi() {
var u = $scope.wizard.loginURL;
var d = $q.defer();
var api1 = u + "/api";
var api3 = u + "/zm/api";
var c = URI.parse(u);
// lets also try without the path
var api2 = c.scheme + "://";
if (c.userinfo) api2 += c.userinfo + "@";
api2 += c.host;
if (c.port) api2 += ":" + c.port;
api2 += "/api";
// lets try both /zm/api and /api. What else is there?
var apilist = [api1, api2, api3];
findFirstReachableUrl(apilist, '/host/getVersion.json')
.then(function (success) {
NVR.log("Valid API response found with:" + success);
$scope.wizard.apiURL = success;
$scope.wizard.apiValidText = "API: " + $scope.wizard.apiURL;
$scope.wizard.apiColor = "#16a085";
d.resolve(true);
return d.promise;
},
function (error) {
//console.log("No APIs found: " + error);
$scope.wizard.apiValidText = $translate.instant('kPortalAPIFailed');
$scope.wizard.apiColor = "#e74c3c";
d.reject(false);
return (d.promise);
});
return d.promise;
}
//--------------------------------------------------------------------------
// logs out of ZM
//--------------------------------------------------------------------------
function logout(u) {
var d = $q.defer();
NVR.debug ("Clearing cookies");
if (window.cordova) {
// we need to do this or ZM will send same auth hash
// this was fixed in a PR dated Oct 18
cordova.plugin.http.clearCookies();
if ($scope.wizard.useauth && $scope.wizard.usebasicauth) {
NVR.debug ("setting basic auth with "+$scope.wizard.basicuser+":"+$scope.wizard.basicpassword);
cordova.plugin.http.useBasicAuth($scope.wizard.basicuser, $scope.wizard.basicpassword);
}
}
else {
angular.forEach($cookies, function (v, k) {
$cookies.remove(k);
});
}
$http({
method: 'POST',
url: u,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
},
transformRequest: function (obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" +
encodeURIComponent(obj[p]));
var params = str.join("&");
return params;
},
data: {
action: "logout",
view: "login"
}
})
.then(function (success) {
$rootScope.zmCookie = "";
//console.log("ZMlogout success, cookie removed");
d.resolve(true);
return d.promise;
}, function (error) {
//console.log("ZMlogout success");
d.resolve(true);
return d.promise;
});
return d.promise;
}
//--------------------------------------------------------------------------
// clears all status updates in the verify results page - if you
// get back to it
//--------------------------------------------------------------------------
$scope.enterResults = function () {
$scope.portalValidText = "";
$scope.apiValidateText = "";
$scope.streamingValidateText = "";
$scope.wizard.fqportal = "";
return true;
};
//--------------------------------------------------------------------------
// tries to log into the portal and then discover api and cgi-bin
//--------------------------------------------------------------------------
function loginWebScrape(u,zmu,zmp) {
var d = $q.defer();
NVR.debug("Logging in using old web-scrape method");
$ionicLoading.show({
template: $translate.instant('kAuthenticatingWebScrape'),
noBackdrop: true,
duration: zm.httpTimeout
});
u=u+'/index.php?view=console';
NVR.debug ("webscrape login to:"+u);
//NVR.debug ("*** AUTH LOGIN URL IS " + loginData.url);
$http({
method: 'post',
timeout: zm.httpTimeout,
//withCredentials: true,
url: u ,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
},
transformRequest: function (obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" +
encodeURIComponent(obj[p]));
var params = str.join("&");
return params;
},
data: {
username: zmu,
password: zmp,
action: "login",
view: "console"
}
})
.then(function (data, status, headers) {
// console.log(">>>>>>>>>>>>>> PARALLEL POST SUCCESS");
data = data.data;
$ionicLoading.hide();
console.log ("GOT "+data);
if (data.indexOf(zm.loginScreenString1) >=0 ||
data.indexOf(zm.loginScreenString2) >=0 ) {
//eventServer.start();
//$rootScope.loggedIntoZm = 1;
NVR.log("zmAutologin successfully logged into Zoneminder");
$scope.wizard.loginURL = $scope.wizard.fqportal;
$scope.wizard.portalValidText = $translate.instant('kPortal') + ": " + $scope.wizard.loginURL;
$scope.wizard.portalColor = "#16a085";
d.resolve(true);
return d.promise;
// now go to authKey part, so don't return yet...
} else // this means login error
{
// $rootScope.loggedIntoZm = -1;
//console.log("**** ZM Login FAILED");
NVR.log("zmAutologin Error: Bad Credentials ", "error");
$scope.wizard.portalValidText = $translate.instant('kPortalDetectionFailed');
$scope.wizard.portalColor = "#e74c3c";
d.reject("Login Error");
return (d.promise);
// no need to go to next code, so return above
}
// Now go ahead and re-get auth key
// if login was a success
},
function (error, status) {
// console.log(">>>>>>>>>>>>>> PARALLEL POST ERROR");
$ionicLoading.hide();
//console.log("**** ZM Login FAILED");
// FIXME: Is this sometimes results in null
NVR.log("zmAutologin Error " + JSON.stringify(error) + " and status " + status);
// bad urls etc come here
//$rootScope.loggedIntoZm = -1;
$scope.wizard.portalValidText = $translate.instant('kPortalDetectionFailed');
$scope.wizard.portalColor = "#e74c3c";
d.reject("Login Error");
return d.promise;
});
return d.promise;
}
function wizardLogin(u,zmu,zmp) {
var d = $q.defer();
var loginAPI = u + '/api/host/login.json';
NVR.debug ("Inside wizardLogin: will try "+loginAPI);
$http({
method: 'post',
url: loginAPI,
timeout: zm.httpTimeout,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
responseType: 'text',
transformResponse: undefined,
transformRequest: function (obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: {
user: zmu,
pass: zmp
}
})
//$http.get(loginAPI)
.then(function (textsucc) {
$ionicLoading.hide();
var succ;
try {
succ = JSON.parse(textsucc.data);
if (!succ.version) {
NVR.debug("API login returned fake success, going back to webscrape");
loginWebScrape(u,zmu,zmp)
.then(function (succ) {
d.resolve("Login Success");
return d.promise;
},
function (err) {
$ionicLoading.hide();
d.reject("Login Error");
return (d.promise);
});
return d.promise;
}
NVR.debug("API based login returned... ");
console.log (JSON.stringify(succ));
$ionicLoading.hide();
//$rootScope.loggedIntoZm = 1;
$rootScope.authSession = '';
if (succ.credentials) {
$rootScope.authSession = "&" + succ.credentials;
if (succ.append_password == '1') {
$rootScope.authSession = $rootScope.authSession +
loginData.password;
}
}
NVR.log("Stream authentication construction: " +
$rootScope.authSession);
NVR.log("zmAutologin successfully logged into Zoneminder via API");
$scope.wizard.loginURL = $scope.wizard.fqportal;
$scope.wizard.portalValidText = $translate.instant('kPortal') + ": " + $scope.wizard.loginURL;
$scope.wizard.portalColor = "#16a085";
d.resolve("Login Success");
return d.promise;
} catch (e) {
NVR.debug("Login API approach did not work...");
loginWebScrape(u,zmu,zmp)
.then(function (succ) {
$scope.wizard.loginURL = $scope.wizard.fqportal;
$scope.wizard.portalValidText = $translate.instant('kPortal') + ": " + $scope.wizard.loginURL;
$scope.wizard.portalColor = "#16a085";
d.resolve("Login Success");
return d.promise;
},
function (err) {
$ionicLoading.hide();
$scope.wizard.portalValidText = $translate.instant('kPortalDetectionFailed');
$scope.wizard.portalColor = "#e74c3c";
d.reject("Login Error");
return (d.promise);
});
return d.promise;
}
},
function (err) {
console.log("******************* API login error " + JSON.stringify(err));
$ionicLoading.hide();
if (1) {
//if (err && err.data && 'success' in err.data) {
console.log("API based login not supported, need to use web scraping...");
loginWebScrape(u,zmu,zmp)
.then(function (succ) {
d.resolve("Login Success");
return d.promise;
},
function (err) {
d.reject("Login Error");
return (d.promise);
});
} else {
// $rootScope.loggedIntoZm = -1;
//console.log("**** ZM Login FAILED");
NVR.log("zmAutologin Error via API: some meta foo", "error");
$rootScope.$broadcast('auth-error', "I'm confused why");
d.reject("Login Error");
return (d.promise);
}
}
); // post
return d.promise;
}
function validateData() {
$rootScope.authSession = '';
$rootScope.zmCookie = '';
$scope.wizard.portalValidText = "";
$scope.wizard.apiValidText = "";
$scope.wizard.streamingValidText = "";
$scope.wizard.fqportal = "";
$scope.wizard.loginURL = "";
$scope.wizard.apiURL = "";
$scope.wizard.streamingURL = "";
$scope.wizard.serverName = "";
var d = $q.defer();
var c = URI.parse($scope.wizard.portalurl);
$scope.wizard.serverName = c.host;
if (c.port)
$scope.wizard.serverName += "-" + c.port;
var b = "";
if ($scope.wizard.useauth && $scope.wizard.usebasicauth) {
// b = $scope.wizard.basicuser + ":" + $scope.wizard.basicpassword + "@";
//console.log("B=" + b);
$rootScope.basicAuthHeader = 'Basic ' + btoa($scope.wizard.basicuser + ':' + $scope.wizard.basicpassword);
//console.log (">>>> WIZARD SET BASIC AUTH TO " + $rootScope.basicAuthHeader);
}
var u = c.scheme + "://" + b + c.host;
if (c.port) u += ":" + c.port;
if (c.path) u += c.path;
if (u.slice(-1) == '/') {
u = u.slice(0, -1);
}
$scope.wizard.fqportal = u;
//u = u + '/index.php?view=console';
NVR.log("Wizard: login url is " + u);
// now lets login
var zmu = "x";
var zmp = "x";
if ($scope.wizard.usezmauth) {
zmu = $scope.wizard.zmuser;
zmp = $scope.wizard.zmpassword;
}
// logout first for the adventurers amongst us who must
// use it even after logging in
NVR.log("zmWizard: logging out");
$ionicLoading.show({
template: $translate.instant('kCleaningUp') + "...",
noBackdrop: true,
duration: zm.httpTimeout
});
logout(u)
.then(function (ans) {
// login now
$ionicLoading.hide();
NVR.log("zmWizard: logging in with " + u + " " + zmu);
// The logic will be:
// Login then do an api detect and cgi-detect together
$ionicLoading.show({
template: $translate.instant('kDiscoveringPortal') + "...",
noBackdrop: true,
duration: zm.httpTimeout
});
NVR.setCurrentServerVersion("");
wizardLogin(u, zmu, zmp)
.then(function (success) {
$ionicLoading.hide();
NVR.log("zmWizard: login succeeded");
// API Detection
$ionicLoading.show({
template: $translate.instant('kDiscoveringAPI') + "...",
noBackdrop: true,
duration: zm.httpTimeout
});
detectapi()
.then(function (success) {
$ionicLoading.hide();
NVR.log("zmWizard: API succeeded");
$ionicLoading.show({
template: $translate.instant('kDiscoveringCGI') + "...",
noBackdrop: true,
duration: zm.httpTimeout
});
// CGI detection
detectcgi()
.then(function (success) {
$ionicLoading.hide();
// return true here because we want to progress
return d.resolve(true);
},
function (error) {
$ionicLoading.hide();
// return true here because we want to progress
return d.resolve(true);
});
},
function (error) {
$ionicLoading.hide();
NVR.log("zmWizard: api failed");
// return true here because we want to progress
return d.resolve(true);
});
},
// if login failed, don't progress in the wizard
function (error) {
$ionicLoading.hide();
NVR.log("zmWizard: login failed");
$scope.wizard.portalValidText = $translate.instant('kPortalLoginUnsuccessful');
$scope.wizard.portalColor = "#e74c3c";
return d.resolve(true);
});
}); //finally
return d.promise;
}
//--------------------------------------------------------------------------
// checks for a protocol
//--------------------------------------------------------------------------
function checkscheme(url) {
if ((!/^(f|ht)tps?:\/\//i.test(url)) && (url != "")) {
return false;
} else
return true;
}
//--------------------------------------------------------------------------
// exit validator for auth wizard
//--------------------------------------------------------------------------
$scope.exitAuth = function () {
NVR.log("Wizard: validating auth syntax");
if ($scope.wizard.useauth) {
if (!$scope.wizard.usezmauth && !$scope.wizard.usebasicauth) {
$rootScope.zmPopup = SecuredPopups.show('show', {
title: $translate.instant('kError'),
template: $translate.instant('kOneAuth'),
buttons: [{
text: $translate.instant('kButtonOk')
}]
});
return false;
}
if ($scope.wizard.usezmauth) {
if ((!$scope.wizard.zmuser) || (!$scope.wizard.zmpassword)) {
$rootScope.zmPopup = SecuredPopups.show('show', {
title: $translate.instant('kError'),
template: $translate.instant('kValidNameZMAuth'),
buttons: [{
text: $translate.instant('kButtonOk')
}]
});
return false;
}
}
if ($scope.wizard.usebasicauth) {
if ((!$scope.wizard.basicuser) || (!$scope.wizard.basicpassword)) {
$rootScope.zmPopup = SecuredPopups.show('show', {
title: $translate.instant('kError'),
template: $translate.instant('kValidNameBasicAuth'),
buttons: [{
text: $translate.instant('kButtonOk')
}]
});
return false;
}
}
}
// Coming here means we can go to the next step
// load the step
WizardHandler.wizard().next();
// start discovery;
validateData();
};
//--------------------------------------------------------------------------
// validator for portal url wizard
//--------------------------------------------------------------------------
$scope.exitPortal = function () {
NVR.log("Wizard: validating portal url syntax");
if (!$scope.wizard.portalurl) {
$rootScope.zmPopup = SecuredPopups.show('show', {
title: $translate.instant('kError'),
template: $translate.instant('kPortalEmpty'),
buttons: [{
text: $translate.instant('kButtonOk')
}]
});
return false;
}
if (!checkscheme($scope.wizard.portalurl)) {
$scope.portalproto = [{
text: "http",
value: "http://"
},
{
text: "https",
value: "https://"
}
];
$scope.myproto = {
proto: ""
};
$rootScope.zmPopup = $ionicPopup.show({
title: $translate.instant('kPortalNoProto'),
scope: $scope,
template: $translate.instant('kPortalPleaseSelect') + ': <ion-radio-fix ng-repeat="item in portalproto" ng-value="item.value" ng-model="myproto.proto">{{item.text}}</ion-radio-fix>',
buttons: [{
text: $translate.instant('kButtonOk'),
onTap: function (e) {
NVR.debug("Protocol selected:" + $scope.myproto.proto);
$scope.wizard.portalurl = $scope.myproto.proto + stripProto($scope.wizard.portalurl);
}
}]
});
return false;
}
//$scope.wizard.portalurl = $scope.wizard.portalurl.toLowerCase().trim();
$scope.wizard.portalurl = $scope.wizard.portalurl.trim();
NVR.log("Wizard: stripped url:" + $scope.wizard.portalurl);
var c = URI.parse($scope.wizard.portalurl);
if (!c.scheme) {
$rootScope.zmPopup = SecuredPopups.show('show', {
title: $translate.instant('kError'),
template: $translate.instant('kPortalInvalidUrl'),
buttons: [{
text: $translate.instant('kButtonOk')
}]
});
return false;
}
if (c.userinfo) // basic auth stuff in here, take it out and put it into the next screen
{
$scope.wizard.useauth = true;
$scope.wizard.usebasicauth = true;
var barray = c.userinfo.split(":", 2);
$scope.wizard.basicuser = barray[0];
$scope.wizard.basicpassword = barray[1];
}
$scope.wizard.portalurl = c.scheme + "://";
if (c.host) $scope.wizard.portalurl += c.host;
if (c.port) $scope.wizard.portalurl += ":" + c.port;
if (c.path) $scope.wizard.portalurl += c.path;
$scope.wizard.portalurl = $scope.wizard.portalurl.toLowerCase();
NVR.log("Wizard: normalized url:" + $scope.wizard.portalurl);
return true;
};
//--------------------------------------------------------------------------
// part of auth wizard - toggles display of auth components
//--------------------------------------------------------------------------
$scope.toggleAuth = function () {
if (!$scope.wizard.useauth) {
$scope.wizard.usebasicauth = false;
$scope.wizard.usezmauth = false;
}
};
//--------------------------------------------------------------------------
// global tip toggler for all wizard steps
//--------------------------------------------------------------------------
$scope.toggleTip = function () {
$scope.wizard.tipshow = !$scope.wizard.tipshow;
if ($scope.wizard.tipshow)
$scope.wizard.tiptext = $translate.instant('kHideTip');
else
$scope.wizard.tiptext = $translate.instant('kShowTip');
};
$scope.gotoLoginState = function () {
$rootScope.wizard = angular.copy($scope.wizard);
$ionicHistory.nextViewOptions({
disableBack: true
});
$state.go("app.login", {
"wizard": true
});
return;
};
//--------------------------------------------------------------------------
// initial
//--------------------------------------------------------------------------
$scope.$on('$ionicView.beforeEnter', function () {
//console.log("**VIEW ** Help Ctrl Entered");
var monId = -1;
$scope.wizard = {
tipshow: false,
tiptext: $translate.instant('kShowTip'),
useauth: false,
usebasicauth: false,
usezmauth: false,
portalurl: "",
basicuser: "",
basicpassword: "",
zmuser: "",
zmpassword: "",
///////////////////////
loginURL: "",
apiURL: "",
streamingURL: "",
fqportal: "",
portalValidText: "",
portalColor: "",
apiValidText: "",
apiColor: "",
streamingValidText: "",
streamingColor: "",
serverName: "",
};
});
}]);
|