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