]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/utils/sugar_file_utils.php
Release 6.4.0
[Github/sugarcrm.git] / include / utils / sugar_file_utils.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  * sugar_mkdir
41  * Call this function instead of mkdir to apply preconfigured permission
42  * settings when creating the directory.  This method is basically
43  * a wrapper to the PHP mkdir function except that it supports setting
44  * the mode value by using the configuration file (if set).  The mode is
45  * 0777 by default.
46  *
47  * @param $pathname - String value of the directory to create
48  * @param $mode - The integer value of the permissions mode to set the created directory to
49  * @param $recursive - boolean value indicating whether or not to create recursive directories if needed
50  * @param $context
51  * @return boolean - Returns true on success false on failure
52  */
53 function sugar_mkdir($pathname, $mode=null, $recursive=false, $context='') {
54         $mode = get_mode('dir_mode', $mode);
55
56         if ( sugar_is_dir($pathname,$mode) )
57             return true;
58
59         $result = false;
60         if(empty($mode))
61                 $mode = 0777;
62         if(empty($context)) {
63                 $result = @mkdir($pathname, $mode, $recursive);
64         } else {
65                 $result = @mkdir($pathname, $mode, $recursive, $context);
66         }
67
68         if($result){
69                 if(!sugar_chmod($pathname, $mode)){
70                         return false;
71                 }
72                 if(!empty($GLOBALS['sugar_config']['default_permissions']['user'])){
73                         if(!sugar_chown($pathname)){
74                                 return false;
75                         }
76                 }
77                 if(!empty($GLOBALS['sugar_config']['default_permissions']['group'])){
78                         if(!sugar_chgrp($pathname)) {
79                                 return false;
80                         }
81                 }
82         }
83         else {
84             $GLOBALS['log']->error("Cannot create directory $pathname cannot be touched");
85         }
86
87         return $result;
88 }
89
90 /**
91  * sugar_fopen
92  * Call this function instead of fopen to apply preconfigured permission
93  * settings when creating the the file.  This method is basically
94  * a wrapper to the PHP fopen function except that it supports setting
95  * the mode value by using the configuration file (if set).  The mode is
96  * 0777 by default.
97  *
98  * @param $filename - String value of the file to create
99  * @param $mode - The integer value of the permissions mode to set the created file to
100  * @param $$use_include_path - boolean value indicating whether or not to search the the included_path
101  * @param $context
102  * @return boolean - Returns a file pointer on success, false otherwise
103  */
104 function sugar_fopen($filename, $mode, $use_include_path=false, $context=null){
105         //check to see if the file exists, if not then use touch to create it.
106         if(!file_exists($filename)){
107                 sugar_touch($filename);
108         }
109
110         if(empty($context)) {
111                 return fopen($filename, $mode, $use_include_path);
112         } else {
113                 return fopen($filename, $mode, $use_include_path, $context);
114         }
115 }
116
117 /**
118  * sugar_file_put_contents
119  * Call this function instead of file_put_contents to apply preconfigured permission
120  * settings when creating the the file.  This method is basically
121  * a wrapper to the PHP file_put_contents function except that it supports setting
122  * the mode value by using the configuration file (if set).  The mode is
123  * 0777 by default.
124  *
125  * @param $filename - String value of the file to create
126  * @param $data - The data to be written to the file
127  * @param $flags - int as specifed by file_put_contents parameters
128  * @param $context
129  * @return int - Returns the number of bytes written to the file, false otherwise.
130  */
131 function sugar_file_put_contents($filename, $data, $flags=null, $context=null){
132         //check to see if the file exists, if not then use touch to create it.
133         if(!file_exists($filename)){
134                 sugar_touch($filename);
135         }
136
137         if ( !is_writable($filename) ) {
138             $GLOBALS['log']->error("File $filename cannot be written to");
139             return false;
140         }
141
142         if(empty($flags)) {
143                 return file_put_contents($filename, $data);
144         } elseif(empty($context)) {
145                 return file_put_contents($filename, $data, $flags);
146         } else{
147                 return file_put_contents($filename, $data, $flags, $context);
148         }
149 }
150
151 /**
152  * sugar_file_get_contents
153  *
154  * @param $filename - String value of the file to create
155  * @param $use_include_path - boolean value indicating whether or not to search the the included_path
156  * @param $context
157  * @return string|boolean - Returns a file data on success, false otherwise
158  */
159 function sugar_file_get_contents($filename, $use_include_path=false, $context=null){
160         //check to see if the file exists, if not then use touch to create it.
161         if(!file_exists($filename)){
162                 sugar_touch($filename);
163         }
164
165         if ( !is_readable($filename) ) {
166             $GLOBALS['log']->error("File $filename cannot be read");
167             return false;
168         }
169
170         if(empty($context)) {
171                 return file_get_contents($filename, $use_include_path);
172         } else {
173                 return file_get_contents($filename, $use_include_path, $context);
174         }
175 }
176
177 /**
178  * sugar_touch
179  * Attempts to set the access and modification times of the file named in the filename
180  * parameter to the value given in time . Note that the access time is always modified,
181  * regardless of the number of parameters.  If the file does not exist it will be created.
182  * This method is basically a wrapper to the PHP touch method except that created files
183  * may be set with the permissions specified in the configuration file (if set).
184  *
185  * @param $filename - The name of the file being touched.
186  * @param $time - The touch time. If time  is not supplied, the current system time is used.
187  * @param $atime - If present, the access time of the given filename is set to the value of atime
188  * @return boolean - Returns TRUE on success or FALSE on failure.
189  *
190  */
191 function sugar_touch($filename, $time=null, $atime=null) {
192
193    $result = false;
194
195    if(!empty($atime) && !empty($time)) {
196           $result = @touch($filename, $time, $atime);
197    } else if(!empty($time)) {
198           $result = @touch($filename, $time);
199    } else {
200           $result = @touch($filename);
201    }
202
203    if(!$result) {
204        $GLOBALS['log']->error("File $filename cannot be touched");
205        return $result;
206    }
207         if(!empty($GLOBALS['sugar_config']['default_permissions']['file_mode'])){
208                 sugar_chmod($filename, $GLOBALS['sugar_config']['default_permissions']['file_mode']);
209         }
210         if(!empty($GLOBALS['sugar_config']['default_permissions']['user'])){
211                 sugar_chown($filename);
212         }
213         if(!empty($GLOBALS['sugar_config']['default_permissions']['group'])){
214                 sugar_chgrp($filename);
215         }
216
217    return true;
218 }
219
220 /**
221  * sugar_chmod
222  * Attempts to change the permission of the specified filename to the mode value specified in the
223  * default_permissions configuration; otherwise, it will use the mode value.
224  *
225  * @param  string    filename - Path to the file
226  * @param  int $mode The integer value of the permissions mode to set the created directory to
227  * @return boolean   Returns TRUE on success or FALSE on failure.
228  */
229 function sugar_chmod($filename, $mode=null) {
230     if ( !is_int($mode) )
231         $mode = (int) $mode;
232         if(!is_windows()){
233                 if(!isset($mode)){
234                         $mode = get_mode('file_mode', $mode);
235                 }
236         if(isset($mode) && $mode > 0){
237                    return @chmod($filename, $mode);
238                 }else{
239                 return false;
240                 }
241         }
242         return true;
243 }
244
245 /**
246  * sugar_chown
247  * Attempts to change the owner of the file filename to the user specified in the
248  * default_permissions configuration; otherwise, it will use the user value.
249  *
250  * @param filename - Path to the file
251  * @param user - A user name or number
252  * @return boolean - Returns TRUE on success or FALSE on failure.
253  */
254 function sugar_chown($filename, $user='') {
255         if(!is_windows()){
256                 if(strlen($user)){
257                         return chown($filename, $user);
258                 }else{
259                         if(strlen($GLOBALS['sugar_config']['default_permissions']['user'])){
260                                 $user = $GLOBALS['sugar_config']['default_permissions']['user'];
261                                 return chown($filename, $user);
262                         }else{
263                                 return false;
264                         }
265                 }
266         }
267         return true;
268 }
269
270 /**
271  * sugar_chgrp
272  * Attempts to change the group of the file filename to the group specified in the
273  * default_permissions configuration; otherwise it will use the group value.
274  *
275  * @param filename - Path to the file
276  * @param group - A group name or number
277  * @return boolean - Returns TRUE on success or FALSE on failure.
278  */
279 function sugar_chgrp($filename, $group='') {
280         if(!is_windows()){
281                 if(!empty($group)){
282                         return chgrp($filename, $group);
283                 }else{
284                         if(!empty($GLOBALS['sugar_config']['default_permissions']['group'])){
285                                 $group = $GLOBALS['sugar_config']['default_permissions']['group'];
286                                 return chgrp($filename, $group);
287                         }else{
288                                 return false;
289                         }
290                 }
291         }
292         return true;
293 }
294
295 /**
296  * get_mode
297  *
298  * Will check to see if there is a default mode defined in the config file, otherwise return the
299  * $mode given as input
300  *
301  * @param int $mode - the mode being passed by the calling function. This value will be overridden by a value
302  * defined in the config file.
303  * @return int - the mode either found in the config file or passed in via the input parameter
304  */
305 function get_mode($key = 'dir_mode', $mode=null) {
306         if ( !is_int($mode) )
307         $mode = (int) $mode;
308     if(!class_exists('SugarConfig', true)) {
309                 require 'include/SugarObjects/SugarConfig.php';
310         }
311         if(!is_windows()){
312                 $conf_inst=SugarConfig::getInstance();
313                 $mode = $conf_inst->get('default_permissions.'.$key, $mode);
314         }
315         return $mode;
316 }
317
318 function sugar_is_dir($path, $mode='r'){
319                 if(defined('TEMPLATE_URL'))return is_dir($path, $mode);
320                 return is_dir($path);
321 }
322
323 function sugar_is_file($path, $mode='r'){
324                 if(defined('TEMPLATE_URL'))return is_file($path, $mode);
325                 return is_file($path);
326 }
327
328 /**
329  * Get filename in cache directory
330  * @api
331  * @param string $file
332  */
333 function sugar_cached($file)
334 {
335     static $cdir = null;
336     if(empty($cdir) && !empty($GLOBALS['sugar_config']['cache_dir'])) {
337         $cdir = rtrim($GLOBALS['sugar_config']['cache_dir'], '/\\');
338     }
339     if(empty($cdir)) {
340         $cdir = "cache";
341     }
342     return "$cdir/$file";
343 }