normal-functions.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. // Print output all purrty
  3. function prettyPrint($v)
  4. {
  5. $trace = debug_backtrace()[0];
  6. echo '<pre style="white-space: pre; text-overflow: ellipsis; overflow: hidden; background-color: #f2f2f2; border: 2px solid black; border-radius: 5px; padding: 5px; margin: 5px;">' . $trace['file'] . ':' . $trace['line'] . ' ' . gettype($v) . "\n\n" . print_r($v, 1) . '</pre><br/>';
  7. }
  8. // Clean Directory string
  9. function cleanDirectory($path)
  10. {
  11. $path = str_replace(array('/', '\\'), '/', $path);
  12. if (substr($path, -1) != '/') {
  13. $path = $path . '/';
  14. }
  15. if ($path[0] != '/' && $path[1] != ':') {
  16. $path = '/' . $path;
  17. }
  18. return $path;
  19. }
  20. // Get Gravatar Email Image
  21. function gravatar($email = '')
  22. {
  23. $email = md5(strtolower(trim($email)));
  24. $gravurl = "https://www.gravatar.com/avatar/$email?s=100&d=mm";
  25. return $gravurl;
  26. }
  27. function parseDomain($value, $force = false)
  28. {
  29. $badDomains = array('ddns.net', 'ddnsking.com', '3utilities.com', 'bounceme.net', 'duckdns.org', 'freedynamicdns.net', 'freedynamicdns.org', 'gotdns.ch', 'hopto.org', 'myddns.me', 'myds.me', 'myftp.biz', 'myftp.org', 'myvnc.com', 'noip.com', 'onthewifi.com', 'redirectme.net', 'serveblog.net', 'servecounterstrike.com', 'serveftp.com', 'servegame.com', 'servehalflife.com', 'servehttp.com', 'serveirc.com', 'serveminecraft.net', 'servemp3.com', 'servepics.com', 'servequake.com', 'sytes.net', 'viewdns.net', 'webhop.me', 'zapto.org');
  30. $Domain = $value;
  31. $Port = strpos($Domain, ':');
  32. if ($Port !== false) {
  33. $Domain = substr($Domain, 0, $Port);
  34. $value = $Domain;
  35. }
  36. $check = substr_count($Domain, '.');
  37. if ($check >= 3) {
  38. if (is_numeric($Domain[0])) {
  39. $Domain = '';
  40. } else {
  41. if (in_array(strtolower(explode('.', $Domain)[2] . '.' . explode('.', $Domain)[3]), $badDomains)) {
  42. $Domain = '.' . explode('.', $Domain)[0] . '.' . explode('.', $Domain)[1] . '.' . explode('.', $Domain)[2] . '.' . explode('.', $Domain)[3];
  43. } else {
  44. $Domain = '.' . explode('.', $Domain)[1] . '.' . explode('.', $Domain)[2] . '.' . explode('.', $Domain)[3];
  45. }
  46. }
  47. } elseif ($check == 2) {
  48. if (in_array(strtolower(explode('.', $Domain)[1] . '.' . explode('.', $Domain)[2]), $badDomains)) {
  49. $Domain = '.' . explode('.', $Domain)[0] . '.' . explode('.', $Domain)[1] . '.' . explode('.', $Domain)[2];
  50. } elseif (explode('.', $Domain)[0] == 'www') {
  51. $Domain = '.' . explode('.', $Domain)[1] . '.' . explode('.', $Domain)[2];
  52. } elseif (explode('.', $Domain)[1] == 'co') {
  53. $Domain = '.' . explode('.', $Domain)[0] . '.' . explode('.', $Domain)[1] . '.' . explode('.', $Domain)[2];
  54. } else {
  55. $Domain = '.' . explode('.', $Domain)[1] . '.' . explode('.', $Domain)[2];
  56. }
  57. } elseif ($check == 1) {
  58. $Domain = '.' . $Domain;
  59. } else {
  60. $Domain = '';
  61. }
  62. /*
  63. if (is_numeric($Domain[0]) || strpos($Domain, '.') == false) {
  64. $Domain = '';
  65. } else {
  66. if (substr($Domain, 0, 3) == 'www') {
  67. $Domain = substr($Domain, 3, strlen($Domain) - 3);
  68. } else {
  69. $Domain = '.' . $Domain;
  70. }
  71. }
  72. */
  73. return ($force) ? $value : $Domain;
  74. }
  75. // Cookie Custom Function
  76. function coookie($type, $name, $value = '', $days = -1, $http = true)
  77. {
  78. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == "https") {
  79. $Secure = true;
  80. $HTTPOnly = true;
  81. } elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  82. $Secure = true;
  83. $HTTPOnly = true;
  84. } else {
  85. $Secure = false;
  86. $HTTPOnly = false;
  87. }
  88. if (!$http) {
  89. $HTTPOnly = false;
  90. }
  91. $Path = '/';
  92. $Domain = parseDomain($_SERVER['HTTP_HOST']);
  93. $DomainTest = parseDomain($_SERVER['HTTP_HOST'], true);
  94. if ($type == 'set') {
  95. $_COOKIE[$name] = $value;
  96. header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value)
  97. . (empty($days) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', time() + (86400 * $days)) . ' GMT')
  98. . (empty($Path) ? '' : '; path=' . $Path)
  99. . (empty($Domain) ? '' : '; domain=' . $Domain)
  100. . (!$Secure ? '' : '; secure')
  101. . (!$HTTPOnly ? '' : '; HttpOnly'), false);
  102. header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value)
  103. . (empty($days) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', time() + (86400 * $days)) . ' GMT')
  104. . (empty($Path) ? '' : '; path=' . $Path)
  105. . (empty($Domain) ? '' : '; domain=' . $DomainTest)
  106. . (!$Secure ? '' : '; secure')
  107. . (!$HTTPOnly ? '' : '; HttpOnly'), false);
  108. } elseif ($type == 'delete') {
  109. unset($_COOKIE[$name]);
  110. header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value)
  111. . (empty($days) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', time() - 3600) . ' GMT')
  112. . (empty($Path) ? '' : '; path=' . $Path)
  113. . (empty($Domain) ? '' : '; domain=' . $Domain)
  114. . (!$Secure ? '' : '; secure')
  115. . (!$HTTPOnly ? '' : '; HttpOnly'), false);
  116. header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value)
  117. . (empty($days) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', time() - 3600) . ' GMT')
  118. . (empty($Path) ? '' : '; path=' . $Path)
  119. . (empty($Domain) ? '' : '; domain=' . $DomainTest)
  120. . (!$Secure ? '' : '; secure')
  121. . (!$HTTPOnly ? '' : '; HttpOnly'), false);
  122. }
  123. }
  124. function getOS()
  125. {
  126. if (PHP_SHLIB_SUFFIX == "dll") {
  127. return "win";
  128. } else {
  129. return "*nix";
  130. }
  131. }
  132. if (!function_exists('getallheaders')) {
  133. function getallheaders()
  134. {
  135. $headers = array();
  136. foreach ($_SERVER as $name => $value) {
  137. if (substr($name, 0, 5) == 'HTTP_') {
  138. $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
  139. }
  140. }
  141. return $headers;
  142. }
  143. }
  144. function random_ascii_string($length)
  145. {
  146. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  147. $charactersLength = strlen($characters);
  148. $randomString = '';
  149. for ($i = 0; $i < $length; $i++) {
  150. $randomString .= $characters[rand(0, $charactersLength - 1)];
  151. }
  152. return $randomString;
  153. }
  154. // Generate Random string
  155. function randString($length = 10, $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')
  156. {
  157. $tmp = '';
  158. for ($i = 0; $i < $length; $i++) {
  159. $tmp .= substr(str_shuffle($chars), 0, 1);
  160. }
  161. return $tmp;
  162. }
  163. function isEncrypted($password)
  164. {
  165. switch (strlen($password)) {
  166. case '24':
  167. return (strpos($password, '==') !== false) ? true : false;
  168. break;
  169. case '44':
  170. return (substr($password, -1, 1) == '=') ? true : false;
  171. break;
  172. case '64':
  173. return true;
  174. case '88':
  175. return (strpos($password, '==') !== false) ? true : false;
  176. break;
  177. case '108':
  178. return (substr($password, -1, 1) == '=') ? true : false;
  179. break;
  180. default:
  181. return false;
  182. }
  183. }
  184. function encrypt($password, $key = null)
  185. {
  186. $key = (isset($GLOBALS['organizrHash'])) ? $GLOBALS['organizrHash'] : $key;
  187. return openssl_encrypt($password, 'AES-256-CBC', $key, 0, fillString($key, 16));
  188. }
  189. function decrypt($password, $key = null)
  190. {
  191. if (empty($password)) {
  192. return '';
  193. }
  194. $key = (isset($GLOBALS['organizrHash'])) ? $GLOBALS['organizrHash'] : $key;
  195. return openssl_decrypt($password, 'AES-256-CBC', $key, 0, fillString($key, 16));
  196. }
  197. function fillString($string, $length)
  198. {
  199. $filler = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*';
  200. if (strlen($string) < $length) {
  201. $diff = $length - strlen($string);
  202. $filler = substr($filler, 0, $diff);
  203. return $string . $filler;
  204. } elseif (strlen($string) > $length) {
  205. return substr($string, 0, $length);
  206. } else {
  207. return $string;
  208. }
  209. }
  210. function userIP()
  211. {
  212. if (isset($_SERVER['HTTP_CLIENT_IP'])) {
  213. $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
  214. } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  215. $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
  216. } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
  217. $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
  218. } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
  219. $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
  220. } elseif (isset($_SERVER['HTTP_FORWARDED'])) {
  221. $ipaddress = $_SERVER['HTTP_FORWARDED'];
  222. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  223. $ipaddress = $_SERVER['REMOTE_ADDR'];
  224. } else {
  225. $ipaddress = 'UNKNOWN';
  226. }
  227. if (strpos($ipaddress, ',') !== false) {
  228. list($first, $last) = explode(",", $ipaddress);
  229. unset($last);
  230. return $first;
  231. } else {
  232. return $ipaddress;
  233. }
  234. }
  235. function arrayIP($string)
  236. {
  237. if (strpos($string, ',') !== false) {
  238. $result = explode(",", $string);
  239. } else {
  240. $result = array($string);
  241. }
  242. foreach ($result as &$ip) {
  243. $ip = is_numeric(substr($ip, 0, 1)) ? $ip : gethostbyname($ip);
  244. }
  245. return $result;
  246. }
  247. function getCert()
  248. {
  249. $url = 'http://curl.haxx.se/ca/cacert.pem';
  250. $file = __DIR__ . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . 'cacert.pem';
  251. if (!file_exists($file)) {
  252. file_put_contents($file, fopen($url, 'r'));
  253. } elseif (file_exists($file) && time() - 2592000 > filemtime($file)) {
  254. file_put_contents($file, fopen($url, 'r'));
  255. }
  256. return $file;
  257. }
  258. function curl($curl, $url, $headers = array(), $data = array())
  259. {
  260. // Initiate cURL
  261. $curlReq = curl_init($url);
  262. if (in_array(trim(strtoupper($curl)), ["GET", "POST", "PUT", "DELETE"])) {
  263. curl_setopt($curlReq, CURLOPT_CUSTOMREQUEST, trim(strtoupper($curl)));
  264. } else {
  265. return null;
  266. }
  267. curl_setopt($curlReq, CURLOPT_RETURNTRANSFER, true);
  268. curl_setopt($curlReq, CURLOPT_CAINFO, getCert());
  269. curl_setopt($curlReq, CURLOPT_CONNECTTIMEOUT, 5);
  270. if (localURL($url)) {
  271. curl_setopt($curlReq, CURLOPT_SSL_VERIFYHOST, 0);
  272. curl_setopt($curlReq, CURLOPT_SSL_VERIFYPEER, 0);
  273. }
  274. // Format Headers
  275. $cHeaders = array();
  276. foreach ($headers as $k => $v) {
  277. $cHeaders[] = $k . ': ' . $v;
  278. }
  279. if (count($cHeaders)) {
  280. curl_setopt($curlReq, CURLOPT_HTTPHEADER, $cHeaders);
  281. }
  282. // Format Data
  283. switch (isset($headers['Content-Type']) ? $headers['Content-Type'] : '') {
  284. case 'application/json':
  285. curl_setopt($curlReq, CURLOPT_POSTFIELDS, json_encode($data));
  286. break;
  287. case 'application/x-www-form-urlencoded':
  288. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  289. break;
  290. default:
  291. $headers['Content-Type'] = 'application/x-www-form-urlencoded';
  292. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  293. }
  294. // Execute
  295. $result = curl_exec($curlReq);
  296. $httpcode = curl_getinfo($curlReq);
  297. // Close
  298. curl_close($curlReq);
  299. // Return
  300. return array('content' => $result, 'http_code' => $httpcode);
  301. }
  302. function getHeaders($url)
  303. {
  304. $ch = curl_init($url);
  305. curl_setopt($ch, CURLOPT_NOBODY, true);
  306. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  307. curl_setopt($ch, CURLOPT_HEADER, false);
  308. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  309. curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
  310. curl_setopt($ch, CURLOPT_CAINFO, getCert());
  311. if (localURL($url)) {
  312. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  313. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  314. }
  315. curl_exec($ch);
  316. $headers = curl_getinfo($ch);
  317. curl_close($ch);
  318. return $headers;
  319. }
  320. function download($url, $path)
  321. {
  322. ini_set('max_execution_time', 0);
  323. set_time_limit(0);
  324. $ch = curl_init($url);
  325. curl_setopt($ch, CURLOPT_HEADER, 1);
  326. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  327. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  328. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  329. curl_setopt($ch, CURLOPT_CAINFO, getCert());
  330. if (localURL($url)) {
  331. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  332. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  333. }
  334. $raw_file_data = curl_exec($ch);
  335. curl_close($ch);
  336. file_put_contents($path, $raw_file_data);
  337. return (filesize($path) > 0) ? true : false;
  338. }
  339. function localURL($url)
  340. {
  341. if (strpos($url, 'https') !== false) {
  342. preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $url, $result);
  343. $result = (!empty($result) ? true : false);
  344. return $result;
  345. }
  346. return false;
  347. }
  348. function array_filter_key(array $array, $callback)
  349. {
  350. $matchedKeys = array_filter(array_keys($array), $callback);
  351. return array_intersect_key($array, array_flip($matchedKeys));
  352. }
  353. function searchArray($array, $field, $value)
  354. {
  355. foreach ($array as $key => $item) {
  356. if ($item[$field] === $value)
  357. return $key;
  358. }
  359. return false;
  360. }
  361. // Qualify URL
  362. function qualifyURL($url, $return = false)
  363. {
  364. //local address?
  365. if (substr($url, 0, 1) == "/") {
  366. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  367. $protocol = "https://";
  368. } else {
  369. $protocol = "http://";
  370. }
  371. $url = $protocol . getServer() . $url;
  372. }
  373. // Get Digest
  374. $digest = parse_url($url);
  375. // http/https
  376. if (!isset($digest['scheme'])) {
  377. if (isset($digest['port']) && in_array($digest['port'], array(80, 8080, 8096, 32400, 7878, 8989, 8182, 8081, 6789))) {
  378. $scheme = 'http';
  379. } else {
  380. $scheme = 'https';
  381. }
  382. } else {
  383. $scheme = $digest['scheme'];
  384. }
  385. // Host
  386. $host = (isset($digest['host']) ? $digest['host'] : '');
  387. // Port
  388. $port = (isset($digest['port']) ? ':' . $digest['port'] : '');
  389. // Path
  390. $path = (isset($digest['path']) && $digest['path'] !== '/' ? $digest['path'] : '');
  391. // Output
  392. $array = array(
  393. 'scheme' => $scheme,
  394. 'host' => $host,
  395. 'port' => $port,
  396. 'path' => $path
  397. );
  398. return ($return) ? $array : $scheme . '://' . $host . $port . $path;
  399. }
  400. function getServerPath($over = false)
  401. {
  402. if ($over) {
  403. if ($GLOBALS['PHPMAILER-domain'] !== '') {
  404. return $GLOBALS['PHPMAILER-domain'];
  405. }
  406. }
  407. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == "https") {
  408. $protocol = "https://";
  409. } elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  410. $protocol = "https://";
  411. } else {
  412. $protocol = "http://";
  413. }
  414. $domain = '';
  415. if (isset($_SERVER['SERVER_NAME']) && strpos($_SERVER['SERVER_NAME'], '.') !== false) {
  416. $domain = $_SERVER['SERVER_NAME'];
  417. } elseif (isset($_SERVER['HTTP_HOST'])) {
  418. if (strpos($_SERVER['HTTP_HOST'], ':') !== false) {
  419. $domain = explode(':', $_SERVER['HTTP_HOST'])[0];
  420. $port = explode(':', $_SERVER['HTTP_HOST'])[1];
  421. if ($port !== "80" && $port !== "443") {
  422. $domain = $_SERVER['HTTP_HOST'];
  423. }
  424. } else {
  425. $domain = $_SERVER['HTTP_HOST'];
  426. }
  427. }
  428. $url = $protocol . $domain . str_replace("\\", "/", dirname($_SERVER['REQUEST_URI']));
  429. if (strpos($url, '/api') !== false) {
  430. $url = explode('/api', $url);
  431. return $url[0] . '/';
  432. } else {
  433. return $url;
  434. }
  435. }
  436. function get_browser_name()
  437. {
  438. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  439. if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) {
  440. return 'Opera';
  441. } elseif (strpos($user_agent, 'Edge')) {
  442. return 'Edge';
  443. } elseif (strpos($user_agent, 'Chrome')) {
  444. return 'Chrome';
  445. } elseif (strpos($user_agent, 'Safari')) {
  446. return 'Safari';
  447. } elseif (strpos($user_agent, 'Firefox')) {
  448. return 'Firefox';
  449. } elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) {
  450. return 'Internet Explorer';
  451. }
  452. return 'Other';
  453. }
  454. function getServer($over = false)
  455. {
  456. if ($over) {
  457. if ($GLOBALS['PHPMAILER-domain'] !== '') {
  458. return $GLOBALS['PHPMAILER-domain'];
  459. }
  460. }
  461. return isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : $_SERVER["SERVER_NAME"];
  462. }
  463. /* Function is to get all the contents from ics and explode all the datas according to the events and its sections */
  464. function getIcsEventsAsArray($file)
  465. {
  466. $icalString = file_get_contents_curl($file);
  467. $icsDates = array();
  468. /* Explode the ICs Data to get datas as array according to string ‘BEGIN:’ */
  469. $icsData = explode("BEGIN:", $icalString);
  470. /* Iterating the icsData value to make all the start end dates as sub array */
  471. foreach ($icsData as $key => $value) {
  472. $icsDatesMeta [$key] = explode("\n", $value);
  473. }
  474. /* Itearting the Ics Meta Value */
  475. foreach ($icsDatesMeta as $key => $value) {
  476. foreach ($value as $subKey => $subValue) {
  477. /* to get ics events in proper order */
  478. $icsDates = getICSDates($key, $subKey, $subValue, $icsDates);
  479. }
  480. }
  481. return $icsDates;
  482. }
  483. /* funcion is to avaid the elements wich is not having the proper start, end and summary informations */
  484. function getICSDates($key, $subKey, $subValue, $icsDates)
  485. {
  486. if ($key != 0 && $subKey == 0) {
  487. $icsDates [$key] ["BEGIN"] = $subValue;
  488. } else {
  489. $subValueArr = explode(":", $subValue, 2);
  490. if (isset ($subValueArr [1])) {
  491. $icsDates [$key] [$subValueArr [0]] = $subValueArr [1];
  492. }
  493. }
  494. return $icsDates;
  495. }
  496. function file_get_contents_curl($url)
  497. {
  498. $ch = curl_init();
  499. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  500. curl_setopt($ch, CURLOPT_HEADER, 0);
  501. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  502. curl_setopt($ch, CURLOPT_URL, $url);
  503. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  504. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  505. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  506. $data = curl_exec($ch);
  507. curl_close($ch);
  508. return $data;
  509. }
  510. function getExtension($string)
  511. {
  512. return preg_replace("#(.+)?\.(\w+)(\?.+)?#", "$2", $string);
  513. }
  514. function safe_json_encode($value, $options = 0, $depth = 512)
  515. {
  516. $encoded = json_encode($value, $options, $depth);
  517. if ($encoded === false && $value && json_last_error() == JSON_ERROR_UTF8) {
  518. $encoded = json_encode(utf8ize($value), $options, $depth);
  519. }
  520. return $encoded;
  521. }
  522. function utf8ize($mixed)
  523. {
  524. if (is_array($mixed)) {
  525. foreach ($mixed as $key => $value) {
  526. $mixed[$key] = utf8ize($value);
  527. }
  528. } elseif (is_string($mixed)) {
  529. return mb_convert_encoding($mixed, "UTF-8", "UTF-8");
  530. }
  531. return $mixed;
  532. }
  533. function gen_uuid()
  534. {
  535. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  536. // 32 bits for "time_low"
  537. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  538. // 16 bits for "time_mid"
  539. mt_rand(0, 0xffff),
  540. // 16 bits for "time_hi_and_version",
  541. // four most significant bits holds version number 4
  542. mt_rand(0, 0x0fff) | 0x4000,
  543. // 16 bits, 8 bits for "clk_seq_hi_res",
  544. // 8 bits for "clk_seq_low",
  545. // two most significant bits holds zero and one for variant DCE1.1
  546. mt_rand(0, 0x3fff) | 0x8000,
  547. // 48 bits for "node"
  548. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  549. );
  550. }