PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/function.php

https://github.com/woodcarver/redisAdmin
PHP | 280 lines | 195 code | 29 blank | 56 comment | 38 complexity | 07a57397016461eeb40ed45ecb831a57 MD5 | raw file
  1. <?php
  2. /**
  3. * 对象自动加载魔法方法
  4. * Enter description here ...
  5. * @param string $classname
  6. */
  7. function __autoload($classname){
  8. if(strtolower(substr($classname, -10,10)) === 'controller'){
  9. $filename = CONTROLLER_DIR.$classname.".php";
  10. if (file_exists($filename)){
  11. include_once $filename;
  12. }else{
  13. exit($filename.' is not exists.');
  14. }
  15. }elseif (strtolower(substr($classname, -5,5)) == 'model'){
  16. $classname = substr($classname, 0,-5);
  17. $filename = MODEL_DIR.$classname.".model.php";
  18. if (file_exists($filename)){
  19. include_once $filename;
  20. }else{
  21. exit($filename.' is not exists.');
  22. }
  23. }
  24. }
  25. // 去除代码中的空白和注释
  26. function strip_whitespace($content) {
  27. $stripStr = '';
  28. //分析php源码
  29. $tokens = token_get_all ($content);
  30. $last_space = false;
  31. for ($i = 0, $j = count ($tokens); $i < $j; $i++)
  32. {
  33. if (is_string ($tokens[$i]))
  34. {
  35. $last_space = false;
  36. $stripStr .= $tokens[$i];
  37. }
  38. else
  39. {
  40. switch ($tokens[$i][0])
  41. {
  42. //过滤各种PHP注释
  43. case T_COMMENT:
  44. case T_DOC_COMMENT:
  45. break;
  46. //过滤空格
  47. case T_WHITESPACE:
  48. if (!$last_space)
  49. {
  50. $stripStr .= ' ';
  51. $last_space = true;
  52. }
  53. break;
  54. default:
  55. $last_space = false;
  56. $stripStr .= $tokens[$i][1];
  57. }
  58. }
  59. }
  60. return $stripStr;
  61. }
  62. /**
  63. * 实例化一个Model
  64. * Enter description here ...
  65. * @param unknown_type $model
  66. */
  67. function D($model = ''){
  68. static $_model_instance = array();
  69. if(empty($model))
  70. return new Model();
  71. $indentify = md5($model);
  72. $model_file = MODEL_DIR.$model.'.model.php';
  73. if(isset($_model_instance[$indentify]))
  74. return $_model_instance[$indentify];
  75. if(file_exists($model_file)){
  76. include_once $model_file;
  77. $_model = ucfirst($model).'Model';
  78. $_model_instance[$indentify] = new $_model;
  79. return $_model_instance[$indentify];
  80. }else{
  81. return new Model($model);
  82. }
  83. exit('the '.$model.' model is not exists.');
  84. }
  85. /**
  86. * 字符串特殊字符转义函数
  87. * Enter description here ...
  88. * @param unknown_type $string
  89. */
  90. function maddslashes($string){
  91. !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
  92. if(!MAGIC_QUOTES_GPC) {
  93. if(is_array($string)) {
  94. foreach($string as $key => $val) {
  95. $string[$key] = maddslashes($val);
  96. }
  97. } else {
  98. $string = htmlentities($string,ENT_QUOTES,'UTF-8');
  99. //$string = addslashes($string);
  100. }
  101. }
  102. return $string;
  103. }
  104. /**
  105. * 变量类型转换
  106. * Enter description here ...
  107. * @param mixed $data
  108. * @param string $type string|bool|int|float|double
  109. */
  110. function convertType($data,$type = "string"){
  111. if(is_array($data)) {
  112. foreach($data as $key => $val) {
  113. $data[$key] = convertType($val,$type);
  114. }
  115. } else {
  116. switch ($type){
  117. case "string":
  118. $data = (string) $data;break;
  119. case "boolean":
  120. case "bool":
  121. $data = (boolean) $data;break;
  122. case "array":
  123. $data = (array) $data;break;
  124. case "object":
  125. $data = (object) $data;break;
  126. case "integer":
  127. case "int":
  128. $data = (integer) $data;break;
  129. case "unset":
  130. default:
  131. $data = (unset) $data;break;
  132. }
  133. }
  134. return $data;
  135. }
  136. /**
  137. * 随机字符串
  138. */
  139. function randStr($len)
  140. {
  141. $chars='0123456789abcdefghijklmnopqrstuvwxyz'; // characters to build the password from
  142. $string="";
  143. for($i=$len;$i>0;$i--)
  144. {
  145. $position=rand()%strlen($chars);
  146. $string.=substr($chars,$position,1);
  147. }
  148. return $string;
  149. }
  150. // 获取客户端IP地址
  151. function get_client_ip(){
  152. if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
  153. $ip = getenv("HTTP_CLIENT_IP");
  154. else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
  155. $ip = getenv("HTTP_X_FORWARDED_FOR");
  156. else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
  157. $ip = getenv("REMOTE_ADDR");
  158. else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
  159. $ip = $_SERVER['REMOTE_ADDR'];
  160. else
  161. $ip = "unknown";
  162. return($ip);
  163. }
  164. function getIpLong(){
  165. $ip = get_client_ip();
  166. if ($ip != 'unknown'){
  167. return sprintf("%.u",ip2long($ip));
  168. }
  169. return 0;
  170. }
  171. /**
  172. * 检测用户名是否已字母开始,是否只包含字母数字和下划线,最短3位,最长20位
  173. * Enter description here ...
  174. * @param string $username
  175. */
  176. function check_username($username){
  177. $pattern = '/^[\x80-\xffa-zA-z0-9][\x80-\xffa-zA-Z0-9-_:]{2,42}$/';
  178. if (!preg_match($pattern, $username)){
  179. return false;
  180. }
  181. return $mac;
  182. }
  183. /**
  184. * 发送邮件的方法
  185. * Enter description here ...
  186. * @param string $mail
  187. * @param array $data $data = array('title'=>'邮件标题','content'=>'邮件正文');
  188. */
  189. function send_mail($mail,$data){
  190. $smtpserver = SENDMAIL_STMP;//SMTP服务器
  191. $smtpserverport = 25;//SMTP服务器端口
  192. $smtpusermail = SENDMAIL_USERMAIL;//SMTP服务器的用户邮箱
  193. $smtpuser = SENDMAIL_USERNAME;//SMTP服务器的用户帐号
  194. $smtppass = SENDMAIL_PASSWORD;//SMTP服务器的用户密码
  195. $smtpemailto = $mail;//发送给谁
  196. $mailsubject = $data['title'];//邮件主题
  197. $mailtime = date("Y-m-d H:i:s");
  198. $utfmailbody = $data['content'];//转换邮件编码
  199. $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
  200. include(LIB_DIR."Sendmail.class.php");//发送邮件类
  201. $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
  202. $smtp->debug = false;//是否显示发送的调试信息 FALSE or TRUE
  203. if($smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $utfmailbody, $mailtype))
  204. {
  205. return 1;
  206. }else
  207. {
  208. return -2;//发送失败
  209. }
  210. }
  211. /**
  212. * 安全验证,验证码为sessionID和约定的token的md5码
  213. * Enter description here ...
  214. */
  215. function sessionToken()
  216. {
  217. if(!isset($_REQUEST['token']) || TOKEN!=$_REQUEST['token']){
  218. echo json_encode(array('state'=>false,'msg'=>'forbidden to access!'));
  219. exit;
  220. }
  221. }
  222. /**
  223. * 记录系统日志
  224. * Enter description here ...
  225. * @param string $FileName
  226. */
  227. function sylog($message,$type = "ERROR")
  228. {
  229. file_put_contents(DATA_DIR.'coco.log', '[ERROR:'.date('Y-m-d H:i:s').']'.$message.'\n\r',FILE_APPEND);
  230. }
  231. //截取utf8字符串
  232. function utf8Substr($str, $from, $len)
  233. {
  234. return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
  235. '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
  236. '$1',$str);
  237. }
  238. /**
  239. * 打印执行过的sql
  240. * Enter description here ...
  241. * @param string $FileName
  242. */
  243. function dumpsql()
  244. {
  245. if (!IS_DEPLOY)
  246. {
  247. global $debugsql;
  248. echo '<br/>------------------------the sqls you have excuted----------------------------------<br/>';
  249. echo $debugsql;
  250. }
  251. }