/* 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(); }; var serverbuttons = []; var availableServers; //---------------------------------------------------------------- // 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; console.log ("*************************************************"); availableServers = Object.keys(ZMDataModel.getServerGroups()); serverbuttons = []; for (var servIter=0; servIter 1) { ZMDataModel.zmLog ("Deleting " + $scope.loginData.serverName); delete zmServers[$scope.loginData.serverName]; ZMDataModel.setServerGroups(zmServers); // point to first element // better than nothing // note this is actually unordered $scope.loginData = zmServers[Object.keys(zmServers)[0]]; ZMDataModel.setLogin($scope.loginData); availableServers = Object.keys(ZMDataModel.getServerGroups()); serverbuttons = []; for (var servIter=0; servIter 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 // Nov 17 Don't mess with this path. centos uses zm-cgi-bin of all things /*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"; } // do this before setLogin so message is sent if (!$scope.check.isUseEventServer) { $rootScope.isAlarm = 0; if ($rootScope.apnsToken) { ZMDataModel.zmLog ("Making sure we don't get push notifications"); EventServer.sendMessage('push', { type: 'token', platform: $rootScope.platformOS, token: $rootScope.apnsToken, state: "disabled" }); } } ZMDataModel.setLogin($scope.loginData); if ($scope.check.isUseEventServer) { EventServer.init(); if ($rootScope.apnsToken && $scope.loginData.disablePush != '1') { ZMDataModel.zmLog ("Making sure we get push notifications"); EventServer.sendMessage('push', { type: 'token', platform: $rootScope.platformOS, token: $rootScope.apnsToken, state: "enabled" }); } EventServer.sendMessage("control", { type: 'filter', monlist: $scope.loginData.eventServerMonitors, intlist: $scope.loginData.eventServerInterval }); } // 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 + "/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(); availableServers = Object.keys(ZMDataModel.getServerGroups()); serverbuttons = []; for (var servIter=0; servIter