| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * This class glues together the various parts of the extension |
| 14 | * system. |
| 15 | * |
| 16 | * @package CRM |
| 17 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 18 | */ |
| 19 | class CRM_Extension_Browser { |
| 20 | |
| 21 | /** |
| 22 | * An URL for public extensions repository. |
| 23 | * |
| 24 | * Note: This default is now handled through setting/*.php. |
| 25 | * |
| 26 | * @deprecated |
| 27 | */ |
| 28 | const DEFAULT_EXTENSIONS_REPOSITORY = 'https://civicrm.org/extdir/ver={ver}|cms={uf}'; |
| 29 | |
| 30 | /** |
| 31 | * Relative path below remote repository URL for single extensions file. |
| 32 | */ |
| 33 | const SINGLE_FILE_PATH = '/single'; |
| 34 | |
| 35 | /** |
| 36 | * The name of the single JSON extension cache file. |
| 37 | */ |
| 38 | const CACHE_JSON_FILE = 'extensions.json'; |
| 39 | |
| 40 | // timeout for when the connection or the server is slow |
| 41 | const CHECK_TIMEOUT = 5; |
| 42 | |
| 43 | /** |
| 44 | * @param string $repoUrl |
| 45 | * URL of the remote repository. |
| 46 | * @param string $indexPath |
| 47 | * Relative path of the 'index' file within the repository. |
| 48 | * @param string $cacheDir |
| 49 | * Local path in which to cache files. |
| 50 | */ |
| 51 | public function __construct($repoUrl, $indexPath, $cacheDir) { |
| 52 | $this->repoUrl = $repoUrl; |
| 53 | $this->cacheDir = $cacheDir; |
| 54 | $this->indexPath = empty($indexPath) ? self::SINGLE_FILE_PATH : $indexPath; |
| 55 | if ($cacheDir && !file_exists($cacheDir) && is_dir(dirname($cacheDir)) && is_writable(dirname($cacheDir))) { |
| 56 | CRM_Utils_File::createDir($cacheDir, FALSE); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Determine whether the system policy allows downloading new extensions. |
| 62 | * |
| 63 | * This is reflection of *policy* and *intent*; it does not indicate whether |
| 64 | * the browser will actually *work*. For that, see checkRequirements(). |
| 65 | * |
| 66 | * @return bool |
| 67 | */ |
| 68 | public function isEnabled() { |
| 69 | return (FALSE !== $this->getRepositoryUrl()); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return string |
| 74 | */ |
| 75 | public function getRepositoryUrl() { |
| 76 | return $this->repoUrl; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Refresh the cache of remotely-available extensions. |
| 81 | */ |
| 82 | public function refresh() { |
| 83 | $file = $this->getTsPath(); |
| 84 | if (file_exists($file)) { |
| 85 | unlink($file); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Determine whether downloading is supported. |
| 91 | * |
| 92 | * @return array |
| 93 | * List of error messages; empty if OK. |
| 94 | */ |
| 95 | public function checkRequirements() { |
| 96 | if (!$this->isEnabled()) { |
| 97 | return []; |
| 98 | } |
| 99 | |
| 100 | $errors = []; |
| 101 | |
| 102 | if (!$this->cacheDir || !is_dir($this->cacheDir) || !is_writable($this->cacheDir)) { |
| 103 | $civicrmDestination = urlencode(CRM_Utils_System::url('civicrm/admin/extensions', 'reset=1')); |
| 104 | $url = CRM_Utils_System::url('civicrm/admin/setting/path', "reset=1&civicrmDestination=${civicrmDestination}"); |
| 105 | $errors[] = array( |
| 106 | 'title' => ts('Directory Unwritable'), |
| 107 | 'message' => ts('Your extensions cache directory (%1) is not web server writable. Please go to the <a href="%2">path setting page</a> and correct it.<br/>', |
| 108 | array( |
| 109 | 1 => $this->cacheDir, |
| 110 | 2 => $url, |
| 111 | ) |
| 112 | ), |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | return $errors; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get a list of all available extensions. |
| 121 | * |
| 122 | * @return array |
| 123 | * ($key => CRM_Extension_Info) |
| 124 | */ |
| 125 | public function getExtensions() { |
| 126 | if (!$this->isEnabled() || count($this->checkRequirements())) { |
| 127 | return []; |
| 128 | } |
| 129 | |
| 130 | $exts = []; |
| 131 | |
| 132 | $remote = $this->_discoverRemote(); |
| 133 | if (is_array($remote)) { |
| 134 | foreach ($remote as $dc => $e) { |
| 135 | $exts[$e->key] = $e; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return $exts; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Get a description of a particular extension. |
| 144 | * |
| 145 | * @param string $key |
| 146 | * Fully-qualified extension name. |
| 147 | * |
| 148 | * @return CRM_Extension_Info|NULL |
| 149 | */ |
| 150 | public function getExtension($key) { |
| 151 | // TODO optimize performance -- we don't need to fetch/cache the entire repo |
| 152 | $exts = $this->getExtensions(); |
| 153 | if (array_key_exists($key, $exts)) { |
| 154 | return $exts[$key]; |
| 155 | } |
| 156 | else { |
| 157 | return NULL; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @return array |
| 163 | * @throws CRM_Extension_Exception_ParseException |
| 164 | */ |
| 165 | private function _discoverRemote() { |
| 166 | $tsPath = $this->getTsPath(); |
| 167 | $timestamp = FALSE; |
| 168 | |
| 169 | if (file_exists($tsPath)) { |
| 170 | $timestamp = file_get_contents($tsPath); |
| 171 | } |
| 172 | |
| 173 | // 3 minutes ago for now |
| 174 | $outdated = (int) $timestamp < (time() - 180) ? TRUE : FALSE; |
| 175 | |
| 176 | if (!$timestamp || $outdated) { |
| 177 | $remotes = json_decode($this->grabRemoteJson(), TRUE); |
| 178 | } |
| 179 | else { |
| 180 | $remotes = json_decode($this->grabCachedJson(), TRUE); |
| 181 | } |
| 182 | |
| 183 | $this->_remotesDiscovered = []; |
| 184 | foreach ((array) $remotes as $id => $xml) { |
| 185 | $ext = CRM_Extension_Info::loadFromString($xml); |
| 186 | $this->_remotesDiscovered[] = $ext; |
| 187 | } |
| 188 | |
| 189 | if (file_exists(dirname($tsPath))) { |
| 190 | file_put_contents($tsPath, (string) time()); |
| 191 | } |
| 192 | |
| 193 | return $this->_remotesDiscovered; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Loads the extensions data from the cache file. If it is empty |
| 198 | * or doesn't exist, try fetching from remote instead. |
| 199 | * |
| 200 | * @return string |
| 201 | */ |
| 202 | private function grabCachedJson() { |
| 203 | $filename = $this->cacheDir . DIRECTORY_SEPARATOR . self::CACHE_JSON_FILE . '.' . md5($this->getRepositoryUrl()); |
| 204 | $json = NULL; |
| 205 | if (file_exists($filename)) { |
| 206 | $json = file_get_contents($filename); |
| 207 | } |
| 208 | if (empty($json)) { |
| 209 | $json = $this->grabRemoteJson(); |
| 210 | } |
| 211 | return $json; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Connects to public server and grabs the list of publicly available |
| 216 | * extensions. |
| 217 | * |
| 218 | * @return string |
| 219 | * @throws \CRM_Extension_Exception |
| 220 | */ |
| 221 | private function grabRemoteJson() { |
| 222 | |
| 223 | ini_set('default_socket_timeout', self::CHECK_TIMEOUT); |
| 224 | set_error_handler(array('CRM_Extension_Browser', 'downloadError')); |
| 225 | |
| 226 | if (!ini_get('allow_url_fopen')) { |
| 227 | ini_set('allow_url_fopen', 1); |
| 228 | } |
| 229 | |
| 230 | if (FALSE === $this->getRepositoryUrl()) { |
| 231 | // don't check if the user has configured civi not to check an external |
| 232 | // url for extensions. See CRM-10575. |
| 233 | return []; |
| 234 | } |
| 235 | |
| 236 | $filename = $this->cacheDir . DIRECTORY_SEPARATOR . self::CACHE_JSON_FILE . '.' . md5($this->getRepositoryUrl()); |
| 237 | $url = $this->getRepositoryUrl() . $this->indexPath; |
| 238 | $status = CRM_Utils_HttpClient::singleton()->fetch($url, $filename); |
| 239 | |
| 240 | ini_restore('allow_url_fopen'); |
| 241 | ini_restore('default_socket_timeout'); |
| 242 | |
| 243 | restore_error_handler(); |
| 244 | |
| 245 | if ($status !== CRM_Utils_HttpClient::STATUS_OK) { |
| 246 | throw new CRM_Extension_Exception(ts('The CiviCRM public extensions directory at %1 could not be contacted - please check your webserver can make external HTTP requests or contact CiviCRM team on <a href="http://forum.civicrm.org/">CiviCRM forum</a>.', array(1 => $this->getRepositoryUrl())), 'connection_error'); |
| 247 | } |
| 248 | |
| 249 | // Don't call grabCachedJson here, that would risk infinite recursion |
| 250 | return file_get_contents($filename); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * @return string |
| 255 | */ |
| 256 | private function getTsPath() { |
| 257 | return $this->cacheDir . DIRECTORY_SEPARATOR . 'timestamp.txt'; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * A dummy function required for suppressing download errors. |
| 262 | * |
| 263 | * @param $errorNumber |
| 264 | * @param $errorString |
| 265 | */ |
| 266 | public static function downloadError($errorNumber, $errorString) { |
| 267 | } |
| 268 | |
| 269 | } |