3 namespace _CiviVersion_
{
8 * Get the CiviCRM version
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
;
18 trigger_error("Unknown version", E_USER_ERROR
);
25 public static function findCMS() {
26 if (defined('CIVICRM_UF')) {
29 elseif (defined('BACKDROP_VERSION')) {
32 elseif (function_exists('drupal_bootstrap') && version_compare(VERSION
, '6.0', '>=') && version_compare(VERSION
, '7.0', '<')) {
35 elseif (function_exists('drupal_bootstrap') && version_compare(VERSION
, '7.0', '>=') && version_compare(VERSION
, '8.0', '<')) {
38 elseif (defined('ABSPATH') && function_exists('get_bloginfo')) {
41 elseif (defined('DRUPAL_ROOT') && class_exists('Drupal') && version_compare(\Drupal
::VERSION
, '8.0', '>=') && version_compare(\Drupal
::VERSION
, '9.0', '<')) {
45 // guess CMS name from the current path
46 list($cmsType,) = self
::findCMSRootPath();
48 if (!empty($cmsType)) {
55 * Get the CMS root path and CMS name
57 public static function findCMSRootPath() {
60 'wp-includes/version.php',
61 // Future? 'vendor/civicrm/wordpress/civicrm.php' => 'wp',
64 'administrator/components/com_civicrm/civicrm/civicrm-version.php',
68 'modules/system/system.module',
72 'core/core.services.yml',
75 'core/modules/layout/layout.module',
79 $parts = explode('/', str_replace('\\', '/', self
::getSearchDir()));
80 while (!empty($parts)) {
81 $basePath = implode('/', $parts);
83 foreach ($cmsPatterns as $cmsType => $relPaths) {
84 foreach ($relPaths as $relPath) {
85 $matches = glob("$basePath/$relPath");
86 if (!empty($matches)) {
87 return [$cmsType, $basePath];
97 * Get the current path
99 public static function getSearchDir() {
100 if ($_SERVER['SCRIPT_FILENAME']) {
101 return dirname($_SERVER['SCRIPT_FILENAME']);
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') {
109 return getenv('PWD');
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
125 function civicrmVersion() {
127 'version' => \_CiviVersion_\Util
::findVersion(),
128 'cms' => \_CiviVersion_\Util
::findCMS(),