. /** * This plugin is used to access files by providing an url * * @since Moodle 2.0 * @package repository_url * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once($CFG->dirroot . '/repository/lib.php'); require_once(dirname(__FILE__).'/locallib.php'); /** * repository_url class * A subclass of repository, which is used to download a file from a specific url * * @since Moodle 2.0 * @package repository_url * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class repository_url extends repository { var $processedfiles = array(); /** * @param int $repositoryid * @param object $context * @param array $options */ public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()){ global $CFG; parent::__construct($repositoryid, $context, $options); $this->file_url = optional_param('file', '', PARAM_RAW); $this->file_url = $this->escape_url($this->file_url); } public function check_login() { if (!empty($this->file_url)) { return true; } else { return false; } } /** * @return mixed */ public function print_login() { $strdownload = get_string('download', 'repository'); $strname = get_string('rename', 'repository_url'); $strurl = get_string('url', 'repository_url'); if ($this->options['ajax']) { $url = new stdClass(); $url->label = $strurl.': '; $url->id = 'fileurl'; $url->type = 'text'; $url->name = 'file'; $ret['login'] = array($url); $ret['login_btn_label'] = get_string('download', 'repository_url'); $ret['allowcaching'] = true; // indicates that login form can be cached in filepicker.js return $ret; } else { echo << {$strurl}: EOD; } } /** * @param mixed $path * @param string $search * @return array */ public function get_listing($path='', $page='') { $ret = array(); $ret['list'] = array(); $ret['nosearch'] = true; $ret['norefresh'] = true; $ret['nologin'] = true; $this->file_url = clean_param($this->file_url, PARAM_URL); if (empty($this->file_url)) { throw new repository_exception('validfiletype', 'repository_url'); } $this->parse_file(null, $this->file_url, $ret, true); return $ret; } /** * Parses one file (either html or css) * * @param string $baseurl (optional) URL of the file where link to this file was found * @param string $relativeurl relative or absolute link to the file * @param array $list * @param bool $mainfile true only for main HTML false and false for all embedded/linked files */ protected function parse_file($baseurl, $relativeurl, &$list, $mainfile = false) { if (preg_match('/([\'"])(.*)\1/', $relativeurl, $matches)) { $relativeurl = $matches[2]; } if (empty($baseurl)) { $url = $relativeurl; } else { $url = htmlspecialchars_decode(url_to_absolute($baseurl, $relativeurl)); } if (in_array($url, $this->processedfiles)) { // avoid endless recursion return; } $this->processedfiles[] = $url; $curl = new curl; $curl->setopt(array('CURLOPT_FOLLOWLOCATION' => true, 'CURLOPT_MAXREDIRS' => 3)); $msg = $curl->head($url); $info = $curl->get_info(); if ($info['http_code'] != 200) { if ($mainfile) { $list['error'] = $msg; } } else { $csstoanalyze = ''; if ($mainfile && (strstr($info['content_type'], 'text/html') || empty($info['content_type']))) { // parse as html $htmlcontent = $curl->get($info['url']); $ddoc = new DOMDocument(); @$ddoc->loadHTML($htmlcontent); // extract $tags = $ddoc->getElementsByTagName('img'); foreach ($tags as $tag) { $url = $tag->getAttribute('src'); $this->add_image_to_list($info['url'], $url, $list); } // analyse embedded css (