Merge pull request #14326 from civicrm/5.14
[civicrm-core.git] / CRM / Utils / System.php
index 7a4c663098a531c0017eba887d2331908e8855f3..cdb29038368502df0ef4b20601d1cbae6ef3e136 100644 (file)
  * @method static int getLoggedInUfID() Get current logged in user id.
  * @method static setHttpHeader(string $name, string $value) Set http header.
  * @method static array synchronizeUsers() Create CRM contacts for all existing CMS users.
+ * @method static appendCoreResources(\Civi\Core\Event\GenericHookEvent $e) Callback for hook_civicrm_coreResourceList.
+ * @method static alterAssetUrl(\Civi\Core\Event\GenericHookEvent $e) Callback for hook_civicrm_getAssetUrl.
  */
 class CRM_Utils_System {
 
-  static $_callbacks = NULL;
+  public static $_callbacks = NULL;
 
   /**
    * @var string
    *   Page title
    */
-  static $title = '';
+  public static $title = '';
 
   /**
    * Access methods in the appropriate CMS class
@@ -71,7 +73,7 @@ class CRM_Utils_System {
    */
   public static function __callStatic($name, $arguments) {
     $userSystem = CRM_Core_Config::singleton()->userSystem;
-    return call_user_func_array(array($userSystem, $name), $arguments);
+    return call_user_func_array([$userSystem, $name], $arguments);
   }
 
   /**
@@ -103,8 +105,7 @@ class CRM_Utils_System {
       }
     }
 
-    return
-      self::url(
+    return self::url(
         $path,
         CRM_Utils_System::getLinksUrl($urlVar, $includeReset, $includeForce),
         $absolute
@@ -133,9 +134,9 @@ class CRM_Utils_System {
    */
   public static function getLinksUrl($urlVar, $includeReset = FALSE, $includeForce = TRUE, $skipUFVar = TRUE) {
     // Sort out query string to prevent messy urls
-    $querystring = array();
-    $qs = array();
-    $arrays = array();
+    $querystring = [];
+    $qs = [];
+    $arrays = [];
 
     if (!empty($_SERVER['QUERY_STRING'])) {
       $qs = explode('&', str_replace('&', '&', $_SERVER['QUERY_STRING']));
@@ -215,8 +216,7 @@ class CRM_Utils_System {
     $print = FALSE,
     $maintenance = FALSE
   ) {
-    $config = &CRM_Core_Config::singleton();
-    return $config->userSystem->theme($content, $print, $maintenance);
+    return CRM_Core_Config::singleton()->userSystem->theme($content, $print, $maintenance);
   }
 
   /**
@@ -452,10 +452,10 @@ class CRM_Utils_System {
 
     // If we are in a json context, respond appropriately
     if ($context['output'] === 'json') {
-      CRM_Core_Page_AJAX::returnJsonResponse(array(
+      CRM_Core_Page_AJAX::returnJsonResponse([
         'status' => 'redirect',
         'userContext' => $url,
-      ));
+      ]);
     }
 
     self::setHttpHeader('Location', $url);
@@ -627,7 +627,7 @@ class CRM_Utils_System {
       list($userID, $ufID, $randomNumber) = $result;
       if ($userID && $ufID) {
         $config = CRM_Core_Config::singleton();
-        $config->userSystem->setUserSession(array($userID, $ufID));
+        $config->userSystem->setUserSession([$userID, $ufID]);
       }
       else {
         return self::authenticateAbort(
@@ -688,7 +688,6 @@ class CRM_Utils_System {
     return $config->userSystem->setMessage($message);
   }
 
-
   /**
    * Determine whether a value is null-ish.
    *
@@ -749,7 +748,7 @@ class CRM_Utils_System {
     $s = preg_replace('/<th[^>]*>([^<]+)<\/th>/', "<info>\\1</info>", $s);
     $s = preg_replace('/<td[^>]*>([^<]+)<\/td>/', "<info>\\1</info>", $s);
     $vTmp = preg_split('/(<h2>[^<]+<\/h2>)/', $s, -1, PREG_SPLIT_DELIM_CAPTURE);
-    $vModules = array();
+    $vModules = [];
     for ($i = 1; $i < count($vTmp); $i++) {
       if (preg_match('/<h2>([^<]+)<\/h2>/', $vTmp[$i], $vMat)) {
         $vName = trim($vMat[1]);
@@ -760,7 +759,7 @@ class CRM_Utils_System {
           $vPat2 = "/$vPat\s*$vPat/";
           // 3cols
           if (preg_match($vPat3, $vOne, $vMat)) {
-            $vModules[$vName][trim($vMat[1])] = array(trim($vMat[2]), trim($vMat[3]));
+            $vModules[$vName][trim($vMat[1])] = [trim($vMat[2]), trim($vMat[3])];
             // 2cols
           }
           elseif (preg_match($vPat2, $vOne, $vMat)) {
@@ -907,7 +906,7 @@ class CRM_Utils_System {
    */
   public static function validCallback($callback) {
     if (self::$_callbacks === NULL) {
-      self::$_callbacks = array();
+      self::$_callbacks = [];
     }
 
     if (!array_key_exists($callback, self::$_callbacks)) {
@@ -1006,7 +1005,7 @@ class CRM_Utils_System {
 
     if ($abort) {
       CRM_Core_Error::fatal(ts('This feature requires PHP Version %1 or greater',
-        array(1 => $ver)
+        [1 => $ver]
       ));
     }
     return FALSE;
@@ -1082,7 +1081,7 @@ class CRM_Utils_System {
 
     if (!$version) {
       $verFile = implode(DIRECTORY_SEPARATOR,
-        array(dirname(__FILE__), '..', '..', 'xml', 'version.xml')
+        [dirname(__FILE__), '..', '..', 'xml', 'version.xml']
       );
       if (file_exists($verFile)) {
         $str = file_get_contents($verFile);
@@ -1131,7 +1130,7 @@ class CRM_Utils_System {
 
     // emulate get all headers
     // http://www.php.net/manual/en/function.getallheaders.php#66335
-    $headers = array();
+    $headers = [];
     foreach ($_SERVER as $name => $value) {
       if (substr($name, 0, 5) == 'HTTP_') {
         $headers[str_replace(' ',
@@ -1168,8 +1167,7 @@ class CRM_Utils_System {
    * this function, please go and change the code in the install script as well.
    */
   public static function isSSL() {
-    return
-      (isset($_SERVER['HTTPS']) &&
+    return (isset($_SERVER['HTTPS']) &&
         !empty($_SERVER['HTTPS']) &&
         strtolower($_SERVER['HTTPS']) != 'off') ? TRUE : FALSE;
   }
@@ -1317,14 +1315,14 @@ class CRM_Utils_System {
       return $docBaseURL . str_replace(' ', '+', $page);
     }
     else {
-      $params = array(
+      $params = [
         'page' => $page,
         'URLonly' => $URLonly,
         'text' => $text,
         'title' => $title,
         'style' => $style,
         'resource' => $resource,
-      );
+      ];
       return self::docURL($params);
     }
   }
@@ -1388,7 +1386,7 @@ class CRM_Utils_System {
    * @return mixed
    */
   public static function formatDocUrl($url) {
-    return preg_replace('#^user/#', 'user/en/stable/', $url);
+    return preg_replace('#^(user|sysadmin|dev)/#', '\1/en/stable/', $url);
   }
 
   /**
@@ -1410,7 +1408,7 @@ class CRM_Utils_System {
     }
 
     $userSystem = CRM_Core_Config::singleton()->userSystem;
-    if (is_callable(array($userSystem, 'onCiviExit'))) {
+    if (is_callable([$userSystem, 'onCiviExit'])) {
       $userSystem->onCiviExit();
     }
     exit($status);
@@ -1469,9 +1467,9 @@ class CRM_Utils_System {
    * @param bool $throwError
    * @param string $realPath
    */
-  public static function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) {
+  public static function loadBootStrap($params = [], $loadUser = TRUE, $throwError = TRUE, $realPath = NULL) {
     if (!is_array($params)) {
-      $params = array();
+      $params = [];
     }
     $config = CRM_Core_Config::singleton();
     $result = $config->userSystem->loadBootStrap($params, $loadUser, $throwError, $realPath);
@@ -1615,8 +1613,7 @@ class CRM_Utils_System {
     $addLanguagePart = TRUE,
     $removeLanguagePart = FALSE
   ) {
-    $config = &CRM_Core_Config::singleton();
-    return $config->userSystem->languageNegotiationURL($url, $addLanguagePart, $removeLanguagePart);
+    return CRM_Core_Config::singleton()->userSystem->languageNegotiationURL($url, $addLanguagePart, $removeLanguagePart);
   }
 
   /**
@@ -1669,7 +1666,7 @@ class CRM_Utils_System {
    *   include path.
    */
   public static function listIncludeFiles($relpath) {
-    $file_list = array();
+    $file_list = [];
     $inc_dirs = explode(PATH_SEPARATOR, get_include_path());
     foreach ($inc_dirs as $inc_dir) {
       $target_dir = $inc_dir . DIRECTORY_SEPARATOR . $relpath;
@@ -1707,9 +1704,9 @@ class CRM_Utils_System {
    *   List of plugins, where the plugin name is both the key and the value of
    *   each element.
    */
-  public static function getPluginList($relpath, $fext = '.php', $skipList = array()) {
+  public static function getPluginList($relpath, $fext = '.php', $skipList = []) {
     $fext_len = strlen($fext);
-    $plugins = array();
+    $plugins = [];
     $inc_files = CRM_Utils_System::listIncludeFiles($relpath);
     foreach ($inc_files as $inc_file) {
       if (substr($inc_file, 0 - $fext_len) == $fext) {
@@ -1751,7 +1748,7 @@ class CRM_Utils_System {
     }
     else {
       $config = CRM_Core_Config::singleton();
-      $vars = array(
+      $vars = [
         '{ver}' => CRM_Utils_System::version(),
         '{uf}' => $config->userFramework,
         '{php}' => phpversion(),
@@ -1759,7 +1756,7 @@ class CRM_Utils_System {
         '{baseUrl}' => $config->userFrameworkBaseURL,
         '{lang}' => $config->lcMessages,
         '{co}' => $config->defaultContactCountry,
-      );
+      ];
       return strtr($url, array_map('urlencode', $vars));
     }
   }
@@ -1776,7 +1773,7 @@ class CRM_Utils_System {
     if (!$sid) {
       $config = CRM_Core_Config::singleton();
       $sid = md5('sid_' . (defined('CIVICRM_SITE_KEY') ? CIVICRM_SITE_KEY : '') . '_' . $config->userFrameworkBaseURL);
-      civicrm_api3('Setting', 'create', array('domain_id' => 'all', 'site_id' => $sid));
+      civicrm_api3('Setting', 'create', ['domain_id' => 'all', 'site_id' => $sid]);
     }
     return $sid;
   }
@@ -1848,9 +1845,9 @@ class CRM_Utils_System {
       return NULL;
     }
 
-    $link = array();
+    $link = [];
     CRM_Utils_Hook::crudLink($crudLinkSpec, $bao, $link);
-    if (empty($link) && is_callable(array($bao, 'createDefaultCrudLink'))) {
+    if (empty($link) && is_callable([$bao, 'createDefaultCrudLink'])) {
       $link = $bao->createDefaultCrudLink($crudLinkSpec);
     }