Merge pull request #12132 from eileenmcnaughton/membership_type_data
[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 elseif (defined('ABSPATH') && function_exists('get_bloginfo')) {
39 return 'WordPress';
40 }
41 elseif (defined('DRUPAL_ROOT') && class_exists('Drupal') && version_compare(\Drupal::VERSION, '8.0', '>=') && version_compare(\Drupal::VERSION, '9.0', '<')) {
42 return 'Drupal8';
43 }
44 else {
45 // guess CMS name from the current path
46 list($cmsType,) = self::findCMSRootPath();
47
48 if (!empty($cmsType)) {
49 return $cmsType;
50 }
51 }
52 }
53
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(
67 'modules/system/system.module', // D7
68 ),
69 'Drupal8' => array(
70 'core/core.services.yml', // D8
71 ),
72 'Backdrop' => array(
73 'core/modules/layout/layout.module',
74 ),
75 );
76
77 $parts = explode('/', str_replace('\\', '/', self::getSearchDir()));
78 while (!empty($parts)) {
79 $basePath = implode('/', $parts);
80
81 foreach ($cmsPatterns as $cmsType => $relPaths) {
82 foreach ($relPaths as $relPath) {
83 $matches = glob("$basePath/$relPath");
84 if (!empty($matches)) {
85 return [$cmsType, $basePath];
86 }
87 }
88 }
89
90 array_pop($parts);
91 }
92 }
93
94 /**
95 * Get the current path
96 */
97 public static function getSearchDir() {
98 if ($_SERVER['SCRIPT_FILENAME']) {
99 return dirname($_SERVER['SCRIPT_FILENAME']);
100 }
101 // getenv('PWD') works better with symlinked source trees, but it's
102 // not portable to Windows.
103 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
104 return getcwd();
105 }
106 else {
107 return getenv('PWD');
108 }
109 }
110
111 }
112 }
113
114 namespace {
115 /**
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
120 * @deprecated
121 */
122 function civicrmVersion() {
123 return [
124 'version' => \_CiviVersion_\Util::findVersion(),
125 'cms' => \_CiviVersion_\Util::findCMS(),
126 ];
127 }
128 }