PageRenderTime 60ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/assicurazioniesinistri/include/Filesystem/utils.php

http://antilophpe.googlecode.com/
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
  1. <?php
  2. /**
  3. * File utils.php
  4. *
  5. * This file contains the utils to write read in filesystem
  6. * type Scheduler.
  7. * @author Mauro Fasolo <mfasolo@ncfsistemi.com>
  8. * @version 1.0
  9. */
  10. function is_windows() {
  11. static $is_windows = null;
  12. if (!isset($is_windows)) {
  13. $is_windows = strtoupper(substr(PHP_OS, 0, 3)) == 'WIN';
  14. }
  15. return $is_windows;
  16. }
  17. function clean_path( $path )
  18. {
  19. // clean directory/file path with a functional equivalent
  20. $appendpath = '';
  21. if ( is_windows() && strlen($path) >= 2 && $path[0].$path[1] == "\\\\" ) {
  22. $path = substr($path,2);
  23. $appendpath = "\\\\";
  24. }
  25. $path = str_replace( "\\", "/", $path );
  26. $path = str_replace( "//", "/", $path );
  27. $path = str_replace( "/./", "/", $path );
  28. return( $appendpath.$path );
  29. }
  30. function mkdir_recursive($path, $check_is_parent_dir = false) {
  31. if(my_is_dir($path, 'instance')) {
  32. return(true);
  33. }
  34. if(my_is_file($path, 'instance')) {
  35. print("ERROR: mkdir_recursive(): argument $path is already a file.\n");
  36. return false;
  37. }
  38. $path = clean_path($path);
  39. $path = str_replace(clean_path(getcwd()), '', $path);
  40. $thePath = '';
  41. $dirStructure = explode("/", $path);
  42. $status = true;
  43. foreach($dirStructure as $dirPath) {
  44. $thePath .= '/'.$dirPath;
  45. $mkPath = getcwd().'/'.$thePath;
  46. if(!is_dir($mkPath )) {
  47. $status = $status & my_mkdir($mkPath);
  48. }
  49. }
  50. return $status;
  51. }
  52. function my_is_dir($path, $mode='r'){
  53. return is_dir($path);
  54. }
  55. function my_is_file($path, $mode='r'){
  56. return is_file($path);
  57. }
  58. function my_mkdir($pathname, $mode=null, $recursive=false, $context='') {
  59. $mode = get_mode('dir_mode', $mode);
  60. $result = false;
  61. if(empty($mode)){
  62. $result = mkdir($pathname);
  63. }else if(empty($context)) {
  64. $result = mkdir($pathname, $mode, $recursive);
  65. } else {
  66. $result = mkdir($pathname, $mode, $recursive, $context);
  67. }
  68. if($result){
  69. if(!my_chmod($pathname, $mode)){
  70. return false;
  71. }
  72. if(!empty($GLOBALS['my_config']['default_permissions']['user'])){
  73. if(!my_chown($pathname)){
  74. return false;
  75. }
  76. }
  77. if(!empty($GLOBALS['my_config']['default_permissions']['group'])){
  78. if(!my_chgrp($pathname)) {
  79. return false;
  80. }
  81. }
  82. }
  83. return $result;
  84. }
  85. function my_chmod($filename, $mode=null) {
  86. if ( !is_int($mode) )
  87. $mode = (int) $mode;
  88. if(!is_windows()){
  89. if(!isset($mode)){
  90. $mode = get_mode('file_mode', $mode);
  91. }
  92. if(isset($mode) && $mode > 0){
  93. return chmod($filename, $mode);
  94. }else{
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. function my_chgrp($filename, $group='') {
  101. if(!is_windows()){
  102. if(!empty($group)){
  103. return chgrp($filename, $group);
  104. }else{
  105. return false;
  106. }
  107. }
  108. return true;
  109. }
  110. function my_chown($filename, $user='') {
  111. if(!is_windows()){
  112. if(strlen($user)){
  113. return chown($filename, $user);
  114. }else{
  115. return false;
  116. }
  117. }
  118. return true;
  119. }
  120. function get_mode($key = 'dir_mode', $mode=null) {
  121. if ( !is_int($mode) )
  122. $mode = (int) $mode;
  123. return 0777;
  124. }
  125. function my_touch($filename, $time=null, $atime=null) {
  126. $result = false;
  127. if(!empty($atime) && !empty($time)) {
  128. $result = touch($filename, $time, $atime);
  129. } else if(!empty($time)) {
  130. $result = touch($filename, $time);
  131. } else {
  132. $result = touch($filename);
  133. }
  134. if(!$result) return $result;
  135. my_chmod(get_mode('file_mode'));
  136. return true;
  137. }
  138. function my_fopen($filename, $mode, $use_include_path=false, $context=null){
  139. //check to see if the file exists, if not then use touch to create it.
  140. if(!file_exists($filename)){
  141. my_touch($filename);
  142. }
  143. if(empty($context)) {
  144. return fopen($filename, $mode, $use_include_path);
  145. } else {
  146. return fopen($filename, $mode, $use_include_path, $context);
  147. }
  148. }
  149. function write_array_to_file( $the_name, $the_array, $the_file, $mode="w", $header='' )
  150. {
  151. if(!empty($header) && ($mode != 'a' || !file_exists($the_file))){
  152. $the_string = $header;
  153. }else{
  154. $the_string = "<?php\n" .
  155. '// created: ' . date('Y-m-d H:i:s') . "\n";
  156. }
  157. $the_string .= "\$$the_name = " .
  158. var_export_helper( $the_array ) .
  159. ";\n?>\n";
  160. if( $fh = @my_fopen( $the_file, $mode ) )
  161. {
  162. fputs( $fh, $the_string);
  163. fclose( $fh );
  164. return( true );
  165. }
  166. else
  167. {
  168. return( false );
  169. }
  170. }
  171. function var_export_helper($tempArray) {
  172. if(!is_array($tempArray)){
  173. return var_export($tempArray, true);
  174. }
  175. $addNone = 0;
  176. foreach($tempArray as $key=>$val)
  177. {
  178. if($key == '' && $val == '')
  179. $addNone = 1;
  180. }
  181. $newArray = var_export($tempArray, true);
  182. if($addNone)
  183. {
  184. $newArray = str_replace("array (", "array ( '' => '',", $newArray);
  185. }
  186. return $newArray;
  187. }
  188. ?>