See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 /** 17 * Contains the remote_resource class definition. 18 * 19 * @package tool_moodlenet 20 * @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com> 21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 22 */ 23 namespace tool_moodlenet\local; 24 25 /** 26 * The remote_resource class. 27 * 28 * Objects of type remote_resource provide a means of interacting with resources over HTTP. 29 * 30 * @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com> 31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 32 */ 33 class remote_resource { 34 35 /** @var \curl $curl the curl http helper.*/ 36 protected $curl; 37 38 /** @var url $url the url to the remote resource.*/ 39 protected $url; 40 41 /** @var string $filename the name of this remote file.*/ 42 protected $filename; 43 44 /** @var string $extension the file extension of this remote file.*/ 45 protected $extension; 46 47 /** @var array $headinfo the array of information for the most recent HEAD request.*/ 48 protected $headinfo = []; 49 50 /** @var \stdClass $metadata information about the resource. */ 51 protected $metadata; 52 53 /** 54 * The remote_resource constructor. 55 * 56 * @param \curl $curl a curl object for HTTP requests. 57 * @param url $url the URL of the remote resource. 58 * @param \stdClass $metadata resource metadata such as name, summary, license, etc. 59 */ 60 public function __construct(\curl $curl, url $url, \stdClass $metadata) { 61 $this->curl = $curl; 62 $this->url = $url; 63 $this->filename = pathinfo($this->url->get_path() ?? '', PATHINFO_FILENAME); 64 $this->extension = pathinfo($this->url->get_path() ?? '', PATHINFO_EXTENSION); 65 $this->metadata = $metadata; 66 } 67 68 /** 69 * Return the URL for this remote resource. 70 * 71 * @return url the url object. 72 */ 73 public function get_url(): url { 74 return $this->url; 75 } 76 77 /** 78 * Get the name of the file as taken from the metadata. 79 */ 80 public function get_name(): string { 81 return $this->metadata->name ?? ''; 82 } 83 84 /** 85 * Get the resource metadata. 86 * 87 * @return \stdClass the metadata. 88 */ 89 public function get_metadata(): \stdClass { 90 return$this->metadata; 91 } 92 93 /** 94 * Get the description of the resource as taken from the metadata. 95 * 96 * @return string 97 */ 98 public function get_description(): string { 99 return $this->metadata->description ?? ''; 100 } 101 102 /** 103 * Return the extension of the file, if found. 104 * 105 * @return string the extension of the file, if found. 106 */ 107 public function get_extension(): string { 108 return $this->extension; 109 } 110 111 /** 112 * Returns the file size of the remote file, in bytes, or null if it cannot be determined. 113 * 114 * @return int|null the content length, if able to be determined, otherwise null. 115 */ 116 public function get_download_size(): ?int { 117 $this->get_resource_info(); 118 return $this->headinfo['download_content_length'] ?? null; 119 } 120 121 /** 122 * Download the remote resource to a local requestdir, returning the path and name of the resulting file. 123 * 124 * @return array an array containing filepath adn filename, e.g. [filepath, filename]. 125 * @throws \moodle_exception if the file cannot be downloaded. 126 */ 127 public function download_to_requestdir(): array { 128 $filename = sprintf('%s.%s', $this->filename, $this->get_extension()); 129 $path = make_request_directory(); 130 $fullpathwithname = sprintf('%s/%s', $path, $filename); 131 132 // In future, use a timeout (download and/or connection) controlled by a tool_moodlenet setting. 133 $downloadtimeout = 30; 134 135 $result = $this->curl->download_one($this->url->get_value(), null, ['filepath' => $fullpathwithname, 136 'timeout' => $downloadtimeout]); 137 if ($result !== true) { 138 throw new \moodle_exception('errorduringdownload', 'tool_moodlenet', '', $result); 139 } 140 141 return [$path, $filename]; 142 } 143 144 /** 145 * Fetches information about the remote resource via a HEAD request. 146 * 147 * @throws \coding_exception if any connection problems occur. 148 */ 149 protected function get_resource_info() { 150 if (!empty($this->headinfo)) { 151 return; 152 } 153 $options['CURLOPT_RETURNTRANSFER'] = 1; 154 $options['CURLOPT_FOLLOWLOCATION'] = 1; 155 $options['CURLOPT_MAXREDIRS'] = 5; 156 $options['CURLOPT_FAILONERROR'] = 1; // We want to consider http error codes as errors to report, not just status codes. 157 158 $this->curl->head($this->url->get_value(), $options); 159 $errorno = $this->curl->get_errno(); 160 $this->curl->resetopt(); 161 162 if ($errorno !== 0) { 163 $message = 'Problem during HEAD request for remote resource \''.$this->url->get_value().'\'. Curl Errno: ' . $errorno; 164 throw new \coding_exception($message); 165 } 166 $this->headinfo = $this->curl->get_info(); 167 } 168 169 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body