| 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 | namespace Civi\API\Subscriber; |
| 13 | |
| 14 | use Civi\API\Events; |
| 15 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 16 | |
| 17 | /** |
| 18 | * Class I18nSubscriber |
| 19 | * @package Civi\API\Subscriber |
| 20 | */ |
| 21 | class I18nSubscriber implements EventSubscriberInterface { |
| 22 | |
| 23 | /** |
| 24 | * Used for rolling back language to its original setting after the api call. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | public $originalLang = []; |
| 29 | |
| 30 | /** |
| 31 | * @return array |
| 32 | */ |
| 33 | public static function getSubscribedEvents() { |
| 34 | return [ |
| 35 | 'civi.api.prepare' => ['onApiPrepare', Events::W_MIDDLE], |
| 36 | 'civi.api.respond' => ['onApiRespond', Events::W_LATE], |
| 37 | ]; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Support multi-lingual requests |
| 42 | * |
| 43 | * @param \Civi\API\Event\Event $event |
| 44 | * API preparation event. |
| 45 | * |
| 46 | * @throws \API_Exception |
| 47 | */ |
| 48 | public function onApiPrepare(\Civi\API\Event\Event $event) { |
| 49 | $apiRequest = $event->getApiRequest(); |
| 50 | |
| 51 | $params = $apiRequest['params']; |
| 52 | if ($apiRequest['version'] < 4) { |
| 53 | $language = $params['options']['language'] ?? $params['option.language'] ?? NULL; |
| 54 | } |
| 55 | else { |
| 56 | $language = $params['language'] ?? NULL; |
| 57 | } |
| 58 | if ($language) { |
| 59 | $this->setLocale($language, $apiRequest['id']); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Reset language to the default. |
| 65 | * |
| 66 | * @param \Civi\API\Event\Event $event |
| 67 | * |
| 68 | * @throws \API_Exception |
| 69 | */ |
| 70 | public function onApiRespond(\Civi\API\Event\Event $event) { |
| 71 | $apiRequest = $event->getApiRequest(); |
| 72 | |
| 73 | if (!empty($this->originalLang[$apiRequest['id']])) { |
| 74 | global $tsLocale; |
| 75 | global $dbLocale; |
| 76 | $tsLocale = $this->originalLang[$apiRequest['id']]['tsLocale']; |
| 77 | $dbLocale = $this->originalLang[$apiRequest['id']]['dbLocale']; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Sets the tsLocale and dbLocale for multi-lingual sites. |
| 83 | * Some code duplication from CRM/Core/BAO/ConfigSetting.php retrieve() |
| 84 | * to avoid regressions from refactoring. |
| 85 | * @param string $lcMessages |
| 86 | * @param int $requestId |
| 87 | * @throws \API_Exception |
| 88 | */ |
| 89 | public function setLocale($lcMessages, $requestId) { |
| 90 | $domain = new \CRM_Core_DAO_Domain(); |
| 91 | $domain->id = \CRM_Core_Config::domainID(); |
| 92 | $domain->find(TRUE); |
| 93 | |
| 94 | // Check if the site is multi-lingual |
| 95 | if ($domain->locales && $lcMessages) { |
| 96 | // Validate language, otherwise a bad dbLocale could probably lead to sql-injection. |
| 97 | if (!array_key_exists($lcMessages, \Civi::settings()->get('languageLimit'))) { |
| 98 | throw new \API_Exception(ts('Language not enabled: %1', [1 => $lcMessages])); |
| 99 | } |
| 100 | |
| 101 | global $dbLocale; |
| 102 | global $tsLocale; |
| 103 | |
| 104 | // Store original value to be restored in $this->onApiRespond |
| 105 | $this->originalLang[$requestId] = [ |
| 106 | 'tsLocale' => $tsLocale, |
| 107 | 'dbLocale' => $dbLocale, |
| 108 | ]; |
| 109 | |
| 110 | // Set suffix for table names - use views if more than one language |
| 111 | $dbLocale = "_{$lcMessages}"; |
| 112 | |
| 113 | // Also set tsLocale - CRM-4041 |
| 114 | $tsLocale = $lcMessages; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | } |