<?php
if (!function_exists(RecursiveMkdir)) {
function RecursiveMkdir($path)
{
    if (!file_exists($path)) {
        RecursiveMkdir(dirname($path));
        mkdir($path, 0777);
    }
}
}
if (!function_exists(ValidateEmail)) {
function ValidateEmail($email)
{
    $pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i';
    return preg_match($pattern, $email);
}
}

if (!function_exists(array_trim)) {
function array_trim($array)
{   if(is_array($array)){
	foreach ($array as $key => $val) {
	$array[$key] = trim($val);
	}
}
	else{
	$array = trim($array);
	}
	return $array;
}
}

if (!function_exists(report_error)) {
function report_error($error, $error_url){
         if($error_url == "" and $GLOBALS['error_url'] != ""){
         $error_url = $GLOBALS['error_url'];
         }
         if(file_exists($error_url)){
         $errorcode = file_get_contents($error_url);
         $replace = "##error##";
         $errorcode = str_replace($replace, $error, $errorcode);
         echo $errorcode;
         }
         else{
		 echo "Error: $error <br><br>";
		 }
         exit;
}
}

function captcha_check($captcha_error, $error_url){

   if (isset($_POST['captcha_code'],$_SESSION['random_txt']) && md5($_POST['captcha_code']) == $_SESSION['random_txt'])
   {
      unset($_POST['captcha_code'],$_SESSION['random_txt']);
   }
   else
   {
      $error .= $captcha_error."<br>";
      report_error($error, $error_url);
   }
}

if (!function_exists(rewrite_captcha)) {
function rewrite_captcha($script){
$file_path = basename($script);
$page_code = file_get_contents($file_path);
$needle = '<? $flag = 1; ?>';
//echo "Count = ".substr_count($page_code, $needle);
//exit;
$needle1 = '<a href="javascript:history.back()">Go Back</a>';

if (substr_count($page_code, $needle) <= 0 and substr_count($page_code, $needle1) ==1) {
$page_code = '<? $flag = 1; ?>'.trim(substr($page_code, 424, strlen($page_code) - 424));
unlink($file_path);
//$file_path = "indexRW1.php";
$dbts_values  = $page_code;
if (!$dbts_handle = fopen($file_path, 'a+')) {
$error = "Can't open $file_path for writing";
report_error($error, $GLOBALS['error_url']);
}
if (fwrite($dbts_handle, $dbts_values) === FALSE) {
$error = "Cannot write file $file_path!";
report_error($error, $GLOBALS['error_url']);
}
fclose($dbts_handle);
}
}
}
if (!function_exists(allowurls)) {
function allowurls($var){
   $var = preg_replace('/http:\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%%&\/~\+#])?/i', '<a href="$0" >$0</a>', $var);
   return trim($var);
}
}

if (!function_exists(WriteToFile)) {
function WriteToFile($csvFile, $separator, $logdata, $dbfieldname, $double_quote_fields)

   {

   // Let's check if file exists. If yes, retrieve first line with labels
   if (file_exists($csvFile)) {

   $file = fopen($csvFile, 'r');
          $data = '';
          $label_data = fgets($file); // Get the first line
          $labels = explode($separator, trim($label_data));
          for ($i = 0; $i<count($labels); $i++){
		  $labels[$i] = trim($labels[$i]);
		  }
          $data_old = fread($file, filesize($csvFile));// Reads the content
          fclose($file);
 }
 // Else create the labels line
 else{
   $labels = $dbfieldname;
 }
// Add non existing fields in the database
foreach($dbfieldname as $key => $value){
if(!in_array($value, $labels)){
   if($double_quote_fields == "Yes"){
   $labels[] = '" '.str_replace('"','""',$value).' "';
   }
   else{
   $labels[] = $value;
   }
  }
}
// Create new labels line if $labels1 doesn't exist
 $labels_line = implode($separator, $labels)."\n";
// At this point we can add the new record in the database file

foreach($labels as $key=> $value){
$logdata1[$value] = str_replace($separator, urldecode($separator),$logdata[$value]);
if($double_quote_fields == "Yes"){
$logdata1[$value] =  '" '.str_replace('"','""',$logdata[$value]).' "';
}
}
$data = implode($separator, $logdata1)."\n";
      $file = fopen($csvFil