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-2013 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
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
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
51 * @return boolean - Returns true on success false on failure
53 function sugar_mkdir($pathname, $mode=null, $recursive=false, $context='') {
54 $mode = get_mode('dir_mode', $mode);
56 if ( sugar_is_dir($pathname,$mode) )
63 $result = @mkdir($pathname, $mode, $recursive);
65 $result = @mkdir($pathname, $mode, $recursive, $context);
69 if(!sugar_chmod($pathname, $mode)){
72 if(!empty($GLOBALS['sugar_config']['default_permissions']['user'])){
73 if(!sugar_chown($pathname)){
77 if(!empty($GLOBALS['sugar_config']['default_permissions']['group'])){
78 if(!sugar_chgrp($pathname)) {
84 $GLOBALS['log']->error("Cannot create directory $pathname cannot be touched");
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
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
102 * @return boolean - Returns a file pointer on success, false otherwise
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);
110 if(empty($context)) {
111 return fopen($filename, $mode, $use_include_path);
113 return fopen($filename, $mode, $use_include_path, $context);
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
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
129 * @return int - Returns the number of bytes written to the file, false otherwise.
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);
137 if ( !is_writable($filename) ) {
138 $GLOBALS['log']->error("File $filename cannot be written to");
143 return file_put_contents($filename, $data);
144 } elseif(empty($context)) {
145 return file_put_contents($filename, $data, $flags);
147 return file_put_contents($filename, $data, $flags, $context);
153 * sugar_file_put_contents_atomic
154 * This is an atomic version of sugar_file_put_contents. It attempts to circumvent the shortcomings of file_put_contents
155 * by creating a temporary unique file and then doing an atomic rename operation.
157 * @param $filename - String value of the file to create
158 * @param $data - The data to be written to the file
159 * @param string $mode String value of the parameter to specify the type of access you require to the file stream
160 * @param boolean $use_include_path set to '1' or TRUE if you want to search for the file in the include_path too
161 * @param context $context Context to pass into fopen operation
162 * @return boolean - Returns true if $filename was created, false otherwise.
164 function sugar_file_put_contents_atomic($filename, $data, $mode='wb', $use_include_path=false, $context=null){
166 $dir = dirname($filename);
167 $temp = tempnam($dir, 'temp');
169 if (!($f = @fopen($temp, $mode))) {
170 $temp = $dir . DIRECTORY_SEPARATOR . uniqid('temp');
171 if (!($f = @fopen($temp, $mode))) {
172 trigger_error("sugar_file_put_contents_atomic() : error writing temporary file '$temp'", E_USER_WARNING);
180 if (!@rename($temp, $filename))
183 if (!@rename($temp, $filename))
185 // cleaning up temp file to avoid filling up temp dir
187 trigger_error("sugar_file_put_contents_atomic() : fatal rename failure '$temp' -> '$filename'", E_USER_ERROR);
191 if(file_exists($filename))
193 return sugar_chmod($filename, 0655);
202 * sugar_file_get_contents
204 * @param $filename - String value of the file to create
205 * @param $use_include_path - boolean value indicating whether or not to search the the included_path
207 * @return string|boolean - Returns a file data on success, false otherwise
209 function sugar_file_get_contents($filename, $use_include_path=false, $context=null){
210 //check to see if the file exists, if not then use touch to create it.
211 if(!file_exists($filename)){
212 sugar_touch($filename);
215 if ( !is_readable($filename) ) {
216 $GLOBALS['log']->error("File $filename cannot be read");
220 if(empty($context)) {
221 return file_get_contents($filename, $use_include_path);
223 return file_get_contents($filename, $use_include_path, $context);
229 * Attempts to set the access and modification times of the file named in the filename
230 * parameter to the value given in time . Note that the access time is always modified,
231 * regardless of the number of parameters. If the file does not exist it will be created.
232 * This method is basically a wrapper to the PHP touch method except that created files
233 * may be set with the permissions specified in the configuration file (if set).
235 * @param $filename - The name of the file being touched.
236 * @param $time - The touch time. If time is not supplied, the current system time is used.
237 * @param $atime - If present, the access time of the given filename is set to the value of atime
238 * @return boolean - Returns TRUE on success or FALSE on failure.
241 function sugar_touch($filename, $time=null, $atime=null) {
245 if(!empty($atime) && !empty($time)) {
246 $result = @touch($filename, $time, $atime);
247 } else if(!empty($time)) {
248 $result = @touch($filename, $time);
250 $result = @touch($filename);
254 $GLOBALS['log']->error("File $filename cannot be touched");
257 if(!empty($GLOBALS['sugar_config']['default_permissions']['file_mode'])){
258 sugar_chmod($filename, $GLOBALS['sugar_config']['default_permissions']['file_mode']);
260 if(!empty($GLOBALS['sugar_config']['default_permissions']['user'])){
261 sugar_chown($filename);
263 if(!empty($GLOBALS['sugar_config']['default_permissions']['group'])){
264 sugar_chgrp($filename);
272 * Attempts to change the permission of the specified filename to the mode value specified in the
273 * default_permissions configuration; otherwise, it will use the mode value.
275 * @param string filename - Path to the file
276 * @param int $mode The integer value of the permissions mode to set the created directory to
277 * @return boolean Returns TRUE on success or FALSE on failure.
279 function sugar_chmod($filename, $mode=null) {
280 if ( !is_int($mode) )
284 $mode = get_mode('file_mode', $mode);
286 if(isset($mode) && $mode > 0){
287 return @chmod($filename, $mode);
297 * Attempts to change the owner of the file filename to the user specified in the
298 * default_permissions configuration; otherwise, it will use the user value.
300 * @param filename - Path to the file
301 * @param user - A user name or number
302 * @return boolean - Returns TRUE on success or FALSE on failure.
304 function sugar_chown($filename, $user='') {
307 return chown($filename, $user);
309 if(strlen($GLOBALS['sugar_config']['default_permissions']['user'])){
310 $user = $GLOBALS['sugar_config']['default_permissions']['user'];
311 return chown($filename, $user);
322 * Attempts to change the group of the file filename to the group specified in the
323 * default_permissions configuration; otherwise it will use the group value.
325 * @param filename - Path to the file
326 * @param group - A group name or number
327 * @return boolean - Returns TRUE on success or FALSE on failure.
329 function sugar_chgrp($filename, $group='') {
332 return chgrp($filename, $group);
334 if(!empty($GLOBALS['sugar_config']['default_permissions']['group'])){
335 $group = $GLOBALS['sugar_config']['default_permissions']['group'];
336 return chgrp($filename, $group);
348 * Will check to see if there is a default mode defined in the config file, otherwise return the
349 * $mode given as input
351 * @param int $mode - the mode being passed by the calling function. This value will be overridden by a value
352 * defined in the config file.
353 * @return int - the mode either found in the config file or passed in via the input parameter
355 function get_mode($key = 'dir_mode', $mode=null) {
356 if ( !is_int($mode) )
358 if(!class_exists('SugarConfig', true)) {
359 require 'include/SugarObjects/SugarConfig.php';
362 $conf_inst=SugarConfig::getInstance();
363 $mode = $conf_inst->get('default_permissions.'.$key, $mode);
368 function sugar_is_dir($path, $mode='r'){
369 if(defined('TEMPLATE_URL'))return is_dir($path, $mode);
370 return is_dir($path);
373 function sugar_is_file($path, $mode='r'){
374 if(defined('TEMPLATE_URL'))return is_file($path, $mode);
375 return is_file($path);
379 * Get filename in cache directory
381 * @param string $file
383 function sugar_cached($file)
386 if(empty($cdir) && !empty($GLOBALS['sugar_config']['cache_dir'])) {
387 $cdir = rtrim($GLOBALS['sugar_config']['cache_dir'], '/\\');
392 return "$cdir/$file";