]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/dir_inc.php
Release 6.4.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 {
68         if(sugar_is_dir($path, 'instance')) {
69                 return(true);
70         }
71         if(sugar_is_file($path, 'instance')) {
72             if(!empty($GLOBALS['log'])) {
73                     $GLOBALS['log']->fatal("ERROR: mkdir_recursive(): argument $path is already a file.");
74             }
75                 return false;
76         }
77
78     //make variables with file paths
79         $pathcmp = $path = rtrim(clean_path($path), '/');
80         $basecmp =$base = rtrim(clean_path(getcwd()), '/');
81         if(is_windows()) {
82         //make path variable lower case for comparison in windows
83             $pathcmp = strtolower($path);
84             $basecmp = strtolower($base);
85         }
86
87     if($basecmp == $pathcmp) {
88         return true;
89     }
90     $base .= "/";
91         if(strncmp($pathcmp, $basecmp, strlen($basecmp)) == 0) {
92         /* strip current path prefix */
93             $path = substr($path, strlen($base));
94         }
95         $thePath = '';
96         $dirStructure = explode("/", $path);
97         if($dirStructure[0] == '') {
98             // absolute path
99         $base = '/';
100         array_shift($dirStructure);
101         }
102         if(is_windows()) {
103             if(strlen($dirStructure[0]) == 2 && $dirStructure[0][1] == ':') {
104                 /* C: prefix */
105                 $base = array_shift($dirStructure)."\\";
106             } elseif ($dirStructure[0][0].$dirStructure[0][1] == "\\\\") {
107                 /* UNC absolute path */
108                 $base = array_shift($dirStructure)."\\".array_shift($dirStructure)."\\"; // we won't try to mkdir UNC share name
109             }
110         }
111         foreach($dirStructure as $dirPath) {
112                 $thePath .= $dirPath."/";
113                 $mkPath = $base.$thePath;
114
115                 if(!is_dir($mkPath )) {
116                         if(!sugar_mkdir($mkPath)) return false;
117                 }
118         }
119         return true;
120 }
121
122 function rmdir_recursive( $path ){
123     if( is_file( $path ) ){
124         return( unlink( $path ) );
125     }
126     if( !is_dir( $path ) ){
127         if(!empty($GLOBALS['log'])) {
128             $GLOBALS['log']->fatal( "ERROR: rmdir_recursive(): argument $path is not a file or a dir." );
129         }
130         return false;
131     }
132
133     $status = true;
134
135     $d = dir( $path );
136     
137     while(($f = $d->read()) !== false){
138         if( $f == "." || $f == ".." ){
139             continue;
140         }
141         $status &= rmdir_recursive( "$path/$f" );
142     }
143     $d->close();
144     $rmOk = @rmdir($path);
145     if($rmOk === FALSE){
146         $GLOBALS['log']->error("ERROR: Unable to remove directory $path");
147     }
148     return( $status );
149 }
150
151 function findTextFiles( $the_dir, $the_array ){
152     if(!is_dir($the_dir)) {
153                 return $the_array;
154         }
155         $d = dir($the_dir);
156     while (false !== ($f = $d->read())) {
157         if( $f == "." || $f == ".." ){
158             continue;
159         }
160         if( is_dir( "$the_dir/$f" ) ){
161             // i think depth first is ok, given our cvs repo structure -Bob.
162             $the_array = findTextFiles( "$the_dir/$f", $the_array );
163         }
164         else {
165             switch( mime_content_type( "$the_dir/$f" ) ){
166                 // we take action on these cases
167                 case "text/html":
168                 case "text/plain":
169                     array_push( $the_array, "$the_dir/$f" );
170                     break;
171                 // we consciously skip these types
172                 case "application/pdf":
173                 case "application/x-zip":
174                 case "image/gif":
175                 case "image/jpeg":
176                 case "image/png":
177                 case "text/rtf":
178                     break;
179                 default:
180                     $GLOBALS['log']->info( "no type handler for $the_dir/$f with mime_content_type: " . mime_content_type( "$the_dir/$f" ) . "\n" );
181             }
182         }
183     }
184     return( $the_array );
185 }
186
187 function findAllFiles( $the_dir, $the_array, $include_dirs=false, $ext='', $exclude_dir=''){
188         // jchi  #24296
189         if(!empty($exclude_dir)){
190                 $exclude_dir = is_array($exclude_dir)?$exclude_dir:array($exclude_dir);
191                 foreach($exclude_dir as $ex_dir){
192                         if($the_dir == $ex_dir){
193                                   return $the_array;
194                         }
195                 }
196         }
197         $the_dir = rtrim($the_dir, "/\\");
198         //end
199     if(!is_dir($the_dir)) {
200                 return $the_array;
201         }
202         $d = dir($the_dir);
203     while (false !== ($f = $d->read())) {
204         if( $f == "." || $f == ".." ){
205             continue;
206         }
207
208         if( is_dir( "$the_dir/$f" ) ) {
209                         // jchi  #24296
210                         if(!empty($exclude_dir)){
211                                 //loop through array to compare directories..
212                                 foreach($exclude_dir as $ex_dir){
213                                         if("$the_dir/$f" == $ex_dir){
214                                                   continue 2;
215                                         }
216                                 }
217                         }
218                         //end
219
220                         if($include_dirs) {
221                                 $the_array[] = clean_path("$the_dir/$f");
222                         }
223             $the_array = findAllFiles( "$the_dir/$f", $the_array, $include_dirs, $ext);
224         } else {
225                         if(empty($ext) || preg_match('/'.$ext.'$/i', $f)){
226                                 $the_array[] = "$the_dir/$f";
227                         }
228         }
229
230
231     }
232     rsort($the_array);
233     return $the_array;
234 }
235
236 function findAllFilesRelative( $the_dir, $the_array ){
237     if(!is_dir($the_dir)) {
238                 return $the_array;
239         }
240     $original_dir   = getCwd();
241     if(is_dir($the_dir)){
242         chdir( $the_dir );
243         $the_array = findAllFiles( ".", $the_array );
244         if(is_dir($original_dir)){
245                 chdir( $original_dir );
246         }
247     }
248     return( $the_array );
249 }
250
251 /*
252  * Obtain an array of files that have been modified after the $date_modified
253  *
254  * @param the_dir                       the directory to begin the search
255  * @param the_array                     array to hold the results
256  * @param date_modified         the date to use when searching for files that have
257  *                                                      been modified
258  * @param filter                        optional regular expression filter
259  *
260  * return                                       an array containing all of the files that have been
261  *                                                      modified since date_modified
262  */
263 function findAllTouchedFiles( $the_dir, $the_array, $date_modified, $filter=''){
264     if(!is_dir($the_dir)) {
265                 return $the_array;
266         }
267         $d = dir($the_dir);
268     while (false !== ($f = $d->read())) {
269         if( $f == "." || $f == ".." ){
270             continue;
271         }
272         if( is_dir( "$the_dir/$f" ) ){
273             // i think depth first is ok, given our cvs repo structure -Bob.
274             $the_array = findAllTouchedFiles( "$the_dir/$f", $the_array, $date_modified, $filter);
275         }
276         else {
277                 $file_date = date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s', filemtime("$the_dir/$f"))) - date('Z'));
278
279                 if(strtotime($file_date) > strtotime($date_modified) && (empty($filter) || preg_match($filter, $f))){
280                          array_push( $the_array, "$the_dir/$f");
281                 }
282         }
283     }
284     return $the_array;
285 }