Funcs.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. use Qii\Autoloader\Import;
  3. use Qii\Autoloader\Psr4;
  4. use Qii\Language\Loader;
  5. if (!function_exists('getallheaders')) {
  6. /**
  7. * Get all HTTP header key/values as an associative array for the current request.
  8. *
  9. * @return string[string] The HTTP header key/value pairs.
  10. */
  11. function getallheaders()
  12. {
  13. $headers = array();
  14. $copy_server = array(
  15. 'CONTENT_TYPE' => 'Content-Type',
  16. 'CONTENT_LENGTH' => 'Content-Length',
  17. 'CONTENT_MD5' => 'Content-Md5',
  18. );
  19. foreach ($_SERVER as $key => $value) {
  20. if (substr($key, 0, 5) === 'HTTP_') {
  21. $key = substr($key, 5);
  22. if (!isset($copy_server[$key]) || !isset($_SERVER[$key])) {
  23. $key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $key))));
  24. $headers[$key] = $value;
  25. }
  26. } elseif (isset($copy_server[$key])) {
  27. $headers[$copy_server[$key]] = $value;
  28. }
  29. }
  30. if (!isset($headers['Authorization'])) {
  31. if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
  32. $headers['Authorization'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
  33. } elseif (isset($_SERVER['PHP_AUTH_USER'])) {
  34. $basic_pass = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
  35. $headers['Authorization'] = 'Basic ' . base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $basic_pass);
  36. } elseif (isset($_SERVER['PHP_AUTH_DIGEST'])) {
  37. $headers['Authorization'] = $_SERVER['PHP_AUTH_DIGEST'];
  38. }
  39. }
  40. return $headers;
  41. }
  42. }
  43. /**
  44. * Qii ...
  45. * @return null|Qii|Qii\Autoloader\Psr4
  46. */
  47. function _Qii()
  48. {
  49. return \Qii::getInstance();
  50. }
  51. /**
  52. * \Qii::i(.., ...)
  53. * @return mixed
  54. */
  55. function _i()
  56. {
  57. return call_user_func_array('\Qii::i', func_get_args());
  58. }
  59. /**
  60. * throw new Exception
  61. */
  62. function _e()
  63. {
  64. return call_user_func_array('\Qii::e', func_get_args());
  65. }
  66. /**
  67. * Chrome logs
  68. * @return mixed
  69. */
  70. function _log() {
  71. return call_user_func_array('\Qii\Library\Chrome::log', func_get_args());
  72. }
  73. /**
  74. * 加载语言包
  75. * @param string $language 语言包
  76. * @param string $dir 路径
  77. */
  78. function _language($language, $dir = '')
  79. {
  80. Loader::getInstance()->load($language, $dir);
  81. }
  82. /**
  83. * \Qii_Config_Register:: get or set
  84. * @param $key
  85. * @param null $val
  86. * @return mixed|void|null
  87. */
  88. function _config($key, $val = null)
  89. {
  90. if($val === null)
  91. {
  92. return \Qii\Config\Register::get($key);
  93. }
  94. return \Qii\Config\Register::set($key, $val);
  95. }
  96. /**
  97. * Adds a base directory for a namespace prefix.
  98. *
  99. * @param string $prefix The namespace prefix.
  100. * @param string $baseDir A base directory for class files in the
  101. * namespace.
  102. * @param bool $prepend If true, prepend the base directory to the stack
  103. * instead of appending it; this causes it to be searched first rather
  104. * than last.
  105. * @return void
  106. */
  107. function _addNamespace($prefix, $baseDir, $prepend = false)
  108. {
  109. _qii()->addNamespace($prefix, $baseDir, $prepend);
  110. }
  111. /**
  112. * 加载loader 可以直接加载指定类
  113. */
  114. function _loader($class = null)
  115. {
  116. $args = func_get_args();
  117. if($class != null){
  118. return call_user_func_array(array(Psr4::getInstance(), 'loadClass'), $args);
  119. }
  120. return Psr4::getInstance();
  121. }
  122. /**
  123. * load library
  124. * @param $name
  125. * @return mixed
  126. */
  127. function _library($name) {
  128. return \_loadClass("\Qii\Library\\". $name);
  129. }
  130. /**
  131. * 简便的loadClass方法
  132. * \Qii\Autoloader\Psr4::getInstance()->loadClass(.., ..);
  133. */
  134. function _loadClass()
  135. {
  136. $args = func_get_args();
  137. return call_user_func_array(array(\_loader(), 'loadClass'), $args);
  138. }
  139. /**
  140. * 根据文件前缀获取文件路径
  141. *
  142. * @param string $file 文件名
  143. */
  144. function _getFileByPrefix($file)
  145. {
  146. return \_loader()->getFileByPrefix($file);
  147. }
  148. /**
  149. * 数据库操作类
  150. *
  151. * @param Qii_Driver_Rules $rule 规则
  152. * @param array|null $privateKey 主键
  153. * @param array|null $fieldsVal 值
  154. * @return mixed
  155. */
  156. function _DBDriver(\Qii\Driver\Rules $rule, $privateKey = null, $fieldsVal = null)
  157. {
  158. $rules = _loadClass('Qii\Driver\Easy')->_initialize();
  159. if ($privateKey) $rules->setPrivateKey($privateKey);
  160. $rules->setRules($rule);
  161. if ($fieldsVal) $rules->setFieldsVal($fieldsVal);
  162. return $rules;
  163. }
  164. /**
  165. * _include include文件
  166. */
  167. function _include($files){
  168. return Import::includes($files);
  169. }
  170. function _require($files)
  171. {
  172. return Import::requires($files);
  173. }
  174. /**
  175. * 检测字符的编码
  176. *
  177. * @param string $str
  178. * @return false|string
  179. */
  180. function detectEncode($str) {
  181. return mb_detect_encoding($str, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
  182. }
  183. /**
  184. * 将字符串转换成指定编码
  185. *
  186. * @param string $str 需要转换的字符串
  187. * @param string $to 转换到的编码
  188. * @return string
  189. */
  190. function convertCode($str, $to)
  191. {
  192. $fromCode = detectEncode($str);
  193. if($fromCode == $to) return $str;
  194. return mb_convert_encoding($str, $to, $fromCode);
  195. }
  196. /**
  197. * 将字符串转换成格式
  198. *
  199. * @param string $str 需要转换的字符串
  200. * @return string
  201. */
  202. function toUTF8($str)
  203. {
  204. return convertCode($str, 'UTF-8');
  205. }
  206. /**
  207. * 将字符串转换成GBK
  208. *
  209. * @param string $str 文本
  210. * @return string
  211. */
  212. function toGBK($str)
  213. {
  214. return convertCode($str, 'GBK');
  215. }
  216. /**
  217. * 字符串是否包含
  218. *
  219. * @param string $haystack
  220. * @param string $needle
  221. * @return bool
  222. */
  223. function strContains(string $haystack, string $needle)
  224. {
  225. return '' === $needle || false !== strpos($haystack, $needle);
  226. }
  227. /**
  228. * 以xxx开头
  229. * @param string $haystack
  230. * @param string $needle
  231. * @return bool
  232. */
  233. function strStartsWith($haystack, $needle)
  234. {
  235. return 0 === strncmp($haystack, $needle, \strlen($needle));
  236. }
  237. /**
  238. * 以xx结尾
  239. * @param string $haystack
  240. * @param string $needle
  241. * @return bool
  242. */
  243. function strEndsWith($haystack, $needle)
  244. {
  245. if ('' === $needle || $needle === $haystack) {
  246. return true;
  247. }
  248. if ('' === $haystack) {
  249. return false;
  250. }
  251. $needleLength = \strlen($needle);
  252. return $needleLength <= \strlen($haystack) && 0 === substr_compare($haystack, $needle, -$needleLength);
  253. }