2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
* @since CakePHP(tm) v .0.10.0.1233
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Short description for file.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class Security extends Object {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Default hash method
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $hashType = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-07-26 10:46:07 +00:00
|
|
|
* Singleton implementation to get object instance.
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function &getInstance() {
|
|
|
|
static $instance = array();
|
2008-06-20 20:17:23 +00:00
|
|
|
if (!$instance) {
|
|
|
|
$instance[0] =& new Security;
|
|
|
|
}
|
|
|
|
return $instance[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-07-26 10:46:07 +00:00
|
|
|
* Get allowed minutes of inactivity based on security level.
|
|
|
|
*
|
|
|
|
* @return integer Allowed inactivity in minutes
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function inactiveMins() {
|
2008-10-23 00:10:44 +00:00
|
|
|
switch (Configure::read('Security.level')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'high':
|
|
|
|
return 10;
|
|
|
|
break;
|
|
|
|
case 'medium':
|
|
|
|
return 100;
|
|
|
|
break;
|
|
|
|
case 'low':
|
|
|
|
default:
|
|
|
|
return 300;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-07-26 10:46:07 +00:00
|
|
|
* Generate authorization hash.
|
|
|
|
*
|
|
|
|
* @return string Hash
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function generateAuthKey() {
|
2008-10-23 00:10:44 +00:00
|
|
|
if (!class_exists('String')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
App::import('Core', 'String');
|
|
|
|
}
|
2008-09-24 23:05:51 +00:00
|
|
|
return Security::hash(String::uuid());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Validate authorization hash.
|
|
|
|
*
|
|
|
|
* @param string $authKey Authorization hash
|
|
|
|
* @return boolean Success
|
|
|
|
* @access public
|
|
|
|
* @static
|
2008-09-25 16:49:56 +00:00
|
|
|
* @todo Complete implementation
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function validateAuthKey($authKey) {
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Create a hash from string using given method.
|
2008-07-07 17:13:09 +00:00
|
|
|
* Fallback on next available method.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $string String to hash
|
|
|
|
* @param string $type Method to use (sha1/sha256/md5)
|
|
|
|
* @param boolean $salt If true, automatically appends the application's salt
|
|
|
|
* value to $string (Security.salt)
|
|
|
|
* @return string Hash
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function hash($string, $type = null, $salt = false) {
|
|
|
|
$_this =& Security::getInstance();
|
2008-10-02 00:03:52 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($salt) {
|
2008-10-02 00:03:52 +00:00
|
|
|
if (is_string($salt)) {
|
|
|
|
$string = $salt . $string;
|
|
|
|
} else {
|
|
|
|
$string = Configure::read('Security.salt') . $string;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-02 14:44:42 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($type)) {
|
|
|
|
$type = $_this->hashType;
|
|
|
|
}
|
|
|
|
$type = strtolower($type);
|
|
|
|
|
|
|
|
if ($type == 'sha1' || $type == null) {
|
|
|
|
if (function_exists('sha1')) {
|
|
|
|
$return = sha1($string);
|
|
|
|
return $return;
|
|
|
|
}
|
2008-08-02 14:44:42 +00:00
|
|
|
$type = 'sha256';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-08-02 14:44:42 +00:00
|
|
|
if ($type == 'sha256' && function_exists('mhash')) {
|
|
|
|
return bin2hex(mhash(MHASH_SHA256, $string));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-07-07 17:13:09 +00:00
|
|
|
if (function_exists('hash')) {
|
2008-08-02 14:44:42 +00:00
|
|
|
return hash($type, $string);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-02 14:44:42 +00:00
|
|
|
return md5($string);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sets the default hash method for the Security object. This affects all objects using
|
|
|
|
* Security::hash().
|
|
|
|
*
|
|
|
|
* @param string $hash Method to use (sha1/sha256/md5)
|
|
|
|
* @access public
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @static
|
|
|
|
* @see Security::hash()
|
|
|
|
*/
|
|
|
|
function setHash($hash) {
|
|
|
|
$_this =& Security::getInstance();
|
|
|
|
$_this->hashType = $hash;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-08-20 18:05:26 +00:00
|
|
|
* Encrypts/Decrypts a text using the given key.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $text Encrypted string to decrypt, normal string to encrypt
|
|
|
|
* @param string $key Key to use
|
|
|
|
* @return string Encrypted/Decrypted string
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function cipher($text, $key) {
|
|
|
|
if (empty($key)) {
|
|
|
|
trigger_error(__('You cannot use an empty key for Security::cipher()', true), E_USER_WARNING);
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2010-01-15 21:56:26 +00:00
|
|
|
srand(Configure::read('Security.cipher_seed'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$out = '';
|
|
|
|
|
|
|
|
for ($i = 0; $i < strlen($text); $i++) {
|
|
|
|
for ($j = 0; $j < ord(substr($key, $i % strlen($key), 1)); $j++) {
|
2008-07-05 15:03:46 +00:00
|
|
|
$toss = rand(0, 255);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-07-05 15:03:46 +00:00
|
|
|
$mask = rand(0, 255);
|
2008-05-30 11:40:08 +00:00
|
|
|
$out .= chr(ord(substr($text, $i, 1)) ^ $mask);
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|