<?PHP
require_once('include/utils/array_utils.php');

function clean_path( $path ){
    // clean directory/file path with a functional equivalent
    $path = str_replace( "\\", "/", $path );
    $path = str_replace( "//", "/", $path );
    $path = str_replace( "/./", "/", $path );
    return( $path );
}

function create_cache_directory($file){
    $paths = explode('/',$file);
    $dir = 'cache';
    if(!file_exists($dir)){
        mkdir($dir, 0755);
    }
    for($i = 0; $i < sizeof($paths) - 1; $i++){
        $dir .= '/' . $paths[$i];
        if(!file_exists($dir)){
            mkdir($dir, 0755);
        }
    }
    return $dir . '/'. $paths[sizeof($paths) - 1];
}

function get_module_dir_list(){
	$modules = array();
	$path = 'modules';
	$d = dir($path);
	while($entry = $d->read()){	
		if($entry != '..' && $entry != '.'){
			if(is_dir($path. '/'. $entry)){
				$modules[$entry] = $entry;
			}
		}
	}
	return $modules;
}

function mk_temp_dir( $base_dir, $prefix="" ){
    $temp_dir = tempnam( $base_dir, $prefix );
    if( !$temp_dir || !unlink( $temp_dir ) ){
        return( false );
    }

    if( mkdir( $temp_dir ) ){
        return( $temp_dir );
    }

    return( false );
}

function remove_file_extension( $filename ){
    return( substr( $filename, 0, strrpos($filename, ".") ) );
}

function write_array_to_file( $the_name, $the_array, $the_file ){
	
    $the_string =   "<?php\n" .
                    '// created: ' . date('Y-m-d H:i:s') . "\n" .
                    "\$$the_name = " .
                    var_export_helper( $the_array ) .
                    ";\n?>\n";

    if( $fh = @fopen( $the_file, "w" ) ){
        fputs( $fh, $the_string, strlen($the_string) );
        fclose( $fh );
        return( true );
    }
    else{
        return( false );
    }
}

function write_encoded_file( $soap_result, $write_to_dir, $write_to_file="" ){
    // this function dies when encountering an error -- use with caution!
    // the path/file is returned upon success

    require_once('upgrade/dir_inc.php');

    if( $write_to_file == "" ){
        $write_to_file = $write_to_dir . "/" . $soap_result['filename'];
    }

    $file = $soap_result['data'];
    $write_to_file = str_replace( "\\", "/", $write_to_file );

    $dir_to_make = dirname( $write_to_file );
    if( !is_dir( $dir_to_make ) ){
        mkdir_recursive( $dir_to_make );
    }
    $fh = fopen( $write_to_file, "wb" );
    fwrite( $fh, base64_decode( $file ) );
    fclose( $fh );

    if( md5_file( $write_to_file ) != $soap_result['md5'] ){
        die( "MD5 error after writing file $write_to_file" );
    }
    return( $write_to_file );
}

function create_custom_directory($file){
    $paths = explode('/',$file);
    $dir = 'custom';
    if(!file_exists($dir)){
        mkdir($dir, 0755);
    }
    for($i = 0; $i < sizeof($paths) - 1; $i++){
        $dir .= '/' . $paths[$i];
        if(!file_exists($dir)){
            mkdir($dir, 0755);
        }
    }
    return $dir . '/'. $paths[sizeof($paths) - 1];
}


?>
