Merge pull request #15955 from JMAConsulting/core-1420
[civicrm-core.git] / CRM / Utils / Request.php
index 831947358ed61658aab352e5009adb8c22eca973..4b5d89fbcd06cb15b734dd64c3edb953d02fd173 100644 (file)
@@ -1,34 +1,18 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 5                                                  |
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2018                                |
- +--------------------------------------------------------------------+
- | This file is a part of CiviCRM.                                    |
- |                                                                    |
- | CiviCRM is free software; you can copy, modify, and distribute it  |
- | under the terms of the GNU Affero General Public License           |
- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ | Copyright CiviCRM LLC. All rights reserved.                        |
  |                                                                    |
- | CiviCRM is distributed in the hope that it will be useful, but     |
- | WITHOUT ANY WARRANTY; without even the implied warranty of         |
- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
- | See the GNU Affero General Public License for more details.        |
- |                                                                    |
- | You should have received a copy of the GNU Affero General Public   |
- | License and the CiviCRM Licensing Exception along                  |
- | with this program; if not, contact CiviCRM LLC                     |
- | at info[AT]civicrm[DOT]org. If you have questions about the        |
- | GNU Affero General Public License or the licensing of CiviCRM,     |
- | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
  +--------------------------------------------------------------------+
  */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2018
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 
 /**
@@ -87,7 +71,7 @@ class CRM_Utils_Request {
    *
    * @throws \CRM_Core_Exception
    */
-  public static function retrieve($name, $type, &$store = NULL, $abort = FALSE, $default = NULL, $method = 'REQUEST', $isThrowException = FALSE) {
+  public static function retrieve($name, $type, &$store = NULL, $abort = FALSE, $default = NULL, $method = 'REQUEST', $isThrowException = TRUE) {
 
     $value = NULL;
     switch ($method) {
@@ -116,9 +100,9 @@ class CRM_Utils_Request {
 
     if (!isset($value) && $abort) {
       if ($isThrowException) {
-        throw new CRM_Core_Exception(ts("Could not find valid value for %1", array(1 => $name)));
+        throw new CRM_Core_Exception(ts("Could not find valid value for %1", [1 => $name]));
       }
-      CRM_Core_Error::fatal(ts("Could not find valid value for %1", array(1 => $name)));
+      CRM_Core_Error::fatal(ts("Could not find valid value for %1", [1 => $name]));
     }
 
     if (!isset($value) && $default) {
@@ -126,8 +110,10 @@ class CRM_Utils_Request {
     }
 
     // minor hack for action
-    if ($name == 'action' && is_string($value)) {
-      $value = CRM_Core_Action::resolve($value);
+    if ($name == 'action') {
+      if (!is_numeric($value) && is_string($value)) {
+        $value = CRM_Core_Action::resolve($value);
+      }
     }
 
     if (isset($value) && $store) {
@@ -144,7 +130,7 @@ class CRM_Utils_Request {
    * @param array $method - '$_GET', '$_POST' or '$_REQUEST'.
    *
    * @return mixed
-   *    The value of the variable
+   *   The value of the variable
    */
   protected static function getValue($name, $method) {
     if (isset($method[$name])) {
@@ -181,7 +167,7 @@ class CRM_Utils_Request {
     // http://www.php.net/manual/en/ini.core.php#ini.request-order
     // http://www.php.net/manual/en/ini.core.php#ini.variables-order
 
-    $result = array();
+    $result = [];
     if ($_GET) {
       $result = array_merge($result, $_GET);
     }
@@ -213,10 +199,42 @@ class CRM_Utils_Request {
    *   Where to look for the value - GET|POST|REQUEST
    *
    * @return mixed
+   * @throws \CRM_Core_Exception
    */
   public static function retrieveValue($name, $type, $defaultValue = NULL, $isRequired = FALSE, $method = 'REQUEST') {
     $null = NULL;
     return CRM_Utils_Request::retrieve((string) $name, (string) $type, $null, (bool) $isRequired, $defaultValue, $method, TRUE);
   }
 
+  /**
+   * Retrieve the component from the action attribute of a form.
+   *
+   * Contribution Page forms and Event Management forms detect the value of a
+   * component (and therefore the desired tab key) by reaching into the "action"
+   * attribute of a form and reading the final item of the path. In WordPress,
+   * however, the URL may be urlencoded, and so the URL may need to be decoded
+   * before parsing it.
+   *
+   * @see https://lab.civicrm.org/dev/wordpress/issues/12#note_10699
+   *
+   * @param array $attributes
+   *   The form attributes array.
+   *
+   * @return string
+   *   The desired value.
+   */
+  public static function retrieveComponent($attributes) {
+    $url = CRM_Utils_Array::value('action', $attributes);
+    // Whilst the following is a fallible universal test for urlencoded URLs,
+    // thankfully the "action" URL has a limited and predictable form and
+    // therefore this comparison is sufficient for our purposes.
+    if (rawurlencode(rawurldecode($url)) !== $url) {
+      $value = strtolower(basename(rawurldecode($url)));
+    }
+    else {
+      $value = strtolower(basename($url));
+    }
+    return $value;
+  }
+
 }