My default functions

<?php
###
# WS Default PHP Functions - v2.0
#
# CHANGES
# -------
# 2.0
# ---
# * Added a check for all functions. If they are already defined, this library will die().
# * Many new functions added.
###

###
# The next three functions are used to encrypt strings and then decrypt them
###

if(!function_exists("get_rnd_iv"))
{
        function get_rnd_iv($iv_len)
        {
                $iv = '';
                while ($iv_len-- > 0) {
                        $iv .= chr(mt_rand() & 0xff);
                }

                return $iv;
        }
}
else
{
        die("get_rnd_iv() exists");
}

if(!function_exists("md5_encrypt"))
{
        function md5_encrypt($plain_text, $password, $iv_len = 16)
        {
                $plain_text .= "\x13";
                $n = strlen($plain_text);
                if ($n % 16) $plain_text .= str_repeat("\0", 16 - ($n % 16));
                $i = 0;
                $enc_text = get_rnd_iv($iv_len);
                $iv = substr($password ^ $enc_text, 0, 512);
                while($i < $n)
                {
                        $block = substr($plain_text, $i, 16) ^ pack('H*', md5($iv));
                        $enc_text .= $block;
                        $iv = substr($block . $iv, 0, 512) ^ $password;
                        $i += 16;
                }

                return base64_encode($enc_text);
        }
}
else
{
        die("md5_encrypt() exists");
}

if(!function_exists("md5_decrypt"))
{
        function md5_decrypt($enc_text, $password, $iv_len = 16)
        {
                $enc_text = base64_decode($enc_text);
                $n = strlen($enc_text);
                $i = $iv_len;
                $plain_text = '';
                $iv = substr($password ^ substr($enc_text, 0, $iv_len), 0, 512);
                while($i < $n)
                {
                        $block = substr($enc_text, $i, 16);
                        $plain_text .= $block ^ pack('H*', md5($iv));
                        $iv = substr($block . $iv, 0, 512) ^ $password;
                        $i += 16;
                }

                return preg_replace('/\\x13\\x00*$/', '', $plain_text);
        }
}
else
{
        die("md5_decrypt() exists");
}

###
# Human readable filesize
###

if(!function_exists("human_filesize"))
{
        function human_filesize($size){
                if(is_file($size)){
                        $size = filesize($size);
                }
                else{
                        #> $size is already assumed to be in bytes.
                }
                #> I set $size = 1; if it was 0 to prevent dividing by zero. It may be bad
                #> practice, but seeing a size of 1byte is better than a big error message.
                #> I'll fix this if I have time. Who uploads a zero byte file anyway?
                if($size == 0){
                        $size = 1;
                }
                $filesizename = array("bytes", "kb", "mb", "gb", "tb", "pb", "eb", "zb", "yb");
                return round($size/pow(1000, ($i = floor(log($size, 1000)))), 2) . $filesizename[$i];
        }
}
else
{
        die("human_filesize() exists");
}

###
# Remove a directory
#
# lixlpixel recursive PHP functions
# recursive_remove_directory( directory to delete, empty )
# expects path to directory and optional TRUE / FALSE to empty
###

if(!function_exists("recursive_remove_directory"))
{
        function recursive_remove_directory($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)){
                        $handle = opendir($directory);
                        while(FALSE !== ($item = readdir($handle))){
                                if($item != '.' && $item != '..'){
                                        $path = $directory.'/'.$item;
                                        if(is_dir($path)){
                                                recursive_remove_directory($path);
                                        }
                                        else{
                                                unlink($path);
                                        }
                                }
                        }
                        closedir($handle);
                        if($empty == FALSE){
                                if(!rmdir($directory)){
                                        return FALSE;
                                }
                        }
                }
                return TRUE;
        }
}
else
{
        die("recursive_remove_directory() exists");
}

###
# Encrypt String (email obfuscation)
###
if(!function_exists("encrypt_string"))
{
        function encrypt_string($orgstring){
                $encstring = '';
                for($i=0;$i<strlen($orgstring);$i++){
                        $encstring .= '&#' . ord($orgstring{$i}) . ';';
                }
                return $encstring;
        }
}
else
{
        die("encrypt_string() exists");
}

###
# Last Modified
###
if(!function_exists("last_modified"))
{
        function last_modified($date_format,$file){
                if($date_format == '')
                {
                        $date_format = 'l, dS F, Y @ h:ia';
                }

                $last_modified = filemtime($file);
                return "Last modified " . date($date_format,$last_modified) . " EST";
        }
}
else
{
        die("last_modified() exists");
}

###
# Verify MySQL data before querying it
# Borrowed from http://www.digitalpropulsion.org/Programming/SQL_Injections_in_PHP_with_MySQL
#
# Sample Usage: $query = "update users set name=". my_verify_input($name) . " where id=" . my_verify_input($id, true);
#
# You could also verify any data sent via POST by say, $name = my_verify_input($_POST['name']).
###

if(!function_exists("my_verify_input"))
{
        function my_verify_input($input, $forceInt = false)
        {
                if(is_numeric($input))
                {
                        return $input;
                }
                elseif(!$forceInt)
                {
                        if(get_magic_quotes_gpc())
                        {
                                #> if magic quotes is enabled, get rid of those
                                #> pesky slashes
                                $input = stripslashes($input);
                        }

                        #> convert the input variable into a MySQL safe string.
                        $input = mysql_real_escape_string($input);

                        return $input;
                }
                else
                {
                        #> if $input not an integer and $forceInt = true,
                        #> kill script
                        die("Invalid Input");
                }
        }
}
else
{
        die("my_verify_input() exists");
}

###
# Smart stripslashes. Detects magic quotes.
###

if(!function_exists("smart_stripslashes"))
{
        function smart_stripslashes($input)
        {
                 if(get_magic_quotes_gpc())
                 {
                          $input = stripslashes($input);
                 }

                 return $input;
        }
}
else
{
        die("smart_stripslashes() exists");
}

###
# Perform a MySQL query
###

if(!function_exists("myq"))
{
        function myq($query,$myid){
                return mysql_query($query,$myid);
        }
}
else
{
        die("myq() exists");
}

###
# Perform a MySQL query and then a fetch row on the query
###

if(!function_exists("myfq"))
{
        function myfq($query,$myid){
                return mysql_fetch_row(mysql_query($query,$myid));
        }
}
else
{
        die("myfq() exists");
}

if(!function_exists("myfa"))
{
        function myfa($query,$myid){
                return mysql_fetch_assoc(mysql_query($query,$myid));
        }
}
else
{
        die("myfa() exists");
}

###
# Perform only a mysql fetch row. I use these in while
# loops of the form while($row = mysql_fetch_row($query)){
# The basic setup would be.
# $myquery = myq("select something from something where something",$myid);
# while($query = myf($myquery)){
#   print($query[0]);
# }
#
# or
#
# while($query = mya($myquery)){
#   print($query['something']);
# }
###

if(!function_exists("myf"))
{
        function myf($query){
                return mysql_fetch_row($query);
        }
}
else
{
        die("myf() exists");
}

if(!function_exists("mya"))
{
        function mya($query){
                return mysql_fetch_assoc($query);
        }
}
else
{
        die("mya() exists");
}

if(!function_exists("ws_touch"))
{
        function ws_touch($file)
        {
                $fp = fopen($file,"w");
                if(flock($fp,LOCK_EX))
                {
                        fwrite($fp,"");
                        flock($fp,LOCK_UN);
                        fclose($fp);

                        return 1;
                }
                else
                {
                        ### todo: handle the case where we don't get a lock
                        return 0;
                }
        }
}
else
{
        die("ws_touch() exists");
}

if(!function_exists("ws_read_file"))
{
        function ws_read_file($file,$var)
        {
                $var = trim($var);
                $a_file = file($file);
                foreach($a_file as $line)
                {
                        if(preg_match("/^{$var}:?/",$line))
                        {
                                $pattern = array("/^{$var}:?(.*)$/","/(\r\n|\r|\n)$/");
                                $replacement = array("$1","");
                                $var_value = preg_replace($pattern,$replacement,$line);
                        }
                }
                trim($var_value);

                return $var_value;
        }
}
else
{
        die("ws_read_file() exists");
}

if(!function_exists("ws_write_file"))
{
        function ws_write_file($file,$var,$var_value)
        {
                if(file_exists($file))
                {
                        $eol = "\n";
                        $is_match_found = "no"; ### if $var is not found, we'll append to the end of the file.
                        $var = trim($var);
                        $var_value = trim($var_value);
                        $a_pattern = array("/\r\n/","/\r/","/\n/");
                        $a_replacement = array("%0a","%0a","%0a");
                        $var = preg_replace($a_pattern,$a_replacement,$var);
                        $var_value = preg_replace($a_pattern,$a_replacement,$var_value);
                        $newfile = ""; ### this will be written to the file
                        $a_file = file($file);
                        foreach($a_file as $line)
                        {
                                if(preg_match("/^{$var}:/",$line))
                                {
                                        $is_match_found = "yes";
                                        $line = "{$var}:{$var_value}" . $eol;
                                }

                                $newfile .= $line;
                        }

                        if($is_match_found == "no")
                        {
                                $newfile .= "{$var}:{$var_value}" . $eol;
                        }

                        file_put_contents($file,$newfile);

                        return 1;
                }
                else
                {
                        return 0;
                }
        }
}
else
{
        die("ws_write_file() exists");
}

if(!function_exists("ws_register_var"))
{
        function ws_register_var($var,$desc)
        {
                $file = "db/variables/registered_variables";
                if(!file_exists($file))
                {
                        if(!ws_touch($file))
                        {
                                return 0;
                        }
                }

                if(ws_read_file($file,$var) == '')
                {
                        ws_write_file($file,$var,$desc);

                        return 1;
                }
                else
                {
                        return 0;
                }
        }
}
else
{
        die("ws_register_var() exists");
}

define('FILE_APPEND', 1);
if(!function_exists("file_put_contents"))
{
        function file_put_contents($n, $d, $flag = false) {
                $mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';
                $f = @fopen($n, $mode);
                if ($f === false) {
                        return 0;
                } else {
                        if (is_array($d)) $d = implode($d);
                        $bytes_written = fwrite($f, $d);
                        fclose($f);
                        return $bytes_written;
                }
        }
}

if(!function_exists("curl_file_get_contents"))
{
        function curl_file_get_contents($url)
        {
                ### start: curl ###
                $ch = curl_init();
                $timeout = 5; // set to zero for no timeout
                curl_setopt ($ch, CURLOPT_URL, $url);
                curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
                ob_start();
                curl_exec($ch);
                curl_close($ch);
                $curl_answer = ob_get_contents();
                ob_end_clean();

                return $curl_answer;
                ### end: curl ###
        }
}
else
{
        die("curl_file_get_contents() exists");
}

if(!function_exists("validate_xml"))
{
        function validate_xml($xml_file)
        {
                $a_errors = array();

                $xml_parser = xml_parser_create();

                #####> open a file and read data
                $fp = fopen($xml_file, 'r');
                while($xml_data = fread($fp, 4096))
                {
                        #####> parse the data chunk
                        if(!xml_parse($xml_parser,$xml_data,feof($fp)))
                        {
                                $a_errors[] = "Error: " . xml_error_string(xml_get_error_code($xml_parser));
                        }

                }
                fclose($fp);

                xml_parser_free($xml_parser);

                if(count($a_errors) == 0)
                {
                        return 1;
                }
                else
                {
                        return 0;
                }
        }
}
else
{
        die("validate_xml() exists");
}

if(!function_exists("validate_xml_return_errors"))
{
        function validate_xml_return_errors($xml_file)
        {
                  $a_errors = array();

                  $xml_parser = xml_parser_create();

                  #####> open a file and read data
                  $fp = fopen($xml_file, 'r');
                  while($xml_data = fread($fp, 4096))
                  {
                        #####> parse the data chunk
                        if(!xml_parse($xml_parser,$xml_data,feof($fp)))
                        {
                          $a_errors[] = "Error: " . xml_error_string(xml_get_error_code($xml_parser));
                        }

                  }
                  fclose($fp);

                  xml_parser_free($xml_parser);

                  return $a_errors;
        }
}
else
{
        die("validate_xml_return_errors() exists");
}

if(!function_exists("removeBOM"))
{
        function removeBOM($str="")
        {
                if(substr($str,0,3) == pack("CCC",0xef,0xbb,0xbf))
                {
                        $str=substr($str,3);
                }

                return $str;
        }
}
else
{
        echo("removeBOM() exists");
}

if(!function_exists("writeUTF8File"))
{
        function writeUTF8File($filename,$content) {
                $dhandle=fopen($filename,"w");
                # Now UTF-8 - Add byte order mark
                fwrite($dhandle, pack("CCC",0xef,0xbb,0xbf));
                fwrite($dhandle,$content);
                fclose($dhandle);
        }
}
else
{
        echo("writeUTF8File() exists");
}

if(!function_exists("dir_filesize"))
{
        function dir_filesize($path)
        {
                if (!is_dir($path))
                {
                        return filesize($path);
                }

                $size=0;
                foreach (scandir($path) as $file)
                {
                        if ($file=='.' or $file=='..')
                        {
                                continue;
                        }
                        $size+=dir_filesize($path.'/'.$file);
                }

                return $size;
        }
}
else
{
        die("dir_filesize() exists");
}

if(!function_exists("string2link"))
{
        function string2link($string)
        {
                $string = preg_replace("/(?:(http:\/\/)|(www\.))(\S+\b\/?)([ [:punct:]]*)(\s|$)/i","<a href=\"http://$2$3\">$1$2$3</a>$4$5", $string);

                return $string;
        }
}
else
{
        die("string2link() exists");
}

if(!function_exists("is_email"))
{
        function is_email($email)
        {
                if(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/",$email))
                {
                        return 1;
                }
                else
                {
                        return 0;
                }
        }
}
else
{
        die("is_email() exists");
}
?>

Page Comments (Click to edit)






[Click to add or edit comments])

Please prepend comments below including a date

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Have a nice day.