security.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: security_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * Short description for file.
00005  *
00006  * Long description for file
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.libs
00023  * @since           CakePHP(tm) v .0.10.0.1233
00024  * @version         $Revision: 580 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 /**
00030  * Short description for file.
00031  *
00032  * Long description for file
00033  *
00034  * @package     cake
00035  * @subpackage  cake.cake.libs
00036  */
00037 class Security extends Object {
00038 
00039 /**
00040  * Default hash method
00041  *
00042  * @var string
00043  * @access public
00044  */
00045     var $hashType = null;
00046 /**
00047   * Singleton implementation to get object instance.
00048   *
00049   * @return object
00050   * @access public
00051   * @static
00052   */
00053     function &getInstance() {
00054         static $instance = array();
00055         if (!$instance) {
00056             $instance[0] =& new Security;
00057         }
00058         return $instance[0];
00059     }
00060 /**
00061   * Get allowed minutes of inactivity based on security level.
00062   *
00063   * @return integer Allowed inactivity in minutes
00064   * @access public
00065   * @static
00066   */
00067     function inactiveMins() {
00068         $_this =& Security::getInstance();
00069         switch(Configure::read('Security.level')) {
00070             case 'high':
00071                 return 10;
00072             break;
00073             case 'medium':
00074                 return 100;
00075             break;
00076             case 'low':
00077             default:
00078                 return 300;
00079                 break;
00080         }
00081     }
00082 /**
00083   * Generate authorization hash.
00084   *
00085   * @return string Hash
00086   * @access public
00087   * @static
00088   */
00089     function generateAuthKey() {
00090         $_this =& Security::getInstance();
00091         if(!class_exists('String')) {
00092             App::import('Core', 'String');
00093         }
00094         return $_this->hash(String::uuid());
00095     }
00096 /**
00097  * Validate authorization hash.
00098  *
00099  * @param string $authKey Authorization hash
00100  * @return boolean Success
00101  * @access public
00102  * @static
00103  */
00104     function validateAuthKey($authKey) {
00105         $_this =& Security::getInstance();
00106         return true;
00107     }
00108 /**
00109  * Create a hash from string using given method.
00110  *
00111  * @param string $string String to hash
00112  * @param string $type Method to use (sha1/sha256/md5)
00113  * @param boolean $salt If true, automatically appends the application's salt
00114  *                value to $string (Security.salt)
00115  * @return string Hash
00116  * @access public
00117  * @static
00118  */
00119     function hash($string, $type = null, $salt = false) {
00120         $_this =& Security::getInstance();
00121 
00122         if ($salt) {
00123             $string = Configure::read('Security.salt') . $string;
00124         }
00125         if (empty($type)) {
00126             $type = $_this->hashType;
00127         }
00128         $type = strtolower($type);
00129 
00130         if ($type == 'sha1' || $type == null) {
00131             if (function_exists('sha1')) {
00132                 $return = sha1($string);
00133                 return $return;
00134             } else {
00135                 $type = 'sha256';
00136             }
00137         }
00138 
00139         if ($type == 'sha256') {
00140             if (function_exists('mhash')) {
00141                 $return = bin2hex(mhash(MHASH_SHA256, $string));
00142                 return $return;
00143             } else {
00144                 $type = 'md5';
00145             }
00146         }
00147 
00148         if ($type == 'md5') {
00149             $return = md5($string);
00150             return $return;
00151         }
00152     }
00153 /**
00154  * Sets the default hash method for the Security object.  This affects all objects using
00155  * Security::hash().
00156  *
00157  * @param string $hash Method to use (sha1/sha256/md5)
00158  * @access public
00159  * @static
00160  * @see Security::hash()
00161  */
00162     function setHash($hash) {
00163         $_this =& Security::getInstance();
00164         $_this->hashType = $hash;
00165     }
00166 /**
00167  * Encripts/Decrypts a text using the given key.
00168  *
00169  * @param string $text Encrypted string to decrypt, normal string to encrypt
00170  * @param string $key Key to use
00171  * @return string Encrypted/Decrypted string
00172  * @access public
00173  * @static
00174  */
00175     function cipher($text, $key) {
00176         if (empty($key)) {
00177             trigger_error(__('You cannot use an empty key for Security::cipher()', true), E_USER_WARNING);
00178             return '';
00179         }
00180 
00181         $_this =& Security::getInstance();
00182         if (!defined('CIPHER_SEED')) {
00183             //This is temporary will change later
00184             define('CIPHER_SEED', '76859309657453542496749683645');
00185         }
00186         srand (CIPHER_SEED);
00187         $out = '';
00188 
00189         for ($i = 0; $i < strlen($text); $i++) {
00190             for ($j = 0; $j < ord(substr($key, $i % strlen($key), 1)); $j++) {
00191                 $toss = rand(0, 255);
00192             }
00193             $mask = rand(0, 255);
00194             $out .= chr(ord(substr($text, $i, 1)) ^ $mask);
00195         }
00196         return $out;
00197     }
00198 }
00199 ?>