buildQuery() public static method

Generates URL-encoded query string.
public static buildQuery ( array $params, string $argSeparator = '&' ) : string
$params array
$argSeparator string
return string
Beispiel #1
0
/**
 * Gibt eine Url zu einem Artikel zurück.
 *
 * @param int|null $id
 * @param int|null $clang     SprachId des Artikels
 * @param array    $params    Array von Parametern
 * @param string   $separator
 *
 * @return string
 *
 * @package redaxo\structure
 */
function rex_getUrl($id = null, $clang = null, array $params = [], $separator = '&')
{
    $id = (int) $id;
    $clang = (int) $clang;
    // ----- get id
    if ($id == 0) {
        $id = rex::getProperty('article_id');
    }
    // ----- get clang
    // Wenn eine rexExtension vorhanden ist, immer die clang mitgeben!
    // Die rexExtension muss selbst entscheiden was sie damit macht
    if (!rex_clang::exists($clang) && (rex_clang::count() > 1 || rex_extension::isRegistered('URL_REWRITE'))) {
        $clang = rex_clang::getCurrentId();
    }
    // ----- EXTENSION POINT
    $url = rex_extension::registerPoint(new rex_extension_point('URL_REWRITE', '', ['id' => $id, 'clang' => $clang, 'params' => $params, 'separator' => $separator]));
    if ($url == '') {
        if (rex_clang::count() > 1) {
            $clang = $separator . 'clang=' . $clang;
        } else {
            $clang = '';
        }
        $params = rex_string::buildQuery($params, $separator);
        $params = $params ? $separator . $params : '';
        $url = rex_url::frontendController() . '?article_id=' . $id . $clang . $params;
    }
    return $url;
}
Beispiel #2
0
 /**
  * Returns the url to the backend-controller (index.php from backend).
  *
  * @param array $params Params
  * @param bool  $escape Flag whether the argument separator "&" should be escaped (&)
  *
  * @return string
  */
 public static function backendController(array $params = [], $escape = true)
 {
     $query = rex_string::buildQuery($params, $escape ? '&' : '&');
     $query = $query ? '?' . $query : '';
     return self::backend('index.php' . $query);
 }
Beispiel #3
0
 /**
  * Makes a POST request.
  *
  * @param string|array|callable $data  Body data as string or array (POST parameters) or a callback for writing the body
  * @param array                 $files Files array, e.g. `array('myfile' => array('path' => $path, 'type' => 'image/png'))`
  *
  * @return rex_socket_response Response
  *
  * @throws rex_socket_exception
  */
 public function doPost($data = '', array $files = [])
 {
     if (is_array($data) && !empty($files)) {
         $data = function ($stream) use($data, $files) {
             $boundary = '----------6n2Yd9bk2liD6piRHb5xF6';
             $eol = "\r\n";
             fwrite($stream, 'Content-Type: multipart/form-data; boundary=' . $boundary . $eol);
             $dataFormat = '--' . $boundary . $eol . 'Content-Disposition: form-data; name="%s"' . $eol . $eol;
             $fileFormat = '--' . $boundary . $eol . 'Content-Disposition: form-data; name="%s"; filename="%s"' . $eol . 'Content-Type: %s' . $eol . $eol;
             $end = '--' . $boundary . '--' . $eol;
             $length = 0;
             $temp = explode('&', rex_string::buildQuery($data));
             $data = [];
             $partLength = rex_string::size(sprintf($dataFormat, '') . $eol);
             foreach ($temp as $t) {
                 list($key, $value) = array_map('urldecode', explode('=', $t, 2));
                 $data[$key] = $value;
                 $length += $partLength + rex_string::size($key) + rex_string::size($value);
             }
             $partLength = rex_string::size(sprintf($fileFormat, '', '', '') . $eol);
             foreach ($files as $key => $file) {
                 $length += $partLength + rex_string::size($key) + rex_string::size(basename($file['path'])) + rex_string::size($file['type']) + filesize($file['path']);
             }
             $length += rex_string::size($end);
             fwrite($stream, 'Content-Length: ' . $length . $eol . $eol);
             foreach ($data as $key => $value) {
                 fwrite($stream, sprintf($dataFormat, $key) . $value . $eol);
             }
             foreach ($files as $key => $file) {
                 fwrite($stream, sprintf($fileFormat, $key, basename($file['path']), $file['type']));
                 $file = fopen($file['path'], 'rb');
                 while (!feof($file)) {
                     fwrite($stream, fread($file, 1024));
                 }
                 fclose($file);
                 fwrite($stream, $eol);
             }
             fwrite($stream, $end);
         };
     } elseif (!is_callable($data)) {
         if (is_array($data)) {
             $data = rex_string::buildQuery($data);
             $this->addHeader('Content-Type', 'application/x-www-form-urlencoded');
         }
     }
     return $this->doRequest('POST', $data);
 }
Beispiel #4
0
 /**
  * @dataProvider buildQueryProvider
  */
 public function testBuildQuery($expected, $params, $argSeparator = '&')
 {
     $this->assertEquals($expected, rex_string::buildQuery($params, $argSeparator));
 }
Beispiel #5
0
 /**
  * Returns the url to the backend-controller (index.php from backend).
  *
  * @param array $params Params
  * @param bool  $escape Flag whether the argument separator "&" should be escaped (&)
  *
  * @return string
  */
 public static function backendController(array $params = [], $escape = true)
 {
     $query = rex_string::buildQuery($params, $escape ? '&' : '&');
     $query = $query ? '?' . $query : '';
     return self::$pathprovider->backendController() . $query;
 }
Beispiel #6
0
 public static function rewrite($params = [], $yparams = [], $fullpath = false)
 {
     // Url wurde von einer anderen Extension bereits gesetzt
     if (isset($params['subject']) && $params['subject'] != '') {
         return $params['subject'];
     }
     $id = $params['id'];
     $clang = $params['clang'];
     if (isset(self::$paths['redirections'][$id][$clang])) {
         $params['id'] = self::$paths['redirections'][$id][$clang]['id'];
         $params['clang'] = self::$paths['redirections'][$id][$clang]['clang'];
         return self::rewrite($params, $yparams, $fullpath);
     }
     //$url = urldecode($_SERVER['REQUEST_URI']);
     $domain = $_SERVER['HTTP_HOST'];
     $www = 'http://';
     if (self::isHttps()) {
         $www = 'https://';
     }
     $path = '';
     // same domain id check
     if (!$fullpath && isset(self::$paths['paths'][$domain][$id][$clang])) {
         $path = '/' . self::$paths['paths'][$domain][$id][$clang];
         // if(rex::isBackend()) { $path = rex_yrewrite::$paths['paths'][$domain][$id][$clang]; }
     }
     if ($path == '') {
         foreach (self::$paths['paths'] as $i_domain => $i_id) {
             if (isset(self::$paths['paths'][$i_domain][$id][$clang])) {
                 if ($i_domain == 'undefined') {
                     $path = '/' . self::$paths['paths'][$i_domain][$id][$clang];
                 } else {
                     $path = $www . $i_domain . '/' . self::$paths['paths'][$i_domain][$id][$clang];
                 }
                 break;
             }
         }
     }
     // params
     $urlparams = '';
     if (isset($params['params'])) {
         $urlparams = rex_string::buildQuery($params['params'], $params['separator']);
     }
     return $path . ($urlparams ? '?' . $urlparams : '');
 }