Novi izvorni kod change_pass dodatka (inacica 3.0).
[squirrelmail-change-pass-cn.git] / change_pass / options.php
index 9343685..7649a3c 100644 (file)
 <?php
-    /* change_pass
-     *
-     * $Id: options.php,v 1.5 2004/07/21 14:28:46 indiri69 Exp $
-     */
-
-    define('SM_PATH', '../../');
-    require_once (SM_PATH . 'include/validate.php');
-    require_once (SM_PATH . 'functions/page_header.php');
-    require_once (SM_PATH . 'functions/imap.php');
-    require_once (SM_PATH . 'functions/prefs.php');
-    require_once (SM_PATH . 'functions/global.php');
-    require_once (SM_PATH . 'functions/i18n.php');
-
-    sqgetGlobalVar('plugin_change_pass', $plugin_change_pass, SQ_POST);
-
-    $messages = array();
-    if (isset($plugin_change_pass)) {
-        sqgetGlobalVar('cp_oldpass', $cp_oldpass, SQ_POST);
-        sqgetGlobalVar('cp_newpass', $cp_newpass, SQ_POST);
-        sqgetGlobalVar('cp_verify',  $cp_verify,  SQ_POST);
-        bindtextdomain('change_pass', SM_PATH . 'plugins/change_pass/locale');
-        textdomain('change_pass');
-        $messages = change_pass_check();
-        bindtextdomain('squirrelmail', SM_PATH . 'locale');
-        textdomain('squirrelmail');
-    }
 
-    displayPageHeader($color, 'None');
+/*
+ * Licensed under the GNU GPL. For full terms see the file COPYING.
+ *
+ * $Id: options.php,v 1.7 2009/05/05 03:09:22 indiri69 Exp $
+ */
 
-    bindtextdomain('change_pass', SM_PATH . 'plugins/change_pass/locale');
-    textdomain('change_pass');
-?>
-    <table width="100%" cellpadding="2" cellspacing="2" border="0">
-        <tr><td bgcolor="<?php echo $color[0] ?>">
-                <center><b><?php echo _("Change Password") ?></b></center>
-        </td></tr>
-        <tr><td>
-<?php
-    if (count($messages)) {
-        foreach ($messages as $line) {
-            echo htmlspecialchars($line) . "<br>\n";
-        }
-        echo "</td></tr>\n";
-        echo "<tr><td>\n";
-    }
-?>
-            <form method="post" action="<?php echo $PHP_SELF ?>">
-            <table align="center">
-<?php
-    $values = array(_("Old Password")        => 'cp_oldpass',
-                    _("New Password")        => 'cp_newpass',
-                    _("Verify New Password") => 'cp_verify');
-    foreach($values as $key=>$value) {
-?>
-                <tr>
-                    <th align="right"><?php echo $key; ?></th>
-                    <td>
-                        <input type="password" name="<?php echo $value; ?>" size="20">
-                    </td>
-                </tr>
-<?php } ?>
-                <tr><td align="center" colspan="2">
-                    <input type="submit" value=<?php echo _("Submit"); ?> name="plugin_change_pass">
-                </td></tr>
-            </table>
-            </form>
-        </td></tr>
-    </table>
-</body></html>
-<?php
-    bindtextdomain('squirrelmail', SM_PATH . 'locale');
-    textdomain('squirrelmail');
+define('SM_PATH', '../../');
+require_once(SM_PATH . 'include/validate.php');
+require_once(SM_PATH . 'functions/html.php');
+require_once(SM_PATH . 'functions/forms.php');
 
-function change_pass_closeport($pop_socket, &$messages, $debug) {
-    if ($debug) {
-        array_push($messages, _("Closing Connection"));
-    }
-    fputs($pop_socket, "quit\r\n");
-    fclose($pop_socket);
-}
+$debug = false;
 
-function change_pass_readfb($pop_socket, &$result, &$messages, $debug) {
-   $strResp = '';
-   $result  = '';
-
-   if (!feof($pop_socket)) {
-      $strResp = fgets($pop_socket, 1024);
-      $result  = substr(trim($strResp), 0, 3);  // 200, 500
-      if(!preg_match('/^[23]\d\d/', $result) || $debug) {
-          $messages[] = "--> $strResp";
-      }
-   }
+// Make sure the plugin is activated
+global $plugins;
+if(!in_array('change_pass', $plugins)) {
+    exit;
 }
 
-function change_pass_check($debug = 0) {
-    global $cp_oldpass, $cp_newpass, $cp_verify;
-    global $plugin_change_pass;
-
-    sqgetGlobalVar('key', $key, SQ_COOKIE);
-    sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
-    $messages = array();
-    $password = OneTimePadDecrypt($key, $onetimepad);
+$min_pass_length = 0;
+$max_pass_length = 99999999;
 
-    if ($cp_oldpass == '') {
-        $messages[] = _("You must type in your old password.");
-    }
-    if ($cp_newpass == '') {
-        $messages[] = _("You must type in a new password.");
-    }
-    if ($cp_verify  == '') {
-        $messages[] = _("You must also type in your new password in the verify box.");
-    }
-    if ($cp_newpass != '' && ($cp_verify != $cp_newpass)) {
-        $messages[] = _("Your new password doesn't match the verify password.");
-    }
-    if ($cp_oldpass != '' && ($cp_oldpass != $password)) {
-        $messages[] = _("Your old password is not correct.");
-    }
-    if (count($messages)) {
-        $return = $messages;
-    } else {
-        $return = change_pass_go($password, $debug);
-    }
-    return $return;
+if (!@include(SM_PATH . 'config/config_change_pass.php')) {
+     @include(SM_PATH . 'plugins/change_pass/config.php');
 }
 
-function change_pass_go($password, $debug) {
-    include_once(SM_PATH . 'plugins/change_pass/settings.php');
+include_once(SM_PATH . 'plugins/change_pass/functions.php');
 
-    global $username;
-    global $cp_newpass, $key, $onetimepad;
+global $color;
+sqgetGlobalVar('change_pass_form', $change_pass_form, SQ_POST);
 
-    sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
-    $messages = array();
+$error_messages = array();
+$showform = true;
 
-    if ($debug) {
-        $messages[] = _("Connecting to Password Server");
-    }
-    $pop_socket = fsockopen($poppass_server, $poppass_port, $errno, $errstr);
-    if (!$pop_socket) {
-        $messages[] = _("ERROR:") .  "$errstr ($errno)";
-        return $messages;
-    }
+if (isset($change_pass_form)) {
+    sq_change_text_domain('change_pass');
+    sqgetGlobalVar('change_pass_old', $change_pass_old, SQ_POST);
+    sqgetGlobalVar('change_pass_new', $change_pass_new, SQ_POST);
+    sqgetGlobalVar('change_pass_verify', $change_pass_verify, SQ_POST);
 
-    change_pass_readfb($pop_socket, $result, $messages, $debug);
-    if(!preg_match('/^2\d\d/', $result) ) {
-        change_pass_closeport($pop_socket, $messages, $debug);
-        return $messages;
+    if(!isset($change_pass_old) || $change_pass_old == '') {
+        $error_messages['cp_no_old'] = _("You must type in your current password.");
     }
 
-    fputs($pop_socket, "user $username\r\n");
-    change_pass_readfb($pop_socket, $result, $messages, $debug);
-    if(!preg_match('/^[23]\d\d/', $result) ) {
-        change_pass_closeport($pop_socket, $messages, $debug);
-        return $messages;
+    if(!isset($change_pass_new) || $change_pass_new == '') {
+        $error_messages['cp_no_new'] = _("You must type in a new password.");
     }
 
-    fputs($pop_socket, "pass $password\r\n");
-    change_pass_readfb($pop_socket, $result, $messages, $debug);
-    if(!preg_match('/^[23]\d\d/', $result) ) {
-        change_pass_closeport($pop_socket, $messages, $debug);
-        return $messages;
+    if(!isset($change_pass_verify) || $change_pass_verify == '') {
+        $error_messages['cp_no_verify'] = _("You must also type in your new password in the verify box.");
     }
 
-    fputs($pop_socket, "newpass $cp_newpass\r\n");
-    change_pass_readfb($pop_socket, $result, $messages, $debug);
-    change_pass_closeport($pop_socket, $messages, $debug);
-    if(!preg_match('/^2\d\d/', $result) ) {
-        return $messages;
+    if(!isset($error_messages['cp_no_new']) && !isset($error_messages['cp_no_verify'])) {
+        if($change_pass_new != $change_pass_verify) {
+            $error_messages['cp_new_mismatch'] = _("Your new password does not match the verify password.");
+        } else {
+            if (strlen($change_pass_new) < $min_pass_length ||
+                strlen($change_pass_new) > $max_pass_length) {
+                    $error_messages[] = sprintf(_("Your new password should be %s to %s characters long."),
+                                                $min_pass_length, $max_pass_length);
+            }
+        }
     }
 
-    $messages[] = _("Password changed successfully.");
+    $old_pass = sqauth_read_password();
+    if(!isset($error_messages['cp_no_old']) && $change_pass_old != $old_pass) {
+        $error_messages['cp_wrong_old'] = _("Your current password is not correct.");
+    }
 
-    // Write new cookies for the password
-    $onetimepad = OneTimePadCreate(strlen($cp_newpass));
-    $key = OneTimePadEncrypt($cp_newpass, $onetimepad);
-    sqsession_register($onetimepad, 'onetimepad');
-    setcookie('key', $key, 0, $base_uri);
+    if(count($error_messages) == 0) {
+        $error_messages = change_pass_dochange($change_pass_old, $change_pass_new, $debug);
+        if(count($error_messages) == 0) {
+            $showform = false;
+        }
+    }
+    sq_change_text_domain('squirrelmail');
+}
+displayPageHeader($color, '');
+sq_change_text_domain('change_pass');
+
+echo
+    html_tag('table', "\n" .
+        html_tag('tr', "\n" .
+            html_tag('td', '<b>' . _("Change Password") . '</b>', 'center', $color[0])
+        ),
+        'center', $color[9], 'width="95%" border="0" cellpadding="1" cellspacing="0"') . "<br>\n";
+
+if(count($error_messages) > 0) {
+    echo html_tag('table', '', 'center', '', 'width="100%" border="0" cellpadding="1" cellspacing="0"');
+    echo html_tag('tr');
+    echo html_tag('td', '', 'center');
+    echo html_tag('ul');
+    foreach($error_messages as $line) {
+        echo html_tag('li', htmlspecialchars($line), '', '', 'style="color: ' . $color[2] . '"');
+    }
+    echo html_tag('/ul');
+    echo html_tag('tr', html_tag('td', '&nbsp;')) . "\n";
+    echo html_tag('/table');
+}
 
-    return $messages;
+if($showform) {
+    echo addForm($PHP_SELF);
+    echo
+        html_tag('table', "\n" .
+            html_tag('tr', "\n" .
+                html_tag('td', _("Current Password:"), 'right') .
+                html_tag('td', addPwField('change_pass_old', ''), 'left')
+            ) .
+            html_tag('tr', "\n" .
+                html_tag('td', _("New Password:"), 'right') .
+                html_tag('td', addPwField('change_pass_new', ''), 'left')
+            ) .
+            html_tag('tr', "\n" .
+                html_tag('td', _("Verify New Password:"), 'right') .
+                html_tag('td', addPwField('change_pass_verify', ''), 'left')
+            ) .
+            html_tag('tr', "\n" .
+                html_tag('td', addSubmit(_('Change Password'), 'change_pass_form'), 'center', '', 'colspan="2"')
+            ),
+            'center', '', 'border="0" cellpadding="1" cellspacing="0"') . "\n";
+    echo html_tag('/form');
+    echo html_tag('/body');
+    echo html_tag('/html');
 }
-?>
+sq_change_text_domain('squirrelmail');