Advertisement
Guest User

Untitled

a guest
Aug 13th, 2013
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.02 KB | None | 0 0
  1. # cat /var/www/localhost/htdocs/sugarcrm/include/utils/sugar_file_utils.php
  2. <?php
  3. if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
  4. /*********************************************************************************
  5. * SugarCRM Community Edition is a customer relationship management program developed by
  6. * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it under
  9. * the terms of the GNU Affero General Public License version 3 as published by the
  10. * Free Software Foundation with the addition of the following permission added
  11. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  12. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  13. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  14. *
  15. * This program is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License along with
  21. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  22. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  23. * 02110-1301 USA.
  24. *
  25. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  26. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  27. *
  28. * The interactive user interfaces in modified source and object code versions
  29. * of this program must display Appropriate Legal Notices, as required under
  30. * Section 5 of the GNU Affero General Public License version 3.
  31. *
  32. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  33. * these Appropriate Legal Notices must retain the display of the "Powered by
  34. * SugarCRM" logo. If the display of the logo is not reasonably feasible for
  35. * technical reasons, the Appropriate Legal Notices must display the words
  36. * "Powered by SugarCRM".
  37. ********************************************************************************/
  38.  
  39.  
  40. /**
  41. * sugar_mkdir
  42. * Call this function instead of mkdir to apply preconfigured permission
  43. * settings when creating the directory. This method is basically
  44. * a wrapper to the PHP mkdir function except that it supports setting
  45. * the mode value by using the configuration file (if set). The mode is
  46. * 0777 by default.
  47. *
  48. * @param $pathname - String value of the directory to create
  49. * @param $mode - The integer value of the permissions mode to set the created directory to
  50. * @param $recursive - boolean value indicating whether or not to create recursive directories if needed
  51. * @param $context
  52. * @return boolean - Returns true on success false on failure
  53. */
  54. function sugar_mkdir($pathname, $mode=null, $recursive=false, $context='') {
  55. $mode = get_mode('dir_mode', $mode);
  56.  
  57. if ( sugar_is_dir($pathname,$mode) )
  58. return true;
  59.  
  60. $result = false;
  61. if(empty($mode))
  62. $mode = 0777;
  63. if(empty($context)) {
  64. $result = @mkdir($pathname, $mode, $recursive);
  65. } else {
  66. $result = @mkdir($pathname, $mode, $recursive, $context);
  67. }
  68.  
  69. if($result){
  70. if(!sugar_chmod($pathname, $mode)){
  71. return false;
  72. }
  73. if(!empty($GLOBALS['sugar_config']['default_permissions']['user'])){
  74. if(!sugar_chown($pathname)){
  75. return false;
  76. }
  77. }
  78. if(!empty($GLOBALS['sugar_config']['default_permissions']['group'])){
  79. if(!sugar_chgrp($pathname)) {
  80. return false;
  81. }
  82. }
  83. }
  84. else {
  85. $GLOBALS['log']->error("Cannot create directory $pathname cannot be touched");
  86. }
  87.  
  88. return $result;
  89. }
  90.  
  91. /**
  92. * sugar_fopen
  93. * Call this function instead of fopen to apply preconfigured permission
  94. * settings when creating the the file. This method is basically
  95. * a wrapper to the PHP fopen function except that it supports setting
  96. * the mode value by using the configuration file (if set). The mode is
  97. * 0777 by default.
  98. *
  99. * @param $filename - String value of the file to create
  100. * @param $mode - The integer value of the permissions mode to set the created file to
  101. * @param $$use_include_path - boolean value indicating whether or not to search the the included_path
  102. * @param $context
  103. * @return boolean - Returns a file pointer on success, false otherwise
  104. */
  105. function sugar_fopen($filename, $mode, $use_include_path=false, $context=null){
  106. //check to see if the file exists, if not then use touch to create it.
  107. if(!file_exists($filename)){
  108. sugar_touch($filename);
  109. }
  110.  
  111. if(empty($context)) {
  112. return fopen($filename, $mode, $use_include_path);
  113. } else {
  114. return fopen($filename, $mode, $use_include_path, $context);
  115. }
  116. }
  117.  
  118. /**
  119. * sugar_file_put_contents
  120. * Call this function instead of file_put_contents to apply preconfigured permission
  121. * settings when creating the the file. This method is basically
  122. * a wrapper to the PHP file_put_contents function except that it supports setting
  123. * the mode value by using the configuration file (if set). The mode is
  124. * 0777 by default.
  125. *
  126. * @param $filename - String value of the file to create
  127. * @param $data - The data to be written to the file
  128. * @param $flags - int as specifed by file_put_contents parameters
  129. * @param $context
  130. * @return int - Returns the number of bytes written to the file, false otherwise.
  131. */
  132. function sugar_file_put_contents($filename, $data, $flags=null, $context=null){
  133. //check to see if the file exists, if not then use touch to create it.
  134. if(!file_exists($filename)){
  135. sugar_touch($filename);
  136. }
  137.  
  138. if ( !is_writable($filename) ) {
  139. $GLOBALS['log']->error("File $filename cannot be written to");
  140. return false;
  141. }
  142.  
  143. if(empty($flags)) {
  144. return file_put_contents($filename, $data);
  145. } elseif(empty($context)) {
  146. return file_put_contents($filename, $data, $flags);
  147. } else{
  148. return file_put_contents($filename, $data, $flags, $context);
  149. }
  150. }
  151.  
  152.  
  153. /**
  154. * sugar_file_put_contents_atomic
  155. * This is an atomic version of sugar_file_put_contents. It attempts to circumvent the shortcomings of file_put_contents
  156. * by creating a temporary unique file and then doing an atomic rename operation.
  157. *
  158. * @param $filename - String value of the file to create
  159. * @param $data - The data to be written to the file
  160. * @param string $mode String value of the parameter to specify the type of access you require to the file stream
  161. * @param boolean $use_include_path set to '1' or TRUE if you want to search for the file in the include_path too
  162. * @param context $context Context to pass into fopen operation
  163. * @return boolean - Returns true if $filename was created, false otherwise.
  164. */
  165. function sugar_file_put_contents_atomic($filename, $data, $mode='wb', $use_include_path=false, $context=null){
  166.  
  167. $dir = dirname($filename);
  168. $temp = tempnam($dir, 'temp');
  169.  
  170. if (!($f = @fopen($temp, $mode))) {
  171. $temp = $dir . DIRECTORY_SEPARATOR . uniqid('temp');
  172. if (!($f = @fopen($temp, $mode))) {
  173. trigger_error("sugar_file_put_contents_atomic() : error writing temporary file '$temp'", E_USER_WARNING);
  174. return false;
  175. }
  176. }
  177.  
  178. fwrite($f, $data);
  179. fclose($f);
  180.  
  181. if (!@rename($temp, $filename))
  182. {
  183. @unlink($filename);
  184. if (!@rename($temp, $filename))
  185. {
  186. // cleaning up temp file to avoid filling up temp dir
  187. @unlink($temp);
  188. trigger_error("sugar_file_put_contents_atomic() : fatal rename failure '$temp' -> '$filename'", E_USER_ERROR);
  189. }
  190. }
  191.  
  192. if(file_exists($filename))
  193. {
  194. return sugar_chmod($filename, 0655);
  195. }
  196.  
  197. return false;
  198. }
  199.  
  200.  
  201.  
  202. /**
  203. * sugar_file_get_contents
  204. *
  205. * @param $filename - String value of the file to create
  206. * @param $use_include_path - boolean value indicating whether or not to search the the included_path
  207. * @param $context
  208. * @return string|boolean - Returns a file data on success, false otherwise
  209. */
  210. function sugar_file_get_contents($filename, $use_include_path=false, $context=null){
  211. //check to see if the file exists, if not then use touch to create it.
  212. if(!file_exists($filename)){
  213. sugar_touch($filename);
  214. }
  215.  
  216. if ( !is_readable($filename) ) {
  217. $GLOBALS['log']->error("File $filename cannot be read");
  218. return false;
  219. }
  220.  
  221. if(empty($context)) {
  222. return file_get_contents($filename, $use_include_path);
  223. } else {
  224. return file_get_contents($filename, $use_include_path, $context);
  225. }
  226. }
  227.  
  228. /**
  229. * sugar_touch
  230. * Attempts to set the access and modification times of the file named in the filename
  231. * parameter to the value given in time . Note that the access time is always modified,
  232. * regardless of the number of parameters. If the file does not exist it will be created.
  233. * This method is basically a wrapper to the PHP touch method except that created files
  234. * may be set with the permissions specified in the configuration file (if set).
  235. *
  236. * @param $filename - The name of the file being touched.
  237. * @param $time - The touch time. If time is not supplied, the current system time is used.
  238. * @param $atime - If present, the access time of the given filename is set to the value of atime
  239. * @return boolean - Returns TRUE on success or FALSE on failure.
  240. *
  241. */
  242. function sugar_touch($filename, $time=null, $atime=null) {
  243.  
  244. $result = false;
  245.  
  246. if(!empty($atime) && !empty($time)) {
  247. $result = @touch($filename, $time, $atime);
  248. } else if(!empty($time)) {
  249. $result = @touch($filename, $time);
  250. } else {
  251. $result = @touch($filename);
  252. }
  253.  
  254. if(!$result) {
  255. $GLOBALS['log']->error("File $filename cannot be touched");
  256. return $result;
  257. }
  258. if(!empty($GLOBALS['sugar_config']['default_permissions']['file_mode'])){
  259. sugar_chmod($filename, $GLOBALS['sugar_config']['default_permissions']['file_mode']);
  260. }
  261. if(!empty($GLOBALS['sugar_config']['default_permissions']['user'])){
  262. sugar_chown($filename);
  263. }
  264. if(!empty($GLOBALS['sugar_config']['default_permissions']['group'])){
  265. sugar_chgrp($filename);
  266. }
  267.  
  268. return true;
  269. }
  270.  
  271. /**
  272. * sugar_chmod
  273. * Attempts to change the permission of the specified filename to the mode value specified in the
  274. * default_permissions configuration; otherwise, it will use the mode value.
  275. *
  276. * @param string filename - Path to the file
  277. * @param int $mode The integer value of the permissions mode to set the created directory to
  278. * @return boolean Returns TRUE on success or FALSE on failure.
  279. */
  280. function sugar_chmod($filename, $mode=null) {
  281. if ( !is_int($mode) )
  282. $mode = (int) $mode;
  283. if(!is_windows()){
  284. if(!isset($mode)){
  285. $mode = get_mode('file_mode', $mode);
  286. }
  287. if(isset($mode) && $mode > 0){
  288. return @chmod($filename, $mode);
  289. }else{
  290. return false;
  291. }
  292. }
  293. return true;
  294. }
  295.  
  296. /**
  297. * sugar_chown
  298. * Attempts to change the owner of the file filename to the user specified in the
  299. * default_permissions configuration; otherwise, it will use the user value.
  300. *
  301. * @param filename - Path to the file
  302. * @param user - A user name or number
  303. * @return boolean - Returns TRUE on success or FALSE on failure.
  304. */
  305. function sugar_chown($filename, $user='') {
  306. if(!is_windows()){
  307. if(strlen($user)){
  308. return chown($filename, $user);
  309. }else{
  310. if(strlen($GLOBALS['sugar_config']['default_permissions']['user'])){
  311. $user = $GLOBALS['sugar_config']['default_permissions']['user'];
  312. return chown($filename, $user);
  313. }else{
  314. return false;
  315. }
  316. }
  317. }
  318. return true;
  319. }
  320.  
  321. /**
  322. * sugar_chgrp
  323. * Attempts to change the group of the file filename to the group specified in the
  324. * default_permissions configuration; otherwise it will use the group value.
  325. *
  326. * @param filename - Path to the file
  327. * @param group - A group name or number
  328. * @return boolean - Returns TRUE on success or FALSE on failure.
  329. */
  330. function sugar_chgrp($filename, $group='') {
  331. if(!is_windows()){
  332. if(!empty($group)){
  333. return chgrp($filename, $group);
  334. }else{
  335. if(!empty($GLOBALS['sugar_config']['default_permissions']['group'])){
  336. $group = $GLOBALS['sugar_config']['default_permissions']['group'];
  337. return chgrp($filename, $group);
  338. }else{
  339. return false;
  340. }
  341. }
  342. }
  343. return true;
  344. }
  345.  
  346. /**
  347. * get_mode
  348. *
  349. * Will check to see if there is a default mode defined in the config file, otherwise return the
  350. * $mode given as input
  351. *
  352. * @param int $mode - the mode being passed by the calling function. This value will be overridden by a value
  353. * defined in the config file.
  354. * @return int - the mode either found in the config file or passed in via the input parameter
  355. */
  356. function get_mode($key = 'dir_mode', $mode=null) {
  357. if ( !is_int($mode) )
  358. $mode = (int) $mode;
  359. if(!class_exists('SugarConfig', true)) {
  360. require 'include/SugarObjects/SugarConfig.php';
  361. }
  362. if(!is_windows()){
  363. $conf_inst=SugarConfig::getInstance();
  364. $mode = $conf_inst->get('default_permissions.'.$key, $mode);
  365. }
  366. return $mode;
  367. }
  368.  
  369. function sugar_is_dir($path, $mode='r'){
  370. if(defined('TEMPLATE_URL'))return is_dir($path, $mode);
  371. return is_dir($path);
  372. }
  373.  
  374. function sugar_is_file($path, $mode='r'){
  375. if(defined('TEMPLATE_URL'))return is_file($path, $mode);
  376. return is_file($path);
  377. }
  378.  
  379. /**
  380. * Get filename in cache directory
  381. * @api
  382. * @param string $file
  383. */
  384. function sugar_cached($file)
  385. {
  386. static $cdir = null;
  387. if(empty($cdir) && !empty($GLOBALS['sugar_config']['cache_dir'])) {
  388. $cdir = rtrim($GLOBALS['sugar_config']['cache_dir'], '/\\');
  389. }
  390. if(empty($cdir)) {
  391. $cdir = "cache";
  392. }
  393. return "$cdir/$file";
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement