functions.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. /* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  3. -- Common Functions
  4. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */
  5. /*
  6. * 返回系统配置
  7. */
  8. function funcSystemConfig($params){
  9. global $configs;
  10. return $configs;
  11. }
  12. /* -- -- -- -- -- -- -- -- -- -- -- -- -- 数据库公共操作 -- -- -- -- -- -- -- -- -- -- -- -- -- */
  13. /*
  14. * 基于模式连接数据库并返回连接对象
  15. * 返回 $db = array('object' => '连接对象', 'function' => '方法集')
  16. */
  17. function connectDB($mode = "MySQLi"){
  18. global $configs;
  19. $conf = $configs['database'][$mode];
  20. $db = $conf['function']['Connect']($conf);
  21. return $db;
  22. }
  23. /* $conn = 数据库连接对象 */
  24. function doSQL($sql, $conn = null){
  25. global $connDB;
  26. if(is_null($conn))$conn = $connDB;
  27. try{
  28. return $conn['function']['Select']($conn['object'], $sql);
  29. }catch(Exception $e){
  30. return array();
  31. }
  32. }
  33. /* $conn = 数据库连接对象 */
  34. function doPage($sql, $wheres, $pagecount = 50, $conn = null){
  35. global $connDB;
  36. if(is_null($conn))$conn = $connDB;
  37. try{
  38. $pageSQL = "select COUNT(*) as counts, CEILING(COUNT(*)/" . $pagecount . ") as pages from (" . $sql . ") pagedata ";
  39. $pageSQL .= ($wheres) ? ("where" . $wheres) : "";
  40. $result = $conn['function']['Select']($conn['object'], $pageSQL);
  41. return ($result) ? $result[0] : array();
  42. }catch(Exception $e){
  43. return array();
  44. }
  45. }
  46. /*
  47. * 分页公共语句
  48. * $params = array('pageno' => '页码', 'pagecount' => '每页数据', 'limit' => '数据量')
  49. */
  50. function doPageSQL($params){
  51. $limits = "";
  52. if(isset($params['pageno'])){
  53. if(!$params['pageno'] || $params['pageno'] == 1){
  54. $limits = " limit " . $params['pagecount'];
  55. }else{
  56. $limits = " limit " . ($params['pageno'] - 1) * $params['pagecount'] . ", " . $params['pagecount'];
  57. }
  58. }else{
  59. if(isset($params['limit'])){
  60. $limits = " limit " . $params['limit'];
  61. }
  62. }
  63. return $limits;
  64. }
  65. /*
  66. * 组合SQL语句
  67. * $data => 数据体, $params = array('mode' => 'insert|update', 'table' => '表名', 'wheres' => '条件字符串')
  68. */
  69. function executeSQL($data, $params){
  70. global $connDB;
  71. if($params['mode'] == "insert"){
  72. $fields = "";
  73. $values = "";
  74. foreach($data as $key => $val){
  75. $fields .= ($fields == "") ? "" : ", ";
  76. $fields .= $key;
  77. $values .= ($values == "") ? "" : ", ";
  78. //$values .= (is_numeric($val)) ? $val : ("'" . $val . "'");
  79. $values .= ("'" . $val . "'");
  80. }
  81. $sql = "insert into " . $params['table'] . "(" . $fields . ") values(" . $values . ")";
  82. }elseif($params['mode'] == "update"){
  83. $upds = "";
  84. foreach($data as $key => $val){
  85. $upds .= ($upds == "") ? "" : ", ";
  86. $upds .= $key . " = ";
  87. $upds .= (is_numeric($val)) ? $val : ("'" . $val . "'");
  88. }
  89. $sql = "update " . $params['table'] . " set " . $upds;
  90. if($params['where'])$sql .= " where " . $params['where'];
  91. }
  92. return $sql;
  93. }
  94. /*
  95. * 执行SQL语句
  96. *
  97. */
  98. function execSQL($sql, $func = null, $conn = null){
  99. global $connDB;
  100. if(is_null($conn))$conn = $connDB;
  101. if($func == "insert"){
  102. return $conn['function']['Insert']($conn['object'], $sql);
  103. }elseif($func == "insertid"){
  104. return $conn['function']['InsertId']($conn['object'], $sql);
  105. }elseif($func == "update"){
  106. return $conn['function']['Update']($conn['object'], $sql);
  107. }elseif($func == "delete"){
  108. return $conn['function']['Delete']($conn['object'], $sql);
  109. }else{
  110. return $conn['function']['Query']($conn['object'], $sql);
  111. }
  112. }
  113. /*
  114. * 基于参照格式化查询数据
  115. * $params = array('data' => 'rs结果集', 'datafield' => '需要格式化的字段', 'datatimes' => '需要格式化的时间戳字段')
  116. */
  117. function funcFormatResultData($params){
  118. global $configs;
  119. $result = array();
  120. $fieldArray = explode(",", $params['datafield']);
  121. $timesArray = explode(",", $params['datatimes']);
  122. for($i = 0; $i < count($params['data']); $i++){
  123. $result[$i] = $params['data'][$i];
  124. if(count($fieldArray)){
  125. for($keyField = 0; $keyField < count($fieldArray); $keyField++){
  126. if($fieldArray[$keyField] != ''){
  127. $result[$i][$fieldArray[$keyField] . '_format'] = $configs['refer']['refer_' . $fieldArray[$keyField]][$params['data'][$i][$fieldArray[$keyField]]];
  128. }
  129. }
  130. }
  131. if(count($timesArray)){
  132. for($keyTimes = 0; $keyTimes < count($timesArray); $keyTimes++){
  133. if($timesArray[$keyTimes]){
  134. $result[$i][$timesArray[$keyTimes] . '_format'] = date("Y-m-d H:i:s", $params['data'][$i][$timesArray[$keyTimes]]);
  135. }
  136. }
  137. }
  138. }
  139. return $result;
  140. }
  141. /*
  142. * 自动加载文件
  143. * $params = array(path => '文件夹路径', method => 'require_once|include_once')
  144. */
  145. function funcComAutoLoad($params){
  146. try{
  147. if(is_file($params['path'])){
  148. funcComLoadExec($params['path'], $params['method']); //单文件加载
  149. }else{
  150. $folder = scandir($params['path']);
  151. foreach($folder as $incfile){
  152. if($incfile != '.' && $incfile != '..'){
  153. if(!is_dir($params['path'] . '/' . $incfile)){
  154. funcComLoadExec($params['path'] . '/' . $incfile, $params['method']);
  155. }
  156. }
  157. }
  158. }
  159. return true;
  160. }catch(Exception $Err){
  161. return $E->getMessage();
  162. }
  163. }
  164. function funcComLoadExec($path, $method){
  165. if($method == "include_once"){
  166. include_once($path);
  167. }
  168. if($method == "include"){
  169. include($path);
  170. }
  171. if($method == "require_once"){
  172. require_once($path);
  173. }
  174. if($method == "require"){
  175. require($path);
  176. }
  177. }
  178. /*
  179. * CURL 执行
  180. * $params = array(url => '请求地址', json => false|true, data => '报文数据')
  181. */
  182. function funcComCurl($params){
  183. if(empty($params['url']))return false;
  184. $CURL = curl_init();
  185. curl_setopt($CURL, CURLOPT_URL, $params['url']);
  186. if($params['json']){
  187. curl_setopt($CURL, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen(json_encode($params['data'], JSON_UNESCAPED_UNICODE))));
  188. }
  189. if(!empty($params['data'])){
  190. curl_setopt($CURL, CURLOPT_POST, 1);
  191. curl_setopt($CURL, CURLOPT_POSTFIELDS, $params['data']);
  192. }
  193. curl_setopt($CURL, CURLOPT_TIMEOUT, 30);curl_setopt($CURL, CURLOPT_SSL_VERIFYPEER, FALSE);
  194. curl_setopt($CURL, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($CURL, CURLOPT_HEADER, FALSE);curl_setopt($CURL, CURLOPT_RETURNTRANSFER, TRUE);
  195. $response = curl_exec($CURL);
  196. curl_close($CURL);
  197. return (!$response) ? false : $response;
  198. }
  199. /*
  200. * 转UTF8
  201. */
  202. function funcComArrayTranUtf8($res){
  203. return ($res != "" && !is_null($res) && !is_object($res)) ? iconv('GBK', 'UTF-8', $res) : '';
  204. }
  205. /*
  206. * 转GBK
  207. */
  208. function funcComArrayTranGBK($res){
  209. return ($res != "" && !is_null($res) && !is_object($res)) ? iconv("UTF-8", "GBK", $res) : '';
  210. }
  211. /*
  212. * 随机函数
  213. * returnRandom('rand', array('num' => 12))
  214. */
  215. function funcComRandom($mode, $params = null){
  216. global $configs;
  217. $randMode = array(
  218. 'verify' => '0123456789',
  219. 'rand' => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  220. 'random' => 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  221. );
  222. $num = ($params['num']) ? $params['num'] : 6;
  223. $key = "";
  224. for($j = 0; $j < $num; $j++){
  225. $key .= $randMode[$mode][rand(0, strlen($randMode[$mode])-1)];
  226. }
  227. return $key;
  228. }
  229. /*
  230. * 数组转http字符串
  231. * array => http&
  232. */
  233. function funcComArrayToHttp($paraMap, $urlencode = false){
  234. $buff = "";
  235. //ksort($paraMap);
  236. foreach ($paraMap as $k => $v){
  237. if($urlencode){$v = urlencode($v);}
  238. $buff .= $k . "=" . $v . "&";
  239. }
  240. $reqPar;
  241. if (strlen($buff) > 0){$reqPar = substr($buff, 0, strlen($buff)-1);}
  242. return $reqPar;
  243. }
  244. /*
  245. * http字符串转数组
  246. * http& => array
  247. */
  248. function funcComHttpToArray($params){
  249. $output = array();if($params){parse_str($params, $output);}return $output;
  250. }
  251. /*
  252. * 数组转XML
  253. */
  254. function funcComArrayToXml($arr){
  255. $xml = "<xml>";
  256. foreach($arr as $key => $val){
  257. if(is_numeric($val)){
  258. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  259. }else{
  260. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  261. }
  262. }
  263. $xml.= "</xml>";
  264. return $xml;
  265. }
  266. /*
  267. * XML转数组
  268. * $xml => object
  269. */
  270. function funcComXmlToArray($xml){
  271. //将XML转为array
  272. return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  273. }
  274. /*
  275. * 获取SHA256加密
  276. */
  277. function funcComSHA256($str){
  278. return bin2hex(hash('sha256', $str, true));
  279. }
  280. /*
  281. * 获取毫秒时间戳
  282. */
  283. function funcComGetMillisecond(){
  284. list($microsecond , $time) = explode(' ', microtime()); //' '中间是一个空格
  285. return (float)sprintf('%.0f',(floatval($microsecond) + floatval($time)) * 1000);
  286. }
  287. /*
  288. * 获取请求客户端基础信息
  289. */
  290. function funcComGetClientInfo($servers){
  291. $returns = array('ip' => '', 'port' => '', 'uri' => '', 'domain' => 0);
  292. $returns['ip'] = $servers['REMOTE_ADDR'];
  293. $returns['port'] = $servers['REMOTE_PORT'];
  294. $returns['uri'] = $servers['REQUEST_URI'];
  295. $returns['domain'] = ($servers['REMOTE_ADDR'] == $servers['SERVER_ADDR']) ? 1 : 0;
  296. return $returns;
  297. }
  298. /*
  299. * 获取文件后缀名
  300. */
  301. function funcComGetFileExt($filename){
  302. $arrays = explode(".", $filename);
  303. return (count($arrays) >= 1) ? $arrays[count($arrays) - 1] : '';
  304. }
  305. /*
  306. * 生成二维码
  307. * $params = array('errorCorrectionLevel' => 'M(默认)', 'matrixPointSize' => '5(默认)', 'filename' => '生成文件名(默认随机串)', 'filepath' => '生成路径', 'lib' => '类库路径(默认)', 'data' => '封装数据')
  308. */
  309. function funcComCreateQrCode($params){
  310. global $configs;
  311. $returns = array('success' => true, 'errorcode' => '200', 'msg' => $configs['errors']['200'], 'data' => array());
  312. $lib = (isset($params['lib'])) ? $params['lib'] : 'lib/php-phpqrcode/phpqrcode.php';
  313. require_once($lib);
  314. $errorCorrectionLevel = (isset($params['errorCorrectionLevel'])) ? $params['errorCorrectionLevel'] : 'M'; //容错级别
  315. $matrixPointSize = (isset($params['matrixPointSize'])) ? $params['matrixPointSize'] : 5;
  316. $filename = (isset($params['filename'])) ? $params['filename'] : (time() . funcComRandom('rand', array('num' => 3)));
  317. $filepath = (isset($params['filepath'])) ? $params['filepath'] : $configs['qrcode']['path'];
  318. QRcode::png($params['data'], $filepath . $filename . ".png", $errorCorrectionLevel, $matrixPointSize, 2);
  319. $returns['data'] = array('filepath' => $filepath . $filename . ".png", 'filename' => $filename);
  320. return $returns;
  321. }
  322. /*
  323. * 上传文件公共函数
  324. */
  325. function funcComFileUpload($file){
  326. global $configs;
  327. $returns = array('success' => false, 'errorcode' => '403', 'data' => array());
  328. $todo = true;
  329. if(isset($file['params'])){
  330. /* 验证 */
  331. foreach($file['params'] as $key => $val){
  332. if($key == "ext-access"){
  333. if(!in_array(funcComGetFileExt($file['file']['name']), $val)){
  334. $todo = false;
  335. break;
  336. }
  337. }
  338. if($key == "limit-access"){
  339. if((int)$file['file']['size'] < $val[0] || (int)$file['file']['size'] > $val[1]){
  340. $todo = false;
  341. break;
  342. }
  343. }
  344. if($key == "check-ext"){
  345. if(!in_array(funcComGetFileExt($file['file']['name']), $configs['upload']['exts'])){
  346. echo "222";
  347. $todo = false;
  348. break;
  349. }
  350. }
  351. if($key == "check-limit"){
  352. if((int)$file['file']['size'] < $configs['upload']['limit'][0] || (int)$file['file']['size'] > $configs['upload']['limit'][1]){
  353. echo "111";
  354. $todo = false;
  355. break;
  356. }
  357. }
  358. }
  359. }
  360. if(isset($file['params']['folder']) && $file['params']['folder']){
  361. $file['params']['folder'] = $file['params']['folder'] . "/";
  362. }else{
  363. $file['params']['folder'] = "/";
  364. }
  365. if($todo){
  366. try{
  367. $obj = $file['file'];
  368. $returns['data']['file_filename'] = $obj['name'];
  369. $returns['data']['file_savename'] = "file-" . funcComGetMillisecond() . "." . funcComGetFileExt($obj['name']);
  370. if(move_uploaded_file($obj["tmp_name"], $configs['upload']['path'] . $file['params']['folder'] . $returns['data']['file_savename'])){
  371. $returns['data']['file_savepath'] = $configs['upload']['path'] . $file['params']['folder'];
  372. $returns['success'] = true;
  373. $returns['errorcode'] = "200";
  374. }else{
  375. $returns['msg'] = $configs['errors'][$returns['errorcode']] . " 保存文件错误";
  376. }
  377. }catch(Exception $E){
  378. $returns['msg'] = $configs['errors'][$returns['errorcode']] . " 执行保存错误" . $E->getMessage();
  379. }
  380. }else{
  381. $returns['msg'] = $configs['errors'][$returns['errorcode']] . " 文件类型错误或超出大小限制";
  382. }
  383. return $returns;
  384. }
  385. /*
  386. * 日志
  387. * $params = array('log' => '日志内容', 'path' => '(默认文件)')
  388. */
  389. function funcComLog($params){
  390. global $configs;
  391. if(isset($params['path'])){
  392. $filename = $params['path'];
  393. }else{
  394. $filepath = strftime("%Y%m%d", time());
  395. if(!file_exists($configs['log']['path'] . "/" . $filepath))mkdir($configs['log']['path'] . "/" . $filepath, 0755);
  396. $filename = $configs['log']['path'] . "/" . $filepath . "/" . strftime("%Y%m%d%H", time()) . ".log";
  397. }
  398. $file = fopen($filename, "a");
  399. flock($file, LOCK_EX);
  400. fwrite($file, date('Y-m-d H:i:s') . ":" . $params['log']. "\n\n");
  401. flock($file, LOCK_UN);
  402. fclose($file);
  403. }
  404. ?>