| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | CiviCRM version 5 | |
| 6 | +--------------------------------------------------------------------+ |
| 7 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 8 | +--------------------------------------------------------------------+ |
| 9 | | This file is a part of CiviCRM. | |
| 10 | | | |
| 11 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 12 | | under the terms of the GNU Affero General Public License | |
| 13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 14 | | | |
| 15 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 18 | | See the GNU Affero General Public License for more details. | |
| 19 | | | |
| 20 | | You should have received a copy of the GNU Affero General Public | |
| 21 | | License and the CiviCRM Licensing Exception along | |
| 22 | | with this program; if not, contact CiviCRM LLC | |
| 23 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 24 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 26 | +--------------------------------------------------------------------+ |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * |
| 31 | * @package CRM |
| 32 | * @copyright CiviCRM LLC (c) 2004-2018 |
| 33 | */ |
| 34 | class CRM_Utils_GeocodeProvider { |
| 35 | |
| 36 | /** |
| 37 | * Caches the provider class name. Disables geocoding when set to FALSE. |
| 38 | * |
| 39 | * @var string|bool |
| 40 | */ |
| 41 | private static $providerClassName; |
| 42 | |
| 43 | /** |
| 44 | * Instantiate a geocode object of the system-configured type. |
| 45 | * |
| 46 | * @return CRM_Utils_Geocode |
| 47 | * @throws CRM_Core_Exception |
| 48 | */ |
| 49 | public static function getConfiguredProvider() { |
| 50 | $geoCodeClassName = self::getUsableClassName(); |
| 51 | if ($geoCodeClassName === FALSE) { |
| 52 | throw new CRM_Core_Exception('No valid geocoding provider enabled'); |
| 53 | } |
| 54 | return new $geoCodeClassName(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get the name of the geocoding class if enabled. |
| 59 | * |
| 60 | * This retrieves the geocoding class, checking it can be accessed. |
| 61 | * Checks are done to mitigate the possibility it has been configured |
| 62 | * and then the file has been removed. |
| 63 | * |
| 64 | * @return string|bool |
| 65 | * Class name if usable, else false. |
| 66 | */ |
| 67 | public static function getUsableClassName() { |
| 68 | if (self::$providerClassName === NULL) { |
| 69 | $provider = Civi::settings()->get('geoProvider'); |
| 70 | if (!class_exists($provider)) { |
| 71 | if (class_exists('CRM_Utils_Geocode_' . $provider)) { |
| 72 | $provider = 'CRM_Utils_Geocode_' . $provider; |
| 73 | } |
| 74 | else { |
| 75 | if (strlen($provider)) { |
| 76 | Civi::log() |
| 77 | ->error('Configured geocoder has been removed from the system', ['geocode_class' => $provider]); |
| 78 | } |
| 79 | $provider = FALSE; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Ideally geocoding providers would be required to implement an interface |
| 84 | // or extend a base class. While we identify and implement a geocoding |
| 85 | // abstraction library (rather than continue to roll our own), we settle for |
| 86 | // this check. |
| 87 | if (!method_exists($provider, 'format')) { |
| 88 | Civi::log()->error('Configured geocoder is invalid, must provide a format method', ['geocode_class' => $provider]); |
| 89 | $provider = FALSE; |
| 90 | } |
| 91 | |
| 92 | self::$providerClassName = $provider; |
| 93 | } |
| 94 | |
| 95 | return self::$providerClassName; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Disable GeoProvider within a session. |
| 100 | * |
| 101 | * This disables geocoding by causing getUsableClassName() to bail out. |
| 102 | */ |
| 103 | public static function disableForSession() { |
| 104 | self::$providerClassName = FALSE; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Reset geoprovider (after it has been disabled). |
| 109 | */ |
| 110 | public static function reset() { |
| 111 | self::$providerClassName = NULL; |
| 112 | self::getUsableClassName(); |
| 113 | } |
| 114 | |
| 115 | } |