| Total Complexity | 67 |
| Total Lines | 484 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Request often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 87 | class Request |
||
| 88 | { |
||
| 89 | /** |
||
| 90 | * here is the request data stored |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | protected $data=[ |
||
| 95 | 'content' => [], |
||
| 96 | 'readonlys' => [], |
||
| 97 | ]; |
||
| 98 | /** |
||
| 99 | * Flag if data has been modified and therefor need to be stored again in the session |
||
| 100 | * |
||
| 101 | * @var boolean |
||
| 102 | */ |
||
| 103 | protected $data_modified=false; |
||
| 104 | /** |
||
| 105 | * Flag that stored data should be removed by destructor, if not modified. |
||
| 106 | * |
||
| 107 | * @var boolean |
||
| 108 | */ |
||
| 109 | protected $remove_if_not_modified=false; |
||
| 110 | /** |
||
| 111 | * mcrypt resource |
||
| 112 | * |
||
| 113 | * @var resource |
||
| 114 | */ |
||
| 115 | static protected $mcrypt; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * See gzcompress, set it to 0 to not compress |
||
| 119 | * |
||
| 120 | * @var int |
||
| 121 | */ |
||
| 122 | static public $compression_level = 6; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Name of request class used |
||
| 126 | * |
||
| 127 | * Can be set here to force a certain class, otherwise the factory method chooses one |
||
| 128 | * |
||
| 129 | * @var string |
||
| 130 | */ |
||
| 131 | static public $request_class; // = 'EGroupware\Api\Etemplate\Request\Session'; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Factory method to get a new request object or the one for an existing request |
||
| 135 | * |
||
| 136 | * New default is to use egw_cache to store requests and no longer request or |
||
| 137 | * session documented below: |
||
| 138 | * |
||
| 139 | * If mcrypt AND gzcompress is available this factory method chooses etemplate_request, |
||
| 140 | * which stores the request data encrypted in a hidden var directly in the form, |
||
| 141 | * over etemplate_request_session, which stores the data in the session (and causing |
||
| 142 | * the sesison to constantly grow). |
||
| 143 | * |
||
| 144 | * @param string $id =null |
||
| 145 | * @param bool $handle_not_found =true true: handle not found by trying to redirect, false: just return null |
||
| 146 | * @return Request|null null if Request not found and $handle_not_found === false |
||
| 147 | */ |
||
| 148 | public static function read($id=null, $handle_not_found=true) |
||
| 149 | { |
||
| 150 | if (is_null(self::$request_class)) |
||
|
|
|||
| 151 | { |
||
| 152 | // new default to use egw_cache to store requests |
||
| 153 | self::$request_class = __CLASS__.'\\Cache'; |
||
| 154 | /* old default to use request if mcrypt and gzcompress are available and session if not |
||
| 155 | self::$request_class = check_load_extension('mcrypt') && function_exists('gzcompress') && |
||
| 156 | self::init_crypt() ? __CLASS__ : 'EGroupware\Api\Etemplate\Request\Session'; |
||
| 157 | */ |
||
| 158 | } |
||
| 159 | if (self::$request_class != __CLASS__) |
||
| 160 | { |
||
| 161 | $request = call_user_func(self::$request_class.'::read', $id); |
||
| 162 | } |
||
| 163 | else |
||
| 164 | { |
||
| 165 | $request = new Request(); |
||
| 166 | |||
| 167 | if (!is_null($id)) |
||
| 168 | { |
||
| 169 | $id = base64_decode($id); |
||
| 170 | |||
| 171 | // decrypt the data if available |
||
| 172 | if (self::init_crypt()) |
||
| 173 | { |
||
| 174 | $id = mdecrypt_generic(self::$mcrypt,$id); |
||
| 175 | } |
||
| 176 | // uncompress the data if available |
||
| 177 | if (self::$compression_level && function_exists('gzcompress')) |
||
| 178 | { |
||
| 179 | //$len_compressed = bytes($id); |
||
| 180 | //$time = microtime(true); |
||
| 181 | $id = gzuncompress($id); |
||
| 182 | //$time = number_format(1000.0 * (microtime(true) - $time),1); |
||
| 183 | //$len_uncompressed = bytes($id); |
||
| 184 | //error_log(__METHOD__."() uncompressed from $len_compressed to $len_uncompressed bytes $time ms"); |
||
| 185 | } |
||
| 186 | $request->data = unserialize($id); |
||
| 187 | |||
| 188 | if (!$request->data) |
||
| 189 | { |
||
| 190 | error_log(__METHOD__."() id not valid!"); |
||
| 191 | $request = false; |
||
| 192 | } |
||
| 193 | //error_log(__METHOD__."() size of request = ".bytes($id)); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | if (!$request && $handle_not_found) // eT2 request/session expired |
||
| 197 | { |
||
| 198 | list($app) = explode('.', $_GET['menuaction']); |
||
| 199 | $global = false; |
||
| 200 | if(isset($GLOBALS['egw_info']['apps'][$app])) |
||
| 201 | { |
||
| 202 | $index_url = isset($GLOBALS['egw_info']['apps'][$app]['index']) ? |
||
| 203 | '/index.php?menuaction='.$GLOBALS['egw_info']['apps'][$app]['index'] : '/'.$app.'/index.php'; |
||
| 204 | } |
||
| 205 | else |
||
| 206 | { |
||
| 207 | $index_url = Api\Framework::link('/index.php'); |
||
| 208 | $global = true; |
||
| 209 | $app = null; |
||
| 210 | } |
||
| 211 | // add a unique token to redirect to avoid client-side framework tries refreshing via nextmatch |
||
| 212 | $index_url .= (strpos($index_url, '?') ? '&' : '?').'redirect='.microtime(true); |
||
| 213 | error_log(__METHOD__."('$id', ...) eT2 request not found / expired --> redirecting app $app to $index_url (_GET[menuaction]=$_GET[menuaction], isJSONRequest()=".array2string(Api\Json\Request::isJSONRequest()).')'); |
||
| 214 | if (Api\Json\Request::isJSONRequest()) |
||
| 215 | { |
||
| 216 | // we must not redirect ajax_destroy_session calls, as they might originate from our own redirect! |
||
| 217 | if (strpos($_GET['menuaction'], '.ajax_destroy_session.etemplate') === false) |
||
| 218 | { |
||
| 219 | $response = Api\Json\Response::get(); |
||
| 220 | $response->redirect($index_url, $global, $app); |
||
| 221 | exit; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | else |
||
| 225 | { |
||
| 226 | Api\Framework::redirect_link($index_url); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | return $request; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * CSRF check using an etemplate-exec-id |
||
| 234 | * |
||
| 235 | * If eTemplate request object could not be read, the function will NOT return, |
||
| 236 | * but send an Ajax error response and exit or die with the error-message! |
||
| 237 | * |
||
| 238 | * @param string $id etemplate-exec-id |
||
| 239 | * @param string $caller calling method to log |
||
| 240 | * @param array $args =[] arguments to log |
||
| 241 | * @throws Api\Json\Exception |
||
| 242 | */ |
||
| 243 | public static function csrfCheck($id, $caller, $args=[]) |
||
| 244 | { |
||
| 245 | if (!self::read($id, false)) // false: do NOT handle not found, but return null |
||
| 246 | { |
||
| 247 | error_log(__METHOD__."('$id', $caller, ".json_encode($args).") called with invalid/expired etemplate_exec_id: possible CSRF detected from IP ".$_SERVER['REMOTE_ADDR'].' to '.$_SERVER['REQUEST_METHOD'].' '.$_SERVER['REQUEST_URI']); |
||
| 248 | $msg = lang('Request could not be processed, please reload your window (press F5 or Cmd R)!'); |
||
| 249 | |||
| 250 | if (Api\Json\Request::isJSONRequest()) |
||
| 251 | { |
||
| 252 | Api\Json\Response::get()->message($msg, 'error'); |
||
| 253 | exit; |
||
| 254 | } |
||
| 255 | die($msg); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Private constructor to force the instancation of this class only via it's static factory method read |
||
| 261 | * |
||
| 262 | * @param string $id =null |
||
| 263 | */ |
||
| 264 | private function __construct($id=null) |
||
| 265 | { |
||
| 266 | unset($id); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * return the id of this request |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function &id() |
||
| 275 | { |
||
| 276 | $this->cleanup(); |
||
| 277 | $data = serialize($this->data); |
||
| 278 | |||
| 279 | // compress the data if available |
||
| 280 | if (self::$compression_level && function_exists('gzcompress')) |
||
| 281 | { |
||
| 282 | //$len_uncompressed = bytes($id); |
||
| 283 | //$time = microtime(true); |
||
| 284 | $data = gzcompress($data, self::$compression_level); |
||
| 285 | //$time = number_format(1000.0 * (microtime(true) - $time),1); |
||
| 286 | //$len_compressed = bytes($id); |
||
| 287 | //error_log(__METHOD__."() compressed from $len_uncompressed to $len_compressed bytes in $time ms"); |
||
| 288 | } |
||
| 289 | // encrypt the data if available |
||
| 290 | if (self::init_crypt()) |
||
| 291 | { |
||
| 292 | $data = mcrypt_generic(self::$mcrypt, $data); |
||
| 293 | } |
||
| 294 | $id = base64_encode($data); |
||
| 295 | |||
| 296 | //error_log(__METHOD__."() #$this->id: size of request = ".bytes($id));//.", id='$id'"); |
||
| 297 | //self::debug(); |
||
| 298 | return $id; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Clean up data before storing it: currently only removes "real" nextmatch rows |
||
| 303 | */ |
||
| 304 | protected function cleanup() |
||
| 305 | { |
||
| 306 | if (isset($this->data['content']['nm'])) |
||
| 307 | { |
||
| 308 | if (is_array($this->data['content']['nm']['rows'])) |
||
| 309 | { |
||
| 310 | foreach(array_keys($this->data['content']['nm']['rows']) as $n) |
||
| 311 | { |
||
| 312 | if (is_int($n)) |
||
| 313 | { |
||
| 314 | unset($this->data['content']['nm']['rows'][$n]); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | //error_log(__METHOD__."() content[nm][rows]=".array2string($this->data['content']['nm']['rows'])); |
||
| 318 | } |
||
| 319 | // do not store actions |
||
| 320 | unset($this->data['content']['nm']['actions'], $this->data['content']['nm']['action_links']); |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Register a form-variable to be processed |
||
| 326 | * |
||
| 327 | * @param string $_form_name form-name |
||
| 328 | * @param string $type etemplate type |
||
| 329 | * @param array $data =array() optional extra data |
||
| 330 | */ |
||
| 331 | public function set_to_process($_form_name, $type, $data=array()) |
||
| 332 | { |
||
| 333 | if (!$_form_name || !$type) return; |
||
| 334 | |||
| 335 | //echo '<p>'.__METHOD__."($form_name,$type,".array2string($data).")</p>\n"; |
||
| 336 | $data['type'] = $type; |
||
| 337 | |||
| 338 | // unquote single and double quotes, as this is how they get returned in $_POST |
||
| 339 | $form_name = str_replace(array('\\\'','"'), array('\'','"'), $_form_name); |
||
| 340 | |||
| 341 | $this->data['to_process'][$form_name] = $data; |
||
| 342 | $this->data_modified = true; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Set an attribute of a to-process record |
||
| 347 | * |
||
| 348 | * @param string $_form_name form-name |
||
| 349 | * @param string $attribute etemplate type |
||
| 350 | * @param array $value |
||
| 351 | * @param boolean $add_to_array =false should $value be added to the attribute array |
||
| 352 | */ |
||
| 353 | public function set_to_process_attribute($_form_name, $attribute, $value, $add_to_array=false) |
||
| 354 | { |
||
| 355 | //echo '<p>'.__METHOD__."($form_name,$attribute,$value,$add_to_array)</p>\n"; |
||
| 356 | if (!$_form_name) return; |
||
| 357 | |||
| 358 | // unquote single and double quotes, as this is how they get returned in $_POST |
||
| 359 | $form_name = str_replace(array('\\\'','"'), array('\'','"'), $_form_name); |
||
| 360 | |||
| 361 | if ($add_to_array) |
||
| 362 | { |
||
| 363 | $this->data['to_process'][$form_name][$attribute][] = $value; |
||
| 364 | } |
||
| 365 | else |
||
| 366 | { |
||
| 367 | $this->data['to_process'][$form_name][$attribute] = $value; |
||
| 368 | } |
||
| 369 | $this->data_modified = true; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Unregister a form-variable to be no longer processed |
||
| 374 | * |
||
| 375 | * @param string $form_name form-name |
||
| 376 | */ |
||
| 377 | public function unset_to_process($form_name) |
||
| 378 | { |
||
| 379 | //echo '<p>'.__METHOD__."($form_name) isset_to_process($form_name)=".$this->isset_to_process($form_name)."</p>\n"; |
||
| 380 | unset($this->data['to_process'][$form_name]); |
||
| 381 | $this->data_modified = true; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * return the data of a form-var to process or the whole array |
||
| 386 | * |
||
| 387 | * @param string $form_name =null |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | public function get_to_process($form_name=null) |
||
| 391 | { |
||
| 392 | //echo '<p>'.__METHOD__."($form_name)</p>\n"; |
||
| 393 | return $form_name ? $this->data['to_process'][$form_name] : $this->data['to_process']; |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * check if something set for a given $form_name |
||
| 398 | * |
||
| 399 | * @param string $form_name |
||
| 400 | * @return boolean |
||
| 401 | */ |
||
| 402 | public function isset_to_process($form_name) |
||
| 403 | { |
||
| 404 | //echo '<p>'.__METHOD__."($form_name) = ".array2string(isset($this->data['to_process'][$form_name]))."</p>\n"; |
||
| 405 | return isset($this->data['to_process'][$form_name]); |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * creates a new unique request-id |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | * @throws \Exception if it was not possible to gather sufficient entropy. |
||
| 413 | */ |
||
| 414 | static function request_id() |
||
| 415 | { |
||
| 416 | // replace url-unsafe chars with _ to not run into url-encoding issues when used in a url |
||
| 417 | $userID = preg_replace('/[^a-z0-9_\\.@-]/i', '_', $GLOBALS['egw_info']['user']['account_lid']); |
||
| 418 | |||
| 419 | // replace + with _ to not run into url-encoding issues when used in a url |
||
| 420 | $token = str_replace('+', '_', base64_encode(random_bytes(32))); |
||
| 421 | |||
| 422 | return $GLOBALS['egw_info']['flags']['currentapp'].'_'.$userID.'_'.$token; |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * magic function to set all request-vars, used eg. as $request->method = 'app.class.method'; |
||
| 427 | * |
||
| 428 | * @param string $var |
||
| 429 | * @param mixed $val |
||
| 430 | */ |
||
| 431 | public function __set($var,$val) |
||
| 432 | { |
||
| 433 | if ($this->data[$var] !== $val) |
||
| 434 | { |
||
| 435 | $this->data[$var] = $val; |
||
| 436 | //error_log(__METHOD__."('$var', ...) data of id=$this->id changed ..."); |
||
| 437 | $this->data_modified = true; |
||
| 438 | } |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * magic function to access the request-vars, used eg. as $method = $request->method; |
||
| 443 | * |
||
| 444 | * @param string $var |
||
| 445 | * @return mixed |
||
| 446 | */ |
||
| 447 | public function &__get($var) |
||
| 448 | { |
||
| 449 | if ($var == 'data_modified') return $this->data_modified; |
||
| 450 | |||
| 451 | return $this->data[$var]; |
||
| 452 | } |
||
| 453 | |||
| 454 | |||
| 455 | /** |
||
| 456 | * magic function to see if a request-var has been set |
||
| 457 | * |
||
| 458 | * @param string $var |
||
| 459 | * @return boolean |
||
| 460 | */ |
||
| 461 | public function __isset($var) |
||
| 462 | { |
||
| 463 | return array_key_exists($var, $this->data); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get the names / keys of existing variables |
||
| 468 | * |
||
| 469 | * @return array |
||
| 470 | */ |
||
| 471 | public function names() |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Output the size-wise important parts of a request |
||
| 478 | * |
||
| 479 | * @param double $min_share minimum share to be reported (in percent of the whole request) |
||
| 480 | * @param double $dump_share minimum share from which on a variable get output |
||
| 481 | */ |
||
| 482 | public function debug($min_share=1.0,$dump_share=25.0) |
||
| 483 | { |
||
| 484 | echo "<p><b>total size request data = ".($total=strlen(serialize($this->data)))."</b></p>\n"; |
||
| 485 | echo "<p>shares bigger then $min_share% percent of it:</p>\n"; |
||
| 486 | foreach($this->data as $key => $val) |
||
| 487 | { |
||
| 488 | $len = strlen(is_array($val) ? serialize($val) : $val); |
||
| 489 | $len .= ' ('.sprintf('%2.1lf',($percent = 100.0 * $len / $total)).'%)'; |
||
| 490 | if ($percent < $min_share) continue; |
||
| 491 | echo "<p><b>$key</b>: strlen(\$val)=$len</p>\n"; |
||
| 492 | if ($percent >= $dump_share) _debug_array($val); |
||
| 493 | if (is_array($val) && $len > 2000) |
||
| 494 | { |
||
| 495 | foreach($val as $k => $v) |
||
| 496 | { |
||
| 497 | $l = strlen(is_array($v) ? serialize($v) : $v); |
||
| 498 | $l .= ' ('.sprintf('%2.1lf',($p = 100.0 * $l / $total)).'%)'; |
||
| 499 | if ($p < $min_share) continue; |
||
| 500 | echo "<p> - {$key}[$k]: strlen(\$v)=$l</p>\n"; |
||
| 501 | } |
||
| 502 | } |
||
| 503 | } |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Check if session encryption is configured, possible and initialise it |
||
| 508 | * |
||
| 509 | * @param string $algo ='tripledes' |
||
| 510 | * @param string $mode ='ecb' |
||
| 511 | * @return boolean true if encryption is used, false otherwise |
||
| 512 | */ |
||
| 513 | static public function init_crypt($algo='tripledes',$mode='ecb') |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Destructor |
||
| 553 | */ |
||
| 554 | function __destruct() |
||
| 555 | { |
||
| 556 | if (self::$mcrypt) |
||
| 557 | { |
||
| 558 | mcrypt_generic_deinit(self::$mcrypt); |
||
| 559 | self::$mcrypt = null; |
||
| 560 | } |
||
| 561 | } |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Mark request as to destroy, if it does not get modified before destructor is called |
||
| 565 | * |
||
| 566 | * If that function is called, request is removed from storage, further modification will work! |
||
| 567 | */ |
||
| 568 | public function remove_if_not_modified() |
||
| 571 | } |
||
| 572 | } |
||
| 573 |