[phpBB Debug] PHP Warning: in file [ROOT]/includes/crs/crs_misc_functions.php on line 37: mime_content_type(): Empty filename or path
[phpBB Debug] PHP Warning: in file [ROOT]/includes/crs/crs_misc_functions.php on line 37: mime_content_type(): Empty filename or path
Zen Cart 源代码 class.zcPassword.php

Zen Cart 源代码 class.zcPassword.php




下载文件

文件名: class.zcPassword.php
文件类型: PHP文件
文件大小: 6.91 KiB
MD5: 35aa2079d3cb0b9972026e4d91d5ecfc

class.zcPassword.php - 关闭高亮
  1. <?php
  2. /**
  3.  * File contains just the zcPassword class
  4.  *
  5.  * @package classes
  6.  * @copyright Copyright 2003-2014 Zen Cart Development Team
  7.  * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
  8.  * @version GIT: $Id: Author: wilt  Modified in v1.5.4 $
  9.  */
  10. /**
  11.  * class zcPassword
  12.  *
  13.  * helper class for managing password hashing for different PHP versions
  14.  *
  15.  * Updates admin/customer tables on successful login
  16.  * For php < 5.3.7 uses custom code to create hashes using SHA256 and longer salts
  17.  * For php >= 5.3.7 and < 5.5.0 uses https://github.com/ircmaxell/PHP-PasswordLib
  18.  * For php >= 5.5.0 uses inbuilt php functions
  19.  *
  20.  * @package classes
  21.  */
  22. class zcPassword extends base
  23. {
  24.   /**
  25.    *
  26.    * @var $instance object
  27.    */
  28.   protected static $instance = null;
  29.   /**
  30.    * enforce singleton
  31.    *
  32.    * @param string $phpVersion
  33.    */
  34.   public static function getInstance($phpVersion)
  35.   {
  36.     if (! self::$instance) {
  37.       $class = __CLASS__;
  38.       self::$instance = new $class($phpVersion);
  39.     }
  40.     return self::$instance;
  41.   }
  42.   /**
  43.    * constructor
  44.    *
  45.    * @param string $phpVersion
  46.    */
  47.   public function __construct($phpVersion = PHP_VERSION)
  48.   {
  49.     if (version_compare($phpVersion, '5.3.7', '<')) {
  50.       require_once (realpath(dirname(__FILE__)) . '/../functions/password_compat.php');
  51.     } elseif (version_compare($phpVersion, '5.5.0', '<')) {
  52.       require_once (realpath(dirname(__FILE__)) . '/vendors/password_compat-master/lib/password.php');
  53.     }
  54.   }
  55.   /**
  56.    * Determine the password type
  57.    *
  58.    * Legacy passwords were hash:salt with a salt of length 2
  59.    * php < 5.3.7 updated passwords are hash:salt with salt of length > 2
  60.    * php >= 5.3.7 passwords are BMCF format
  61.    *
  62.    * @param string $encryptedPassword
  63.    * @return string
  64.    */
  65.   function detectPasswordType($encryptedPassword)
  66.   {
  67.     $type = 'unknown';
  68.     $tmp = explode(':', $encryptedPassword);
  69.     if (count($tmp) == 2) {
  70.       if (strlen($tmp [1]) > 2) {
  71.         $type = 'compatSha256';
  72.       } elseif (strlen($tmp [1]) == 2) {
  73.         $type = 'oldMd5';
  74.       }
  75.     }
  76.     return $type;
  77.   }
  78.   /**
  79.    * validate a password where format is unknown
  80.    *
  81.    * @param string $plain
  82.    * @param string $encrypted
  83.    * @return boolean
  84.    */
  85.   public function validatePassword($plain, $encrypted)
  86.   {
  87.     $type = $this->detectPasswordType($encrypted);
  88.     if ($type != 'unknown') {
  89.       $method = 'validatePassword' . ucfirst($type);
  90.       return $this->{$method}($plain, $encrypted);
  91.     }
  92.     $result = password_verify($plain, $encrypted);
  93.     return $result;
  94.   }
  95.   /**
  96.    * validate a legacy md5 type password
  97.    *
  98.    * @param string $plain
  99.    * @param string $encrypted
  100.    * @return boolean
  101.    */
  102.   public function validatePasswordOldMd5($plain, $encrypted)
  103.   {
  104.     if (zen_not_null($plain) && zen_not_null($encrypted)) {
  105.       $stack = explode(':', $encrypted);
  106.       if (sizeof($stack) != 2)
  107.         return false;
  108.       if (md5($stack [1] . $plain) == $stack [0]) {
  109.         return true;
  110.       }
  111.     }
  112.     return false;
  113.   }
  114.   /**
  115.    * validate a SHA256 type password
  116.    *
  117.    * @param string $plain
  118.    * @param string $encrypted
  119.    * @return boolean
  120.    */
  121.   public function validatePasswordCompatSha256($plain, $encrypted)
  122.   {
  123.     if (zen_not_null($plain) && zen_not_null($encrypted)) {
  124.       $stack = explode(':', $encrypted);
  125.       if (sizeof($stack) != 2)
  126.         return false;
  127.       if (hash('sha256', $stack [1] . $plain) == $stack [0]) {
  128.         return true;
  129.       }
  130.     }
  131.     return false;
  132.   }
  133.   /**
  134.    * Update a logged in Customer password.
  135.    * e.g. when customer wants to change password
  136.    *
  137.    * @param string $plain
  138.    * @param integer $customerId
  139.    * @return string
  140.    */
  141.   public function updateLoggedInCustomerPassword($plain, $customerId)
  142.   {
  143.     $this->confirmDbSchema('customer');
  144.     global $db;
  145.     $updatedPassword = password_hash($plain, PASSWORD_DEFAULT);
  146.     $sql = "UPDATE " . TABLE_CUSTOMERS . "
  147.              SET customers_password = :password:
  148.              WHERE customers_id = :customersId:";
  149.  
  150.     $sql = $db->bindVars($sql, ':customersId:', $_SESSION ['customer_id'], 'integer');
  151.     $sql = $db->bindVars($sql, ':password:', $updatedPassword, 'string');
  152.     $db->Execute($sql);
  153.     return $updatePassword;
  154.   }
  155.   /**
  156.    * Update a not logged in Customer password.
  157.    * e.g. login/timeout
  158.    *
  159.    * @param string $plain
  160.    * @param string $emailAddress
  161.    * @return string
  162.    */
  163.   public function updateNotLoggedInCustomerPassword($plain, $emailAddress)
  164.   {
  165.     $this->confirmDbSchema('customer');
  166.     global $db;
  167.     $updatedPassword = password_hash($plain, PASSWORD_DEFAULT);
  168.     $sql = "UPDATE " . TABLE_CUSTOMERS . "
  169.              SET customers_password = :password:
  170.              WHERE customers_email_address = :emailAddress:";
  171.  
  172.     $sql = $db->bindVars($sql, ':emailAddress:', $emailAddress, 'string');
  173.     $sql = $db->bindVars($sql, ':password:', $updatedPassword, 'string');
  174.     $db->Execute($sql);
  175.     return $updatedPassword;
  176.   }
  177.   /**
  178.    * Update a not logged in Admin password.
  179.    *
  180.    * @param string $plain
  181.    * @param string $admin
  182.    * @return string
  183.    */
  184.   public function updateNotLoggedInAdminPassword($plain, $admin)
  185.   {
  186.     $this->confirmDbSchema('admin');
  187.     global $db;
  188.     $updatedPassword = password_hash($plain, PASSWORD_DEFAULT);
  189.     $sql = "UPDATE " . TABLE_ADMIN . "
  190.              SET admin_pass = :password:
  191.              WHERE admin_name = :adminName:";
  192.  
  193.     $sql = $db->bindVars($sql, ':adminName:', $admin, 'string');
  194.     $sql = $db->bindVars($sql, ':password:', $updatedPassword, 'string');
  195.     $db->Execute($sql);
  196.     return $updatedPassword;
  197.   }
  198.   /**
  199.    * Ensure db schema has been updated to support the required password lengths
  200.    * @param string $mode
  201.    */
  202.   public function confirmDbSchema($mode = '') {
  203.     global $db;
  204.     if ($mode == '' || $mode == 'admin') {
  205.       $sql = "ALTER TABLE " . TABLE_ADMIN . " MODIFY admin_pass VARCHAR( 255 ) NOT NULL DEFAULT ''";
  206.       $db->Execute($sql);
  207.       $sql = "ALTER TABLE " . TABLE_ADMIN . " MODIFY prev_pass1 VARCHAR( 255 ) NOT NULL DEFAULT ''";
  208.       $db->Execute($sql);
  209.       $sql = "ALTER TABLE " . TABLE_ADMIN . " MODIFY prev_pass2 VARCHAR( 255 ) NOT NULL DEFAULT ''";
  210.       $db->Execute($sql);
  211.       $sql = "ALTER TABLE " . TABLE_ADMIN . " MODIFY prev_pass3 VARCHAR( 255 ) NOT NULL DEFAULT ''";
  212.       $db->Execute($sql);
  213.       $sql = "ALTER TABLE " . TABLE_ADMIN . " MODIFY reset_token VARCHAR( 255 ) NOT NULL DEFAULT ''";
  214.       $db->Execute($sql);
  215.     }
  216.     if ($mode == '' || $mode == 'customer') {
  217.       $found = false;
  218.       $sql = "show fields from " . TABLE_CUSTOMERS;
  219.       $result = $db->Execute($sql);
  220.       while (!$result->EOF && !$found) {
  221.         if ($result->fields['Field'] == 'customers_password' && strtoupper($result->fields['Type']) == 'VARCHAR(255)') {
  222.           $found = true;
  223.         }
  224.         $result->MoveNext();
  225.       }
  226.       if (!$found) {
  227.         $sql = "ALTER TABLE " . TABLE_CUSTOMERS . " MODIFY customers_password VARCHAR( 255 ) NOT NULL DEFAULT ''";
  228.         $db->Execute($sql);
  229.       }
  230.     }
  231.     return;
  232.   }
  233. }
  234.