Commit | Line | Data |
---|---|---|
fce0c43e | 1 | <?php |
2 | ||
8450425a | 3 | namespace _CiviVersion_ { |
fce0c43e | 4 | |
8450425a | 5 | class Util { |
fce0c43e | 6 | |
292931f6 TO |
7 | /** |
8 | * Get the CiviCRM version | |
9 | */ | |
10 | public static function findVersion() { | |
11 | $verFile = implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'xml', 'version.xml']); | |
12 | if (file_exists($verFile)) { | |
13 | $str = file_get_contents($verFile); | |
14 | $xmlObj = simplexml_load_string($str); | |
15 | return (string) $xmlObj->version_no; | |
16 | } | |
fce0c43e | 17 | |
292931f6 TO |
18 | trigger_error("Unknown version", E_USER_ERROR); |
19 | exit(); | |
fce0c43e | 20 | } |
fce0c43e | 21 | |
292931f6 TO |
22 | /** |
23 | * Get the CMS name | |
24 | */ | |
25 | public static function findCMS() { | |
26 | if (defined('CIVICRM_UF')) { | |
27 | return CIVICRM_UF; | |
28 | } | |
27a6a146 TO |
29 | elseif (defined('BACKDROP_VERSION')) { |
30 | return 'Backdrop'; | |
31 | } | |
292931f6 TO |
32 | elseif (function_exists('drupal_bootstrap') && version_compare(VERSION, '7.0', '>=') && version_compare(VERSION, '8.0', '<')) { |
33 | return 'Drupal'; | |
34 | } | |
1fd292a9 TO |
35 | elseif (defined('ABSPATH') && function_exists('get_bloginfo')) { |
36 | return 'WordPress'; | |
37 | } | |
71771613 TO |
38 | elseif (defined('DRUPAL_ROOT') && class_exists('Drupal') && version_compare(\Drupal::VERSION, '8.0', '>=') && version_compare(\Drupal::VERSION, '9.0', '<')) { |
39 | return 'Drupal8'; | |
40 | } | |
292931f6 TO |
41 | else { |
42 | // guess CMS name from the current path | |
43 | list($cmsType,) = self::findCMSRootPath(); | |
44 | ||
45 | if (!empty($cmsType)) { | |
46 | return $cmsType; | |
47 | } | |
fce0c43e | 48 | } |
49 | } | |
fce0c43e | 50 | |
292931f6 TO |
51 | /** |
52 | * Get the CMS root path and CMS name | |
53 | */ | |
54 | public static function findCMSRootPath() { | |
55 | $cmsPatterns = array( | |
56 | 'Wordpress' => array( | |
57 | 'wp-includes/version.php', | |
58 | // Future? 'vendor/civicrm/wordpress/civicrm.php' => 'wp', | |
59 | ), | |
60 | 'Joomla' => array( | |
61 | 'administrator/components/com_civicrm/civicrm/civicrm-version.php', | |
62 | ), | |
63 | 'Drupal' => array( | |
683bf891 SL |
64 | // D7 |
65 | 'modules/system/system.module', | |
292931f6 TO |
66 | ), |
67 | 'Drupal8' => array( | |
683bf891 SL |
68 | // D8 |
69 | 'core/core.services.yml', | |
292931f6 TO |
70 | ), |
71 | 'Backdrop' => array( | |
72 | 'core/modules/layout/layout.module', | |
73 | ), | |
74 | ); | |
fce0c43e | 75 | |
292931f6 TO |
76 | $parts = explode('/', str_replace('\\', '/', self::getSearchDir())); |
77 | while (!empty($parts)) { | |
78 | $basePath = implode('/', $parts); | |
fce0c43e | 79 | |
292931f6 TO |
80 | foreach ($cmsPatterns as $cmsType => $relPaths) { |
81 | foreach ($relPaths as $relPath) { | |
82 | $matches = glob("$basePath/$relPath"); | |
83 | if (!empty($matches)) { | |
84 | return [$cmsType, $basePath]; | |
85 | } | |
fce0c43e | 86 | } |
87 | } | |
292931f6 TO |
88 | |
89 | array_pop($parts); | |
fce0c43e | 90 | } |
292931f6 | 91 | } |
fce0c43e | 92 | |
292931f6 TO |
93 | /** |
94 | * Get the current path | |
95 | */ | |
96 | public static function getSearchDir() { | |
97 | if ($_SERVER['SCRIPT_FILENAME']) { | |
98 | return dirname($_SERVER['SCRIPT_FILENAME']); | |
99 | } | |
100 | // getenv('PWD') works better with symlinked source trees, but it's | |
101 | // not portable to Windows. | |
102 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { | |
103 | return getcwd(); | |
104 | } | |
105 | else { | |
106 | return getenv('PWD'); | |
107 | } | |
fce0c43e | 108 | } |
292931f6 | 109 | |
fce0c43e | 110 | } |
292931f6 | 111 | } |
fce0c43e | 112 | |
292931f6 | 113 | namespace { |
683bf891 | 114 | |
fce0c43e | 115 | /** |
292931f6 TO |
116 | * Get the CiviCRM version. |
117 | * TODO : For now this function is not included in \Civi\Version class so not to break any code | |
118 | * which directly call civicrmVersion(). So those call need to replaced with \Civi\Version::civicrmVersion() | |
119 | * when included in the class | |
c61bca23 | 120 | * @deprecated |
fce0c43e | 121 | */ |
292931f6 TO |
122 | function civicrmVersion() { |
123 | return [ | |
8450425a TO |
124 | 'version' => \_CiviVersion_\Util::findVersion(), |
125 | 'cms' => \_CiviVersion_\Util::findCMS(), | |
292931f6 | 126 | ]; |
fce0c43e | 127 | } |
683bf891 | 128 | |
fce0c43e | 129 | } |