Browse Source

Actually implement alert email system

tags/v1.0
Skylar Ittner 1 year ago
parent
commit
865d47121c
6 changed files with 141 additions and 5 deletions
  1. 2
    1
      composer.json
  2. 77
    1
      composer.lock
  3. 4
    1
      index.php
  4. 3
    1
      lang/en_us.php
  5. 43
    1
      lib/login.php
  6. 12
    0
      settings.template.php

+ 2
- 1
composer.json View File

@@ -7,7 +7,8 @@
7 7
         "spomky-labs/otphp": "^8.3",
8 8
         "endroid/qrcode": "^1.9",
9 9
         "ldaptools/ldaptools": "^0.24.0",
10
-        "guzzlehttp/guzzle": "^6.2"
10
+        "guzzlehttp/guzzle": "^6.2",
11
+        "phpmailer/phpmailer": "^5.2"
11 12
     },
12 13
     "authors": [
13 14
         {

+ 77
- 1
composer.lock View File

@@ -4,7 +4,7 @@
4 4
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
5 5
         "This file is @generated automatically"
6 6
     ],
7
-    "content-hash": "4965262916e04d361db07e7f14ed06d6",
7
+    "content-hash": "0ca1975ebb0ba7d9d480257323a5d727",
8 8
     "packages": [
9 9
         {
10 10
             "name": "beberlei/assert",
@@ -520,6 +520,82 @@
520 520
             ],
521 521
             "time": "2017-03-13T16:27:32+00:00"
522 522
         },
523
+        {
524
+            "name": "phpmailer/phpmailer",
525
+            "version": "v5.2.23",
526
+            "source": {
527
+                "type": "git",
528
+                "url": "https://github.com/PHPMailer/PHPMailer.git",
529
+                "reference": "7115df4a6f76281109ebe352900c42403b728bb4"
530
+            },
531
+            "dist": {
532
+                "type": "zip",
533
+                "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/7115df4a6f76281109ebe352900c42403b728bb4",
534
+                "reference": "7115df4a6f76281109ebe352900c42403b728bb4",
535
+                "shasum": ""
536
+            },
537
+            "require": {
538
+                "php": ">=5.0.0"
539
+            },
540
+            "require-dev": {
541
+                "doctrine/annotations": "1.2.*",
542
+                "jms/serializer": "0.16.*",
543
+                "phpdocumentor/phpdocumentor": "2.*",
544
+                "phpunit/phpunit": "4.8.*",
545
+                "symfony/debug": "2.8.*",
546
+                "symfony/filesystem": "2.8.*",
547
+                "symfony/translation": "2.8.*",
548
+                "symfony/yaml": "2.8.*",
549
+                "zendframework/zend-cache": "2.5.1",
550
+                "zendframework/zend-config": "2.5.1",
551
+                "zendframework/zend-eventmanager": "2.5.1",
552
+                "zendframework/zend-filter": "2.5.1",
553
+                "zendframework/zend-i18n": "2.5.1",
554
+                "zendframework/zend-json": "2.5.1",
555
+                "zendframework/zend-math": "2.5.1",
556
+                "zendframework/zend-serializer": "2.5.*",
557
+                "zendframework/zend-servicemanager": "2.5.*",
558
+                "zendframework/zend-stdlib": "2.5.1"
559
+            },
560
+            "suggest": {
561
+                "league/oauth2-google": "Needed for Google XOAUTH2 authentication"
562
+            },
563
+            "type": "library",
564
+            "autoload": {
565
+                "classmap": [
566
+                    "class.phpmailer.php",
567
+                    "class.phpmaileroauth.php",
568
+                    "class.phpmaileroauthgoogle.php",
569
+                    "class.smtp.php",
570
+                    "class.pop3.php",
571
+                    "extras/EasyPeasyICS.php",
572
+                    "extras/ntlm_sasl_client.php"
573
+                ]
574
+            },
575
+            "notification-url": "https://packagist.org/downloads/",
576
+            "license": [
577
+                "LGPL-2.1"
578
+            ],
579
+            "authors": [
580
+                {
581
+                    "name": "Jim Jagielski",
582
+                    "email": "jimjag@gmail.com"
583
+                },
584
+                {
585
+                    "name": "Marcus Bointon",
586
+                    "email": "phpmailer@synchromedia.co.uk"
587
+                },
588
+                {
589
+                    "name": "Andy Prevost",
590
+                    "email": "codeworxtech@users.sourceforge.net"
591
+                },
592
+                {
593
+                    "name": "Brent R. Matzelle"
594
+                }
595
+            ],
596
+            "description": "PHPMailer is a full-featured email creation and transfer class for PHP",
597
+            "time": "2017-03-15T19:32:56+00:00"
598
+        },
523 599
         {
524 600
             "name": "psr/http-message",
525 601
             "version": "1.0.1",

+ 4
- 1
index.php View File

@@ -40,7 +40,10 @@ if ($VARS['progress'] == "1") {
40 40
                     $username_ok = true;
41 41
                     break;
42 42
                 case "ALERT_ON_ACCESS":
43
-                    sendLoginAlertEmail($VARS['username']);
43
+                    $mail_resp = sendLoginAlertEmail($VARS['username']);
44
+                    if (DEBUG) {
45
+                        var_dump($mail_resp);
46
+                    }
44 47
                     $username_ok = true;
45 48
                     break;
46 49
                 default:

+ 3
- 1
lang/en_us.php View File

@@ -51,5 +51,7 @@ $STRINGS = [
51 51
     "open app" => "Open App",
52 52
     "sign in again" => "Please sign in again to continue.",
53 53
     "login failed try on web" => "There is a problem with your account. Visit Portal via a web browser for more information.",
54
-    "mobile login disabled" => "Mobile login has been disabled by your system administrator.  Contact technical support for more information."
54
+    "mobile login disabled" => "Mobile login has been disabled by your system administrator.  Contact technical support for more information.",
55
+    "admin alert email subject" => "Alert: User login notification",
56
+    "admin alert email message" => "You (or another administrator) requested to be notified when user \"{username}\" logged in, an event which happened just now.\r\n\r\nUsername: {username}\r\nDate/Time: {datetime}\r\nIP address: {ipaddr}",
55 57
 ];

+ 43
- 1
lib/login.php View File

@@ -280,9 +280,51 @@ function doLoginUser($username, $password) {
280 280
  * 
281 281
  * Used when an account with the status ALERT_ON_ACCESS logs in
282 282
  * @param String $username the account username
283
+ * @return Mixed TRUE if successful, error string if not
283 284
  */
284 285
 function sendLoginAlertEmail($username) {
285
-    // TODO: add email code
286
+    if (is_empty(ADMIN_EMAIL) || filter_var(ADMIN_EMAIL, FILTER_VALIDATE_EMAIL) === FALSE) {
287
+        return "false";
288
+    }
289
+    if (is_empty(FROM_EMAIL) || filter_var(FROM_EMAIL, FILTER_VALIDATE_EMAIL) === FALSE) {
290
+        return "false";
291
+    }
292
+
293
+    $mail = new PHPMailer;
294
+
295
+    if (DEBUG) {
296
+        $mail->SMTPDebug = 2;
297
+    }
298
+
299
+    if (USE_SMTP) {
300
+        $mail->isSMTP();
301
+        $mail->Host = SMTP_HOST;
302
+        $mail->SMTPAuth = SMTP_AUTH;
303
+        $mail->Username = SMTP_USER;
304
+        $mail->Password = SMTP_PASS;
305
+        $mail->SMTPSecure = SMTP_SECURE;
306
+        $mail->Port = SMTP_PORT;
307
+        if (SMTP_ALLOW_INVALID_CERTIFICATE) {
308
+            $mail->SMTPOptions = array(
309
+                'ssl' => array(
310
+                    'verify_peer' => false,
311
+                    'verify_peer_name' => false,
312
+                    'allow_self_signed' => true
313
+                )
314
+            );
315
+        }
316
+    }
317
+
318
+    $mail->setFrom(FROM_EMAIL, 'Account Alerts');
319
+    $mail->addAddress(ADMIN_EMAIL, "System Admin");
320
+    $mail->isHTML(false);
321
+    $mail->Subject = lang("admin alert email subject", false);
322
+    $mail->Body = lang2("admin alert email message", ["username" => $username, "datetime" => date("Y-m-d H:i:s"), "ipaddr" => getClientIP()], false);
323
+
324
+    if (!$mail->send()) {
325
+        return $mail->ErrorInfo;
326
+    }
327
+    return TRUE;
286 328
 }
287 329
 
288 330
 function insertAuthLog($type, $uid = null, $data = "") {

+ 12
- 0
settings.template.php View File

@@ -68,6 +68,18 @@ define('QWIKCLOCK_HOME', '/qwikclock/app.php');
68 68
 // See lang folder for language options
69 69
 define('LANGUAGE', "en_us");
70 70
 
71
+// Email settings for receiving admin alerts.
72
+define("USE_SMTP", TRUE); // if FALSE, will use PHP's mail() instead
73
+define("ADMIN_EMAIL", "");
74
+define("FROM_EMAIL", "portal-noreply@apps.biz.netsyms.com");
75
+define("SMTP_HOST", "");
76
+define("SMTP_AUTH", true);
77
+define("SMTP_PORT", 587);
78
+define("SMTP_SECURE", 'tls');
79
+define("SMTP_USER", "");
80
+define("SMTP_PASS", "");
81
+define("SMTP_ALLOW_INVALID_CERTIFICATE", TRUE);
82
+
71 83
 // Minimum length for new passwords
72 84
 // The system checks new passwords against the 500 worst passwords and rejects
73 85
 // any matches.

Loading…
Cancel
Save