continue;
}
- // Replaced file content
- if ($oldfile->get_contenthash() != $newfile->get_contenthash()) {
- $oldfile->replace_content_with($newfile);
- }
// Updated author
if ($oldfile->get_author() != $newfile->get_author()) {
$oldfile->set_author($newfile->get_author());
$oldfile->set_sortorder($newfile->get_sortorder());
}
- // Update file size
- if ($oldfile->get_filesize() != $newfile->get_filesize()) {
- $oldfile->set_filesize($newfile->get_filesize());
- }
-
// Update file timemodified
if ($oldfile->get_timemodified() != $newfile->get_timemodified()) {
$oldfile->set_timemodified($newfile->get_timemodified());
}
+ // Replaced file content
+ if ($oldfile->get_contenthash() != $newfile->get_contenthash() || $oldfile->get_filesize() != $newfile->get_filesize()) {
+ $oldfile->replace_content_with($newfile);
+ // push changes to all local files that are referencing this file
+ $fs->update_references_to_storedfile($oldfile);
+ }
+
// unchanged file or directory - we keep it as is
unset($newhashes[$oldhash]);
if (!$oldfile->is_directory()) {
}
// handle external resource
- if ($stored_file && $stored_file->is_external_file()) {
+ if ($stored_file && $stored_file->is_external_file() && !isset($options['sendcachedexternalfile'])) {
$stored_file->send_file($lifetime, $filter, $forcedownload, $options);
die;
}
public $info;
/** @var string error */
public $error;
+ /** @var int error code */
+ public $errno;
/** @var array cURL options */
private $options;
unset($this->options['CURLOPT_INFILE']);
unset($this->options['CURLOPT_INFILESIZE']);
unset($this->options['CURLOPT_CUSTOMREQUEST']);
+ unset($this->options['CURLOPT_FILE']);
+ }
+
+ /**
+ * Resets the HTTP Request headers (to prepare for the new request)
+ */
+ public function resetHeader() {
+ $this->header = array();
}
/**
$this->info = curl_getinfo($curl);
$this->error = curl_error($curl);
+ $this->errno = curl_errno($curl);
if ($this->debug){
echo '<h1>Return Data</h1>';
return $this->request($url, $options);
}
+ /**
+ * Downloads one file and writes it to the specified file handler
+ *
+ * <code>
+ * $c = new curl();
+ * $file = fopen('savepath', 'w');
+ * $result = $c->download_one('http://localhost/', null,
+ * array('file' => $file, 'timeout' => 5, 'followlocation' => true, 'maxredirs' => 3));
+ * fclose($file);
+ * $download_info = $c->get_info();
+ * if ($result === true) {
+ * // file downloaded successfully
+ * } else {
+ * $error_text = $result;
+ * $error_code = $c->get_errno();
+ * }
+ * </code>
+ *
+ * <code>
+ * $c = new curl();
+ * $result = $c->download_one('http://localhost/', null,
+ * array('filepath' => 'savepath', 'timeout' => 5, 'followlocation' => true, 'maxredirs' => 3));
+ * // ... see above, no need to close handle and remove file if unsuccessful
+ * </code>
+ *
+ * @param string $url
+ * @param array|null $params key-value pairs to be added to $url as query string
+ * @param array $options request options. Must include either 'file' or 'filepath'
+ * @return bool|string true on success or error string on failure
+ */
+ public function download_one($url, $params, $options = array()) {
+ $options['CURLOPT_HTTPGET'] = 1;
+ $options['CURLOPT_BINARYTRANSFER'] = true;
+ if (!empty($params)){
+ $url .= (stripos($url, '?') !== false) ? '&' : '?';
+ $url .= http_build_query($params, '', '&');
+ }
+ if (!empty($options['filepath']) && empty($options['file'])) {
+ // open file
+ if (!($options['file'] = fopen($options['filepath'], 'w'))) {
+ $this->errno = 100;
+ return get_string('cannotwritefile', 'error', $options['filepath']);
+ }
+ $filepath = $options['filepath'];
+ }
+ unset($options['filepath']);
+ $result = $this->request($url, $options);
+ if (isset($filepath)) {
+ fclose($options['file']);
+ if ($result !== true) {
+ unlink($filepath);
+ }
+ }
+ return $result;
+ }
+
/**
* HTTP PUT method
*
public function get_info() {
return $this->info;
}
+
+ /**
+ * Get curl error code
+ *
+ * @return int
+ */
+ public function get_errno() {
+ return $this->errno;
+ }
}
/**
}
}
-
-/**
- * Universe file cacheing class
- *
- * @package core_files
- * @category files
- * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class cache_file {
- /** @var string */
- public $cachedir = '';
-
- /**
- * static method to create cache_file class instance
- *
- * @param array $options caching ooptions
- */
- public static function get_instance($options = array()) {
- return new cache_file($options);
- }
-
- /**
- * Constructor
- *
- * @param array $options
- */
- private function __construct($options = array()) {
- global $CFG;
-
- // Path to file caches.
- if (isset($options['cachedir'])) {
- $this->cachedir = $options['cachedir'];
- } else {
- $this->cachedir = $CFG->cachedir . '/filedir';
- }
-
- // Create cache directory.
- if (!file_exists($this->cachedir)) {
- mkdir($this->cachedir, $CFG->directorypermissions, true);
- }
-
- // When use cache_file::get, it will check ttl.
- if (isset($options['ttl']) && is_numeric($options['ttl'])) {
- $this->ttl = $options['ttl'];
- } else {
- // One day.
- $this->ttl = 60 * 60 * 24;
- }
- }
-
- /**
- * Get cached file, false if file expires
- *
- * @param mixed $param
- * @param array $options caching options
- * @return bool|string
- */
- public static function get($param, $options = array()) {
- $instance = self::get_instance($options);
- $filepath = $instance->generate_filepath($param);
- if (file_exists($filepath)) {
- $lasttime = filemtime($filepath);
- if (time() - $lasttime > $instance->ttl) {
- // Remove cache file.
- unlink($filepath);
- return false;
- } else {
- return $filepath;
- }
- } else {
- return false;
- }
- }
-
- /**
- * Static method to create cache from a file
- *
- * @param mixed $ref
- * @param string $srcfile
- * @param array $options
- * @return string cached file path
- */
- public static function create_from_file($ref, $srcfile, $options = array()) {
- $instance = self::get_instance($options);
- $cachedfilepath = $instance->generate_filepath($ref);
- copy($srcfile, $cachedfilepath);
- return $cachedfilepath;
- }
-
- /**
- * Static method to create cache from url
- *
- * @param mixed $ref file reference
- * @param string $url file url
- * @param array $options options
- * @return string cached file path
- */
- public static function create_from_url($ref, $url, $options = array()) {
- $instance = self::get_instance($options);
- $cachedfilepath = $instance->generate_filepath($ref);
- $fp = fopen($cachedfilepath, 'w');
- $curl = new curl;
- $curl->download(array(array('url'=>$url, 'file'=>$fp)));
- // Must close file handler.
- fclose($fp);
- return $cachedfilepath;
- }
-
- /**
- * Static method to create cache from string
- *
- * @param mixed $ref file reference
- * @param string $url file url
- * @param array $options options
- * @return string cached file path
- */
- public static function create_from_string($ref, $string, $options = array()) {
- $instance = self::get_instance($options);
- $cachedfilepath = $instance->generate_filepath($ref);
- $fp = fopen($cachedfilepath, 'w');
- fwrite($fp, $string);
- // Must close file handler.
- fclose($fp);
- return $cachedfilepath;
- }
-
- /**
- * Build path to cache file
- *
- * @param mixed $ref
- * @return string
- */
- private function generate_filepath($ref) {
- global $CFG;
- $hash = sha1(serialize($ref));
- $l1 = $hash[0].$hash[1];
- $l2 = $hash[2].$hash[3];
- $dir = $this->cachedir . "/$l1/$l2";
- if (!file_exists($dir)) {
- mkdir($dir, $CFG->directorypermissions, true);
- }
- return "$dir/$hash";
- }
-
- /**
- * Remove cache files
- *
- * @param array $options options
- * @param int $expire The number of seconds before expiry
- */
- public static function cleanup($options = array(), $expire) {
- global $CFG;
- $instance = self::get_instance($options);
- if ($dir = opendir($instance->cachedir)) {
- while (($file = readdir($dir)) !== false) {
- if (!is_dir($file) && $file != '.' && $file != '..') {
- $lasttime = @filemtime($instance->cachedir . $file);
- if(time() - $lasttime > $expire){
- @unlink($instance->cachedir . $file);
- }
- }
- }
- closedir($dir);
- }
- }
-}