PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/rxwandc/application/ad.rxwan.com/daemon/lib/functions.inc.php

https://bitbucket.org/i1598/caiyun_stat
PHP | 321 lines | 264 code | 41 blank | 16 comment | 29 complexity | 7217e22953ff303fb37785d4e213f37a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. // firstopen_ --- 256
  3. // active_ --- 256
  4. // install_ --- 256
  5. // reinstall_ --- 256 (升级的重复安装会导致很多)
  6. // uninstall_ --- 256
  7. // reuninstall_ --- 256 (暂时不用)
  8. function table($table, $key='', $postfix=''){
  9. $numerator = hexdec(substr($key, 0, 3));
  10. switch($table){
  11. case 'uninstall':
  12. case 'firstopen':
  13. case 'reinstall':
  14. case 'install':
  15. case 'server':
  16. case 'active':
  17. $factor = 256;
  18. break;
  19. case 'ractive':
  20. case 'ractive2':
  21. $table = 'active';
  22. $factor = 256;
  23. break;
  24. case 'uslib':
  25. $factor = 32;
  26. break;
  27. case 'runinstall':
  28. case 'runinstall2':
  29. $table = 'uninstall';
  30. $factor = 64;
  31. break;
  32. case 'reuninstall':
  33. case 'reuninstall2':
  34. $table = 'uninstall';
  35. $factor = 128;
  36. }
  37. return $table.'_'.($numerator%$factor);
  38. }
  39. function database($db, $key='', $postfix=''){
  40. $numerator = hexdec(substr($key, 4, 1));
  41. $db_postfix = $db=='active'?'_'.($numerator%16):'';
  42. return $db.$postfix.$db_postfix;
  43. }
  44. function msg_die($msg){
  45. exit($msg);
  46. }
  47. function dblink($type='default', $is_new=false){
  48. global $cfg_db;
  49. $cfg_db[$type]['link'] = mysql_connect($cfg_db[$type]['host'].':'.$cfg_db[$type]['port'], $cfg_db[$type]['username'], $cfg_db[$type]['password'], $is_new) or msg_die("\nMySQL has got an error. ErrorNO: ".mysql_errno($cfg_db[$type]['link']).',ErrorMsg:'.mysql_error($cfg_db[$type]['link']).".\n");
  50. mysql_query("SET NAMES '".$cfg_db[$type]['charset']."'", $cfg_db[$type]['link']);
  51. mysql_select_db($cfg_db[$type]['dbname'], $cfg_db[$type]['link']); //dbname
  52. return $cfg_db[$type]['link'];
  53. }
  54. function dbclose($type='default'){
  55. global $cfg_db;
  56. return mysql_close($cfg_db[$type]['link'] );
  57. }
  58. function query($sql, $type='default'){
  59. global $cfg_db;
  60. $flag = false;
  61. if(!isset($cfg_db[$type]['link'])){
  62. $flag = true;
  63. }else{
  64. if(mysql_ping($cfg_db[$type]['link'])===false){
  65. $flag = true;
  66. }
  67. }
  68. if($flag){
  69. $cfg_db[$type]['link'] = dblink($type, true);
  70. }
  71. //不停执行,以保证在同一个库里面
  72. mysql_select_db($cfg_db[$type]['dbname'], $cfg_db[$type]['link']); //dbname
  73. $query = mysql_query($sql,$cfg_db[$type]['link']);
  74. if($query===false){
  75. echo "\nError in Query.MySQL has got an error. ErrorNO: ".mysql_errno($cfg_db[$type]['link']).',ErrorMsg:'.mysql_error($cfg_db[$type]['link']).".\n";
  76. echo "SQL:$sql.\n";
  77. exit;
  78. }
  79. return $query;
  80. }
  81. function getOne($sql, $type='default'){
  82. global $cfg_db;
  83. $row = '';
  84. $query = query($sql .' LIMIT 0,1', $type);
  85. if($query && mysql_num_rows($query)){
  86. $row = mysql_result($query, 0);
  87. mysql_free_result($query);
  88. }
  89. return $row;
  90. }
  91. function getRow($sql, $type='default'){
  92. global $cfg_db;
  93. $result = array();
  94. $query = query($sql.' LIMIT 0,1', $type);
  95. if($query && mysql_num_rows($query)){
  96. $data = array();
  97. while(($row = mysql_fetch_assoc($query))!==FALSE){
  98. $data[] = $row;
  99. }
  100. mysql_free_result($query);
  101. $result = $data[0];
  102. }
  103. return $result;
  104. }
  105. function getAll($sql, $type='default'){
  106. global $cfg_db;
  107. $result = array();
  108. $query = query($sql, $type);
  109. if($query && mysql_num_rows($query)){
  110. while(($row = mysql_fetch_assoc($query))!==FALSE){
  111. $result[] = $row;
  112. }
  113. mysql_free_result($query);
  114. }
  115. return $result;
  116. }
  117. function free_result($result, $type='default'){
  118. return mysql_free_result ($result);
  119. }
  120. function fetch_assoc($result, $type='default'){
  121. return mysql_fetch_assoc($result);
  122. }
  123. function num_rows($result, $type='default'){
  124. return mysql_num_rows($result);
  125. }
  126. function last_insert_id($type='default'){
  127. global $cfg_db;
  128. return mysql_insert_id($cfg_db[$type]['link']);
  129. }
  130. function logtofile($file_name='', $title="", $content="", $is_exit=false){
  131. global $logfile;
  132. $output = "======================================\r\n";
  133. $output .= date("r")."\t".$title."\r\n";
  134. $output .= "Content: ".$content."\r\n";
  135. $output .= "--------------------------------------\r\n";
  136. if(empty($file_name)){
  137. $file_name = $logfile;
  138. }
  139. file_put_contents($file_name, $output, FILE_APPEND);
  140. if($is_exit){
  141. exit;
  142. }
  143. }
  144. function get_insert_sql($table, $data, $db="", $type='default'){
  145. global $cfg_db;
  146. $keys = $values = array();
  147. foreach($data as $k=>$d){
  148. $keys[] = $k;
  149. $values[] = mysql_real_escape_string($d, $cfg_db[$type]['link']);
  150. }
  151. return "INSERT INTO `$db`.`$table`(`".implode('`,`', $keys)."`) VALUES('".implode("','",$values)."')";
  152. }
  153. function get_update_sql($table, $data, $attach_sql = '', $db='', $type='default'){
  154. global $cfg_db;
  155. if(!count($data)){
  156. return false;
  157. }
  158. $sets = array();
  159. foreach($data as $k=>$d){
  160. $sets[] = '`'.$k.'`=\''.mysql_real_escape_string($d, $cfg_db[$type]['link']).'\'';
  161. }
  162. if(!empty($attach_sql)){
  163. $attach_sql = ' AND '.$attach_sql;
  164. }
  165. return "UPDATE `$db`.`$table` SET ".implode(', ',$sets).' WHERE 1 '.$attach_sql;
  166. }
  167. function cache_link($type='default'){
  168. global $cfg_cache;
  169. $craw_name = 'memcache_'.$type;
  170. if (!is_object($$craw_name)) {
  171. $$craw_name = new Memcache;
  172. $$craw_name->addServer($cfg_cache[$type]['host'],$cfg_cache[$type]['port']);
  173. }
  174. return $cfg_cache[$type]['link'] = $$craw_name;
  175. }
  176. function cache_get($key, $type='default'){
  177. global $cfg_cache;
  178. if(!isset($cfg_cache[$type]['link'])){
  179. cache_link($type);
  180. }
  181. return $cfg_cache[$type]['link']->get($cfg_cache[$type]['prefix'].$key);
  182. }
  183. function cache_set($key, $value, $type='default', $expire_time=0, $is_compressed=0 ){
  184. global $cfg_cache;
  185. if(!isset($cfg_cache[$type]['link'])){
  186. cache_link($type);
  187. }
  188. $expire_time = empty($expire_time)?0:$expire_time;
  189. return $cfg_cache[$type]['link']->set($cfg_cache[$type]['prefix'].$key, $value, $is_compressed, $expire_time);
  190. }
  191. ///////////////////////////
  192. ///////////////////////////
  193. ///////////////////////////
  194. ///////////////////////////
  195. function crand($min, $max){
  196. list($usec, $sec) = explode(' ', microtime());
  197. $seed =(float) $sec + ((float) $usec * 100000);
  198. mt_srand($seed);
  199. return mt_rand($min, $max);
  200. }
  201. function is_md5str($string=""){
  202. return preg_match('/^[a-f0-9]{32}$/', $string);
  203. }
  204. function is_version($string){
  205. $flag = false;
  206. if(preg_match('/^[v0-9\.]{3,20}$/', $string)){
  207. if(strpos($string, '.')){ // 以 .开头的字符串,也不算版本
  208. $flag = true;
  209. }
  210. }
  211. return $flag;
  212. }
  213. function explode_package($pkg_name){
  214. $pkg_info = array(
  215. 'pkg'=>$pkg_name,
  216. 'ver'=>'',
  217. 'coop'=>'' //默认为空,就是我们公司的
  218. );
  219. $protect_members = array('caiyun', 'update', 'setup', 'install','installer', 'uninstall', 'inst', 'uninst', 'log', 'login', 'beta');
  220. $pkg_name = strtolower(trim(sbc2abc($pkg_name)));
  221. if(!empty($pkg_name)){
  222. $pkg_name_info = substr($pkg_name, 0, strlen($pkg_name)-4);
  223. $pkg_name_infos = explode('_',$pkg_name_info);
  224. $pkg_name_infos_length = count($pkg_name_infos);
  225. if($pkg_name_infos_length>2){
  226. $coopinfo = coopfilter($pkg_name_infos[$pkg_name_infos_length-1]);
  227. if(!in_array($coopinfo, $protect_members)){
  228. if(is_version($coopinfo)){
  229. $pkg_info['ver']=$coopinfo;
  230. }elseif(is_cooper($coopinfo, $protect_members)){
  231. $pkg_info['coop'] = $coopinfo;
  232. }
  233. }
  234. if(is_version($pkg_name_infos[$pkg_name_infos_length-2])){
  235. $pkg_info['ver'] = $pkg_name_infos[$pkg_name_infos_length-2];
  236. }
  237. }
  238. }
  239. return $pkg_info;
  240. }
  241. function is_cooper($cooper, $protect_members){
  242. $flag = strlen($cooper)>1? true:false;
  243. if ($flag) {
  244. foreach ($protect_members as $protect_member){
  245. if(strpos($cooper, $protect_member)!==FALSE){ //只要出现,就不是了
  246. $flag = false;
  247. break;
  248. }
  249. }
  250. }
  251. return $flag;
  252. }
  253. function sbc2abc($str) {
  254. $f = array (' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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', '.', '-', '_', '@', '(', ')', '【', '】', '[', ']', '{', '}', '=', '+', ':', ';', ',', '。','《', '》');
  255. $t = array (' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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', '.', '-', '_', '@' , '(', ')', '[', ']', '[', ']','{','}','=','+',':',';',',','.','<','>');
  256. $str = str_replace ( $f, $t, $str );
  257. return $str;
  258. }
  259. // sbc2abc 在原始字符串的时候,就替换过了
  260. // 过滤掉非字母等字符,并且输出全部是小写
  261. // 过滤掉了空格和汉字等
  262. // 从左到右,会过滤掉后面的非法字符,也就是左合法,到右边,依次不合法
  263. // 例如:asdfasf我要合作商---> asdfasf, 我要mdsfuowef合作msfasom商---->mdsfuowef
  264. function coopfilter($string){
  265. $coops = explode('.exe', strtolower($string));
  266. $string = preg_replace(array('/(?:\[|\(|\{)[0-9a-z\.\-\_\,\;\[\{\(\)\}\]]+(?:\}|\)|\])/','/(?:\[|\(|\{)/','/(?:\}|\)|\])/' ), '', $coops[0]);
  267. $length = strlen($string);
  268. $avaliable_alpha = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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', '-', '_', '@' ,'.');
  269. $output = '';
  270. for ($i = 0; $i < $length; $i++) {
  271. if (in_array($string[$i], $avaliable_alpha)) {
  272. $output .= $string[$i];
  273. }else{
  274. if(!empty($output)){
  275. break;
  276. }
  277. }
  278. }
  279. return trim($output);
  280. }