base.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. <?php
  2. defined('ONLY_ONLY_ONLY') or exit('Access Denied');
  3. function crossdomain_cookie_init()
  4. {
  5. $maxlifetime = 0;
  6. $secure = true;
  7. $httponly = true;
  8. $samesite = 'None';
  9. if (PHP_VERSION_ID < 70300) {
  10. session_set_cookie_params($maxlifetime, '/; samesite=' . $samesite, $_SERVER['HTTP_HOST'], $secure, $httponly);
  11. } else {
  12. session_set_cookie_params([
  13. 'lifetime' => $maxlifetime,
  14. 'path' => '/',
  15. 'secure' => $secure,
  16. 'httponly' => $httponly,
  17. 'samesite' => $samesite,
  18. ]);
  19. }
  20. }
  21. function input_getPostObj()
  22. {
  23. $httpdata = file_get_contents("php://input");
  24. $ajaxdata = @json_decode($httpdata, true);
  25. if (empty($ajaxdata)) {
  26. parse_str($httpdata, $ajaxdata);
  27. }
  28. return $ajaxdata;
  29. }
  30. function base_stripslashes_deep($value)
  31. {
  32. $value = is_array($value) ? array_map('base_stripslashes_deep', $value) : stripslashes($value);
  33. return $value;
  34. }
  35. function base_irequestsplite($var)
  36. {
  37. if (is_array($var)) {
  38. foreach ($var as $key => $value) {
  39. $var[htmlspecialchars($key)] = base_irequestsplite($value);
  40. }
  41. } else {
  42. $var = str_replace('&amp;', '&', htmlspecialchars($var, ENT_QUOTES));
  43. }
  44. return $var;
  45. }
  46. function input_param_handle($preHandle = true)
  47. {
  48. $GET = $_GET;
  49. $POST = $_POST;
  50. $COOKIE = $_COOKIE;
  51. $REQUEST = $_REQUEST;
  52. if ($preHandle) {
  53. $GET = array_map('base_stripslashes_deep', $_GET);
  54. $POST = array_map('base_stripslashes_deep', $_POST);
  55. $COOKIE = array_map('base_stripslashes_deep', $_COOKIE);
  56. $REQUEST = array_map('base_stripslashes_deep', $_REQUEST);
  57. }
  58. $_GPC = array();
  59. $_GPC = array_merge($GET, $POST, $_GPC);
  60. if ($preHandle) {
  61. $_GPC = base_irequestsplite($_GPC);
  62. }
  63. return $_GPC;
  64. }
  65. function input_file_errhandle($filectl)
  66. {
  67. $fileerror = intval($filectl["error"]);
  68. if ($fileerror > 0) {
  69. switch ($fileerror) {
  70. case 1: //UPLOAD_ERR_INI_SIZE
  71. $err_msg = "上传的文件超过了 php.ini 中 upload_max_filesize选项限制的值";
  72. break;
  73. case 2: //UPLOAD_ERR_FORM_SIZE
  74. $err_msg = "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值";
  75. break;
  76. case 3: //UPLOAD_ERR_PARTIAL
  77. $err_msg = "文件只有部分被上传";
  78. break;
  79. case 4: //UPLOAD_ERR_NO_FILE
  80. $err_msg = "文件没有被上传";
  81. break;
  82. case 5: //UPLOAD_ERR_NO_TMP_DIR
  83. $err_msg = "找不到临时文件夹";
  84. break;
  85. case 6: //UPLOAD_ERR_CANT_WRITE
  86. $err_msg = "文件写入失败";
  87. break;
  88. default:
  89. $err_msg = "未知错误";
  90. break;
  91. }
  92. throw new Exception("上传失败:" . $err_msg);
  93. }
  94. }
  95. function input_file_upload($filectl_name, $subdir, $filename = "", $maxsize = 10 * 1024 * 1024, $filetypes = "jpg|png|jpeg|gif")
  96. {
  97. if (!isset($_FILES[$filectl_name])) {
  98. $GPC = input_param_handle(false);
  99. $ajaxdata = input_getPostObj();
  100. $tmpfilectl_name = isset($ajaxdata[$filectl_name]) ? $ajaxdata[$filectl_name] : $GPC[$filectl_name];
  101. if (empty($tmpfilectl_name) || !isset($_FILES[$tmpfilectl_name])) {
  102. throw new GeneralException("", "没有定位到文件控件或文件内容为空,请核查!!");
  103. }
  104. $filectl_name = $tmpfilectl_name;
  105. }
  106. $filectl = $_FILES[$filectl_name];
  107. input_file_errhandle($filectl);
  108. $filetype = $filectl["type"];
  109. $filesize = $filectl["size"];
  110. if ($filesize > $maxsize) {
  111. throw new GeneralException("", "上传失败:文件不能超过" . ($maxsize / 1024) . "KB,请您核查一下是否上传错了:)");
  112. }
  113. $fileex = pathinfo($filectl["name"])['extension'];
  114. if (empty($filetypes) || (mb_strpos($filetypes, strtolower($fileex) . "|") !== false)) {
  115. $dest_urldir = WEB_URL_FILEROOT . (empty($subdir) ? "" : "/" . $subdir);
  116. $dest_phydir = WEB_PHY_FILEROOT . (empty($subdir) ? "" : "/" . $subdir);
  117. is_dir($dest_phydir) or mkdir($dest_phydir, 0755, false);
  118. if (empty($filename)) {
  119. do {
  120. $filename = 'file_' . rand(123456, 9999) . "." . $fileex;
  121. $file_relative_path = $dest_urldir . "/" . $filename;
  122. $file_physical_path = $dest_phydir . "/" . $filename;
  123. } while (is_file($file_physical_path));
  124. } else {
  125. $file_relative_path = $dest_urldir . "/" . $filename;
  126. $file_physical_path = $dest_phydir . "/" . $filename;
  127. }
  128. $filetmp_name = $filectl["tmp_name"];
  129. if (move_uploaded_file($filetmp_name, $file_physical_path)) {
  130. } else {
  131. throw new GeneralException("", "图片上传保存时失败");
  132. }
  133. return $file_relative_path;
  134. } else {
  135. throw new GeneralException("", "上传失败:图片文件只能是" . $filetypes . "格式");
  136. }
  137. }
  138. /*****************/
  139. function random($length, $numeric = false)
  140. {
  141. $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  142. $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  143. if ($numeric) {
  144. $hash = '';
  145. } else {
  146. $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
  147. $length--;
  148. }
  149. $max = strlen($seed) - 1;
  150. for ($i = 0; $i < $length; $i++) {
  151. $hash .= $seed[mt_rand(0, $max)];
  152. }
  153. return $hash;
  154. }
  155. /******http通信处理******/
  156. function http_post($url, $header = array(), $post_data, $withpem = false, $client_cert_pem = '', $client_cert_key = '', $cert_pwd = '')
  157. {
  158. $ch = curl_init();
  159. curl_setopt($ch, CURLOPT_URL, $url);
  160. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  161. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  162. curl_setopt($ch, CURLOPT_TIMEOUT, 10); //10秒超时
  163. curl_setopt($ch, CURLOPT_POST, 1);
  164. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  165. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  166. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  167. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1');
  168. if ($withpem) { //证书相关
  169. curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
  170. curl_setopt($ch, CURLOPT_SSLCERT, '' . $client_cert_pem . '');
  171. curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
  172. curl_setopt($ch, CURLOPT_SSLKEY, '' . $client_cert_key . '');
  173. curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $cert_pwd);
  174. curl_setopt($ch, CURLOPT_KEYPASSWD, $cert_pwd);
  175. }
  176. $data = curl_exec($ch);
  177. $error_no = curl_errno($ch);
  178. $err_msg = "";
  179. curl_close($ch);
  180. if (!empty($error_no)) {
  181. $err_msg = http_curl_geterr($error_no);
  182. eeglobal_log_handler('http_post', 'error', "http_post通信异常 err_msg=>$err_msg result=>$data");
  183. throw new GeneralException("http_post", "http_post通信异常");
  184. }
  185. return $data;
  186. }
  187. function http_get($url, $header = array())
  188. {
  189. $ch = curl_init();
  190. curl_setopt($ch, CURLOPT_URL, $url);
  191. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  192. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  193. curl_setopt($ch, CURLOPT_HEADER, 0);
  194. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  195. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  196. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1');
  197. $data = curl_exec($ch);
  198. $error_no = curl_errno($ch);
  199. $err_msg = "";
  200. curl_close($ch);
  201. if (!empty($error_no)) {
  202. $err_msg = http_curl_geterr($error_no);
  203. eeglobal_log_handler('http_get', 'error', "http_get通信异常 err_msg=>$err_msg result=>$data");
  204. throw new GeneralException("http_get", 'http_get通信异常');
  205. }
  206. return $data;
  207. }
  208. function http_curl_geterr($no)
  209. {
  210. $error_codes = array(
  211. '1' => 'CURLE_UNSUPPORTED_PROTOCOL (1) – 您传送给 libcurl 的网址使用了此 libcurl 不支持的协议。 可能是您没有使用的编译时选项造成了这种情况(可能是协议字符串拼写有误,或没有指定协议 libcurl 代码)。',
  212. '2' => 'CURLE_FAILED_INIT (2) – 非常早期的初始化代码失败。 可能是内部错误或问题。',
  213. '3' => 'CURLE_URL_MALFORMAT (3) – 网址格式不正确。',
  214. '5' => 'CURLE_COULDNT_RESOLVE_PROXY (5) – 无法解析代理服务器。 指定的代理服务器主机无法解析。',
  215. '6' => 'CURLE_COULDNT_RESOLVE_HOST (6) – 无法解析主机。 指定的远程主机无法解析。',
  216. '7' => 'CURLE_COULDNT_CONNECT (7) – 无法通过 connect() 连接至主机或代理服务器。',
  217. '8' => 'CURLE_FTP_WEIRD_SERVER_REPLY (8) – 在连接到 FTP 服务器后,libcurl 需要收到特定的回复。 此错误代码表示收到了不正常或不正确的回复。 指定的远程服务器可能不是正确的 FTP 服务器。',
  218. '9' => 'CURLE_REMOTE_ACCESS_DENIED (9) – 我们无法访问网址中指定的资源。 对于 FTP,如果尝试更改为远程目录,就会发生这种情况。',
  219. '11' => 'CURLE_FTP_WEIRD_PASS_REPLY (11) – 在将 FTP 密码发送到服务器后,libcurl 需要收到正确的回复。 此错误代码表示返回的是意外的代码。',
  220. '13' => 'CURLE_FTP_WEIRD_PASV_REPLY (13) – libcurl 无法从服务器端收到有用的结果,作为对 PASV 或 EPSV 命令的响应。 服务器有问题。',
  221. '14' => 'CURLE_FTP_WEIRD_227_FORMAT (14) – FTP 服务器返回 227 行作为对 PASV 命令的响应。如果 libcurl 无法解析此行,就会返回此代码。',
  222. '15' => 'CURLE_FTP_CANT_GET_HOST (15) – 在查找用于新连接的主机时出现内部错误。',
  223. '17' => 'CURLE_FTP_COULDNT_SET_TYPE (17) – 在尝试将传输模式设置为二进制或 ascii 时发生错误。',
  224. '18' => 'CURLE_PARTIAL_FILE (18) – 文件传输尺寸小于或大于预期。当服务器先报告了一个预期的传输尺寸,然后所传送的数据与先前指定尺寸不相符时,就会发生此错误。',
  225. '19' => 'CURLE_FTP_COULDNT_RETR_FILE (19) – ‘RETR’ 命令收到了不正常的回复,或完成的传输尺寸为零字节。',
  226. '21' => 'CURLE_QUOTE_ERROR (21) – 在向远程服务器发送自定义 “QUOTE” 命令时,其中一个命令返回的错误代码为 400 或更大的数字(对于 FTP),或以其他方式表明命令无法成功完成。',
  227. '22' => 'CURLE_HTTP_RETURNED_ERROR (22) – 如果 CURLOPT_FAILONERROR 设置为 TRUE,且 HTTP 服务器返回 >= 400 的错误代码,就会返回此代码。 (此错误代码以前又称为 CURLE_HTTP_NOT_FOUND。)',
  228. '23' => 'CURLE_WRITE_ERROR (23) – 在向本地文件写入所收到的数据时发生错误,或由写入回调 (write callback) 向 libcurl 返回了一个错误。',
  229. '25' => 'CURLE_UPLOAD_FAILED (25) – 无法开始上传。 对于 FTP,服务器通常会拒绝执行 STOR 命令。错误缓冲区通常会提供服务器对此问题的说明。 (此错误代码以前又称为 CURLE_FTP_COULDNT_STOR_FILE。)',
  230. '26' => 'CURLE_READ_ERROR (26) – 读取本地文件时遇到问题,或由读取回调 (read callback) 返回了一个错误。',
  231. '27' => 'CURLE_OUT_OF_MEMORY (27) – 内存分配请求失败。此错误比较严重,若发生此错误,则表明出现了非常严重的问题。',
  232. '28' => 'CURLE_OPERATION_TIMEDOUT (28) – 操作超时。 已达到根据相应情况指定的超时时间。',
  233. '30' => 'CURLE_FTP_PORT_FAILED (30) – FTP PORT 命令返回错误。 在没有为 libcurl 指定适当的地址使用时,最有可能发生此问题。 请参阅 CURLOPT_FTPPORT。',
  234. '31' => 'CURLE_FTP_COULDNT_USE_REST (31) – FTP REST 命令返回错误。如果服务器正常,则应当不会发生这种情况。',
  235. '33' => 'CURLE_RANGE_ERROR (33) – 服务器不支持或不接受范围请求。',
  236. '34' => 'CURLE_HTTP_POST_ERROR (34) – 此问题比较少见,主要由内部混乱引发。',
  237. '35' => 'CURLE_SSL_CONNECT_ERROR (35) – 同时使用 SSL/TLS 时可能会发生此错误。您可以访问错误缓冲区查看相应信息,其中会对此问题进行更详细的介绍。可能是证书(文件格式、路径、许可)、密码及其他因素导致了此问题。',
  238. '36' => 'CURLE_FTP_BAD_DOWNLOAD_RESUME (36) – 尝试恢复超过文件大小限制的 FTP 连接。',
  239. '37' => 'CURLE_FILE_COULDNT_READ_FILE (37) – 无法打开 FILE:// 路径下的文件。原因很可能是文件路径无法识别现有文件。 建议您检查文件的访问权限。',
  240. '38' => 'CURLE_LDAP_CANNOT_BIND (38) – LDAP 无法绑定。LDAP 绑定操作失败。',
  241. '39' => 'CURLE_LDAP_SEARCH_FAILED (39) – LDAP 搜索无法进行。',
  242. '41' => 'CURLE_FUNCTION_NOT_FOUND (41) – 找不到函数。 找不到必要的 zlib 函数。',
  243. '42' => 'CURLE_ABORTED_BY_CALLBACK (42) – 由回调中止。 回调向 libcurl 返回了 “abort”。',
  244. '43' => 'CURLE_BAD_FUNCTION_ARGUMENT (43) – 内部错误。 使用了不正确的参数调用函数。',
  245. '45' => 'CURLE_INTERFACE_FAILED (45) – 界面错误。 指定的外部界面无法使用。 请通过 CURLOPT_INTERFACE 设置要使用哪个界面来处理外部连接的来源 IP 地址。 (此错误代码以前又称为 CURLE_HTTP_PORT_FAILED。)',
  246. '47' => 'CURLE_TOO_MANY_REDIRECTS (47) – 重定向过多。 进行重定向时,libcurl 达到了网页点击上限。请使用 CURLOPT_MAXREDIRS 设置上限。',
  247. '48' => 'CURLE_UNKNOWN_TELNET_OPTION (48) – 无法识别以 CURLOPT_TELNETOPTIONS 设置的选项。 请参阅相关文档。',
  248. '49' => 'CURLE_TELNET_OPTION_SYNTAX (49) – telnet 选项字符串的格式不正确。',
  249. '51' => 'CURLE_PEER_FAILED_VERIFICATION (51) – 远程服务器的 SSL 证书或 SSH md5 指纹不正确。',
  250. '52' => 'CURLE_GOT_NOTHING (52) – 服务器未返回任何数据,在相应情况下,未返回任何数据就属于出现错误。',
  251. '53' => 'CURLE_SSL_ENGINE_NOTFOUND (53) – 找不到指定的加密引擎。',
  252. '54' => 'CURLE_SSL_ENGINE_SETFAILED (54) – 无法将选定的 SSL 加密引擎设为默认选项。',
  253. '55' => 'CURLE_SEND_ERROR (55) – 无法发送网络数据。',
  254. '56' => 'CURLE_RECV_ERROR (56) – 接收网络数据失败。',
  255. '58' => 'CURLE_SSL_CERTPROBLEM (58) – 本地客户端证书有问题',
  256. '59' => 'CURLE_SSL_CIPHER (59) – 无法使用指定的密钥',
  257. '60' => 'CURLE_SSL_CACERT (60) – 无法使用已知的 CA 证书验证对等证书',
  258. '61' => 'CURLE_BAD_CONTENT_ENCODING (61) – 无法识别传输编码',
  259. '62' => 'CURLE_LDAP_INVALID_URL (62) – LDAP 网址无效',
  260. '63' => 'CURLE_FILESIZE_EXCEEDED (63) – 超过了文件大小上限',
  261. '64' => 'CURLE_USE_SSL_FAILED (64) – 请求的 FTP SSL 级别失败',
  262. '65' => 'CURLE_SEND_FAIL_REWIND (65) – 进行发送操作时,curl 必须回转数据以便重新传输,但回转操作未能成功',
  263. '66' => 'CURLE_SSL_ENGINE_INITFAILED (66) – SSL 引擎初始化失败',
  264. '67' => 'CURLE_LOGIN_DENIED (67) – 远程服务器拒绝 curl 登录(7.13.1 新增功能)',
  265. '68' => 'CURLE_TFTP_NOTFOUND (68) – 在 TFTP 服务器上找不到文件',
  266. '69' => 'CURLE_TFTP_PERM (69) – 在 TFTP 服务器上遇到权限问题',
  267. '70' => 'CURLE_REMOTE_DISK_FULL (70) – 服务器磁盘空间不足',
  268. '71' => 'CURLE_TFTP_ILLEGAL (71) – TFTP 操作非法',
  269. '72' => 'CURLE_TFTP_UNKNOWNID (72) – TFTP 传输 ID 未知',
  270. '73' => 'CURLE_REMOTE_FILE_EXISTS (73) – 文件已存在,无法覆盖',
  271. '74' => 'CURLE_TFTP_NOSUCHUSER (74) – 运行正常的 TFTP 服务器不会返回此错误',
  272. '75' => 'CURLE_CONV_FAILED (75) – 字符转换失败',
  273. '76' => 'CURLE_CONV_REQD (76) – 调用方必须注册转换回调',
  274. '77' => 'CURLE_SSL_CACERT_BADFILE (77) – 读取 SSL CA 证书时遇到问题(可能是路径错误或访问权限问题)',
  275. '78' => 'CURLE_REMOTE_FILE_NOT_FOUND (78) – 网址中引用的资源不存在',
  276. '79' => 'CURLE_SSH (79) – SSH 会话中发生无法识别的错误',
  277. '80' => 'CURLE_SSL_SHUTDOWN_FAILED (80) – 无法终止 SSL 连接',
  278. '81' => 'CURLE_AGAIN---Socket是没有准备好发送/接收等待,直到它准备好了,然后再试一次。',
  279. '82' => 'CURLE_SSL_CRL_BADFILE---无法加载CRL文件(在7.19.0版加入)',
  280. '83' => 'CURLE_SSL_ISSUER_ERROR---发行人检查失败(在7.19.0版加入)',
  281. '84' => 'CURLE_FTP_PRET_FAILED---FTP服务器不理解的PRET命令,所有不支持给定的参数。要小心时usingCURLOPT_CUSTOMREQUEST,自定义列表“命令将发送PRET CMD前PASV以及。',
  282. '85' => 'CURLE_RTSP_CSEQ_ERROR---RTSP的Cseq号码不匹配。',
  283. '86' => 'CURLE_RTSP_SESSION_ERROR---RTSP会话标识符不匹配。',
  284. '87' => 'CURLE_FTP_BAD_FILE_LIST--无法,解析FTP文件列表(在FTP通配符下载)。',
  285. '88' => 'CURLE_CHUNK_FAILED--块回调报告错误。',
  286. );
  287. if (isset($error_codes[$no])) {
  288. return $error_codes[$no];
  289. } else {
  290. return '通信异常=>' . $no;
  291. }
  292. }
  293. function oshandle_mkdirs($path)
  294. {
  295. if (!is_dir($path)) {
  296. oshandle_mkdirs(dirname($path));
  297. if (!empty($path)) {
  298. mkdir($path);
  299. }
  300. }
  301. return is_dir($path);
  302. }
  303. class global_process2lock
  304. {
  305. public static $flockSet = array();
  306. public static function lock($fp)
  307. {
  308. if (empty($fp)) {
  309. throw new Exception("锁文件参数为空!");
  310. }
  311. if (!file_exists($fp)) {
  312. throw new Exception("锁文件路径【" . $fp . "】不存在!");
  313. }
  314. global_process2lock::$flockSet[$fp] = fopen($fp, 'r');
  315. if (empty(global_process2lock::$flockSet[$fp])) {
  316. throw new Exception(" global_process2lock 未能打开文件 fp=>" . $fp);
  317. }
  318. $flockflag = flock(global_process2lock::$flockSet[$fp], LOCK_EX);
  319. if (empty($flockflag)) {
  320. throw new Exception(" global_process2lock flock没有能够加上LOCK_EX独占阻塞锁 fp=>" . $fp);
  321. }
  322. return $flockflag;
  323. }
  324. public static function unlock($fp)
  325. {
  326. try {
  327. if (empty(global_process2lock::$flockSet[$fp])) {
  328. throw new Exception(" global_process2lock flock解锁LOCK_UN未成功 fp为空");
  329. }
  330. $resultA = flock(global_process2lock::$flockSet[$fp], LOCK_UN);
  331. if (empty($resultA)) {
  332. throw new Exception(" global_process2lock flock解锁LOCK_UN未成功 fp=>" . $fp);
  333. }
  334. $resultB = fclose(global_process2lock::$flockSet[$fp]);
  335. if (empty($resultB)) {
  336. throw new Exception(" global_process2lock fclose释放文件锁未成功 fp=>" . $fp);
  337. }
  338. } catch (Throwable $ex) {
  339. if (true) {
  340. eeglobal_log_handler("global_process2lock", 'ERROR', " global_process2lock fp=>" . $fp . " 释放锁时异常=>" . $ex->getMessage());
  341. }
  342. }
  343. }
  344. }
  345. function filelock_handle($lockCatalog, $lockId, $handleArgs, $bizHandle)
  346. {
  347. $logCatalog = "filelock_handle";
  348. eeglobal_log_handler($logCatalog, "debug", "enter lockCatalog=$lockCatalog lockId=$lockId");
  349. if (empty($lockCatalog)) {
  350. throw new GeneralException("", "lockCatalog文件锁类别不能为空!");
  351. }
  352. $lockId = intval($lockId);
  353. if ($lockId == 0) {
  354. $lockId = "all";
  355. }
  356. //为空则为此业务类别的全局锁,非记录锁
  357. $FLCK_PHY_ROOT = WEB_PHY_FILEROOT . "/FILELOCK_ROOT";
  358. $lockFile = $FLCK_PHY_ROOT . "/$lockCatalog/FL_$lockId.flk";
  359. $destPhyPath = dirname($lockFile);
  360. try {
  361. if (!is_dir($destPhyPath)) {
  362. if (!mkdir($destPhyPath, 0777, true)) {
  363. throw new Exception("锁相关目录时发生错误!");
  364. }
  365. chmod($destPhyPath, 0777);
  366. }
  367. if (!file_exists($lockFile)) {
  368. if (!fopen($lockFile, "w")) {
  369. throw new Exception("创建lockId文件锁文件失败!");
  370. }
  371. }
  372. } catch (Throwable $e) {
  373. throw new GeneralException("", "创建lockId文件锁相关目录及文件时发生错误!" . $e->getMessage());
  374. }
  375. try {
  376. eeglobal_log_handler($logCatalog, "debug", "lockbefore lockCatalog=$lockCatalog lockId=$lockId");
  377. global_process2lock::lock($lockFile);
  378. eeglobal_log_handler($logCatalog, "debug", "lockafter lockCatalog=$lockCatalog lockId=$lockId");
  379. return $bizHandle($handleArgs);
  380. } catch (Throwable $ex) {
  381. throw $ex;
  382. } finally {
  383. eeglobal_log_handler($logCatalog, "debug", "unlockbefore lockCatalog=$lockCatalog lockId=$lockId");
  384. global_process2lock::unlock($lockFile);
  385. eeglobal_log_handler($logCatalog, "debug", "unlockafter lockCatalog=$lockCatalog lockId=$lockId");
  386. }
  387. }
  388. function basecfg_getKey($subSystem, $groupKey)
  389. {
  390. if (empty($groupKey)) {
  391. throw new GeneralException("", "groupKey不能为空");
  392. }
  393. if (empty(trim($subSystem))) {
  394. $subSystem = "GC";
  395. }
  396. $groupKeyALL = trim($subSystem . "_" . $groupKey);
  397. if (empty($groupKeyALL)) {
  398. throw new GeneralException("", "groupKeyALL不能为空");
  399. }
  400. $groupKeyCache = "GGCONFIG_$groupKeyALL";
  401. return array(
  402. "groupKeyALL" => $groupKeyALL,
  403. "groupKeyCache" => $groupKeyCache,
  404. );
  405. }
  406. function basecfg_getConfig($subSystem, $groupKey)
  407. {
  408. $keyInfo = basecfg_getKey($subSystem, $groupKey);
  409. $groupKeyALL = $keyInfo["groupKeyALL"];
  410. $groupKeyCache = $keyInfo["groupKeyCache"];
  411. if (!empty($GLOBALS[$groupKeyCache])) {
  412. return $GLOBALS[$groupKeyCache];
  413. }
  414. $system_config_cache = pdo_fetch("select * from base_config where `group`=:group and `name`='system_config_cache'", array(':group' => $groupKeyALL));
  415. if (empty($system_config_cache['value'])) {
  416. $config = array();
  417. $configdata = pdo_fetchall("select * from base_config where `group`=:group", array(':group' => $groupKeyALL));
  418. foreach ($configdata as $item) {
  419. $cvalue = $item['value'];
  420. if (mb_strpos($item['value'], "YPWYPWARRAY") !== false) {
  421. $cvalue = mb_ereg_replace("YPWYPWARRAY", "", $item['value']);
  422. $cvalue = json_decode($cvalue, true);
  423. }
  424. $config[$item['name']] = $cvalue;
  425. }
  426. if (!empty($system_config_cache['name'])) {
  427. pdo_update('base_config', array('value' => serialize($config)), array('name' => 'system_config_cache', 'group' => $groupKeyALL));
  428. } else {
  429. pdo_insert('base_config', array('name' => 'system_config_cache', 'value' => serialize($config), 'group' => $groupKeyALL));
  430. }
  431. $GLOBALS[$groupKeyCache] = $config;
  432. return $GLOBALS[$groupKeyCache];
  433. } else {
  434. $GLOBALS[$groupKeyCache] = unserialize($system_config_cache['value']);
  435. return $GLOBALS[$groupKeyCache];
  436. }
  437. }
  438. function basecfg_setConfig($subSystem, $groupKey, $arrays, $needClear = false)
  439. {
  440. $keyInfo = basecfg_getKey($subSystem, $groupKey);
  441. $groupKeyALL = $keyInfo["groupKeyALL"];
  442. $groupKeyCache = $keyInfo["groupKeyCache"];
  443. if (empty($arrays) || !is_array($arrays) || count($arrays) <= 0) {
  444. throw new GeneralException("", "basecfg_setConfig的参数arrays不能为空");
  445. }
  446. $sqlTemp = "";
  447. $sqlParam = array();
  448. $sqlParam[":group"] = $groupKeyALL;
  449. if ($needClear == true) {
  450. $sqlTemp .= " delete from base_config where `group`=:group; ";
  451. }
  452. $index = 0;
  453. foreach ($arrays as $cname => $cvalue) {
  454. $index++;
  455. $cnamePN = ":P{$index}_cname";
  456. $cvaluePN = ":P{$index}_cvalue";
  457. $sqlParam[$cnamePN] = $cname;
  458. $sqlParam[$cvaluePN] = (!is_array($cvalue) ? $cvalue : "YPWYPWARRAY" . json_encode($cvalue));
  459. $sqlTemp .= " set @handle_id='0'; "; //根据条件查询是否存在对应记录limit 1
  460. $sqlTemp .= " select @handle_id:=`name` from base_config where `group`=:group and `name`=$cnamePN limit 1;";
  461. //基于查询结果作为条件的dual虚表进行关联插入
  462. $sqlTemp .= " insert into base_config ( `group`,`name`,`value` )";
  463. $sqlTemp .= " select :group,$cnamePN,$cvaluePN from dual where ifnull(@handle_id,'0')='0' ;";
  464. //候补更新,可能为空更新
  465. $sqlTemp .= " update base_config SET `value`=$cvaluePN where ifnull(@handle_id,'0')<>'0' and `group`=:group and `name`=$cnamePN;";
  466. }
  467. $sqlTemp .= " update base_config SET `value`='' where `group`=:group and `name`='system_config_cache'; ";
  468. pdo_query3($sqlTemp, $sqlParam);
  469. unset($GLOBALS[$groupKeyCache]); //清理同一进程内可共享缓存
  470. }
  471. /**序列化相关**/
  472. function base_iserializer($value)
  473. {
  474. return serialize($value);
  475. }
  476. function base_iunserializer($value)
  477. {
  478. if (empty($value)) {
  479. return '';
  480. }
  481. if (!base_isserialized($value)) {
  482. return $value;
  483. }
  484. $result = unserialize($value);
  485. if ($result === false) {
  486. $temp = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $value);
  487. return unserialize($temp);
  488. }
  489. return $result;
  490. }
  491. function base_isserialized($data, $strict = true)
  492. {
  493. if (!is_string($data)) {
  494. return false;
  495. }
  496. $data = trim($data);
  497. if ('N;' == $data) {
  498. return true;
  499. }
  500. if (strlen($data) < 4) {
  501. return false;
  502. }
  503. if (':' !== $data[1]) {
  504. return false;
  505. }
  506. if ($strict) {
  507. $lastc = substr($data, -1);
  508. if (';' !== $lastc && '}' !== $lastc) {
  509. return false;
  510. }
  511. } else {
  512. $semicolon = strpos($data, ';');
  513. $brace = strpos($data, '}');
  514. if (false === $semicolon && false === $brace) {
  515. return false;
  516. }
  517. if (false !== $semicolon && $semicolon < 3) {
  518. return false;
  519. }
  520. if (false !== $brace && $brace < 4) {
  521. return false;
  522. }
  523. }
  524. $token = $data[0];
  525. switch ($token) {
  526. case 's':
  527. if ($strict) {
  528. if ('"' !== substr($data, -2, 1)) {
  529. return false;
  530. }
  531. } elseif (false === strpos($data, '"')) {
  532. return false;
  533. }
  534. case 'a':
  535. case 'O':
  536. return (bool) preg_match("/^{$token}:[0-9]+:/s", $data);
  537. case 'b':
  538. case 'i':
  539. case 'd':
  540. $end = $strict ? '$' : '';
  541. return (bool) preg_match("/^{$token}:[0-9.E-]+;$end/", $data);
  542. }
  543. return false;
  544. }
  545. function imghandle_buildAllUrl($oldUrl)
  546. {
  547. if (empty($oldUrl)) {
  548. return '';
  549. }
  550. $newUrl = $oldUrl;
  551. if (mb_strpos(strtolower($oldUrl), "http") !== 0) {
  552. $newUrl = WEBSITE_ROOT . $oldUrl;
  553. }
  554. return $newUrl;
  555. }
  556. function imghandle_createImage($imgurl)
  557. {
  558. $resp = http_get(imghandle_buildAllUrl($imgurl));
  559. return imagecreatefromstring($resp);
  560. }
  561. function imghandle_mergeImage($target, $data, $imgurl)
  562. {
  563. $img = imghandle_createImage($imgurl);
  564. $w = imagesx($img);
  565. $h = imagesy($img);
  566. if (intval($data['width']) <= 0) {
  567. $data['width'] = $w;
  568. }
  569. if (intval($data['height']) <= 0) {
  570. $data['height'] = $w;
  571. }
  572. imagecopyresized($target, $img, $data['left'], $data['top'], 0, 0, $data['width'], $data['height'], $w, $h);
  573. imagedestroy($img);
  574. return $target;
  575. }
  576. function imghandle_mergeText($target, $data, $text, $fontFileName)
  577. {
  578. $font = WEB_PHY_ASSETROOT . "/font/$fontFileName.ttf";
  579. if (!file_exists($font)) {
  580. $font = WEB_PHY_ASSETROOT . "/font/msyh.ttf";
  581. }
  582. if (intval($data['size']) <= 0) {
  583. $data['size'] = 12;
  584. }
  585. $colors = imghandle_hex2rgb($data['color']);
  586. if (empty($colors)) {
  587. $colors = array('red' => 0, 'green' => 0, 'blue' => 0);
  588. }
  589. $color = imagecolorallocate($target, $colors['red'], $colors['green'], $colors['blue']);
  590. if (intval($data['width']) <= 0) {
  591. $data['width'] = 50;
  592. }
  593. mb_internal_encoding("UTF-8"); //设置编码
  594. $content = "";
  595. // 将字符串拆分成一个个单字 保存到数组 letter 中
  596. for ($i = 0; $i < mb_strlen($text); $i++) {
  597. $letter[] = mb_substr($text, $i, 1);
  598. }
  599. foreach ($letter as $l) {
  600. $teststr = $content . " " . $l;
  601. $fontBox = imagettfbbox($data['size'], 0, $font, $teststr);
  602. // 判断拼接后的字符串是否超过预设的宽度
  603. if (($fontBox[2] > intval($data['width'])) && ($content !== "")) {
  604. $content .= "\n";
  605. }
  606. $content .= $l;
  607. }
  608. imagettftext($target, $data['size'], 0, $data['left'], $data['top'] + $data['size'], $color, $font, $content);
  609. return $target;
  610. }
  611. function imghandle_hex2rgb($colour)
  612. {
  613. if ($colour[0] == '#') {
  614. $colour = substr($colour, 1);
  615. }
  616. if (strlen($colour) == 6) {
  617. list($r, $g, $b) = array(
  618. $colour[0] . $colour[1],
  619. $colour[2] . $colour[3],
  620. $colour[4] . $colour[5],
  621. );
  622. } elseif (strlen($colour) == 3) {
  623. list($r, $g, $b) = array(
  624. $colour[0] . $colour[0],
  625. $colour[1] . $colour[1],
  626. $colour[2] . $colour[2],
  627. );
  628. } else {
  629. return false;
  630. }
  631. $r = hexdec($r);
  632. $g = hexdec($g);
  633. $b = hexdec($b);
  634. return array(
  635. 'red' => $r,
  636. 'green' => $g,
  637. 'blue' => $b,
  638. );
  639. }
  640. function poster_build_pre($bizCatalog, $bizid, $userid, $poster, $saveSubPath, $cbQR, $cbQrMina)
  641. {
  642. $bizCatalog = intval($bizCatalog);
  643. $bizid = intval($bizid);
  644. $userid = intval($userid);
  645. foreach ($poster["data"] as &$d) {
  646. if ($d['type'] == 'qr') {
  647. $qr_imgtype = $d["imgtype"] == "png" ? "png" : "jpeg";
  648. $qr_md5 = md5(json_encode($d));
  649. $qr_fileName = "{$bizCatalog}_{$bizid}_{$userid}_$qr_md5.{$qr_imgtype}";
  650. $bizData = $cbQR($bizCatalog, $bizid, $userid, $d);
  651. $qrEmbedData = $bizData["qrEmbedData"];
  652. $qrSubPath = $bizData["qrSubPath"];
  653. $qr_pathPath = "$qrSubPath/$qr_fileName";
  654. $qr_urlPath = WEB_URL_FILEROOT . "/$qr_pathPath";
  655. $qr_phyPath = WEB_PHY_FILEROOT . "/$qr_pathPath";
  656. $destPhyPath = dirname($qr_phyPath);
  657. if (!is_dir($destPhyPath)) {
  658. oshandle_mkdirs($destPhyPath);
  659. }
  660. if (!file_exists($qr_phyPath)) {
  661. require_once WEB_PHY_ROOT . "/base/lib_qr.php";
  662. $d['margin'] = intval(str_replace('px', '', $d['margin']));
  663. $d['size'] = intval(str_replace('px', '', $d['size']));
  664. lib_qrbuild_core($qr_imgtype, $qrEmbedData, $qr_phyPath, $d['size'], $d['margin']
  665. , null, null, null, null, $d["logpath"]);
  666. }
  667. $d['src'] = $qr_urlPath;
  668. $d['type'] = "img";
  669. } else if ($d['type'] == 'qrmina') {
  670. $qrmina_imgtype = $d["imgtype"] == "png" ? "png" : "jpeg";
  671. $qrmina_md5 = md5(json_encode($d));
  672. $qrmina_fileName = "{$bizCatalog}_{$bizid}_{$userid}_$qrmina_md5.{$qrmina_imgtype}";
  673. $bizData = $cbQrMina($bizCatalog, $bizid, $userid, $d);
  674. $qrminaSubPath = $bizData["qrminaSubPath"];
  675. $qrminaAppId = $bizData["qrminaAppId"];
  676. $qrminaAppSecret = $bizData["qrminaAppSecret"];
  677. $qrminaPagePath = $bizData["qrminaPagePath"];
  678. $qrmina_pathPath = "$qrminaSubPath/$qrmina_fileName";
  679. $qrmina_urlPath = WEB_URL_FILEROOT . "/$qrmina_pathPath";
  680. $qrmina_phyPath = WEB_PHY_FILEROOT . "/$qrmina_pathPath";
  681. $destPhyPath = dirname($qrmina_phyPath);
  682. if (!is_dir($destPhyPath)) {
  683. oshandle_mkdirs($destPhyPath);
  684. }
  685. if (!file_exists($qrmina_phyPath)) {
  686. require_once WEB_PHY_ROOT . "/base/wxsrv.php";
  687. $d['size'] = intval(str_replace('px', '', $d['size']));
  688. weixin_getMinaQCode($qrminaAppId, $qrminaAppSecret, $qrmina_phyPath,
  689. $bizCatalog, $bizid, $userid, $qrminaPagePath, $d['size']);
  690. }
  691. $d['src'] = $qrmina_urlPath;
  692. $d['type'] = "img";
  693. }
  694. }
  695. $poster_md5 = md5(json_encode(array(
  696. 'version' => 1,
  697. 'catalog' => $bizCatalog,
  698. 'bizid' => $bizid,
  699. 'userid' => $userid,
  700. 'width' => $poster["width"],
  701. 'height' => $poster["height"],
  702. 'bg' => $poster["bg"],
  703. 'data' => $poster["data"],
  704. 'imgtype' => $poster["imgtype"],
  705. )));
  706. $poster_fileName = "{$bizCatalog}_{$bizid}_{$userid}_$poster_md5.{$poster["imgtype"]}";
  707. $poster_pathPath = "$saveSubPath/$poster_fileName";
  708. $poster_phyPath = WEB_PHY_FILEROOT . "/$poster_pathPath";
  709. $destPhyPath = dirname($poster_phyPath);
  710. if (!is_dir($destPhyPath)) {
  711. oshandle_mkdirs($destPhyPath);
  712. }
  713. if (!file_exists($poster_phyPath)) {
  714. poster_build($poster["width"], $poster["height"], $poster["bg"], $poster["data"], $poster_phyPath, $poster["imgtype"]);
  715. }
  716. return WEB_URL_FILEROOT . "/$poster_pathPath";
  717. }
  718. function poster_build($width, $height, $bgPicPath, $data, $savePath, $saveType = "jpg")
  719. {
  720. set_time_limit(0);
  721. @ini_set('memory_limit', '256M');
  722. $width = intval($width);
  723. if ($width < 10) {
  724. $width = 10;
  725. }
  726. if ($width > 1240) {
  727. $width = 1240;
  728. }
  729. $height = intval($height);
  730. if ($height < 10) {
  731. $height = 10;
  732. }
  733. if ($height > 1624) {
  734. $height = 1624;
  735. }
  736. $target = imagecreatetruecolor($width, $height);
  737. if (!empty($bgPicPath)) {
  738. $bg = imghandle_createImage($bgPicPath);
  739. imagecopy($target, $bg, 0, 0, 0, 0, $width, $height);
  740. imagedestroy($bg);
  741. }
  742. foreach ($data as $d) {
  743. $d['left'] = intval(str_replace('px', '', $d['left']));
  744. $d['top'] = intval(str_replace('px', '', $d['top']));
  745. $d['width'] = intval(str_replace('px', '', $d['width']));
  746. $d['height'] = intval(str_replace('px', '', $d['height']));
  747. $d['size'] = intval(str_replace('px', '', $d['size']));
  748. $d['src'] = imghandle_buildAllUrl($d['src']);
  749. if ($d['type'] == 'img') {
  750. $target = imghandle_mergeImage($target, $d, $d['src']);
  751. } else if ($d['type'] == 'text') {
  752. if ($d['width'] <= 0) {
  753. $d['width'] = $width;
  754. }
  755. $target = imghandle_mergeText($target, $d, $d['text'], $d['font']);
  756. }
  757. }
  758. $saveType == "jpg" ? imagejpeg($target, $savePath) : imagepng($target, $savePath);
  759. imagedestroy($target);
  760. }
  761. function base_putForendErrLog($forendtype)
  762. {
  763. $GPC = input_param_handle(false);
  764. $ajaxdata = input_getPostObj();
  765. $forendid = isset($ajaxdata["forendid"]) ? $ajaxdata["forendid"] : $GPC["forendid"];
  766. if (empty(trim($forendid))) {
  767. $forendid = "无";
  768. }
  769. $catalog = isset($ajaxdata["catalog"]) ? $ajaxdata["catalog"] : $GPC["catalog"];
  770. if (empty(trim($catalog))) {
  771. $catalog = "无";
  772. }
  773. $content = isset($ajaxdata["content"]) ? $ajaxdata["content"] : $GPC["content"];
  774. if (empty(trim($content))) {
  775. $content = "没有传递content,内容为空";
  776. }
  777. pdo_insert("base_forend_errlog", array(
  778. "createtime" => time(),
  779. "forendtype" => $forendtype,
  780. "forendid" => $forendid,
  781. "errcatalog" => $catalog,
  782. "errcontent" => $content,
  783. ));
  784. }
  785. function base_buildForendToken($userInfo)
  786. {
  787. $stoptime = time() + 3600 * 24 * 15;
  788. $userInfo["salt"] = random(8);
  789. pdo_query("update base_user set salt=:salt where id=:id;", array(
  790. ":salt" => $userInfo["salt"], ":id" => $userInfo["id"],
  791. ));
  792. switch (FOREND_TOKEN_MODE) {
  793. default:
  794. $nowsign = base_getforendTokenSign($userInfo, $stoptime);
  795. return FOREND_TOKEN_MODE . "_$stoptime$nowsign";
  796. break;
  797. }
  798. }
  799. function base_getforendTokenSign($userInfo, $stoptime)
  800. {
  801. return md5(trim($stoptime) . "_" . trim($userInfo["id"]) . "_" . trim($userInfo["createtime"]) . "_" . trim($userInfo["salt"]));
  802. }
  803. function base_verifyForendToken($userInfo)
  804. {
  805. $GPC = input_param_handle(false);
  806. $ajaxdata = input_getPostObj();
  807. $token = isset($ajaxdata[FOREND_TOKEN_NAME]) ? $ajaxdata[FOREND_TOKEN_NAME] : $GPC[FOREND_TOKEN_NAME];
  808. if (empty(trim($token))) {
  809. throw new GeneralException("", "没有传递前端会话Token的参数[ " . FOREND_TOKEN_NAME . " ]或参数为空!");
  810. }
  811. $token = mb_ereg_replace("_", "", $token);
  812. switch (intval($token[0])) { //0-md5/sha1; 1-对称加密; 2-非对称加密
  813. default:
  814. $stoptime = mb_substr($token, 1, 10);
  815. if (time() > intval($stoptime)) {
  816. base_verifyForendTokenFail("会话令牌已过期,请重新登录验证以获取新令牌!");
  817. }
  818. $oldsign = mb_substr($token, 11);
  819. $nowsign = base_getforendTokenSign($userInfo, $stoptime);
  820. if ($oldsign != $nowsign) {
  821. base_verifyForendTokenFail("会话令牌不匹配,请重新登录验证以获取新令牌!");
  822. }
  823. break;
  824. }
  825. return true;
  826. }
  827. function base_verifyForendTokenFail($attachMsg = "会话令牌无效,请重新登录验证以获取新令牌!")
  828. {
  829. $ajaxRes = new AjaxResult;
  830. $ajaxRes->ErrMsg = FOREND_TOKEN_ERRMSGFLAG . $attachMsg;
  831. ob_clean();
  832. ob_start();
  833. header('Content-Type:application/json;charset=UTF-8');
  834. echo @json_encode($ajaxRes);
  835. exit;
  836. }
  837. function base_loginByPhone($subSystem)
  838. {
  839. $GPC = input_param_handle(false);
  840. $ajaxdata = input_getPostObj();
  841. $phone = isset($ajaxdata["phone"]) ? $ajaxdata["phone"] : $GPC["phone"];
  842. if (empty($phone)) {
  843. throw new GeneralException("", "没有传递必要的phone参数");
  844. }
  845. if (!preg_match("/^1\d{10,10}$/", trim($phone))) {
  846. throw new GeneralException("", "传递的不是正确的手机号");
  847. }
  848. $code = isset($ajaxdata["code"]) ? $ajaxdata["code"] : $GPC["code"];
  849. if ($phone == "18920787806" && (intval($subSystem) == 1)) {
  850. $code = "123456";
  851. }
  852. //会员侧测试锁码,用于测试及第三方验证
  853. if ($phone == "13920062668" && (intval($subSystem) == 2)) {
  854. $code = "687206";
  855. }
  856. //商家侧测试锁码,用于测试及第三方验证
  857. if (empty(trim($code))) {
  858. $code = rand(123456, 654321);
  859. /***UPSERT手机用户记录**start***/
  860. $sqlTemp = "";
  861. $sqlParam = array();
  862. $sqlParam[":nowtime"] = time();
  863. $sqlParam[":subsystem"] = intval($subSystem);
  864. $sqlParam[":mobile"] = trim($phone);
  865. $sqlParam[":smscode"] = $code;
  866. $sqlParam[":salt"] = random(8);
  867. $sqlTemp .= " set @handle_id='0'; ";
  868. $sqlTemp .= " select @handle_id:=`mobile` from base_user where subsystem=:subsystem and `mobile`=:mobile limit 1;";
  869. $sqlTemp .= " insert into base_user ( `createtime`,`subsystem`,`mobile`,`smscode`,`salt` )";
  870. $sqlTemp .= " select :nowtime ,:subsystem ,:mobile ,:smscode ,:salt from dual where ifnull(@handle_id,'0')='0' ;";
  871. $sqlTemp .= " update base_user SET `updatetime`=:nowtime,`smscode`=:smscode where ifnull(@handle_id,'0')<>'0' and subsystem=:subsystem and `mobile`=:mobile;";
  872. pdo_query3($sqlTemp, $sqlParam);
  873. /***UPSERT手机用户记录**finish***/
  874. include_once WEB_PHY_ROOT . "/base/smslib.php"; //发短信阿里
  875. $sended = alisms_sendSms($phone, AliSMS_CODE_TMPCODE, AliSMS_CODE_TMPSIGN, array("code" => $code));
  876. return true;
  877. } else {
  878. $baseUser = pdo_fetch("select * from base_user where subsystem=:subsystem and `mobile`=:mobile limit 1;", array(
  879. ":subsystem" => intval($subSystem),
  880. ":mobile" => trim($phone),
  881. ));
  882. if (empty($baseUser)) {
  883. throw new GeneralException("", "没有定位到此手机号的用户记录,请先不带code参数方式调用此接口先获取验证码并预构建手机用户记录!");
  884. }
  885. if (trim($baseUser["smscode"]) != trim($code)) {
  886. throw new GeneralException("", "输入的短信验证码不正确,或者已过期!");
  887. } else { //生成并返给前端会话token
  888. $baseUser["FOREND_TOKEN"] = base_buildForendToken($baseUser);
  889. return $baseUser;
  890. }
  891. }
  892. }
  893. function basebaidumap_fetchplace($lat, $lng, $coordtype = "wgs84ll")
  894. {
  895. $url = "https://api.map.baidu.com/reverse_geocoding/v3/?";
  896. $url .= "ak=" . BaiduLBS_APPKEY;
  897. $url .= "&output=json&coordtype={$coordtype}";
  898. $url .= "&location={$lat},{$lng}";
  899. $result = http_get($url);
  900. $obj = @json_decode($result, true);
  901. if (!empty($obj) && intval($obj["status"]) == 0
  902. && is_array($obj["result"]) && !empty($obj["result"]["formatted_address"])) {
  903. return $obj["result"];
  904. } else {
  905. throw new Exception($result);
  906. }
  907. }
  908. //-----------------------------------------------------
  909. use AlibabaCloud\SDK\Dyplsapi\V20170525\Dyplsapi;
  910. use Darabonba\OpenApi\Models\Config;
  911. use AlibabaCloud\SDK\Dyplsapi\V20170525\Models\CreateSubscriptionRequest;
  912. use AlibabaCloud\SDK\Dyplsapi\V20170525\Models\BindAxnRequest;
  913. function alivphone_BindAxn($expiration, $phoneNoA, $poolKey)
  914. {
  915. require_once WEB_PHY_ROOT . "/base/composer/vendor/autoload.php";
  916. try {
  917. $config = new Config([
  918. "accessKeyId" => AliSMSAccessKey,
  919. "accessKeySecret" => AliSMSAccessSecret,
  920. ]);
  921. $config->endpoint = "dyplsapi.aliyuncs.com";
  922. $client = new Dyplsapi($config);
  923. $bindAxnRequest = new BindAxnRequest([
  924. "poolKey" => $poolKey,
  925. "phoneNoA" => $phoneNoA,
  926. "expiration" => date("Y-m-d H:i:s", $expiration),
  927. ]);
  928. eeglobal_log_handler('alivphone_BindAxn', 'info', "bindAxnRequest=>" . json_encode($bindAxnRequest));
  929. $response = $client->bindAxn($bindAxnRequest);
  930. if ($response->body->code == "OK") {
  931. eeglobal_log_handler('alivphone_BindAxn', 'info', "bindok=>" . json_encode($response->body->secretBindDTO));
  932. $secretNo = $response->body->secretBindDTO->secretNo;
  933. $subsId = $response->body->secretBindDTO->subsId;
  934. return array("secretNo" => $secretNo, "subsId" => $subsId);
  935. } else {
  936. throw new GeneralException("AlibabaCloudError", json_encode($response->body));
  937. }
  938. } catch (Throwable $ex) {
  939. throw new GeneralException("AlibabaCloudError", $ex->getMessage());
  940. }
  941. }
  942. use AlibabaCloud\SDK\Dyplsapi\V20170525\Models\QueryCallStatusRequest;
  943. function alivphone_QueryCallStatus($poolKey, $subsId)
  944. {
  945. require_once WEB_PHY_ROOT . "/base/composer/vendor/autoload.php";
  946. try {
  947. $config = new Config([
  948. "accessKeyId" => AliSMSAccessKey,
  949. "accessKeySecret" => AliSMSAccessSecret,
  950. ]);
  951. $config->endpoint = "dyplsapi.aliyuncs.com";
  952. $client = new Dyplsapi($config);
  953. $queryCallStatusRequest = new QueryCallStatusRequest([
  954. "poolKey" => $poolKey,
  955. "subsId" => $subsId,
  956. ]);
  957. eeglobal_log_handler('alivphone_QueryCallStatus', 'info', "queryCallStatusRequest=>" . json_encode($queryCallStatusRequest));
  958. $response = $client->queryCallStatus($queryCallStatusRequest);
  959. if ($response->body->code == "OK") {
  960. eeglobal_log_handler('alivphone_QueryCallStatus', 'info', "queryok=>" . json_encode($response->body->secretCallStatusDTO));
  961. $calledNo = $response->body->secretCallStatusDTO->calledNo;
  962. $status = $response->body->secretCallStatusDTO->status;
  963. return array("calledNo" => $calledNo, "status" => $status);
  964. } else {
  965. throw new GeneralException("AlibabaCloudError", json_encode($response->body));
  966. }
  967. } catch (Throwable $ex) {
  968. throw new GeneralException("AlibabaCloudError", $ex->getMessage());
  969. }
  970. }
  971. /**************************************************************/
  972. function baseexcel_exportCsv($fileName, $title, $args, $handle)
  973. {
  974. require_once WEB_PHY_ROOT . "/base/composer/vendor/autoload.php";
  975. set_time_limit(0);
  976. $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  977. $activeSheet = $spreadsheet->getActiveSheet();
  978. foreach ($title as $key => $value) {
  979. $activeSheet->setCellValueByColumnAndRow($key + 1, 1, $value);
  980. }
  981. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
  982. $writer->setOutputEncoding("GBK");
  983. $writer->save('php://output');
  984. $fp = fopen('php://output', 'w'); //打开output流
  985. header("Content-Description: File Transfer");
  986. header("Expires: 0");
  987. header("Cache-Control: must-revalidate");
  988. header("Pragma: public");
  989. header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  990. header("Content-Disposition: attachment;filename={$fileName}.csv");
  991. header("Cache-Control: max-age=0");
  992. do {
  993. $backdata = $handle($args);
  994. if (empty($backdata)) {
  995. break;
  996. }
  997. $pagedata = $backdata["pagedata"];
  998. if (empty($pagedata)) {
  999. break;
  1000. }
  1001. $args = $backdata["args"];
  1002. foreach ($pagedata as $item) {
  1003. mb_convert_variables('GBK', 'UTF-8', $item);
  1004. fputcsv($fp, $item);
  1005. }
  1006. ob_flush();
  1007. flush();
  1008. } while (!empty($pagedata));
  1009. fclose($fp);
  1010. exit();
  1011. }
  1012. function base_buildSNById($prefix, $id, $prefixMaxLen = 11, $zeroPadMaxLen = 11)
  1013. {
  1014. $strId = intval($id) . "";
  1015. if ((intval($prefixMaxLen) - mb_strlen($strId)) <= 0) {
  1016. $prefix = "";
  1017. }
  1018. $diff = intval($zeroPadMaxLen) - mb_strlen($strId);
  1019. $strPad = "";
  1020. for ($iii = 0; $diff > 0 && $iii < $diff; $iii++) {
  1021. $strPad .= "0";
  1022. }
  1023. return $prefix . $strPad . $strId;
  1024. }
  1025. function base_fetchIdBySN($sn)
  1026. {
  1027. preg_match_all('/\d/', $sn, $arr);
  1028. $id = implode($arr[0]);
  1029. return intval($id);
  1030. }