]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/dir_inc.php
Release 6.1.4
[Github/sugarcrm.git] / include / dir_inc.php
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
38
39
40 function copy_recursive( $source, $dest ){
41
42
43     if( is_file( $source ) ){
44         return( copy( $source, $dest ) );
45     }
46     if( !sugar_is_dir($dest, 'instance') ){
47         sugar_mkdir( $dest );
48     }
49
50     $status = true;
51
52     $d = dir( $source );
53     while( $f = $d->read() ){
54         if( $f == "." || $f == ".." ){
55             continue;
56         }
57         $status &= copy_recursive( "$source/$f", "$dest/$f" );
58     }
59     $d->close();
60     return( $status );
61 }
62
63 function mkdir_recursive($path, $check_is_parent_dir = false) {
64         if(sugar_is_dir($path, 'instance')) {
65                 return(true);
66         }
67         if(sugar_is_file($path, 'instance')) {
68                 print("ERROR: mkdir_recursive(): argument $path is already a file.\n");
69                 return false;
70         }
71
72         $path = clean_path($path);
73         $path = str_replace(clean_path(getcwd()), '', $path);
74         $thePath = '';
75         $dirStructure = explode("/", $path);
76         $status = true;
77         foreach($dirStructure as $dirPath) {
78                 $thePath .= '/'.$dirPath;
79                 $mkPath = getcwd().'/'.$thePath;
80
81                 if(!is_dir($mkPath )) {
82                         $status = $status & sugar_mkdir($mkPath);
83                 }
84         }
85         return $status;
86
87 }
88
89 function rmdir_recursive( $path ){
90     if( is_file( $path ) ){
91         return( unlink( $path ) );
92     }
93     if( !is_dir( $path ) ){
94         print( "ERROR: rmdir_recursive(): argument $path is not a file or a dir.\n" );
95         return( false );
96     }
97
98     $status = true;
99
100     $d = dir( $path );
101     while( $f = $d->read() ){
102         if( $f == "." || $f == ".." ){
103             continue;
104         }
105         $status &= rmdir_recursive( "$path/$f" );
106     }
107     $d->close();
108     rmdir( $path );
109     return( $status );
110 }
111
112 function findTextFiles( $the_dir, $the_array ){
113     if(!is_dir($the_dir)) {
114                 return $the_array;
115         }
116         $d = dir($the_dir);
117     while (false !== ($f = $d->read())) {
118         if( $f == "." || $f == ".." ){
119             continue;
120         }
121         if( is_dir( "$the_dir/$f" ) ){
122             // i think depth first is ok, given our cvs repo structure -Bob.
123             $the_array = findTextFiles( "$the_dir/$f", $the_array );
124         }
125         else {
126             switch( mime_content_type( "$the_dir/$f" ) ){
127                 // we take action on these cases
128                 case "text/html":
129                 case "text/plain":
130                     array_push( $the_array, "$the_dir/$f" );
131                     break;
132                 // we consciously skip these types
133                 case "application/pdf":
134                 case "application/x-zip":
135                 case "image/gif":
136                 case "image/jpeg":
137                 case "image/png":
138                 case "text/rtf":
139                     break;
140                 default:
141                     debug( "no type handler for $the_dir/$f with mime_content_type: " . mime_content_type( "$the_dir/$f" ) . "\n" );
142             }
143         }
144     }
145     return( $the_array );
146 }
147
148 function findAllFiles( $the_dir, $the_array, $include_dirs=false, $ext='', $exclude_dir=''){
149         // jchi  #24296 
150         if(!empty($exclude_dir)){
151                 $exclude_dir = is_array($exclude_dir)?$exclude_dir:array($exclude_dir);
152                 foreach($exclude_dir as $ex_dir){             
153                         if($the_dir == $ex_dir){
154                                   return $the_array;
155                         }                
156                 }
157         }
158         //end
159     if(!is_dir($the_dir)) {
160                 return $the_array;
161         }
162         $d = dir($the_dir);
163     while (false !== ($f = $d->read())) {
164         if( $f == "." || $f == ".." ){
165             continue;
166         }
167
168         if( is_dir( "$the_dir/$f" ) ) {
169                         // jchi  #24296 
170                         if(!empty($exclude_dir)){
171                                 //loop through array to compare directories..
172                                 foreach($exclude_dir as $ex_dir){             
173                                         if("$the_dir/$f" == $ex_dir){
174                                                   continue 2;
175                                         }                
176                                 }
177                         }
178                         //end
179                         
180                         if($include_dirs) {
181                                 $the_array[] = clean_path("$the_dir/$f");
182                         }
183             $the_array = findAllFiles( "$the_dir/$f/", $the_array, $include_dirs, $ext);
184         } else {
185                         if(empty($ext) || preg_match('/'.$ext.'$/i', $f)){
186                                 $the_array[] = clean_path("$the_dir/$f");
187                         }
188         }
189
190
191     }
192     rsort($the_array);
193     return $the_array;
194 }
195
196 function findAllFilesRelative( $the_dir, $the_array ){
197     if(!is_dir($the_dir)) {
198                 return $the_array;
199         }
200     $original_dir   = getCwd();
201     if(is_dir($the_dir)){
202         chdir( $the_dir );
203         $the_array = findAllFiles( ".", $the_array );
204         if(is_dir($original_dir)){
205                 chdir( $original_dir );
206         }
207     }
208     return( $the_array );
209 }
210
211 /*
212  * Obtain an array of files that have been modified after the $date_modified
213  *
214  * @param the_dir                       the directory to begin the search
215  * @param the_array                     array to hold the results
216  * @param date_modified         the date to use when searching for files that have
217  *                                                      been modified
218  * @param filter                        optional regular expression filter
219  *
220  * return                                       an array containing all of the files that have been
221  *                                                      modified since date_modified
222  */
223 function findAllTouchedFiles( $the_dir, $the_array, $date_modified, $filter=''){
224     if(!is_dir($the_dir)) {
225                 return $the_array;
226         }
227         $d = dir($the_dir);
228     while (false !== ($f = $d->read())) {
229         if( $f == "." || $f == ".." ){
230             continue;
231         }
232         if( is_dir( "$the_dir/$f" ) ){
233             // i think depth first is ok, given our cvs repo structure -Bob.
234             $the_array = findAllTouchedFiles( "$the_dir/$f", $the_array, $date_modified, $filter);
235         }
236         else {
237                 $file_date = date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s', filemtime("$the_dir/$f"))) - date('Z'));
238
239                 if(strtotime($file_date) > strtotime($date_modified) && (empty($filter) || preg_match($filter, $f))){
240                          array_push( $the_array, "$the_dir/$f");
241                 }
242         }
243     }
244     return( $the_array );
245 }
246 ?>