| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | use LucidFrame\Console\Command; |
| 17: | use LucidFrame\Console\Console; |
| 18: | use LucidFrame\Console\ConsoleTable; |
| 19: | use LucidFrame\Core\Form; |
| 20: | use LucidFrame\Core\Middleware; |
| 21: | use LucidFrame\Core\Pager; |
| 22: | use LucidFrame\Core\AsynFileUploader; |
| 23: | use LucidFrame\Core\File; |
| 24: | use LucidFrame\Core\SchemaManager; |
| 25: | use LucidFrame\Core\App; |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | function _app($name, $value = null) |
| 34: | { |
| 35: | if (count(func_get_args()) == 2) { |
| 36: | return App::$$name = $value; |
| 37: | } else { |
| 38: | return App::$$name; |
| 39: | } |
| 40: | } |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | function _version() |
| 47: | { |
| 48: | $findIn = array(LIB, ROOT); |
| 49: | foreach ($findIn as $dir) { |
| 50: | $versionFile = $dir . 'VERSION'; |
| 51: | if (is_file($versionFile) && file_exists($versionFile)) { |
| 52: | return trim(file_get_contents($versionFile)); |
| 53: | } |
| 54: | } |
| 55: | |
| 56: | return 'Unknown'; |
| 57: | } |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | function _flush($buffer, $phase) |
| 83: | { |
| 84: | if (function_exists('__flush')) { |
| 85: | $buffer = __flush($buffer, $phase); |
| 86: | } else { |
| 87: | $posHtml = stripos($buffer, '<html'); |
| 88: | $posHead = stripos($buffer, '<head'); |
| 89: | |
| 90: | $beforeHtmlTag = substr($buffer, 0, $posHtml); |
| 91: | $afterHtmlTag = substr($buffer, $posHead); |
| 92: | $htmlTag = trim(str_ireplace($beforeHtmlTag, '', substr($buffer, 0, $posHead))); |
| 93: | |
| 94: | if (trim($htmlTag)) { |
| 95: | $htmlTag = trim(ltrim($htmlTag, '<html.<HTML'), '>. '); |
| 96: | $attributes = array(); |
| 97: | $attrList = explode(' ', $htmlTag); |
| 98: | foreach ($attrList as $list) { |
| 99: | $attr = explode('=', $list); |
| 100: | $attr[0] = trim($attr[0]); |
| 101: | if (count($attr) == 2) { |
| 102: | $attr[1] = trim($attr[1], '".\''); |
| 103: | } |
| 104: | $attributes[$attr[0]] = $attr; |
| 105: | } |
| 106: | |
| 107: | $IE = false; |
| 108: | $IEVersion = ''; |
| 109: | $userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
| 110: | if (strpos($userAgent, 'MSIE') !== false || strpos($userAgent, 'Trident') !== false) { |
| 111: | $IE = true; |
| 112: | if (preg_match('/(MSIE|rv)\s(\d+)/i', $userAgent, $m)) { |
| 113: | $IEVersion = 'ie' . $m[2]; |
| 114: | } |
| 115: | } |
| 116: | |
| 117: | if (array_key_exists('class', $attributes)) { |
| 118: | |
| 119: | if ($IE) { |
| 120: | $attributes['class'][1] .= ' ie ' . $IEVersion; |
| 121: | } |
| 122: | if (_multilingual()) { |
| 123: | $attributes['class'][1] .= ' ' . _lang(); |
| 124: | } |
| 125: | } else { |
| 126: | $translationEnabled = _cfg('translationEnabled'); |
| 127: | |
| 128: | if ($IE || _multilingual() || $translationEnabled) { |
| 129: | $value = array(); |
| 130: | if ($IE) { |
| 131: | $value[] = 'ie ' . $IEVersion; |
| 132: | } |
| 133: | if (_multilingual() || $translationEnabled) { |
| 134: | $value[] = _lang(); |
| 135: | } |
| 136: | if (count($value)) { |
| 137: | $attributes['class'] = array('class', implode(' ', $value)); |
| 138: | } |
| 139: | } |
| 140: | } |
| 141: | |
| 142: | if (_multilingual()) { |
| 143: | |
| 144: | if (!array_key_exists('lang', $attributes)) { |
| 145: | |
| 146: | $attributes['lang'] = array('lang', _lang()); |
| 147: | } |
| 148: | } |
| 149: | |
| 150: | if (!array_key_exists('itemscope', $attributes)) { |
| 151: | $attributes['itemscope'] = array('itemscope'); |
| 152: | } |
| 153: | |
| 154: | if (!array_key_exists('itemtype', $attributes)) { |
| 155: | |
| 156: | |
| 157: | $attributes['itemtype'] = array('itemtype', "http://schema.org/WebPage"); |
| 158: | } |
| 159: | |
| 160: | ksort($attributes); |
| 161: | $html = '<html'; |
| 162: | foreach ($attributes as $key => $value) { |
| 163: | $html .= ' '.$key; |
| 164: | if (isset($value[1])) { |
| 165: | |
| 166: | $html .= '="' . $value[1] . '"'; |
| 167: | } |
| 168: | } |
| 169: | $html .= '>' . "\r\n"; |
| 170: | $buffer = $beforeHtmlTag . $html . $afterHtmlTag; |
| 171: | } |
| 172: | } |
| 173: | |
| 174: | |
| 175: | $buffer = _minifyHTML($buffer); |
| 176: | |
| 177: | $posDoc = stripos($buffer, '<!DOCTYPE'); |
| 178: | |
| 179: | return substr($buffer, $posDoc); |
| 180: | } |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | function _minifyHTML($html) |
| 188: | { |
| 189: | if (_cfg('minifyHTML')) { |
| 190: | |
| 191: | |
| 192: | |
| 193: | return preg_replace(array('/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s'), array('>', '<', '\\1'), $html); |
| 194: | } |
| 195: | |
| 196: | return $html; |
| 197: | } |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | |
| 204: | function _dir($name) |
| 205: | { |
| 206: | switch($name) { |
| 207: | case 'inc': |
| 208: | return ROOT . 'inc' . _DS_; |
| 209: | case 'db': |
| 210: | return ROOT . 'db' . _DS_; |
| 211: | case 'lib': |
| 212: | return ROOT . 'lib' . _DS_; |
| 213: | case 'helper': |
| 214: | return ROOT . 'lib' . _DS_ . 'helpers' . _DS_; |
| 215: | case 'class': |
| 216: | return ROOT . 'lib' . _DS_ . 'classes' . _DS_; |
| 217: | case 'i18n': |
| 218: | return ROOT . 'i18n' . _DS_; |
| 219: | case 'vendor': |
| 220: | return ROOT . 'vendor' . _DS_; |
| 221: | case 'business': |
| 222: | return ROOT . 'business' . _DS_; |
| 223: | case 'asset': |
| 224: | return ROOT . 'assets' . _DS_; |
| 225: | case 'image': |
| 226: | return ROOT . 'assets' . _DS_ . 'images' . _DS_; |
| 227: | case 'file': |
| 228: | return ROOT . 'files' . _DS_; |
| 229: | case 'cache': |
| 230: | return ROOT . 'files' . _DS_ . 'cache' . _DS_; |
| 231: | case 'test': |
| 232: | return ROOT . 'tests' . _DS_; |
| 233: | } |
| 234: | |
| 235: | return ROOT; |
| 236: | } |
| 237: | |
| 238: | |
| 239: | |
| 240: | |
| 241: | |
| 242: | |
| 243: | |
| 244: | function _loader($name, $path = HELPER) |
| 245: | { |
| 246: | global $lc_autoload; |
| 247: | |
| 248: | $path = rtrim($path, _DS_) . _DS_; |
| 249: | |
| 250: | $dir = $path . $name . _DS_; |
| 251: | if (is_dir($dir)) { |
| 252: | |
| 253: | $files = scandir($dir); |
| 254: | foreach ($files as $fileName) { |
| 255: | $dir = rtrim(rtrim($dir, '/'), '\\'); |
| 256: | $file = $dir . _DS_ . $fileName; |
| 257: | |
| 258: | if (!in_array(substr($fileName, -3), array('php', 'inc')) || !is_file($file)) { |
| 259: | continue; |
| 260: | } |
| 261: | |
| 262: | if (file_exists($file)) { |
| 263: | $lc_autoload[] = $file; |
| 264: | } |
| 265: | } |
| 266: | } else { |
| 267: | |
| 268: | $name = rtrim($name, '.php'); |
| 269: | $lc_autoload[] = $path . $name . '.php'; |
| 270: | } |
| 271: | |
| 272: | $lc_autoload = array_unique($lc_autoload); |
| 273: | } |
| 274: | |
| 275: | |
| 276: | |
| 277: | |
| 278: | |
| 279: | |
| 280: | |
| 281: | function _unloader($name, $path = HELPER) |
| 282: | { |
| 283: | global $lc_autoload; |
| 284: | |
| 285: | $file = $path . $name . '.php'; |
| 286: | $key = array_search($file, $lc_autoload); |
| 287: | if ($key !== false) { |
| 288: | unset($lc_autoload[$key]); |
| 289: | $lc_autoload = array_values($lc_autoload); |
| 290: | } |
| 291: | } |
| 292: | |
| 293: | |
| 294: | |
| 295: | |
| 296: | |
| 297: | |
| 298: | |
| 299: | |
| 300: | |
| 301: | |
| 302: | function _readyloader($name, $path = HELPER) |
| 303: | { |
| 304: | global $lc_autoload; |
| 305: | |
| 306: | if (stripos($name, '.php') === false) { |
| 307: | $file = $path . $name . '.php'; |
| 308: | } else { |
| 309: | $file = $name; |
| 310: | } |
| 311: | |
| 312: | return (array_search($file, $lc_autoload) !== false && is_file($file) && file_exists($file)) ? $file : false; |
| 313: | } |
| 314: | |
| 315: | |
| 316: | |
| 317: | |
| 318: | |
| 319: | |
| 320: | |
| 321: | function _autoloadDir($dir, $scope = '') |
| 322: | { |
| 323: | $scope = trim($scope, '/'); |
| 324: | |
| 325: | if (is_dir($dir)) { |
| 326: | $files = scandir($dir); |
| 327: | foreach ($files as $fileName) { |
| 328: | if (in_array($fileName, array('.', '..', '.gitkeep', 'README.md', 'empty'))) { |
| 329: | continue; |
| 330: | } |
| 331: | |
| 332: | $dir = rtrim(rtrim($dir, '/'), '\\'); |
| 333: | $file = $dir . _DS_ . $fileName; |
| 334: | |
| 335: | if (is_dir($file)) { |
| 336: | _autoloadDir($file, $scope); |
| 337: | break; |
| 338: | } |
| 339: | |
| 340: | if (!in_array(substr($fileName, -3), array('php', 'inc')) || !is_file($file)) { |
| 341: | continue; |
| 342: | } |
| 343: | |
| 344: | if (file_exists($file) && (empty($scope) || $scope == LC_NAMESPACE)) { |
| 345: | require_once $file; |
| 346: | } |
| 347: | } |
| 348: | } |
| 349: | } |
| 350: | |
| 351: | |
| 352: | |
| 353: | |
| 354: | |
| 355: | |
| 356: | |
| 357: | function _script() |
| 358: | { |
| 359: | $sitewideWarnings = _cfg('sitewideWarnings'); |
| 360: | $sites = _cfg('sites'); |
| 361: | $script = '<script type="text/javascript">'; |
| 362: | $script .= 'var LC = {};'; |
| 363: | if (WEB_ROOT) { |
| 364: | $script .= 'var WEB_ROOT = "'.WEB_ROOT.'";'; |
| 365: | $script .= 'LC.root = WEB_ROOT;'; |
| 366: | } |
| 367: | if (WEB_APP_ROOT) { |
| 368: | $script .= 'var WEB_APP_ROOT = "'.WEB_APP_ROOT.'";'; |
| 369: | $script .= 'LC.appRoot = WEB_ROOT;'; |
| 370: | } |
| 371: | $script .= 'LC.env = "' . __env() . '";'; |
| 372: | $script .= 'LC.self = "'._self().'";'; |
| 373: | $script .= 'LC.lang = "'._lang().'";'; |
| 374: | $script .= 'LC.baseURL = "'._cfg('baseURL').'/";'; |
| 375: | $script .= 'LC.route = "'._r().'";'; |
| 376: | $script .= 'LC.cleanRoute = "'._cfg('cleanRoute').'";'; |
| 377: | $script .= 'LC.namespace = "'.LC_NAMESPACE.'";'; |
| 378: | $script .= 'LC.sites = "' . base64_encode(is_array($sites) && count($sites) ? json_encode($sites) : '[]') . _randomCode() . '";'; |
| 379: | $script .= 'LC.sitewideWarnings = '.json_encode($sitewideWarnings).';'; |
| 380: | |
| 381: | $csrf = ['name' => _cfg('csrfHeaderTokenName'), 'value' => _encrypt(Form::$formToken)]; |
| 382: | $script .= 'LC.csrfToken = ' . json_encode($csrf) . ';'; |
| 383: | |
| 384: | |
| 385: | |
| 386: | if (function_exists('__script')) { |
| 387: | __script(); |
| 388: | } |
| 389: | |
| 390: | |
| 391: | $jsVars = _cfg('jsVars'); |
| 392: | $script .= 'LC.vars = {};'; |
| 393: | $script .= 'LC.vars.baseDir = "' . _cfg('baseDir') . '";'; |
| 394: | if (count($jsVars)) { |
| 395: | foreach ($jsVars as $name => $val) { |
| 396: | if (is_object($val)) { |
| 397: | $val = (array) $val; |
| 398: | } |
| 399: | |
| 400: | if (is_array($val)) { |
| 401: | $script .= 'LC.vars.'.$name.' = '.json_encode($val).';'; |
| 402: | } elseif (is_numeric($val)) { |
| 403: | $script .= 'LC.vars.'.$name.' = '.$val.';'; |
| 404: | } else { |
| 405: | $script .= 'LC.vars.'.$name.' = "'.$val.'";'; |
| 406: | } |
| 407: | } |
| 408: | } |
| 409: | $script .= '</script>'; |
| 410: | echo $script; |
| 411: | } |
| 412: | |
| 413: | |
| 414: | |
| 415: | |
| 416: | |
| 417: | |
| 418: | function _addJsVar($name, $value = '') |
| 419: | { |
| 420: | global $lc_jsVars; |
| 421: | $lc_jsVars[$name] = $value; |
| 422: | } |
| 423: | |
| 424: | |
| 425: | |
| 426: | |
| 427: | |
| 428: | |
| 429: | |
| 430: | |
| 431: | |
| 432: | |
| 433: | |
| 434: | function _js($file, $subDir = '', $return = false) |
| 435: | { |
| 436: | $version = '?v' . _cfg('assetVersion'); |
| 437: | |
| 438: | if (stripos($file, 'http') === 0 || stripos($file, '//') === 0) { |
| 439: | $html = '<script src="' . $file . '" type="text/javascript"></script>'; |
| 440: | if ($return) { |
| 441: | return $html; |
| 442: | } |
| 443: | |
| 444: | echo $html; |
| 445: | return true; |
| 446: | } |
| 447: | |
| 448: | if ($subDir) { |
| 449: | $subDir = trim('assets/' . $subDir, '/') . '/'; |
| 450: | } else { |
| 451: | $subDir = 'assets/js/'; |
| 452: | } |
| 453: | |
| 454: | $includeFiles = array(); |
| 455: | if ($file == 'jquery.ui' || $file == 'jquery-ui') { |
| 456: | |
| 457: | $file = (stripos($file, '.js') !== false) ? $file : 'jquery-ui.min.js'; |
| 458: | $includeFiles[] = $subDir . 'vendor/jquery-ui/' . $file; |
| 459: | } elseif ($file == 'jquery') { |
| 460: | |
| 461: | $file = (stripos($file, '.js') !== false) ? $file : 'jquery.min.js'; |
| 462: | $includeFiles[] = $subDir . 'vendor/jquery/' . $file; |
| 463: | } else { |
| 464: | |
| 465: | $includeFiles[] = $subDir . $file; |
| 466: | } |
| 467: | |
| 468: | foreach ($includeFiles as $includeFile) { |
| 469: | $includeFile = _i($includeFile); |
| 470: | if (stripos($includeFile, 'http') === 0) { |
| 471: | if (stripos($includeFile, WEB_APP_ROOT) === 0) { |
| 472: | $fileWithSystemPath = APP_ROOT . str_replace(WEB_APP_ROOT, '', $includeFile); |
| 473: | } else { |
| 474: | $fileWithSystemPath = ROOT . str_replace(WEB_ROOT, '', $includeFile); |
| 475: | } |
| 476: | if (file_exists($fileWithSystemPath)) { |
| 477: | $html = '<script src="' . $includeFile . $version . '" type="text/javascript"></script>'; |
| 478: | if ($return) { |
| 479: | return $html; |
| 480: | } |
| 481: | |
| 482: | echo $html; |
| 483: | return true; |
| 484: | } |
| 485: | } |
| 486: | } |
| 487: | |
| 488: | return false; |
| 489: | } |
| 490: | |
| 491: | |
| 492: | |
| 493: | |
| 494: | |
| 495: | |
| 496: | |
| 497: | |
| 498: | |
| 499: | |
| 500: | |
| 501: | function _css($file, $subDir = '', $return = false) |
| 502: | { |
| 503: | $version = '?v' . _cfg('assetVersion'); |
| 504: | |
| 505: | if (stripos($file, 'http') === 0 || stripos($file, '//') === 0) { |
| 506: | $html = '<link href="' . $file . '" rel="stylesheet" type="text/css" />'; |
| 507: | if ($return) { |
| 508: | return $html; |
| 509: | } |
| 510: | |
| 511: | echo $html; |
| 512: | return true; |
| 513: | } |
| 514: | |
| 515: | if ($subDir) { |
| 516: | $subDir = trim('assets/' . $subDir, '/') . '/'; |
| 517: | } else { |
| 518: | $subDir = 'assets/css/'; |
| 519: | } |
| 520: | |
| 521: | $includeFiles = array(); |
| 522: | if ($file == 'jquery.ui' || $file == 'jquery-ui') { |
| 523: | |
| 524: | $includeFiles[] = 'assets/js/vendor/jquery-ui/jquery-ui.min.css'; |
| 525: | } else { |
| 526: | |
| 527: | $includeFiles[] = $subDir . $file; |
| 528: | } |
| 529: | |
| 530: | foreach ($includeFiles as $includeFile) { |
| 531: | $includeFile = _i($includeFile); |
| 532: | if (stripos($includeFile, 'http') === 0) { |
| 533: | if (stripos($includeFile, WEB_APP_ROOT) === 0) { |
| 534: | $fileWithSystemPath = APP_ROOT . str_replace(WEB_APP_ROOT, '', $includeFile); |
| 535: | } else { |
| 536: | $fileWithSystemPath = ROOT . str_replace(WEB_ROOT, '', $includeFile); |
| 537: | } |
| 538: | |
| 539: | if (file_exists($fileWithSystemPath)) { |
| 540: | $html = '<link href="' . $includeFile . $version . '" rel="stylesheet" type="text/css" />'; |
| 541: | if ($return) { |
| 542: | return $html; |
| 543: | } |
| 544: | |
| 545: | echo $html; |
| 546: | return true; |
| 547: | } |
| 548: | } |
| 549: | } |
| 550: | |
| 551: | return false; |
| 552: | } |
| 553: | |
| 554: | |
| 555: | |
| 556: | |
| 557: | |
| 558: | |
| 559: | |
| 560: | |
| 561: | function _img($file, $version = null) |
| 562: | { |
| 563: | $fileWithPath = 'assets/images/' . $file; |
| 564: | $fileWithPath = _i($fileWithPath); |
| 565: | |
| 566: | if (empty($fileWithPath)) { |
| 567: | return ''; |
| 568: | } |
| 569: | |
| 570: | if (stripos($fileWithPath, APP_ROOT) === 0) { |
| 571: | $img = WEB_APP_ROOT . str_replace(APP_ROOT, '', $fileWithPath); |
| 572: | } else { |
| 573: | $img = WEB_ROOT . str_replace(ROOT, '', $fileWithPath); |
| 574: | } |
| 575: | |
| 576: | if ($version) { |
| 577: | $img .= '?v' . _cfg('assetVersion'); |
| 578: | } |
| 579: | |
| 580: | return $img; |
| 581: | } |
| 582: | |
| 583: | if (!function_exists('_image')) { |
| 584: | |
| 585: | |
| 586: | |
| 587: | |
| 588: | |
| 589: | |
| 590: | |
| 591: | |
| 592: | |
| 593: | |
| 594: | |
| 595: | |
| 596: | |
| 597: | |
| 598: | |
| 599: | function _image($file, $caption = '', $dimension = '0x0', array $attributes = array()) |
| 600: | { |
| 601: | $directory = array( |
| 602: | 'images' => IMAGE, |
| 603: | 'files' => FILE, |
| 604: | ); |
| 605: | |
| 606: | foreach ($directory as $dir => $path) { |
| 607: | $image = $path . $file; |
| 608: | if (is_file($image) && file_exists($image)) { |
| 609: | list($width, $height) = getimagesize($image); |
| 610: | if (strpos($path, 'assets') !== false) { |
| 611: | $dir = 'assets/images'; |
| 612: | } |
| 613: | break; |
| 614: | } |
| 615: | } |
| 616: | if (isset($width) && isset($height)) { |
| 617: | |
| 618: | $image = WEB_ROOT . $dir . '/' . $file; |
| 619: | echo File::img($image, $caption, $width.'x'.$height, $dimension, $attributes); |
| 620: | } else { |
| 621: | |
| 622: | echo '<div class="image404" align="center">'; |
| 623: | echo function_exists('_t') ? _t('No Image') : 'No Image'; |
| 624: | echo '</div>'; |
| 625: | } |
| 626: | } |
| 627: | } |
| 628: | |
| 629: | if (!function_exists('_pr')) { |
| 630: | |
| 631: | |
| 632: | |
| 633: | |
| 634: | |
| 635: | |
| 636: | |
| 637: | |
| 638: | |
| 639: | |
| 640: | |
| 641: | function _pr($input, $pre = true) |
| 642: | { |
| 643: | if ($pre) { |
| 644: | echo '<pre>'; |
| 645: | } |
| 646: | if (is_array($input) || is_object($input)) { |
| 647: | print_r($input); |
| 648: | } else { |
| 649: | if (is_bool($input)) { |
| 650: | var_dump($input); |
| 651: | } else { |
| 652: | echo $input; |
| 653: | } |
| 654: | if ($pre == false) { |
| 655: | echo '<br>'; |
| 656: | } |
| 657: | } |
| 658: | if ($pre) { |
| 659: | echo '</pre>'; |
| 660: | } |
| 661: | } |
| 662: | } |
| 663: | |
| 664: | if (!function_exists('_dpr')) { |
| 665: | |
| 666: | |
| 667: | |
| 668: | |
| 669: | |
| 670: | |
| 671: | |
| 672: | |
| 673: | |
| 674: | |
| 675: | |
| 676: | function _dpr($input, $pre = true) |
| 677: | { |
| 678: | _pr($input); |
| 679: | exit; |
| 680: | } |
| 681: | } |
| 682: | |
| 683: | if (!function_exists('_dump')) { |
| 684: | |
| 685: | |
| 686: | |
| 687: | |
| 688: | |
| 689: | |
| 690: | |
| 691: | |
| 692: | |
| 693: | function _dump($input, $pre = true) |
| 694: | { |
| 695: | if ($pre) { |
| 696: | echo '<pre>'; |
| 697: | } |
| 698: | var_dump($input); |
| 699: | if ($pre) { |
| 700: | echo '</pre>'; |
| 701: | } |
| 702: | } |
| 703: | } |
| 704: | |
| 705: | |
| 706: | |
| 707: | |
| 708: | |
| 709: | |
| 710: | |
| 711: | function _h($string) |
| 712: | { |
| 713: | if (empty($string)) { |
| 714: | return $string; |
| 715: | } |
| 716: | |
| 717: | $string = stripslashes($string); |
| 718: | $string = htmlspecialchars_decode($string, ENT_QUOTES); |
| 719: | |
| 720: | return htmlspecialchars($string, ENT_QUOTES); |
| 721: | } |
| 722: | |
| 723: | |
| 724: | |
| 725: | |
| 726: | |
| 727: | function _lang() |
| 728: | { |
| 729: | return _cfg('lang'); |
| 730: | } |
| 731: | |
| 732: | |
| 733: | |
| 734: | |
| 735: | |
| 736: | |
| 737: | |
| 738: | |
| 739: | |
| 740: | function _getLang() |
| 741: | { |
| 742: | if (function_exists('__getLang')) { |
| 743: | return __getLang(); |
| 744: | } |
| 745: | |
| 746: | $lang = (_arg('lang')) ? _arg('lang') : _defaultLang(); |
| 747: | |
| 748: | return ($lang) ? $lang : _defaultLang(); |
| 749: | } |
| 750: | |
| 751: | |
| 752: | |
| 753: | |
| 754: | |
| 755: | function _defaultLang() |
| 756: | { |
| 757: | return _cfg('defaultLang'); |
| 758: | } |
| 759: | |
| 760: | |
| 761: | |
| 762: | |
| 763: | |
| 764: | |
| 765: | function _langs($excepts = null) |
| 766: | { |
| 767: | global $lc_languages; |
| 768: | |
| 769: | $langs = array(); |
| 770: | if ($excepts) { |
| 771: | foreach ($lc_languages as $lcode => $lname) { |
| 772: | if (is_array($excepts) && in_array($lcode, $excepts)) { |
| 773: | continue; |
| 774: | } |
| 775: | if (is_string($excepts) && $lcode == $excepts) { |
| 776: | continue; |
| 777: | } |
| 778: | $langs[$lcode] = $lname; |
| 779: | } |
| 780: | } else { |
| 781: | $langs = $lc_languages; |
| 782: | } |
| 783: | |
| 784: | return count($langs) ? $langs : false; |
| 785: | } |
| 786: | |
| 787: | |
| 788: | |
| 789: | |
| 790: | |
| 791: | |
| 792: | function _queryLang($lang = null) |
| 793: | { |
| 794: | if (!$lang) { |
| 795: | $lang = _cfg('lang');; |
| 796: | } |
| 797: | |
| 798: | return str_replace('-', '_', $lang); |
| 799: | } |
| 800: | |
| 801: | |
| 802: | |
| 803: | |
| 804: | |
| 805: | |
| 806: | function _urlLang($lang = null) |
| 807: | { |
| 808: | if (!$lang) { |
| 809: | $lang = _cfg('lang'); |
| 810: | } |
| 811: | |
| 812: | return str_replace('_', '-', $lang); |
| 813: | } |
| 814: | |
| 815: | |
| 816: | |
| 817: | |
| 818: | |
| 819: | function _defaultQueryLang() |
| 820: | { |
| 821: | return str_replace('-', '_', _cfg('defaultLang')); |
| 822: | } |
| 823: | |
| 824: | |
| 825: | |
| 826: | |
| 827: | |
| 828: | |
| 829: | |
| 830: | |
| 831: | |
| 832: | |
| 833: | function _langName($lang = '') |
| 834: | { |
| 835: | if (!_multilingual()) { |
| 836: | return ''; |
| 837: | } |
| 838: | |
| 839: | global $lc_languages; |
| 840: | $lang = str_replace('_', '-', $lang); |
| 841: | |
| 842: | if (isset($lc_languages[$lang])) { |
| 843: | return $lc_languages[$lang]; |
| 844: | } else { |
| 845: | return $lc_languages[_cfg('defaultLang')]; |
| 846: | } |
| 847: | } |
| 848: | |
| 849: | |
| 850: | |
| 851: | |
| 852: | |
| 853: | function _multilingual() |
| 854: | { |
| 855: | if (_cfg('languages')) { |
| 856: | return count(_cfg('languages')) > 1; |
| 857: | } else { |
| 858: | return false; |
| 859: | } |
| 860: | } |
| 861: | |
| 862: | |
| 863: | |
| 864: | |
| 865: | |
| 866: | |
| 867: | |
| 868: | function _protocol() |
| 869: | { |
| 870: | $protocol = current(explode('/', $_SERVER['SERVER_PROTOCOL'])); |
| 871: | |
| 872: | return strtolower($protocol); |
| 873: | } |
| 874: | |
| 875: | |
| 876: | |
| 877: | |
| 878: | |
| 879: | |
| 880: | function _ssl() |
| 881: | { |
| 882: | return _cfg('ssl'); |
| 883: | } |
| 884: | |
| 885: | |
| 886: | |
| 887: | |
| 888: | |
| 889: | |
| 890: | |
| 891: | |
| 892: | |
| 893: | |
| 894: | |
| 895: | function _r() |
| 896: | { |
| 897: | return route_path(); |
| 898: | } |
| 899: | |
| 900: | |
| 901: | |
| 902: | |
| 903: | |
| 904: | |
| 905: | |
| 906: | |
| 907: | |
| 908: | |
| 909: | |
| 910: | |
| 911: | function _rr() |
| 912: | { |
| 913: | if (!_isRewriteRule()) { |
| 914: | return _r(); |
| 915: | } |
| 916: | |
| 917: | $uri = REQUEST_URI; |
| 918: | if (strpos($uri, '?') !== false) { |
| 919: | $uri = substr($uri, 0, strpos($uri, '?')); |
| 920: | } |
| 921: | |
| 922: | return $uri; |
| 923: | } |
| 924: | |
| 925: | |
| 926: | |
| 927: | |
| 928: | |
| 929: | |
| 930: | function _cr() |
| 931: | { |
| 932: | return _cfg('cleanRoute'); |
| 933: | } |
| 934: | |
| 935: | |
| 936: | |
| 937: | |
| 938: | |
| 939: | |
| 940: | |
| 941: | |
| 942: | |
| 943: | |
| 944: | |
| 945: | |
| 946: | |
| 947: | |
| 948: | |
| 949: | |
| 950: | function _url($path = null, $queryStr = array(), $lang = '') |
| 951: | { |
| 952: | return route_url($path, $queryStr, $lang); |
| 953: | } |
| 954: | |
| 955: | |
| 956: | |
| 957: | |
| 958: | |
| 959: | |
| 960: | |
| 961: | |
| 962: | |
| 963: | |
| 964: | |
| 965: | |
| 966: | |
| 967: | |
| 968: | |
| 969: | function _self($queryStr = array(), $lang = '') |
| 970: | { |
| 971: | return route_url(null, $queryStr, $lang); |
| 972: | } |
| 973: | |
| 974: | |
| 975: | |
| 976: | |
| 977: | |
| 978: | |
| 979: | |
| 980: | function _header($status, $message = null) |
| 981: | { |
| 982: | _g('httpStatusCode', $status); |
| 983: | |
| 984: | if (PHP_SAPI != 'cli' && _cfg('env') != ENV_TEST && __env() != ENV_TEST) { |
| 985: | header('HTTP/1.1 ' . $status . ($message ? ' ' . $message : '')); |
| 986: | } |
| 987: | } |
| 988: | |
| 989: | |
| 990: | |
| 991: | |
| 992: | |
| 993: | |
| 994: | |
| 995: | |
| 996: | |
| 997: | |
| 998: | |
| 999: | |
| 1000: | |
| 1001: | |
| 1002: | |
| 1003: | |
| 1004: | |
| 1005: | |
| 1006: | function _redirect($path = null, $queryStr = array(), $lang = '', $status = null) |
| 1007: | { |
| 1008: | if ($path && stripos($path, 'http') === 0) { |
| 1009: | if ($status === 301) { |
| 1010: | _header(301, 'Moved Permanently'); |
| 1011: | } |
| 1012: | header('Location: ' . $path); |
| 1013: | exit; |
| 1014: | } |
| 1015: | |
| 1016: | if ($path == 'self') { |
| 1017: | $url = _self(null, $lang); |
| 1018: | } else { |
| 1019: | $url = route_url($path, $queryStr, $lang); |
| 1020: | } |
| 1021: | |
| 1022: | if ($status === 301) { |
| 1023: | _header(301, 'Moved Permanently'); |
| 1024: | } |
| 1025: | |
| 1026: | header('Location: ' . $url); |
| 1027: | exit; |
| 1028: | } |
| 1029: | |
| 1030: | |
| 1031: | |
| 1032: | |
| 1033: | |
| 1034: | |
| 1035: | |
| 1036: | |
| 1037: | |
| 1038: | |
| 1039: | |
| 1040: | |
| 1041: | |
| 1042: | |
| 1043: | |
| 1044: | |
| 1045: | function _redirect301($path = null, $queryStr = array(), $lang = '') |
| 1046: | { |
| 1047: | _redirect($path, $queryStr, $lang, 301); |
| 1048: | } |
| 1049: | |
| 1050: | |
| 1051: | |
| 1052: | |
| 1053: | |
| 1054: | |
| 1055: | function _page401($message = '') |
| 1056: | { |
| 1057: | $message = $message ?: _t('Access Denied'); |
| 1058: | |
| 1059: | if (_isContentType('application/json')) { |
| 1060: | _jsonError($message, '', 401); |
| 1061: | } |
| 1062: | |
| 1063: | _header(401); |
| 1064: | |
| 1065: | _cfg('layoutMode', true); |
| 1066: | include(INC . 'tpl/401.php'); |
| 1067: | exit; |
| 1068: | } |
| 1069: | |
| 1070: | |
| 1071: | |
| 1072: | |
| 1073: | |
| 1074: | |
| 1075: | function _page403($message = '') |
| 1076: | { |
| 1077: | $message = $message ?: _t('403 Forbidden'); |
| 1078: | |
| 1079: | if (_isContentType('application/json')) { |
| 1080: | _jsonError($message, '', 403); |
| 1081: | } |
| 1082: | |
| 1083: | _header(403); |
| 1084: | |
| 1085: | _cfg('layoutMode', true); |
| 1086: | include(INC . 'tpl/403.php'); |
| 1087: | exit; |
| 1088: | } |
| 1089: | |
| 1090: | |
| 1091: | |
| 1092: | |
| 1093: | |
| 1094: | |
| 1095: | |
| 1096: | function _page404($message = '', $entity = '') |
| 1097: | { |
| 1098: | $message = $message ?: _t('404 Not Found'); |
| 1099: | |
| 1100: | if (_isContentType('application/json')) { |
| 1101: | _jsonError($message, $entity, 404); |
| 1102: | } |
| 1103: | |
| 1104: | _header(404); |
| 1105: | |
| 1106: | _cfg('layoutMode', true); |
| 1107: | include(INC . 'tpl/404.php'); |
| 1108: | exit; |
| 1109: | } |
| 1110: | |
| 1111: | |
| 1112: | |
| 1113: | |
| 1114: | |
| 1115: | |
| 1116: | |
| 1117: | |
| 1118: | function _error($message, $code, $status = 500) |
| 1119: | { |
| 1120: | if (_isContentType('application/json')) { |
| 1121: | _json(['error' => $message], $status); |
| 1122: | } |
| 1123: | |
| 1124: | _cfg('layoutMode', true); |
| 1125: | _g('httpStatusCode', $status); |
| 1126: | $type = __kernelErrorTypes($code); |
| 1127: | |
| 1128: | _header($status); |
| 1129: | |
| 1130: | include(INC . 'tpl/exception.php'); |
| 1131: | exit; |
| 1132: | } |
| 1133: | |
| 1134: | |
| 1135: | |
| 1136: | |
| 1137: | |
| 1138: | function _isRewriteRule() |
| 1139: | { |
| 1140: | return strcasecmp(REQUEST_URI, _r()) !== 0; |
| 1141: | } |
| 1142: | |
| 1143: | |
| 1144: | |
| 1145: | |
| 1146: | |
| 1147: | |
| 1148: | function _canonical($url = null) |
| 1149: | { |
| 1150: | global $lc_canonical; |
| 1151: | if (!is_null($url)) { |
| 1152: | $lc_canonical = $url; |
| 1153: | } else { |
| 1154: | return (_cfg('canonical')) ? _cfg('canonical') : _url(); |
| 1155: | } |
| 1156: | } |
| 1157: | |
| 1158: | |
| 1159: | |
| 1160: | |
| 1161: | |
| 1162: | function _hreflang() |
| 1163: | { |
| 1164: | global $lc_languages; |
| 1165: | if (_multilingual()) { |
| 1166: | foreach ($lc_languages as $hrefLang => $langDesc) { |
| 1167: | if (_canonical() == _url()) { |
| 1168: | $alternate = _url('', null, $hrefLang); |
| 1169: | $xdefault = _url('', null, false); |
| 1170: | } else { |
| 1171: | $alternate = preg_replace('/\/'._lang().'\b/', '/'.$hrefLang, _canonical()); |
| 1172: | $xdefault = preg_replace('/\/'._lang().'\b/', '', _canonical()); |
| 1173: | } |
| 1174: | echo '<link rel="alternate" hreflang="'.$hrefLang.'" href="'.$alternate.'" />'."\n"; |
| 1175: | } |
| 1176: | echo '<link rel="alternate" hreflang="x-default" href="'.$xdefault.'" />'."\n"; |
| 1177: | } |
| 1178: | } |
| 1179: | |
| 1180: | |
| 1181: | |
| 1182: | |
| 1183: | |
| 1184: | |
| 1185: | |
| 1186: | |
| 1187: | |
| 1188: | |
| 1189: | |
| 1190: | |
| 1191: | function _getLangInURI() |
| 1192: | { |
| 1193: | global $lc_languages; |
| 1194: | |
| 1195: | if (!isset($_SERVER['REQUEST_URI'])) { |
| 1196: | return false; |
| 1197: | } |
| 1198: | |
| 1199: | if (!is_array($lc_languages)) { |
| 1200: | $lc_languages = array('en' => 'English'); |
| 1201: | } |
| 1202: | |
| 1203: | $baseURL = trim(_cfg('baseURL'), '/'); |
| 1204: | $baseURL = ($baseURL) ? "/$baseURL/" : '/'; |
| 1205: | $baseURL = str_replace('/', '\/', $baseURL); |
| 1206: | $baseURL = str_replace('.', '\.', $baseURL); |
| 1207: | $regex = '/^('.$baseURL.')\b('.implode('|', array_keys($lc_languages)).'){1}\b(\/?)/i'; |
| 1208: | |
| 1209: | if (preg_match($regex, $_SERVER['REQUEST_URI'], $matches)) { |
| 1210: | return $matches[2]; |
| 1211: | } |
| 1212: | |
| 1213: | return false; |
| 1214: | } |
| 1215: | |
| 1216: | |
| 1217: | |
| 1218: | |
| 1219: | |
| 1220: | |
| 1221: | |
| 1222: | function _validHost($host) |
| 1223: | { |
| 1224: | return preg_match('/^\[?(?:[a-zA-Z0-9-:\]_]+\.?)+$/', $host); |
| 1225: | } |
| 1226: | |
| 1227: | |
| 1228: | |
| 1229: | |
| 1230: | |
| 1231: | |
| 1232: | |
| 1233: | function _title() |
| 1234: | { |
| 1235: | global $lc_siteName; |
| 1236: | global $lc_titleSeparator; |
| 1237: | |
| 1238: | $args = func_get_args(); |
| 1239: | |
| 1240: | if (count($args) == 0) { |
| 1241: | $title = _app('title'); |
| 1242: | if (is_array($title)) { |
| 1243: | $args = $title; |
| 1244: | } |
| 1245: | |
| 1246: | if (is_string($title)) { |
| 1247: | $args = array($title); |
| 1248: | } |
| 1249: | } |
| 1250: | |
| 1251: | if (count($args) == 0) { |
| 1252: | return $lc_siteName; |
| 1253: | } |
| 1254: | |
| 1255: | if (count($args) == 1) { |
| 1256: | if (is_array($args[0])) { |
| 1257: | $args = _filterArrayEmpty($args[0]); |
| 1258: | $title = $args; |
| 1259: | } else { |
| 1260: | $title = ($args[0]) ? array($args[0]) : array(); |
| 1261: | } |
| 1262: | } else { |
| 1263: | $args = _filterArrayEmpty($args); |
| 1264: | $title = $args; |
| 1265: | } |
| 1266: | |
| 1267: | $lc_titleSeparator = trim($lc_titleSeparator); |
| 1268: | if ($lc_titleSeparator) { |
| 1269: | $lc_titleSeparator = ' '.$lc_titleSeparator.' '; |
| 1270: | } else { |
| 1271: | $lc_titleSeparator = ' '; |
| 1272: | } |
| 1273: | |
| 1274: | if (count($title)) { |
| 1275: | $title = implode($lc_titleSeparator, $title); |
| 1276: | if ($lc_siteName) { |
| 1277: | $title .= ' | '.$lc_siteName; |
| 1278: | } |
| 1279: | |
| 1280: | return strip_tags($title); |
| 1281: | } |
| 1282: | |
| 1283: | return $lc_siteName; |
| 1284: | } |
| 1285: | |
| 1286: | |
| 1287: | |
| 1288: | |
| 1289: | |
| 1290: | |
| 1291: | |
| 1292: | function _filterArrayEmpty($input) |
| 1293: | { |
| 1294: | return array_filter($input, '_notEmpty'); |
| 1295: | } |
| 1296: | |
| 1297: | |
| 1298: | |
| 1299: | |
| 1300: | |
| 1301: | |
| 1302: | |
| 1303: | function _notEmpty($value) |
| 1304: | { |
| 1305: | return trim($value) !== ''; |
| 1306: | } |
| 1307: | |
| 1308: | |
| 1309: | |
| 1310: | |
| 1311: | |
| 1312: | |
| 1313: | |
| 1314: | function _breadcrumb() |
| 1315: | { |
| 1316: | global $lc_breadcrumbSeparator; |
| 1317: | |
| 1318: | $args = func_get_args(); |
| 1319: | |
| 1320: | if (!$lc_breadcrumbSeparator) { |
| 1321: | $lc_breadcrumbSeparator = '»'; |
| 1322: | } |
| 1323: | |
| 1324: | if (count($args) == 1 && is_array($args[0])) { |
| 1325: | $args = $args[0]; |
| 1326: | } |
| 1327: | |
| 1328: | echo implode(" {$lc_breadcrumbSeparator} ", $args); |
| 1329: | } |
| 1330: | |
| 1331: | |
| 1332: | |
| 1333: | |
| 1334: | |
| 1335: | |
| 1336: | |
| 1337: | |
| 1338: | |
| 1339: | |
| 1340: | function _shorten($str, $length = 50, $trail = '...') |
| 1341: | { |
| 1342: | if (empty($str)) { |
| 1343: | return $str; |
| 1344: | } |
| 1345: | |
| 1346: | $str = strip_tags(trim($str)); |
| 1347: | if (strlen($str) <= $length) { |
| 1348: | return $str; |
| 1349: | } |
| 1350: | |
| 1351: | $short = trim(substr($str, 0, $length)); |
| 1352: | $lastSpacePos = strrpos($short, ' '); |
| 1353: | if ($lastSpacePos !== false) { |
| 1354: | $short = substr($short, 0, $lastSpacePos); |
| 1355: | } |
| 1356: | |
| 1357: | if ($trail) { |
| 1358: | $short = rtrim($short, '.') . $trail; |
| 1359: | } |
| 1360: | |
| 1361: | return $short; |
| 1362: | } |
| 1363: | |
| 1364: | if (!function_exists('_fstr')) { |
| 1365: | |
| 1366: | |
| 1367: | |
| 1368: | |
| 1369: | |
| 1370: | |
| 1371: | |
| 1372: | |
| 1373: | |
| 1374: | function _fstr($value, $glue = ', ', $lastGlue = 'and') |
| 1375: | { |
| 1376: | if (empty($value)) { |
| 1377: | return $value; |
| 1378: | } |
| 1379: | |
| 1380: | if (!is_array($value)) { |
| 1381: | return $value == '' ? _nullFill($value) : nl2br($value); |
| 1382: | } elseif (is_array($value) && sizeof($value) > 1) { |
| 1383: | $last = array_slice($value, -2, 2); |
| 1384: | $lastImplode = implode(' '.$lastGlue.' ', $last); |
| 1385: | $first = array_slice($value, 0, sizeof($value)-2); |
| 1386: | $firstImplode = implode($glue, $first); |
| 1387: | |
| 1388: | return $firstImplode ? $firstImplode.$glue.$lastImplode : $lastImplode; |
| 1389: | } else { |
| 1390: | return nl2br($value[0]); |
| 1391: | } |
| 1392: | } |
| 1393: | } |
| 1394: | |
| 1395: | if (!function_exists('_fnum')) { |
| 1396: | |
| 1397: | |
| 1398: | |
| 1399: | |
| 1400: | |
| 1401: | |
| 1402: | |
| 1403: | |
| 1404: | |
| 1405: | function _fnum($value, $decimals = 2, $unit = '') |
| 1406: | { |
| 1407: | if ($value === '') { |
| 1408: | return _nullFill($value); |
| 1409: | } elseif (is_numeric($value)) { |
| 1410: | $value = number_format($value, $decimals, '.', ','); |
| 1411: | |
| 1412: | return $unit ? $value . ' ' . $unit : $value; |
| 1413: | } |
| 1414: | |
| 1415: | return $value; |
| 1416: | } |
| 1417: | } |
| 1418: | |
| 1419: | if (!function_exists('_fnumSmart')) { |
| 1420: | |
| 1421: | |
| 1422: | |
| 1423: | |
| 1424: | |
| 1425: | |
| 1426: | |
| 1427: | |
| 1428: | |
| 1429: | |
| 1430: | function _fnumSmart($value, $decimals = 2, $unit = '') |
| 1431: | { |
| 1432: | $value = _fnum($value, $decimals, $unit); |
| 1433: | $v = explode('.', $value); |
| 1434: | if ($decimals > 0 && isset($v[1])) { |
| 1435: | if (preg_match('/0{'.$decimals.'}/i', $v[1])) { |
| 1436: | $value = $v[0]; |
| 1437: | } |
| 1438: | } |
| 1439: | |
| 1440: | return $value; |
| 1441: | } |
| 1442: | } |
| 1443: | |
| 1444: | if (!function_exists('_fnumReverse')) { |
| 1445: | |
| 1446: | |
| 1447: | |
| 1448: | |
| 1449: | |
| 1450: | |
| 1451: | function _fnumReverse($num) |
| 1452: | { |
| 1453: | return str_replace(',', '', $num); |
| 1454: | } |
| 1455: | } |
| 1456: | |
| 1457: | if (!function_exists('_fdate')) { |
| 1458: | |
| 1459: | |
| 1460: | |
| 1461: | |
| 1462: | |
| 1463: | |
| 1464: | |
| 1465: | function _fdate($date = '', $format = '') |
| 1466: | { |
| 1467: | if (!$format) { |
| 1468: | $format = _cfg('dateFormat'); |
| 1469: | } |
| 1470: | |
| 1471: | if (func_num_args() === 0) { |
| 1472: | return date($format); |
| 1473: | } |
| 1474: | |
| 1475: | if (empty($date)) { |
| 1476: | return ''; |
| 1477: | } |
| 1478: | |
| 1479: | return is_string($date) ? date($format, strtotime($date)) : date($format, $date); |
| 1480: | } |
| 1481: | } |
| 1482: | |
| 1483: | if (!function_exists('_fdatetime')) { |
| 1484: | |
| 1485: | |
| 1486: | |
| 1487: | |
| 1488: | |
| 1489: | |
| 1490: | |
| 1491: | function _fdatetime($dateTime = '', $format = '') |
| 1492: | { |
| 1493: | if (!$format) { |
| 1494: | $format = _cfg('dateTimeFormat'); |
| 1495: | } |
| 1496: | |
| 1497: | if (func_num_args() == 0) { |
| 1498: | return date($format); |
| 1499: | } |
| 1500: | |
| 1501: | if (empty($dateTime)) { |
| 1502: | return ''; |
| 1503: | } |
| 1504: | |
| 1505: | return is_string($dateTime) ? date($format, strtotime($dateTime)) : date($format, $dateTime); |
| 1506: | } |
| 1507: | } |
| 1508: | |
| 1509: | if (!function_exists('_ftimeAgo')) { |
| 1510: | |
| 1511: | |
| 1512: | |
| 1513: | |
| 1514: | |
| 1515: | |
| 1516: | |
| 1517: | function _ftimeAgo($time, $format = 'M j Y') |
| 1518: | { |
| 1519: | if (empty($time)) { |
| 1520: | return ''; |
| 1521: | } |
| 1522: | |
| 1523: | $now = time(); |
| 1524: | if (!is_numeric($time)) { |
| 1525: | $time = strtotime($time); |
| 1526: | } |
| 1527: | |
| 1528: | $secElapsed = $now - $time; |
| 1529: | if ($secElapsed <= 60) { |
| 1530: | return _t('just now'); |
| 1531: | } elseif ($secElapsed <= 3540) { |
| 1532: | $min = $now - $time; |
| 1533: | $min = round($min/60); |
| 1534: | return _t('%d minutes ago', $min); |
| 1535: | } elseif ($secElapsed <= 3660) { |
| 1536: | return _t('1 hour ago'); |
| 1537: | } elseif (date('j-n-y', $now) == date('j-n-y', $time)) { |
| 1538: | return date("g:i a", $time); |
| 1539: | } elseif (date('j-n-y', mktime(0, 0, 0, date('n', $now), date('j', $now)-1, date('Y', $now))) == date('j-n-y', $time)) { |
| 1540: | return _t('yesterday'); |
| 1541: | } elseif ($secElapsed <= 345600) { |
| 1542: | return date('l', $time); |
| 1543: | } else { |
| 1544: | return date($format, $time); |
| 1545: | } |
| 1546: | } |
| 1547: | } |
| 1548: | |
| 1549: | if (!function_exists('_msg')) { |
| 1550: | |
| 1551: | |
| 1552: | |
| 1553: | |
| 1554: | |
| 1555: | |
| 1556: | |
| 1557: | |
| 1558: | |
| 1559: | |
| 1560: | |
| 1561: | |
| 1562: | function _msg($msg, $class = 'error', $return = null, $display = null) |
| 1563: | { |
| 1564: | $class = $class ?: 'error'; |
| 1565: | $return = strtolower($return); |
| 1566: | |
| 1567: | $html = ''; |
| 1568: | $html .= '<div class="message"'; |
| 1569: | if ($display) { |
| 1570: | $html .= ' style="display:' . $display . '"'; |
| 1571: | } |
| 1572: | $html .= '>'; |
| 1573: | $html .= '<div class="message-'. $class . ' alert alert-' . ($class == 'error' ? 'danger' : $class) . '">'; |
| 1574: | if (is_array($msg)) { |
| 1575: | if (count($msg) > 0) { |
| 1576: | $html .= '<ul>'; |
| 1577: | foreach ($msg as $m) { |
| 1578: | if (is_array($msg) && isset($m['message'])) { |
| 1579: | $html .= '<li>'.$m['message'].'</li>'; |
| 1580: | } else { |
| 1581: | $html .= '<li>'.$m.'</li>'; |
| 1582: | } |
| 1583: | } |
| 1584: | $html .= '</ul>'; |
| 1585: | } else { |
| 1586: | $html = ''; |
| 1587: | } |
| 1588: | } else { |
| 1589: | $html .= $msg; |
| 1590: | } |
| 1591: | |
| 1592: | $html .= '</div></div>'; |
| 1593: | |
| 1594: | if (is_array($msg) && count($msg) == 0) { |
| 1595: | $html = ''; |
| 1596: | } |
| 1597: | |
| 1598: | if ($return == 'html' || $return === true) { |
| 1599: | return $html; |
| 1600: | } else { |
| 1601: | echo $html; |
| 1602: | } |
| 1603: | |
| 1604: | return ''; |
| 1605: | } |
| 1606: | } |
| 1607: | |
| 1608: | |
| 1609: | |
| 1610: | |
| 1611: | |
| 1612: | |
| 1613: | |
| 1614: | |
| 1615: | |
| 1616: | |
| 1617: | function _filesize($file, $digits = 2, $sizes = array("MB","KB","B")) |
| 1618: | { |
| 1619: | if (is_file($file)) { |
| 1620: | $filePath = $file; |
| 1621: | if (!realpath($filePath)) { |
| 1622: | $filePath = $_SERVER["DOCUMENT_ROOT"].$filePath; |
| 1623: | } |
| 1624: | $fileSize = filesize($filePath); |
| 1625: | $total = count($sizes); |
| 1626: | while ($total-- && $fileSize > 1024) { |
| 1627: | $fileSize /= 1024; |
| 1628: | } |
| 1629: | |
| 1630: | return round($fileSize, $digits)." ".$sizes[$total]; |
| 1631: | } |
| 1632: | |
| 1633: | return false; |
| 1634: | } |
| 1635: | |
| 1636: | if (!function_exists('_randomCode')) { |
| 1637: | |
| 1638: | |
| 1639: | |
| 1640: | |
| 1641: | |
| 1642: | |
| 1643: | |
| 1644: | |
| 1645: | function _randomCode($length = 5, $letters = array(), $prefix = '') |
| 1646: | { |
| 1647: | |
| 1648: | if (sizeof($letters) == 0) { |
| 1649: | $letters = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z')); |
| 1650: | } |
| 1651: | |
| 1652: | shuffle($letters); |
| 1653: | $randArr = array_splice($letters, 0, $length); |
| 1654: | |
| 1655: | return $prefix . implode('', $randArr); |
| 1656: | } |
| 1657: | } |
| 1658: | |
| 1659: | if (!function_exists('_slug')) { |
| 1660: | |
| 1661: | |
| 1662: | |
| 1663: | |
| 1664: | |
| 1665: | |
| 1666: | |
| 1667: | |
| 1668: | |
| 1669: | function _slug($string, $table = '', array $condition = array()) |
| 1670: | { |
| 1671: | if (empty($string)) { |
| 1672: | return ''; |
| 1673: | } |
| 1674: | |
| 1675: | $specChars = array( |
| 1676: | '`','~','!','@','#','$','%','\^','&', |
| 1677: | '*','(',')','=','+','{','}','[',']', |
| 1678: | ':',';',"'",'"','<','>','\\','|','?','/',',' |
| 1679: | ); |
| 1680: | $table = db_table($table); |
| 1681: | $slug = strtolower(trim($string)); |
| 1682: | $slug = trim($slug, '-'); |
| 1683: | |
| 1684: | $slug = preg_replace('/(&|"|'|<|>)/i', '', $slug); |
| 1685: | $slug = str_replace($specChars, '-', $slug); |
| 1686: | $slug = str_replace(array(' ', '.'), '-', $slug); |
| 1687: | $slug = trim($slug, '-'); |
| 1688: | |
| 1689: | $condition = array_merge( |
| 1690: | array('slug' => $slug), |
| 1691: | $condition |
| 1692: | ); |
| 1693: | |
| 1694: | while (true && $table) { |
| 1695: | $count = db_count($table)->where($condition)->fetch(); |
| 1696: | if ($count == 0) { |
| 1697: | break; |
| 1698: | } |
| 1699: | |
| 1700: | $segments = explode('-', $slug); |
| 1701: | if (sizeof($segments) > 1 && is_numeric($segments[sizeof($segments)-1])) { |
| 1702: | $index = array_pop($segments); |
| 1703: | $index++; |
| 1704: | } else { |
| 1705: | $index = 1; |
| 1706: | } |
| 1707: | |
| 1708: | $segments[] = $index; |
| 1709: | $slug = implode('-', $segments); |
| 1710: | } |
| 1711: | |
| 1712: | $slug = preg_replace('/[\-]+/', '-', $slug); |
| 1713: | |
| 1714: | return trim($slug, '-'); |
| 1715: | } |
| 1716: | } |
| 1717: | |
| 1718: | |
| 1719: | |
| 1720: | |
| 1721: | |
| 1722: | |
| 1723: | |
| 1724: | |
| 1725: | |
| 1726: | |
| 1727: | function _sqlDate($date, $givenFormat = 'dmy', $separator = '-') |
| 1728: | { |
| 1729: | if (empty($date)) { |
| 1730: | return null; |
| 1731: | } |
| 1732: | |
| 1733: | $dt = explode($separator, $date); |
| 1734: | $format = str_split($givenFormat); |
| 1735: | $ft = array_flip($format); |
| 1736: | |
| 1737: | $y = $dt[$ft['y']]; |
| 1738: | $m = $dt[$ft['m']]; |
| 1739: | $d = $dt[$ft['d']]; |
| 1740: | |
| 1741: | return checkdate($m, $d, $y) ? $y . '-' . $m .'-'. $d : null; |
| 1742: | } |
| 1743: | |
| 1744: | |
| 1745: | |
| 1746: | |
| 1747: | |
| 1748: | |
| 1749: | |
| 1750: | |
| 1751: | |
| 1752: | function _encrypt($text) |
| 1753: | { |
| 1754: | $secret = _cfg('securitySecret'); |
| 1755: | if (!$secret || !function_exists('openssl_encrypt')) { |
| 1756: | return $text; |
| 1757: | } |
| 1758: | |
| 1759: | $method = _cipher(); |
| 1760: | $ivlen = openssl_cipher_iv_length($method); |
| 1761: | $iv = openssl_random_pseudo_bytes($ivlen); |
| 1762: | |
| 1763: | $textRaw = openssl_encrypt($text, $method, $secret, OPENSSL_RAW_DATA, $iv); |
| 1764: | $hmac = hash_hmac('sha256', $textRaw, $secret, true); |
| 1765: | |
| 1766: | return base64_encode($iv . $hmac . $textRaw ); |
| 1767: | } |
| 1768: | |
| 1769: | |
| 1770: | |
| 1771: | |
| 1772: | |
| 1773: | |
| 1774: | |
| 1775: | |
| 1776: | |
| 1777: | function _decrypt($encryptedText) |
| 1778: | { |
| 1779: | $secret = _cfg('securitySecret'); |
| 1780: | if (!$secret || !function_exists('openssl_decrypt')) { |
| 1781: | return $encryptedText; |
| 1782: | } |
| 1783: | |
| 1784: | $method = _cipher(); |
| 1785: | $sha2len = 32; |
| 1786: | $ivlen = openssl_cipher_iv_length($method); |
| 1787: | $text = base64_decode($encryptedText); |
| 1788: | $iv = substr($text, 0, $ivlen); |
| 1789: | |
| 1790: | $rawText = substr($text, $ivlen + $sha2len); |
| 1791: | $plainText = openssl_decrypt($rawText, $method, $secret, OPENSSL_RAW_DATA, $iv); |
| 1792: | |
| 1793: | $hmac = substr($text, $ivlen, $sha2len); |
| 1794: | $mac = hash_hmac('sha256', $rawText, $secret, true); |
| 1795: | if (hash_equals($hmac, $mac)) { |
| 1796: | return $plainText; |
| 1797: | } |
| 1798: | |
| 1799: | return $text; |
| 1800: | } |
| 1801: | |
| 1802: | |
| 1803: | |
| 1804: | |
| 1805: | |
| 1806: | function _cipher() |
| 1807: | { |
| 1808: | $method = _cfg('cipher'); |
| 1809: | if (!in_array($method, openssl_get_cipher_methods())) { |
| 1810: | $method = 'AES-256-CBC'; |
| 1811: | } |
| 1812: | |
| 1813: | return $method; |
| 1814: | } |
| 1815: | |
| 1816: | |
| 1817: | |
| 1818: | |
| 1819: | |
| 1820: | |
| 1821: | |
| 1822: | |
| 1823: | function _meta($key, $value = '') |
| 1824: | { |
| 1825: | global $_meta; |
| 1826: | $value = trim($value); |
| 1827: | if (empty($value)) { |
| 1828: | return (isset($_meta[$key])) ? $_meta[$key] : ''; |
| 1829: | } else { |
| 1830: | if (in_array($key, array('description', 'og:description', 'twitter:description', 'gp:description'))) { |
| 1831: | $value = trim(substr($value, 0, 200)); |
| 1832: | } |
| 1833: | $_meta[$key] = $value; |
| 1834: | } |
| 1835: | } |
| 1836: | |
| 1837: | |
| 1838: | |
| 1839: | |
| 1840: | |
| 1841: | function _metaSeoTags() |
| 1842: | { |
| 1843: | if (_meta('description')) { |
| 1844: | _cfg('metaDescription', _meta('description')); |
| 1845: | } |
| 1846: | |
| 1847: | $tags = array(); |
| 1848: | $tags['description'] = _cfg('metaDescription'); |
| 1849: | |
| 1850: | $tags['og'] = array(); |
| 1851: | $tags['og']['title'] = _meta('og:title') ? _meta('og:title') : _title(); |
| 1852: | $tags['og']['url'] = _meta('og:url') ? _meta('og:url') : _url(); |
| 1853: | $tags['og']['type'] = _meta('og:type') ? _meta('og:type') : 'website'; |
| 1854: | $tags['og']['image'] = _meta('og:image') ? _meta('og:image') : _img('logo-social.jpg'); |
| 1855: | $tags['og']['description'] = _meta('og:description') ? _meta('og:description') : _cfg('metaDescription'); |
| 1856: | $tags['og']['site_name'] = _meta('og:site_name') ? _meta('og:site_name') : _cfg('siteName'); |
| 1857: | |
| 1858: | $tags['twitter'] = array(); |
| 1859: | $tags['twitter']['card'] = _meta('twitter:card') ? _meta('twitter:card') : 'summary'; |
| 1860: | $tags['twitter']['site'] = _meta('twitter:site') ? '@'._meta('twitter:site') : '@'._cfg('siteDomain'); |
| 1861: | $tags['twitter']['title'] = _meta('twitter:title') ? _meta('twitter:title') : _title(); |
| 1862: | $tags['twitter']['description'] = _meta('twitter:description') ? _meta('twitter:description') : _cfg('metaDescription'); |
| 1863: | $tags['twitter']['image'] = _meta('twitter:image') ? _meta('twitter:image') : _img('logo-social.jpg'); |
| 1864: | |
| 1865: | if (function_exists('__metaSeoTags')) { |
| 1866: | echo __metaSeoTags($tags); |
| 1867: | } else { |
| 1868: | echo "\n"; |
| 1869: | foreach ($tags as $name => $tag) { |
| 1870: | if ($name == 'og') { |
| 1871: | foreach ($tag as $key => $content) { |
| 1872: | echo '<meta property="og:' . $key . '" content="' . $content . '" />'."\n"; |
| 1873: | } |
| 1874: | } elseif ($name == 'twitter') { |
| 1875: | foreach ($tag as $key => $content) { |
| 1876: | echo '<meta name="twitter:' . $key . '" content="' . $content . '" />'."\n"; |
| 1877: | } |
| 1878: | } else { |
| 1879: | echo '<meta name="' . $name . '" content="' . $tag . '" />'."\n"; |
| 1880: | } |
| 1881: | } |
| 1882: | } |
| 1883: | } |
| 1884: | |
| 1885: | |
| 1886: | |
| 1887: | |
| 1888: | |
| 1889: | |
| 1890: | |
| 1891: | |
| 1892: | |
| 1893: | |
| 1894: | |
| 1895: | |
| 1896: | |
| 1897: | |
| 1898: | |
| 1899: | |
| 1900: | |
| 1901: | |
| 1902: | |
| 1903: | function _mail($from, $to, $subject = '', $message = '', $cc = '', $bcc = '') |
| 1904: | { |
| 1905: | $charset = mb_detect_encoding($message); |
| 1906: | $message = nl2br(stripslashes($message)); |
| 1907: | |
| 1908: | $EEOL = PHP_EOL; |
| 1909: | $headers = 'From: ' . $from . $EEOL; |
| 1910: | $headers .= 'MIME-Version: 1.0' . $EEOL; |
| 1911: | $headers .= 'Content-type: text/html; charset=' . $charset . $EEOL; |
| 1912: | $headers .= 'Reply-To: ' . $from . $EEOL; |
| 1913: | $headers .= 'Return-Path:'.$from . $EEOL; |
| 1914: | if ($cc) { |
| 1915: | $headers .= 'Cc: ' . $cc . $EEOL; |
| 1916: | } |
| 1917: | if ($bcc) { |
| 1918: | $headers .= 'Bcc: ' . $bcc . $EEOL; |
| 1919: | } |
| 1920: | $headers .= 'X-Mailer: PHP'; |
| 1921: | |
| 1922: | return mail($to, $subject, $message, $headers); |
| 1923: | } |
| 1924: | |
| 1925: | |
| 1926: | |
| 1927: | |
| 1928: | |
| 1929: | |
| 1930: | |
| 1931: | |
| 1932: | |
| 1933: | |
| 1934: | function _postTranslationStrings($post, $fields, $lang = null) |
| 1935: | { |
| 1936: | global $lc_languages; |
| 1937: | |
| 1938: | $data = array(); |
| 1939: | foreach ($fields as $key => $name) { |
| 1940: | if ($lang) { |
| 1941: | $lcode = _queryLang($lang); |
| 1942: | if (isset($post[$name.'_'.$lcode])) { |
| 1943: | $data[$key.'_'.$lcode] = $post[$name.'_'.$lcode]; |
| 1944: | } |
| 1945: | } else { |
| 1946: | if (isset($post[$name])) { |
| 1947: | $data[$key.'_'._defaultLang()] = $post[$name]; |
| 1948: | } |
| 1949: | foreach ($lc_languages as $lcode => $lname) { |
| 1950: | $lcode = _queryLang($lcode); |
| 1951: | if (isset($post[$name.'_'.$lcode])) { |
| 1952: | $data[$key.'_'.$lcode] = $post[$name.'_'.$lcode]; |
| 1953: | } |
| 1954: | } |
| 1955: | } |
| 1956: | } |
| 1957: | |
| 1958: | return $data; |
| 1959: | } |
| 1960: | |
| 1961: | |
| 1962: | |
| 1963: | |
| 1964: | |
| 1965: | |
| 1966: | |
| 1967: | |
| 1968: | |
| 1969: | |
| 1970: | |
| 1971: | |
| 1972: | function _getTranslationStrings($data, $fields, $lang = null) |
| 1973: | { |
| 1974: | global $lc_languages; |
| 1975: | |
| 1976: | $isObject = is_object($data); |
| 1977: | $data = (array) $data; |
| 1978: | |
| 1979: | if (is_string($fields)) { |
| 1980: | $fields = array($fields); |
| 1981: | } |
| 1982: | |
| 1983: | foreach ($fields as $name) { |
| 1984: | if ($lang) { |
| 1985: | $lcode = _queryLang($lang); |
| 1986: | if (isset($data[$name.'_'.$lcode]) && $data[$name.'_'.$lcode]) { |
| 1987: | $data[$name.'_i18n'] = $data[$name.'_'.$lcode]; |
| 1988: | } else { |
| 1989: | $data[$name.'_i18n'] = $data[$name]; |
| 1990: | } |
| 1991: | } else { |
| 1992: | foreach ($lc_languages as $lcode => $lname) { |
| 1993: | $lcode = _queryLang($lcode); |
| 1994: | if (isset($data[$name.'_'.$lcode])) { |
| 1995: | $data[$name.'_i18n'][$lcode] = $data[$name.'_'.$lcode]; |
| 1996: | } |
| 1997: | } |
| 1998: | } |
| 1999: | } |
| 2000: | |
| 2001: | if ($isObject) { |
| 2002: | $data = (object) $data; |
| 2003: | } |
| 2004: | |
| 2005: | return $data; |
| 2006: | } |
| 2007: | |
| 2008: | |
| 2009: | |
| 2010: | |
| 2011: | |
| 2012: | |
| 2013: | function _isBot() |
| 2014: | { |
| 2015: | if (!isset($_SERVER['HTTP_USER_AGENT'])) { |
| 2016: | return false; |
| 2017: | } |
| 2018: | |
| 2019: | $userAgent = $_SERVER['HTTP_USER_AGENT']; |
| 2020: | if (empty($userAgent)) { |
| 2021: | return false; |
| 2022: | } |
| 2023: | |
| 2024: | $bots = array( |
| 2025: | 'Googlebot', |
| 2026: | 'Slurp', |
| 2027: | 'msnbot', |
| 2028: | 'bingbot', |
| 2029: | 'yahoo', |
| 2030: | 'search.msn.com', |
| 2031: | 'Baidu', |
| 2032: | 'baiduspider', |
| 2033: | 'Yandex', |
| 2034: | 'nutch', |
| 2035: | 'FAST', |
| 2036: | 'Sosospider', |
| 2037: | 'Exabot', |
| 2038: | 'sogou', |
| 2039: | 'bot', |
| 2040: | 'crawler', |
| 2041: | 'spider', |
| 2042: | 'Feedfetcher-Google', |
| 2043: | 'ASPSeek', |
| 2044: | 'simpy', |
| 2045: | 'ips-agent', |
| 2046: | 'Libwww-perl', |
| 2047: | 'ask jeeves', |
| 2048: | 'fastcrawler', |
| 2049: | 'infoseek', |
| 2050: | 'lycos', |
| 2051: | 'mediapartners-google', |
| 2052: | 'CRAZYWEBCRAWLER', |
| 2053: | 'adsbot-google', |
| 2054: | 'curious george', |
| 2055: | 'ia_archiver', |
| 2056: | 'MJ12bot', |
| 2057: | 'Uptimebot', |
| 2058: | 'Dataprovider.com', |
| 2059: | 'Go-http-client', |
| 2060: | 'Barkrowler', |
| 2061: | 'panscient.com', |
| 2062: | 'Symfony BrowserKit', |
| 2063: | 'Apache-HttpClient', |
| 2064: | 'serpstatbot', |
| 2065: | 'BLEXBot', |
| 2066: | 'DotBot', |
| 2067: | 'AhrefsBot', |
| 2068: | ); |
| 2069: | foreach ($bots as $bot) { |
| 2070: | if (false !== strpos(strtolower($userAgent), strtolower($bot))) { |
| 2071: | return true; |
| 2072: | } |
| 2073: | } |
| 2074: | |
| 2075: | return false; |
| 2076: | } |
| 2077: | |
| 2078: | |
| 2079: | |
| 2080: | |
| 2081: | |
| 2082: | |
| 2083: | |
| 2084: | |
| 2085: | function _write($text = '') |
| 2086: | { |
| 2087: | $args = func_get_args(); |
| 2088: | $text = array_shift($args); |
| 2089: | if ($text) { |
| 2090: | echo vsprintf($text, $args); |
| 2091: | } |
| 2092: | } |
| 2093: | |
| 2094: | |
| 2095: | |
| 2096: | |
| 2097: | |
| 2098: | |
| 2099: | |
| 2100: | |
| 2101: | function _writeln($text = '') |
| 2102: | { |
| 2103: | $args = func_get_args(); |
| 2104: | $text = array_shift($args); |
| 2105: | if ($text) { |
| 2106: | echo vsprintf($text, $args); |
| 2107: | } |
| 2108: | |
| 2109: | echo "\n"; |
| 2110: | } |
| 2111: | |
| 2112: | |
| 2113: | |
| 2114: | |
| 2115: | |
| 2116: | |
| 2117: | |
| 2118: | function _indent($width = 2) |
| 2119: | { |
| 2120: | return str_repeat(' ', $width); |
| 2121: | } |
| 2122: | |
| 2123: | |
| 2124: | |
| 2125: | |
| 2126: | |
| 2127: | |
| 2128: | |
| 2129: | function _consoleCommand($command) |
| 2130: | { |
| 2131: | return new Command($command); |
| 2132: | } |
| 2133: | |
| 2134: | |
| 2135: | |
| 2136: | |
| 2137: | |
| 2138: | |
| 2139: | function _consoleTable() |
| 2140: | { |
| 2141: | return new ConsoleTable(); |
| 2142: | } |
| 2143: | |
| 2144: | |
| 2145: | |
| 2146: | |
| 2147: | |
| 2148: | |
| 2149: | function _consoleCommands() |
| 2150: | { |
| 2151: | return Console::getCommands(); |
| 2152: | } |
| 2153: | |
| 2154: | |
| 2155: | |
| 2156: | |
| 2157: | |
| 2158: | |
| 2159: | |
| 2160: | function _pager($pageQueryStr = '') |
| 2161: | { |
| 2162: | return new Pager($pageQueryStr); |
| 2163: | } |
| 2164: | |
| 2165: | |
| 2166: | |
| 2167: | |
| 2168: | |
| 2169: | |
| 2170: | |
| 2171: | function _fileHelper($fileName = '') |
| 2172: | { |
| 2173: | return new File($fileName); |
| 2174: | } |
| 2175: | |
| 2176: | |
| 2177: | |
| 2178: | |
| 2179: | |
| 2180: | |
| 2181: | |
| 2182: | function _asynFileUploader() |
| 2183: | { |
| 2184: | if (func_num_args()) { |
| 2185: | return new AsynFileUploader(func_get_arg(0)); |
| 2186: | } else { |
| 2187: | return new AsynFileUploader(); |
| 2188: | } |
| 2189: | } |
| 2190: | |
| 2191: | |
| 2192: | |
| 2193: | |
| 2194: | |
| 2195: | |
| 2196: | |
| 2197: | |
| 2198: | function _middleware(\Closure $closure, $event = 'before') |
| 2199: | { |
| 2200: | $middleware = Middleware::getInstance(); |
| 2201: | |
| 2202: | return $middleware->register($closure, $event); |
| 2203: | } |
| 2204: | |
| 2205: | |
| 2206: | |
| 2207: | |
| 2208: | |
| 2209: | function _view() |
| 2210: | { |
| 2211: | if (_cfg('view')) { |
| 2212: | $viewName = 'view_'._cfg('view'); |
| 2213: | } elseif (_g('view')) { |
| 2214: | $viewName = 'view_'._g('view'); |
| 2215: | } else { |
| 2216: | $viewName = 'view'; |
| 2217: | } |
| 2218: | |
| 2219: | return _i(_ds(_cr(), $viewName.'.php')); |
| 2220: | } |
| 2221: | |
| 2222: | |
| 2223: | |
| 2224: | |
| 2225: | |
| 2226: | function _ds() |
| 2227: | { |
| 2228: | return implode(_DS_, func_get_args()); |
| 2229: | } |
| 2230: | |
| 2231: | |
| 2232: | |
| 2233: | |
| 2234: | |
| 2235: | function _isAjax() |
| 2236: | { |
| 2237: | if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && |
| 2238: | strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { |
| 2239: | return true; |
| 2240: | } |
| 2241: | |
| 2242: | return false; |
| 2243: | } |
| 2244: | |
| 2245: | |
| 2246: | |
| 2247: | |
| 2248: | |
| 2249: | function _isHttpPost() |
| 2250: | { |
| 2251: | return _isRequestMethod('POST') && count($_POST); |
| 2252: | } |
| 2253: | |
| 2254: | |
| 2255: | |
| 2256: | |
| 2257: | |
| 2258: | |
| 2259: | |
| 2260: | |
| 2261: | |
| 2262: | function _json($data = [], $status = 200, $return = false) |
| 2263: | { |
| 2264: | _cfg('layoutMode', false); |
| 2265: | |
| 2266: | if (_isRequestMethod('OPTIONS')) { |
| 2267: | _header(200); |
| 2268: | exit; |
| 2269: | } |
| 2270: | |
| 2271: | _header($status); |
| 2272: | |
| 2273: | header('Content-Type: application/json'); |
| 2274: | if ($status != 204) { |
| 2275: | $json = json_encode($data); |
| 2276: | if ($return) { |
| 2277: | return $json; |
| 2278: | } |
| 2279: | |
| 2280: | echo $json; |
| 2281: | } |
| 2282: | |
| 2283: | Middleware::runAfter(); |
| 2284: | exit; |
| 2285: | } |
| 2286: | |
| 2287: | |
| 2288: | |
| 2289: | |
| 2290: | |
| 2291: | |
| 2292: | |
| 2293: | |
| 2294: | function _jsonError($message, $field = '', $status = 400) |
| 2295: | { |
| 2296: | $errors = []; |
| 2297: | if (is_array($message)) { |
| 2298: | $errors = $message; |
| 2299: | } else { |
| 2300: | $errors[] = [ |
| 2301: | 'field' => $field, |
| 2302: | 'message' => $message, |
| 2303: | ]; |
| 2304: | |
| 2305: | } |
| 2306: | |
| 2307: | _json(['error' => $errors], $status); |
| 2308: | } |
| 2309: | |
| 2310: | |
| 2311: | |
| 2312: | |
| 2313: | |
| 2314: | function _requestHeaders() |
| 2315: | { |
| 2316: | if (function_exists('getallheaders')) { |
| 2317: | return getallheaders(); |
| 2318: | } |
| 2319: | |
| 2320: | if (function_exists('apache_request_headers')) { |
| 2321: | return apache_request_headers(); |
| 2322: | } |
| 2323: | |
| 2324: | $headers = array(); |
| 2325: | foreach ($_SERVER as $name => $value) { |
| 2326: | $name = strtolower($name); |
| 2327: | if (substr($name, 0, 5) == 'http_') { |
| 2328: | $headers[str_replace(' ', '-', ucwords(str_replace('_', ' ', substr($name, 5))))] = $value; |
| 2329: | } elseif ($name == 'content_type') { |
| 2330: | $headers['Content-Type'] = $value; |
| 2331: | } elseif ($name == 'content_length') { |
| 2332: | $headers['Content-Length'] = $value; |
| 2333: | } |
| 2334: | } |
| 2335: | |
| 2336: | return $headers; |
| 2337: | } |
| 2338: | |
| 2339: | |
| 2340: | |
| 2341: | |
| 2342: | |
| 2343: | |
| 2344: | function _requestHeader($name) |
| 2345: | { |
| 2346: | $headers = _requestHeaders(); |
| 2347: | if (!is_array($headers)) { |
| 2348: | return null; |
| 2349: | } |
| 2350: | |
| 2351: | $headers = array_change_key_case($headers); |
| 2352: | $name = strtolower($name); |
| 2353: | |
| 2354: | if (isset($headers[$name])) { |
| 2355: | return $headers[$name]; |
| 2356: | } |
| 2357: | |
| 2358: | return null; |
| 2359: | } |
| 2360: | |
| 2361: | |
| 2362: | |
| 2363: | |
| 2364: | |
| 2365: | function _requestMethod() |
| 2366: | { |
| 2367: | if (isset($_SERVER['REQUEST_METHOD'])) { |
| 2368: | return strtoupper($_SERVER['REQUEST_METHOD']); |
| 2369: | } |
| 2370: | |
| 2371: | return null; |
| 2372: | } |
| 2373: | |
| 2374: | |
| 2375: | |
| 2376: | |
| 2377: | |
| 2378: | |
| 2379: | function _isRequestMethod($method) |
| 2380: | { |
| 2381: | return _requestMethod() == strtoupper($method); |
| 2382: | } |
| 2383: | |
| 2384: | |
| 2385: | |
| 2386: | |
| 2387: | |
| 2388: | |
| 2389: | function _addFormData($name, array $data) |
| 2390: | { |
| 2391: | echo '<script>LC.Form.formData["' . $name . '"] = ' . json_encode($data) . ';</script>'; |
| 2392: | } |
| 2393: | |
| 2394: | |
| 2395: | |
| 2396: | |
| 2397: | |
| 2398: | |
| 2399: | |
| 2400: | function _nullFill($value) |
| 2401: | { |
| 2402: | if (function_exists('__nullFill')) { |
| 2403: | return __nullFill($value); |
| 2404: | } |
| 2405: | |
| 2406: | return $value ?: '<span class="null-fill">-</span>'; |
| 2407: | } |
| 2408: | |
| 2409: | |
| 2410: | |
| 2411: | |
| 2412: | |
| 2413: | |
| 2414: | |
| 2415: | |
| 2416: | function _entity($table, array $data = [], $dbNamespace = null) |
| 2417: | { |
| 2418: | if (!$dbNamespace) { |
| 2419: | $dbNamespace = _cfg('defaultDbSource'); |
| 2420: | } |
| 2421: | |
| 2422: | $schema = _schema($dbNamespace, true); |
| 2423: | |
| 2424: | $entity = array(); |
| 2425: | if ($schema && isset($schema[$table])) { |
| 2426: | $options = array_merge(SchemaManager::$relationships, array('options')); |
| 2427: | foreach ($schema[$table] as $field => $def) { |
| 2428: | if (in_array($field, $options)) { |
| 2429: | continue; |
| 2430: | } |
| 2431: | |
| 2432: | if (isset($def['autoinc'])) { |
| 2433: | $value = 0; |
| 2434: | } else { |
| 2435: | $value = isset($def['null']) ? null : ''; |
| 2436: | if (isset($def['default'])) { |
| 2437: | $value = $def['default']; |
| 2438: | } |
| 2439: | } |
| 2440: | |
| 2441: | if ($field == 'created' || $field == 'updated') { |
| 2442: | $value = date('Y-m-i H:i:s'); |
| 2443: | } |
| 2444: | |
| 2445: | if ($def['type'] == 'array' || $def['type'] == 'json') { |
| 2446: | $value = array(); |
| 2447: | } |
| 2448: | |
| 2449: | $entity[$field] = $value; |
| 2450: | |
| 2451: | if (isset($data[$field])) { |
| 2452: | $entity[$field] = $data[$field]; |
| 2453: | } |
| 2454: | } |
| 2455: | } |
| 2456: | |
| 2457: | return (object) $entity; |
| 2458: | } |
| 2459: | |
| 2460: | |
| 2461: | |
| 2462: | |
| 2463: | |
| 2464: | |
| 2465: | function _addHeadStyle($file) |
| 2466: | { |
| 2467: | $view = _app('view'); |
| 2468: | $view->addHeadStyle($file); |
| 2469: | } |
| 2470: | |
| 2471: | |
| 2472: | |
| 2473: | |
| 2474: | |
| 2475: | |
| 2476: | function _addHeadScript($file) |
| 2477: | { |
| 2478: | $view = _app('view'); |
| 2479: | $view->addHeadScript($file); |
| 2480: | } |
| 2481: | |
| 2482: | |
| 2483: | |
| 2484: | |
| 2485: | |
| 2486: | |
| 2487: | function _en2myNum($num) |
| 2488: | { |
| 2489: | $digits = array( |
| 2490: | '/0/' => '၀', |
| 2491: | '/1/' => '၁', |
| 2492: | '/2/' => '၂', |
| 2493: | '/3/' => '၃', |
| 2494: | '/4/' => '၄', |
| 2495: | '/5/' => '၅', |
| 2496: | '/6/' => '၆', |
| 2497: | '/7/' => '၇', |
| 2498: | '/8/' => '၈', |
| 2499: | '/9/' => '၉', |
| 2500: | ); |
| 2501: | |
| 2502: | return preg_replace(array_keys($digits), array_values($digits), $num); |
| 2503: | } |
| 2504: | |
| 2505: | |
| 2506: | |
| 2507: | |
| 2508: | |
| 2509: | |
| 2510: | function _arrayAssoc(array $arr) |
| 2511: | { |
| 2512: | if (empty($arr)) { |
| 2513: | return false; |
| 2514: | } |
| 2515: | |
| 2516: | return array_keys($arr) !== range(0, count($arr) - 1); |
| 2517: | } |
| 2518: | |
| 2519: | |
| 2520: | |
| 2521: | |
| 2522: | |
| 2523: | |
| 2524: | function _isContentType($type) |
| 2525: | { |
| 2526: | return isset($_SERVER['CONTENT_TYPE']) && $_SERVER['CONTENT_TYPE'] == $type; |
| 2527: | } |
| 2528: | |
| 2529: | |
| 2530: | |
| 2531: | |
| 2532: | |
| 2533: | |
| 2534: | |
| 2535: | |
| 2536: | |
| 2537: | |
| 2538: | |
| 2539: | |
| 2540: | |
| 2541: | function _curl($url, $params = array(), $method = 'get', $headers = array()) |
| 2542: | { |
| 2543: | $method = strtoupper($method); |
| 2544: | if (!in_array($method, array('GET', 'POST', 'PUT', 'DELETE'))) { |
| 2545: | $method = 'GET'; |
| 2546: | } |
| 2547: | |
| 2548: | $queryStr = ''; |
| 2549: | if (!empty($params)) { |
| 2550: | $queryStr = is_array($params) ? http_build_query($params) : $params; |
| 2551: | } |
| 2552: | |
| 2553: | $ch = curl_init(); |
| 2554: | if ($method == 'POST') { |
| 2555: | curl_setopt($ch, CURLOPT_POST, 1); |
| 2556: | if ($queryStr) { |
| 2557: | curl_setopt($ch, CURLOPT_POSTFIELDS, $queryStr); |
| 2558: | } |
| 2559: | } else { |
| 2560: | if ($queryStr) { |
| 2561: | $url .= '?' . $queryStr; |
| 2562: | } |
| 2563: | } |
| 2564: | |
| 2565: | |
| 2566: | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 2567: | curl_setopt($ch, CURLOPT_URL, $url); |
| 2568: | if (!empty($headers)) { |
| 2569: | if (_arrayAssoc($headers)) { |
| 2570: | $headers = array_map(function($key, $value) { |
| 2571: | return $key . ': ' . $value; |
| 2572: | }, array_keys($headers), array_values($headers)); |
| 2573: | } |
| 2574: | |
| 2575: | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 2576: | } |
| 2577: | |
| 2578: | $response = curl_exec($ch); |
| 2579: | $info = curl_getinfo($ch); |
| 2580: | $error = curl_error($ch); |
| 2581: | |
| 2582: | curl_close($ch); |
| 2583: | |
| 2584: | return [ |
| 2585: | 'options' => $info, |
| 2586: | 'error' => $error, |
| 2587: | 'response' => $response, |
| 2588: | ]; |
| 2589: | } |
| 2590: | |
| 2591: | |
| 2592: | |
| 2593: | |
| 2594: | |
| 2595: | |
| 2596: | |
| 2597: | |
| 2598: | |
| 2599: | function form_select($name, $data = [], $value = null, $attributes = []) |
| 2600: | { |
| 2601: | $attr = ''; |
| 2602: | foreach ($attributes as $key => $val) { |
| 2603: | $attr .= ' ' . $key . '="' . $val . '"'; |
| 2604: | } |
| 2605: | |
| 2606: | $html = '<select name="' . $name . '" ' . $attr . '>'; |
| 2607: | foreach ($data as $key => $val) { |
| 2608: | if (isset($val->id) && isset($val->name)) { |
| 2609: | $key = $val->id; |
| 2610: | $val = $val->name; |
| 2611: | } |
| 2612: | $html .= '<option value="' . $key . '" '; |
| 2613: | $html .= form_selected($name, $key, $value); |
| 2614: | $html .= '>' . _t($val) . '</option>'; |
| 2615: | } |
| 2616: | $html .= '</select>'; |
| 2617: | |
| 2618: | return $html; |
| 2619: | } |
| 2620: | |
| 2621: | |
| 2622: | |
| 2623: | |
| 2624: | |
| 2625: | |
| 2626: | function _mime2ext($mime) { |
| 2627: | $mimeMap = [ |
| 2628: | '3g2' => ['video/3gpp2'], |
| 2629: | '3gp' => ['video/3gp', 'video/3gpp'], |
| 2630: | '7zip' => ['application/x-compressed'], |
| 2631: | 'acc' => ['audio/x-acc'], |
| 2632: | 'ac3' => ['audio/ac3'], |
| 2633: | 'ai' => ['application/postscript'], |
| 2634: | 'aif' => ['audio/x-aiff', 'audio/aiff'], |
| 2635: | 'au' => ['audio/x-au'], |
| 2636: | 'avi' => ['video/x-msvideo', 'video/msvideo', 'video/avi', 'application/x-troff-msvideo'], |
| 2637: | 'bin' => ['application/macbinary', 'application/mac-binary', 'application/x-binary', 'application/mac-binary'], |
| 2638: | 'bmp' => ['image/bmp', 'image/x-bmp', 'image/x-bitmap', 'image/x-xbitmap', 'image/x-win-bitmap', 'image/x-windows-bmp', 'image/ms-bmp', 'image/x-ms-bmp', 'application/bmp', 'application/x-bmp', 'application/x-win-bitmap'], |
| 2639: | 'cdr' => ['application/cdr', 'application/x-cdr', 'application/coreldraw', 'application/x-coreldraw', 'image/cdr', 'image/x-cdr', 'zz-application/zz-winassoc-cdr'], |
| 2640: | 'cpt' => ['application/mac-compactpro'], |
| 2641: | 'crl' => ['application/pkix-crl', 'application/pkcs-crl'], |
| 2642: | 'crt' => ['application/x-x509-ca-cert', 'application/pkix-cert'], |
| 2643: | 'css' => ['text/css'], |
| 2644: | 'csv' => ['text/x-comma-separated-values', 'text/comma-separated-values', 'application/vnd.msexcel'], |
| 2645: | 'dcr' => ['application/x-director'], |
| 2646: | 'doc' => ['application/msword'], |
| 2647: | 'docx' => ['application/vnd.openxmlformats-officedocument.wordprocessingml.document'], |
| 2648: | 'dvi' => ['application/x-dvi'], |
| 2649: | 'eml' => ['message/rfc822'], |
| 2650: | 'exe' => ['application/x-msdownload'], |
| 2651: | 'f4v' => ['video/x-f4v'], |
| 2652: | 'flac' => ['audio/x-flac'], |
| 2653: | 'flv' => ['video/x-flv'], |
| 2654: | 'gif' => ['image/gif'], |
| 2655: | 'gpg' => ['application/gpg-keys'], |
| 2656: | 'gtar' => ['application/x-gtar'], |
| 2657: | 'gzip' => ['application/x-gzip'], |
| 2658: | 'hqx' => ['application/mac-binhex40', 'application/mac-binhex', 'application/x-binhex40', 'application/x-mac-binhex40'], |
| 2659: | 'html' => ['text/html'], |
| 2660: | 'ico' => ['image/x-icon', 'image/x-ico', 'image/vnd.microsoft.icon'], |
| 2661: | 'ics' => ['text/calendar'], |
| 2662: | 'jar' => ['application/java-archive', 'application/x-java-application', 'application/x-jar'], |
| 2663: | 'jp2' => ['image/jp2', 'video/mj2', 'image/jpx', 'image/jpm'], |
| 2664: | 'jpeg' => ['image/jpeg', 'image/pjpeg'], |
| 2665: | 'js' => ['application/x-javascript'], |
| 2666: | 'json' => ['application/json', 'text/json'], |
| 2667: | 'kml' => ['application/vnd.google-earth.kml+xml'], |
| 2668: | 'kmz' => ['application/vnd.google-earth.kmz'], |
| 2669: | 'log' => ['text/x-log'], |
| 2670: | 'm4a' => ['audio/x-m4a', 'audio/mp4'], |
| 2671: | 'm4u' => ['application/vnd.mpegurl'], |
| 2672: | 'mid' => ['audio/midi'], |
| 2673: | 'mif' => ['application/vnd.mif'], |
| 2674: | 'mov' => ['video/quicktime'], |
| 2675: | 'movie' => ['video/x-sgi-movie'], |
| 2676: | 'mp3' => ['audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'], |
| 2677: | 'mp4' => ['video/mp4'], |
| 2678: | 'mpeg' => ['video/mpeg'], |
| 2679: | 'oda' => ['application/oda'], |
| 2680: | 'ogg' => ['audio/ogg', 'video/ogg', 'application/ogg'], |
| 2681: | 'otf' => ['font/otf'], |
| 2682: | 'p10' => ['application/x-pkcs10', 'application/pkcs10'], |
| 2683: | 'p12' => ['application/x-pkcs12'], |
| 2684: | 'p7a' => ['application/x-pkcs7-signature'], |
| 2685: | 'p7c' => ['application/pkcs7-mime', 'application/x-pkcs7-mime'], |
| 2686: | 'p7r' => ['application/x-pkcs7-certreqresp'], |
| 2687: | 'p7s' => ['application/pkcs7-signature'], |
| 2688: | 'pdf' => ['application/pdf', 'application/octet-stream'], |
| 2689: | 'pem' => ['application/x-x509-user-cert', 'application/x-pem-file'], |
| 2690: | 'pgp' => ['application/pgp'], |
| 2691: | 'php' => ['application/x-httpd-php', 'application/php', 'application/x-php', 'text/php', 'text/x-php', 'application/x-httpd-php-source'], |
| 2692: | 'png' => ['image/png', 'image/x-png'], |
| 2693: | 'ppt' => ['application/powerpoint', 'application/vnd.ms-powerpoint', 'application/vnd.ms-office'], |
| 2694: | 'pptx' => ['application/vnd.openxmlformats-officedocument.presentationml.presentation'], |
| 2695: | 'psd' => ['application/x-photoshop', 'image/vnd.adobe.photoshop'], |
| 2696: | 'ra' => ['audio/x-realaudio'], |
| 2697: | 'ram' => ['audio/x-pn-realaudio'], |
| 2698: | 'rar' => ['application/x-rar', 'application/rar', 'application/x-rar-compressed'], |
| 2699: | 'rpm' => ['audio/x-pn-realaudio-plugin'], |
| 2700: | 'rsa' => ['application/x-pkcs7'], |
| 2701: | 'rtf' => ['text/rtf'], |
| 2702: | 'rtx' => ['text/richtext'], |
| 2703: | 'rv' => ['video/vnd.rn-realvideo'], |
| 2704: | 'sit' => ['application/x-stuffit'], |
| 2705: | 'smil' => ['application/smil'], |
| 2706: | 'srt' => ['text/srt'], |
| 2707: | 'svg' => ['image/svg+xml'], |
| 2708: | 'swf' => ['application/x-shockwave-flash'], |
| 2709: | 'tar' => ['application/x-tar'], |
| 2710: | 'tgz' => ['application/x-gzip-compressed'], |
| 2711: | 'tiff' => ['image/tiff'], |
| 2712: | 'ttf' => ['ont/ttf'], |
| 2713: | 'txt' => ['text/plain'], |
| 2714: | 'vcf' => ['text/x-vcard'], |
| 2715: | 'vlc' => ['application/videolan'], |
| 2716: | 'vtt' => ['text/vtt'], |
| 2717: | 'wav' => ['audio/x-wav', 'audio/wave', 'audio/wav'], |
| 2718: | 'wbxml' => ['application/wbxml'], |
| 2719: | 'webm' => ['video/webm'], |
| 2720: | 'webp' => ['image/webp'], |
| 2721: | 'wma' => ['audio/x-ms-wma'], |
| 2722: | 'wmlc' => ['application/wmlc'], |
| 2723: | 'wmv' => ['video/x-ms-wmv', 'video/x-ms-asf'], |
| 2724: | 'woff' => ['font/woff', 'font/woff2'], |
| 2725: | 'xhtml' => ['application/xhtml+xml'], |
| 2726: | 'xl' => ['application/excel'], |
| 2727: | 'xls' => ['application/msexcel', 'application/x-msexcel', 'application/x-ms-excel', 'application/x-excel', 'application/x-dos_ms_excel', 'application/xls', 'application/x-xls'], |
| 2728: | 'xlsx' => ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel'], |
| 2729: | 'xml' => ['application/xml', 'text/xml'], |
| 2730: | 'xsl' => ['text/xsl'], |
| 2731: | 'xspf' => ['application/xspf+xml'], |
| 2732: | 'z' => ['application/x-compress'], |
| 2733: | 'zip' => ['application/x-zip', 'application/zip', 'application/x-zip-compressed', 'application/s-compressed', 'multipart/x-zip'], |
| 2734: | 'zsh' => ['text/x-scriptzsh'], |
| 2735: | ]; |
| 2736: | |
| 2737: | $result = array_filter($mimeMap, function($val) use ($mime) { |
| 2738: | return in_array($mime, $val); |
| 2739: | }); |
| 2740: | |
| 2741: | return count($result) ? array_keys($result)[0] : null; |
| 2742: | } |
| 2743: | |