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
|
/* jshint -W041 */
/* jslint browser: true*/
/* global cordova,StatusBar,angular,console */
angular.module('zmApp.controllers').controller('zmApp.LoginCtrl', ['$scope', '$rootScope', 'zm', '$ionicModal', 'ZMDataModel', '$ionicSideMenuDelegate', '$ionicPopup', '$http', '$q', '$ionicLoading', 'zmAutoLogin', '$cordovaPinDialog', 'EventServer', '$ionicHistory', '$state', '$ionicActionSheet', function ($scope, $rootScope, zm, $ionicModal, ZMDataModel, $ionicSideMenuDelegate, $ionicPopup, $http, $q, $ionicLoading, zmAutoLogin, $cordovaPinDialog, EventServer, $ionicHistory, $state, $ionicActionSheet) {
$scope.openMenu = function () {
$ionicSideMenuDelegate.toggleLeft();
};
//----------------------------------------------------------------
// Alarm notification handling
//----------------------------------------------------------------
$scope.handleAlarms = function()
{
$rootScope.isAlarm=!$rootScope.isAlarm;
if (!$rootScope.isAlarm)
{
$rootScope.alarmCount="0";
$ionicHistory.nextViewOptions({disableBack: true});
$state.go("events", {"id": 0}, { reload: true });
}
};
$scope.loginData = ZMDataModel.getLogin();
$scope.check = {
isUseAuth: "",
isUseEventServer: ""
};
$scope.check.isUseAuth = ($scope.loginData.isUseAuth == '1') ? true : false;
$scope.check.isUseEventServer = ($scope.loginData.isUseEventServer == '1') ? true : false;
$scope.serverActionSheet = function () {
var hideSheet = $ionicActionSheet.show({
buttons: [
{
text: 'New Group...'
},
{
text: 'Rename'
},
],
destructiveText: 'Delete',
titleText: 'Manage Server Groups',
cancelText: 'Cancel',
cancel: function () {
// add cancel code..
},
buttonClicked: function (index) {
return true;
}
});
};
//----------------------------------------------------------------
// Save anyway when you exit
//----------------------------------------------------------------
$scope.$on('$ionicView.beforeLeave', function () {
// Don't do this -- it will try to login to ZM
// and go back to the menu
//saveItems();
});
//-------------------------------------------------------------------------
// Lets make sure we set screen dim properly as we enter
// The problem is we enter other states before we leave previous states
// from a callback perspective in ionic, so we really can't predictably
// reset power state on exit as if it is called after we enter another
// state, that effectively overwrites current view power management needs
//------------------------------------------------------------------------
$scope.$on('$ionicView.enter', function () {
console.log("**VIEW ** LoginCtrl Entered");
ZMDataModel.setAwake(false);
});
//--------------------------------------------------------------------------
// When PIN is enabled, this is called to specify a PIN
// FIXME: Get rid of cordovaPinDialog. It's really not needed
//--------------------------------------------------------------------------
$scope.pinPrompt = function (evt) {
ZMDataModel.zmLog("Password prompt");
if ($scope.loginData.usePin) {
$scope.loginData.pinCode = "";
$cordovaPinDialog.prompt('Enter PIN', 'PIN Protect').then(
function (result1) {
// console.log (JSON.stringify(result1));
if (result1.input1 && result1.buttonIndex == 1) {
$cordovaPinDialog.prompt('Reconfirm PIN', 'PIN Protect')
.then(function (result2) {
if (result1.input1 == result2.input1) {
ZMDataModel.zmLog("Pin code match");
$scope.loginData.pinCode = result1.input1;
} else {
ZMDataModel.zmLog("Pin code mismatch");
$scope.loginData.usePin = false;
ZMDataModel.displayBanner('error', ['Pin code mismatch']);
}
},
function (error) {
console.log("Error inside");
$scope.loginData.usePin = false;
});
} else {
$scope.loginData.usePin = false;
}
},
function (error) {
console.log("Error outside");
$scope.loginData.usePin = false;
});
} else {
ZMDataModel.zmDebug("Password disabled");
}
};
//-------------------------------------------------------------------------------
// Makes input easier
//-------------------------------------------------------------------------------
$scope.portalKeypress = function (evt) {
if (/^https:\/\//i.test($scope.loginData.url)) {
$scope.loginData.useSSL = true;
}
// if ($scope.loginData.streamingurl.indexOf($scope.loginData.url) !=0)
$scope.loginData.streamingurl = $scope.loginData.url;
// Changed Sep 16 2015: Seems cgi-bin will now have /zm/cgi-bin by
// default in packages instead of /cgi-bin
//if ($scope.loginData.streamingurl.slice(-3).toLowerCase() == '/zm') {
//$scope.loginData.streamingurl = $scope.loginData.streamingurl.slice(0, -3);
//}
// if ($scope.loginData.apiurl.indexOf($scope.loginData.url) !=0)
$scope.loginData.apiurl = $scope.loginData.url + "/api";
};
//-------------------------------------------------------------------------------
// Adds http to url if not present
// http://stackoverflow.com/questions/11300906/check-if-a-string-starts-with-http-using-javascript
//-------------------------------------------------------------------------------
function addhttp(url) {
if ((!/^(f|ht)tps?:\/\//i.test(url)) && (url != "")) {
url = "http://" + url;
}
return url;
}
function addWsOrWss(url) {
if ((!/^wss?:\/\//i.test(url)) && (url != "")) {
url = "ws://" + url;
}
return url;
}
//-----------------------------------------------------------------------------
// Perform the login action when the user submits the login form
//-----------------------------------------------------------------------------
function saveItems()
{
console.log('Saving login');
ZMDataModel.setFirstUse(false);
/*if (parseInt($scope.loginData.maxMontage) > zm.safeMontageLimit) {
$ionicPopup.alert({
title: 'Note',
template: 'You have selected to view more than 10 monitors in the Montage screen. Note that this is very resource intensive and may load the server or cause issues in the application. If you are not sure, please consider limiting this value to 10'
});
}*/
// lets so some basic sanitization of the data
// I am already adding "/" so lets remove spurious ones
// though webkit has no problems. Even so, this is to avoid
// a deluge of folks who look at the error logs and say
// the reason the login data is not working is because
// the app is adding multiple "/" characters
$scope.loginData.url = $scope.loginData.url.trim();
$scope.loginData.apiurl = $scope.loginData.apiurl.trim();
$scope.loginData.username = $scope.loginData.username.trim();
$scope.loginData.streamingurl = $scope.loginData.streamingurl.trim();
$scope.loginData.eventServer = $scope.loginData.eventServer.trim();
$scope.loginData.isUseAuth = ($scope.check.isUseAuth) ? "1" : "0";
$scope.loginData.isUseEventServer = ($scope.check.isUseEventServer) ? "1" : "0";
if ($scope.loginData.url.slice(-1) == '/') {
$scope.loginData.url = $scope.loginData.url.slice(0, -1);
}
if ($scope.loginData.apiurl.slice(-1) == '/') {
$scope.loginData.apiurl = $scope.loginData.apiurl.slice(0, -1);
}
if ($scope.loginData.streamingurl.slice(-1) == '/') {
$scope.loginData.streamingurl = $scope.loginData.streamingurl.slice(0, -1);
}
if ($scope.loginData.eventServer.slice(-1) == '/') {
$scope.loginData.eventServer = $scope.loginData.eventServer.slice(0, -1);
}
// strip cgi-bin if it is there but only at the end
if ($scope.loginData.streamingurl.slice(-7).toLowerCase() == 'cgi-bin') {
$scope.loginData.streamingurl = $scope.loginData.streamingurl.slice(0, -7);
}
// check for protocol and if not put it in
$scope.loginData.url = addhttp($scope.loginData.url);
$scope.loginData.apiurl = addhttp($scope.loginData.apiurl);
$scope.loginData.streamingurl = addhttp($scope.loginData.streamingurl);
$scope.loginData.eventServer = addWsOrWss($scope.loginData.eventServer);
if ($scope.loginData.useSSL) {
// replace all http with https
$scope.loginData.url = $scope.loginData.url.replace("http:", "https:");
$scope.loginData.apiurl = $scope.loginData.apiurl.replace("http:", "https:");
$scope.loginData.streamingurl = $scope.loginData.streamingurl.replace("http:", "https:");
$scope.loginData.eventServer = $scope.loginData.eventServer.replace("ws:", "wss:");
} else {
// replace all https with http
$scope.loginData.url = $scope.loginData.url.replace("https:", "http:");
$scope.loginData.apiurl = $scope.loginData.apiurl.replace("https:", "http:");
$scope.loginData.streamingurl = $scope.loginData.streamingurl.replace("https:", "http:");
// don't do it for WSS - lets mandate that
}
var apiurl = $scope.loginData.apiurl + '/host/getVersion.json';
var portalurl = $scope.loginData.url + '/index.php';
// Check if isUseAuth is set make sure u/p have a dummy value
if ($scope.check.isUseAuth) {
if (!$scope.loginData.username) $scope.loginData.username = "x";
if (!$scope.loginData.password) $scope.loginData.password = "x";
ZMDataModel.zmLog("Authentication is disabled, setting dummy user & pass");
}
if (parseInt($scope.loginData.maxMontage) <=0)
{
$scope.loginData.maxMontage ="10";
}
ZMDataModel.setLogin($scope.loginData);
// now grab and report PATH_ZMS
ZMDataModel.getPathZms()
.then(function (data) {
var ld = ZMDataModel.getLogin();
ZMDataModel.zmLog("PATH_ZMS:" + data + " ,Path ZmNinja will use:" + ld.streamingurl + "/cgi-bin/nph-zms");
ZMDataModel.zmLog("If live streams are not working, make sure you check these values");
});
zmAutoLogin.doLogin("authenticating...")
// Do the happy menu only if authentication works
// if it does not work, there is an emitter for auth
// fail in app.js that will be called to show an error
// box
.then(function (data) {
// Now let's validate if the API works
ZMDataModel.zmLog("Validating APIs at " + apiurl);
$http.get(apiurl)
.success(function (data) {
EventServer.refresh();
$ionicPopup.alert({
title: 'Login validated',
template: 'Please explore the menu and enjoy zmNinja!'
}).then(function (res) {
$ionicSideMenuDelegate.toggleLeft();
ZMDataModel.zmDebug ("Force reloading monitors...");
var refresh = ZMDataModel.getMonitors(1);
});
})
.error(function (error) {
ZMDataModel.displayBanner('error', ['ZoneMinder API check failed', 'Please check API settings']);
ZMDataModel.zmLog("API login error " + JSON.stringify(error));
$ionicPopup.alert({
title: 'Login validated but API failed',
template: 'Please check your API settings'
});
});
});
}
$scope.saveItems = function () {
if (!$scope.loginData.serverName)
{
$ionicPopup.alert({
title:'Error',
template: 'Server Name cannot be empty',
})
.then(function(res)
{return;});
}
else
{
saveItems();
}
};
}]);
|