From 4dbb2b0507b922cab6843d1714a6c70e751f08d1 Mon Sep 17 00:00:00 2001 From: dongsheng Date: Sun, 28 Jun 2009 09:38:32 +0000 Subject: [PATCH] "MDL-19180, repository url plugin, if users provide a url of webpage, this plugin could list all images in this page" --- lib/filelib.php | 7 ++- repository/url/repository.class.php | 72 +++++++++++++++++++++++------ 2 files changed, 64 insertions(+), 15 deletions(-) diff --git a/lib/filelib.php b/lib/filelib.php index 6de214281a..bceb262450 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -2285,7 +2285,9 @@ class curl { if (empty($this->error)){ return $ret; } else { - throw new moodle_exception($this->error, 'curl'); + return $this->error; + // exception is not ajax friendly + //throw new moodle_exception($this->error, 'curl'); } } @@ -2417,6 +2419,9 @@ class curl { $ret = $this->request($url, $options); return $ret; } + public function get_info() { + return $this->info; + } } /** diff --git a/repository/url/repository.class.php b/repository/url/repository.class.php index dd315bffa5..b6ad1f3e3f 100755 --- a/repository/url/repository.class.php +++ b/repository/url/repository.class.php @@ -21,11 +21,12 @@ class repository_url extends repository { // will be used to construct download form $this->client_id = $options['client_id']; } - $this->file_url = optional_param('download_from', '', PARAM_RAW); + $this->file_url = optional_param('file', '', PARAM_RAW); } public function get_file($url, $file = '') { global $CFG; + //$CFG->repository_no_delete = true; $path = $this->prepare_file($file); $fp = fopen($path, 'w'); $c = new curl; @@ -54,15 +55,8 @@ class repository_url extends repository { $url->type = 'text'; $url->name = 'file'; - $title = new stdclass; - $title->label = $strname.': '; - $title->id = 'newname-'.$this->client_id; - $title->type = 'text'; - $title->name = 'title'; - - $ret['login'] = array($url, $title); + $ret['login'] = array($url); $ret['login_btn_label'] = get_string('download', 'repository_url'); - $ret['login_btn_action'] = 'download'; return $ret; } else { echo << {$strurl}: - -{$strname}: - - EOD; @@ -87,7 +77,61 @@ EOD; * @return array */ public function get_listing($path='', $page='') { - $this->print_login(); + global $CFG; + set_time_limit(0); + $ret = array(); + $curl = new curl; + $msg = $curl->head($this->file_url); + $info = $curl->get_info(); + if ($info['http_code'] != 200) { + $ret['e'] = $msg; + } else { + $ret['list'] = array(); + $ret['nosearch'] = true; + $ret['nologin'] = true; + $filename = $this->guess_filename($info['url'], $info['content_type']); + if (strstr($info['content_type'], 'text/html') || empty($info['content_type'])) { + // analysis this web page, general file list + $ret['list'] = array(); + $a = $curl->get($info['url']); + $this->analyse_page($a, $ret); + } else { + // download this file + $ret['list'][] = array( + 'title'=>$filename, + 'source'=>$this->file_url, + 'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo('icon32', $filename) + ); + } + } + return $ret; + } + public function analyse_page($content, &$list) { + global $CFG; + $pattern = '#src="?\'?([[:alnum:]:?=&@/._+-]+)"?\'?#i'; + $matches = null; + preg_match_all($pattern, $content, $matches); + $matches = array_unique($matches[1]); + if (!empty($matches)) { + foreach($matches as $url) { + $list['list'][] = array( + 'title'=>$this->guess_filename($url, ''), + // XXX: need to convert relative url to absolute url + 'source'=>$url, + 'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo('icon32', $url) + ); + } + } + } + public function guess_filename($url, $type) { + $pattern = '#\/([\w_\?\-.]+)$#'; + $matches = null; + preg_match($pattern, $url, $matches); + if (empty($matches[1])) { + return $url; + } else { + return $matches[1]; + } } public function get_name(){ -- 2.39.5