| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | define('API_V3_EXTENSION_DELIMITER', ','); |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * This provides an api interface for CiviCRM extension management. |
| 33 | * |
| 34 | * @package CiviCRM_APIv3 |
| 35 | */ |
| 36 | |
| 37 | /** |
| 38 | * Install an extension. |
| 39 | * |
| 40 | * @param array $params |
| 41 | * Input parameters. |
| 42 | * - key: string, eg "com.example.myextension" |
| 43 | * - keys: array of string, eg array("com.example.myextension1", "com.example.myextension2") |
| 44 | * - path: string, e.g. "/var/www/extensions/*" |
| 45 | * |
| 46 | * Using 'keys' should be more performant than making multiple API calls with 'key' |
| 47 | * |
| 48 | * @return array |
| 49 | */ |
| 50 | function civicrm_api3_extension_install($params) { |
| 51 | $keys = _civicrm_api3_getKeys($params); |
| 52 | if (!$keys) { |
| 53 | return civicrm_api3_create_success(); |
| 54 | } |
| 55 | |
| 56 | try { |
| 57 | $manager = CRM_Extension_System::singleton()->getManager(); |
| 58 | $manager->install($manager->findInstallRequirements($keys)); |
| 59 | } |
| 60 | catch (CRM_Extension_Exception $e) { |
| 61 | return civicrm_api3_create_error($e->getMessage()); |
| 62 | } |
| 63 | |
| 64 | return civicrm_api3_create_success(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Spec function for getfields |
| 69 | * @param $fields |
| 70 | */ |
| 71 | function _civicrm_api3_extension_install_spec(&$fields) { |
| 72 | $fields['keys'] = array( |
| 73 | 'title' => 'Extension Key(s)', |
| 74 | 'api.aliases' => array('key'), |
| 75 | 'type' => CRM_Utils_Type::T_STRING, |
| 76 | 'description' => 'Fully qualified name of one or more extensions', |
| 77 | ); |
| 78 | $fields['path'] = array( |
| 79 | 'title' => 'Extension Path', |
| 80 | 'type' => CRM_Utils_Type::T_STRING, |
| 81 | 'description' => 'The path to the extension. May use wildcard ("*").', |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Upgrade an extension - runs upgrade_N hooks and system.flush. |
| 87 | * |
| 88 | * @return array |
| 89 | * API result |
| 90 | */ |
| 91 | function civicrm_api3_extension_upgrade() { |
| 92 | CRM_Core_Invoke::rebuildMenuAndCaches(TRUE); |
| 93 | $queue = CRM_Extension_Upgrades::createQueue(); |
| 94 | $runner = new CRM_Queue_Runner(array( |
| 95 | 'title' => 'Extension Upgrades', |
| 96 | 'queue' => $queue, |
| 97 | 'errorMode' => CRM_Queue_Runner::ERROR_ABORT, |
| 98 | )); |
| 99 | |
| 100 | try { |
| 101 | $result = $runner->runAll(); |
| 102 | } |
| 103 | catch (CRM_Extension_Exception $e) { |
| 104 | return civicrm_api3_create_error($e->getMessage()); |
| 105 | } |
| 106 | |
| 107 | if ($result === TRUE) { |
| 108 | return civicrm_api3_create_success(); |
| 109 | } |
| 110 | else { |
| 111 | return $result; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Enable an extension. |
| 117 | * |
| 118 | * @param array $params |
| 119 | * Input parameters. |
| 120 | * - key: string, eg "com.example.myextension" |
| 121 | * - keys: array of string, eg array("com.example.myextension1", "com.example.myextension2") |
| 122 | * - path: string, e.g. "/var/www/vendor/foo/myext" or "/var/www/vendor/*" |
| 123 | * |
| 124 | * Using 'keys' should be more performant than making multiple API calls with 'key' |
| 125 | * |
| 126 | * @return array |
| 127 | */ |
| 128 | function civicrm_api3_extension_enable($params) { |
| 129 | $keys = _civicrm_api3_getKeys($params); |
| 130 | if (count($keys) == 0) { |
| 131 | return civicrm_api3_create_success(); |
| 132 | } |
| 133 | |
| 134 | $manager = CRM_Extension_System::singleton()->getManager(); |
| 135 | $manager->enable($manager->findInstallRequirements($keys)); |
| 136 | return civicrm_api3_create_success(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Spec function for getfields |
| 141 | * @param $fields |
| 142 | */ |
| 143 | function _civicrm_api3_extension_enable_spec(&$fields) { |
| 144 | _civicrm_api3_extension_install_spec($fields); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Disable an extension. |
| 149 | * |
| 150 | * @param array $params |
| 151 | * Input parameters. |
| 152 | * - key: string, eg "com.example.myextension" |
| 153 | * - keys: array of string, eg array("com.example.myextension1", "com.example.myextension2") |
| 154 | * - path: string, e.g. "/var/www/vendor/foo/myext" or "/var/www/vendor/*" |
| 155 | * |
| 156 | * Using 'keys' should be more performant than making multiple API calls with 'key' |
| 157 | * |
| 158 | * @return array |
| 159 | */ |
| 160 | function civicrm_api3_extension_disable($params) { |
| 161 | $keys = _civicrm_api3_getKeys($params); |
| 162 | if (count($keys) == 0) { |
| 163 | return civicrm_api3_create_success(); |
| 164 | } |
| 165 | |
| 166 | CRM_Extension_System::singleton()->getManager()->disable($keys); |
| 167 | return civicrm_api3_create_success(); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Spec function for getfields |
| 172 | * @param $fields |
| 173 | */ |
| 174 | function _civicrm_api3_extension_disable_spec(&$fields) { |
| 175 | _civicrm_api3_extension_install_spec($fields); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Uninstall an extension. |
| 180 | * |
| 181 | * @param array $params |
| 182 | * Input parameters. |
| 183 | * - key: string, eg "com.example.myextension" |
| 184 | * - keys: array of string, eg array("com.example.myextension1", "com.example.myextension2") |
| 185 | * - path: string, e.g. "/var/www/vendor/foo/myext" or "/var/www/vendor/*" |
| 186 | * |
| 187 | * Using 'keys' should be more performant than making multiple API calls with 'key' |
| 188 | * |
| 189 | * @todo: removeFiles as optional param |
| 190 | * |
| 191 | * @return array |
| 192 | */ |
| 193 | function civicrm_api3_extension_uninstall($params) { |
| 194 | $keys = _civicrm_api3_getKeys($params); |
| 195 | if (count($keys) == 0) { |
| 196 | return civicrm_api3_create_success(); |
| 197 | } |
| 198 | |
| 199 | CRM_Extension_System::singleton()->getManager()->uninstall($keys); |
| 200 | return civicrm_api3_create_success(); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Spec function for getfields |
| 205 | * @param $fields |
| 206 | */ |
| 207 | function _civicrm_api3_extension_uninstall_spec(&$fields) { |
| 208 | _civicrm_api3_extension_install_spec($fields); |
| 209 | //$fields['removeFiles'] = array( |
| 210 | // 'title' => 'Remove files', |
| 211 | // 'description' => 'Whether to remove the source tree. Default FALSE.', |
| 212 | // 'type' => CRM_Utils_Type::T_BOOLEAN, |
| 213 | //); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Download and install an extension. |
| 218 | * |
| 219 | * @param array $params |
| 220 | * Input parameters. |
| 221 | * - key: string, eg "com.example.myextension" |
| 222 | * - url: string eg "http://repo.com/myextension-1.0.zip" |
| 223 | * |
| 224 | * @throws API_Exception |
| 225 | * @return array |
| 226 | * API result |
| 227 | */ |
| 228 | function civicrm_api3_extension_download($params) { |
| 229 | if (!array_key_exists('url', $params)) { |
| 230 | if (!CRM_Extension_System::singleton()->getBrowser()->isEnabled()) { |
| 231 | throw new API_Exception('Automatic downloading is disabled. Try adding parameter "url"'); |
| 232 | } |
| 233 | if ($reqs = CRM_Extension_System::singleton()->getBrowser()->checkRequirements()) { |
| 234 | $first = array_shift($reqs); |
| 235 | throw new API_Exception($first['message']); |
| 236 | } |
| 237 | if ($info = CRM_Extension_System::singleton()->getBrowser()->getExtension($params['key'])) { |
| 238 | if ($info->downloadUrl) { |
| 239 | $params['url'] = $info->downloadUrl; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (!array_key_exists('url', $params)) { |
| 245 | throw new API_Exception('Cannot resolve download url for extension. Try adding parameter "url"'); |
| 246 | } |
| 247 | |
| 248 | foreach (CRM_Extension_System::singleton()->getDownloader()->checkRequirements() as $requirement) { |
| 249 | return civicrm_api3_create_error($requirement['message']); |
| 250 | } |
| 251 | |
| 252 | if (!CRM_Extension_System::singleton()->getDownloader()->download($params['key'], $params['url'])) { |
| 253 | return civicrm_api3_create_error('Download failed - ZIP file is unavailable or malformed'); |
| 254 | } |
| 255 | CRM_Extension_System::singleton()->getCache()->flush(); |
| 256 | CRM_Extension_System::singleton(TRUE); |
| 257 | if (CRM_Utils_Array::value('install', $params, TRUE)) { |
| 258 | CRM_Extension_System::singleton()->getManager()->install(array($params['key'])); |
| 259 | } |
| 260 | |
| 261 | return civicrm_api3_create_success(); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Spec function for getfields |
| 266 | * @param $fields |
| 267 | */ |
| 268 | function _civicrm_api3_extension_download_spec(&$fields) { |
| 269 | $fields['key'] = array( |
| 270 | 'title' => 'Extension Key', |
| 271 | 'api.required' => 1, |
| 272 | 'type' => CRM_Utils_Type::T_STRING, |
| 273 | 'description' => 'Fully qualified name of the extension', |
| 274 | ); |
| 275 | $fields['url'] = array( |
| 276 | 'title' => 'Download URL', |
| 277 | 'type' => CRM_Utils_Type::T_STRING, |
| 278 | 'description' => 'Optional as the system can determine the url automatically for public extensions', |
| 279 | ); |
| 280 | $fields['install'] = array( |
| 281 | 'title' => 'Auto-install', |
| 282 | 'type' => CRM_Utils_Type::T_STRING, |
| 283 | 'description' => 'Automatically install the downloaded extension', |
| 284 | 'api.default' => TRUE, |
| 285 | ); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Download and install an extension. |
| 290 | * |
| 291 | * @param array $params |
| 292 | * Input parameters. |
| 293 | * - local: bool, whether to rescan local filesystem (default: TRUE) |
| 294 | * - remote: bool, whether to rescan remote repository (default: TRUE) |
| 295 | * |
| 296 | * @return array |
| 297 | * API result |
| 298 | */ |
| 299 | function civicrm_api3_extension_refresh($params) { |
| 300 | $system = CRM_Extension_System::singleton(TRUE); |
| 301 | |
| 302 | if ($params['local']) { |
| 303 | $system->getManager()->refresh(); |
| 304 | $system->getManager()->getStatuses(); // force immediate scan |
| 305 | } |
| 306 | |
| 307 | if ($params['remote']) { |
| 308 | if ($system->getBrowser()->isEnabled() && empty($system->getBrowser()->checkRequirements)) { |
| 309 | $system->getBrowser()->refresh(); |
| 310 | $system->getBrowser()->getExtensions(); // force immediate download |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | return civicrm_api3_create_success(); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Spec function for getfields |
| 319 | * @param $fields |
| 320 | */ |
| 321 | function _civicrm_api3_extension_refresh_spec(&$fields) { |
| 322 | $fields['local'] = array( |
| 323 | 'title' => 'Rescan Local', |
| 324 | 'api.default' => 1, |
| 325 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
| 326 | 'description' => 'Whether to rescan the local filesystem (default TRUE)', |
| 327 | ); |
| 328 | $fields['remote'] = array( |
| 329 | 'title' => 'Rescan Remote', |
| 330 | 'api.default' => 1, |
| 331 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
| 332 | 'description' => 'Whether to rescan the remote repository (default TRUE)', |
| 333 | ); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Get a list of available extensions. |
| 338 | * |
| 339 | * @param array $params |
| 340 | * |
| 341 | * @return array |
| 342 | * API result |
| 343 | */ |
| 344 | function civicrm_api3_extension_get($params) { |
| 345 | $full_names = _civicrm_api3_getKeys($params, 'full_name'); |
| 346 | $keys = _civicrm_api3_getKeys($params, 'key'); |
| 347 | $keys = array_merge($full_names, $keys); |
| 348 | $statuses = CRM_Extension_System::singleton()->getManager()->getStatuses(); |
| 349 | $mapper = CRM_Extension_System::singleton()->getMapper(); |
| 350 | $result = array(); |
| 351 | $id = 0; |
| 352 | foreach ($statuses as $key => $status) { |
| 353 | try { |
| 354 | $obj = $mapper->keyToInfo($key); |
| 355 | } |
| 356 | catch (CRM_Extension_Exception $ex) { |
| 357 | CRM_Core_Session::setStatus(ts('Failed to read extension (%1). Please refresh the extension list.', array(1 => $key))); |
| 358 | continue; |
| 359 | } |
| 360 | $info = CRM_Extension_System::createExtendedInfo($obj); |
| 361 | $info['id'] = $id++; // backward compatibility with indexing scheme |
| 362 | if (!empty($keys)) { |
| 363 | if (in_array($key, $keys)) { |
| 364 | $result[] = $info; |
| 365 | } |
| 366 | } |
| 367 | else { |
| 368 | $result[] = $info; |
| 369 | } |
| 370 | } |
| 371 | $options = _civicrm_api3_get_options_from_params($params); |
| 372 | $returnFields = !empty($options['return']) ? $options['return'] : array(); |
| 373 | if (!in_array('id', $returnFields)) { |
| 374 | $returnFields = array_merge($returnFields, array('id')); |
| 375 | } |
| 376 | return _civicrm_api3_basic_array_get('Extension', $params, $result, 'id', $returnFields); |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Get a list of remotely available extensions. |
| 381 | * |
| 382 | * @param array $params |
| 383 | * |
| 384 | * @return array |
| 385 | * API result |
| 386 | */ |
| 387 | function civicrm_api3_extension_getremote($params) { |
| 388 | $extensions = CRM_Extension_System::singleton()->getBrowser()->getExtensions(); |
| 389 | $result = array(); |
| 390 | $id = 0; |
| 391 | foreach ($extensions as $key => $obj) { |
| 392 | $info = array(); |
| 393 | $info['id'] = $id++; // backward compatibility with indexing scheme |
| 394 | $info = array_merge($info, (array) $obj); |
| 395 | $result[] = $info; |
| 396 | } |
| 397 | return _civicrm_api3_basic_array_get('Extension', $params, $result, 'id', CRM_Utils_Array::value('return', $params, array())); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Determine the list of extension keys. |
| 402 | * |
| 403 | * @param array $params |
| 404 | * @param string $key |
| 405 | * API request params with 'keys' or 'path'. |
| 406 | * - keys: A comma-delimited list of extension names |
| 407 | * - path: An absolute directory path. May append '*' to match all sub-directories. |
| 408 | * |
| 409 | * @return array |
| 410 | */ |
| 411 | function _civicrm_api3_getKeys($params, $key = 'keys') { |
| 412 | if ($key == 'path') { |
| 413 | return CRM_Extension_System::singleton()->getMapper()->getKeysByPath($params['path']); |
| 414 | } |
| 415 | if (isset($params[$key])) { |
| 416 | if (is_array($params[$key])) { |
| 417 | return $params[$key]; |
| 418 | } |
| 419 | if ($params[$key] == '') { |
| 420 | return array(); |
| 421 | } |
| 422 | return explode(API_V3_EXTENSION_DELIMITER, $params[$key]); |
| 423 | } |
| 424 | else { |
| 425 | return array(); |
| 426 | } |
| 427 | } |