summaryrefslogtreecommitdiff
path: root/www/js/LoginCtrl.js
blob: e014500bc116510247ca9d11cb262f14a89c2752 (plain)
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
/* jshint -W041 */
/* jslint browser: true*/
/* global cordova,StatusBar,angular,console */

angular.module('zmApp.controllers').controller('zmApp.LoginCtrl', ['$scope', '$rootScope', '$ionicModal', 'ZMDataModel', '$ionicSideMenuDelegate', '$ionicPopup', '$http', '$q', '$ionicLoading', function ($scope, $rootScope, $ionicModal, ZMDataModel, $ionicSideMenuDelegate, $ionicPopup, $http, $q, $ionicLoading) {
    $scope.openMenu = function () {
        $ionicSideMenuDelegate.toggleLeft();
    };

    $scope.loginData = ZMDataModel.getLogin();

//-------------------------------------------------------------------------------
// 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 = "http://" + url;
   }
   return url;
}

    //-----------------------------------------------------------------------------
    // Perform the login action when the user submits the login form
    //-----------------------------------------------------------------------------
    $scope.save = function () {
        console.log('Saving login');

        if (parseInt($scope.loginData.maxMontage) > 10) {
            $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();

        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);

        }

        // 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);

        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:");

        }
        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:");
        }



        // FIXME:: Do a login id check too

        var apiurl = $scope.loginData.apiurl + '/host/getVersion.json';
        var portalurl = $scope.loginData.url + '/index.php';
        var streamingurl = $scope.loginData.streamingurl +
            '/cgi-bin/zms?user=' + $scope.loginData.username + "&pass=" + $scope.loginData.password;


        console.log("Checking API: " + apiurl + " PORTAL: " + portalurl + " CGI-BIN: " + streamingurl);


        // Let's do a sanity check to see if the URLs are ok

        $ionicLoading.show({
            template: 'Checking data...',
            animation: 'fade-in',
            showBackdrop: true,
            duration: 15000,
            maxWidth: 200,
            showDelay: 0
        });


        $q.all([
    $http.get(apiurl),
    $http.get(portalurl),
    //$http.get(streamingurl),
  ]).then(
            function (results) {
                $ionicLoading.hide();
                //alert("All good");
            },
            function (error) {
                $ionicLoading.hide();
                //alert("Error string" + JSON.stringify(error));

                $ionicPopup.show({
                    title: 'Please Check your settings',
                    template: 'I tried reaching out using the data you provided and failed. This may also be because ZoneMinder is currently not reachable.',
                    buttons: [
                        {
                            text: 'Ok',
                            type: 'button-positive'
                        },
                        {
                            text: 'Details...',
                            onTap: function (e) {
                                $ionicPopup.alert({
                                    title: 'Error Details',
                                    template: JSON.stringify(error)
                                });
                            }
                        }
                    ]
                });

            }

        );
        ZMDataModel.setLogin($scope.loginData);
    };


}]);