functions.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. function get_client_ip()
  3. {
  4. $ip = '';
  5. if (getenv('HTTP_CLIENT_IP')) {
  6. $ip = getenv('HTTP_CLIENT_IP');
  7. } else if (getenv('HTTP_X_FORWARDED_FOR')) {
  8. $ip = getenv('HTTP_X_FORWARDED_FOR');
  9. } else if (getenv('HTTP_X_FORWARDED')) {
  10. $ip = getenv('HTTP_X_FORWARDED');
  11. } else if (getenv('HTTP_FORWARDED_FOR')) {
  12. $ip = getenv('HTTP_FORWARDED_FOR');
  13. } else if (getenv('HTTP_FORWARDED')) {
  14. $ip = getenv('HTTP_FORWARDED');
  15. } else if (getenv('REMOTE_ADDR')) {
  16. $ip = getenv('REMOTE_ADDR');
  17. } else {
  18. $ip = 'UNKNOWN';
  19. }
  20. return $ip;
  21. }
  22. function get_client_os()
  23. {
  24. $os_platform = "Unknown OS Platform";
  25. $os_array = array(
  26. '/windows nt 11/i' => 'Windows 11',
  27. '/windows nt 10/i' => 'Windows 10',
  28. '/windows nt 6.3/i' => 'Windows 8.1',
  29. '/windows nt 6.2/i' => 'Windows 8',
  30. '/windows nt 6.1/i' => 'Windows 7',
  31. '/windows nt 6.0/i' => 'Windows Vista',
  32. '/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
  33. '/windows nt 5.1/i' => 'Windows XP',
  34. '/windows xp/i' => 'Windows XP',
  35. '/windows nt 5.0/i' => 'Windows 2000',
  36. '/windows me/i' => 'Windows ME',
  37. '/win98/i' => 'Windows 98',
  38. '/win95/i' => 'Windows 95',
  39. '/win16/i' => 'Windows 3.11',
  40. '/macintosh|mac os x/i' => 'Mac OS X',
  41. '/mac_powerpc/i' => 'Mac OS 9',
  42. '/linux/i' => 'Linux',
  43. '/ubuntu/i' => 'Ubuntu',
  44. '/iphone/i' => 'iPhone',
  45. '/ipod/i' => 'iPod',
  46. '/ipad/i' => 'iPad',
  47. '/android/i' => 'Android',
  48. '/blackberry/i' => 'BlackBerry',
  49. '/webos/i' => 'Mobile',
  50. );
  51. foreach ($os_array as $regex => $value) {
  52. if (preg_match($regex, $_SERVER['HTTP_USER_AGENT'])) {
  53. $os_platform = $value;
  54. }
  55. }
  56. return $os_platform;
  57. }
  58. function get_client_browser()
  59. {
  60. $browser = "Unknown Browser";
  61. $browser_array = array(
  62. '/msie/i' => 'Internet Explorer',
  63. '/Trident/i' => 'Internet Explorer',
  64. '/firefox/i' => 'Firefox',
  65. '/safari/i' => 'Safari',
  66. '/edg/i' => 'Edge',
  67. '/chrome/i' => 'Chrome',
  68. '/opera/i' => 'Opera',
  69. '/netscape/i' => 'Netscape',
  70. '/maxthon/i' => 'Maxthon',
  71. '/konqueror/i' => 'Konqueror',
  72. '/ubrowser/i' => 'UC Browser',
  73. '/mobile/i' => 'Handheld Browser',
  74. );
  75. foreach ($browser_array as $regex => $value) {
  76. if (preg_match($regex, $_SERVER['HTTP_USER_AGENT'])) {
  77. $browser = $value;
  78. }
  79. }
  80. return $browser;
  81. }
  82. function get_client_device()
  83. {
  84. $tablet_browser = 0;
  85. $mobile_browser = 0;
  86. if (preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
  87. $tablet_browser++;
  88. }
  89. if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
  90. $mobile_browser++;
  91. }
  92. if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']), 'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
  93. $mobile_browser++;
  94. }
  95. $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
  96. $mobile_agents = array(
  97. 'w3c ', 'acs-', 'alav', 'alca', 'amoi', 'audi', 'avan', 'benq', 'bird', 'blac',
  98. 'blaz', 'brew', 'cell', 'cldc', 'cmd-', 'dang', 'doco', 'eric', 'hipt', 'inno',
  99. 'ipaq', 'java', 'jigs', 'kddi', 'keji', 'leno', 'lg-c', 'lg-d', 'lg-g', 'lge-',
  100. 'maui', 'maxo', 'midp', 'mits', 'mmef', 'mobi', 'mot-', 'moto', 'mwbp', 'nec-',
  101. 'newt', 'noki', 'palm', 'pana', 'pant', 'phil', 'play', 'port', 'prox',
  102. 'qwap', 'sage', 'sams', 'sany', 'sch-', 'sec-', 'send', 'seri', 'sgh-', 'shar',
  103. 'sie-', 'siem', 'smal', 'smar', 'sony', 'sph-', 'symb', 't-mo', 'teli', 'tim-',
  104. 'tosh', 'tsm-', 'upg1', 'upsi', 'vk-v', 'voda', 'wap-', 'wapa', 'wapi', 'wapp',
  105. 'wapr', 'webc', 'winw', 'winw', 'xda ', 'xda-'
  106. );
  107. if (in_array($mobile_ua, $mobile_agents)) {
  108. $mobile_browser++;
  109. }
  110. if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'opera mini') > 0) {
  111. $mobile_browser++;
  112. // Check for tablets on opera mini alternative headers
  113. $stock_ua = strtolower(isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']) ? $_SERVER['HTTP_X_OPERAMINI_PHONE_UA'] : (isset($_SERVER['HTTP_DEVICE_STOCK_UA']) ? $_SERVER['HTTP_DEVICE_STOCK_UA'] : ''));
  114. if (preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i', $stock_ua)) {
  115. $tablet_browser++;
  116. }
  117. }
  118. if ($tablet_browser > 0) {
  119. // do something for tablet devices
  120. return 'Tablet';
  121. } else if ($mobile_browser > 0) {
  122. // do something for mobile devices
  123. return 'Mobile';
  124. } else {
  125. // do something for everything else
  126. return 'Computer';
  127. }
  128. }
  129. function get_filemanager_url($ftp_host, $account_username, $account_password, $domain = '')
  130. {
  131. $params = base64_encode(
  132. json_encode(
  133. array(
  134. 't' => 'ftp',
  135. 'c' => array(
  136. 'v' => 1,
  137. 'p' => $account_password,
  138. 'i' => empty($domain) ? '/htdocs' : '/' . $domain . '/htdocs/',
  139. ),
  140. )
  141. )
  142. );
  143. return 'https://filemanager.ai/new/#/c/' . $ftp_host . '/' . $account_username . '/' . $params;
  144. }
  145. function upload_image($image)
  146. {
  147. if (!is_dir(ROOT . '/images')) {
  148. mkdir(ROOT . '/images');
  149. }
  150. if ($image['error'] == 4) {
  151. die('image file not uploaded');
  152. }
  153. if ($image['type'] != 'image/png') {
  154. die('Only, png image files are allowed');
  155. }
  156. $image_info = pathinfo($image['name']);
  157. extract($image_info);
  158. $image_convention = $filename . time() . '.$extension';
  159. if (move_uploaded_file($image['tmp_name'], ROOT . '/images/' . $imageConvention)) {
  160. return $image_convention;
  161. } else {
  162. return false;
  163. }
  164. }
  165. function cTime($t = '')
  166. {
  167. return isset($t) && $t != '' ? date("Y-m-d H:i:s", $t) : '';
  168. }
  169. function get_execution_time()
  170. {
  171. // $starttime = explode(' ', microtime());
  172. global $start_time;
  173. // 程序运行时间
  174. $endtime = explode(' ', microtime());
  175. $t = $endtime[0] + $endtime[1] - ($start_time[0] + $start_time[1]);
  176. return round($t, 3);
  177. }
  178. function get($field = '', $default = '')
  179. {
  180. if (!$field) {
  181. return $_GET;
  182. }
  183. if (!isset($_GET[$field])) {
  184. return $default ? $default : false;
  185. }
  186. if (is_string($_GET[$field])) {
  187. return trim(htmlspecialchars($_GET[$field]));
  188. }
  189. return $_GET[$field];
  190. }
  191. function post($field = '', $default = '')
  192. {
  193. if (!$field) {
  194. return $_POST;
  195. }
  196. if (!isset($_POST[$field])) {
  197. return $default ? $default : false;
  198. }
  199. if (is_string($_POST[$field])) {
  200. return trim(htmlspecialchars($_POST[$field]));
  201. }
  202. return $_POST[$field];
  203. }
  204. /**
  205. * API Response
  206. */
  207. function send_response($code = 200, $data = NULL, $msg = '')
  208. {
  209. header("Access-Control-Allow-Origin: *");
  210. header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
  211. // header("Access-Control-Allow-Headers: DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization");
  212. header("Content-Type: application/json");
  213. $raw = array(
  214. 'code' => $code,
  215. 'data' => isset($data) && !empty($data) ? $data : NULL,
  216. 'msg' => $msg,
  217. );
  218. $raw = json_encode($raw);
  219. // echo $raw;
  220. exit($raw);
  221. }
  222. // 格式化输出
  223. function dump($data = '')
  224. {
  225. echo '<pre>';
  226. print_r($data);
  227. echo '</pre>';
  228. }
  229. // 随机生成字符串(字母+数字)
  230. function randstr($count = 6)
  231. {
  232. $res = '';
  233. // 将你想要的字符添加到下面字符串中,默认是数字0-9和26个英文字母
  234. $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  235. $m = strlen($str) - 1;
  236. for ($i = 0; $i < $count; $i++) {
  237. // 将这个字符串当作一个数组,随机取出一个字符,并循环拼接成你需要的位数
  238. $res .= $str[rand(0, $m)];
  239. }
  240. return $res;
  241. }
  242. function setMessage($message, $class_name = 'success')
  243. {
  244. $_SESSION['message'] = '<div class="alert alert-' . $class_name . '" role="alert">
  245. <button class="close" data-dismiss="alert" type="button" aria-label="Close">
  246. <span aria-hidden="true">&times;</span>
  247. </button>
  248. ' . $message . '
  249. </div>';
  250. }
  251. function getMessage()
  252. {
  253. if (isset($_SESSION['message'])) {
  254. echo ($_SESSION['message']);
  255. unset($_SESSION['message']);
  256. }
  257. }
  258. function setMsg($name, $value, $class = 'success')
  259. {
  260. if (is_array($value)) {
  261. $_SESSION[$name] = $value;
  262. } else {
  263. $_SESSION[$name] = "<div class='alert alert-$class text-center'>$value</div>";
  264. }
  265. }
  266. function getMsg($name)
  267. {
  268. if (isset($_SESSION[$name])) {
  269. $session = $_SESSION[$name];
  270. unset($_SESSION[$name]);
  271. return $session;
  272. }
  273. }
  274. function isUserLoggedIn()
  275. {
  276. if (isset($_SESSION['user']) || isset($_COOKIE['user'])) {
  277. return true;
  278. } else {
  279. return false;
  280. }
  281. }
  282. function get_userinfo()
  283. {
  284. if (isUserLoggedIn()) {
  285. return isset($_COOKIE['user']) ? unserialize($_COOKIE['user']) : $_SESSION['user'];
  286. }
  287. return '';
  288. }
  289. function send_mail($detail = array())
  290. {
  291. if (!empty($detail['to']) && !empty($detail['message']) && !empty($detail['subject'])) {
  292. $to = $detail['to'];
  293. $totitle = isset($detail['totitle']) ? $detail['totitle'] : '';
  294. $from = SMTP_MAILADDR;
  295. $fromtitle = isset($detail['fromtitle']) ? $detail['fromtitle'] : 'UIISC';
  296. $subject = $detail['subject'];
  297. $body = $detail['message'];
  298. $mailtype = 'HTML'; // HTML/TXT
  299. $smtp = new \lib\MailSMTP(SMTP_SERVER, SMTP_USERNAME, SMTP_PASSWORD, SMTP_PORT);
  300. $res = $smtp->sendmail($from, $to, $subject, $body, $totitle, $fromtitle, $mailtype);
  301. if (!$res) {
  302. return false;
  303. } else {
  304. return true;
  305. }
  306. } else {
  307. die('Your Mail Handler requires four main paramters');
  308. }
  309. }
  310. /**
  311. * redirect to router URL
  312. */
  313. function redirect($module, $section = '', $param = array(), $anchor = '')
  314. {
  315. header('Location: ' . setURL($module, $section, $param, $anchor));
  316. exit;
  317. }
  318. /** make router URL
  319. * @param mixed $module
  320. * @param mixed $section
  321. * @return string
  322. */
  323. function setRouter($module, $section = '', $param = array(), $anchor = '')
  324. {
  325. if (!empty($section)) {
  326. $param = array_merge(array('s' => $section), $param);
  327. }
  328. if (empty($param)) {
  329. return $module . '.php' . $anchor;
  330. }
  331. return $module . '.php?' . http_build_query($param) . $anchor;
  332. }
  333. /** make a full path http URL
  334. * @param mixed $module
  335. * @param mixed $section
  336. * @return string
  337. */
  338. function setURL($module, $section = '', $param = array(), $anchor = '')
  339. {
  340. return SITEURL . '/' . setRouter($module, $section, $param, $anchor);
  341. }
  342. /** Determine if a variable is an email address
  343. *
  344. * @param string $email
  345. * @return bool
  346. */
  347. function is_email($email = '')
  348. {
  349. return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/", $email);
  350. }
  351. /** check PHP version
  352. * @return bool
  353. */
  354. function getVersion()
  355. {
  356. if ((float) phpversion() < 5.5) {
  357. exit('requires the php version 5.5.+');
  358. }
  359. }
  360. function setProtect($x)
  361. {
  362. return htmlentities(htmlspecialchars($x));
  363. }
  364. /**
  365. * logout, clear session UIISC_ADMIN
  366. */
  367. function logout()
  368. {
  369. if (isset($_SESSION['UIISC_ADMIN'])) {
  370. unset($_SESSION['UIISC_ADMIN']);
  371. setMessage('Logged out <b>successfully!</b>');
  372. } else {
  373. setMessage('Login to <b>continue!</b>', 'danger');
  374. }
  375. }
  376. function email_build_body($title, $nickname, $content, $description = '')
  377. {
  378. return '<div class="container" style="margin:20px 5px;font-family: Arial, Helvetica, sans-serif;">
  379. <h2 style="text-align:center;"><b>' . $title . '</b></h2>
  380. <hr />
  381. <h3>Dear ' . $nickname . ',</h3>
  382. <div>' . $content . '</div>
  383. <div>' . $description . '</div>
  384. <hr />
  385. <p>After you login to your account you can use any service ❤</p>
  386. <p>Best Regards</p>
  387. <p>' . date('Y-m-d H:i:s') . '</p>
  388. <hr />
  389. <div style="text-align:center;">
  390. <p>Need our help ?</p>
  391. <p><a href="' . setURL('clientarea/tickets', '', array('action' => 'add')) . '">We are here to help you out !</a></p>
  392. <p><b>UIISC</b></p>
  393. </div>
  394. </div>';
  395. }
  396. function checkRefererHost()
  397. {
  398. if (!isset($_SERVER['HTTP_REFERER']) || !$_SERVER['HTTP_REFERER']) return false;
  399. $url_arr = parse_url($_SERVER['HTTP_REFERER']);
  400. $http_host = $_SERVER['HTTP_HOST'];
  401. if (strpos($http_host, ':')) $http_host = substr($http_host, 0, strpos($http_host, ':'));
  402. return $url_arr['host'] === $http_host;
  403. }
  404. function checkIfActive($string)
  405. {
  406. $array = explode(',', $string);
  407. $php_self = substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '/') + 1, strrpos($_SERVER['REQUEST_URI'], '.') - strrpos($_SERVER['REQUEST_URI'], '/') - 1);
  408. if (in_array($php_self, $array)) {
  409. return 'active';
  410. } elseif (isset($_GET['m']) && in_array($_GET['m'], $array)) {
  411. return 'active';
  412. }
  413. return null;
  414. }