]> git.wh0rd.org - tt-rss.git/blobdiff - classes/pref/prefs.php
add support for plugins in prefs
[tt-rss.git] / classes / pref / prefs.php
index 705b709faed9a98ba048c9c0e7bc9145d8a7e36f..013d589ca814208077b59015e09879e1bada0754 100644 (file)
@@ -154,12 +154,15 @@ class Pref_Prefs extends Handler_Protected {
 
                print "<table width=\"100%\" class=\"prefPrefsList\">";
 
-               $result = db_query($this->link, "SELECT email,full_name,
+               print "<h2>" . __("Personal data") . "</h2>";
+
+               $result = db_query($this->link, "SELECT email,full_name,otp_enabled,
                        access_level FROM ttrss_users
                        WHERE id = ".$_SESSION["uid"]);
 
                $email = htmlspecialchars(db_fetch_result($result, 0, "email"));
                $full_name = htmlspecialchars(db_fetch_result($result, 0, "full_name"));
+               $otp_enabled = sql_bool_to_bool(db_fetch_result($result, 0, "otp_enabled"));
 
                print "<tr><td width=\"40%\">".__('Full name')."</td>";
                print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" name=\"full_name\" required=\"1\"
@@ -194,6 +197,8 @@ class Pref_Prefs extends Handler_Protected {
 
                if ($authenticator && method_exists($authenticator, "change_password")) {
 
+                       print "<h2>" . __("Password") . "</h2>";
+
                        $result = db_query($this->link, "SELECT id FROM ttrss_users
                                WHERE id = ".$_SESSION["uid"]." AND pwd_hash
                                = 'SHA1:5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'");
@@ -225,6 +230,10 @@ class Pref_Prefs extends Handler_Protected {
                        }
                        </script>";
 
+                       if ($otp_enabled) {
+                               print_notice("Changing your current password will disable OTP.");
+                       }
+
                        print "<table width=\"100%\" class=\"prefPrefsList\">";
 
                        print "<tr><td width=\"40%\">".__("Old password")."</td>";
@@ -249,6 +258,112 @@ class Pref_Prefs extends Handler_Protected {
 
                        print "</form>";
 
+                       if ($_SESSION["auth_module"] == "internal") {
+
+                               print "<h2>" . __("One time passwords / Authenticator") . "</h2>";
+
+                               if ($otp_enabled) {
+
+                                       print_notice("One time passwords are currently enabled. Enter your current password below to disable.");
+
+                                       print "<form dojoType=\"dijit.form.Form\">";
+
+                               print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
+                               evt.preventDefault();
+                               if (this.validate()) {
+                                       notify_progress('Disabling OTP', true);
+
+                                       new Ajax.Request('backend.php', {
+                                               parameters: dojo.objectToQuery(this.getValues()),
+                                               onComplete: function(transport) {
+                                                       notify('');
+                                                       if (transport.responseText.indexOf('ERROR: ') == 0) {
+                                                               notify_error(transport.responseText.replace('ERROR: ', ''));
+                                                       } else {
+                                                               window.location.reload();
+                                                       }
+                                       }});
+                                       this.reset();
+                               }
+                               </script>";
+
+                               print "<table width=\"100%\" class=\"prefPrefsList\">";
+
+                               print "<tr><td width=\"40%\">".__("Enter your password")."</td>";
+
+                               print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\"
+                                       name=\"password\"></td></tr>";
+
+                               print "</table>";
+
+                               print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-prefs\">";
+                               print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"otpdisable\">";
+
+                               print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
+                                       __("Disable OTP")."</button>";
+
+                               print "</form>";
+
+                               } else {
+
+                                       print "<p>".__("You will need a compatible Authenticator to use this. Changing your password would automatically disable OTP.") . "</p>";
+
+                                       print "<p>".__("Scan the following code by the Authenticator application:")."</p>";
+
+                                       $csrf_token = $_SESSION["csrf_token"];
+
+                                       print "<img src=\"backend.php?op=pref-prefs&method=otpqrcode&csrf_token=$csrf_token\">";
+
+                                       print "<form dojoType=\"dijit.form.Form\" id=\"changeOtpForm\">";
+
+                                       print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-prefs\">";
+                                       print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"otpenable\">";
+
+                                       print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
+                                       evt.preventDefault();
+                                       if (this.validate()) {
+                                               notify_progress('Saving data...', true);
+
+                                               new Ajax.Request('backend.php', {
+                                                       parameters: dojo.objectToQuery(this.getValues()),
+                                                       onComplete: function(transport) {
+                                                               notify('');
+                                                               if (transport.responseText.indexOf('ERROR: ') == 0) {
+                                                                       notify_error(transport.responseText.replace('ERROR: ', ''));
+                                                               } else {
+                                                                       window.location.reload();
+                                                               }
+                                               } });
+
+                                       }
+                                       </script>";
+
+                                       print "<table width=\"100%\" class=\"prefPrefsList\">";
+
+                                       print "<tr><td width=\"40%\">".__("Enter your password")."</td>";
+
+                                       print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" type=\"password\" required=\"1\"
+                                               name=\"password\"></td></tr>";
+
+                                       print "<tr><td colspan=\"2\">";
+
+                                       print "<input dojoType=\"dijit.form.CheckBox\" required=\"1\"
+                                               type=\"checkbox\" id=\"enable_otp\" name=\"enable_otp\"/> ";
+                                       print "<label for=\"enable_otp\">".__("I have scanned the code and would like to enable OTP")."</label>";
+
+                                       print "</td></tr><tr><td colspan=\"2\">";
+
+                                       print "</td></tr>";
+                                       print "</table>";
+
+                                       print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
+                                               __("Enable OTP")."</button>";
+
+                                       print "</form>";
+
+                               }
+
+                       }
                }
 
                print "</div>"; #pane
@@ -535,6 +650,10 @@ class Pref_Prefs extends Handler_Protected {
                        print "</div>"; #pane
                }
 
+               global $pluginhost;
+               $pluginhost->run_hooks($pluginhost::HOOK_PREFS_TAB,
+                       "hook_prefs_tab", "prefPrefs");
+
                print "</div>"; #container
        }
 
@@ -571,5 +690,66 @@ class Pref_Prefs extends Handler_Protected {
        function toggleAdvanced() {
                $_SESSION["prefs_show_advanced"] = !$_SESSION["prefs_show_advanced"];
        }
+
+       function otpqrcode() {
+               require_once "lib/otphp/vendor/base32.php";
+               require_once "lib/otphp/lib/otp.php";
+               require_once "lib/otphp/lib/totp.php";
+               require_once "lib/phpqrcode/phpqrcode.php";
+
+               $result = db_query($this->link, "SELECT login,salt,otp_enabled
+                       FROM ttrss_users
+                       WHERE id = ".$_SESSION["uid"]);
+
+               $base32 = new Base32();
+
+               $login = db_fetch_result($result, 0, "login");
+               $otp_enabled = sql_bool_to_bool(db_fetch_result($result, 0, "otp_enabled"));
+
+               if (!$otp_enabled) {
+                       $secret = $base32->encode(sha1(db_fetch_result($result, 0, "salt")));
+                       $topt = new \OTPHP\TOTP($secret);
+                       print QRcode::png($topt->provisioning_uri($login));
+               }
+       }
+
+       function otpenable() {
+               $password = db_escape_string($_REQUEST["password"]);
+
+               $module_class = "auth_" . $_SESSION["auth_module"];
+               $authenticator = new $module_class($this->link);
+               $enable_otp = $_REQUEST["enable_otp"] == "on";
+
+               if ($authenticator->check_password($_SESSION["uid"], $password)) {
+
+                       if ($enable_otp) {
+                               db_query($this->link, "UPDATE ttrss_users SET otp_enabled = true WHERE
+                                       id = " . $_SESSION["uid"]);
+
+                               print "OK";
+                       }
+               } else {
+                       print "ERROR: ".__("Incorrect password");
+               }
+
+       }
+
+       function otpdisable() {
+               $password = db_escape_string($_REQUEST["password"]);
+
+               $module_class = "auth_" . $_SESSION["auth_module"];
+               $authenticator = new $module_class($this->link);
+
+               if ($authenticator->check_password($_SESSION["uid"], $password)) {
+
+                       db_query($this->link, "UPDATE ttrss_users SET otp_enabled = false WHERE
+                               id = " . $_SESSION["uid"]);
+
+                       print "OK";
+               } else {
+                       print "ERROR: ".__("Incorrect password");
+               }
+
+       }
 }
 ?>