[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 3 // This file is part of Moodle - http://moodle.org/ 4 // 5 // Moodle is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // Moodle is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 17 18 /** 19 * Provides tool_installaddon_pluginfo_client and related classes 20 * 21 * @package tool_installaddon 22 * @subpackage classes 23 * @copyright 2013 David Mudrak <[email protected]> 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 /** 30 * Implements a client for https://download.moodle.org/api/x.y/pluginfo.php service 31 * 32 * @copyright 2013 David Mudrak <[email protected]> 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class tool_installaddon_pluginfo_client { 36 37 /** 38 * Factory method returning an instance of this class. 39 * 40 * @return tool_installaddon_pluginfo_client 41 */ 42 public static function instance() { 43 return new static(); 44 } 45 46 /** 47 * Return the information about the plugin 48 * 49 * @throws tool_installaddon_pluginfo_exception 50 * @param string $component 51 * @param string $version 52 * @return stdClass the pluginfo structure 53 */ 54 public function get_pluginfo($component, $version) { 55 56 $response = $this->call_service($component, $version); 57 $response = $this->decode_response($response); 58 $this->validate_response($response); 59 60 return $response->pluginfo; 61 } 62 63 // End of external API ///////////////////////////////////////////////// 64 65 /** 66 * @see self::instance() 67 */ 68 protected function __construct() { 69 } 70 71 /** 72 * Calls the pluginfo.php service and returns the raw response 73 * 74 * @param string $component 75 * @param string $version 76 * @return string 77 */ 78 protected function call_service($component, $version) { 79 global $CFG; 80 require_once($CFG->libdir.'/filelib.php'); 81 82 $curl = new curl(array('proxy' => true)); 83 84 $response = $curl->get( 85 $this->service_request_url(), 86 $this->service_request_params($component, $version), 87 $this->service_request_options()); 88 89 $curlerrno = $curl->get_errno(); 90 $curlinfo = $curl->get_info(); 91 92 if (!empty($curlerrno)) { 93 throw new tool_installaddon_pluginfo_exception('err_curl_exec', array( 94 'url' => $curlinfo['url'], 'errno' => $curlerrno, 'error' => $curl->error)); 95 96 } else if ($curlinfo['http_code'] != 200) { 97 throw new tool_installaddon_pluginfo_exception('err_curl_http_code', array( 98 'url' => $curlinfo['url'], 'http_code' => $curlinfo['http_code'])); 99 100 } else if (isset($curlinfo['ssl_verify_result']) and $curlinfo['ssl_verify_result'] != 0) { 101 throw new tool_installaddon_pluginfo_exception('err_curl_ssl_verify', array( 102 'url' => $curlinfo['url'], 'ssl_verify_result' => $curlinfo['ssl_verify_result'])); 103 } 104 105 return $response; 106 } 107 108 /** 109 * Return URL to the pluginfo.php service 110 * 111 * @return moodle_url 112 */ 113 protected function service_request_url() { 114 global $CFG; 115 116 if (!empty($CFG->config_php_settings['alternativepluginfoserviceurl'])) { 117 $url = $CFG->config_php_settings['alternativepluginfoserviceurl']; 118 } else { 119 $url = 'https://download.moodle.org/api/1.2/pluginfo.php'; 120 } 121 122 return new moodle_url($url); 123 } 124 125 /** 126 * Return list of pluginfo service parameters 127 * 128 * @param string $component 129 * @param string $version 130 * @return array 131 */ 132 protected function service_request_params($component, $version) { 133 134 $params = array(); 135 $params['format'] = 'json'; 136 $params['plugin'] = $component.'@'.$version; 137 138 return $params; 139 } 140 141 /** 142 * Return cURL options for the service request 143 * 144 * @return array of (string)param => (string)value 145 */ 146 protected function service_request_options() { 147 global $CFG; 148 149 $options = array( 150 'CURLOPT_SSL_VERIFYHOST' => 2, // this is the default in {@link curl} class but just in case 151 'CURLOPT_SSL_VERIFYPEER' => true, 152 ); 153 154 return $options; 155 } 156 157 /** 158 * Decode the raw service response 159 * 160 * @param string $raw 161 * @return stdClass 162 */ 163 protected function decode_response($raw) { 164 return json_decode($raw); 165 } 166 167 /** 168 * Validate decoded service response 169 * 170 * @param stdClass $response 171 */ 172 protected function validate_response($response) { 173 174 if (empty($response)) { 175 throw new tool_installaddon_pluginfo_exception('err_response_empty'); 176 } 177 178 if (empty($response->status) or $response->status !== 'OK') { 179 throw new tool_installaddon_pluginfo_exception('err_response_status', $response->status); 180 } 181 182 if (empty($response->apiver) or $response->apiver !== '1.2') { 183 throw new tool_installaddon_pluginfo_exception('err_response_api_version', $response->apiver); 184 } 185 186 if (empty($response->pluginfo->component) or empty($response->pluginfo->downloadurl) 187 or empty($response->pluginfo->downloadmd5)) { 188 throw new tool_installaddon_pluginfo_exception('err_response_pluginfo'); 189 } 190 } 191 } 192 193 194 /** 195 * General exception thrown by {@link tool_installaddon_pluginfo_client} class 196 * 197 * @copyright 2013 David Mudrak <[email protected]> 198 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 199 */ 200 class tool_installaddon_pluginfo_exception extends moodle_exception { 201 202 /** 203 * @param string $errorcode exception description identifier 204 * @param mixed $debuginfo debugging data to display 205 */ 206 public function __construct($errorcode, $a=null, $debuginfo=null) { 207 parent::__construct($errorcode, 'tool_installaddon', '', $a, print_r($debuginfo, true)); 208 } 209 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |