*/
class CRM_Utils_VersionCheck {
const
- PINGBACK_URL = 'http://latest.civicrm.org/stable.php?format=json',
- CACHEFILE_NAME = '[civicrm.files]/persist/version-info-cache.json',
+ CACHEFILE_NAME = 'version-info-cache.json',
// after this length of time we fall back on poor-man's cron (7+ days)
CACHEFILE_EXPIRE = 605000;
*/
public $cronJob = array();
+ /**
+ * @var string
+ */
+ public $pingbackUrl = 'http://latest.civicrm.org/stable.php?format=json';
+
/**
* Pingback params
*
*
* @var string
*/
- protected $cacheFile;
+ public $cacheFile;
/**
* Class constructor.
public function __construct() {
$this->localVersion = CRM_Utils_System::version();
$this->localMajorVersion = $this->getMajorVersion($this->localVersion);
- $this->cacheFile = Civi::paths()->getPath(self::CACHEFILE_NAME);
+ $this->cacheFile = CRM_Core_Config::singleton()->uploadDir . self::CACHEFILE_NAME;
}
/**
* Self-populates version info
+ *
+ * @throws \Exception
*/
public function initialize() {
$this->getJob();
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();
}
}
),
);
$ctx = stream_context_create($params);
- $rawJson = file_get_contents(self::PINGBACK_URL, FALSE, $ctx);
+ $rawJson = file_get_contents($this->pingbackUrl, FALSE, $ctx);
$versionInfo = $rawJson ? json_decode($rawJson, TRUE) : NULL;
// If we couldn't fetch or parse the data $versionInfo will be NULL
// Otherwise it will be an array and we'll cache it.
/**
* 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');
+ }
}
/**