security.php
Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 class SecurityComponent extends Object {
00038
00039
00040
00041
00042
00043
00044 var $Security = null;
00045
00046
00047
00048
00049
00050
00051 var $blackHoleCallback = null;
00052
00053
00054
00055
00056
00057
00058
00059 var $requirePost = array();
00060
00061
00062
00063
00064
00065
00066
00067 var $requireAuth = array();
00068
00069
00070
00071
00072
00073
00074
00075 var $allowedControllers = array();
00076
00077
00078
00079
00080
00081
00082
00083 var $allowedActions = array();
00084
00085
00086
00087
00088
00089
00090 var $components = array('RequestHandler', 'Session');
00091
00092
00093
00094 function __construct () {
00095 $this->Security = Security::getInstance();
00096 }
00097
00098
00099
00100
00101
00102
00103
00104 function startup(&$controller) {
00105 if (is_array($this->requirePost) && !empty($this->requirePost)) {
00106
00107 if (in_array($controller->action, $this->requirePost)) {
00108
00109 if (!$this->RequestHandler->isPost()) {
00110
00111 if (!$this->blackHole($controller)) {
00112 return null;
00113 }
00114 }
00115 }
00116 }
00117
00118 if (is_array($this->requireAuth) && !empty($this->requireAuth) && !empty($controller->params['form'])) {
00119 if (in_array($controller->action, $this->requireAuth)) {
00120
00121 if (!isset($controller->params['data']['_Token'])) {
00122
00123 if (!$this->blackHole($controller)) {
00124 return null;
00125 }
00126 }
00127 $token = $controller->params['data']['_Token']['key'];
00128
00129 if ($this->Session->check('_Token')) {
00130 $tData = $this->Session->read('_Token');
00131 if (!(intval($tData['expires']) > strtotime('now')) || $tData['key'] !== $token) {
00132
00133 if (!$this->blackHole($controller)) {
00134 return null;
00135 }
00136 }
00137
00138 if (!empty($tData['allowedControllers']) && !in_array($controller->params['controller'], $tData['allowedControllers']) ||!empty($tData['allowedActions']) && !in_array($controller->params['action'], $tData['allowedActions'])) {
00139 if (!$this->blackHole($controller)) {
00140 return null;
00141 }
00142 }
00143 } else {
00144 if (!$this->blackHole($controller)) {
00145 return null;
00146 }
00147 }
00148 }
00149 }
00150
00151 if (!isset($controller->params['requested']) || $controller->params['requested'] != 1) {
00152
00153 $authKey = Security::generateAuthKey();
00154 $expires = strtotime('+'.Security::inactiveMins().' minutes');
00155 $token = array(
00156 'key' => $authKey,
00157 'expires' => $expires,
00158 'allowedControllers' => $this->allowedControllers,
00159 'allowedActions' => $this->allowedActions
00160 );
00161 if (!isset($controller->params['data'])) {
00162 $controller->params['data'] = array();
00163 }
00164 $controller->params['_Token'] = $token;
00165 $this->Session->write('_Token', $token);
00166 }
00167 }
00168
00169
00170
00171
00172
00173
00174
00175 function blackHole(&$controller) {
00176 if ($this->blackHoleCallback == null) {
00177 header('HTTP/1.0 404 Not Found');
00178 exit();
00179 } elseif (method_exists($controller, $this->blackHoleCallback)) {
00180 return $controller->{$this->blackHoleCallback}();
00181 }
00182 }
00183
00184
00185
00186
00187
00188
00189 function requirePost() {
00190 $this->requirePost = func_get_args();
00191 }
00192
00193
00194
00195
00196
00197
00198 function requireAuth() {
00199 $this->requireAuth = func_get_args();
00200 }
00201 }
00202 ?>