| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | namespace Aurora\System; |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | class Utils |
| 18: | { |
| 19: | |
| 20: | |
| 21: | |
| 22: | public static $aSuppostedCharsets = array( |
| 23: | 'iso-8859-1', 'iso-8859-2', 'iso-8859-3', 'iso-8859-4', 'iso-8859-5', 'iso-8859-6', |
| 24: | 'iso-8859-7', 'iso-8859-8', 'iso-8859-9', 'iso-8859-10', 'iso-8859-11', 'iso-8859-12', |
| 25: | 'iso-8859-13', 'iso-8859-14', 'iso-8859-15', 'iso-8859-16', |
| 26: | 'koi8-r', 'koi8-u', 'koi8-ru', |
| 27: | 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1257', 'cp949', 'cp1133', |
| 28: | 'cp850', 'cp866', 'cp1255', 'cp1256', 'cp862', 'cp874', 'cp932', 'cp950', 'cp1258', |
| 29: | 'windows-1250', 'windows-1251', 'windows-1252', 'windows-1253', 'windows-1254', 'windows-1255', |
| 30: | 'windows-1256', 'windows-1257', 'windows-1258', 'windows-874', |
| 31: | 'macroman', 'maccentraleurope', 'maciceland', 'maccroatian', 'macromania', 'maccyrillic', |
| 32: | 'macukraine', 'macgreek', 'macturkish', 'macintosh', 'machebrew', 'macarabic', |
| 33: | 'euc-jp', 'shift_jis', 'iso-2022-jp', 'iso-2022-jp-2', 'iso-2022-jp-1', |
| 34: | 'euc-cn', 'gb2312', 'hz', 'gbk', 'gb18030', 'euc-tw', 'big5', 'big5-hkscs', |
| 35: | 'iso-2022-cn', 'iso-2022-cn-ext', 'euc-kr', 'iso-2022-kr', 'johab', |
| 36: | 'armscii-8', 'georgian-academy', 'georgian-ps', 'koi8-t', |
| 37: | 'tis-620', 'macthai', 'mulelao-1', |
| 38: | 'viscii', 'tcvn', 'hp-roman8', 'nextstep', |
| 39: | 'utf-8', 'ucs-2', 'ucs-2be', 'ucs-2le', 'ucs-4', 'ucs-4be', 'ucs-4le', |
| 40: | 'utf-16', 'utf-16be', 'utf-16le', 'utf-32', 'utf-32be', 'utf-32le', 'utf-7', |
| 41: | 'c99', 'java', 'ucs-2-internal', 'ucs-4-internal'); |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | public static $sTimeZone = null; |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | public static function Microtime() |
| 52: | { |
| 53: | return microtime(true); |
| 54: | } |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | public static function ArrayValue($aArray, $sKey, $mDefault) |
| 63: | { |
| 64: | return (isset($aArray[$sKey])) ? $aArray[$sKey] : $mDefault; |
| 65: | } |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | public static function IsAssocArray($aArray) |
| 72: | { |
| 73: | return in_array(false, array_map('is_int', array_keys($aArray))); |
| 74: | } |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | public static function EncodeSpecialXmlChars($sValue) |
| 81: | { |
| 82: | return str_replace('>', '>', str_replace('<', '<', str_replace('&', '&', $sValue))); |
| 83: | } |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | public static function DecodeSpecialXmlChars($sValue) |
| 90: | { |
| 91: | return str_replace('&', '&', str_replace('<', '<', str_replace('>', '>', $sValue))); |
| 92: | } |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | public static function EncodeSimpleSpecialXmlChars($sValue) |
| 99: | { |
| 100: | return str_replace(']]>', ']]>', $sValue); |
| 101: | } |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | public static function DecodeSimpleSpecialXmlChars($sValue) |
| 108: | { |
| 109: | return str_replace(']]>', ']]>', $sValue); |
| 110: | } |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | public static function ShowCRLF($sValue) |
| 117: | { |
| 118: | return str_replace(array("\r", "\n", "\t"), array('\r', '\n', '\t'), $sValue); |
| 119: | } |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | public static function GetFullPath($sPath, $sPrefix = null) |
| 127: | { |
| 128: | if ($sPrefix !== null && !self::IsFullPath($sPath)) { |
| 129: | $sPath = rtrim($sPrefix, '\\/').'/'.trim($sPath, '\\/'); |
| 130: | } |
| 131: | |
| 132: | if (@is_dir($sPath)) { |
| 133: | $sPath = rtrim(str_replace('\\', '/', realpath($sPath)), '/'); |
| 134: | } |
| 135: | |
| 136: | return $sPath; |
| 137: | } |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | public static function IsFullPath($sPpath) |
| 144: | { |
| 145: | if (strlen($sPpath) > 0) { |
| 146: | return (($sPpath[0] == '/' || $sPpath[0] == '\\') || (strlen($sPpath) > 1 && self::IsWin() && $sPpath[1] == ':')); |
| 147: | } |
| 148: | return false; |
| 149: | } |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | public static function IsWin() |
| 155: | { |
| 156: | return (defined('PHP_OS') && 'WIN' === strtoupper(substr(PHP_OS, 0, 3))); |
| 157: | } |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | public static function SetTypeArrayValue($aArray, $sType) |
| 165: | { |
| 166: | $aResult = array(); |
| 167: | foreach ($aArray as $mValue) { |
| 168: | settype($mValue, $sType); |
| 169: | $aResult[] = $mValue; |
| 170: | } |
| 171: | return $aResult; |
| 172: | } |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | public static function ClearPrefix($sPrefix) |
| 179: | { |
| 180: | $sNewPrefix = preg_replace('/[^a-z0-9_]/i', '_', $sPrefix); |
| 181: | if ($sNewPrefix !== $sPrefix) { |
| 182: | $sNewPrefix = preg_replace('/[_]+/', '_', $sNewPrefix); |
| 183: | } |
| 184: | return $sNewPrefix; |
| 185: | } |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | protected static function iconvNormalizeCharset($sEncoding) |
| 192: | { |
| 193: | $sEncoding = strtolower($sEncoding); |
| 194: | switch ($sEncoding) { |
| 195: | case 'ansi': |
| 196: | case 'ansii': |
| 197: | case 'us-ansii': |
| 198: | $sEncoding = 'iso-8859-1'; |
| 199: | break; |
| 200: | case 'utf8': |
| 201: | case 'utf-8': |
| 202: | $sEncoding = 'utf-8'; |
| 203: | break; |
| 204: | case 'utf7-imap': |
| 205: | case 'utf7imap': |
| 206: | case 'utf-7imap': |
| 207: | case 'utf-7-imap': |
| 208: | $sEncoding = 'utf7-imap'; |
| 209: | break; |
| 210: | case 'ks-c-5601-1987': |
| 211: | case 'ks_c_5601-1987': |
| 212: | $sEncoding = 'euc-kr'; |
| 213: | break; |
| 214: | case 'x-gbk': |
| 215: | $sEncoding = 'gb2312'; |
| 216: | break; |
| 217: | case 'iso-8859-i': |
| 218: | case 'iso-8859-8-i': |
| 219: | $sEncoding = 'iso-8859-8'; |
| 220: | break; |
| 221: | } |
| 222: | |
| 223: | return $sEncoding; |
| 224: | } |
| 225: | |
| 226: | |
| 227: | |
| 228: | |
| 229: | |
| 230: | |
| 231: | |
| 232: | public static function ConvertEncoding($sString, $sFromEncoding, $sToEncoding) |
| 233: | { |
| 234: | $sResult = $sString; |
| 235: | $sFromEncoding = self::iconvNormalizeCharset($sFromEncoding); |
| 236: | $sToEncoding = self::iconvNormalizeCharset($sToEncoding); |
| 237: | |
| 238: | if ('' === trim($sResult) || $sFromEncoding === $sToEncoding) { |
| 239: | return $sResult; |
| 240: | } |
| 241: | |
| 242: | switch (true) { |
| 243: | default: |
| 244: | break; |
| 245: | case ($sFromEncoding === 'iso-8859-1' && $sToEncoding === 'utf-8' && function_exists('utf8_encode')): |
| 246: | $sResult = utf8_encode($sResult); |
| 247: | break; |
| 248: | case ($sFromEncoding === 'utf-8' && $sToEncoding === 'iso-8859-1' && function_exists('utf8_decode')): |
| 249: | $sResult = utf8_decode($sResult); |
| 250: | break; |
| 251: | case ($sFromEncoding === 'utf7-imap' && $sToEncoding === 'utf-8'): |
| 252: | $sResult = self::Utf7ModifiedToUtf8($sResult); |
| 253: | if (false === $sResult) { |
| 254: | $sResult = $sString; |
| 255: | } |
| 256: | break; |
| 257: | case ($sFromEncoding === 'utf-8' && $sToEncoding === 'utf7-imap'): |
| 258: | if (!self::IsUtf7($sResult)) { |
| 259: | $sResult = self::Utf8ToUtf7Modified($sResult); |
| 260: | } |
| 261: | if (false === $sResult) { |
| 262: | $sResult = $sString; |
| 263: | } |
| 264: | break; |
| 265: | case (in_array(strtolower($sFromEncoding), self::$aSuppostedCharsets)): |
| 266: | $sResult = @iconv($sFromEncoding, $sToEncoding.'//IGNORE', $sResult); |
| 267: | if (false === $sResult) { |
| 268: | Api::Log('iconv FALSE result ["'.$sFromEncoding.'", "'.$sToEncoding.'//IGNORE", "'.substr($sString, 0, 20).' / cut"]', Enums\LogLevel::Error); |
| 269: | $sResult = $sString; |
| 270: | } |
| 271: | break; |
| 272: | } |
| 273: | |
| 274: | return $sResult; |
| 275: | } |
| 276: | |
| 277: | |
| 278: | |
| 279: | |
| 280: | |
| 281: | public static function UrlSafeBase64Encode($sValue) |
| 282: | { |
| 283: | return \rtrim(\strtr(\base64_encode($sValue), '+/', '-_'), '='); |
| 284: | } |
| 285: | |
| 286: | |
| 287: | |
| 288: | |
| 289: | |
| 290: | public static function UrlSafeBase64Decode($sValue) |
| 291: | { |
| 292: | $sValue = \rtrim(\strtr($sValue, '-_', '+/'), '='); |
| 293: | return \MailSo\Base\Utils::Base64Decode(\str_pad($sValue, \strlen($sValue) + (\strlen($sValue) % 4), '=', STR_PAD_RIGHT)); |
| 294: | } |
| 295: | |
| 296: | |
| 297: | |
| 298: | |
| 299: | |
| 300: | public static function IsUtf7($sStr) |
| 301: | { |
| 302: | $iAmp = strpos($sStr, '&'); |
| 303: | return (false !== $iAmp && false !== strpos($sStr, '-', $iAmp)); |
| 304: | } |
| 305: | |
| 306: | |
| 307: | |
| 308: | |
| 309: | |
| 310: | public static function Utf7ModifiedToUtf8($str) |
| 311: | { |
| 312: | $array = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62, 63,-1,-1,-1,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1); |
| 313: | |
| 314: | $result = ''; |
| 315: | $error = false; |
| 316: | $strlen = strlen($str); |
| 317: | |
| 318: | for ($i = 0; $strlen > 0; $i++, $strlen--) { |
| 319: | $char = $str[$i]; |
| 320: | if ($char == '&') { |
| 321: | $i++; |
| 322: | $strlen--; |
| 323: | |
| 324: | $char = isset($str[$i]) ? $str[$i] : null; |
| 325: | if ($char === null) { |
| 326: | break; |
| 327: | } |
| 328: | |
| 329: | if ($strlen && $char == '-') { |
| 330: | $result .= '&'; |
| 331: | continue; |
| 332: | } |
| 333: | |
| 334: | $ch = 0; |
| 335: | $k = 10; |
| 336: | for (; $strlen > 0; $i++, $strlen--) { |
| 337: | $char = $str[$i]; |
| 338: | |
| 339: | $b = $array[ord($char)]; |
| 340: | if ((ord($char) & 0x80) || $b == -1) { |
| 341: | break; |
| 342: | } |
| 343: | |
| 344: | if ($k > 0) { |
| 345: | $ch |= $b << $k; |
| 346: | $k -= 6; |
| 347: | } else { |
| 348: | $ch |= $b >> (-$k); |
| 349: | if ($ch < 0x80) { |
| 350: | if (0x20 <= $ch && $ch < 0x7f) { |
| 351: | return $error; |
| 352: | } |
| 353: | |
| 354: | $result .= chr($ch); |
| 355: | } elseif ($ch < 0x800) { |
| 356: | $result .= chr(0xc0 | ($ch >> 6)); |
| 357: | $result .= chr(0x80 | ($ch & 0x3f)); |
| 358: | } else { |
| 359: | $result .= chr(0xe0 | ($ch >> 12)); |
| 360: | $result .= chr(0x80 | (($ch >> 6) & 0x3f)); |
| 361: | $result .= chr(0x80 | ($ch & 0x3f)); |
| 362: | } |
| 363: | |
| 364: | $ch = ($b << (16 + $k)) & 0xffff; |
| 365: | $k += 10; |
| 366: | } |
| 367: | } |
| 368: | |
| 369: | if (($ch || $k < 6) || |
| 370: | (!$strlen || $char != '-') || |
| 371: | ($strlen > 2 && '&' === $str[$i+1] && '-' !== $str[$i+2])) { |
| 372: | return $error; |
| 373: | } |
| 374: | } elseif (ord($char) < 0x20 || ord($char) >= 0x7f) { |
| 375: | return $error; |
| 376: | } else { |
| 377: | $result .= $char; |
| 378: | } |
| 379: | } |
| 380: | |
| 381: | return $result; |
| 382: | } |
| 383: | |
| 384: | |
| 385: | |
| 386: | |
| 387: | |
| 388: | public static function Utf8ToUtf7Modified($str) |
| 389: | { |
| 390: | $array = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+',','); |
| 391: | |
| 392: | $strlen = strlen($str); |
| 393: | $isB = false; |
| 394: | $i = $n = 0; |
| 395: | $return = ''; |
| 396: | $error = false; |
| 397: | $ch = $b = $k = 0; |
| 398: | |
| 399: | while ($strlen) { |
| 400: | $c = ord($str[$i]); |
| 401: | if ($c < 0x80) { |
| 402: | $ch = $c; |
| 403: | $n = 0; |
| 404: | } elseif ($c < 0xc2) { |
| 405: | return $error; |
| 406: | } elseif ($c < 0xe0) { |
| 407: | $ch = $c & 0x1f; |
| 408: | $n = 1; |
| 409: | } elseif ($c < 0xf0) { |
| 410: | $ch = $c & 0x0f; |
| 411: | $n = 2; |
| 412: | } elseif ($c < 0xf8) { |
| 413: | $ch = $c & 0x07; |
| 414: | $n = 3; |
| 415: | } elseif ($c < 0xfc) { |
| 416: | $ch = $c & 0x03; |
| 417: | $n = 4; |
| 418: | } elseif ($c < 0xfe) { |
| 419: | $ch = $c & 0x01; |
| 420: | $n = 5; |
| 421: | } else { |
| 422: | return $error; |
| 423: | } |
| 424: | |
| 425: | $i++; |
| 426: | $strlen--; |
| 427: | |
| 428: | if ($n > $strlen) { |
| 429: | return $error; |
| 430: | } |
| 431: | |
| 432: | for ($j=0; $j < $n; $j++) { |
| 433: | $o = ord($str[$i+$j]); |
| 434: | if (($o & 0xc0) != 0x80) { |
| 435: | return $error; |
| 436: | } |
| 437: | |
| 438: | $ch = ($ch << 6) | ($o & 0x3f); |
| 439: | } |
| 440: | |
| 441: | if ($n > 1 && !($ch >> ($n * 5 + 1))) { |
| 442: | return $error; |
| 443: | } |
| 444: | |
| 445: | $i += $n; |
| 446: | $strlen -= $n; |
| 447: | |
| 448: | if ($ch < 0x20 || $ch >= 0x7f) { |
| 449: | if (!$isB) { |
| 450: | $return .= '&'; |
| 451: | $isB = true; |
| 452: | $b = 0; |
| 453: | $k = 10; |
| 454: | } |
| 455: | |
| 456: | if ($ch & ~0xffff) { |
| 457: | $ch = 0xfffe; |
| 458: | } |
| 459: | |
| 460: | $return .= $array[($b | $ch >> $k)]; |
| 461: | $k -= 6; |
| 462: | for (; $k >= 0; $k -= 6) { |
| 463: | $return .= $array[(($ch >> $k) & 0x3f)]; |
| 464: | } |
| 465: | |
| 466: | $b = ($ch << (-$k)) & 0x3f; |
| 467: | $k += 16; |
| 468: | } else { |
| 469: | if ($isB) { |
| 470: | if ($k > 10) { |
| 471: | $return .= $array[$b]; |
| 472: | } |
| 473: | $return .= '-'; |
| 474: | $isB = false; |
| 475: | } |
| 476: | |
| 477: | $return .= chr($ch); |
| 478: | if ('&' === chr($ch)) { |
| 479: | $return .= '-'; |
| 480: | } |
| 481: | } |
| 482: | } |
| 483: | |
| 484: | if ($isB) { |
| 485: | if ($k > 10) { |
| 486: | $return .= $array[$b]; |
| 487: | } |
| 488: | |
| 489: | $return .= '-'; |
| 490: | } |
| 491: | |
| 492: | return $return; |
| 493: | } |
| 494: | |
| 495: | |
| 496: | |
| 497: | |
| 498: | |
| 499: | public static function EncryptValue($sPassword) |
| 500: | { |
| 501: | $sPassword = \MailSo\Base\Crypt::XxteaEncrypt($sPassword, Api::$sSalt); |
| 502: | return @trim(self::UrlSafeBase64Encode($sPassword)); |
| 503: | } |
| 504: | |
| 505: | |
| 506: | |
| 507: | |
| 508: | |
| 509: | public static function DecryptValue($sPassword) |
| 510: | { |
| 511: | $sPassword = self::UrlSafeBase64Decode(trim($sPassword)); |
| 512: | return \MailSo\Base\Crypt::XxteaDecrypt($sPassword, Api::$sSalt); |
| 513: | } |
| 514: | |
| 515: | |
| 516: | |
| 517: | |
| 518: | |
| 519: | public static function GetAccountNameFromEmail($sEmail) |
| 520: | { |
| 521: | $sResult = ''; |
| 522: | if (!empty($sEmail)) { |
| 523: | $iPos = strpos($sEmail, '@'); |
| 524: | $sResult = (false === $iPos) ? $sEmail : substr($sEmail, 0, $iPos); |
| 525: | } |
| 526: | |
| 527: | return $sResult; |
| 528: | } |
| 529: | |
| 530: | |
| 531: | |
| 532: | |
| 533: | |
| 534: | public static function GetFriendlySize($iSizeInBytes) |
| 535: | { |
| 536: | $iSizeInKB = ceil($iSizeInBytes / 1024); |
| 537: | $iSizeInMB = $iSizeInKB / 1024; |
| 538: | if ($iSizeInMB >= 100) { |
| 539: | $iSizeInKB = ceil($iSizeInMB * 10 / 10).'MB'; |
| 540: | } elseif ($iSizeInMB > 1) { |
| 541: | $iSizeInKB = (ceil($iSizeInMB * 10) / 10).'MB'; |
| 542: | } else { |
| 543: | $iSizeInKB = $iSizeInKB.'KB'; |
| 544: | } |
| 545: | |
| 546: | return $iSizeInKB; |
| 547: | } |
| 548: | |
| 549: | |
| 550: | |
| 551: | |
| 552: | |
| 553: | public static function GetFriendlySizeSpec($iSizeInBytes) |
| 554: | { |
| 555: | $size = ceil($iSizeInBytes / 1024); |
| 556: | $mbSize = $size / 1024; |
| 557: | return ($mbSize > 1) |
| 558: | ? (($mbSize >= 1024) |
| 559: | ? (ceil(($mbSize * 10) / 1024) / 10).'GB' |
| 560: | : (ceil($mbSize * 10) / 10).'MB') |
| 561: | : $size.'KB'; |
| 562: | } |
| 563: | |
| 564: | |
| 565: | |
| 566: | |
| 567: | |
| 568: | |
| 569: | public static function GetCodePageName($iCodePage) |
| 570: | { |
| 571: | static $aMapping = array( |
| 572: | 0 => 'default', |
| 573: | 51936 => 'euc-cn', |
| 574: | 936 => 'gb2312', |
| 575: | 950 => 'big5', |
| 576: | 946 => 'euc-kr', |
| 577: | 50225 => 'iso-2022-kr', |
| 578: | 50220 => 'iso-2022-jp', |
| 579: | 932 => 'shift-jis', |
| 580: | 65000 => 'utf-7', |
| 581: | 65001 => 'utf-8', |
| 582: | 1250 => 'windows-1250', |
| 583: | 1251 => 'windows-1251', |
| 584: | 1252 => 'windows-1252', |
| 585: | 1253 => 'windows-1253', |
| 586: | 1254 => 'windows-1254', |
| 587: | 1255 => 'windows-1255', |
| 588: | 1256 => 'windows-1256', |
| 589: | 1257 => 'windows-1257', |
| 590: | 1258 => 'windows-1258', |
| 591: | 20866 => 'koi8-r', |
| 592: | 28591 => 'iso-8859-1', |
| 593: | 28592 => 'iso-8859-2', |
| 594: | 28593 => 'iso-8859-3', |
| 595: | 28594 => 'iso-8859-4', |
| 596: | 28595 => 'iso-8859-5', |
| 597: | 28596 => 'iso-8859-6', |
| 598: | 28597 => 'iso-8859-7', |
| 599: | 28598 => 'iso-8859-8' |
| 600: | ); |
| 601: | |
| 602: | return (isset($aMapping[$iCodePage])) ? $aMapping[$iCodePage] : ''; |
| 603: | } |
| 604: | |
| 605: | |
| 606: | |
| 607: | |
| 608: | |
| 609: | |
| 610: | public static function GetCodePageNumber($sCodePageName) |
| 611: | { |
| 612: | static $aMapping = array( |
| 613: | 'default' => 0, |
| 614: | 'euc-cn' => 51936, |
| 615: | 'gb2312' => 936, |
| 616: | 'big5' => 950, |
| 617: | 'euc-kr' => 949, |
| 618: | 'iso-2022-kr' => 50225, |
| 619: | 'iso-2022-jp' => 50220, |
| 620: | 'shift-jis' => 932, |
| 621: | 'utf-7' => 65000, |
| 622: | 'utf-8' => 65001, |
| 623: | 'windows-1250' => 1250, |
| 624: | 'windows-1251' => 1251, |
| 625: | 'windows-1252' => 1252, |
| 626: | 'windows-1253' => 1253, |
| 627: | 'windows-1254' => 1254, |
| 628: | 'windows-1255' => 1255, |
| 629: | 'windows-1256' => 1256, |
| 630: | 'windows-1257' => 1257, |
| 631: | 'windows-1258' => 1258, |
| 632: | 'koi8-r' => 20866, |
| 633: | 'iso-8859-1' => 28591, |
| 634: | 'iso-8859-2' => 28592, |
| 635: | 'iso-8859-3' => 28593, |
| 636: | 'iso-8859-4' => 28594, |
| 637: | 'iso-8859-5' => 28595, |
| 638: | 'iso-8859-6' => 28596, |
| 639: | 'iso-8859-7' => 28597, |
| 640: | 'iso-8859-8' => 28598 |
| 641: | ); |
| 642: | |
| 643: | return (isset($aMapping[$sCodePageName])) ? $aMapping[$sCodePageName] : 0; |
| 644: | } |
| 645: | |
| 646: | |
| 647: | |
| 648: | |
| 649: | |
| 650: | public static function DateParse($sDateTime) |
| 651: | { |
| 652: | if (function_exists('date_parse')) { |
| 653: | return date_parse($sDateTime); |
| 654: | } |
| 655: | |
| 656: | $mReturn = false; |
| 657: | $aDateTime = explode(' ', $sDateTime, 2); |
| 658: | if (count($aDateTime) == 2) { |
| 659: | $aDate = explode('-', trim($aDateTime[0]), 3); |
| 660: | $aTime = explode(':', trim($aDateTime[1]), 3); |
| 661: | |
| 662: | if (3 === count($aDate) && 3 === count($aTime)) { |
| 663: | $mReturn = array( |
| 664: | 'year' => $aDate[0], |
| 665: | 'day' => $aDate[2], |
| 666: | 'month' => $aDate[1], |
| 667: | |
| 668: | 'hour' => $aTime[0], |
| 669: | 'minute' => $aTime[1], |
| 670: | 'second' => $aTime[2] |
| 671: | ); |
| 672: | } |
| 673: | } |
| 674: | return $mReturn; |
| 675: | } |
| 676: | |
| 677: | |
| 678: | |
| 679: | |
| 680: | |
| 681: | public static function GetTimeOffsetFromHoursString($sTimeOffset) |
| 682: | { |
| 683: | $iResult = 0; |
| 684: | $sTimeOffset = trim($sTimeOffset); |
| 685: | if (0 < strlen($sTimeOffset)) { |
| 686: | $sSign = $sTimeOffset[0]; |
| 687: | $sTimeOffset = substr($sTimeOffset, 1); |
| 688: | $nOffset = (is_numeric($sTimeOffset)) ? (int) $sTimeOffset : 0; |
| 689: | |
| 690: | $iHours = $nOffset / 100; |
| 691: | $iMinutes = $nOffset % 100; |
| 692: | |
| 693: | $iMultiplier = ('-' === $sSign) ? -1 : 1; |
| 694: | |
| 695: | $iResult += $iMultiplier * $iHours * 60 * 60; |
| 696: | $iResult += $iMultiplier * $iMinutes * 60; |
| 697: | } |
| 698: | |
| 699: | return $iResult; |
| 700: | } |
| 701: | |
| 702: | |
| 703: | |
| 704: | |
| 705: | |
| 706: | |
| 707: | public static function GetTimeOffset($iDefaultTimeZone, $sClientTimeZone = '') |
| 708: | { |
| 709: | if ('' !== $sClientTimeZone) { |
| 710: | try { |
| 711: | $oDateTimeZone = new \DateTimeZone($sClientTimeZone); |
| 712: | return $oDateTimeZone->getOffset(new \DateTime('now')) / 60; |
| 713: | } catch (\Exception $oE) { |
| 714: | Api::Log($sClientTimeZone); |
| 715: | Api::LogObject($oE, Enums\LogLevel::Warning); |
| 716: | } |
| 717: | } |
| 718: | |
| 719: | $iTimeOffset = 0; |
| 720: | switch ($iDefaultTimeZone) { |
| 721: | default: |
| 722: | case 0: |
| 723: | break; |
| 724: | case 1: |
| 725: | $iTimeOffset = -12 * 60; |
| 726: | break; |
| 727: | case 2: |
| 728: | $iTimeOffset = -11 * 60; |
| 729: | break; |
| 730: | case 3: |
| 731: | $iTimeOffset = -10 * 60; |
| 732: | break; |
| 733: | case 4: |
| 734: | $iTimeOffset = -9 * 60; |
| 735: | break; |
| 736: | case 5: |
| 737: | $iTimeOffset = -8*60; |
| 738: | break; |
| 739: | case 6: |
| 740: | case 7: |
| 741: | $iTimeOffset = -7 * 60; |
| 742: | break; |
| 743: | case 8: |
| 744: | case 9: |
| 745: | case 10: |
| 746: | case 11: |
| 747: | $iTimeOffset = -6 * 60; |
| 748: | break; |
| 749: | case 12: |
| 750: | case 13: |
| 751: | case 14: |
| 752: | $iTimeOffset = -5 * 60; |
| 753: | break; |
| 754: | case 15: |
| 755: | case 16: |
| 756: | case 17: |
| 757: | $iTimeOffset = -4 * 60; |
| 758: | break; |
| 759: | case 18: |
| 760: | $iTimeOffset = -3.5 * 60; |
| 761: | break; |
| 762: | case 19: |
| 763: | case 20: |
| 764: | case 21: |
| 765: | $iTimeOffset = -3 * 60; |
| 766: | break; |
| 767: | case 22: |
| 768: | $iTimeOffset = -2 * 60; |
| 769: | break; |
| 770: | case 23: |
| 771: | case 24: |
| 772: | $iTimeOffset = -60; |
| 773: | break; |
| 774: | case 25: |
| 775: | case 26: |
| 776: | $iTimeOffset = 0; |
| 777: | break; |
| 778: | case 27: |
| 779: | case 28: |
| 780: | case 29: |
| 781: | case 30: |
| 782: | case 31: |
| 783: | $iTimeOffset = 60; |
| 784: | break; |
| 785: | case 32: |
| 786: | case 33: |
| 787: | case 34: |
| 788: | case 35: |
| 789: | case 36: |
| 790: | case 37: |
| 791: | $iTimeOffset = 2 * 60; |
| 792: | break; |
| 793: | case 38: |
| 794: | case 39: |
| 795: | case 40: |
| 796: | $iTimeOffset = 3 * 60; |
| 797: | break; |
| 798: | case 41: |
| 799: | $iTimeOffset = 3.5 * 60; |
| 800: | break; |
| 801: | case 42: |
| 802: | case 43: |
| 803: | case 44: |
| 804: | $iTimeOffset = 4 * 60; |
| 805: | break; |
| 806: | case 45: |
| 807: | $iTimeOffset = 4.5 * 60; |
| 808: | break; |
| 809: | case 46: |
| 810: | $iTimeOffset = 5 * 60; |
| 811: | break; |
| 812: | case 47: |
| 813: | $iTimeOffset = 5.5 * 60; |
| 814: | break; |
| 815: | case 48: |
| 816: | $iTimeOffset = 5 * 60 + 45; |
| 817: | break; |
| 818: | case 49: |
| 819: | case 50: |
| 820: | case 51: |
| 821: | case 52: |
| 822: | $iTimeOffset = 6 * 60; |
| 823: | break; |
| 824: | case 53: |
| 825: | $iTimeOffset = 6.5 * 60; |
| 826: | |
| 827: | case 54: |
| 828: | $iTimeOffset = 7 * 60; |
| 829: | break; |
| 830: | case 55: |
| 831: | case 56: |
| 832: | case 57: |
| 833: | case 58: |
| 834: | case 59: |
| 835: | case 60: |
| 836: | $iTimeOffset = 8 * 60; |
| 837: | break; |
| 838: | case 61: |
| 839: | case 62: |
| 840: | $iTimeOffset = 9 * 60; |
| 841: | break; |
| 842: | case 63: |
| 843: | case 64: |
| 844: | $iTimeOffset = 9.5 * 60; |
| 845: | break; |
| 846: | case 65: |
| 847: | case 66: |
| 848: | case 67: |
| 849: | case 68: |
| 850: | case 69: |
| 851: | $iTimeOffset = 10 * 60; |
| 852: | break; |
| 853: | case 70: |
| 854: | case 71: |
| 855: | $iTimeOffset = 11 * 60; |
| 856: | break; |
| 857: | case 72: |
| 858: | case 73: |
| 859: | $iTimeOffset = 12 * 60; |
| 860: | break; |
| 861: | case 74: |
| 862: | $iTimeOffset = 13 * 60; |
| 863: | break; |
| 864: | } |
| 865: | |
| 866: | return $iTimeOffset; |
| 867: | } |
| 868: | |
| 869: | |
| 870: | |
| 871: | |
| 872: | |
| 873: | |
| 874: | public static function GetStrTimeZone($iDefaultTimeZone, $sClientTimeZone = '') |
| 875: | { |
| 876: | if ('' !== $sClientTimeZone) { |
| 877: | return $sClientTimeZone; |
| 878: | } |
| 879: | |
| 880: | if (null !== self::$sTimeZone) { |
| 881: | return self::$sTimeZone; |
| 882: | } |
| 883: | |
| 884: | $sResult = 'Etc/GMT'; |
| 885: | |
| 886: | $aTimeZones = array( |
| 887: | 'Default', |
| 888: | 'Pacific/Kwajalein', |
| 889: | 'Pacific/Midway', |
| 890: | 'US/Hawaii', |
| 891: | 'US/Alaska', |
| 892: | 'America/Tijuana', |
| 893: | 'America/Dawson_Creek', |
| 894: | 'America/Denver', |
| 895: | 'America/Belize', |
| 896: | 'America/Chicago', |
| 897: | 'America/Cancun', |
| 898: | 'America/Belize', |
| 899: | 'America/Havana', |
| 900: | 'America/New_York', |
| 901: | 'America/Bogota', |
| 902: | 'America/Santiago', |
| 903: | 'America/Caracas', |
| 904: | 'America/Glace_Bay', |
| 905: | 'America/St_Johns', |
| 906: | 'America/Godthab', |
| 907: | 'America/Argentina/Buenos_Aires', |
| 908: | 'America/Sao_Paulo', |
| 909: | 'America/Noronha', |
| 910: | 'Atlantic/Cape_Verde', |
| 911: | 'Atlantic/Azores', |
| 912: | 'Africa/Abidjan', |
| 913: | 'Europe/Dublin', |
| 914: | 'Europe/Amsterdam', |
| 915: | 'Europe/Belgrade', |
| 916: | 'Europe/Brussels', |
| 917: | 'Europe/Sarajevo', |
| 918: | 'Africa/Algiers', |
| 919: | 'Europe/Minsk', |
| 920: | 'Europe/Bucharest', |
| 921: | 'Africa/Cairo', |
| 922: | 'Africa/Blantyre', |
| 923: | 'Africa/Harare', |
| 924: | 'Asia/Jerusalem', |
| 925: | 'Asia/Baghdad', |
| 926: | 'Asia/Kuwait', |
| 927: | 'Africa/Addis_Ababa', |
| 928: | 'Europe/Moscow', |
| 929: | 'Asia/Tehran', |
| 930: | 'Asia/Dubai', |
| 931: | 'Asia/Yerevan', |
| 932: | 'Asia/Kabul', |
| 933: | 'Asia/Tashkent', |
| 934: | 'Asia/Kolkata', |
| 935: | 'Asia/Katmandu', |
| 936: | 'Asia/Yekaterinburg', |
| 937: | 'Asia/Almaty', |
| 938: | 'Asia/Dhaka', |
| 939: | 'Asia/Colombo', |
| 940: | 'Asia/Rangoon', |
| 941: | 'Asia/Bangkok', |
| 942: | 'Asia/Krasnoyarsk', |
| 943: | 'Asia/Hong_Kong', |
| 944: | 'Asia/Irkutsk', |
| 945: | 'Asia/Kuala_Lumpur', |
| 946: | 'Australia/Perth', |
| 947: | 'Asia/Taipei', |
| 948: | 'Asia/Tokyo', |
| 949: | 'Asia/Seoul', |
| 950: | 'Australia/Adelaide', |
| 951: | 'Australia/Darwin', |
| 952: | 'Asia/Yakutsk', |
| 953: | 'Australia/Brisbane', |
| 954: | 'Australia/Canberra', |
| 955: | 'Pacific/Guam', |
| 956: | 'Australia/Hobart', |
| 957: | 'Asia/Vladivostok', |
| 958: | 'Pacific/Noumea', |
| 959: | 'Asia/Magadan', |
| 960: | 'Asia/Anadyr', |
| 961: | 'Pacific/Tongatapu' |
| 962: | ); |
| 963: | |
| 964: | $iDefaultTimeZone = isset($aTimeZones[$iDefaultTimeZone]) ? $iDefaultTimeZone : 0; |
| 965: | |
| 966: | if (0 === $iDefaultTimeZone) { |
| 967: | $iOffset = self::GetTimeOffset($iDefaultTimeZone, $sClientTimeZone) / 60 * -1; |
| 968: | $sSign = ($iOffset < 0) ? '-' : '+'; |
| 969: | $sResult = 'Etc/GMT'.$sSign.abs($iOffset); |
| 970: | } else { |
| 971: | $sResult = $aTimeZones[$iDefaultTimeZone]; |
| 972: | } |
| 973: | |
| 974: | self::$sTimeZone = $sResult; |
| 975: | return self::$sTimeZone; |
| 976: | } |
| 977: | |
| 978: | |
| 979: | |
| 980: | |
| 981: | public static function RecRmdir($sDir) |
| 982: | { |
| 983: | if (is_dir($sDir)) { |
| 984: | $aObjects = scandir($sDir); |
| 985: | foreach ($aObjects as $sObject) { |
| 986: | if ($sObject != '.' && $sObject != '..') { |
| 987: | if (filetype($sDir.'/'.$sObject) == 'dir') { |
| 988: | self::RecRmdir($sDir.'/'.$sObject); |
| 989: | } else { |
| 990: | unlink($sDir.'/'.$sObject); |
| 991: | } |
| 992: | } |
| 993: | } |
| 994: | |
| 995: | reset($aObjects); |
| 996: | rmdir($sDir); |
| 997: | } |
| 998: | } |
| 999: | |
| 1000: | |
| 1001: | |
| 1002: | |
| 1003: | public static function IsPhp53() |
| 1004: | { |
| 1005: | return (bool) (version_compare(phpversion(), '5.3.0') > -1); |
| 1006: | } |
| 1007: | |
| 1008: | |
| 1009: | |
| 1010: | |
| 1011: | public static function HasGdSupport() |
| 1012: | { |
| 1013: | return (bool) @function_exists('imagecreatefrompng'); |
| 1014: | } |
| 1015: | |
| 1016: | |
| 1017: | |
| 1018: | |
| 1019: | public static function hasSslSupport() |
| 1020: | { |
| 1021: | return (bool) @function_exists('openssl_open'); |
| 1022: | } |
| 1023: | |
| 1024: | |
| 1025: | |
| 1026: | |
| 1027: | public static function IsMcryptSupported() |
| 1028: | { |
| 1029: | return (bool) @function_exists('mcrypt_encrypt'); |
| 1030: | } |
| 1031: | |
| 1032: | |
| 1033: | |
| 1034: | |
| 1035: | public static function IsIconvSupported() |
| 1036: | { |
| 1037: | return (bool) @function_exists('iconv'); |
| 1038: | } |
| 1039: | |
| 1040: | |
| 1041: | |
| 1042: | |
| 1043: | public static function IsGzipSupported() |
| 1044: | { |
| 1045: | return (bool) |
| 1046: | ((false !== strpos(isset($_SERVER['HTTP_ACCEPT_ENCODING']) |
| 1047: | ? $_SERVER['HTTP_ACCEPT_ENCODING'] : '', 'gzip')) |
| 1048: | && function_exists('gzencode')); |
| 1049: | } |
| 1050: | |
| 1051: | |
| 1052: | |
| 1053: | |
| 1054: | |
| 1055: | public static function GetFileExtension($sFileName) |
| 1056: | { |
| 1057: | $iLast = strrpos($sFileName, '.'); |
| 1058: | $sExtension = ''; |
| 1059: | if ($iLast !== false) { |
| 1060: | $sExtension = substr($sFileName, $iLast + 1); |
| 1061: | if (strlen($sExtension) > 5) { |
| 1062: | $sExtension = ''; |
| 1063: | } |
| 1064: | } |
| 1065: | return $sExtension; |
| 1066: | } |
| 1067: | |
| 1068: | |
| 1069: | |
| 1070: | |
| 1071: | |
| 1072: | public static function GenerateShortHashString($sLen = 10) |
| 1073: | { |
| 1074: | $sResult = ''; |
| 1075: | $sChars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789'; |
| 1076: | $iCharLen = strlen($sChars); |
| 1077: | |
| 1078: | for (; 0 < $sLen; $sLen--) { |
| 1079: | $sResult .= substr($sChars, rand(0, $iCharLen), 1); |
| 1080: | } |
| 1081: | |
| 1082: | return $sResult; |
| 1083: | } |
| 1084: | |
| 1085: | public static function GetMimeContentTypes() |
| 1086: | { |
| 1087: | return array( |
| 1088: | |
| 1089: | 'eml' => 'message/rfc822', |
| 1090: | 'mime' => 'message/rfc822', |
| 1091: | 'txt' => 'text/plain', |
| 1092: | 'text' => 'text/plain', |
| 1093: | 'def' => 'text/plain', |
| 1094: | 'list' => 'text/plain', |
| 1095: | 'in' => 'text/plain', |
| 1096: | 'ini' => 'text/plain', |
| 1097: | 'log' => 'text/plain', |
| 1098: | 'sql' => 'text/plain', |
| 1099: | 'cfg' => 'text/plain', |
| 1100: | 'conf' => 'text/plain', |
| 1101: | 'rtx' => 'text/richtext', |
| 1102: | 'vcard' => 'text/vcard', |
| 1103: | 'vcf' => 'text/vcard', |
| 1104: | 'htm' => 'text/html', |
| 1105: | 'html' => 'text/html', |
| 1106: | 'csv' => 'text/csv', |
| 1107: | 'ics' => 'text/calendar', |
| 1108: | 'ifb' => 'text/calendar', |
| 1109: | 'xml' => 'text/xml', |
| 1110: | 'json' => 'application/json', |
| 1111: | 'swf' => 'application/x-shockwave-flash', |
| 1112: | 'hlp' => 'application/winhlp', |
| 1113: | 'wgt' => 'application/widget', |
| 1114: | 'chm' => 'application/vnd.ms-htmlhelp', |
| 1115: | 'p10' => 'application/pkcs10', |
| 1116: | 'p7c' => 'application/pkcs7-mime', |
| 1117: | 'p7m' => 'application/pkcs7-mime', |
| 1118: | 'p7s' => 'application/pkcs7-signature', |
| 1119: | |
| 1120: | 'torrent' => 'application/x-bittorrent', |
| 1121: | |
| 1122: | |
| 1123: | 'js' => 'application/javascript', |
| 1124: | 'pl' => 'text/perl', |
| 1125: | 'css' => 'text/css', |
| 1126: | 'asp' => 'text/asp', |
| 1127: | 'php' => 'application/x-httpd-php', |
| 1128: | 'php3' => 'application/x-httpd-php', |
| 1129: | 'php4' => 'application/x-httpd-php', |
| 1130: | 'php5' => 'application/x-httpd-php', |
| 1131: | 'phtml' => 'application/x-httpd-php', |
| 1132: | |
| 1133: | |
| 1134: | 'png' => 'image/png', |
| 1135: | 'jpg' => 'image/jpeg', |
| 1136: | 'jpeg' => 'image/jpeg', |
| 1137: | 'jpe' => 'image/jpeg', |
| 1138: | 'jfif' => 'image/jpeg', |
| 1139: | 'gif' => 'image/gif', |
| 1140: | 'bmp' => 'image/bmp', |
| 1141: | 'cgm' => 'image/cgm', |
| 1142: | 'ief' => 'image/ief', |
| 1143: | 'ico' => 'image/x-icon', |
| 1144: | 'tif' => 'image/tiff', |
| 1145: | 'tiff' => 'image/tiff', |
| 1146: | 'svg' => 'image/svg+xml', |
| 1147: | 'svgz' => 'image/svg+xml', |
| 1148: | 'djv' => 'image/vnd.djvu', |
| 1149: | 'djvu' => 'image/vnd.djvu', |
| 1150: | 'webp' => 'image/webp', |
| 1151: | |
| 1152: | |
| 1153: | 'zip' => 'application/zip', |
| 1154: | '7z' => 'application/x-7z-compressed', |
| 1155: | 'rar' => 'application/x-rar-compressed', |
| 1156: | 'exe' => 'application/x-msdownload', |
| 1157: | 'dll' => 'application/x-msdownload', |
| 1158: | 'scr' => 'application/x-msdownload', |
| 1159: | 'com' => 'application/x-msdownload', |
| 1160: | 'bat' => 'application/x-msdownload', |
| 1161: | 'msi' => 'application/x-msdownload', |
| 1162: | 'cab' => 'application/vnd.ms-cab-compressed', |
| 1163: | 'gz' => 'application/x-gzip', |
| 1164: | 'tgz' => 'application/x-gzip', |
| 1165: | 'bz' => 'application/x-bzip', |
| 1166: | 'bz2' => 'application/x-bzip2', |
| 1167: | 'deb' => 'application/x-debian-package', |
| 1168: | |
| 1169: | |
| 1170: | 'psf' => 'application/x-font-linux-psf', |
| 1171: | 'otf' => 'application/x-font-otf', |
| 1172: | 'pcf' => 'application/x-font-pcf', |
| 1173: | 'snf' => 'application/x-font-snf', |
| 1174: | 'ttf' => 'application/x-font-ttf', |
| 1175: | 'ttc' => 'application/x-font-ttf', |
| 1176: | |
| 1177: | |
| 1178: | 'mp3' => 'audio/mpeg', |
| 1179: | 'amr' => 'audio/amr', |
| 1180: | 'aac' => 'audio/x-aac', |
| 1181: | 'aif' => 'audio/x-aiff', |
| 1182: | 'aifc' => 'audio/x-aiff', |
| 1183: | 'aiff' => 'audio/x-aiff', |
| 1184: | 'wav' => 'audio/x-wav', |
| 1185: | 'wma' => 'audio/x-ms-wma', |
| 1186: | 'wax' => 'audio/x-ms-wax', |
| 1187: | 'midi' => 'audio/midi', |
| 1188: | 'mp4a' => 'audio/mp4', |
| 1189: | 'ogg' => 'audio/ogg', |
| 1190: | 'weba' => 'audio/webm', |
| 1191: | 'ra' => 'audio/x-pn-realaudio', |
| 1192: | 'ram' => 'audio/x-pn-realaudio', |
| 1193: | 'rmp' => 'audio/x-pn-realaudio-plugin', |
| 1194: | 'm3u' => 'audio/x-mpegurl', |
| 1195: | |
| 1196: | |
| 1197: | 'flv' => 'video/x-flv', |
| 1198: | 'qt' => 'video/quicktime', |
| 1199: | 'mov' => 'video/quicktime', |
| 1200: | |
| 1201: | 'avi' => 'video/x-msvideo', |
| 1202: | 'mpg' => 'video/mpeg', |
| 1203: | 'mpeg' => 'video/mpeg', |
| 1204: | 'mpe' => 'video/mpeg', |
| 1205: | 'm1v' => 'video/mpeg', |
| 1206: | 'm2v' => 'video/mpeg', |
| 1207: | '3gp' => 'video/3gpp', |
| 1208: | '3g2' => 'video/3gpp2', |
| 1209: | 'h261' => 'video/h261', |
| 1210: | 'h263' => 'video/h263', |
| 1211: | 'h264' => 'video/h264', |
| 1212: | 'jpgv' => 'video/jpgv', |
| 1213: | 'mp4v' => 'video/mp4', |
| 1214: | 'mpg4' => 'video/mp4', |
| 1215: | 'ogv' => 'video/ogg', |
| 1216: | 'webm' => 'video/webm', |
| 1217: | 'm4v' => 'video/x-m4v', |
| 1218: | 'asf' => 'video/x-ms-asf', |
| 1219: | 'asx' => 'video/x-ms-asf', |
| 1220: | 'wm' => 'video/x-ms-wm', |
| 1221: | 'wmv' => 'video/x-ms-wmv', |
| 1222: | 'wmx' => 'video/x-ms-wmx', |
| 1223: | 'wvx' => 'video/x-ms-wvx', |
| 1224: | 'movie' => 'video/x-sgi-movie', |
| 1225: | |
| 1226: | |
| 1227: | 'pdf' => 'application/pdf', |
| 1228: | 'psd' => 'image/vnd.adobe.photoshop', |
| 1229: | 'ai' => 'application/postscript', |
| 1230: | 'eps' => 'application/postscript', |
| 1231: | 'ps' => 'application/postscript', |
| 1232: | |
| 1233: | |
| 1234: | 'doc' => 'application/msword', |
| 1235: | 'docx' => 'application/msword', |
| 1236: | 'rtf' => 'application/rtf', |
| 1237: | 'xls' => 'application/vnd.ms-excel', |
| 1238: | 'ppt' => 'application/vnd.ms-powerpoint', |
| 1239: | |
| 1240: | |
| 1241: | 'odt' => 'application/vnd.oasis.opendocument.text', |
| 1242: | 'ods' => 'application/vnd.oasis.opendocument.spreadsheet' |
| 1243: | |
| 1244: | ); |
| 1245: | } |
| 1246: | |
| 1247: | |
| 1248: | |
| 1249: | |
| 1250: | |
| 1251: | public static function MimeContentType($sFileName) |
| 1252: | { |
| 1253: | $sResult = 'application/octet-stream'; |
| 1254: | |
| 1255: | $aMimeTypes = self::GetMimeContentTypes(); |
| 1256: | $sExt = strtolower(self::GetFileExtension($sFileName)); |
| 1257: | if (!empty($sExt) && isset($aMimeTypes[$sExt])) { |
| 1258: | $sResult = $aMimeTypes[$sExt]; |
| 1259: | } |
| 1260: | |
| 1261: | return $sResult; |
| 1262: | } |
| 1263: | |
| 1264: | |
| 1265: | |
| 1266: | |
| 1267: | |
| 1268: | public static function GetFileExtensionFromMimeContentType($sMimeContentType) |
| 1269: | { |
| 1270: | $aMimeTypes = self::GetMimeContentTypes(); |
| 1271: | return array_search($sMimeContentType, $aMimeTypes); |
| 1272: | } |
| 1273: | |
| 1274: | |
| 1275: | |
| 1276: | |
| 1277: | |
| 1278: | public static function ConvertLanguageNameToShort($sLanguage) |
| 1279: | { |
| 1280: | $aList = array( |
| 1281: | 'arabic' => 'ar', |
| 1282: | 'bulgarian' => 'bg', |
| 1283: | 'chinese-traditional' => 'zh-tw', |
| 1284: | 'chinese-simplified' => 'zh-cn', |
| 1285: | 'czech' => 'cs', |
| 1286: | 'danish' => 'da', |
| 1287: | 'dutch' => 'nl', |
| 1288: | 'english' => 'en', |
| 1289: | 'estonian' => 'et', |
| 1290: | 'finnish' => 'fi', |
| 1291: | 'french' => 'fr', |
| 1292: | 'german' => 'de', |
| 1293: | 'greek' => 'el', |
| 1294: | 'hebrew' => 'he', |
| 1295: | 'hungarian' => 'hu', |
| 1296: | 'italian' => 'it', |
| 1297: | 'japanese' => 'ja', |
| 1298: | 'korean' => 'ko', |
| 1299: | 'latvian' => 'lv', |
| 1300: | 'lithuanian' => 'lt', |
| 1301: | 'norwegian' => 'nb', |
| 1302: | 'persian' => 'fa', |
| 1303: | 'polish' => 'pl', |
| 1304: | 'portuguese-brazil' => 'pt-br', |
| 1305: | 'portuguese-portuguese' => 'pt', |
| 1306: | 'romanian' => 'ro', |
| 1307: | 'russian' => 'ru', |
| 1308: | 'serbian' => 'sr', |
| 1309: | 'slovenian' => 'sl', |
| 1310: | 'spanish' => 'es', |
| 1311: | 'swedish' => 'sv', |
| 1312: | 'thai' => 'th', |
| 1313: | 'turkish' => 'tr', |
| 1314: | 'ukrainian' => 'uk', |
| 1315: | 'vietnamese' => 'vi' |
| 1316: | ); |
| 1317: | |
| 1318: | $sLanguage = strtolower($sLanguage); |
| 1319: | return isset($aList[$sLanguage]) ? $aList[$sLanguage] : 'en'; |
| 1320: | } |
| 1321: | |
| 1322: | |
| 1323: | |
| 1324: | |
| 1325: | |
| 1326: | public static function GetGoodBigInt($iBigInt) |
| 1327: | { |
| 1328: | if (null === $iBigInt || false == $iBigInt) { |
| 1329: | return 0; |
| 1330: | } elseif ($iBigInt > AU_API_PHP_INT_MAX) { |
| 1331: | return AU_API_PHP_INT_MAX; |
| 1332: | } elseif ($iBigInt < AU_API_PHP_INT_MIN) { |
| 1333: | return AU_API_PHP_INT_MIN; |
| 1334: | } |
| 1335: | |
| 1336: | return (int) $iBigInt; |
| 1337: | } |
| 1338: | |
| 1339: | |
| 1340: | |
| 1341: | |
| 1342: | |
| 1343: | |
| 1344: | public static function Utf8Truncate($sUtf8String, $iLength) |
| 1345: | { |
| 1346: | if (strlen($sUtf8String) <= $iLength) { |
| 1347: | return $sUtf8String; |
| 1348: | } |
| 1349: | |
| 1350: | while ($iLength >= 0) { |
| 1351: | if ((ord($sUtf8String[$iLength]) < 0x80) || (ord($sUtf8String[$iLength]) >= 0xC0)) { |
| 1352: | return substr($sUtf8String, 0, $iLength); |
| 1353: | } |
| 1354: | |
| 1355: | $iLength--; |
| 1356: | } |
| 1357: | |
| 1358: | return ''; |
| 1359: | } |
| 1360: | |
| 1361: | |
| 1362: | |
| 1363: | |
| 1364: | |
| 1365: | public static function Utf8ToLowerCase($sUtf8String) |
| 1366: | { |
| 1367: | if (function_exists('mb_strtolower')) { |
| 1368: | return mb_strtolower($sUtf8String, 'utf-8'); |
| 1369: | } |
| 1370: | |
| 1371: | $aMapping = array( |
| 1372: | "\xC3\x80" => "\xC3\xA0", "\xC3\x81" => "\xC3\xA1", |
| 1373: | "\xC3\x82" => "\xC3\xA2", "\xC3\x83" => "\xC3\xA3", "\xC3\x84" => "\xC3\xA4", "\xC3\x85" => "\xC3\xA5", |
| 1374: | "\xC3\x86" => "\xC3\xA6", "\xC3\x87" => "\xC3\xA7", "\xC3\x88" => "\xC3\xA8", "\xC3\x89" => "\xC3\xA9", |
| 1375: | "\xC3\x8A" => "\xC3\xAA", "\xC3\x8B" => "\xC3\xAB", "\xC3\x8C" => "\xC3\xAC", "\xC3\x8D" => "\xC3\xAD", |
| 1376: | "\xC3\x8E" => "\xC3\xAE", "\xC3\x8F" => "\xC3\xAF", "\xC3\x90" => "\xC3\xB0", "\xC3\x91" => "\xC3\xB1", |
| 1377: | "\xC3\x92" => "\xC3\xB2", "\xC3\x93" => "\xC3\xB3", "\xC3\x94" => "\xC3\xB4", "\xC3\x95" => "\xC3\xB5", |
| 1378: | "\xC3\x96" => "\xC3\xB6", "\xC3\x98" => "\xC3\xB8", "\xC3\x99" => "\xC3\xB9", "\xC3\x9A" => "\xC3\xBA", |
| 1379: | "\xC3\x9B" => "\xC3\xBB", "\xC3\x9C" => "\xC3\xBC", "\xC3\x9D" => "\xC3\xBD", "\xC3\x9E" => "\xC3\xBE", |
| 1380: | "\xC4\x80" => "\xC4\x81", "\xC4\x82" => "\xC4\x83", "\xC4\x84" => "\xC4\x85", "\xC4\x86" => "\xC4\x87", |
| 1381: | "\xC4\x88" => "\xC4\x89", "\xC4\x8A" => "\xC4\x8B", "\xC4\x8C" => "\xC4\x8D", "\xC4\x8E" => "\xC4\x8F", |
| 1382: | "\xC4\x90" => "\xC4\x91", "\xC4\x92" => "\xC4\x93", "\xC4\x96" => "\xC4\x97", "\xC4\x98" => "\xC4\x99", |
| 1383: | "\xC4\x9A" => "\xC4\x9B", "\xC4\x9C" => "\xC4\x9D", "\xC4\x9E" => "\xC4\x9F", "\xC4\xA0" => "\xC4\xA1", |
| 1384: | "\xC4\xA2" => "\xC4\xA3", "\xC4\xA4" => "\xC4\xA5", "\xC4\xA6" => "\xC4\xA7", "\xC4\xA8" => "\xC4\xA9", |
| 1385: | "\xC4\xAA" => "\xC4\xAB", "\xC4\xAE" => "\xC4\xAF", "\xC4\xB4" => "\xC4\xB5", "\xC4\xB6" => "\xC4\xB7", |
| 1386: | "\xC4\xB9" => "\xC4\xBA", "\xC4\xBB" => "\xC4\xBC", "\xC4\xBD" => "\xC4\xBE", "\xC5\x81" => "\xC5\x82", |
| 1387: | "\xC5\x83" => "\xC5\x84", "\xC5\x85" => "\xC5\x86", "\xC5\x87" => "\xC5\x88", "\xC5\x8A" => "\xC5\x8B", |
| 1388: | "\xC5\x8C" => "\xC5\x8D", "\xC5\x90" => "\xC5\x91", "\xC5\x94" => "\xC5\x95", "\xC5\x96" => "\xC5\x97", |
| 1389: | "\xC5\x98" => "\xC5\x99", "\xC5\x9A" => "\xC5\x9B", "\xC5\x9C" => "\xC5\x9D", "\xC5\x9E" => "\xC5\x9F", |
| 1390: | "\xC5\xA0" => "\xC5\xA1", "\xC5\xA2" => "\xC5\xA3", "\xC5\xA4" => "\xC5\xA5", "\xC5\xA6" => "\xC5\xA7", |
| 1391: | "\xC5\xA8" => "\xC5\xA9", "\xC5\xAA" => "\xC5\xAB", "\xC5\xAC" => "\xC5\xAD", "\xC5\xAE" => "\xC5\xAF", |
| 1392: | "\xC5\xB0" => "\xC5\xB1", "\xC5\xB2" => "\xC5\xB3", "\xC5\xB4" => "\xC5\xB5", "\xC5\xB6" => "\xC5\xB7", |
| 1393: | "\xC5\xB8" => "\xC3\xBF", "\xC5\xB9" => "\xC5\xBA", "\xC5\xBB" => "\xC5\xBC", "\xC5\xBD" => "\xC5\xBE", |
| 1394: | "\xC6\xA0" => "\xC6\xA1", "\xC6\xAF" => "\xC6\xB0", "\xC8\x98" => "\xC8\x99", "\xC8\x9A" => "\xC8\x9B", |
| 1395: | "\xCE\x86" => "\xCE\xAC", "\xCE\x88" => "\xCE\xAD", "\xCE\x89" => "\xCE\xAE", "\xCE\x8A" => "\xCE\xAF", |
| 1396: | "\xCE\x8C" => "\xCF\x8C", "\xCE\x8E" => "\xCF\x8D", "\xCE\x8F" => "\xCF\x8E", "\xCE\x91" => "\xCE\xB1", |
| 1397: | "\xCE\x92" => "\xCE\xB2", "\xCE\x93" => "\xCE\xB3", "\xCE\x94" => "\xCE\xB4", "\xCE\x95" => "\xCE\xB5", |
| 1398: | "\xCE\x96" => "\xCE\xB6", "\xCE\x97" => "\xCE\xB7", "\xCE\x98" => "\xCE\xB8", "\xCE\x99" => "\xCE\xB9", |
| 1399: | "\xCE\x9A" => "\xCE\xBA", "\xCE\x9B" => "\xCE\xBB", "\xCE\x9C" => "\xCE\xBC", "\xCE\x9D" => "\xCE\xBD", |
| 1400: | "\xCE\x9E" => "\xCE\xBE", "\xCE\x9F" => "\xCE\xBF", "\xCE\xA0" => "\xCF\x80", "\xCE\xA1" => "\xCF\x81", |
| 1401: | "\xCE\xA3" => "\xCF\x83", "\xCE\xA4" => "\xCF\x84", "\xCE\xA5" => "\xCF\x85", "\xCE\xA6" => "\xCF\x86", |
| 1402: | "\xCE\xA7" => "\xCF\x87", "\xCE\xA8" => "\xCF\x88", "\xCE\xA9" => "\xCF\x89", "\xCE\xAA" => "\xCF\x8A", |
| 1403: | "\xCE\xAB" => "\xCF\x8B", "\xD0\x81" => "\xD1\x91", "\xD0\x82" => "\xD1\x92", "\xD0\x83" => "\xD1\x93", |
| 1404: | "\xD0\x84" => "\xD1\x94", "\xD0\x85" => "\xD1\x95", "\xD0\x86" => "\xD1\x96", "\xD0\x87" => "\xD1\x97", |
| 1405: | "\xD0\x88" => "\xD1\x98", "\xD0\x89" => "\xD1\x99", "\xD0\x8A" => "\xD1\x9A", "\xD0\x8B" => "\xD1\x9B", |
| 1406: | "\xD0\x8C" => "\xD1\x9C", "\xD0\x8E" => "\xD1\x9E", "\xD0\x8F" => "\xD1\x9F", "\xD0\x90" => "\xD0\xB0", |
| 1407: | "\xD0\x91" => "\xD0\xB1", "\xD0\x92" => "\xD0\xB2", "\xD0\x93" => "\xD0\xB3", "\xD0\x94" => "\xD0\xB4", |
| 1408: | "\xD0\x95" => "\xD0\xB5", "\xD0\x96" => "\xD0\xB6", "\xD0\x97" => "\xD0\xB7", "\xD0\x98" => "\xD0\xB8", |
| 1409: | "\xD0\x99" => "\xD0\xB9", "\xD0\x9A" => "\xD0\xBA", "\xD0\x9B" => "\xD0\xBB", "\xD0\x9C" => "\xD0\xBC", |
| 1410: | "\xD0\x9D" => "\xD0\xBD", "\xD0\x9E" => "\xD0\xBE", "\xD0\x9F" => "\xD0\xBF", "\xD0\xA0" => "\xD1\x80", |
| 1411: | "\xD0\xA1" => "\xD1\x81", "\xD0\xA2" => "\xD1\x82", "\xD0\xA3" => "\xD1\x83", "\xD0\xA4" => "\xD1\x84", |
| 1412: | "\xD0\xA5" => "\xD1\x85", "\xD0\xA6" => "\xD1\x86", "\xD0\xA7" => "\xD1\x87", "\xD0\xA8" => "\xD1\x88", |
| 1413: | "\xD0\xA9" => "\xD1\x89", "\xD0\xAA" => "\xD1\x8A", "\xD0\xAB" => "\xD1\x8B", "\xD0\xAC" => "\xD1\x8C", |
| 1414: | "\xD0\xAD" => "\xD1\x8D", "\xD0\xAE" => "\xD1\x8E", "\xD0\xAF" => "\xD1\x8F", "\xD2\x90" => "\xD2\x91", |
| 1415: | "\xE1\xB8\x82" => "\xE1\xB8\x83", "\xE1\xB8\x8A" => "\xE1\xB8\x8B", "\xE1\xB8\x9E" => "\xE1\xB8\x9F", "\xE1\xB9\x80" => "\xE1\xB9\x81", |
| 1416: | "\xE1\xB9\x96" => "\xE1\xB9\x97", "\xE1\xB9\xA0" => "\xE1\xB9\xA1", "\xE1\xB9\xAA" => "\xE1\xB9\xAB", "\xE1\xBA\x80" => "\xE1\xBA\x81", |
| 1417: | "\xE1\xBA\x82" => "\xE1\xBA\x83", "\xE1\xBA\x84" => "\xE1\xBA\x85", "\xE1\xBB\xB2" => "\xE1\xBB\xB3" |
| 1418: | ); |
| 1419: | |
| 1420: | return strtr(strtolower($sUtf8String), $aMapping); |
| 1421: | } |
| 1422: | |
| 1423: | |
| 1424: | |
| 1425: | |
| 1426: | |
| 1427: | public static function LdapUriParse($sPabUri) |
| 1428: | { |
| 1429: | $aReturn = array( |
| 1430: | 'host' => '127.0.0.1', |
| 1431: | 'port' => 389, |
| 1432: | 'search_dn' => '', |
| 1433: | ); |
| 1434: | |
| 1435: | $sPabUriLower = strtolower($sPabUri); |
| 1436: | if ('ldap://' === substr($sPabUriLower, 0, 7)) { |
| 1437: | $sPabUriLower = substr($sPabUriLower, 7); |
| 1438: | } |
| 1439: | |
| 1440: | $aPabUriLowerExplode = explode('/', $sPabUriLower, 2); |
| 1441: | $aReturn['search_dn'] = isset($aPabUriLowerExplode[1]) ? $aPabUriLowerExplode[1] : ''; |
| 1442: | |
| 1443: | if (isset($aPabUriLowerExplode[0])) { |
| 1444: | $aPabUriLowerHostPortExplode = explode(':', $aPabUriLowerExplode[0], 2); |
| 1445: | $aReturn['host'] = isset($aPabUriLowerHostPortExplode[0]) ? $aPabUriLowerHostPortExplode[0] : $aReturn['host']; |
| 1446: | $aReturn['port'] = isset($aPabUriLowerHostPortExplode[1]) ? (int) $aPabUriLowerHostPortExplode[1] : $aReturn['port']; |
| 1447: | } |
| 1448: | |
| 1449: | return $aReturn; |
| 1450: | } |
| 1451: | |
| 1452: | |
| 1453: | |
| 1454: | |
| 1455: | public static function RequestUri() |
| 1456: | { |
| 1457: | $sUri = ''; |
| 1458: | if (isset($_SERVER['REQUEST_URI'])) { |
| 1459: | $sUri = $_SERVER['REQUEST_URI']; |
| 1460: | } else { |
| 1461: | if (isset($_SERVER['SCRIPT_NAME'])) { |
| 1462: | if (isset($_SERVER['argv'], $_SERVER['argv'][0])) { |
| 1463: | $sUri = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['argv'][0]; |
| 1464: | } elseif (isset($_SERVER['QUERY_STRING'])) { |
| 1465: | $sUri = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING']; |
| 1466: | } else { |
| 1467: | $sUri = $_SERVER['SCRIPT_NAME']; |
| 1468: | } |
| 1469: | } |
| 1470: | } |
| 1471: | |
| 1472: | $sUri = '/'. ltrim($sUri, '/'); |
| 1473: | return $sUri; |
| 1474: | } |
| 1475: | |
| 1476: | public static function DirMtime($dir) |
| 1477: | { |
| 1478: | $last_modified = 0; |
| 1479: | $files = glob($dir.'/*'); |
| 1480: | foreach ($files as $file) { |
| 1481: | if (is_dir($file)) { |
| 1482: | $modified = self::DirMtime($file); |
| 1483: | } else { |
| 1484: | $modified = filemtime($file); |
| 1485: | } |
| 1486: | if ($modified > $last_modified) { |
| 1487: | $last_modified = $modified; |
| 1488: | } |
| 1489: | } |
| 1490: | return $last_modified; |
| 1491: | } |
| 1492: | |
| 1493: | public static function GlobRecursive($pattern, $flags = 0) |
| 1494: | { |
| 1495: | $files = glob($pattern, $flags); |
| 1496: | |
| 1497: | $aPaternFiles = glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT); |
| 1498: | |
| 1499: | if (is_array($aPaternFiles) && is_array($files)) { |
| 1500: | foreach ($aPaternFiles as $dir) { |
| 1501: | $files = array_merge($files, self::GlobRecursive($dir.'/'.basename($pattern), $flags)); |
| 1502: | } |
| 1503: | } |
| 1504: | |
| 1505: | return is_array($files) ? $files : array(); |
| 1506: | } |
| 1507: | |
| 1508: | |
| 1509: | |
| 1510: | |
| 1511: | |
| 1512: | public static function ClearPhone($sPhone) |
| 1513: | { |
| 1514: | return preg_replace('/^0000000000/', '+', preg_replace( |
| 1515: | '/[^\d]+/', |
| 1516: | '', |
| 1517: | preg_replace('/^[+]/', '0000000000', trim($sPhone)) |
| 1518: | )); |
| 1519: | } |
| 1520: | |
| 1521: | |
| 1522: | |
| 1523: | |
| 1524: | |
| 1525: | public static function ClearPhoneSearch($sSearch) |
| 1526: | { |
| 1527: | return preg_replace('/[\s\-()]+/', '', preg_replace('/^[+]/', '', trim($sSearch))); |
| 1528: | } |
| 1529: | |
| 1530: | |
| 1531: | |
| 1532: | |
| 1533: | |
| 1534: | public static function IsPhoneSearch($sSearch) |
| 1535: | { |
| 1536: | return (bool) preg_match('/^[\d]{3,}$/', self::ClearPhoneSearch($sSearch)); |
| 1537: | } |
| 1538: | |
| 1539: | |
| 1540: | |
| 1541: | |
| 1542: | |
| 1543: | |
| 1544: | public static function IsGDImageMimeTypeSuppoted($sMimeType, $sFileName = '') |
| 1545: | { |
| 1546: | $bResult = function_exists('gd_info'); |
| 1547: | if ($bResult) { |
| 1548: | $bResult = false; |
| 1549: | switch (strtolower($sMimeType)) { |
| 1550: | case 'image/jpg': |
| 1551: | case 'image/jpeg': |
| 1552: | $bResult = function_exists('imagecreatefromjpeg'); |
| 1553: | break; |
| 1554: | case 'image/png': |
| 1555: | $bResult = function_exists('imagecreatefrompng'); |
| 1556: | break; |
| 1557: | case 'image/gif': |
| 1558: | $bResult = function_exists('imagecreatefromgif'); |
| 1559: | break; |
| 1560: | } |
| 1561: | } |
| 1562: | |
| 1563: | return $bResult; |
| 1564: | } |
| 1565: | |
| 1566: | public static function GetDirectorySize($path) |
| 1567: | { |
| 1568: | $size = 0; |
| 1569: | $files = 0; |
| 1570: | $directories = 0; |
| 1571: | |
| 1572: | $handle = is_dir($path) ? opendir($path) : null; |
| 1573: | if ($handle) { |
| 1574: | while (false !== ($file = readdir($handle))) { |
| 1575: | $nextpath = $path . '/' . $file; |
| 1576: | if ($file != '.' && $file != '..' && !is_link($nextpath)) { |
| 1577: | if (is_dir($nextpath)) { |
| 1578: | $directories++; |
| 1579: | $result = self::GetDirectorySize($nextpath); |
| 1580: | $size += $result['size']; |
| 1581: | $files += $result['files']; |
| 1582: | $directories += $result['directories']; |
| 1583: | } elseif (is_file($nextpath)) { |
| 1584: | $size += filesize($nextpath); |
| 1585: | $files++; |
| 1586: | } |
| 1587: | } |
| 1588: | } |
| 1589: | |
| 1590: | if (is_resource($handle)) { |
| 1591: | closedir($handle); |
| 1592: | } |
| 1593: | } |
| 1594: | |
| 1595: | return array( |
| 1596: | 'size' => $size, |
| 1597: | 'files' => $files, |
| 1598: | 'directories' => $directories |
| 1599: | ); |
| 1600: | } |
| 1601: | |
| 1602: | public static function SearchFiles($sPath, $sPattern) |
| 1603: | { |
| 1604: | $files = array(); |
| 1605: | |
| 1606: | |
| 1607: | $oDirIterator = new \RecursiveDirectoryIterator( |
| 1608: | $sPath, |
| 1609: | \FilesystemIterator::SKIP_DOTS | |
| 1610: | \FilesystemIterator::UNIX_PATHS |
| 1611: | ); |
| 1612: | |
| 1613: | $oIterators = new \RecursiveIteratorIterator( |
| 1614: | $oDirIterator, |
| 1615: | \RecursiveIteratorIterator::SELF_FIRST |
| 1616: | ); |
| 1617: | |
| 1618: | if ($sPattern === "*") { |
| 1619: | $sPattern = "\w+"; |
| 1620: | } else { |
| 1621: | $sPattern = preg_quote($sPattern); |
| 1622: | } |
| 1623: | |
| 1624: | foreach ($oIterators as $oIterator) { |
| 1625: | $sName = $oIterator->getFilename(); |
| 1626: | $aMatches = array(); |
| 1627: | |
| 1628: | $iResult = preg_match("/" . $sPattern . "/ui", $sName, $aMatches); |
| 1629: | if ($sName !== '.sabredav' && $sName !== AU_API_HELPDESK_PUBLIC_NAME && $iResult === 1) { |
| 1630: | $files[] = $oIterator->getPathname(); |
| 1631: | } |
| 1632: | } |
| 1633: | |
| 1634: | return $files; |
| 1635: | } |
| 1636: | |
| 1637: | public static function GetRemoteFileInfo($sUrl) |
| 1638: | { |
| 1639: | $aResult = array( |
| 1640: | 'size' => 0, |
| 1641: | 'content-type' => '', |
| 1642: | 'code' => 0 |
| 1643: | ); |
| 1644: | |
| 1645: | $oCurl = \curl_init(); |
| 1646: | \curl_setopt_array($oCurl, array( |
| 1647: | CURLOPT_URL => $sUrl, |
| 1648: | CURLOPT_HEADER => true, |
| 1649: | CURLOPT_SSL_VERIFYPEER => false, |
| 1650: | CURLOPT_RETURNTRANSFER => true, |
| 1651: | CURLOPT_NOBODY => true |
| 1652: | )); |
| 1653: | |
| 1654: | \curl_exec($oCurl); |
| 1655: | $aInfo = \curl_getinfo($oCurl); |
| 1656: | |
| 1657: | if ($aInfo) { |
| 1658: | $sContentType = ''; |
| 1659: | $aResult['size'] = isset($aInfo['download_content_length']) ? (int) $aInfo['download_content_length'] : 0; |
| 1660: | |
| 1661: | if (isset($aInfo['content_type'])) { |
| 1662: | $aContentType = explode(';', $aInfo['content_type']); |
| 1663: | $sContentType = isset($aContentType[0]) ? $aContentType[0] : ''; |
| 1664: | } |
| 1665: | |
| 1666: | $aResult['code'] = $aInfo['http_code']; |
| 1667: | $aResult['content-type'] = $sContentType; |
| 1668: | } |
| 1669: | |
| 1670: | if (\is_resource($oCurl)) { |
| 1671: | \curl_close($oCurl); |
| 1672: | } |
| 1673: | |
| 1674: | return $aResult; |
| 1675: | } |
| 1676: | |
| 1677: | |
| 1678: | |
| 1679: | |
| 1680: | |
| 1681: | |
| 1682: | public static function GetRemoteFileRealUrl($sUrl, $iStep = 1) |
| 1683: | { |
| 1684: | $oCurl = curl_init(); |
| 1685: | \curl_setopt_array($oCurl, array( |
| 1686: | CURLOPT_URL => $sUrl, |
| 1687: | CURLOPT_HEADER => true, |
| 1688: | CURLOPT_SSL_VERIFYPEER => false, |
| 1689: | CURLOPT_RETURNTRANSFER => true, |
| 1690: | CURLOPT_NOBODY => true |
| 1691: | )); |
| 1692: | curl_exec($oCurl); |
| 1693: | |
| 1694: | $aInfo = \curl_getinfo($oCurl); |
| 1695: | $iCode = \curl_getinfo($oCurl, CURLINFO_HTTP_CODE); |
| 1696: | |
| 1697: | curl_close($oCurl); |
| 1698: | |
| 1699: | if (($iCode === 301 || $iCode === 302) && isset($aInfo['redirect_url']) && $aInfo['redirect_url'] !== '' && $iStep < 2) { |
| 1700: | return self::GetRemoteFileRealUrl($aInfo['redirect_url'], ++$iStep); |
| 1701: | } |
| 1702: | |
| 1703: | return ($iCode === 200 || $iCode === 0 || $iCode === 400) ? $sUrl : false; |
| 1704: | } |
| 1705: | |
| 1706: | public static function PopulateGoogleDriveFileInfo(&$oFileInfo) |
| 1707: | { |
| 1708: | if ($oFileInfo->mimeType !== "application/vnd.google-apps.folder" && !isset($oFileInfo->downloadUrl)) { |
| 1709: | switch($oFileInfo->mimeType) { |
| 1710: | case 'application/vnd.google-apps.document': |
| 1711: | if (is_array($oFileInfo->exportLinks)) { |
| 1712: | $oFileInfo->downloadUrl = $oFileInfo->exportLinks['application/vnd.openxmlformats-officedocument.wordprocessingml.document']; |
| 1713: | } else { |
| 1714: | $oFileInfo->downloadUrl = $oFileInfo->exportLinks->{'application/vnd.openxmlformats-officedocument.wordprocessingml.document'}; |
| 1715: | } |
| 1716: | $oFileInfo->title = $oFileInfo->title . '.docx'; |
| 1717: | break; |
| 1718: | case 'application/vnd.google-apps.spreadsheet': |
| 1719: | if (is_array($oFileInfo->exportLinks)) { |
| 1720: | $oFileInfo->downloadUrl = $oFileInfo->exportLinks['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']; |
| 1721: | } else { |
| 1722: | $oFileInfo->downloadUrl = $oFileInfo->exportLinks->{'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}; |
| 1723: | } |
| 1724: | $oFileInfo->title = $oFileInfo->title . '.xlsx'; |
| 1725: | break; |
| 1726: | case 'application/vnd.google-apps.drawing': |
| 1727: | if (is_array($oFileInfo->exportLinks)) { |
| 1728: | $oFileInfo->downloadUrl = $oFileInfo->exportLinks['image/png']; |
| 1729: | } else { |
| 1730: | $oFileInfo->downloadUrl = $oFileInfo->exportLinks->{'image/png'}; |
| 1731: | } |
| 1732: | $oFileInfo->title = $oFileInfo->title . '.png'; |
| 1733: | break; |
| 1734: | case 'application/vnd.google-apps.presentation': |
| 1735: | if (is_array($oFileInfo->exportLinks)) { |
| 1736: | $oFileInfo->downloadUrl = $oFileInfo->exportLinks['application/vnd.openxmlformats-officedocument.presentationml.presentation']; |
| 1737: | } else { |
| 1738: | $oFileInfo->downloadUrl = $oFileInfo->exportLinks->{'application/vnd.openxmlformats-officedocument.presentationml.presentation'}; |
| 1739: | } |
| 1740: | $oFileInfo->title = $oFileInfo->title . '.pptx'; |
| 1741: | break; |
| 1742: | |
| 1743: | |
| 1744: | |
| 1745: | |
| 1746: | } |
| 1747: | |
| 1748: | |
| 1749: | |
| 1750: | |
| 1751: | |
| 1752: | |
| 1753: | |
| 1754: | |
| 1755: | } |
| 1756: | } |
| 1757: | |
| 1758: | public static function GetAppUrl() |
| 1759: | { |
| 1760: | $aUrlParts = parse_url($_SERVER['HTTP_REFERER']); |
| 1761: | |
| 1762: | $sProtocol = !empty($aUrlParts['scheme']) ? $aUrlParts['scheme'] : 'http'; |
| 1763: | $sHost = !empty($aUrlParts['host']) ? $aUrlParts['host'] : 'localhost'; |
| 1764: | $sPath = !empty($aUrlParts['path']) ? str_replace('index.php', '', $aUrlParts['path']) : ''; |
| 1765: | $sPort = !empty($aUrlParts['port']) ? ':'.$aUrlParts['port'] : ''; |
| 1766: | |
| 1767: | return $sProtocol.'://'.$sHost.$sPath.$sPort; |
| 1768: | } |
| 1769: | |
| 1770: | |
| 1771: | |
| 1772: | |
| 1773: | |
| 1774: | public static function ExplodeIntUids($sValue) |
| 1775: | { |
| 1776: | $aValue = explode(',', (string) $sValue); |
| 1777: | $aValue = array_map('trim', $aValue); |
| 1778: | $aValue = array_map('intval', $aValue); |
| 1779: | |
| 1780: | $aValue = array_filter($aValue, function ($iValue) { |
| 1781: | return 0 < $iValue; |
| 1782: | }); |
| 1783: | |
| 1784: | return $aValue; |
| 1785: | } |
| 1786: | |
| 1787: | public static function parseIniString($sIniString) |
| 1788: | { |
| 1789: | $aResult = array(); |
| 1790: | foreach (explode("\n", $sIniString) as $sLine) { |
| 1791: | $aValues = explode("=", $sLine, 2); |
| 1792: | if (isset($aValues[0], $aValues[1])) { |
| 1793: | $aResult[$aValues[0]] = trim(rtrim($aValues[1], "\r"), "\""); |
| 1794: | } |
| 1795: | } |
| 1796: | return $aResult; |
| 1797: | } |
| 1798: | |
| 1799: | |
| 1800: | |
| 1801: | |
| 1802: | |
| 1803: | |
| 1804: | |
| 1805: | |
| 1806: | |
| 1807: | public static function ConvertCssToInlineStyles($sHtml, $sEncoding = 'utf-8') |
| 1808: | { |
| 1809: | $sResult = ''; |
| 1810: | |
| 1811: | if (is_string($sHtml)) { |
| 1812: | $oCssToInlineStyles = new \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles($sHtml); |
| 1813: | $oCssToInlineStyles->setEncoding($sEncoding); |
| 1814: | $oCssToInlineStyles->setUseInlineStylesBlock(true); |
| 1815: | $sResult = $oCssToInlineStyles->convert(); |
| 1816: | } |
| 1817: | |
| 1818: | return $sResult; |
| 1819: | } |
| 1820: | |
| 1821: | |
| 1822: | |
| 1823: | |
| 1824: | |
| 1825: | |
| 1826: | |
| 1827: | |
| 1828: | public static function OutputFileHeaders($bDownload, $sContentType, $sFileName) |
| 1829: | { |
| 1830: | if ($bDownload) { |
| 1831: | \header('Content-Type: '.$sContentType, true); |
| 1832: | } else { |
| 1833: | $aParts = \explode('/', $sContentType, 2); |
| 1834: | if (\in_array(\strtolower($aParts[0]), array('image', 'video', 'audio')) || |
| 1835: | \in_array(\strtolower($sContentType), array('application/pdf', 'application/x-pdf', 'text/html'))) { |
| 1836: | \header('Content-Type: '.$sContentType, true); |
| 1837: | } elseif (\strtolower($sContentType) === 'application/octet-stream') { |
| 1838: | $sContentType = \MailSo\Base\Utils::MimeContentType($sFileName); |
| 1839: | \header('Content-Type: '.$sContentType, true); |
| 1840: | } else { |
| 1841: | \header('Content-Type: text/plain', true); |
| 1842: | } |
| 1843: | } |
| 1844: | |
| 1845: | \header('Content-Disposition: '.($bDownload ? 'attachment' : 'inline').'; '. |
| 1846: | \trim(\MailSo\Base\Utils::EncodeHeaderUtf8AttributeValue('filename', $sFileName)), true); |
| 1847: | |
| 1848: | \header('Accept-Ranges: none', true); |
| 1849: | \header('Content-Transfer-Encoding: binary'); |
| 1850: | } |
| 1851: | |
| 1852: | public static function OutputFileResource($iUserId, $sContentType, $sFileName, $rResource, $bThumbnail, $bDownload) |
| 1853: | { |
| 1854: | self::OutputFileHeaders($bDownload, $sContentType, $sFileName); |
| 1855: | |
| 1856: | if (!$bDownload && 'text/html' === $sContentType) { |
| 1857: | $sHtml = \stream_get_contents($rResource); |
| 1858: | if ($sHtml) { |
| 1859: | $sCharset = ''; |
| 1860: | $aMacth = array(); |
| 1861: | if (\preg_match('/charset[\s]?=[\s]?([^\s"\']+)/i', $sHtml, $aMacth) && !empty($aMacth[1])) { |
| 1862: | $sCharset = $aMacth[1]; |
| 1863: | } |
| 1864: | |
| 1865: | if ('' !== $sCharset && \MailSo\Base\Enumerations\Charset::UTF_8 !== $sCharset) { |
| 1866: | $sHtml = \MailSo\Base\Utils::ConvertEncoding( |
| 1867: | $sHtml, |
| 1868: | \MailSo\Base\Utils::NormalizeCharset($sCharset, true), |
| 1869: | \MailSo\Base\Enumerations\Charset::UTF_8 |
| 1870: | ); |
| 1871: | } |
| 1872: | |
| 1873: | echo '<html><head></head><body>'. |
| 1874: | \MailSo\Base\HtmlUtils::ClearHtmlSimple(self::ConvertCssToInlineStyles($sHtml), true). |
| 1875: | '</body></html>'; |
| 1876: | } |
| 1877: | } else { |
| 1878: | if ($bThumbnail && !$bDownload) { |
| 1879: | Managers\Thumb::GetResource($iUserId, $rResource, $sFileName, true); |
| 1880: | } else { |
| 1881: | \MailSo\Base\Utils::FpassthruWithTimeLimitReset($rResource); |
| 1882: | } |
| 1883: | } |
| 1884: | } |
| 1885: | |
| 1886: | public static function GetClientFileResponse($sModule, $iUserId, $sFileName, $sTempName, $iSize) |
| 1887: | { |
| 1888: | $sMimeType = \MailSo\Base\Utils::MimeContentType($sFileName); |
| 1889: | $sModule = !empty($sModule) ? $sModule : 'System'; |
| 1890: | |
| 1891: | $sHash = Api::EncodeKeyValues(array( |
| 1892: | 'Module' => $sModule, |
| 1893: | 'TempFile' => true, |
| 1894: | 'UserId' => $iUserId, |
| 1895: | 'Name' => $sFileName, |
| 1896: | 'TempName' => $sTempName |
| 1897: | )); |
| 1898: | $aActions = array( |
| 1899: | 'view' => array( |
| 1900: | 'url' => '?file-cache/' . $sHash .'/view' |
| 1901: | ), |
| 1902: | 'download' => array( |
| 1903: | 'url' => '?file-cache/' . $sHash |
| 1904: | ) |
| 1905: | ); |
| 1906: | $oSettings =& Api::GetSettings(); |
| 1907: | $iThumbnailLimit = ((int) $oSettings->GetValue('ThumbnailMaxFileSizeMb', 5)) * 1024 * 1024; |
| 1908: | $bThumb = ($oSettings->GetValue('AllowThumbnail', true) && |
| 1909: | $iSize < $iThumbnailLimit && Utils::IsGDImageMimeTypeSuppoted($sMimeType, $sFileName)); |
| 1910: | return array( |
| 1911: | 'Name' => $sFileName, |
| 1912: | 'FileName' => $sFileName, |
| 1913: | 'TempName' => $sTempName, |
| 1914: | 'MimeType' => $sMimeType, |
| 1915: | 'Size' => (int) $iSize, |
| 1916: | 'Hash' => $sHash, |
| 1917: | 'Actions' => $aActions, |
| 1918: | 'ThumbnailUrl' => $bThumb ? '?file-cache/' . $sHash .'/thumb' : '', |
| 1919: | ); |
| 1920: | } |
| 1921: | |
| 1922: | |
| 1923: | |
| 1924: | |
| 1925: | |
| 1926: | |
| 1927: | |
| 1928: | |
| 1929: | public static function clearFileName($sFileName, $sContentType, $sMimeIndex = '') |
| 1930: | { |
| 1931: | $sFileName = 0 === \strlen($sFileName) ? \preg_replace('/[^a-zA-Z0-9]/', '.', (empty($sMimeIndex) ? '' : $sMimeIndex.'.').$sContentType) : $sFileName; |
| 1932: | $sClearedFileName = \preg_replace('/[\s]+/', ' ', \preg_replace('/[\.]+/', '.', $sFileName)); |
| 1933: | $sExt = \MailSo\Base\Utils::GetFileExtension($sClearedFileName); |
| 1934: | |
| 1935: | $iSize = 100; |
| 1936: | if ($iSize < \strlen($sClearedFileName) - \strlen($sExt)) { |
| 1937: | $sClearedFileName = \substr($sClearedFileName, 0, $iSize).(empty($sExt) ? '' : '.'.$sExt); |
| 1938: | } |
| 1939: | |
| 1940: | return \MailSo\Base\Utils::ClearFileName(\MailSo\Base\Utils::Utf8Clear($sClearedFileName)); |
| 1941: | } |
| 1942: | |
| 1943: | public static function getShortClassName($sClassName) |
| 1944: | { |
| 1945: | if ($mPos = \strrpos($sClassName, '\\')) { |
| 1946: | return \substr($sClassName, $mPos + 1); |
| 1947: | } |
| 1948: | return $sClassName; |
| 1949: | } |
| 1950: | |
| 1951: | public static function getSanitizedFilename($sFileName) |
| 1952: | { |
| 1953: | return preg_replace("/[\/\*\?\[^\]<>\|:]/i", "", $sFileName); |
| 1954: | } |
| 1955: | |
| 1956: | public static function getClientIp() |
| 1957: | { |
| 1958: | $ipaddress = ''; |
| 1959: | |
| 1960: | if (isset($_SERVER['HTTP_CLIENT_IP'])) { |
| 1961: | $ipaddress = $_SERVER['HTTP_CLIENT_IP']; |
| 1962: | } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
| 1963: | $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 1964: | } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) { |
| 1965: | $ipaddress = $_SERVER['HTTP_X_FORWARDED']; |
| 1966: | } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) { |
| 1967: | $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; |
| 1968: | } elseif (isset($_SERVER['HTTP_FORWARDED'])) { |
| 1969: | $ipaddress = $_SERVER['HTTP_FORWARDED']; |
| 1970: | } elseif (isset($_SERVER['REMOTE_ADDR'])) { |
| 1971: | $ipaddress = $_SERVER['REMOTE_ADDR']; |
| 1972: | } |
| 1973: | |
| 1974: | return $ipaddress; |
| 1975: | } |
| 1976: | } |
| 1977: | |
| 1978: | |
| 1979: | |
| 1980: | |
| 1981: | class Ints |
| 1982: | { |
| 1983: | |
| 1984: | |
| 1985: | |
| 1986: | public static function getIntMax() |
| 1987: | { |
| 1988: | $iMax = 0x7fff; |
| 1989: | $iProbe = 0x7fffffff; |
| 1990: | while ($iMax == ($iProbe >> 16)) { |
| 1991: | $iMax = $iProbe; |
| 1992: | $iProbe = ($iProbe << 16) + 0xffff; |
| 1993: | } |
| 1994: | return $iMax; |
| 1995: | } |
| 1996: | } |
| 1997: | |
| 1998: | function fNullCallback() |
| 1999: | { |
| 2000: | } |
| 2001: | |
| 2002: | defined('AU_API_PHP_INT_MAX') || define('AU_API_PHP_INT_MAX', (int) Ints::getIntMax()); |
| 2003: | defined('AU_API_PHP_INT_MIN') || define('AU_API_PHP_INT_MIN', (int) (AU_API_PHP_INT_MAX + 1)); |
| 2004: | |