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.
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)) {
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
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
98 * @return boolean - Returns a file pointer on success, false otherwise
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);
106 if(empty($context)) {
107 return fopen($filename, $mode, $use_include_path);
109 return fopen($filename, $mode, $use_include_path, $context);
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
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
125 * @return int - Returns the number of bytes written to the file, false otherwise.
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);
134 return file_put_contents($filename, $data);
135 } elseif(empty($context)) {
136 return file_put_contents($filename, $data, $flags);
138 return file_put_contents($filename, $data, $flags, $context);
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).
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.
156 function sugar_touch($filename, $time=null, $atime=null) {
160 if(!empty($atime) && !empty($time)) {
161 $result = touch($filename, $time, $atime);
162 } else if(!empty($time)) {
163 $result = touch($filename, $time);
165 $result = touch($filename);
168 if(!$result) return $result;
170 if(!empty($GLOBALS['sugar_config']['default_permissions']['file_mode'])){
171 sugar_chmod($filename, $GLOBALS['sugar_config']['default_permissions']['file_mode']);
173 if(!empty($GLOBALS['sugar_config']['default_permissions']['user'])){
174 sugar_chown($filename);
176 if(!empty($GLOBALS['sugar_config']['default_permissions']['group'])){
177 sugar_chgrp($filename);
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.
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.
192 function sugar_chmod($filename, $mode=null) {
193 if ( !is_int($mode) )
197 $mode = get_mode('file_mode', $mode);
199 if(isset($mode) && $mode > 0){
200 return @chmod($filename, $mode);
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.
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.
217 function sugar_chown($filename, $user='') {
220 return chown($filename, $user);
222 if(strlen($GLOBALS['sugar_config']['default_permissions']['user'])){
223 $user = $GLOBALS['sugar_config']['default_permissions']['user'];
224 return chown($filename, $user);
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.
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.
242 function sugar_chgrp($filename, $group='') {
245 return chgrp($filename, $group);
247 if(!empty($GLOBALS['sugar_config']['default_permissions']['group'])){
248 $group = $GLOBALS['sugar_config']['default_permissions']['group'];
249 return chgrp($filename, $group);
261 * Will check to see if there is a default mode defined in the config file, otherwise return the
262 * $mode given as input
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
268 function get_mode($key = 'dir_mode', $mode=null) {
269 if ( !is_int($mode) )
271 if(!class_exists('SugarConfig', true)) {
272 require 'include/SugarObjects/SugarConfig.php';
275 $conf_inst=SugarConfig::getInstance();
276 $mode = $conf_inst->get('default_permissions.'.$key, $mode);
281 function sugar_is_dir($path, $mode='r'){
282 if(defined('TEMPLATE_URL'))return is_dir($path, $mode);
283 return is_dir($path);
286 function sugar_is_file($path, $mode='r'){
287 if(defined('TEMPLATE_URL'))return is_file($path, $mode);
288 return is_file($path);