civicrm-version.php 3.54 KB
Newer Older
1 2
<?php

3
namespace _CiviVersion_ {
4

5
  class Util {
6

7 8 9 10 11 12 13 14 15 16
    /**
     * 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;
      }
17

18 19
      trigger_error("Unknown version", E_USER_ERROR);
      exit();
20 21
    }

22 23 24 25 26 27 28
    /**
     * Get the CMS name
     */
    public static function findCMS() {
      if (defined('CIVICRM_UF')) {
        return CIVICRM_UF;
      }
29 30 31
      elseif (defined('BACKDROP_VERSION')) {
        return 'Backdrop';
      }
32 33 34 35 36 37
      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';
      }
38 39 40
      elseif (defined('ABSPATH') && function_exists('get_bloginfo')) {
        return 'WordPress';
      }
41 42 43
      elseif (defined('DRUPAL_ROOT') && class_exists('Drupal') && version_compare(\Drupal::VERSION, '8.0', '>=') && version_compare(\Drupal::VERSION, '9.0', '<')) {
        return 'Drupal8';
      }
44 45 46 47 48 49 50
      else {
        // guess CMS name from the current path
        list($cmsType,) = self::findCMSRootPath();

        if (!empty($cmsType)) {
          return $cmsType;
        }
51 52 53
      }
    }

54 55 56 57 58 59 60 61 62 63 64 65 66
    /**
     * 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(
67 68
          // D7
          'modules/system/system.module',
69 70
        ),
        'Drupal8' => array(
71 72
          // D8
          'core/core.services.yml',
73 74 75 76 77
        ),
        'Backdrop' => array(
          'core/modules/layout/layout.module',
        ),
      );
78

79 80 81
      $parts = explode('/', str_replace('\\', '/', self::getSearchDir()));
      while (!empty($parts)) {
        $basePath = implode('/', $parts);
82

83 84 85 86 87 88
        foreach ($cmsPatterns as $cmsType => $relPaths) {
          foreach ($relPaths as $relPath) {
            $matches = glob("$basePath/$relPath");
            if (!empty($matches)) {
              return [$cmsType, $basePath];
            }
89 90
          }
        }
91 92

        array_pop($parts);
93
      }
94
    }
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    /**
     * Get the current path
     */
    public static function getSearchDir() {
      if ($_SERVER['SCRIPT_FILENAME']) {
        return dirname($_SERVER['SCRIPT_FILENAME']);
      }
      // 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');
      }
111
    }
112

113
  }
114
}
115

116
namespace {
117

118
  /**
119 120 121 122
   * 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
123
   * @deprecated
124
   */
125 126
  function civicrmVersion() {
    return [
127 128
      'version' => \_CiviVersion_\Util::findVersion(),
      'cms' => \_CiviVersion_\Util::findCMS(),
129
    ];
130
  }
131

132
}