<?php
/**
  @licence GPL 2005-2010  The osCSS developers - osCSS Open Source E-commerce
  @portion code Copyright (c) 2002 osCommerce
  @package osCSS-2 <www http://www.oscss.org>
  @version 2.1.0
  @date  19/12/10, 19:49
  @author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
  @encode UTF-8
*/



/**
Mise a niveau chemin admin, vers adminxxx
*/
function replace_path_admin($file){
  return str_replace('admin/',PATH_ADMIN,$file);
}


//========================================================
//Functions:
//========================================================
function recursive_mkdir($path) {
  global $message;
  if (!file_exists($path)) {
    if (is_dir($path))     recursive_permissions_check($path);
    else {
      recursive_mkdir(dirname($path));
      if (!is_writeable(dirname($path))) {
        $message->add(WRITE_PERMISSINS_NEEDED_TEXT.dirname($path).'/','error');
        return;
      }
      if (!@mkdir($path, 0777)) {
        $message->add(CANT_CREATE_DIR_TEXT.$path,'error');
        return;
      }
      if (is_dir($path))    chmod($path, 0777);
    }
  }
}


function prepare_ci_installation() {
  global $message;
  //Do install
  //==========================================================
  //Rinon wrote this. I don't know what it is doing :-):
  $matches=array();
  if (preg_replace('/^(.*)(\/|\\\)$/', $_REQUEST['contrib_dir'], $matches)) {
      $contrib_dir=$matches[1];
  } else    $contrib_dir=$_REQUEST['contrib_dir'];
  //=======================================
  //Check if upper folder exists and create if nessesary:
  if(!is_dir(dirname($contrib_dir))) {
      if(!@mkdir(dirname($contrib_dir), 0777)) {
          $message->add(WRITE_PERMISSINS_NEEDED_TEXT.dirname($contrib_dir),'error');
          return;
      }
  }
  //CONTIBUTIONS FOLDER
  if(!is_dir($contrib_dir)) {
      recursive_mkdir($contrib_dir);
      if (is_dir($contrib_dir))    chmod($contrib_dir, 0777);
      else return;
  }
  //BACKUP FOLDER
  if(!is_dir(DIR_FS_ADMIN_BACKUP)){
      recursive_mkdir(DIR_FS_ADMIN_BACKUP);
      if (is_dir(DIR_FS_ADMIN_BACKUP))    chmod(DIR_FS_ADMIN_BACKUP, 0777);
      else return;
  }
  //=======================================


  //=======================================
  // Name of Contrib Installer folder:
  $ci_cip=CONTRIB_INSTALLER_NAME."_".CONTRIB_INSTALLER_VERSION;

  //Creates Contrib Installer folder in contribs folder:
  if (!is_dir($contrib_dir.'/'.$ci_cip)) {
      if (!@mkdir($contrib_dir.'/'.$ci_cip.'/')) {
          $message->add(WRITE_PERMISSINS_NEEDED_TEXT.$contrib_dir.'/'.$ci_cip,'error');
          return;
      }
      if (is_dir($contrib_dir.'/'.$ci_cip))    chmod($contrib_dir.'/'.$ci_cip, 0777);
  }

  //If install.xml with Contrib Installer exists we remove them:
  if(file_exists($contrib_dir.'/'.$ci_cip.'/'. CONFIG_FILENAME)) {
      if(!(@ unlink($contrib_dir.'/'.$ci_cip . '/' . CONFIG_FILENAME))) {
          $message->add(COULDNT_REMOVE_FILE_TEXT.$contrib_dir.'/'.$ci_cip . '/' . CONFIG_FILENAME,'error');
          return;
      }
  }
  //Copy install.xml with Contrib Installer to right place:
  if (@!copy(DIR_FS_ADMIN_FUNCTIONS.'/'.CONFIG_FILENAME, $contrib_dir.'/'.$ci_cip.'/'.CONFIG_FILENAME)) {
      $message->add(COULDNT_COPY_TO_TEXT. CONFIG_FILENAME,'error');
      return;
  }
  //Set permissions:
  if (is_dir($contrib_dir.'/'.$ci_cip))    chmod($contrib_dir.'/'.$ci_cip.'/', 0777);
  chmod($contrib_dir.'/'.$ci_cip.'/'.CONFIG_FILENAME, 0777);

  //Zip CI folder.
  define ('DIR_FS_CIP', $contrib_dir);
  require_once(DIR_FS_ADMIN_CLASSES.'ci_cip.class.php');
  $cip=new CIP ($ci_cip);
  $cip->pack_cip();
  chmod($contrib_dir.'/'.$ci_cip.'.zip', 0777);
  //Remove CI folde
  //If I remove this folder post notes will not appear and CI is not listed as installed.
  //ci_remove($contrib_dir.'/'.$ci_cip);

}



// SQL begin ===========================================================
function cip_db_query($query, $report='no') {
    //     $report manage a way of error reporting and can be:
    //     no, direct, return, add_session, add
    global  $message;

    $DB=Database::getInstance();

    $result = $DB->query($query);

    if (_cst_bool('STORE_DB_TRANSACTIONS')) {
      if ($result===false) trigger_error(sprintf(__FUNCTION__.' lg :'.__LINE__.' %s', $query."\n".mysql_error() ), E_USER_ERROR);
    }
    if ($result===false) {
	$error='SQL error :<b>'.mysql_errno().' - '.mysql_error().'<br>'.$query;

	if ($report=='direct')    echo $error;
	elseif ($report=='return')     $result=$error;
	elseif ($report=='add')     $message->add($error, 'error');
	elseif ($report=='add_session')     $message->add_session($error, 'error');
	return false;
    } else {
	//         ������ ��� �������� SELECT, SHOW, EXPLAIN, DESCRIBE
	//         mysql_query() ���������� ��������� �� ��������� �������
	return $result;
    }
}

  function sql_error($query) {return 'SQL error : '.mysql_errno().' - '.mysql_error().'<br>'.$query;}


    /*
    Usage:
            $restore_query=file_get_contents($backup_file);
            $sql_array=parse_sql($restore_query);
            foreach ($sql_array as $query)     tep_db_query($query);
            //for ($i=0, $n=sizeof($sql_array); $i<$n; $i++)     tep_db_query($sql_array[$i]);
    */
    function parse_sql($restore_query='') {
        //tep_set_time_limit(0);

        if (!$restore_query)    return;
        //From backup.php (begin)
        $sql_array = array();
        $sql_length = strlen($restore_query);
        $pos = strpos($restore_query, ';');

        if (($sql_length-$pos)==1)     return array($restore_query);

        for ($i=$pos; $i<$sql_length; $i++) {
// 	    if ($i>10000)  break;
            if ($restore_query[0] == '#') {
                $restore_query = ltrim(substr($restore_query, strpos($restore_query, "\n")));
                $sql_length = strlen($restore_query);
                $i = strpos($restore_query, ';')-1;
                continue;
            }
 	    if (($sql_length-$i)==1){
		     $sql_array[] = $restore_query;
	    }else if ($restore_query[($i+1)] == "\n") {
                for ($j=($i+2); $j<$sql_length; $j++) {
                    if (trim($restore_query[$j]) != '') {
                    $next = substr($restore_query, $j, 6);
                        if ($next[0] == '#') {
                            // find out where the break position is so we can remove this line (#comment line)
                            for ($k=$j; $k<$sql_length; $k++)     if ($restore_query[$k] == "\n")    break;
                            $query = substr($restore_query, 0, $i+1);
                            $restore_query = substr($restore_query, $k);
                            // join the query before the comment appeared, with the rest of the dump
                            $restore_query = $query . $restore_query;
                            $sql_length = strlen($restore_query);
                            $i = strpos($restore_query, ';')-1;
                            continue 2;
                        }
                        break;
                    }
                }
                if ($next == '') { // get the last insert query
                    $next = 'insert';
                }
                if ((preg_match('/update/i', $next)) || (preg_match('/create/i', $next)) || (preg_match('/insert/i', $next)) || (preg_match('/drop t/i', $next)) ) {
                    $next = '';
                    $sql_array[] = substr($restore_query, 0, $i);
                    $restore_query = ltrim(substr($restore_query, $i+1));
                    $sql_length = strlen($restore_query);
                    $i = strpos($restore_query, ';')-1;
                }
            }
        }
        return $sql_array;
    }
    // SQL end =============================================================





function ci_remove($source) {
    //Return true if error.
    global $messageStack;

// echo $source."<br />";

  if (!is_file($source) and !is_dir($source))    return;
  if (is_dir($source)) {
    $dir = dir($source);
//     if (!is_object($dir))    return;
    while ($file = $dir->read()) {
	if ( ($file!='.') && ($file!='..') ) {
	  if (is_writeable($source.'/'.$file)) {
	    if ( ($res=ci_remove($source.'/'.$file)) && $res !=true)    return $res;
	  } else {
	    $messageStack->add(sprintf(__('error file not removeable'), $source.'/'.$file), 'error');
// 	    return false;
	  }
	}
    }
    $dir->close();
    if (is_writeable($source)) return @rmdir($source);
    else /*{*/
      return sprintf(__('error directory not removeable'), $source)/*, 'error')*/;
//       return false;
//     }
  } else {
    if (is_writeable($source)) return @unlink($source);
    else /*{*/
	return sprintf(__('error file not removeable'), $source)/*, 'error')*/;
// 	return false;
//     }
  }
}



//=================================================
/**
 * Function to print an array within pre tags, debug use
 * @author Bobby Easland
 * @version 1.0
 * @param mixed $array
 */
	function PrintArray($array, $heading = ''){
		echo '<fieldset style="border:solid 1px silver;">' . "\n";
		echo '<legend style="background-color:#FFFFCC; border-style:solid; border-width:1px;">' . $heading . '</legend>' . "\n";
		echo '<pre style="text-align:left;">' . "\n";
		print_r($array);
		echo '</pre>' . "\n";
		echo '</fieldset><br/>' . "\n";
	} # end function


    function echo_array($array, $tab='') {    // ���� ������ �������
            $tab=$tab.'<img width="20"> ';
            if (is_array($array) ) {
                foreach ($array as $key =>$value) {
                    if (is_array($value) ) {
                        echo $tab."<br>".$key." (array): <br>";
                        echo_array($value, $tab);
                    } else echo $tab.$key." =>  ".linebreak_view($value)."<br>";
                }
            } else echo htmlentities(linebreak_view($array));
            echo "<hr>";
    }










//============================================================

    function unicode2win_old($str, $charset='cp1251') {

        //return unicode_russian($str);//win1251

        return $str;//win1251
        //return htmlentities($str);
        //return html_entity_decode($str, ENT_COMPAT, $charset);//old
    }

	function unicode2win($str, $charset = 'UTF-8') {
		if (function_exists('iconv') && 'UTF-8' != strtoupper($charset)) {
			$str = iconv("UTF-8", $charset, $str);
		}
	return $str;
}


//http://ua2.php.net/manual/en/function.convert-cyr-string.php
// Modificated by tapin13
 // Corrected by Timuretis
 // Convert win-1251 to utf-8



  function unicode_russian($str) {
      $encode = "";
      for ($ii=0;$ii<strlen($str);$ii++) {
          $xchr=substr($str,$ii,1);
          if (ord($xchr)>191) {
              $xchr=ord($xchr)+848;
              $xchr="&#" . $xchr . ";";
          }
          if(ord($xchr) == 168)     $xchr = "&#1025;";
          if(ord($xchr) == 184)     $xchr = "&#1105;";
          $encode=$encode . $xchr;
    }
      return $encode;
 }



 //http://ua2.php.net/manual/en/function.convert-cyr-string.php
function Encode($str, $type ){
  // $type:
 // 'w' - encodes from UTF to win
  // 'u' - encodes from win to UTF

    static $conv='';
        if (!is_array ( $conv )) {
            $conv=array ();
            for ( $x=128; $x <=143; $x++ ) {
            $conv['utf'][]=chr(209).chr($x);
            $conv['win'][]=chr($x+112);
            }

            for ( $x=144; $x <=191; $x++ ) {
                    $conv['utf'][]=chr(208).chr($x);
                    $conv['win'][]=chr($x+48);
            }
            $conv['utf'][]=chr(208).chr(129);
            $conv['win'][]=chr(168);
            $conv['utf'][]=chr(209).chr(145);
            $conv['win'][]=chr(184);
      }
      if ( $type=='w' )    return str_replace ( $conv['utf'], $conv['win'], $str );
      elseif ( $type=='u' )    return str_replace ( $conv['win'], $conv['utf'], $str );
      else    return $str;
   }















//Vlad Savitsky:
/*
    function recursive_permissions_check($path) {
    //if not writeable returns path to unwriteable folder
    //if all is writeable returns 0 (zero)
        if ($path!='/' && $path) {
            $result=recursive_permissions_check(dirname($path));
            //Displays all unwritable folders in path:
            //chmod($path, 0777);
            if(!is_writable($path) && is_dir($path))     return $result."<br>".$path."/";
            return $result;

            //Displays one uppest unwritable folder:
//            if (!$result)     if(!is_writable($path)) return $result."<br>".$path
//            else return $result;

        }
    }
*/

//php.net:

//Not used:
function recurse_chown_chgrp($mypath, $uid, $gid) {
    $d = opendir ($mypath) ;
    while(($file = readdir($d)) !== false) {
        if ($file != "." && $file != "..") {
            $typepath = $mypath . "/" . $file ;
            //print $typepath. " : " . filetype ($typepath). "<BR>" ;
            if (filetype ($typepath) == 'dir')     recurse_chown_chgrp ($typepath, $uid, $gid);
            chown($typepath, $uid);
            chgrp($typepath, $gid);
        }
    }
}

//Not used:
function recursive_chmod($path, $mod=0777) {
    if (is_dir($path) && $path!='/') {
        recursive_chmod(dirname($path));
        //echo "chmod: ".$path." - ".$mod."<br>";
        chmod($path, $mod);
    }
}




//Vlad Savitsky:

    function get_all_files_in_tree( $dir='', $exclude_files=array(), $exclude_folders=array()) {
        if (!$dir)    $dir=DIR_FS_CURRENT;
        $files=array();
        $all_folders=get_all_folders_in_tree($dir, $exclude_folders);
        foreach ($all_folders as $folder) {
            $inner_files=get_all_files_in_dir($folder, $exclude_files);
            if (count($inner_files)>0)     foreach ($inner_files as $file)     $files[]=$folder.$file;
        }
        return $files;
    }


    function get_all_folders_in_tree($dir='', $exclude_folders=array()) {
    //����������� ����� ���������� �������.

        if (!$dir)    $dir=DIR_FS_CURRENT;
        $folders[0]=$dir;
        for ($i=0; $i<=count($folders); $i++) {
            if(isset($folders[$i])) {
                if ($folders[$i])      $inner_folders=get_all_folders_in_dir($folders[$i], $exclude_folders);
                if (count($inner_folders)>0) {
                    foreach ($inner_folders as $folder)     $folders[]=$folders[$i]. $folder."/";
                }
            }
        }
        return $folders;
    }

    function get_all_folders_in_dir($dir='', $exclude_folders=array()) {
        if (!$dir)    $dir=DIR_FS_CURRENT;
        $dirs=array();
        if ($dir!=DIR_FS_CURRENT)     if (!is_dir($dir))     return array();
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
                $firstchar=substr($file, 0, 1);
                if($file!=".." && !in_array($file, $exclude_folders) && $firstchar!="_" && $firstchar!="."  ) {
                    if (is_dir($dir.$file))     $dirs[]= $file;
                }
            }
            closedir($dh);
        }
        return $dirs;
    }

    function get_all_files_in_dir($dir='', $exclude_files=array()) {
        $files=array();
        if (!$dir)    $dir=DIR_FS_CURRENT;
        if ($dir!=DIR_FS_CURRENT)     if (!is_dir($dir))     return array();
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
                if (!in_array($file, $exclude_files) && substr($file, 0, 1)!="_" ) {
                    if (is_file($dir.$file))     $files[]= $file;
                }
            }
            closedir($dh);
        }
        return $files;
    }

	function gzcompressfile($source,$level=false){
	   $dest=$source.'.gz';
	   $mode='wb'.$level;
	   $error=false;
	   if($fp_out=gzopen($dest,$mode)){
	       if($fp_in=fopen($source,'rb')){
	           while(!feof($fp_in))
	               gzwrite($fp_out,fread($fp_in,1024*512));
	           fclose($fp_in);
	           }
	         else $error=true;
	       gzclose($fp_out);
	       }
	     else $error=true;
	   if($error) return false;
	     else return $dest;
	}
?>
