]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/commitdiff
Add email support
authorValerie Pond <redacted>
Tue, 7 Feb 2023 00:53:50 +0000 (00:53 +0000)
committerValerie Pond <redacted>
Tue, 7 Feb 2023 00:53:50 +0000 (00:53 +0000)
Currently this will only email itself when there was a login to the admin panel.

Also when there is a failed login attempt.

Classes/class-hook.php
config.php.sample
login/index.php
plugins/php_mailer/php_mailer.php [new file with mode: 0644]

index 26c84eebef0bb2a558d52c6fbcfbd962c5bd29d6..8efb26b123c1bf04a8d98a97fbb8ccf3b1f7e360 100644 (file)
@@ -124,6 +124,10 @@ define('HOOKTYPE_USER_CREATE', 111);
 define('HOOKTYPE_GET_USER_LIST', 112);
 
 define('HOOKTYPE_USER_DELETE', 113);
+
+define('HOOKTYPE_USER_LOGIN', 114);
+
+define('HOOKTYPE_USER_LOGIN_FAIL', 115);
 /** 
  *  Class for "Hook"
  * This is the main function which gets called whenever you want to use a Hook.
index 6726eb846ed634231fd05ca03336ca027851f8e9..f7d99a58ec93f60bde097d1c04b91bebd7bcddec 100644 (file)
@@ -58,6 +58,7 @@ define(
                */
                //"example_plugin", /* An example plugin */
                //"sql_auth", /* Provides a login page which uses SQL */
+               //"php_mailer", /* Provides ability to send emails on behalf of the admin panel */
        ]
 );
 
@@ -118,3 +119,16 @@ define('DNSBL', [
        "dnsbl.dronebl.org",
        "rbl.efnetrbl.org"
 ]);
+
+/** Your email settings
+ * Requires plugin "php_mailer"
+*/
+define('EMAIL_SETTINGS', [
+    "host" => 'smtp.host.example.com',
+    "port" => 587,
+    "encryption" => 'tls', /* Use 'tls' for STARTTLS or 'ssl' for TLS/SSL
+    "username" => "v.a.pond@outlook.com", /* The email to login with and send emails from */
+    "password" => "supersecretpassword", /* The password for the above email */
+    "from_name" => "UnrealIRCd Admin Panel",
+
+]);
\ No newline at end of file
index 95c33267e7b0cb00cd8e4b5e3dea6db5234585c8..ea24e29596bf8bb469c4acc187e9f27c7df4a764 100644 (file)
@@ -24,18 +24,21 @@ if (!empty($_POST))
                $user = new PanelUser($_POST['username']);
                
                /* not being too informative with the login error in case of attackers */
-               if (!$user->id)
-               {
-                       $failmsg = "Incorrect login";
-               }
-               else if ($user->password_verify($_POST['password']))
+               if (isset($user->id) && $user->password_verify($_POST['password']))
                {
                        $_SESSION['id'] = $user->id;
                        header('Location: ' . $redirect);
                        $user->add_meta("last_login", date("Y-m-d m:i:s"));
+                       Hook::run(HOOKTYPE_USER_LOGIN, $user);
+                       die();
                }
                else
                {
+                       $fail = [
+                               "login" => htmlspecialchars($_POST['username']),
+                               "IP" => $_SERVER['REMOTE_ADDR']
+                       ];
+                       Hook::run(HOOKTYPE_USER_LOGIN_FAIL, $fail);
                        $failmsg = "Incorrect login";
                }
 
diff --git a/plugins/php_mailer/php_mailer.php b/plugins/php_mailer/php_mailer.php
new file mode 100644 (file)
index 0000000..24f5de4
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+
+use PHPMailer\PHPMailer\PHPMailer;
+use PHPMailer\PHPMailer\SMTP;
+
+class php_mailer
+{
+       public $name = "PHPMailer()";
+       public $author = "Valware";
+       public $version = "1.0";
+       public $description = "Send mail using PHPMailer()";
+
+       function __construct()
+       {
+               Hook::func(HOOKTYPE_USER_LOGIN, 'php_mailer::user_login_notif');
+               Hook::func(HOOKTYPE_USER_LOGIN_FAIL, 'php_mailer::user_login_fail_notif');
+       }
+
+       public static function send_mail($to, $subject, $body)
+       {
+               $mail = new PHPMailer(true);
+               try {
+                       //Server settings
+                       //$mail->SMTPDebug = 2;
+                       $mail->isSMTP();                                            //Send using SMTP
+                       $mail->Host       = EMAIL_SETTINGS['host'];                     //Set the SMTP server to send through
+                       $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
+                       $mail->Username   = EMAIL_SETTINGS['username'];                     //SMTP username
+                       $mail->Password = EMAIL_SETTINGS['password'];                               //SMTP password
+                       $mail->SMTPSecure = EMAIL_SETTINGS['encryption'];            //Enable implicit TLS encryption
+                       $mail->Port       = EMAIL_SETTINGS['port'];                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
+
+                       //Recipients
+                       $mail->setFrom(EMAIL_SETTINGS['username'], EMAIL_SETTINGS['from_name']);
+                       $mail->addAddress($to['email'], $to['name']);     //Add a recipient
+
+                       //Content
+                       $mail->isHTML(true);                                  //Set email format to HTML
+                       $mail->Subject = $subject;
+                       $mail->Body    = $body . "<br><br>Thank you for using UnrealIRCd!";
+                       $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
+
+                       $mail->send();
+                       echo 'Message has been sent';
+               } catch (Exception $e) {
+                       echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
+               }
+       }
+
+       /**
+        *  Send a login notification to the admin (note-to-self)
+        * @param mixed $user
+        * @return void
+        */
+       public static function user_login_notif($user)
+       {
+               self::send_mail(
+                       ["email" => EMAIL_SETTINGS['username'], "name" => EMAIL_SETTINGS['from_name']],
+                       "New login to Unreal Admin Panel",
+                       "There was a new login to the admin panel.<br>User: \"$user->username\"<br>IP: \"".$_SERVER['REMOTE_ADDR']."\""
+               );
+       }
+       public static function user_login_fail_notif($fail)
+       {
+               self::send_mail(
+                       ["email" => EMAIL_SETTINGS['username'], "name" => EMAIL_SETTINGS['from_name']],
+                       "Failed login attempt - Unreal Admin Panel",
+                       "There was a failed login attempt to the admin panel.<br>User: \"".$fail['login']."\"<br>IP: \"".$fail['IP']."\""
+               );
+       }
+}
\ No newline at end of file