PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/include/dir_inc.php

https://github.com/vincentamari/SuperSweetAdmin
PHP | 248 lines | 170 code | 23 blank | 55 comment | 58 complexity | 3d81f515f41d3f7d654560093e10fcd2 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, AGPL-3.0, LGPL-2.1
  1. <?php
  2. if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
  3. /*********************************************************************************
  4. * SugarCRM is a customer relationship management program developed by
  5. * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU Affero General Public License version 3 as published by the
  9. * Free Software Foundation with the addition of the following permission added
  10. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  11. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  12. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License along with
  20. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  21. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  22. * 02110-1301 USA.
  23. *
  24. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  25. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  26. *
  27. * The interactive user interfaces in modified source and object code versions
  28. * of this program must display Appropriate Legal Notices, as required under
  29. * Section 5 of the GNU Affero General Public License version 3.
  30. *
  31. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  32. * these Appropriate Legal Notices must retain the display of the "Powered by
  33. * SugarCRM" logo. If the display of the logo is not reasonably feasible for
  34. * technical reasons, the Appropriate Legal Notices must display the words
  35. * "Powered by SugarCRM".
  36. ********************************************************************************/
  37. function copy_recursive( $source, $dest ){
  38. if( is_file( $source ) ){
  39. return( copy( $source, $dest ) );
  40. }
  41. if( !sugar_is_dir($dest, 'instance') ){
  42. sugar_mkdir( $dest );
  43. }
  44. $status = true;
  45. $d = dir( $source );
  46. if($d === false) {
  47. return false;
  48. }
  49. while(false !== ($f = $d->read())) {
  50. if( $f == "." || $f == ".." ) {
  51. continue;
  52. }
  53. $status &= copy_recursive( "$source/$f", "$dest/$f" );
  54. }
  55. $d->close();
  56. return( $status );
  57. }
  58. function mkdir_recursive($path, $check_is_parent_dir = false) {
  59. if(sugar_is_dir($path, 'instance')) {
  60. return(true);
  61. }
  62. if(sugar_is_file($path, 'instance')) {
  63. print("ERROR: mkdir_recursive(): argument $path is already a file.\n");
  64. return false;
  65. }
  66. $path = clean_path($path);
  67. $path = str_replace(clean_path(getcwd()), '', $path);
  68. $thePath = '';
  69. $dirStructure = explode("/", $path);
  70. $status = true;
  71. foreach($dirStructure as $dirPath) {
  72. $thePath .= '/'.$dirPath;
  73. $mkPath = getcwd().'/'.$thePath;
  74. if(!is_dir($mkPath )) {
  75. $status = $status & sugar_mkdir($mkPath);
  76. }
  77. }
  78. return $status;
  79. }
  80. function rmdir_recursive( $path ){
  81. if( is_file( $path ) ){
  82. return( unlink( $path ) );
  83. }
  84. if( !is_dir( $path ) ){
  85. $GLOBALS['log']->error( "ERROR: rmdir_recursive(): argument $path is not a file or a dir.\n" );
  86. return( false );
  87. }
  88. $status = true;
  89. $d = dir( $path );
  90. while( $f = $d->read() ){
  91. if( $f == "." || $f == ".." ){
  92. continue;
  93. }
  94. $status &= rmdir_recursive( "$path/$f" );
  95. }
  96. $d->close();
  97. rmdir( $path );
  98. return( $status );
  99. }
  100. function findTextFiles( $the_dir, $the_array ){
  101. if(!is_dir($the_dir)) {
  102. return $the_array;
  103. }
  104. $d = dir($the_dir);
  105. while (false !== ($f = $d->read())) {
  106. if( $f == "." || $f == ".." ){
  107. continue;
  108. }
  109. if( is_dir( "$the_dir/$f" ) ){
  110. // i think depth first is ok, given our cvs repo structure -Bob.
  111. $the_array = findTextFiles( "$the_dir/$f", $the_array );
  112. }
  113. else {
  114. switch( mime_content_type( "$the_dir/$f" ) ){
  115. // we take action on these cases
  116. case "text/html":
  117. case "text/plain":
  118. array_push( $the_array, "$the_dir/$f" );
  119. break;
  120. // we consciously skip these types
  121. case "application/pdf":
  122. case "application/x-zip":
  123. case "image/gif":
  124. case "image/jpeg":
  125. case "image/png":
  126. case "text/rtf":
  127. break;
  128. default:
  129. debug( "no type handler for $the_dir/$f with mime_content_type: " . mime_content_type( "$the_dir/$f" ) . "\n" );
  130. }
  131. }
  132. }
  133. return( $the_array );
  134. }
  135. function findAllFiles( $the_dir, $the_array, $include_dirs=false, $ext='', $exclude_dir=''){
  136. // jchi #24296
  137. if(!empty($exclude_dir)){
  138. $exclude_dir = is_array($exclude_dir)?$exclude_dir:array($exclude_dir);
  139. foreach($exclude_dir as $ex_dir){
  140. if($the_dir == $ex_dir){
  141. return $the_array;
  142. }
  143. }
  144. }
  145. //end
  146. if(!is_dir($the_dir)) {
  147. return $the_array;
  148. }
  149. $d = dir($the_dir);
  150. while (false !== ($f = $d->read())) {
  151. if( $f == "." || $f == ".." ){
  152. continue;
  153. }
  154. if( is_dir( "$the_dir/$f" ) ) {
  155. // jchi #24296
  156. if(!empty($exclude_dir)){
  157. //loop through array to compare directories..
  158. foreach($exclude_dir as $ex_dir){
  159. if("$the_dir/$f" == $ex_dir){
  160. continue 2;
  161. }
  162. }
  163. }
  164. //end
  165. if($include_dirs) {
  166. $the_array[] = clean_path("$the_dir/$f");
  167. }
  168. $the_array = findAllFiles( "$the_dir/$f/", $the_array, $include_dirs, $ext);
  169. } else {
  170. if(empty($ext) || preg_match('/'.$ext.'$/i', $f)){
  171. $the_array[] = clean_path("$the_dir/$f");
  172. }
  173. }
  174. }
  175. rsort($the_array);
  176. return $the_array;
  177. }
  178. function findAllFilesRelative( $the_dir, $the_array ){
  179. if(!is_dir($the_dir)) {
  180. return $the_array;
  181. }
  182. $original_dir = getCwd();
  183. if(is_dir($the_dir)){
  184. chdir( $the_dir );
  185. $the_array = findAllFiles( ".", $the_array );
  186. if(is_dir($original_dir)){
  187. chdir( $original_dir );
  188. }
  189. }
  190. return( $the_array );
  191. }
  192. /*
  193. * Obtain an array of files that have been modified after the $date_modified
  194. *
  195. * @param the_dir the directory to begin the search
  196. * @param the_array array to hold the results
  197. * @param date_modified the date to use when searching for files that have
  198. * been modified
  199. * @param filter optional regular expression filter
  200. *
  201. * return an array containing all of the files that have been
  202. * modified since date_modified
  203. */
  204. function findAllTouchedFiles( $the_dir, $the_array, $date_modified, $filter=''){
  205. if(!is_dir($the_dir)) {
  206. return $the_array;
  207. }
  208. $d = dir($the_dir);
  209. while (false !== ($f = $d->read())) {
  210. if( $f == "." || $f == ".." ){
  211. continue;
  212. }
  213. if( is_dir( "$the_dir/$f" ) ){
  214. // i think depth first is ok, given our cvs repo structure -Bob.
  215. $the_array = findAllTouchedFiles( "$the_dir/$f", $the_array, $date_modified, $filter);
  216. }
  217. else {
  218. $file_date = date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s', filemtime("$the_dir/$f"))) - date('Z'));
  219. if(strtotime($file_date) > strtotime($date_modified) && (empty($filter) || preg_match($filter, $f))){
  220. array_push( $the_array, "$the_dir/$f");
  221. }
  222. }
  223. }
  224. return $the_array;
  225. }