0
0
Fork 0
mirror of https://github.com/salesagility/SuiteCRM.git synced 2025-03-16 22:33:34 +00:00
salesagility_SuiteCRM/include/utils/sugar_file_utils.php

441 lines
14 KiB
PHP
Raw Normal View History

2013-09-23 19:30:44 +00:00
<?php
/**
*
2013-09-23 19:30:44 +00:00
* SugarCRM Community Edition is a customer relationship management program developed by
* SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
*
* SuiteCRM is an extension to SugarCRM Community Edition developed by SalesAgility Ltd.
2017-01-24 22:44:09 +00:00
* Copyright (C) 2011 - 2017 SalesAgility Ltd.
2014-07-07 15:33:23 +00:00
*
2013-09-23 19:30:44 +00:00
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by the
* Free Software Foundation with the addition of the following permission added
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
2014-07-07 15:33:23 +00:00
*
2013-09-23 19:30:44 +00:00
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
2014-07-07 15:33:23 +00:00
*
2013-09-23 19:30:44 +00:00
* You should have received a copy of the GNU Affero General Public License along with
* this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
2014-07-07 15:33:23 +00:00
*
2013-09-23 19:30:44 +00:00
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
* SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
2014-07-07 15:33:23 +00:00
*
2013-09-23 19:30:44 +00:00
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
2014-07-07 15:33:23 +00:00
*
2013-09-23 19:30:44 +00:00
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
2014-07-07 15:33:23 +00:00
* SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
* reasonably feasible for technical reasons, the Appropriate Legal Notices must
* display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
*/
2013-09-23 19:30:44 +00:00
2016-12-04 22:31:21 +00:00
if (!defined('sugarEntry') || !sugarEntry) {
die('Not A Valid Entry Point');
}
2013-09-23 19:30:44 +00:00
/**
* sugar_mkdir
2016-12-04 22:31:21 +00:00
* Call this function instead of mkdir to apply pre-configured permission
2013-09-23 19:30:44 +00:00
* settings when creating the directory. This method is basically
* a wrapper to the PHP mkdir function except that it supports setting
* the mode value by using the configuration file (if set). The mode is
* 0777 by default.
*
2016-12-04 22:31:21 +00:00
* @param string $pathname - String value of the directory to create
* @param int $mode - The integer value of the permissions mode to set the created directory to
* @param bool $recursive - boolean value indicating whether or not to create recursive directories if needed
2016-12-04 22:31:21 +00:00
* @param resource $context
*
* @return bool - Returns true on success false on failure
*
* @throws Exception
2013-09-23 19:30:44 +00:00
*/
2016-12-04 22:31:21 +00:00
function sugar_mkdir($pathname, $mode = null, $recursive = false, $context = null)
{
$mode = get_mode('dir_mode', $mode);
if (is_dir($pathname)) {
return true;
}
if (empty($mode)) {
$mode = 0777;
}
if (empty($context)) {
$result = @mkdir($pathname, $mode, $recursive);
} else {
$result = @mkdir($pathname, $mode, $recursive, $context);
}
if ($result) {
if (!sugar_chmod($pathname, $mode) && !is_writable($pathname)) {
return false;
}
if (!empty($GLOBALS['sugar_config']['default_permissions']['user'])) {
if (!sugar_chown($pathname)) {
return false;
}
}
if (!empty($GLOBALS['sugar_config']['default_permissions']['group'])) {
if (!sugar_chgrp($pathname)) {
return false;
}
}
} else {
$errorMessage = "Cannot create directory $pathname cannot be touched";
if (is_null($GLOBALS['log'])) {
throw new Exception("Error occurred but the system doesn't have logger. Error message: \"$errorMessage\"");
}
$GLOBALS['log']->error($errorMessage);
}
return $result;
2013-09-23 19:30:44 +00:00
}
/**
* sugar_fopen
2016-12-04 22:31:21 +00:00
* Call this function instead of fopen to apply pre-configured permission
2013-09-23 19:30:44 +00:00
* settings when creating the the file. This method is basically
* a wrapper to the PHP fopen function except that it supports setting
* the mode value by using the configuration file (if set). The mode is
* 0777 by default.
*
2016-12-04 22:31:21 +00:00
* @param string $filename - String value of the file to create
* @param int $mode - The integer value of the permissions mode to set the created file to
* @param bool $use_include_path - boolean value indicating whether or not to search the the included_path
* @param resource $context
*
* @return resource - Returns a file pointer on success, false otherwise
2013-09-23 19:30:44 +00:00
*/
function sugar_fopen($filename, $mode, $use_include_path = false, $context = null)
{
//check to see if the file exists, if not then use touch to create it.
if (!file_exists($filename)) {
sugar_touch($filename);
}
if (empty($context)) {
return fopen($filename, $mode, $use_include_path);
2018-07-24 14:42:22 +00:00
}
return fopen($filename, $mode, $use_include_path, $context);
2013-09-23 19:30:44 +00:00
}
/**
* sugar_file_put_contents
2016-12-04 22:31:21 +00:00
* Call this function instead of file_put_contents to apply pre-configured permission
2013-09-23 19:30:44 +00:00
* settings when creating the the file. This method is basically
* a wrapper to the PHP file_put_contents function except that it supports setting
* the mode value by using the configuration file (if set). The mode is
* 0777 by default.
*
2016-12-04 22:31:21 +00:00
* @param string $filename - String value of the file to create
* @param mixed $data - The data to be written to the file
* @param int $flags - int as specified by file_put_contents parameters
* @param resource $context
*
2013-09-23 19:30:44 +00:00
* @return int - Returns the number of bytes written to the file, false otherwise.
*/
function sugar_file_put_contents($filename, $data, $flags = null, $context = null)
{
//check to see if the file exists, if not then use touch to create it.
if (!file_exists($filename)) {
sugar_touch($filename);
}
if (!is_writable($filename)) {
$GLOBALS['log']->error("File $filename cannot be written to");
return false;
}
2013-09-23 19:30:44 +00:00
2016-12-04 22:31:21 +00:00
return file_put_contents($filename, $data, $flags, $context);
}
2013-09-23 19:30:44 +00:00
/**
* sugar_file_put_contents_atomic
2016-12-04 22:31:21 +00:00
* This is an atomic version of sugar_file_put_contents. It attempts to circumvent the shortcomings of file_put_contents
2013-09-23 19:30:44 +00:00
* by creating a temporary unique file and then doing an atomic rename operation.
*
2016-12-04 22:31:21 +00:00
* @param string $filename - String value of the file to create
* @param mixed $data - The data to be written to the file
2013-09-23 19:30:44 +00:00
* @param string $mode String value of the parameter to specify the type of access you require to the file stream
*
* @return bool - Returns true if $filename was created, false otherwise.
2013-09-23 19:30:44 +00:00
*/
2016-12-04 22:31:21 +00:00
function sugar_file_put_contents_atomic($filename, $data, $mode = 'wb')
{
2013-09-23 19:30:44 +00:00
$dir = dirname($filename);
$temp = tempnam($dir, 'temp');
if (!($f = @fopen($temp, $mode))) {
$temp = $dir . DIRECTORY_SEPARATOR . uniqid('temp');
2013-09-23 19:30:44 +00:00
if (!($f = @fopen($temp, $mode))) {
trigger_error("sugar_file_put_contents_atomic() : error writing temporary file '$temp'", E_USER_WARNING);
2013-09-23 19:30:44 +00:00
return false;
}
}
fwrite($f, $data);
fclose($f);
if (!@rename($temp, $filename)) {
2013-09-23 19:30:44 +00:00
@unlink($filename);
if (!@rename($temp, $filename)) {
2013-09-23 19:30:44 +00:00
// cleaning up temp file to avoid filling up temp dir
@unlink($temp);
2016-12-04 22:31:21 +00:00
trigger_error(
"sugar_file_put_contents_atomic() : fatal rename failure '$temp' -> '$filename'",
E_USER_WARNING
);
2013-09-23 19:30:44 +00:00
}
}
if (file_exists($filename)) {
return sugar_chmod($filename, 0755);
2013-09-23 19:30:44 +00:00
}
return false;
}
/**
* sugar_file_get_contents.
2013-09-23 19:30:44 +00:00
*
2016-12-04 22:31:21 +00:00
* @param string $filename - String value of the file to create
* @param bool $use_include_path - boolean value indicating whether or not to search the the included_path
* @param resource $context
*
* @return string|bool - Returns a file data on success, false otherwise
2013-09-23 19:30:44 +00:00
*/
function sugar_file_get_contents($filename, $use_include_path = false, $context = null)
{
//check to see if the file exists, if not then use touch to create it.
if (!file_exists($filename)) {
sugar_touch($filename);
}
if (!is_readable($filename)) {
$GLOBALS['log']->error("File $filename cannot be read");
return false;
}
if (empty($context)) {
return file_get_contents($filename, $use_include_path);
2018-07-24 14:42:22 +00:00
}
return file_get_contents($filename, $use_include_path, $context);
2013-09-23 19:30:44 +00:00
}
/**
* sugar_touch
* Attempts to set the access and modification times of the file named in the filename
* parameter to the value given in time . Note that the access time is always modified,
* regardless of the number of parameters. If the file does not exist it will be created.
* This method is basically a wrapper to the PHP touch method except that created files
* may be set with the permissions specified in the configuration file (if set).
*
2016-12-04 22:31:21 +00:00
* @param string $filename - The name of the file being touched.
* @param int $time - The touch time. If time is not supplied, the current system time is used.
2013-09-23 19:30:44 +00:00
* @param $atime - If present, the access time of the given filename is set to the value of atime
*
* @return bool - Returns TRUE on success or FALSE on failure.
2013-09-23 19:30:44 +00:00
*/
function sugar_touch($filename, $time = null, $atime = null)
{
if (!empty($atime) && !empty($time)) {
$result = @touch($filename, $time, $atime);
} elseif (!empty($time)) {
$result = @touch($filename, $time);
} else {
$result = @touch($filename);
}
if (!$result) {
$GLOBALS['log']->error("File $filename cannot be touched");
return $result;
}
if (!empty($GLOBALS['sugar_config']['default_permissions']['file_mode'])) {
sugar_chmod($filename, $GLOBALS['sugar_config']['default_permissions']['file_mode']);
}
if (!empty($GLOBALS['sugar_config']['default_permissions']['user'])) {
sugar_chown($filename);
}
if (!empty($GLOBALS['sugar_config']['default_permissions']['group'])) {
sugar_chgrp($filename);
}
return true;
2013-09-23 19:30:44 +00:00
}
/**
* sugar_chmod
* Attempts to change the permission of the specified filename to the mode value specified in the
* default_permissions configuration; otherwise, it will use the mode value.
*
2016-12-04 22:31:21 +00:00
* @param string $filename - Path to the file
* @param int $mode The integer value of the permissions mode to set the created directory to
*
* @return bool Returns TRUE on success or FALSE on failure.
2013-09-23 19:30:44 +00:00
*/
function sugar_chmod($filename, $mode = null)
{
if (!is_int($mode)) {
$mode = (int)$mode;
}
if (!is_windows()) {
if (!isset($mode)) {
$mode = get_mode('file_mode', $mode);
}
if (isset($mode) && $mode > 0) {
return @chmod($filename, $mode);
2018-07-24 14:42:22 +00:00
}
return false;
}
return true;
2013-09-23 19:30:44 +00:00
}
/**
* sugar_chown
* Attempts to change the owner of the file filename to the user specified in the
* default_permissions configuration; otherwise, it will use the user value.
*
* @param $filename - Path to the file
* @param string $user - A user name or number
*
* @return bool - Returns TRUE on success or FALSE on failure.
2013-09-23 19:30:44 +00:00
*/
function sugar_chown($filename, $user = '')
{
if (!is_windows()) {
if (strlen($user)) {
return chown($filename, $user);
2018-07-24 14:42:22 +00:00
}
if (strlen($GLOBALS['sugar_config']['default_permissions']['user'])) {
$user = $GLOBALS['sugar_config']['default_permissions']['user'];
2018-07-24 14:42:22 +00:00
return chown($filename, $user);
}
return false;
}
return true;
2013-09-23 19:30:44 +00:00
}
/**
* sugar_chgrp
* Attempts to change the group of the file filename to the group specified in the
* default_permissions configuration; otherwise it will use the group value.
*
* @param filename - Path to the file
* @param string $group - A group name or number
*
* @return bool - Returns TRUE on success or FALSE on failure.
2013-09-23 19:30:44 +00:00
*/
function sugar_chgrp($filename, $group = '')
{
if (!is_windows()) {
if (!empty($group)) {
return chgrp($filename, $group);
2018-07-24 14:42:22 +00:00
}
if (!empty($GLOBALS['sugar_config']['default_permissions']['group'])) {
$group = $GLOBALS['sugar_config']['default_permissions']['group'];
2018-07-24 14:42:22 +00:00
return chgrp($filename, $group);
}
return false;
}
return true;
2013-09-23 19:30:44 +00:00
}
/**
* get_mode.
2013-09-23 19:30:44 +00:00
*
* Will check to see if there is a default mode defined in the config file, otherwise return the
* $mode given as input
*
* @param string $key
2013-09-23 19:30:44 +00:00
* @param int $mode - the mode being passed by the calling function. This value will be overridden by a value
* defined in the config file.
*
2013-09-23 19:30:44 +00:00
* @return int - the mode either found in the config file or passed in via the input parameter
*/
function get_mode($key = 'dir_mode', $mode = null)
{
if (!is_int($mode)) {
$mode = (int)$mode;
}
if (!class_exists('SugarConfig', true)) {
require 'include/SugarObjects/SugarConfig.php';
}
if (!is_windows()) {
$conf_inst = SugarConfig::getInstance();
$mode = $conf_inst->get('default_permissions.' . $key, $mode);
}
return $mode;
2013-09-23 19:30:44 +00:00
}
/**
* Get filename in cache directory.
*
2013-09-23 19:30:44 +00:00
* @api
*
2013-09-23 19:30:44 +00:00
* @param string $file
*
* @return string
2013-09-23 19:30:44 +00:00
*/
function sugar_cached($file)
{
static $cdir = null;
if (empty($cdir) && !empty($GLOBALS['sugar_config']['cache_dir'])) {
2013-09-23 19:30:44 +00:00
$cdir = rtrim($GLOBALS['sugar_config']['cache_dir'], '/\\');
}
if (empty($cdir)) {
$cdir = 'cache';
2013-09-23 19:30:44 +00:00
}
2013-09-23 19:30:44 +00:00
return "$cdir/$file";
}
2016-12-04 22:31:21 +00:00
/**
* @deprecated 7.8
* @see is_dir
*
* @param $path
* @return bool
*/
function sugar_is_dir($path)
{
2018-07-12 10:40:10 +00:00
if (isset($GLOBALS['log'])) {
2016-12-04 22:31:21 +00:00
$GLOBALS['log']->deprecated('sugar_file_utils.php: sugar_is_dir() is deprecated');
}
return is_dir($path);
}
/**
* @deprecated 7.8
* @see is_file
*
* @param $path
* @return bool
*/
function sugar_is_file($path)
{
2018-07-12 10:40:10 +00:00
if (isset($GLOBALS['log'])) {
2016-12-04 22:31:21 +00:00
$GLOBALS['log']->deprecated('sugar_file_utils.php: sugar_is_file() is deprecated');
}
return is_file($path);
}