Merge pull request #15357 from civicrm/5.18
[civicrm-core.git] / CRM / Utils / System / DrupalBase.php
index e2b5a295227bc8b1176bdb75f3a1994f9f39e3ae..115efaebebbec9b58c00eb0b4aef02dc65fa102f 100644 (file)
@@ -40,10 +40,10 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
 
   /**
    * Does this CMS / UF support a CMS specific logging mechanism?
-   * @todo - we should think about offering up logging mechanisms in a way that is also extensible by extensions
    * @var bool
+   * @todo - we should think about offering up logging mechanisms in a way that is also extensible by extensions
    */
-  var $supports_UF_Logging = TRUE;
+  public $supports_UF_Logging = TRUE;
 
   /**
    */
@@ -73,10 +73,10 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
       $filesURL = $baseURL . "sites/default/files/civicrm/";
     }
 
-    return array(
+    return [
       'url' => $filesURL,
       'path' => CRM_Utils_File::baseFilePath(),
-    );
+    ];
   }
 
   /**
@@ -85,7 +85,7 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
   public function getDefaultSiteSettings($dir) {
     $config = CRM_Core_Config::singleton();
     $siteName = $siteRoot = NULL;
-    $matches = array();
+    $matches = [];
     if (preg_match(
       '|/sites/([\w\.\-\_]+)/|',
       $config->templateCompileDir,
@@ -101,7 +101,7 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
       }
     }
     $url = $config->userFrameworkBaseURL;
-    return array($url, $siteName, $siteRoot);
+    return [$url, $siteName, $siteRoot];
   }
 
   /**
@@ -117,26 +117,24 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     $internal = FALSE;
     $base = CRM_Core_Config::singleton()->resourceBase;
     global $base_url;
+    // Strip query string
+    $q = strpos($url, '?');
+    $url_path = $q ? substr($url, 0, $q) : $url;
     // Handle absolute urls
     // compares $url (which is some unknown/untrusted value from a third-party dev) to the CMS's base url (which is independent of civi's url)
     // to see if the url is within our drupal dir, if it is we are able to treated it as an internal url
-    if (strpos($url, $base_url) === 0) {
-      $file = trim(str_replace($base_url, '', $url), '/');
+    if (strpos($url_path, $base_url) === 0) {
+      $file = trim(str_replace($base_url, '', $url_path), '/');
       // CRM-18130: Custom CSS URL not working if aliased or rewritten
-      if (file_exists(DRUPAL_ROOT . $file)) {
+      if (file_exists(DRUPAL_ROOT . '/' . $file)) {
         $url = $file;
         $internal = TRUE;
       }
     }
     // Handle relative urls that are within the CiviCRM module directory
-    elseif (strpos($url, $base) === 0) {
+    elseif (strpos($url_path, $base) === 0) {
       $internal = TRUE;
-      $url = $this->appendCoreDirectoryToResourceBase(dirname(drupal_get_path('module', 'civicrm')) . '/') . trim(substr($url, strlen($base)), '/');
-    }
-    // Strip query string
-    $q = strpos($url, '?');
-    if ($q && $internal) {
-      $url = substr($url, 0, $q);
+      $url = $this->appendCoreDirectoryToResourceBase(dirname(drupal_get_path('module', 'civicrm')) . '/') . trim(substr($url_path, strlen($base)), '/');
     }
     return $internal;
   }
@@ -268,10 +266,10 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
   public function getUserRecordUrl($contactID) {
     $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
     if (CRM_Core_Session::singleton()
-        ->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm(array(
-          'cms:administer users',
-          'cms:view user account',
-        ))
+      ->get('userID') == $contactID || CRM_Core_Permission::checkAnyPerm([
+        'cms:administer users',
+        'cms:view user account',
+      ])
     ) {
       return $this->url('user/' . $uid);
     };
@@ -289,7 +287,7 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
    */
   public function logger($message) {
     if (CRM_Core_Config::singleton()->userFrameworkLogging && function_exists('watchdog')) {
-      watchdog('civicrm', '%message', array('%message' => $message), NULL, WATCHDOG_DEBUG);
+      watchdog('civicrm', '%message', ['%message' => $message], NULL, WATCHDOG_DEBUG);
     }
   }
 
@@ -311,7 +309,7 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
    * @inheritDoc
    */
   public function getModules() {
-    $result = array();
+    $result = [];
     $q = db_query('SELECT name, status FROM {system} WHERE type = \'module\' AND schema_version <> -1');
     foreach ($q as $row) {
       $result[] = new CRM_Core_Module('drupal.' . $row->name, ($row->status == 1) ? TRUE : FALSE);
@@ -333,7 +331,7 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     $roles = user_roles(FALSE, $oldPerm);
     if (!empty($roles)) {
       foreach (array_keys($roles) as $rid) {
-        user_role_revoke_permissions($rid, array($oldPerm));
+        user_role_revoke_permissions($rid, [$oldPerm]);
         user_role_grant_permissions($rid, $newPerms);
       }
     }
@@ -494,7 +492,7 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
    *
    * FIXME: Document values accepted/required by $params
    */
-  public function userLoginFinalize($params = array()) {
+  public function userLoginFinalize($params = []) {
     user_login_finalize($params);
   }
 
@@ -634,7 +632,7 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
       include $confdir . "/sites.php";
     }
     else {
-      $sites = array();
+      $sites = [];
     }
 
     $uri = explode('/', $phpSelf);
@@ -665,4 +663,24 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base {
     return (!empty($language->language)) ? $language->language : $language;
   }
 
+  /**
+   * Is a front end page being accessed.
+   *
+   * Generally this would be a contribution form or other public page as opposed to a backoffice page (like contact edit).
+   *
+   * See https://github.com/civicrm/civicrm-drupal/pull/546/files
+   *
+   * @return bool
+   */
+  public function isFrontEndPage() {
+    $path = CRM_Utils_System::getUrlPath();
+
+    // Get the menu for above URL.
+    $item = CRM_Core_Menu::get($path);
+    if (!empty(CRM_Utils_Array::value('is_public', $item))) {
+      return TRUE;
+    }
+    return FALSE;
+  }
+
 }