Advertisement
bondcemil

globals.php

Dec 21st, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.41 KB | None | 0 0
  1. <?php
  2. if( !function_exists('array_fill_keys') )
  3. {
  4.     function array_fill_keys($keys, $value)
  5.     {
  6.         $return_array = array();
  7.         foreach($keys as $key)
  8.         {
  9.             $return_array[$key] = $value;
  10.         }
  11.         return $return_array;
  12.     }
  13. }
  14.  
  15. function str_replace_first($search, $replace, $data)
  16. {
  17.     $res = strpos($data, $search);
  18.     if($res === false)
  19.     {
  20.         return $data;
  21.     }
  22.     else
  23.     {
  24.         $left_seg = substr($data, 0, strpos($data, $search));
  25.         $right_seg = substr($data, (strpos($data, $search) + strlen($search)));
  26.         return $left_seg . $replace . $right_seg;
  27.     }
  28. }  
  29. function array_remove_null(&$array, $maintian_keys=true){
  30.     $new_array = array();
  31.     if(is_array($array)){
  32.         foreach($array as $k=>$v){
  33.             if($v&&$maintian_keys) $new_array[$k]=$v;
  34.             elseif($v) $new_array[]=$v;
  35.         }
  36.     }
  37.     $array=$new_array;
  38.     return $array;
  39. }
  40.  
  41. function array_key_index($key, $array){
  42.     $counter=0;
  43.     foreach($array as $k=>$v){
  44.         if($k==$key){
  45.             return $counter;
  46.         }
  47.         $counter++;
  48.     }
  49. }
  50.  
  51. function form_data($string, $encoding='UTF-8'){
  52.     return htmlentities(html_entity_decode($string, ENT_QUOTES, $encoding), ENT_QUOTES, $encoding);
  53. }
  54.  
  55. function slug($string){
  56.     $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
  57.     $string = strip_tags($string);
  58.     $string = trim(preg_replace("#[^a-zA-Z0-9 '/-]#", "", $string));
  59.     $string = str_replace(array(' ', '/', '\''), '-', $string);
  60.     $string = preg_replace('#-+#', '-', $string);
  61.     $string = strtolower($string);
  62.     return $string;
  63. }
  64.  
  65. function render_content( $string ){
  66.     $string = str_replace('<hr />', '<div class="hr-light hr-spacing"></div>', $string);
  67.     return $string;
  68. }
  69.  
  70. function clean_string($string){
  71.     $string = slug($string);
  72.     $string = str_replace('-', ' ', $string);
  73.     return $string;
  74. }
  75.  
  76. function file_size_format($bytes)
  77. {
  78.     if($bytes <= 1000000){
  79.         return round($bytes / 1000, 2) . " Kb";
  80.     }elseif($byteSize <= 1000000000){
  81.         return round($bytes / 1000000, 2) . " Mb";
  82.     }elseif($byteSize > 1000000000){
  83.         return round($bytes / 1000000000, 2) . " Gb";
  84.     }
  85. }
  86.  
  87. function nice_trim($string, $max_length, $hellip=true){
  88.     $string = trim($string);
  89.     if(strlen($string) > $max_length){
  90.         return substr($string, 0, $max_length).($hellip?'&hellip;':null);
  91.     }else{
  92.         return $string;
  93.     }
  94. }
  95.  
  96. function path_clean($path){
  97.     return preg_replace("#/+#", "/", $path);
  98. }
  99.  
  100. function get($query_string){
  101.     $get = array();
  102.     foreach(explode('&', $query_string) as $get_full){
  103.         list($key, $value)=explode('=', $get_full);
  104.         if(preg_match('@(.+?)\[(.+?)\]@', $key, $matches)){
  105.             $get[$matches[1]][$matches[2]]=$value;
  106.         }else{
  107.             $get[$key]=$value;
  108.         }
  109.     }
  110.     return $get;
  111. }
  112.  
  113. function array_merge_replace(array $base_array, array $merge_array, $depth = 0)
  114. {
  115.     $new_array = array();
  116.  
  117.     $key_list = array_unique( array_merge( array_keys( $base_array ), array_keys( $merge_array) ) );
  118.  
  119.     foreach($key_list as $k)
  120.     {
  121.         if( !array_key_exists($k, $base_array) && array_key_exists($k, $merge_array) )
  122.         {
  123.             $new_array[$k] = $merge_array[$k];
  124.         }
  125.         elseif( array_key_exists($k, $base_array) && !array_key_exists($k, $merge_array) )
  126.         {
  127.             $new_array[$k] = $base_array[$k];
  128.         }
  129.         elseif( array_key_exists($k, $base_array) && array_key_exists($k, $merge_array) )
  130.         {
  131.             if( ( $depth !== 1 ) && is_array($base_array[$k]) && is_array($merge_array[$k]) )
  132.             {
  133.                 $new_array[$k] = array_merge_replace($base_array[$k], $merge_array[$k], $depth - 1 );
  134.             }
  135.             else
  136.             {
  137.                 $new_array[$k] = $merge_array[$k];
  138.             }
  139.         }
  140.  
  141.     }
  142.    
  143.     return $new_array;
  144.  
  145. }
  146.  
  147. function write_ini_file($assoc_arr, $path, $has_sections = false) {
  148.     $content = "";
  149.     if ($has_sections) {
  150.         foreach ($assoc_arr as $key=>$elem) {
  151.             $content .= "[".$key."]\n";
  152.             foreach ($elem as $key2=>$elem2) {
  153.                 if(is_array($elem2))
  154.                 {
  155.                     for($i=0;$i<count($elem2);$i++)
  156.                     {
  157.                         $content .= $key2."[] = \"".MK_Utility::escapeText($elem2[$i])."\"\n";
  158.                     }
  159.                 }
  160.                 else if($elem2=="") $content .= $key2." = \n";
  161.                 else $content .= $key2." = \"".MK_Utility::escapeText($elem2)."\"\n";
  162.             }
  163.         }
  164.     }
  165.     else {
  166.         foreach ($assoc_arr as $key=>$elem) {
  167.             if(is_array($elem))
  168.             {
  169.                 for($i=0;$i<count($elem);$i++)
  170.                 {
  171.                     $content .= $key."[] = \"".MK_Utility::escapeText($elem[$i])."\"\n";
  172.                 }
  173.             }
  174.             else if($elem=="") $content .= $key." = \n";
  175.             else $content .= $key." = \"".MK_Utility::escapeText($elem)."\"\n";
  176.         }
  177.     }
  178.  
  179.     if (!$handle = fopen($path, 'w')) {
  180.         return false;
  181.     }
  182.  
  183.     if (!fwrite($handle, $content)) {
  184.         return false;
  185.     }
  186.     fclose($handle);
  187.     return true;
  188. }
  189.  
  190.  
  191. function Friendly_URL($video_id){
  192. global $site_url;
  193.   $new_url= $site_url.'/video/'.$video_id.'/';
  194.     return $new_url;
  195.  
  196. }
  197.  
  198. function Get_Thumb($video_id){
  199.  
  200.  $thumb_url= 'http://i4.ytimg.com/vi/'.$video_id.'/2.jpg';
  201.  
  202.  return $thumb_url;
  203.  
  204. }
  205.  
  206. function SpecialChars($string){
  207.  
  208.   if ( is_array($string) ):
  209.  
  210.     return $string;
  211.  
  212.   else:
  213.  
  214.     return htmlspecialchars($string);
  215.  
  216.   endif;
  217.  
  218.  
  219.  
  220. }
  221.  
  222.  
  223. function Security($string){
  224.  
  225.  
  226.  
  227.   $security_vulns = "DELETE |INSERT |UPDATE |RENAME |MYSQL |SQL ";
  228.  
  229.  
  230.  
  231.   $string_check = strtoupper($string);
  232.  
  233.   $do_it = explode("|", $security_vulns);
  234.  
  235.  
  236.  
  237.   static $isok = true;
  238.  
  239.  
  240.  
  241.   foreach($do_it as $vuln):
  242.  
  243.     if ( strstr($string_check, $vuln) ) { $isok = false; }
  244.  
  245.   endforeach;
  246.  
  247.  
  248.  
  249.   if ($isok) { return $string; } else { die(); }
  250.  
  251.  
  252.  
  253. }
  254.  
  255.  
  256. /* Let's prepare an advanced functions wich can make text readable in the url for stranger characters*/
  257.  
  258. $vibe_charset = "UTF-8";
  259. function seo_clean_url($text)
  260. {
  261.     global $vibe_charset;
  262.  
  263.     //  Do you know your ABCs?
  264.     $characterHash = array (
  265.         'a' =>  array ('a', 'A', 'à', 'À', 'á', 'Á', 'â', 'Â', 'ã', 'Ã', 'ä', 'Ä', 'å', 'Å', 'ª', 'a', 'A', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', 'A', 'a', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?'),
  266.         'ae'    =>  array ('æ', 'Æ'),
  267.         'b' =>  array ('b', 'B'),
  268.         'c' =>  array ('c', 'C', 'ç', 'Ç', 'c', 'C', 'c', 'C'),
  269.         'd' =>  array ('d', 'D', '?', 'd', 'D', 'd', 'D'),
  270.         'e' =>  array ('e', 'E', 'è', 'È', 'é', 'É', 'ê', 'Ê', 'ë', 'Ë', 'e', 'E', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', 'e', 'E'),
  271.         'f' =>  array ('f', 'F'),
  272.         'g' =>  array ('g', 'G', 'ğ', 'Ğ'),
  273.         'h' =>  array ('h', 'H'),
  274.         'i' =>  array ('i', 'I', 'ì', 'Ì', 'í', 'Í', 'î', 'Î', 'ï', 'Ï', 'ı', 'İ', '?', '?', '?', '?', 'I', 'i', '?', '?'),
  275.         'j' =>  array ('j', 'J'),
  276.         'k' =>  array ('k', 'K', '?', '?', '?', '?'),
  277.         'l' =>  array ('l', 'L', 'l', 'L'),
  278.         'm' =>  array ('m', 'M', '?', '?', '?'),
  279.         'n' =>  array ('n', 'N', 'ñ', 'Ñ', 'n', 'N', 'n', 'N'),
  280.         'o' =>  array ('o', 'O', 'ò', 'Ò', 'ó', 'Ó', 'ô', 'Ô', 'õ', 'Õ', 'ö', 'Ö', 'ø', 'Ø', 'º', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', 'O', 'o', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?'),
  281.         'p' =>  array ('p', 'P'),
  282.         'q' =>  array ('q', 'Q'),
  283.         'r' =>  array ('r', 'R', 'r', 'R'),
  284.         's' =>  array ('s', 'S', 'ş', 'Ş', 's', 'S', 'š', 'Š'),
  285.         'ss'    =>  array ('ß'),
  286.         't' =>  array ('t', 'T', '?', '?', '?', '?', 't', 'T', 't', 'T'),
  287.         'u' =>  array ('u', 'U', 'ù', 'Ù', 'ú', 'Ú', 'û', 'Û', 'ü', 'Ü', '?', '?', '?', '?', 'U', 'u', 'U', 'u', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', 'u', 'U'),
  288.         'v' =>  array ('v', 'V'),
  289.         'w' =>  array ('w', 'W'),
  290.         'x' =>  array ('x', 'X', '×'),
  291.         'y' =>  array ('y', 'Y', 'y', 'Y', 'ÿ', '?', '?', '?', '?', '?', '?', '?', '?'),
  292.         'z' =>  array ('z', 'Z', 'z', 'Z', 'z', 'Z', 'z', 'Z', '?'),
  293.         '-' =>  array ('-', ' ', '.', ','),
  294.         '_' =>  array ('_'),
  295.         '!' =>  array ('!'),
  296.         '~' =>  array ('~'),
  297.         '*' =>  array ('*'),
  298.         "\x12"  =>  array ("'", '"'),
  299.         '(' =>  array ('(', '{', '['),
  300.         ')' =>  array (')', '}', ']'),
  301.         '$' =>  array ('$'),
  302.         '0' =>  array ('0'),
  303.         '1' =>  array ('1', '¹'),
  304.         '2' =>  array ('2', '²'),
  305.         '3' =>  array ('3', '³'),
  306.         '4' =>  array ('4'),
  307.         '5' =>  array ('5'),
  308.         '6' =>  array ('6'),
  309.         '7' =>  array ('7'),
  310.         '8' =>  array ('8'),
  311.         '9' =>  array ('9'),
  312.     );
  313.  
  314.  
  315.     //  Get or detect the database encoding, firstly from the settings or language files
  316.     if (preg_match('~.~su', $text))
  317.         $encoding = 'UTF-8';
  318.     //  or sadly... we may have to assume Latin-1
  319.     else
  320.         $encoding = 'ISO-8859-1';
  321.  
  322.     //  If the database encoding isn't UTF-8 and multibyte string functions are available, try converting the text to UTF-8
  323.     if ($encoding != 'UTF-8' && function_exists('mb_convert_encoding'))
  324.         $text = mb_convert_encoding($text, 'UTF-8', $encoding);
  325.     //  Or maybe we can convert with iconv
  326.     else if ($encoding != 'UTF-8' && function_exists('iconv'))
  327.         $text = iconv($encoding, 'UTF-8', $text);
  328.     //  Fix Turkish
  329.     else if ($encoding == 'ISO-8859-9')
  330.     {
  331.         $text = str_replace(array("\xD0", "\xDD", "\xDE", "\xF0", "\xFD", "\xFE"), array('g', 'i', 's', 'g', 'i', 's'), $text);
  332.         $text = utf8_encode($text);
  333.     }
  334.     //  Latin-1 can be converted easily
  335.     else if ($encoding == 'ISO-8859-1')
  336.         $text = utf8_encode($text);
  337.  
  338.     //  Change the entities back to normal characters
  339.     $text = str_replace(array('&amp;', '&quot;'), array('&', '"'), $text);
  340.     $prettytext = '';
  341.  
  342.     //  Split up $text into UTF-8 letters
  343.     preg_match_all("~.~su", $text, $characters);
  344.     foreach ($characters[0] as $aLetter)
  345.     {
  346.         foreach ($characterHash as $replace => $search)
  347.         {
  348.             //  Found a character? Replace it!
  349.             if (in_array($aLetter, $search))
  350.             {
  351.                 $prettytext .= $replace;
  352.                 break;
  353.             }
  354.         }
  355.     }
  356.     //  Remove unwanted '-'s
  357.     $prettytext = preg_replace(array('~^-+|-+$~', '~-+~'), array('', '-'), $prettytext);
  358.     if (empty($prettytext)) { $prettytext = 'video'; }
  359.  
  360.     return $prettytext;
  361. }
  362.  
  363. // Filtering Function
  364.  
  365. function filterBadWords($badWords,$str) {
  366.   $badFlag = 0;
  367.   if(!empty($badWords)) {  
  368.  
  369.     $badWordsArray = explode("|", $badWords);
  370.   }
  371.   foreach ($badWordsArray as $badWord) {
  372.  
  373.   //echo $badWord;
  374.     if(!$badWord) continue;
  375.     else {
  376.       $regexp = "/\b".$badWord."\b/i";
  377.       if(preg_match($regexp,$str)) $badFlag = 1;
  378.     }
  379.   }
  380.     if(preg_match("/\[url/",$str)) $badFlag = 1;
  381.   return $badFlag;
  382. }
  383.  
  384. function sec2hms ($sec, $padHours = false) {
  385.  
  386.     $hms = "";
  387.    
  388.     // there are 3600 seconds in an hour, so if we
  389.     // divide total seconds by 3600 and throw away
  390.     // the remainder, we've got the number of hours
  391.     $hours = intval(intval($sec) / 3600);
  392.  if ($hours > 0):
  393.     // add to $hms, with a leading 0 if asked for
  394.     $hms .= ($padHours)
  395.           ? str_pad($hours, 2, "0", STR_PAD_LEFT). ':'
  396.           : $hours. ':';
  397.      endif;
  398.     // dividing the total seconds by 60 will give us
  399.     // the number of minutes, but we're interested in
  400.     // minutes past the hour: to get that, we need to
  401.     // divide by 60 again and keep the remainder
  402.     $minutes = intval(($sec / 60) % 60);
  403.  
  404.     // then add to $hms (with a leading 0 if needed)
  405.     $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':';
  406.  
  407.     // seconds are simple - just divide the total
  408.     // seconds by 60 and keep the remainder
  409.     $seconds = intval($sec % 60);
  410.  
  411.     // add to $hms, again with a leading 0 if needed
  412.     $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
  413.  
  414.     return $hms;
  415. }
  416.  
  417.  
  418.  
  419. $yt_err = '';
  420. function grab_video($url)
  421. {
  422.     global $yt_err;
  423.    
  424.     $yt_data = array();
  425.     $str        = '';
  426.     $yt_vid_id      = '';
  427.     $yt_target = '';
  428.    
  429.     preg_match("/v=([^(\&|$)]*)/", $url, $matches);
  430.     $yt_vid_id = $matches[1];
  431.    
  432.     $yt_target = "http://gdata.youtube.com/feeds/api/videos/" . $yt_vid_id;
  433.    
  434.     $error = 0;
  435.     if(function_exists('curl_init'))
  436.     {
  437.         $ch = curl_init();
  438.         curl_setopt($ch, CURLOPT_URL, $yt_target);
  439.         curl_setopt($ch, CURLOPT_HEADER, 0);
  440.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  441.         curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
  442.         $yt_data = curl_exec($ch);
  443.         $errormsg = curl_error($ch);
  444.         curl_close($ch);
  445.        
  446.         if($errormsg != '')
  447.         {
  448.             echo '<div class="info_msg_err">'.$errormsg.'</div>';
  449.             return false;
  450.         }
  451.     }
  452.     else if(ini_get('allow_url_fopen') == 1)
  453.     {
  454.         $yt_data = @file($yt_target);
  455.         if($yt_data === false)
  456.             $error = 1;
  457.     }
  458.     if( ! is_array($yt_data))
  459.     {
  460.         $yt_data = explode("\n", $yt_data);
  461.     }
  462.    
  463.     $str = implode("", $yt_data);
  464.     $str = str_replace('><', ">\n<", $str);
  465.    
  466.     unset($yt_data);
  467.     $yt_data = array();
  468.    
  469.     $yt_data = explode("\n", $str);
  470.    
  471.     return $yt_data;
  472. }
  473.  
  474.  
  475. // MySQL database functions
  476. function dbquery($query)
  477. {
  478.     $result = @mysql_query($query);
  479.     if (!$result) {
  480.         echo mysql_error();
  481.         return false;
  482.     } else {
  483.         return $result;
  484.     }
  485. }
  486.  
  487. function dbcount($field,$table,$conditions="")
  488. {
  489.     $cond = ($conditions ? " WHERE ".$conditions : "");
  490.     $result = @mysql_query("SELECT Count(".$field.") FROM ".$table.$cond);
  491.     if (!$result) {
  492.         echo mysql_error();
  493.         return false;
  494.     } else {
  495.         $rows = mysql_result($result, 0);
  496.         return $rows;
  497.     }
  498. }
  499.  
  500. function dbrows($query)
  501. {
  502.     $result = @mysql_num_rows($query);
  503.     return $result;
  504. }
  505.  
  506. function dbarray($query)
  507. {
  508.     $result = @mysql_fetch_assoc($query);
  509.     if (!$result) {
  510.         echo mysql_error();
  511.         return false;
  512.     } else {
  513.         return $result;
  514.     }
  515. }
  516.  
  517.  
  518.  
  519.  
  520. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement