static $version;
if (!$version) {
- $verFile = implode(DIRECTORY_SEPARATOR,
- array(dirname(__FILE__), '..', '..', 'xml', 'version.xml')
- );
- if (file_exists($verFile)) {
- $str = file_get_contents($verFile);
- $xmlObj = simplexml_load_string($str);
- $version = (string) $xmlObj->version_no;
- }
+ $verFile = implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), '..', '..', 'civicrm-version.php']);
+ require_once $verFile;
+ $version = \Civi\Version::findVersion();
// pattern check
if (!CRM_Utils_System::isVersionFormatValid($version)) {
--- /dev/null
+<?php
+
+namespace Civi;
+
+class Version {
+
+ /**
+ * Get the CiviCRM version
+ */
+ public static function findVersion() {
+ $verFile = implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'xml', 'version.xml']);
+ if (file_exists($verFile)) {
+ $str = file_get_contents($verFile);
+ $xmlObj = simplexml_load_string($str);
+ return (string) $xmlObj->version_no;
+ }
+
+ trigger_error("Unknown version", E_USER_ERROR);
+ exit();
+ }
+
+ /**
+ * Get the CMS name
+ */
+ public static function findCMS() {
+ if (defined('CIVICRM_UF')) {
+ return CIVICRM_UF;
+ }
+ elseif (function_exists('drupal_bootstrap') && version_compare(VERSION, '6.0', '>=') && version_compare(VERSION, '7.0', '<')) {
+ return 'Drupal6';
+ }
+ elseif (function_exists('drupal_bootstrap') && version_compare(VERSION, '7.0', '>=') && version_compare(VERSION, '8.0', '<')) {
+ return 'Drupal';
+ }
+ else {
+ // guess CMS name from the current path
+ list($cmsType,) = self::findCMSRootPath();
+
+ if (!empty($cmsType)) {
+ return $cmsType;
+ }
+ }
+ }
+
+ /**
+ * Get the CMS root path and CMS name
+ */
+ public static function findCMSRootPath() {
+ $cmsPatterns = array(
+ 'Wordpress' => array(
+ 'wp-includes/version.php',
+ // Future? 'vendor/civicrm/wordpress/civicrm.php' => 'wp',
+ ),
+ 'Joomla' => array(
+ 'administrator/components/com_civicrm/civicrm/civicrm-version.php',
+ ),
+ 'Drupal' => array(
+ 'modules/system/system.module', // D7
+ ),
+ 'Drupal8' => array(
+ 'core/core.services.yml', // D8
+ ),
+ 'Backdrop' => array(
+ 'core/modules/layout/layout.module',
+ ),
+ );
+
+ $parts = explode('/', str_replace('\\', '/', self::getSearchDir()));
+ while (!empty($parts)) {
+ $basePath = implode('/', $parts);
+
+ foreach ($cmsPatterns as $cmsType => $relPaths) {
+ foreach ($relPaths as $relPath) {
+ $matches = glob("$basePath/$relPath");
+ if (!empty($matches)) {
+ return [$cmsType, $basePath];
+ }
+ }
+ }
+
+ array_pop($parts);
+ }
+ }
+
+ /**
+ * Get the current path
+ */
+ public static function getSearchDir() {
+ // getenv('PWD') works better with symlinked source trees, but it's
+ // not portable to Windows.
+ if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
+ return getcwd();
+ }
+ else {
+ return getenv('PWD');
+ }
+ }
+
+}
+
+/**
+ * Get the CiviCRM version.
+ * TODO : For now this function is not included in \Civi\Version class so not to break any code
+ * which directly call civicrmVersion(). So those call need to replaced with \Civi\Version::civicrmVersion()
+ * when included in the class
+ */
+function civicrmVersion() {
+ return [
+ 'version' => \Civi\Version::findVersion(),
+ 'cms' => \Civi\Version::findCMS(),
+ ];
+}