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