EasyFunction.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * @name eolinker ams open source,eolinker开源版本
  4. * @link https://www.eolinker.com/
  5. * @package eolinker
  6. * @author www.eolinker.com 广州银云信息科技有限公司 ©2015-2018
  7. * eoLinker是目前全球领先、国内最大的在线API接口管理平台,提供自动生成API文档、API自动化测试、Mock测试、团队协作等功能,旨在解决由于前后端分离导致的开发效率低下问题。
  8. * 如在使用的过程中有任何问题,欢迎加入用户讨论群进行反馈,我们将会以最快的速度,最好的服务态度为您解决问题。
  9. *
  10. * eoLinker AMS开源版的开源协议遵循Apache License 2.0,如需获取最新的eolinker开源版以及相关资讯,请访问:https://www.eolinker.com/#/os/download
  11. *
  12. * 官方网站:https://www.eolinker.com/
  13. * 官方博客以及社区:http://blog.eolinker.com/
  14. * 使用教程以及帮助:http://help.eolinker.com/
  15. * 商务合作邮箱:market@eolinker.com
  16. * 用户讨论QQ群:284421832
  17. */
  18. use RTP\Module as M;
  19. $filePaths = NULL;
  20. /**
  21. * 快捷数据库操作函数
  22. */
  23. function getDatabase($isNewInstance = false)
  24. {
  25. return $isNewInstance ? M\DatabaseModule::getNewInstance() : M\DatabaseModule::getInstance();
  26. }
  27. /**
  28. * 快捷完成请求函数,用于一次性按顺序返回所有信息,无须担心Cookie放置位置。
  29. * 注意,需要配合P()函数使用
  30. */
  31. function quickFlush()
  32. {
  33. ob_start();
  34. $outputFlush = M\OutputStorageModule::getAll();
  35. if (is_null($outputFlush))
  36. return;
  37. foreach ($outputFlush as $value) {
  38. echo $value;
  39. }
  40. //输出缓冲区并且清除缓冲区内容
  41. ob_end_flush();
  42. M\OutputStorageModule::clean();
  43. }
  44. /**
  45. * 快捷输入函数
  46. */
  47. function quickInput($paramName, $defaultValue = NULL)
  48. {
  49. switch (strtolower(AT)) {
  50. case 'auto' :
  51. {
  52. if (is_null($_GET[$paramName]) || $_GET[$paramName] == '') {
  53. if (is_null($_POST[$paramName] || $_POST[$paramName] == ''))
  54. return $defaultValue;
  55. else
  56. return $_POST[$paramName];
  57. } else
  58. return $_GET[$paramName];
  59. }
  60. case 'post' :
  61. {
  62. if (is_null($_POST[$paramName]) || $_POST[$paramName] == '')
  63. return $defaultValue;
  64. else
  65. return $_POST[$paramName];
  66. }
  67. case 'get' :
  68. {
  69. if (is_null($_GET[$paramName]) || $_GET[$paramName] == '')
  70. return $defaultValue;
  71. else
  72. return $_GET[$paramName];
  73. }
  74. default :
  75. return NULL;
  76. }
  77. }
  78. /**
  79. * 安全输入函数,获取参数并且对参数进行过滤
  80. */
  81. function securelyInput($paramName, $defaultValue = NULL)
  82. {
  83. switch (strtolower(AT)) {
  84. case 'auto' :
  85. {
  86. if (is_null($_GET[$paramName]) || $_GET[$paramName] == '') {
  87. if (is_null($_POST[$paramName]) || $_POST[$paramName] == '')
  88. return $defaultValue;
  89. else
  90. return cleanFormat($_POST[$paramName]);
  91. } else
  92. return cleanFormat($_GET[$paramName]);
  93. }
  94. case 'post' :
  95. {
  96. if (is_null($_POST[$paramName]) || $_POST[$paramName] == '')
  97. return $defaultValue;
  98. else
  99. return cleanFormat($_POST[$paramName]);
  100. }
  101. case 'get' :
  102. {
  103. if (is_null($_GET[$paramName]) || $_GET[$paramName] == '')
  104. return $defaultValue;
  105. else
  106. return cleanFormat($_GET[$paramName]);
  107. }
  108. default :
  109. return NULL;
  110. }
  111. }
  112. /**
  113. * 快捷输出函数:output,默认数组输出json,字符串直接输出
  114. */
  115. function quickOutput($output)
  116. {
  117. echo is_array($output) ? json_encode($output) : $output;
  118. }
  119. /**
  120. * 结束输出函数:output,默认数组输出json,字符串直接输出,并且输出之后停止程序
  121. */
  122. function exitOutput($output)
  123. {
  124. exit(is_array($output) ? json_encode($output) : $output);
  125. }
  126. /**
  127. * 快捷序列化输出函数,需要配合quickFlush()函数使用
  128. */
  129. function serialPrint($output, $distinct = FALSE)
  130. {
  131. if ($distinct)
  132. if (M\OutputStorageModule::isExist($output))
  133. return;
  134. M\OutputStorageModule::set($output);
  135. }
  136. /**
  137. * 快速引入文件函数
  138. */
  139. function quickRequire($filePath)
  140. {
  141. global $filePaths;
  142. if (is_null($filePaths))
  143. $filePaths = array();
  144. if (!isset($filePaths[$filePath])) {
  145. if (is_file($filePath)) {
  146. //require不使用函数形式是因为参数带括号会降低运行速度
  147. require $filePath;
  148. $filePaths[$filePath] = TRUE;
  149. } else {
  150. $filePaths[$filePath] = FALSE;
  151. }
  152. }
  153. }
  154. /**
  155. * 快捷Session操作函数:session
  156. */
  157. function quickSession(&$key, &$value)
  158. {
  159. if (session_status() == 1)
  160. session_start();
  161. if (isset($_SESSION[$key])) {
  162. if (isset($value))
  163. $_SESSION[$key] = $value;
  164. return $_SESSION[$key];
  165. } else
  166. $_SESSION[$key] = $value;
  167. }
  168. /**
  169. * 格式清除函数
  170. */
  171. function cleanFormat(&$value)
  172. {
  173. return htmlspecialchars(stripcslashes(trim($value)));
  174. }
  175. /**
  176. * 换行输出数组信息
  177. */
  178. function printFormatted(array $info)
  179. {
  180. foreach ($info as $key => $value) {
  181. echo "$key:$value</br>";
  182. };
  183. }
  184. /**
  185. * 判断请求协议是否为https
  186. * @return bool
  187. */
  188. function is_https()
  189. {
  190. if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
  191. return TRUE;
  192. } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
  193. return TRUE;
  194. } elseif (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
  195. return TRUE;
  196. }
  197. return FALSE;
  198. }
  199. /**
  200. * 异常处理
  201. * @param $error_level
  202. * @param $error_str
  203. * @throws Exception
  204. */
  205. function err_handler($error_level, $error_str)
  206. {
  207. throw new Exception("[$error_level] $error_str", 100001);
  208. }
  209. /**
  210. * 判断session是否已开启
  211. * @return bool
  212. */
  213. function is_session_started()
  214. {
  215. return function_exists('session_status') ? (PHP_SESSION_ACTIVE == session_status()) : (!empty (session_id()));
  216. }
  217. ?>