PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/src/aCloud/system/core/ACloudSysCoreCommon.php

https://gitlab.com/wuhang2003/phpwind
PHP | 250 lines | 225 code | 25 blank | 0 comment | 58 complexity | 389e8ce38e9b3b562b8e7059b3c07f0b MD5 | raw file
  1. <?php
  2. ! defined ( 'ACLOUD_PATH' ) && exit ( 'Forbidden' );
  3. require_once Wind::getRealPath ( 'ACLOUD:system.core.ACloudSysCoreDefine' );
  4. require_once Wind::getRealPath ( 'ACLOUD:system.core.ACloudSysCoreS' );
  5. class ACloudSysCoreCommon {
  6. public static function getGlobal($key, $default = NULL) {
  7. return (isset ( $GLOBALS [$key] )) ? $GLOBALS [$key] : $default;
  8. }
  9. public static function setGlobal($key, $value) {
  10. $GLOBALS [$key] = $value;
  11. }
  12. public static function getSiteSign() {
  13. $sign = self::parseDomainName ( self::getSiteUnique () );
  14. return substr ( md5 ( $sign ), 8, 8 );
  15. }
  16. public static function getSiteUnique() {
  17. return isset ( $_SERVER ['SERVER_NAME'] ) ? trim ( $_SERVER ['SERVER_NAME'] ) : trim ( ACloudSysCoreDefine::ACLOUD_V );
  18. }
  19. public static function showError($message) {
  20. echo $message;
  21. exit ();
  22. }
  23. public static function randCode($length = 32) {
  24. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
  25. $chars_length = (strlen ( $chars ) - 1);
  26. $string = $chars {rand ( 0, $chars_length )};
  27. for($i = 1; $i < $length; $i = strlen ( $string )) {
  28. $r = $chars {rand ( 0, $chars_length )};
  29. if ($r != $string {$i - 1})
  30. $string .= $r;
  31. }
  32. return $string;
  33. }
  34. public static function getIp() {
  35. static $ip = null;
  36. if (! $ip) {
  37. if (isset ( $_SERVER ['HTTP_X_FORWARDED_FOR'] ) && $_SERVER ['HTTP_X_FORWARDED_FOR'] && $_SERVER ['REMOTE_ADDR']) {
  38. if (strstr ( $_SERVER ['HTTP_X_FORWARDED_FOR'], ',' )) {
  39. $x = explode ( ',', $_SERVER ['HTTP_X_FORWARDED_FOR'] );
  40. $_SERVER ['HTTP_X_FORWARDED_FOR'] = trim ( end ( $x ) );
  41. }
  42. if (preg_match ( '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER ['HTTP_X_FORWARDED_FOR'] )) {
  43. $ip = $_SERVER ['HTTP_X_FORWARDED_FOR'];
  44. }
  45. } elseif (isset ( $_SERVER ['HTTP_CLIENT_IP'] ) && $_SERVER ['HTTP_CLIENT_IP'] && preg_match ( '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER ['HTTP_CLIENT_IP'] )) {
  46. $ip = $_SERVER ['HTTP_CLIENT_IP'];
  47. }
  48. if (! $ip && preg_match ( '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER ['REMOTE_ADDR'] )) {
  49. $ip = $_SERVER ['REMOTE_ADDR'];
  50. }
  51. ! $ip && $ip = 'Unknown';
  52. }
  53. return $ip;
  54. }
  55. public static function getDirName($path = null) {
  56. if (! empty ( $path )) {
  57. if (strpos ( $path, '\\' ) !== false) {
  58. return substr ( $path, 0, strrpos ( $path, '\\' ) ) . '/';
  59. } elseif (strpos ( $path, '/' ) !== false) {
  60. return substr ( $path, 0, strrpos ( $path, '/' ) ) . '/';
  61. }
  62. }
  63. return './';
  64. }
  65. public static function parseDomainName($url) {
  66. return ($url) ? trim ( str_replace ( array ("http://", "https://" ), array ("" ), $url ), "/" ) : "";
  67. }
  68. public static function listDir($path) {
  69. static $result = array ();
  70. $path = rtrim ( $path, "/" ) . "/";
  71. $folder_handle = opendir ( $path );
  72. while ( false !== ($filename = readdir ( $folder_handle )) ) {
  73. if (strpos ( $filename, '.' ) !== 0) {
  74. if (is_dir ( $path . $filename . "/" )) {
  75. self::listDir ( $path . $filename . "/" );
  76. } else {
  77. $result [] = $path . $filename;
  78. }
  79. }
  80. }
  81. closedir ( $folder_handle );
  82. return $result;
  83. }
  84. public static function getSiteUserAgent() {
  85. list ( $key, $ua ) = array ('_ac_app_ua', '' );
  86. if (! $_COOKIE || ! isset ( $_COOKIE [$key] ) || ! $_COOKIE [$key]) {
  87. $ua = substr ( md5 ( $_SERVER ['HTTP_USER_AGENT'] . '\t' . rand ( 1000, 9999 ) . '\t' . time () ), 8, 18 );
  88. setcookie ( $key, $ua, time () + 86400 * 365 * 5 );
  89. }
  90. $ua = $ua ? $ua : $_COOKIE [$key];
  91. return (strlen ( $ua ) == 18) ? ACloudSysCoreS::escapeChar ( $ua ) : '';
  92. }
  93. public static function simpleResponse($code, $info = null) {
  94. $info = self::convert ( $info, 'UTF-8', self::getGlobal ( 'g_charset' ) );
  95. return self::jsonEncode ( array ('code' => $code, 'info' => $info ) );
  96. }
  97. public static function jsonEncode($data) {
  98. if (function_exists ( "json_encode" ))
  99. return json_encode ( $data );
  100. require_once Wind::getRealPath ( "ACLOUD:system.core.ACloudSysCoreJson" );
  101. $jsonClass = new Services_JSON ();
  102. return $jsonClass->encode ( $data );
  103. }
  104. public static function jsonDecode($data) {
  105. if (function_exists ( "json_decode" ))
  106. return json_decode ( $data );
  107. require_once Wind::getRealPath ( "ACLOUD:system.core.ACloudSysCoreJson" );
  108. $jsonClass = new Services_JSON ();
  109. return $jsonClass->decode ( $data );
  110. }
  111. public static function loadSystemClass($className, $module = 'core') {
  112. static $classes = array ();
  113. $className = str_replace ( " ", "", sprintf ( "ACloudSys%s", ucwords ( str_replace ( ".", " ", $module . " " . $className ) ) ) );
  114. if (isset ( $classes [$className] ))
  115. return $classes [$className];
  116. $class = Wind::import ( sprintf ( "ACLOUD:system.%s.%s", $module, $className ) );
  117. if (! class_exists ( $className ))
  118. self::showError ( 'cann`t find class' );
  119. $classes [$className] = new $className ();
  120. return $classes [$className];
  121. }
  122. public static function loadAppClass($appName) {
  123. static $classes = array ();
  124. $appName = strtolower ( $appName );
  125. $class = sprintf ( "ACloudApp%sGuiding", ucfirst ( $appName ) );
  126. if (isset ( $classes [$class] ))
  127. return $classes [$class];
  128. $class = Wind::import ( sprintf ( "ACLOUD:app.%s.%s", $appName, $class ) );
  129. if (! class_exists ( $class ))
  130. self::showError ( 'cann`t find class' );
  131. $classes [$class] = new $class ();
  132. return $classes [$class];
  133. }
  134. public static function loadApps($page) {
  135. require_once Wind::getRealPath ( 'ACLOUD:app.ACloudAppRouter' );
  136. $router = new ACloudAppRouter ();
  137. $apps = $router->getAppsByPage ( $page );
  138. foreach ( $apps as $appname ) {
  139. $appClass = self::loadAppClass ( $appname );
  140. $appClass->execute ();
  141. }
  142. return $apps;
  143. }
  144. public static function arrayCombination($array, $ik, $vk) {
  145. if (! is_array ( $array ))
  146. return array ();
  147. $tmp = array ();
  148. foreach ( $array as $a ) {
  149. (isset ( $a [$ik] ) && isset ( $a [$vk] )) && $tmp [$a [$ik]] = $a [$vk];
  150. }
  151. return $tmp;
  152. }
  153. public static function arrayIntersectAssoc($array1, $array2) {
  154. if (! is_array ( $array1 ) || ! is_array ( $array2 ))
  155. return array ();
  156. $tmp = array ();
  157. if (! function_exists ( "array_intersect_assoc" )) {
  158. $tmp = array_intersect_assoc ( $array1, $array2 );
  159. } else {
  160. foreach ( $array1 as $k => $v ) {
  161. if (! isset ( $array2 [$k] ) || $array2 [$k] != $v) {
  162. continue;
  163. }
  164. $tmp [$k] = $v;
  165. }
  166. }
  167. return $tmp;
  168. }
  169. public static function refresh($url) {
  170. echo '<meta http-equiv="expires" content="0">';
  171. echo '<meta http-equiv="Pragma" content="no-cache">';
  172. echo '<meta http-equiv="Cache-Control" content="no-cache">';
  173. echo "<meta http-equiv='refresh' content='0;url=$url'>";
  174. exit ();
  175. }
  176. public static function getMicrotime() {
  177. $t_array = explode ( ' ', microtime () );
  178. return $t_array [0] + $t_array [1];
  179. }
  180. public static function convertToUTF8($str) {
  181. $charset = self::getGlobal ( 'g_charset', 'gbk' );
  182. return self::convert ( $str, 'utf-8', $charset );
  183. }
  184. public static function convertFromUTF8($str) {
  185. $charset = self::getGlobal ( 'g_charset', 'gbk' );
  186. return self::convert ( $str, $charset, 'utf-8' );
  187. }
  188. public static function convert($str, $toEncoding, $fromEncoding, $ifMb = true) {
  189. if (strtolower ( $toEncoding ) == strtolower ( $fromEncoding ))
  190. return $str;
  191. is_object ( $str ) && $str = get_object_vars ( $str );
  192. if (is_array ( $str )) {
  193. foreach ( $str as $key => $value ) {
  194. is_object ( $value ) && $value = get_object_vars ( $value );
  195. $str [$key] = self::convert ( $value, $toEncoding, $fromEncoding, $ifMb );
  196. }
  197. return $str;
  198. } else {
  199. if (function_exists ( 'mb_convert_encoding' ) && $ifMb) {
  200. return mb_convert_encoding ( $str, $toEncoding, $fromEncoding );
  201. } else {
  202. static $sConvertor = null;
  203. ! $toEncoding && $toEncoding = 'GBK';
  204. ! $fromEncoding && $fromEncoding = 'GBK';
  205. if (! isset ( $sConvertor ) && ! is_object ( $sConvertor )) {
  206. require_once Wind::getRealPath ( "ACLOUD:system.core.ACloudSysCoreCharset" );
  207. $sConvertor = new ACloudSysCoreCharset ();
  208. }
  209. return $sConvertor->Convert ( $str, $fromEncoding, $toEncoding, ! $ifMb );
  210. }
  211. }
  212. }
  213. public static function buildSecutiryCode($string, $key, $action = 'ENCODE') {
  214. $action != 'ENCODE' && $string = base64_decode ( $string );
  215. $code = '';
  216. $key = substr ( md5 ( $key ), 8, 18 );
  217. $keyLen = strlen ( $key );
  218. $strLen = strlen ( $string );
  219. for($i = 0; $i < $strLen; $i ++) {
  220. $k = $i % $keyLen;
  221. $code .= $string [$i] ^ $key [$k];
  222. }
  223. return ($action != 'DECODE' ? base64_encode ( $code ) : $code);
  224. }
  225. }