Skip to content

Commit

Permalink
Merge branch 'wip-MDL-34290-master' of git://github.com/marinaglancy/…
Browse files Browse the repository at this point in the history
…moodle
  • Loading branch information
stronk7 committed Aug 29, 2012
2 parents 42ac7cf + 63d8cce commit 69689b4
Show file tree
Hide file tree
Showing 24 changed files with 1,123 additions and 484 deletions.
1 change: 1 addition & 0 deletions lang/en/repository.php
Expand Up @@ -100,6 +100,7 @@
$string['errornotyourfile'] = 'You cannot pick file which is not added by your';
$string['erroruniquename'] = 'Repository instance name should be unique';
$string['errorpostmaxsize'] = 'The uploaded file may exceed max_post_size directive in php.ini.';
$string['errorwhiledownload'] = 'An error occured while downloading the file: {$a}';
$string['existingrepository'] = 'This repository already exists';
$string['federatedsearch'] = 'Federated search';
$string['fileexists'] = 'File name already being used, please use another name';
Expand Down
22 changes: 12 additions & 10 deletions lib/boxlib.php
Expand Up @@ -176,7 +176,7 @@ function getfiletree($path, $params = array()) {
$params['action'] = 'get_account_tree';
$params['onelevel'] = 1;
$params['params[]'] = 'nozip';
$c = new curl(array('debug'=>$this->debug, 'cache'=>true, 'module_cache'=>'repository'));
$c = new curl(array('debug'=>$this->debug));
$c->setopt(array('CURLOPT_FOLLOWLOCATION'=>1));
try {
$args = array();
Expand All @@ -196,23 +196,25 @@ function getfiletree($path, $params = array()) {
* Get box.net file info
*
* @param string $fileid
* @return string|null
* @param int $timeout request timeout in seconds
* @return stdClass|null
*/
function get_file_info($fileid) {
function get_file_info($fileid, $timeout = 0) {
$this->_clearErrors();
$params = array();
$params['action'] = 'get_file_info';
$params['file_id'] = $fileid;
$params['auth_token'] = $this->auth_token;
$params['api_key'] = $this->api_key;
$http = new curl(array('debug'=>$this->debug, 'cache'=>true, 'module_cache'=>'repository'));
$xml = $http->get($this->_box_api_url, $params);
$o = simplexml_load_string(trim($xml));
if ($o->status == 's_get_file_info') {
return $o->info;
} else {
return null;
$http = new curl(array('debug'=>$this->debug));
$xml = $http->get($this->_box_api_url, $params, array('timeout' => $timeout));
if (!$http->get_errno()) {
$o = simplexml_load_string(trim($xml));
if ($o->status == 's_get_file_info') {
return $o->info;
}
}
return null;
}

/**
Expand Down
4 changes: 0 additions & 4 deletions lib/cronlib.php
Expand Up @@ -466,10 +466,6 @@ function cron_run() {
$fs = get_file_storage();
$fs->cron();

mtrace("Clean up cached external files");
// 1 week
cache_file::cleanup(array(), 60 * 60 * 24 * 7);

mtrace("Cron script completed correctly");

$difftime = microtime_diff($starttime, microtime());
Expand Down
261 changes: 84 additions & 177 deletions lib/filelib.php
Expand Up @@ -804,10 +804,6 @@ function file_save_draft_area_files($draftitemid, $contextid, $component, $filea
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());
Expand All @@ -827,16 +823,18 @@ function file_save_draft_area_files($draftitemid, $contextid, $component, $filea
$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($this);
}

// unchanged file or directory - we keep it as is
unset($newhashes[$oldhash]);
if (!$oldfile->is_directory()) {
Expand Down Expand Up @@ -2328,7 +2326,7 @@ function send_stored_file($stored_file, $lifetime=86400 , $filter=0, $forcedownl
}

// 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;
}
Expand Down Expand Up @@ -2752,6 +2750,8 @@ class curl {
public $info;
/** @var string error */
public $error;
/** @var int error code */
public $errno;

/** @var array cURL options */
private $options;
Expand Down Expand Up @@ -2895,6 +2895,14 @@ public function cleanopt(){
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();
}

/**
Expand Down Expand Up @@ -3119,6 +3127,7 @@ protected function request($url, $options = array()){

$this->info = curl_getinfo($curl);
$this->error = curl_error($curl);
$this->errno = curl_errno($curl);

if ($this->debug){
echo '<h1>Return Data</h1>';
Expand Down Expand Up @@ -3202,6 +3211,62 @@ public function get($url, $params = array(), $options = array()){
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
*
Expand Down Expand Up @@ -3279,6 +3344,15 @@ public function options($url, $options = array()){
public function get_info() {
return $this->info;
}

/**
* Get curl error code
*
* @return int
*/
public function get_errno() {
return $this->errno;
}
}

/**
Expand Down Expand Up @@ -4197,170 +4271,3 @@ function file_pluginfile($relativepath, $forcedownload, $preview = null) {
}

}

/**
* 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);
}
}
}

0 comments on commit 69689b4

Please sign in to comment.