From: Coleman Watts Date: Thu, 10 Dec 2015 17:31:30 +0000 (-0500) Subject: CRM-17637 - Ensure files directory is writeable X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=e2fb6a98e32ea55539f57e79806a9bf492722962;p=civicrm-core.git CRM-17637 - Ensure files directory is writeable --- diff --git a/CRM/Utils/Check/Env.php b/CRM/Utils/Check/Env.php index 89883fe6ed..6d5b616e2b 100644 --- a/CRM/Utils/Check/Env.php +++ b/CRM/Utils/Check/Env.php @@ -288,8 +288,21 @@ class CRM_Utils_Check_Env { */ public function checkVersion() { $messages = array(); - $vc = new CRM_Utils_VersionCheck(); - $vc->initialize(); + try { + $vc = new CRM_Utils_VersionCheck(); + $vc->initialize(); + } + catch (Exception $e) { + $messages[] = new CRM_Utils_Check_Message( + 'checkVersionError', + ts('Directory %1 is not writable. Please change your file permissions.', + array(1 => dirname($vc->cacheFile))), + ts('Directory not writable'), + \Psr\Log\LogLevel::ERROR, + 'fa-times-circle-o' + ); + return $messages; + } // Show a notice if the version_check job is disabled if (empty($vc->cronJob['is_active'])) { @@ -380,7 +393,7 @@ class CRM_Utils_Check_Env { __FUNCTION__, ts('Your extensions directory is not set. Click here to set the extensions directory.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/path', 'reset=1'))), - ts('Extensions directory not writable'), + ts('Directory not writable'), \Psr\Log\LogLevel::NOTICE, 'fa-plug' ); @@ -401,9 +414,9 @@ class CRM_Utils_Check_Env { elseif (!is_writable($basedir)) { $messages[] = new CRM_Utils_Check_Message( __FUNCTION__, - ts('Your extensions directory, %1, is not writable. Please change your file permissions.', + ts('Directory %1 is not writable. Please change your file permissions.', array(1 => $basedir)), - ts('Extensions directory not writable'), + ts('Directory not writable'), \Psr\Log\LogLevel::ERROR, 'fa-plug' ); diff --git a/CRM/Utils/VersionCheck.php b/CRM/Utils/VersionCheck.php index 0a11e53294..316672db4d 100644 --- a/CRM/Utils/VersionCheck.php +++ b/CRM/Utils/VersionCheck.php @@ -97,6 +97,8 @@ class CRM_Utils_VersionCheck { /** * Self-populates version info + * + * @throws \Exception */ public function initialize() { $this->getJob(); @@ -109,6 +111,12 @@ class CRM_Utils_VersionCheck { if (!empty($this->cronJob['is_active']) && (!$this->isInfoAvailable || filemtime($this->cacheFile) < $expiryTime) ) { + // First try updating the files modification time, for 2 reasons: + // - if the file is not writeable, this saves the trouble of pinging back + // - if the remote server is down, this will prevent an immediate retry + if (touch($this->cacheFile) === FALSE) { + throw new Exception('File not writable'); + } $this->fetch(); } } @@ -412,11 +420,12 @@ class CRM_Utils_VersionCheck { /** * Save version info to file. * @param string $contents + * @throws \Exception */ private function writeCacheFile($contents) { - $fp = fopen($this->cacheFile, 'w'); - fwrite($fp, $contents); - fclose($fp); + if (file_put_contents($this->cacheFile, $contents) === FALSE) { + throw new Exception('File not writable'); + } } /**