/branches/assicurazioniesinistri/include/Filesystem/utils.php
PHP | 221 lines | 178 code | 33 blank | 10 comment | 48 complexity | 849ab38d48c1d758b6592f1432245328 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, GPL-3.0
- <?php
- /**
- * File utils.php
- *
- * This file contains the utils to write read in filesystem
- * type Scheduler.
- * @author Mauro Fasolo <mfasolo@ncfsistemi.com>
- * @version 1.0
- */
- function is_windows() {
- static $is_windows = null;
- if (!isset($is_windows)) {
- $is_windows = strtoupper(substr(PHP_OS, 0, 3)) == 'WIN';
- }
- return $is_windows;
- }
- function clean_path( $path )
- {
- // clean directory/file path with a functional equivalent
- $appendpath = '';
- if ( is_windows() && strlen($path) >= 2 && $path[0].$path[1] == "\\\\" ) {
- $path = substr($path,2);
- $appendpath = "\\\\";
- }
- $path = str_replace( "\\", "/", $path );
- $path = str_replace( "//", "/", $path );
- $path = str_replace( "/./", "/", $path );
- return( $appendpath.$path );
- }
- function mkdir_recursive($path, $check_is_parent_dir = false) {
- if(my_is_dir($path, 'instance')) {
- return(true);
- }
- if(my_is_file($path, 'instance')) {
- print("ERROR: mkdir_recursive(): argument $path is already a file.\n");
- return false;
- }
- $path = clean_path($path);
- $path = str_replace(clean_path(getcwd()), '', $path);
- $thePath = '';
- $dirStructure = explode("/", $path);
- $status = true;
- foreach($dirStructure as $dirPath) {
- $thePath .= '/'.$dirPath;
- $mkPath = getcwd().'/'.$thePath;
- if(!is_dir($mkPath )) {
- $status = $status & my_mkdir($mkPath);
- }
- }
- return $status;
- }
- function my_is_dir($path, $mode='r'){
- return is_dir($path);
- }
- function my_is_file($path, $mode='r'){
- return is_file($path);
- }
- function my_mkdir($pathname, $mode=null, $recursive=false, $context='') {
- $mode = get_mode('dir_mode', $mode);
- $result = false;
- if(empty($mode)){
- $result = mkdir($pathname);
- }else if(empty($context)) {
- $result = mkdir($pathname, $mode, $recursive);
- } else {
- $result = mkdir($pathname, $mode, $recursive, $context);
- }
- if($result){
- if(!my_chmod($pathname, $mode)){
- return false;
- }
- if(!empty($GLOBALS['my_config']['default_permissions']['user'])){
- if(!my_chown($pathname)){
- return false;
- }
- }
- if(!empty($GLOBALS['my_config']['default_permissions']['group'])){
- if(!my_chgrp($pathname)) {
- return false;
- }
- }
- }
- return $result;
- }
- function my_chmod($filename, $mode=null) {
- if ( !is_int($mode) )
- $mode = (int) $mode;
- if(!is_windows()){
- if(!isset($mode)){
- $mode = get_mode('file_mode', $mode);
- }
- if(isset($mode) && $mode > 0){
- return chmod($filename, $mode);
- }else{
- return false;
- }
- }
- return true;
- }
- function my_chgrp($filename, $group='') {
- if(!is_windows()){
- if(!empty($group)){
- return chgrp($filename, $group);
- }else{
- return false;
- }
- }
- return true;
- }
- function my_chown($filename, $user='') {
- if(!is_windows()){
- if(strlen($user)){
- return chown($filename, $user);
- }else{
- return false;
- }
- }
- return true;
- }
- function get_mode($key = 'dir_mode', $mode=null) {
- if ( !is_int($mode) )
- $mode = (int) $mode;
- return 0777;
- }
- function my_touch($filename, $time=null, $atime=null) {
- $result = false;
- if(!empty($atime) && !empty($time)) {
- $result = touch($filename, $time, $atime);
- } else if(!empty($time)) {
- $result = touch($filename, $time);
- } else {
- $result = touch($filename);
- }
- if(!$result) return $result;
- my_chmod(get_mode('file_mode'));
- return true;
- }
- function my_fopen($filename, $mode, $use_include_path=false, $context=null){
- //check to see if the file exists, if not then use touch to create it.
- if(!file_exists($filename)){
- my_touch($filename);
- }
-
- if(empty($context)) {
- return fopen($filename, $mode, $use_include_path);
- } else {
- return fopen($filename, $mode, $use_include_path, $context);
- }
- }
- function write_array_to_file( $the_name, $the_array, $the_file, $mode="w", $header='' )
- {
- if(!empty($header) && ($mode != 'a' || !file_exists($the_file))){
- $the_string = $header;
- }else{
- $the_string = "<?php\n" .
- '// created: ' . date('Y-m-d H:i:s') . "\n";
- }
- $the_string .= "\$$the_name = " .
- var_export_helper( $the_array ) .
- ";\n?>\n";
- if( $fh = @my_fopen( $the_file, $mode ) )
- {
- fputs( $fh, $the_string);
- fclose( $fh );
- return( true );
- }
- else
- {
- return( false );
- }
- }
- function var_export_helper($tempArray) {
- if(!is_array($tempArray)){
- return var_export($tempArray, true);
- }
- $addNone = 0;
- foreach($tempArray as $key=>$val)
- {
- if($key == '' && $val == '')
- $addNone = 1;
- }
- $newArray = var_export($tempArray, true);
- if($addNone)
- {
- $newArray = str_replace("array (", "array ( '' => '',", $newArray);
- }
- return $newArray;
- }
- ?>