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

angular.module('zmApp.controllers').controller('zmApp.LogCtrl', ['$scope', '$rootScope','zm', '$ionicModal', 'ZMDataModel', '$ionicSideMenuDelegate', '$fileLogger', '$cordovaEmailComposer', '$ionicPopup', '$timeout', function ($scope, $rootScope,zm, $ionicModal, ZMDataModel, $ionicSideMenuDelegate, $fileLogger, $cordovaEmailComposer, $ionicPopup, $timeout) {
    $scope.openMenu = function () {
        $ionicSideMenuDelegate.toggleLeft();
    };

    //---------------------------------------------------------------
    // Controller main
    //---------------------------------------------------------------
    $scope.zmAppVersion = ZMDataModel.getAppVersion();


    $scope.deleteLogs = function () {

        var confirmPopup = $ionicPopup.confirm({
            title: 'Please Confirm',
            template: 'Are you sure you want to delete logs?',
        });

        confirmPopup.then(function (res) {
            if (res) {
                $fileLogger.deleteLogfile().then(function () {
                    console.log('Logfile deleted');
                    $fileLogger.setStorageFilename(zm.logFile);
                    $scope.zmLog.logString = "";
                });
            }
        });
    };


    //--------------------------------------------------------------------------
    // Make sure user knows information masking is best effort
    //--------------------------------------------------------------------------

    $scope.sendEmail = function (logstring) {
        $ionicPopup.confirm({
                title: 'Sensitive Information',
                template: 'zmNinja will modify the logs when creating the email to remove sensitive data like urls and passwords. However it is eventually <b>your responsibility</b> to make sure there is no sensitive data in the logs. Please make sure you review and edit the logs in the next screen before you send.'
            })
            .then(function (res) {
                if (res) sendEmailReally(logstring);

            });
    };

    //--------------------------------------------------------------------------
    // Convenience function to send logs via email
    //--------------------------------------------------------------------------
    function sendEmailReally(logstring) {
        if (window.cordova) {

            $cordovaEmailComposer.isAvailable().then(function () {

                // do my best to replace sensitive information
                var loginData = ZMDataModel.getLogin();
                var re1 = new RegExp(loginData.password, "g");
                logstring = logstring.replace(re1, "<deleted>");
                // keep the protocol, helps to debug
                var urlNoProtocol = loginData.url.replace(/.*?:\/\//, "");
                var re2 = new RegExp(urlNoProtocol, "g");
                // just replacing baseurl - that will take care of
                // masking api but may not be cgi
                logstring = logstring.replace(re2, "<server>");

                urlNoProtocol = loginData.streamingurl.replace(/.*?:\/\//, "");
                var re3 = new RegExp(urlNoProtocol, "g");
                logstring = logstring.replace(re3, "<server>");

                var email = {
                    to: zm.authoremail,
                    subject: 'zmNinja Logs',
                    body: logstring,
                    isHtml: false
                };
                $cordovaEmailComposer.open(email)
                    .then(null, function () {
                        // user cancelled email
                    });
            }, function () {
                ZMDataModel.zmLog("Email plugin not found", "error");
            });
        } else {
            console.log("Skipping email module as cordova does not exist");
        }

    }

    //-------------------------------------------------------------------------
    // 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 ** Log Ctrl Entered");
        ZMDataModel.setAwake(false);

        $scope.zmLog = {
            logString: ""
        };
        $fileLogger.getLogfile().then(function (l) {

                $scope.zmLog.logString = l;
                //console.log ("LOGS" + logstring);
            },
            function (error) {
                $scope.zmLog.logString = "Error getting log: " + JSON.stringify(error);
            });


    });

}]);