Linux multi-monitor fullscreen support
[ryzomcore.git] / web / public_php / webtt / cake / libs / security.php
blob4020b83ac5ea5f4bc94366b334cc4db223156088
1 <?php
2 /**
3 * Core Security
5 * PHP versions 4 and 5
7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10 * Licensed under The MIT License
11 * Redistributions of files must retain the above copyright notice.
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://cakephp.org CakePHP(tm) Project
15 * @package cake
16 * @subpackage cake.cake.libs
17 * @since CakePHP(tm) v .0.10.0.1233
18 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
21 /**
22 * Security Library contains utility methods related to security
24 * @package cake
25 * @subpackage cake.cake.libs
27 class Security extends Object {
29 /**
30 * Default hash method
32 * @var string
33 * @access public
35 var $hashType = null;
37 /**
38 * Singleton implementation to get object instance.
40 * @return object
41 * @access public
42 * @static
44 function &getInstance() {
45 static $instance = array();
46 if (!$instance) {
47 $instance[0] =& new Security;
49 return $instance[0];
52 /**
53 * Get allowed minutes of inactivity based on security level.
55 * @return integer Allowed inactivity in minutes
56 * @access public
57 * @static
59 function inactiveMins() {
60 switch (Configure::read('Security.level')) {
61 case 'high':
62 return 10;
63 break;
64 case 'medium':
65 return 100;
66 break;
67 case 'low':
68 default:
69 return 300;
70 break;
74 /**
75 * Generate authorization hash.
77 * @return string Hash
78 * @access public
79 * @static
81 function generateAuthKey() {
82 if (!class_exists('String')) {
83 App::import('Core', 'String');
85 return Security::hash(String::uuid());
88 /**
89 * Validate authorization hash.
91 * @param string $authKey Authorization hash
92 * @return boolean Success
93 * @access public
94 * @static
95 * @todo Complete implementation
97 function validateAuthKey($authKey) {
98 return true;
102 * Create a hash from string using given method.
103 * Fallback on next available method.
105 * @param string $string String to hash
106 * @param string $type Method to use (sha1/sha256/md5)
107 * @param boolean $salt If true, automatically appends the application's salt
108 * value to $string (Security.salt)
109 * @return string Hash
110 * @access public
111 * @static
113 function hash($string, $type = null, $salt = false) {
114 $_this =& Security::getInstance();
116 if ($salt) {
117 if (is_string($salt)) {
118 $string = $salt . $string;
119 } else {
120 $string = Configure::read('Security.salt') . $string;
124 if (empty($type)) {
125 $type = $_this->hashType;
127 $type = strtolower($type);
129 if ($type == 'sha1' || $type == null) {
130 if (function_exists('sha1')) {
131 $return = sha1($string);
132 return $return;
134 $type = 'sha256';
137 if ($type == 'sha256' && function_exists('mhash')) {
138 return bin2hex(mhash(MHASH_SHA256, $string));
141 if (function_exists('hash')) {
142 return hash($type, $string);
144 return md5($string);
148 * Sets the default hash method for the Security object. This affects all objects using
149 * Security::hash().
151 * @param string $hash Method to use (sha1/sha256/md5)
152 * @access public
153 * @return void
154 * @static
155 * @see Security::hash()
157 function setHash($hash) {
158 $_this =& Security::getInstance();
159 $_this->hashType = $hash;
163 * Encrypts/Decrypts a text using the given key.
165 * @param string $text Encrypted string to decrypt, normal string to encrypt
166 * @param string $key Key to use
167 * @return string Encrypted/Decrypted string
168 * @access public
169 * @static
171 function cipher($text, $key) {
172 if (empty($key)) {
173 trigger_error(__('You cannot use an empty key for Security::cipher()', true), E_USER_WARNING);
174 return '';
177 srand(Configure::read('Security.cipherSeed'));
178 $out = '';
179 $keyLength = strlen($key);
180 for ($i = 0, $textLength = strlen($text); $i < $textLength; $i++) {
181 $j = ord(substr($key, $i % $keyLength, 1));
182 while ($j--) {
183 rand(0, 255);
185 $mask = rand(0, 255);
186 $out .= chr(ord(substr($text, $i, 1)) ^ $mask);
188 srand();
189 return $out;