123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660 |
- <?php
- function get_page_url($page, $CONF, $full = true)
- {
- $full_url = get_http($CONF).$CONF['host'];
- switch ($CONF['url_type'])
- {
- case 'sub':
- if ($page == $CONF['default_page'])
- {
- $page = 'www';
- }
- $full_url = get_subdomain_full($page, $CONF);
- break;
- case 'page':
- $cur_sub = get_subdomain($CONF);
- $full_url = get_http($CONF).$cur_sub.".".$CONF['host']."/".$page;
- break;
- }
- return $full_url;
- }
-
- function get_subdomain_full($sub_part, $CONF)
- {
- $url = get_http($CONF).$sub_part.".".$CONF['host'];
- return $url;
- }
-
- function extract_domain($domain)
- {
- if(preg_match("/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i", $domain, $matches))
- {
- return $matches['domain'];
- } else {
- return $domain;
- }
- }
-
- function extract_subdomains($domain)
- {
- $subdomains = $domain;
- $domain = extract_domain($subdomains);
-
- $subdomains = rtrim(strstr($subdomains, $domain, true), '.');
-
- return $subdomains;
- }
-
- function get_subdomain($CONF)
- {
- $sub = extract_subdomains($_SERVER['HTTP_HOST']);
- if ($sub == "" || $sub == "www")
- {
- $sub = $CONF['default_page'];
- }
- return $sub;
- }
-
- function get_page()
- {
- $url_array = explode("/",$_SERVER["REQUEST_URI"]);
-
- return ltrim($url_array[1], "/");
- }
-
- function get_http($CONF)
- {
- if ($CONF['https'] != "on")
- {
- $http = "http://";
- }
- else
- {
- $http = "https://";
- }
- return $http;
- }
-
- function get_active($page, $CONF)
- {
- $cur_page = '';
- switch ($CONF['url_type'])
- {
- case 'sub':
- $cur_page = get_subdomain($CONF);
- break;
- case 'page':
- $cur_page = get_page();
- break;
- }
- if ($cur_page == $page)
- {
- return 'active';
- }
- return "";
- }
-
- function checkemail($email)
- {
- return preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $email) ? TRUE : FALSE;
- }
-
- function safe($input)
- {
- $valid_input = addslashes($input);
- return $valid_input;
- }
-
- function safe_register($input)
- {
- $input = rawurlencode($input);
- $valid_input = mysql_real_escape_string($input);
- return $valid_input;
- }
-
- function rand_string( $length ) {
- $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
-
- $size = strlen( $chars );
- for( $i = 0; $i < $length; $i++ ) {
- $str .= $chars[ rand( 0, $size - 1 ) ];
- }
-
- return $str;
- }
-
- function hashPassword($password, $CONF)
- {
- $hashed_pass = hash("sha256",sha1($CONF['salt'].$password.$CONF['salt_2']));
- return $hashed_pass;
- }
-
- function generate_code($key, $CONF)
- {
- $randomString = rand_string(6);
- $key = hash("sha256",sha1($randomString.$CONF['salt'].$key.$CONF['salt_2']));
- return $key;
- }
-
- function get_mime_type($filepath) {
- ob_start();
- system("file -i -b {$filepath}");
- $output = ob_get_clean();
- $output = explode("; ",$output);
- if ( is_array($output) ) {
- $output = $output[0];
- }
- return $output;
- }
-
- function set_page_title($title)
- {
-
- $output = ob_get_contents();
- // clean the buffer to avoid duplicates
- ob_clean();
- // replace the title with the generated title
- $output = str_replace('{title_holder}', $title,$output);
- // put the html back in buffer
- echo $output;
-
- }
-
- function redirect($url){
- if (headers_sent()){
- die('<script type="text/javascript">window.location.href="' . $url . '";</script>');
- }else{
- header('Location: ' . $url);
- die();
- }
- }
-
- function upload($files, $CONF, $db)
- {
- if (!empty($files)) {
- $filesize = filesize($files['file']['tmp_name']);
- $file_type = mime_content_type($files['file']['tmp_name']);
- $ext = pathinfo($files['file']['name'], PATHINFO_EXTENSION);
- if ($logged_in == 1)
- {
- $user_id = $user->id;
- }
- else
- {
- $user_id = 0;
- }
- if ($filesize <= (pow(1024, 2) * $CONF['max_upload_size']))
- {
- $iv = rand_string(32);
- $targetFile = upload_file($files, $CONF['upload_dir'], $CONF['key'], $iv, $CONF['cipher']);
- $file_used = true;
- while ($file_used)
- {
- $randomString = rand_string(6);
- if (empty($ext)) {
- $fileURL = $randomString;
- } else {
- $fileURL = $randomString.".".$ext;
- }
- $result = $db->select("uploads", "url=?", array($fileURL));
- if (!$result)
- {
- $file_used = false;
- }
- }
- $data = array(
- "filename" => $targetFile,
- "url" => $fileURL,
- "type" => $file_type,
- "user_id" => $user_id,
- "upload_date" => date("Y-m-d H:i:s",time()),
- "filesize" => $filesize,
- "hash" => $iv,
- "cipher" => $CONF['cipher']
- );
- $db->insert($data, 'uploads');
- $_SESSION[$fileURL] = $fileURL;
- return array('results' => array('file' => array('name' => $fileURL, 'url' => get_page_url("u", $CONF).'/'.$fileURL, 'type' => $file_type, 'size' => $filesize)));
- }
- return array('error' => $CONF['errors']['InvFile']);
- }
- return array('error' => $CONF['errors']['NoFile']);
- }
-
- function upload_file($file, $destination, $key, $iv, $cipher)
- {
- $tempFile = $file['file']['tmp_name'];
-
- $fileType = pathinfo($file['file']['name'], PATHINFO_EXTENSION);
- $file_used = true;
- while ($file_used)
- {
- $randomString = rand_string(12);
- $targetFile = $randomString.'.'.$fileType;
- if (!file_exists($destination.$targetFile))
- {
- $file_used = false;
- }
- }
-
- $crypt = new Cryptography();
- $crypt->Encrypt($key, $iv, $tempFile, $destination.$targetFile, $cipher);
- $result = unlink($tempFile);
- return $targetFile;
- }
-
- function get_blog($blog_id, $db, $post_count = null, $start_post = null)
- {
- if ($post_count != null && $start_post != null)
- {
- $limit = " LIMIT ".$start_post.", ".$post_count;
- }
- else if ($post_count != null)
- {
- $limit = " LIMIT ".$post_count;
- }
- else
- {
- $limit == "";
- }
-
- $blog_posts = $db->select('blog', "user_id=? ORDER BY date_posted DESC".$limit, array($blog_id));
-
- $posts = array();
- foreach ($blog_posts as $post)
- {
- if (!is_array($post))
- {
- $posts = array($blog_posts);
- break;
- }
- array_push($posts, $post);
- }
-
- return $posts;
- }
-
- function get_podcast($db, $post_count = null, $start_post = null)
- {
- if ($post_count != null && $start_post != null)
- {
- $limit = " LIMIT ".$start_post.", ".$post_count;
- }
- else if ($post_count != null)
- {
- $limit = " LIMIT ".$post_count;
- }
- else
- {
- $limit == "";
- }
-
- $podcast_posts = $db->select('podcast', "1=? ORDER BY date_posted DESC".$limit, array(1));
-
- $posts = array();
- foreach ($podcast_posts as $post)
- {
- if (!is_array($post))
- {
- $posts = array($podcast_posts);
- break;
- }
- array_push($posts, $post);
- }
-
- return $posts;
- }
-
- function get_comments($service, $post_id, $db, $comment_count = null, $start_comment = null)
- {
- if ($comment_count != null && $start_comment != null)
- {
- $limit = " LIMIT ".$start_comment.", ".$comment_count;
- }
- else if ($comment_count != null)
- {
- $limit = " LIMIT ".$comment_count;
- }
- else
- {
- $limit == "";
- }
-
- $post_comments = $db->select('comments', "service=? AND reply_id=? ORDER BY date_posted ASC".$limit, array($service, $post_id));
-
- $comments = array();
- foreach ($post_comments as $comment)
- {
- if (!is_array($comment))
- {
- $comments = array($post_comments);
- break;
- }
- array_push($comments, $comment);
- }
-
- return $comments;
- }
-
- function get_post($service, $post_id, $db)
- {
- $all_posts = $db->select($service, "id=?", array($post_id));
-
- $posts = array();
- foreach ($all_posts as $post)
- {
- if (!is_array($post))
- {
- $posts = array($all_posts);
- break;
- }
- array_push($posts, $post);
- }
-
- return $posts;
- }
-
- function run_command($command, $cwd = ".") {
- $descriptorspec = array(
- 1 => array('pipe', 'w'),
- 2 => array('pipe', 'w'),
- );
- $pipes = array();
- /* Depending on the value of variables_order, $_ENV may be empty.
- * In that case, we have to explicitly set the new variables with
- * putenv, and call proc_open with env=null to inherit the reset
- * of the system.
- *
- * This is kind of crappy because we cannot easily restore just those
- * variables afterwards.
- *
- * If $_ENV is not empty, then we can just copy it and be done with it.
- */
- if(count($_ENV) === 0) {
- $env = NULL;
- foreach($this->envopts as $k => $v) {
- putenv(sprintf("%s=%s",$k,$v));
- }
- } else {
- $env = array_merge($_ENV, $this->envopts);
- }
- $resource = proc_open($command, $descriptorspec, $pipes, $cwd, $env);
- $stdout = stream_get_contents($pipes[1]);
- $stderr = stream_get_contents($pipes[2]);
- foreach ($pipes as $pipe) {
- fclose($pipe);
- }
-
- $status = trim(proc_close($resource));
- if ($status) throw new Exception($stderr);
- //exec($command, $stdout);
- return $stdout;
- }
-
- function mirc2html($x) {
-
- $tokenizer = new Tokenizer($x);
-
- while(list($token, $data) = $tokenizer->getNext())
- {
- switch($token)
- {
- case 'color-fgbg':
- printf('<%s:%d,%d>', $token, $data[1], $data[2]);
- break;
-
- case 'color-fg':
- printf('<%s:%d>', $token, $data[1]);
- break;
-
- case 'color-reset':
- case 'style-bold';
- printf('<%s>', $token);
- break;
-
- case 'catch-all':
- echo $data[0];
- break;
-
- default:
- throw new Exception(sprintf('Unknown token <%s>.', $token));
- }
- }
-
- //$c = array("FFF","000","00007F","009000","FF0000","7F0000","9F009F","FF7F00","FFFF00","00F800","00908F","00FFFF","0000FF","FF00FF","7F7F7F","CFD0CF");
- $x = preg_replace("/\x02(.*?)((?=\x02)\x02|$)/", "<b>$1</b>", $x);
- $x = preg_replace("/\x1F(.*?)((?=\x1F)\x1F|$)/", "<u>$1</u>", $x);
- $x = preg_replace("/\x1D(.*?)((?=\x1D)\x1D|$)/", "<i>$1</i>", $x);
- /*
- $x = preg_replace("/\x03(\d\d?),(\d\d?)(.*?)(?(?=\x03)|$)/e", "'</span><span style=\"color: #'.\$c[$1].'; background-color: #'.\$c[$2].';\">$3</span>'", $x);
- $x = preg_replace("/\x03(\d\d?)(.*?)(?(?=\x03)|$)/e", "'</span><span style=\"color: #'.\$c[$1].';\">$2</span>'", $x);
- $x = preg_replace("/(\x0F|\x03)(.*?)/", "<span style=\"color: #000; background-color: #FFF;\">$2</span>", $x);
- //$x = preg_replace("/\x16(.*?)/", "<span style=\"color: #FFF; background-color: #000;\">$1</span>", $x);
- //$x = preg_replace("/\<\/span\>/","",$x,1);
- //$x = preg_replace("/(\<\/span\>){2}/","</span>",$x);
- */
- //preg_replace_callback("/(\x03)(\d\d?,\d\d?|\d\d?)(\s?.*?)(?(?=\x03)|$)/","color_rep",trim($topic));
- $x = preg_replace_callback('/\^C([0-9]{1,2}),?([0-9]{1,2})(.*?)\^C/', 'mycallback', $x);
- return $x;
- }
-
- function mycallback($matches) {
- $bindings = array(
- 0=>'white',
- 1=>'black',
- 2=>'blue',
- 3=>'green',
- 4=>'red',
- 5=>'brown',
- 6=>'purple',
- );
- $c = array("FFF","000","00007F","009000","FF0000","7F0000","9F009F","FF7F00","FFFF00","00F800","00908F","00FFFF","0000FF","FF00FF","7F7F7F","CFD0CF");
-
- $fg = isset($c[$matches[1]]) ? $c[$matches[1]] : 'transparent';
- $bg = isset($c[$matches[2]]) ? $c[$matches[2]] : 'transparent';
-
- return '<span style="color: #'.$fg.'; background: #'.$bg.';">'.$matches[3].'</span>';
- }
-
-
- function color_rep($matches) {
- $matches[2] = ltrim($matches[2], "0");
- $bindings = array(0=>'white',1=>'black',2=>'blue',3=>'green',4=>'red',5=>'brown',6=>'purple',7=>'orange',8=>'yellow',9=>'lightgreen',10=>'#00908F',
- 11=>'lightblue',12=>'blue',13=>'pink',14=>'grey',15=>'lightgrey');
- $preg = preg_match_all('/(\d\d?),(\d\d?)/',$matches[2], $col_arr);
- //print_r($col_arr);
- $fg = isset($bindings[$matches[2]]) ? $bindings[$matches[2]] : 'transparent';
- if ($preg == 1) {
- $fg = $bindings[$col_arr[1][0]];
- $bg = $bindings[$col_arr[2][0]];
- }
- else {
- $bg = 'transparent';
- }
-
- return '<span style="color: '.$fg.'; background: '.$bg.';">'.$matches[3].'</span>';
- }
-
- function curPageURL() {
- $pageURL = 'http';
- if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
- $pageURL .= "://";
- if ($_SERVER["SERVER_PORT"] != "80") {
- $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
- } else {
- $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
- }
- return $pageURL;
- }
-
-
- /*
- // Extend the rcon class to tweak it for minecraft.
- class minecraftRcon extends rcon {
- function mcSendCommand($Command) {
- $this->_Write(SERVERDATA_EXECCOMMAND,$Command,'');
- }
-
- function mcRconCommand($Command) {
- $this->mcSendcommand($Command);
-
- $ret = $this->Read();
-
- return $ret[$this->_Id]['S1'];
- }
-
- function Auth () {
- $PackID = $this->_Write(SERVERDATA_AUTH,$this->Password);$ret = $this->_PacketRead();
- if ($ret[0]['ID'] == -1) {
- die("Authentication Failure\n");
- }
- return true;
- }
- }
- */
- function full_copy($source, $target)
- {
- if ( is_dir( $source ) )
- {
- @mkdir( $target );
-
- $d = dir( $source );
-
- while ( FALSE !== ( $entry = $d->read() ) )
- {
- if ( $entry == '.' || $entry == '..' )
- {
- continue;
- }
-
- $Entry = $source . '/' . $entry;
- if ( is_dir( $Entry ) )
- {
- full_copy( $Entry, $target . '/' . $entry );
- continue;
- }
- copy( $Entry, $target . '/' . $entry );
- }
-
- $d->close();
- }
- else
- {
- copy( $source, $target );
- }
- }
-
- function deleteAll($directory, $empty = false)
- {
- if(substr($directory,-1) == "/") {
- $directory = substr($directory,0,-1);
- }
-
- if(!file_exists($directory) || !is_dir($directory)) {
- return false;
- } elseif(!is_readable($directory)) {
- return false;
- } else {
- $directoryHandle = opendir($directory);
-
- while ($contents = readdir($directoryHandle)) {
- if($contents != '.' && $contents != '..') {
- $path = $directory . "/" . $contents;
-
- if(is_dir($path)) {
- deleteAll($path);
- } else {
- unlink($path);
- }
- }
- }
-
- closedir($directoryHandle);
-
- if($empty == false) {
- if(!rmdir($directory)) {
- return false;
- }
- }
- return true;
- }
- }
-
- function bytesToSize($bytes, $precision = 2)
- {
- $kilobyte = 1024;
- $megabyte = $kilobyte * 1024;
- $gigabyte = $megabyte * 1024;
- $terabyte = $gigabyte * 1024;
-
- if (($bytes >= 0) && ($bytes < $kilobyte)) {
- return $bytes . ' B';
-
- } elseif (($bytes >= $kilobyte) && ($bytes < $megabyte)) {
- return round($bytes / $kilobyte, $precision) . ' KB';
-
- } elseif (($bytes >= $megabyte) && ($bytes < $gigabyte)) {
- return round($bytes / $megabyte, $precision) . ' MB';
-
- } elseif (($bytes >= $gigabyte) && ($bytes < $terabyte)) {
- return round($bytes / $gigabyte, $precision) . ' GB';
-
- } elseif ($bytes >= $terabyte) {
- return round($bytes / $terabyte, $precision) . ' TB';
- } else {
- return $bytes . ' B';
- }
- }
-
- function trim_value(&$value)
- {
- $value = trim($value); // this removes whitespace and related characters from the beginning and end of the string
- }
-
- function compress($buffer)
- {
- /* remove comments */
- $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
- /* remove tabs, spaces, newlines, etc. */
- $buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);
- return $buffer;
- }
-
- function is_dir_empty($dir)
- {
- if (!is_readable($dir)) return NULL;
- $handle = opendir($dir);
- while (false !== ($entry = readdir($handle)))
- {
- if ($entry != "." && $entry != "..")
- {
- return FALSE;
- }
- }
- return TRUE;
- }
-
- function multi_array_search($array, $search)
- {
- // Create the result array
- $result = array();
-
- // Iterate over each array element
- foreach ($array as $key => $value)
- {
- // Iterate over each search condition
- foreach ($search as $k => $v)
- {
- // If the array element does not meet the search condition then continue to the next element
- if (!isset($value[$k]) || $value[$k] != $v)
- {
- continue 2;
- }
- }
- // Add the array element's key to the result array
- $result[] = $key;
- }
- // Return the result array
- return $result;
- }
- ?>
|