civicrm-version.php - Fix CMS detection in pre-booted Backdrop
[civicrm-core.git] / civicrm-version.php
1 <?php
2
3 namespace _CiviVersion_ {
4
5 class Util {
6
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 }
17
18 trigger_error("Unknown version", E_USER_ERROR);
19 exit();
20 }
21
22 /**
23 * Get the CMS name
24 */
25 public static function findCMS() {
26 if (defined('CIVICRM_UF')) {
27 return CIVICRM_UF;
28 }
29 elseif (defined('BACKDROP_VERSION')) {
30 return 'Backdrop';
31 }
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 }
38 else {
39 // guess CMS name from the current path
40 list($cmsType,) = self::findCMSRootPath();
41
42 if (!empty($cmsType)) {
43 return $cmsType;
44 }
45 }
46 }
47
48 /**
49 * Get the CMS root path and CMS name
50 */
51 public static function findCMSRootPath() {
52 $cmsPatterns = array(
53 'Wordpress' => array(
54 'wp-includes/version.php',
55 // Future? 'vendor/civicrm/wordpress/civicrm.php' => 'wp',
56 ),
57 'Joomla' => array(
58 'administrator/components/com_civicrm/civicrm/civicrm-version.php',
59 ),
60 'Drupal' => array(
61 'modules/system/system.module', // D7
62 ),
63 'Drupal8' => array(
64 'core/core.services.yml', // D8
65 ),
66 'Backdrop' => array(
67 'core/modules/layout/layout.module',
68 ),
69 );
70
71 $parts = explode('/', str_replace('\\', '/', self::getSearchDir()));
72 while (!empty($parts)) {
73 $basePath = implode('/', $parts);
74
75 foreach ($cmsPatterns as $cmsType => $relPaths) {
76 foreach ($relPaths as $relPath) {
77 $matches = glob("$basePath/$relPath");
78 if (!empty($matches)) {
79 return [$cmsType, $basePath];
80 }
81 }
82 }
83
84 array_pop($parts);
85 }
86 }
87
88 /**
89 * Get the current path
90 */
91 public static function getSearchDir() {
92 if ($_SERVER['SCRIPT_FILENAME']) {
93 return dirname($_SERVER['SCRIPT_FILENAME']);
94 }
95 // getenv('PWD') works better with symlinked source trees, but it's
96 // not portable to Windows.
97 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
98 return getcwd();
99 }
100 else {
101 return getenv('PWD');
102 }
103 }
104
105 }
106 }
107
108 namespace {
109 /**
110 * Get the CiviCRM version.
111 * TODO : For now this function is not included in \Civi\Version class so not to break any code
112 * which directly call civicrmVersion(). So those call need to replaced with \Civi\Version::civicrmVersion()
113 * when included in the class
114 */
115 function civicrmVersion() {
116 return [
117 'version' => \_CiviVersion_\Util::findVersion(),
118 'cms' => \_CiviVersion_\Util::findCMS(),
119 ];
120 }
121 }