1: <?php
2: /* SVN FILE: $Id$ */
3: /**
4: * Short description for file.
5: *
6: * Long description for file
7: *
8: * PHP versions 4 and 5
9: *
10: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
11: * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
12: *
13: * Licensed under The MIT License
14: * Redistributions of files must retain the above copyright notice.
15: *
16: * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
17: * @link http://cakephp.org CakePHP(tm) Project
18: * @package cake
19: * @subpackage cake.cake.libs
20: * @since CakePHP(tm) v .0.10.0.1233
21: * @version $Revision$
22: * @modifiedby $LastChangedBy$
23: * @lastmodified $Date$
24: * @license http://www.opensource.org/licenses/mit-license.php The MIT License
25: */
26: /**
27: * Short description for file.
28: *
29: * Long description for file
30: *
31: * @package cake
32: * @subpackage cake.cake.libs
33: */
34: class Security extends Object {
35: /**
36: * Default hash method
37: *
38: * @var string
39: * @access public
40: */
41: var $hashType = null;
42: /**
43: * Singleton implementation to get object instance.
44: *
45: * @return object
46: * @access public
47: * @static
48: */
49: function &getInstance() {
50: static $instance = array();
51: if (!$instance) {
52: $instance[0] =& new Security;
53: }
54: return $instance[0];
55: }
56: /**
57: * Get allowed minutes of inactivity based on security level.
58: *
59: * @return integer Allowed inactivity in minutes
60: * @access public
61: * @static
62: */
63: function inactiveMins() {
64: $_this =& Security::getInstance();
65: switch (Configure::read('Security.level')) {
66: case 'high':
67: return 10;
68: break;
69: case 'medium':
70: return 100;
71: break;
72: case 'low':
73: default:
74: return 300;
75: break;
76: }
77: }
78: /**
79: * Generate authorization hash.
80: *
81: * @return string Hash
82: * @access public
83: * @static
84: */
85: function generateAuthKey() {
86: if (!class_exists('String')) {
87: App::import('Core', 'String');
88: }
89: return Security::hash(String::uuid());
90: }
91: /**
92: * Validate authorization hash.
93: *
94: * @param string $authKey Authorization hash
95: * @return boolean Success
96: * @access public
97: * @static
98: * @todo Complete implementation
99: */
100: function validateAuthKey($authKey) {
101: return true;
102: }
103: /**
104: * Create a hash from string using given method.
105: * Fallback on next available method.
106: *
107: * @param string $string String to hash
108: * @param string $type Method to use (sha1/sha256/md5)
109: * @param boolean $salt If true, automatically appends the application's salt
110: * value to $string (Security.salt)
111: * @return string Hash
112: * @access public
113: * @static
114: */
115: function hash($string, $type = null, $salt = false) {
116: $_this =& Security::getInstance();
117:
118: if ($salt) {
119: if (is_string($salt)) {
120: $string = $salt . $string;
121: } else {
122: $string = Configure::read('Security.salt') . $string;
123: }
124: }
125:
126: if (empty($type)) {
127: $type = $_this->hashType;
128: }
129: $type = strtolower($type);
130:
131: if ($type == 'sha1' || $type == null) {
132: if (function_exists('sha1')) {
133: $return = sha1($string);
134: return $return;
135: }
136: $type = 'sha256';
137: }
138:
139: if ($type == 'sha256' && function_exists('mhash')) {
140: return bin2hex(mhash(MHASH_SHA256, $string));
141: }
142:
143: if (function_exists('hash')) {
144: return hash($type, $string);
145: }
146: return md5($string);
147: }
148: /**
149: * Sets the default hash method for the Security object. This affects all objects using
150: * Security::hash().
151: *
152: * @param string $hash Method to use (sha1/sha256/md5)
153: * @access public
154: * @return void
155: * @static
156: * @see Security::hash()
157: */
158: function setHash($hash) {
159: $_this =& Security::getInstance();
160: $_this->hashType = $hash;
161: }
162: /**
163: * Encrypts/Decrypts a text using the given key.
164: *
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
170: */
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 '';
175: }
176:
177: $_this =& Security::getInstance();
178: if (!defined('CIPHER_SEED')) {
179: //This is temporary will change later
180: define('CIPHER_SEED', '76859309657453542496749683645');
181: }
182: srand(CIPHER_SEED);
183: $out = '';
184:
185: for ($i = 0; $i < strlen($text); $i++) {
186: for ($j = 0; $j < ord(substr($key, $i % strlen($key), 1)); $j++) {
187: $toss = rand(0, 255);
188: }
189: $mask = rand(0, 255);
190: $out .= chr(ord(substr($text, $i, 1)) ^ $mask);
191: }
192: return $out;
193: }
194: }
195: ?>