| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | trait Hm_Module_Output { |
| 16: | |
| 17: | |
| 18: | protected $output = []; |
| 19: | |
| 20: | |
| 21: | protected $protected = []; |
| 22: | |
| 23: | |
| 24: | protected $appendable = []; |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | protected function check_overwrite($name, $list, $type, $value) { |
| 34: | if (in_array($name, $list, true)) { |
| 35: | Hm_Debug::add(sprintf('MODULES: Cannot overwrite %s %s with %s', $type, $name, print_r($value,true))); |
| 36: | return false; |
| 37: | } |
| 38: | return true; |
| 39: | } |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | public function out($name, $value, $protected = true) { |
| 49: | if (!$this->check_overwrite($name, $this->protected, 'protected', $value)) { |
| 50: | return false; |
| 51: | } |
| 52: | if (!$this->check_overwrite($name, $this->appendable, 'protected', $value)) { |
| 53: | return false; |
| 54: | } |
| 55: | if ($protected) { |
| 56: | $this->protected[] = $name; |
| 57: | } |
| 58: | $this->output[$name] = $value; |
| 59: | return true; |
| 60: | } |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | public function append($name, $value) { |
| 69: | if (!$this->check_overwrite($name, $this->protected, 'protected', $value)) { |
| 70: | return false; |
| 71: | } |
| 72: | if (array_key_exists($name, $this->output)) { |
| 73: | if (is_array($this->output[$name])) { |
| 74: | $this->output[$name][] = $value; |
| 75: | return true; |
| 76: | } else { |
| 77: | Hm_Debug::add(sprintf('Tried to append %s to scaler %s', $value, $name)); |
| 78: | return false; |
| 79: | } |
| 80: | } else { |
| 81: | $this->output[$name] = [$value]; |
| 82: | $this->appendable[] = $name; |
| 83: | return true; |
| 84: | } |
| 85: | } |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | public function html_safe($string, $special_only = false) { |
| 94: | if ($special_only) { |
| 95: | return htmlspecialchars((string) $string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); |
| 96: | } |
| 97: | return htmlentities((string) $string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); |
| 98: | } |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | public function concat($name, $value) { |
| 107: | if (array_key_exists($name, $this->output)) { |
| 108: | if (is_string($this->output[$name])) { |
| 109: | $this->output[$name] .= $value; |
| 110: | return true; |
| 111: | } else { |
| 112: | Hm_Debug::add(sprintf('Could not append %s to %s', print_r($value,true), $name)); |
| 113: | return false; |
| 114: | } |
| 115: | } else { |
| 116: | $this->output[$name] = $value; |
| 117: | return true; |
| 118: | } |
| 119: | } |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | public function module_output() { |
| 126: | return $this->output; |
| 127: | } |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | public function output_protected() { |
| 134: | return $this->protected; |
| 135: | } |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | public function get($name, $default = NULL, $typed = true) { |
| 145: | if (array_key_exists($name, $this->output)) { |
| 146: | $val = $this->output[$name]; |
| 147: | if (!is_null($default) && $typed) { |
| 148: | if (gettype($default) != gettype($val)) { |
| 149: | Hm_Debug::add(sprintf('TYPE CONVERSION: %s to %s for %s', gettype($val), gettype($default), $name), 'info'); |
| 150: | settype($val, gettype($default)); |
| 151: | } |
| 152: | } |
| 153: | return $val; |
| 154: | } |
| 155: | return $default; |
| 156: | } |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | public function exists($name) { |
| 164: | return array_key_exists($name, $this->output); |
| 165: | } |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | public function in($name, $values) { |
| 174: | if (array_key_exists($name, $this->output) && in_array($this->output[$name], $values, true)) { |
| 175: | return true; |
| 176: | } |
| 177: | return false; |
| 178: | } |
| 179: | } |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | trait Hm_Handler_Validate { |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | public function validate_method($session, $request) { |
| 194: | if (!empty($request->method) && is_string($request->method)) { |
| 195: | if (!in_array(mb_strtolower($request->method), ['get', 'post'], true)) { |
| 196: | if ($session->loaded) { |
| 197: | $session->destroy($request); |
| 198: | Hm_Debug::add(sprintf('LOGGED OUT: invalid method %s', $request->method)); |
| 199: | } |
| 200: | return false; |
| 201: | } |
| 202: | return true; |
| 203: | } |
| 204: | |
| 205: | if ($session->loaded) { |
| 206: | $session->destroy($request); |
| 207: | Hm_Debug::add('LOGGED OUT: missing or invalid request method'); |
| 208: | } |
| 209: | return false; |
| 210: | } |
| 211: | |
| 212: | |
| 213: | |
| 214: | |
| 215: | |
| 216: | public function validate_origin($session, $request, $config) { |
| 217: | if (!$session->loaded) { |
| 218: | return true; |
| 219: | } |
| 220: | list($source, $target) = $this->source_and_target($request, $config); |
| 221: | if (!$this->validate_target($target, $source, $session, $request) || |
| 222: | !$this->validate_source($target, $source, $session, $request)) { |
| 223: | return false; |
| 224: | } |
| 225: | return true; |
| 226: | } |
| 227: | |
| 228: | |
| 229: | |
| 230: | |
| 231: | |
| 232: | private function source_and_target($request, $config) { |
| 233: | $source = false; |
| 234: | $target = $config->get('cookie_domain', false); |
| 235: | if ($target == 'none') { |
| 236: | $target = false; |
| 237: | } |
| 238: | $server_vars = [ |
| 239: | 'HTTP_REFERER' => 'source', |
| 240: | 'HTTP_ORIGIN' => 'source', |
| 241: | 'HTTP_HOST' => 'target', |
| 242: | 'HTTP_X_FORWARDED_HOST' => 'target' |
| 243: | ]; |
| 244: | foreach ($server_vars as $header => $type) { |
| 245: | if (!empty($request->server[$header])) { |
| 246: | $$type = $request->server[$header]; |
| 247: | } |
| 248: | } |
| 249: | return [$source, $target]; |
| 250: | } |
| 251: | |
| 252: | |
| 253: | |
| 254: | |
| 255: | |
| 256: | |
| 257: | private function validate_target($target, $source, $session, $request) { |
| 258: | if (!$target || !$source) { |
| 259: | $session->destroy($request); |
| 260: | Hm_Debug::add('LOGGED OUT: missing target origin'); |
| 261: | return false; |
| 262: | } |
| 263: | return true; |
| 264: | } |
| 265: | |
| 266: | |
| 267: | |
| 268: | |
| 269: | |
| 270: | |
| 271: | private function validate_source($target, $source, $session, $request) { |
| 272: | $source = parse_url($source); |
| 273: | if (!is_array($source) || !array_key_exists('host', $source)) { |
| 274: | $session->destroy($request); |
| 275: | Hm_Debug::add('LOGGED OUT: invalid source origin'); |
| 276: | return false; |
| 277: | } |
| 278: | if (array_key_exists('port', $source)) { |
| 279: | $source['host'] .= ':'.$source['port']; |
| 280: | } |
| 281: | if ($source['host'] !== $target) { |
| 282: | $session->destroy($request); |
| 283: | Hm_Debug::add('LOGGED OUT: invalid source origin'); |
| 284: | return false; |
| 285: | } |
| 286: | return true; |
| 287: | } |
| 288: | } |
| 289: | |
| 290: | |
| 291: | |
| 292: | |
| 293: | |
| 294: | |
| 295: | |
| 296: | |
| 297: | |
| 298: | |
| 299: | |
| 300: | |
| 301: | |
| 302: | |
| 303: | |
| 304: | |
| 305: | |
| 306: | abstract class Hm_Handler_Module { |
| 307: | |
| 308: | use Hm_Module_Output; |
| 309: | use Hm_Handler_Validate; |
| 310: | |
| 311: | |
| 312: | public $session; |
| 313: | |
| 314: | |
| 315: | public $request; |
| 316: | |
| 317: | |
| 318: | public $config; |
| 319: | |
| 320: | |
| 321: | protected $page = ''; |
| 322: | |
| 323: | |
| 324: | public $user_config; |
| 325: | |
| 326: | public $cache; |
| 327: | |
| 328: | |
| 329: | |
| 330: | |
| 331: | |
| 332: | |
| 333: | |
| 334: | |
| 335: | public function __construct($parent, $page, $output = [], $protected = []) { |
| 336: | $this->session = $parent->session; |
| 337: | $this->request = $parent->request; |
| 338: | $this->cache = $parent->cache; |
| 339: | $this->page = $page; |
| 340: | $this->config = $parent->site_config; |
| 341: | $this->user_config = $parent->user_config; |
| 342: | $this->output = $output; |
| 343: | $this->protected = $protected; |
| 344: | } |
| 345: | |
| 346: | |
| 347: | |
| 348: | |
| 349: | private function invalid_ajax_key() { |
| 350: | if (DEBUG_MODE or $this->config->get('debug_log')) { |
| 351: | Hm_Debug::add('REQUEST KEY check failed'); |
| 352: | Hm_Debug::load_page_stats(); |
| 353: | Hm_Debug::show(); |
| 354: | } |
| 355: | Hm_Functions::cease(json_encode(['status' => 'not callable'])); |
| 356: | return 'exit'; |
| 357: | } |
| 358: | |
| 359: | |
| 360: | |
| 361: | |
| 362: | private function invalid_http_key() { |
| 363: | if ($this->session->loaded) { |
| 364: | $this->session->destroy($this->request); |
| 365: | Hm_Debug::add('LOGGED OUT: request key check failed'); |
| 366: | } |
| 367: | Hm_Dispatch::page_redirect('?page=home'); |
| 368: | return 'redirect'; |
| 369: | } |
| 370: | |
| 371: | |
| 372: | |
| 373: | |
| 374: | |
| 375: | |
| 376: | |
| 377: | public function process_key() { |
| 378: | if (empty($this->request->post)) { |
| 379: | return false; |
| 380: | } |
| 381: | $key = array_key_exists('hm_page_key', $this->request->post) ? $this->request->post['hm_page_key'] : false; |
| 382: | $valid = Hm_Request_Key::validate($key); |
| 383: | if ($valid) { |
| 384: | return false; |
| 385: | } |
| 386: | if ($this->request->type == 'AJAX') { |
| 387: | return $this->invalid_ajax_key(); |
| 388: | } else { |
| 389: | return $this->invalid_http_key(); |
| 390: | } |
| 391: | } |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | |
| 397: | |
| 398: | private function check_field($val) { |
| 399: | switch (true) { |
| 400: | case is_array($val): |
| 401: | case is_string($val): |
| 402: | case is_int($val): |
| 403: | case is_float($val): |
| 404: | case is_bool($val): |
| 405: | case $val === '0': |
| 406: | case $val === 0: |
| 407: | return $val; |
| 408: | default: |
| 409: | return NULL; |
| 410: | } |
| 411: | } |
| 412: | |
| 413: | |
| 414: | |
| 415: | |
| 416: | |
| 417: | |
| 418: | public function process_form($form) { |
| 419: | $new_form = []; |
| 420: | foreach($form as $name) { |
| 421: | if (!array_key_exists($name, $this->request->post)) { |
| 422: | continue; |
| 423: | } |
| 424: | $val = $this->check_field($this->request->post[$name]); |
| 425: | if ($val !== NULL) { |
| 426: | $new_form[$name] = $val; |
| 427: | } |
| 428: | } |
| 429: | return [(count($form) === count($new_form)), $new_form]; |
| 430: | } |
| 431: | |
| 432: | |
| 433: | |
| 434: | |
| 435: | |
| 436: | |
| 437: | public function module_is_supported($name) { |
| 438: | return in_array(mb_strtolower($name), $this->config->get_modules(true), true); |
| 439: | } |
| 440: | |
| 441: | |
| 442: | |
| 443: | |
| 444: | |
| 445: | |
| 446: | |
| 447: | |
| 448: | public function should_skip_execution($setting_key, $default = false) { |
| 449: | return !$this->user_config->get($setting_key, $default); |
| 450: | } |
| 451: | |
| 452: | public function save_hm_msgs() { |
| 453: | $msgs = Hm_Msgs::getRaw(); |
| 454: | if (!empty($msgs)) { |
| 455: | Hm_Msgs::flush(); |
| 456: | $this->session->secure_cookie($this->request, 'hm_msgs', base64_encode(json_encode($msgs))); |
| 457: | } |
| 458: | } |
| 459: | |
| 460: | |
| 461: | |
| 462: | |
| 463: | abstract public function process(); |
| 464: | } |
| 465: | |
| 466: | |
| 467: | |
| 468: | |
| 469: | |
| 470: | |
| 471: | |
| 472: | |
| 473: | abstract class Hm_Output_Module { |
| 474: | |
| 475: | use Hm_Module_Output; |
| 476: | |
| 477: | |
| 478: | protected $lstr = []; |
| 479: | |
| 480: | |
| 481: | protected $lang = false; |
| 482: | |
| 483: | |
| 484: | protected $dir = 'ltr'; |
| 485: | |
| 486: | |
| 487: | protected $format = ''; |
| 488: | |
| 489: | |
| 490: | |
| 491: | |
| 492: | |
| 493: | |
| 494: | public function __construct($input, $protected) { |
| 495: | $this->output = $input; |
| 496: | $this->protected = $protected; |
| 497: | } |
| 498: | |
| 499: | |
| 500: | |
| 501: | |
| 502: | |
| 503: | |
| 504: | public function trans($string) { |
| 505: | if (array_key_exists($string, $this->lstr)) { |
| 506: | if ($this->lstr[$string] === false) { |
| 507: | return strip_tags($string); |
| 508: | } else { |
| 509: | return strip_tags($this->lstr[$string]); |
| 510: | } |
| 511: | } |
| 512: | else { |
| 513: | Hm_Debug::add(sprintf('TRANSLATION NOT FOUND :%s:', $string), 'warning'); |
| 514: | } |
| 515: | return str_replace('\n', '<br />', strip_tags($string)); |
| 516: | } |
| 517: | |
| 518: | |
| 519: | |
| 520: | |
| 521: | |
| 522: | public function all_trans() { |
| 523: | |
| 524: | $language_files = glob(APP_PATH.'language/'. '*.php'); |
| 525: | $translations = []; |
| 526: | |
| 527: | foreach ($language_files as $file) { |
| 528: | |
| 529: | $language_code = pathinfo($file, PATHINFO_FILENAME); |
| 530: | |
| 531: | |
| 532: | $content = include $file; |
| 533: | |
| 534: | |
| 535: | $translations[$language_code] = $content; |
| 536: | } |
| 537: | |
| 538: | return $translations; |
| 539: | } |
| 540: | |
| 541: | |
| 542: | |
| 543: | |
| 544: | |
| 545: | |
| 546: | |
| 547: | public function translate_number($number) { |
| 548: | if (!is_numeric($number) || !in_array($this->lang, ['fa'])) { |
| 549: | return $number; |
| 550: | } |
| 551: | $number_splitted = mb_str_split($number); |
| 552: | $translated_number = ""; |
| 553: | foreach ($number_splitted as $number_splitted) { |
| 554: | $translated_number .= $this->trans($number_splitted); |
| 555: | } |
| 556: | return $translated_number; |
| 557: | } |
| 558: | |
| 559: | |
| 560: | |
| 561: | |
| 562: | |
| 563: | |
| 564: | |
| 565: | public function output_content($format, $lang_str) { |
| 566: | $this->lstr = $lang_str; |
| 567: | $this->format = str_replace('Hm_Format_', '', $format); |
| 568: | if (array_key_exists('interface_lang', $lang_str)) { |
| 569: | $this->lang = $lang_str['interface_lang']; |
| 570: | } |
| 571: | if (array_key_exists('interface_direction', $lang_str)) { |
| 572: | $this->dir = $lang_str['interface_direction']; |
| 573: | } |
| 574: | return $this->output(); |
| 575: | } |
| 576: | |
| 577: | |
| 578: | |
| 579: | |
| 580: | |
| 581: | abstract protected function output(); |
| 582: | } |
| 583: | |
| 584: | |
| 585: | |
| 586: | |
| 587: | |
| 588: | class Hm_Output_ extends Hm_Output_Module { protected function output() {} } |
| 589: | class Hm_Handler_ extends Hm_Handler_Module { public function process() {} } |
| 590: | |