Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. <?php
  2. function get_page_url($page, $CONF, $full = true)
  3. {
  4. $full_url = get_http($CONF).$CONF['host'];
  5. switch ($CONF['url_type'])
  6. {
  7. case 'sub':
  8. if ($page == $CONF['default_page'])
  9. {
  10. $page = 'www';
  11. }
  12. $full_url = get_subdomain_full($page, $CONF);
  13. break;
  14. case 'page':
  15. $cur_sub = get_subdomain($CONF);
  16. $full_url = get_http($CONF).$cur_sub.".".$CONF['host']."/".$page;
  17. break;
  18. case 'get':
  19. if (isset($_GET['site_page']))
  20. {
  21. $cur_page = $_GET['site_page'];
  22. }
  23. else
  24. {
  25. $cur_page = $CONF['default_page'];
  26. }
  27. $cur_sub = get_subdomain($CONF);
  28. $full_url = get_http($CONF).$cur_sub.".".$CONF['host']."/".$cur_page;
  29. break;
  30. }
  31. return $full_url;
  32. }
  33. function get_subdomain_full($sub_part, $CONF)
  34. {
  35. $url = get_http($CONF).$sub_part.".".$CONF['host'];
  36. return $url;
  37. }
  38. function extract_domain($domain)
  39. {
  40. if(preg_match("/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i", $domain, $matches))
  41. {
  42. return $matches['domain'];
  43. } else {
  44. return $domain;
  45. }
  46. }
  47. function extract_subdomains($domain)
  48. {
  49. $subdomains = $domain;
  50. $domain = extract_domain($subdomains);
  51. $subdomains = rtrim(strstr($subdomains, $domain, true), '.');
  52. return $subdomains;
  53. }
  54. function get_subdomain($CONF)
  55. {
  56. $sub = extract_subdomains($_SERVER['HTTP_HOST']);
  57. if ($sub == "" || $sub == "www")
  58. {
  59. $sub = $CONF['default_page'];
  60. }
  61. return $sub;
  62. }
  63. function get_page()
  64. {
  65. $url_array = explode("/",$_SERVER["REQUEST_URI"]);
  66. return ltrim($url_array[1], "/");
  67. }
  68. function get_http($CONF)
  69. {
  70. if ($CONF['https'] != "on")
  71. {
  72. $http = "http://";
  73. }
  74. else
  75. {
  76. $http = "https://";
  77. }
  78. return $http;
  79. }
  80. function get_active($page, $CONF)
  81. {
  82. $cur_page = '';
  83. switch ($CONF['url_type'])
  84. {
  85. case 'sub':
  86. $cur_page = get_subdomain($CONF);
  87. break;
  88. case 'page':
  89. $cur_page = get_page();
  90. break;
  91. case 'get':
  92. if (isset($_GET['site_page']))
  93. {
  94. $cur_page = $_GET['site_page'];
  95. }
  96. else
  97. {
  98. $cur_page = $CONF['default_page'];
  99. }
  100. break;
  101. }
  102. if ($cur_page == $page)
  103. {
  104. return 'active';
  105. }
  106. return "";
  107. }
  108. function checkemail($email)
  109. {
  110. return preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $email) ? TRUE : FALSE;
  111. }
  112. function safe($input)
  113. {
  114. $valid_input = addslashes($input);
  115. return $valid_input;
  116. }
  117. function safe_register($input)
  118. {
  119. $input = rawurlencode($input);
  120. $valid_input = mysql_real_escape_string($input);
  121. return $valid_input;
  122. }
  123. function rand_string( $length ) {
  124. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  125. $size = strlen( $chars );
  126. for( $i = 0; $i < $length; $i++ ) {
  127. $str .= $chars[ rand( 0, $size - 1 ) ];
  128. }
  129. return $str;
  130. }
  131. function hashPassword($password, $CONF)
  132. {
  133. $hashed_pass = hash("sha256",sha1($CONF['salt'].$password.$CONF['salt_2']));
  134. return $hashed_pass;
  135. }
  136. function generate_code($key, $CONF)
  137. {
  138. $randomString = rand_string(6);
  139. $key = hash("sha256",sha1($randomString.$CONF['salt'].$key.$CONF['salt_2']));
  140. return $key;
  141. }
  142. function get_mime_type($filepath) {
  143. ob_start();
  144. system("file -i -b {$filepath}");
  145. $output = ob_get_clean();
  146. $output = explode("; ",$output);
  147. if ( is_array($output) ) {
  148. $output = $output[0];
  149. }
  150. return $output;
  151. }
  152. function set_page_title($title)
  153. {
  154. $output = ob_get_contents();
  155. // clean the buffer to avoid duplicates
  156. ob_clean();
  157. // replace the title with the generated title
  158. $output = str_replace('{title_holder}', $title,$output);
  159. // put the html back in buffer
  160. echo $output;
  161. }
  162. function redirect($url){
  163. if (headers_sent()){
  164. die('<script type="text/javascript">window.location.href="' . $url . '";</script>');
  165. }else{
  166. header('Location: ' . $url);
  167. die();
  168. }
  169. }
  170. function upload($files, $CONF, $db)
  171. {
  172. if (!empty($files)) {
  173. $filesize = filesize($files['file']['tmp_name']);
  174. $file_type = mime_content_type($files['file']['tmp_name']);
  175. if ($logged_in == 1)
  176. {
  177. $user_id = $user->id;
  178. }
  179. else
  180. {
  181. $user_id = 0;
  182. }
  183. if ($filesize <= (pow(1024, 2) * $CONF['max_upload_size']))
  184. {
  185. $iv = rand_string(32);
  186. $targetFile = upload_file($files, $CONF['upload_dir'], $CONF['key'], $iv, $CONF['cipher']);
  187. $file_used = true;
  188. while ($file_used)
  189. {
  190. $randomString = rand_string(6);
  191. $fileURL = $randomString;
  192. $result = $db->select("uploads", "url=?", array($fileURL));
  193. if (!$result)
  194. {
  195. $file_used = false;
  196. }
  197. }
  198. $data = array(
  199. "filename" => $targetFile,
  200. "url" => $fileURL,
  201. "type" => $file_type,
  202. "user_id" => $user_id,
  203. "upload_date" => date("Y-m-d H:i:s",time()),
  204. "filesize" => $filesize,
  205. "hash" => $iv,
  206. "cipher" => $CONF['cipher']
  207. );
  208. $db->insert($data, 'uploads');
  209. $_SESSION[$fileURL] = $fileURL;
  210. return array('results' => array('file' => array('name' => $fileURL, 'url' => get_page_url("u", $CONF).'/'.$fileURL, 'type' => $file_type, 'size' => $filesize)));
  211. }
  212. return array('error' => $CONF['errors']['InvFile']);
  213. }
  214. return array('error' => $CONF['errors']['NoFile']);
  215. }
  216. function upload_file($file, $destination, $key, $iv, $cipher)
  217. {
  218. $tempFile = $file['file']['tmp_name'];
  219. $fileType = pathinfo($file['file']['name'], PATHINFO_EXTENSION);
  220. $file_used = true;
  221. while ($file_used)
  222. {
  223. $randomString = rand_string(12);
  224. $targetFile = $randomString.'.'.$fileType;
  225. if (!file_exists($destination.$targetFile))
  226. {
  227. $file_used = false;
  228. }
  229. }
  230. $crypt = new Cryptography();
  231. $crypt->Encrypt($key, $iv, $tempFile, $destination.$targetFile, $cipher);
  232. $result = unlink($tempFile);
  233. return $targetFile;
  234. }
  235. function get_blog($blog_id, $db, $post_count = null, $start_post = null)
  236. {
  237. if ($post_count != null && $start_post != null)
  238. {
  239. $limit = " LIMIT ".$start_post.", ".$post_count;
  240. }
  241. else if ($post_count != null)
  242. {
  243. $limit = " LIMIT ".$post_count;
  244. }
  245. else
  246. {
  247. $limit == "";
  248. }
  249. $blog_posts = $db->select('blog', "user_id=? ORDER BY date_posted DESC".$limit, array($blog_id));
  250. $posts = array();
  251. foreach ($blog_posts as $post)
  252. {
  253. if (!is_array($post))
  254. {
  255. $posts = array($blog_posts);
  256. break;
  257. }
  258. array_push($posts, $post);
  259. }
  260. return $posts;
  261. }
  262. function get_podcast($db, $post_count = null, $start_post = null)
  263. {
  264. if ($post_count != null && $start_post != null)
  265. {
  266. $limit = " LIMIT ".$start_post.", ".$post_count;
  267. }
  268. else if ($post_count != null)
  269. {
  270. $limit = " LIMIT ".$post_count;
  271. }
  272. else
  273. {
  274. $limit == "";
  275. }
  276. $podcast_posts = $db->select('podcast', "1=? ORDER BY date_posted DESC".$limit, array(1));
  277. $posts = array();
  278. foreach ($podcast_posts as $post)
  279. {
  280. if (!is_array($post))
  281. {
  282. $posts = array($podcast_posts);
  283. break;
  284. }
  285. array_push($posts, $post);
  286. }
  287. return $posts;
  288. }
  289. function get_comments($service, $post_id, $db, $comment_count = null, $start_comment = null)
  290. {
  291. if ($comment_count != null && $start_comment != null)
  292. {
  293. $limit = " LIMIT ".$start_comment.", ".$comment_count;
  294. }
  295. else if ($comment_count != null)
  296. {
  297. $limit = " LIMIT ".$comment_count;
  298. }
  299. else
  300. {
  301. $limit == "";
  302. }
  303. $post_comments = $db->select('comments', "service=? AND reply_id=? ORDER BY date_posted ASC".$limit, array($service, $post_id));
  304. $comments = array();
  305. foreach ($post_comments as $comment)
  306. {
  307. if (!is_array($comment))
  308. {
  309. $comments = array($post_comments);
  310. break;
  311. }
  312. array_push($comments, $comment);
  313. }
  314. return $comments;
  315. }
  316. function get_post($service, $post_id, $db)
  317. {
  318. $all_posts = $db->select($service, "id=?", array($post_id));
  319. $posts = array();
  320. foreach ($all_posts as $post)
  321. {
  322. if (!is_array($post))
  323. {
  324. $posts = array($all_posts);
  325. break;
  326. }
  327. array_push($posts, $post);
  328. }
  329. return $posts;
  330. }
  331. function run_command($command, $cwd = ".") {
  332. $descriptorspec = array(
  333. 1 => array('pipe', 'w'),
  334. 2 => array('pipe', 'w'),
  335. );
  336. $pipes = array();
  337. /* Depending on the value of variables_order, $_ENV may be empty.
  338. * In that case, we have to explicitly set the new variables with
  339. * putenv, and call proc_open with env=null to inherit the reset
  340. * of the system.
  341. *
  342. * This is kind of crappy because we cannot easily restore just those
  343. * variables afterwards.
  344. *
  345. * If $_ENV is not empty, then we can just copy it and be done with it.
  346. */
  347. if(count($_ENV) === 0) {
  348. $env = NULL;
  349. foreach($this->envopts as $k => $v) {
  350. putenv(sprintf("%s=%s",$k,$v));
  351. }
  352. } else {
  353. $env = array_merge($_ENV, $this->envopts);
  354. }
  355. $resource = proc_open($command, $descriptorspec, $pipes, $cwd, $env);
  356. $stdout = stream_get_contents($pipes[1]);
  357. $stderr = stream_get_contents($pipes[2]);
  358. foreach ($pipes as $pipe) {
  359. fclose($pipe);
  360. }
  361. $status = trim(proc_close($resource));
  362. if ($status) throw new Exception($stderr);
  363. //exec($command, $stdout);
  364. return $stdout;
  365. }
  366. function mirc2html($x) {
  367. $tokenizer = new Tokenizer($x);
  368. while(list($token, $data) = $tokenizer->getNext())
  369. {
  370. switch($token)
  371. {
  372. case 'color-fgbg':
  373. printf('<%s:%d,%d>', $token, $data[1], $data[2]);
  374. break;
  375. case 'color-fg':
  376. printf('<%s:%d>', $token, $data[1]);
  377. break;
  378. case 'color-reset':
  379. case 'style-bold';
  380. printf('<%s>', $token);
  381. break;
  382. case 'catch-all':
  383. echo $data[0];
  384. break;
  385. default:
  386. throw new Exception(sprintf('Unknown token <%s>.', $token));
  387. }
  388. }
  389. //$c = array("FFF","000","00007F","009000","FF0000","7F0000","9F009F","FF7F00","FFFF00","00F800","00908F","00FFFF","0000FF","FF00FF","7F7F7F","CFD0CF");
  390. $x = preg_replace("/\x02(.*?)((?=\x02)\x02|$)/", "<b>$1</b>", $x);
  391. $x = preg_replace("/\x1F(.*?)((?=\x1F)\x1F|$)/", "<u>$1</u>", $x);
  392. $x = preg_replace("/\x1D(.*?)((?=\x1D)\x1D|$)/", "<i>$1</i>", $x);
  393. /*
  394. $x = preg_replace("/\x03(\d\d?),(\d\d?)(.*?)(?(?=\x03)|$)/e", "'</span><span style=\"color: #'.\$c[$1].'; background-color: #'.\$c[$2].';\">$3</span>'", $x);
  395. $x = preg_replace("/\x03(\d\d?)(.*?)(?(?=\x03)|$)/e", "'</span><span style=\"color: #'.\$c[$1].';\">$2</span>'", $x);
  396. $x = preg_replace("/(\x0F|\x03)(.*?)/", "<span style=\"color: #000; background-color: #FFF;\">$2</span>", $x);
  397. //$x = preg_replace("/\x16(.*?)/", "<span style=\"color: #FFF; background-color: #000;\">$1</span>", $x);
  398. //$x = preg_replace("/\<\/span\>/","",$x,1);
  399. //$x = preg_replace("/(\<\/span\>){2}/","</span>",$x);
  400. */
  401. //preg_replace_callback("/(\x03)(\d\d?,\d\d?|\d\d?)(\s?.*?)(?(?=\x03)|$)/","color_rep",trim($topic));
  402. $x = preg_replace_callback('/\^C([0-9]{1,2}),?([0-9]{1,2})(.*?)\^C/', 'mycallback', $x);
  403. return $x;
  404. }
  405. function mycallback($matches) {
  406. $bindings = array(
  407. 0=>'white',
  408. 1=>'black',
  409. 2=>'blue',
  410. 3=>'green',
  411. 4=>'red',
  412. 5=>'brown',
  413. 6=>'purple',
  414. );
  415. $c = array("FFF","000","00007F","009000","FF0000","7F0000","9F009F","FF7F00","FFFF00","00F800","00908F","00FFFF","0000FF","FF00FF","7F7F7F","CFD0CF");
  416. $fg = isset($c[$matches[1]]) ? $c[$matches[1]] : 'transparent';
  417. $bg = isset($c[$matches[2]]) ? $c[$matches[2]] : 'transparent';
  418. return '<span style="color: #'.$fg.'; background: #'.$bg.';">'.$matches[3].'</span>';
  419. }
  420. function color_rep($matches) {
  421. $matches[2] = ltrim($matches[2], "0");
  422. $bindings = array(0=>'white',1=>'black',2=>'blue',3=>'green',4=>'red',5=>'brown',6=>'purple',7=>'orange',8=>'yellow',9=>'lightgreen',10=>'#00908F',
  423. 11=>'lightblue',12=>'blue',13=>'pink',14=>'grey',15=>'lightgrey');
  424. $preg = preg_match_all('/(\d\d?),(\d\d?)/',$matches[2], $col_arr);
  425. //print_r($col_arr);
  426. $fg = isset($bindings[$matches[2]]) ? $bindings[$matches[2]] : 'transparent';
  427. if ($preg == 1) {
  428. $fg = $bindings[$col_arr[1][0]];
  429. $bg = $bindings[$col_arr[2][0]];
  430. }
  431. else {
  432. $bg = 'transparent';
  433. }
  434. return '<span style="color: '.$fg.'; background: '.$bg.';">'.$matches[3].'</span>';
  435. }
  436. function curPageURL() {
  437. $pageURL = 'http';
  438. if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
  439. $pageURL .= "://";
  440. if ($_SERVER["SERVER_PORT"] != "80") {
  441. $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  442. } else {
  443. $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  444. }
  445. return $pageURL;
  446. }
  447. /*
  448. // Extend the rcon class to tweak it for minecraft.
  449. class minecraftRcon extends rcon {
  450. function mcSendCommand($Command) {
  451. $this->_Write(SERVERDATA_EXECCOMMAND,$Command,'');
  452. }
  453. function mcRconCommand($Command) {
  454. $this->mcSendcommand($Command);
  455. $ret = $this->Read();
  456. return $ret[$this->_Id]['S1'];
  457. }
  458. function Auth () {
  459. $PackID = $this->_Write(SERVERDATA_AUTH,$this->Password);$ret = $this->_PacketRead();
  460. if ($ret[0]['ID'] == -1) {
  461. die("Authentication Failure\n");
  462. }
  463. return true;
  464. }
  465. }
  466. */
  467. function full_copy($source, $target)
  468. {
  469. if ( is_dir( $source ) )
  470. {
  471. @mkdir( $target );
  472. $d = dir( $source );
  473. while ( FALSE !== ( $entry = $d->read() ) )
  474. {
  475. if ( $entry == '.' || $entry == '..' )
  476. {
  477. continue;
  478. }
  479. $Entry = $source . '/' . $entry;
  480. if ( is_dir( $Entry ) )
  481. {
  482. full_copy( $Entry, $target . '/' . $entry );
  483. continue;
  484. }
  485. copy( $Entry, $target . '/' . $entry );
  486. }
  487. $d->close();
  488. }
  489. else
  490. {
  491. copy( $source, $target );
  492. }
  493. }
  494. function deleteAll($directory, $empty = false)
  495. {
  496. if(substr($directory,-1) == "/") {
  497. $directory = substr($directory,0,-1);
  498. }
  499. if(!file_exists($directory) || !is_dir($directory)) {
  500. return false;
  501. } elseif(!is_readable($directory)) {
  502. return false;
  503. } else {
  504. $directoryHandle = opendir($directory);
  505. while ($contents = readdir($directoryHandle)) {
  506. if($contents != '.' && $contents != '..') {
  507. $path = $directory . "/" . $contents;
  508. if(is_dir($path)) {
  509. deleteAll($path);
  510. } else {
  511. unlink($path);
  512. }
  513. }
  514. }
  515. closedir($directoryHandle);
  516. if($empty == false) {
  517. if(!rmdir($directory)) {
  518. return false;
  519. }
  520. }
  521. return true;
  522. }
  523. }
  524. function bytesToSize($bytes, $precision = 2)
  525. {
  526. $kilobyte = 1024;
  527. $megabyte = $kilobyte * 1024;
  528. $gigabyte = $megabyte * 1024;
  529. $terabyte = $gigabyte * 1024;
  530. if (($bytes >= 0) && ($bytes < $kilobyte)) {
  531. return $bytes . ' B';
  532. } elseif (($bytes >= $kilobyte) && ($bytes < $megabyte)) {
  533. return round($bytes / $kilobyte, $precision) . ' KB';
  534. } elseif (($bytes >= $megabyte) && ($bytes < $gigabyte)) {
  535. return round($bytes / $megabyte, $precision) . ' MB';
  536. } elseif (($bytes >= $gigabyte) && ($bytes < $terabyte)) {
  537. return round($bytes / $gigabyte, $precision) . ' GB';
  538. } elseif ($bytes >= $terabyte) {
  539. return round($bytes / $terabyte, $precision) . ' TB';
  540. } else {
  541. return $bytes . ' B';
  542. }
  543. }
  544. function trim_value(&$value)
  545. {
  546. $value = trim($value); // this removes whitespace and related characters from the beginning and end of the string
  547. }
  548. function compress($buffer)
  549. {
  550. /* remove comments */
  551. $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
  552. /* remove tabs, spaces, newlines, etc. */
  553. $buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);
  554. return $buffer;
  555. }
  556. ?>