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