]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/utils/sugar_file_utils.php
Release 6.1.4
[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 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         return $result;
84 }
85
86 /**
87  * sugar_fopen
88  * Call this function instead of fopen to apply preconfigured permission
89  * settings when creating the the file.  This method is basically
90  * a wrapper to the PHP fopen function except that it supports setting
91  * the mode value by using the configuration file (if set).  The mode is
92  * 0777 by default.
93  *
94  * @param $filename - String value of the file to create
95  * @param $mode - The integer value of the permissions mode to set the created file to
96  * @param $$use_include_path - boolean value indicating whether or not to search the the included_path
97  * @param $context
98  * @return boolean - Returns a file pointer on success, false otherwise
99  */
100 function sugar_fopen($filename, $mode, $use_include_path=false, $context=null){
101         //check to see if the file exists, if not then use touch to create it.
102         if(!file_exists($filename)){
103                 sugar_touch($filename);
104         }
105                 
106         if(empty($context)) {
107                 return fopen($filename, $mode, $use_include_path);
108         } else {
109                 return fopen($filename, $mode, $use_include_path, $context);
110         }
111 }
112
113 /**
114  * sugar_file_put_contents
115  * Call this function instead of file_put_contents to apply preconfigured permission
116  * settings when creating the the file.  This method is basically
117  * a wrapper to the PHP file_put_contents function except that it supports setting
118  * the mode value by using the configuration file (if set).  The mode is
119  * 0777 by default.
120  *
121  * @param $filename - String value of the file to create
122  * @param $data - The data to be written to the file
123  * @param $flags - int as specifed by file_put_contents parameters
124  * @param $context
125  * @return int - Returns the number of bytes written to the file, false otherwise.
126  */
127 function sugar_file_put_contents($filename, $data, $flags=null, $context=null){
128         //check to see if the file exists, if not then use touch to create it.
129         if(!file_exists($filename)){
130                 sugar_touch($filename);
131         }
132
133         if(empty($flags)) {
134                 return file_put_contents($filename, $data);
135         } elseif(empty($context)) {
136                 return file_put_contents($filename, $data, $flags);
137         } else{
138                 return file_put_contents($filename, $data, $flags, $context);
139         }
140 }
141
142 /**
143  * sugar_touch
144  * Attempts to set the access and modification times of the file named in the filename
145  * parameter to the value given in time . Note that the access time is always modified,
146  * regardless of the number of parameters.  If the file does not exist it will be created.
147  * This method is basically a wrapper to the PHP touch method except that created files
148  * may be set with the permissions specified in the configuration file (if set).
149  *
150  * @param $filename - The name of the file being touched.
151  * @param $time - The touch time. If time  is not supplied, the current system time is used.
152  * @param $atime - If present, the access time of the given filename is set to the value of atime
153  * @return boolean - Returns TRUE on success or FALSE on failure.
154  *
155  */
156 function sugar_touch($filename, $time=null, $atime=null) {
157
158    $result = false;
159
160    if(!empty($atime) && !empty($time)) {
161           $result = touch($filename, $time, $atime);
162    } else if(!empty($time)) {
163           $result = touch($filename, $time);
164    } else {
165           $result = touch($filename);
166    }
167
168    if(!$result) return $result;
169
170         if(!empty($GLOBALS['sugar_config']['default_permissions']['file_mode'])){
171                 sugar_chmod($filename, $GLOBALS['sugar_config']['default_permissions']['file_mode']);
172         }
173         if(!empty($GLOBALS['sugar_config']['default_permissions']['user'])){
174                 sugar_chown($filename);
175         }
176         if(!empty($GLOBALS['sugar_config']['default_permissions']['group'])){
177                 sugar_chgrp($filename);
178         }
179
180    return true;
181 }
182
183 /**
184  * sugar_chmod
185  * Attempts to change the permission of the specified filename to the mode value specified in the
186  * default_permissions configuration; otherwise, it will use the mode value.
187  *
188  * @param  string    filename - Path to the file
189  * @param  int $mode The integer value of the permissions mode to set the created directory to
190  * @return boolean   Returns TRUE on success or FALSE on failure.
191  */
192 function sugar_chmod($filename, $mode=null) {
193     if ( !is_int($mode) )
194         $mode = (int) $mode;
195         if(!is_windows()){
196                 if(!isset($mode)){
197                         $mode = get_mode('file_mode', $mode);
198                 }
199         if(isset($mode) && $mode > 0){
200                    return @chmod($filename, $mode);
201                 }else{
202                 return false;
203                 }
204         }
205         return true;
206 }
207
208 /**
209  * sugar_chown
210  * Attempts to change the owner of the file filename to the user specified in the
211  * default_permissions configuration; otherwise, it will use the user value.
212  *
213  * @param filename - Path to the file
214  * @param user - A user name or number
215  * @return boolean - Returns TRUE on success or FALSE on failure.
216  */
217 function sugar_chown($filename, $user='') {
218         if(!is_windows()){
219                 if(strlen($user)){
220                         return chown($filename, $user);
221                 }else{
222                         if(strlen($GLOBALS['sugar_config']['default_permissions']['user'])){
223                                 $user = $GLOBALS['sugar_config']['default_permissions']['user'];
224                                 return chown($filename, $user);
225                         }else{
226                                 return false;
227                         }
228                 }
229         }
230         return true;
231 }
232
233 /**
234  * sugar_chgrp
235  * Attempts to change the group of the file filename to the group specified in the
236  * default_permissions configuration; otherwise it will use the group value.
237  *
238  * @param filename - Path to the file
239  * @param group - A group name or number
240  * @return boolean - Returns TRUE on success or FALSE on failure.
241  */
242 function sugar_chgrp($filename, $group='') {
243         if(!is_windows()){
244                 if(!empty($group)){
245                         return chgrp($filename, $group);
246                 }else{
247                         if(!empty($GLOBALS['sugar_config']['default_permissions']['group'])){
248                                 $group = $GLOBALS['sugar_config']['default_permissions']['group'];
249                                 return chgrp($filename, $group);
250                         }else{
251                                 return false;
252                         }
253                 }
254         }
255         return true;
256 }
257
258 /**
259  * get_mode
260  *
261  * Will check to see if there is a default mode defined in the config file, otherwise return the
262  * $mode given as input
263  *
264  * @param int $mode - the mode being passed by the calling function. This value will be overridden by a value
265  * defined in the config file.
266  * @return int - the mode either found in the config file or passed in via the input parameter
267  */
268 function get_mode($key = 'dir_mode', $mode=null) {
269         if ( !is_int($mode) )
270         $mode = (int) $mode;
271     if(!class_exists('SugarConfig', true)) {
272                 require 'include/SugarObjects/SugarConfig.php';
273         }
274         if(!is_windows()){
275                 $conf_inst=SugarConfig::getInstance();
276                 $mode = $conf_inst->get('default_permissions.'.$key, $mode);
277         }
278         return $mode;
279 }
280
281 function sugar_is_dir($path, $mode='r'){
282                 if(defined('TEMPLATE_URL'))return is_dir($path, $mode);
283                 return is_dir($path);
284 }
285
286 function sugar_is_file($path, $mode='r'){
287                 if(defined('TEMPLATE_URL'))return is_file($path, $mode);
288                 return is_file($path);
289 }
290
291 ?>