/**
     * Generates a URL representing a specified resource in this context.
     *
     * Note that if this method is called from a context other than the one
     * initialized for the modX instance, and the scheme is not specified, an
     * empty string, or abs, the method will force the scheme to full.
     *
     * @access public
     * @param integer $id The id of a resource.
     * @param string $args A query string to append to the generated URL.
     * @param mixed $scheme The scheme indicates in what format the URL is generated.<br>
     * <pre>
     *      -1 : (default value) URL is relative to site_url
     *       0 : see http
     *       1 : see https
     *    full : URL is absolute, prepended with site_url from config
     *     abs : URL is absolute, prepended with base_url from config
     *    http : URL is absolute, forced to http scheme
     *   https : URL is absolute, forced to https scheme
     * </pre>
     * @return string The URL for the resource.
     */
    public function makeUrl($id, $args = '', $scheme = -1) {
        $url = '';
        $found = false;
        if ($id= intval($id)) {
            if (is_object($this->xpdo->context) && $this->get('key') !== $this->xpdo->context->get('key')) {
                $config = array_merge($this->xpdo->_systemConfig, $this->config, $this->xpdo->_userConfig);
                if ($scheme === -1 || $scheme === '' || strpos($scheme, 'abs') !== false) {
                    $scheme= 'full';
                }
            } else {
                $config = $this->xpdo->config;
            }

            if ($config['friendly_urls'] == 1) {
                if ($id == $config['site_start']) {
                    $alias= ($scheme === '' || $scheme === -1) ? $config['base_url'] : '';
                    $found= true;
                } else {
                    $alias= array_search($id, $this->aliasMap);
                    if (!$alias) {
                        $alias= '';
                        $this->xpdo->log(xPDO::LOG_LEVEL_WARN, '`' . $id . '` was requested but no alias was located.');
                    } else {
                        $found= true;
                    }
                }
            } elseif (isset($this->resourceListing["{$id}"])) {
                $found= true;
            }

            if ($found) {
                if (is_array($args)) {
                    $args = modX::toQueryString($args);
                }
                if ($args != '' && $config['friendly_urls'] == 1) {
                    /* add ? to $args if missing */
                    $c= substr($args, 0, 1);
                    if ($c == '&') {
                        $args= '?' . substr($args, 1);
                    } elseif ($c != '?') {
                        $args= '?' . $args;
                    }
                }
                elseif ($args != '') {
                    /* add & to $args if missing */
                    $c= substr($args, 0, 1);
                    if ($c == '?')
                        $args= '&' . substr($args, 1);
                    elseif ($c != '&') $args= '&' . $args;
                }

                if ($config['friendly_urls'] == 1) {
                    $url= $alias . $args;
                } else {
                    $url= $config['request_controller'] . '?' . $config['request_param_id'] . '=' . $id . $args;
                }

                $host= '';
                if ($scheme !== -1 && $scheme !== '') {
                    if ($scheme === 1 || $scheme === 0) {
                        $https_port= $this->getOption('https_port',$config,443);
                        $isSecure= ($_SERVER['SERVER_PORT'] == $https_port || (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS'])=='on')) ? 1 : 0;
                        if ($scheme != $isSecure) {
                            $scheme = $isSecure ? 'http' : 'https';
                        }
                    }
                    switch ($scheme) {
                        case 'full':
                            $host= $config['site_url'];
                            break;
                        case 'abs':
                        case 'absolute':
                            $host= $config['base_url'];
                            break;
                        case 'https':
                        case 'http':
                            $host= $scheme . '://' . $config['http_host'] . $config['base_url'];
                            break;
                    }
                    $url= $host . $url;
                }
            }
        }
        if ($this->xpdo->getDebug() === true) {
            $this->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "modContext[" . $this->get('key') . "]->makeUrl({$id}) = {$url}");
        }
        return $url;
    }
Exemple #2
0
 /**
  * @param $id
  * @param array $options
  * @param array $args
  *
  * @return mixed|string
  */
 public function makeUrl($id, $options = array(), $args = array())
 {
     $scheme = !empty($options['scheme']) ? $options['scheme'] : $this->config['scheme'];
     if (strtolower($scheme) == 'uri' && !empty($options['uri'])) {
         $url = $options['uri'];
         if (!empty($args)) {
             if (is_array($args)) {
                 $args = rtrim(modX::toQueryString($args), '?&');
             }
             $url .= strpos($url, '?') !== false ? '&' : '?';
             $url .= ltrim(trim($args), '?&');
         }
     } else {
         if (!empty($options['context_key'])) {
             $context = $options['context_key'];
         } elseif (!empty($options['context'])) {
             $context = $options['context'];
         } else {
             $context = '';
         }
         if (strtolower($scheme) == 'uri') {
             $scheme = -1;
         }
         $url = $this->modx->makeUrl($id, $context, $args, $scheme, $options);
     }
     return $url;
 }
Exemple #3
0
 /**
  * @param array $parameters
  * @param string $expected
  * @dataProvider providerToQueryString
  */
 public function testToQueryString(array $parameters, $expected)
 {
     $result = modX::toQueryString($parameters);
     $this->assertEquals($expected, $result);
 }
 /**
  * Generates a URL representing a specified resource in this context.
  *
  * Note that if this method is called from a context other than the one
  * initialized for the modX instance, and the scheme is not specified, an
  * empty string, or abs, the method will force the scheme to full.
  *
  * @access public
  * @param integer $id The id of a resource.
  * @param string $args A query string to append to the generated URL.
  * @param mixed $scheme The scheme indicates in what format the URL is generated.<br>
  * <pre>
  *      -1 : (default value) URL is relative to site_url
  *       0 : see http
  *       1 : see https
  *    full : URL is absolute, prepended with site_url from config
  *     abs : URL is absolute, prepended with base_url from config
  *    http : URL is absolute, forced to http scheme
  *   https : URL is absolute, forced to https scheme
  * </pre>
  * @param array $options An array of options for generating the Resource URL.
  * @return string The URL for the resource.
  */
 public function makeUrl($id, $args = '', $scheme = -1, array $options = array())
 {
     $url = '';
     $found = false;
     if ($id = intval($id)) {
         if ($this->config === null) {
             $this->prepare();
         }
         if (is_object($this->xpdo->context) && $this->get('key') !== $this->xpdo->context->get('key')) {
             $config = array_merge($this->xpdo->_systemConfig, $this->config, $this->xpdo->_userConfig, $options);
             if ($scheme === -1 || $scheme === '' || strpos($scheme, 'abs') !== false) {
                 $scheme = 'full';
             }
         } else {
             $config = array_merge($this->xpdo->config, $options);
         }
         if ($config['friendly_urls'] == 1) {
             if ((int) $id === (int) $config['site_start']) {
                 $alias = $scheme === '' || $scheme === -1 ? $config['base_url'] : '';
                 $found = true;
             } else {
                 $alias = $this->getResourceURI($id);
                 if (!$alias) {
                     $alias = '';
                     $this->xpdo->log(xPDO::LOG_LEVEL_WARN, '`' . $id . '` was requested but no alias was located.');
                 } else {
                     $found = true;
                 }
             }
         } elseif (array_keys(array((string) $id), $this->resourceMap, true) !== false) {
             $found = true;
         }
         if ($found) {
             $target = null;
             if (isset($config['use_weblink_target']) && !empty($config['use_weblink_target'])) {
                 if (array_key_exists($id, $this->webLinkMap)) {
                     $target = $this->webLinkMap[$id];
                     if (!empty($target)) {
                         $alias = $target;
                     }
                 }
             }
             $targetHasQS = empty($config['friendly_urls']) || strpos($alias, '?') !== false;
             if (is_array($args)) {
                 $args = modX::toQueryString($args);
             }
             if ($args != '') {
                 if (!$targetHasQS) {
                     /* add ? to $args if missing */
                     $c = substr($args, 0, 1);
                     if ($c == '&') {
                         $args = '?' . substr($args, 1);
                     } elseif ($c != '?') {
                         $args = '?' . $args;
                     }
                 } elseif ($args != '') {
                     /* add & to $args if missing */
                     $c = substr($args, 0, 1);
                     if ($c == '?') {
                         $args = '&' . substr($args, 1);
                     } elseif ($c != '&') {
                         $args = '&' . $args;
                     }
                 }
             }
             if ($config['friendly_urls'] == 1 || $target !== null) {
                 $url = $alias . $args;
             } else {
                 $url = $config['request_controller'] . '?' . $config['request_param_id'] . '=' . $id . $args;
             }
             $host = '';
             if ($target === null && $scheme !== -1 && $scheme !== '') {
                 if ($scheme === 1 || $scheme === 0) {
                     $https_port = $this->getOption('https_port', $config, 443);
                     $isSecure = $_SERVER['SERVER_PORT'] == $https_port || isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 1 : 0;
                     if ($scheme != $isSecure) {
                         $scheme = $isSecure ? 'http' : 'https';
                     }
                 }
                 switch ($scheme) {
                     case 'full':
                         $host = $config['site_url'];
                         break;
                     case 'abs':
                     case 'absolute':
                         $host = $config['base_url'];
                         break;
                     case 'https':
                     case 'http':
                         $host = $scheme . '://' . $config['http_host'] . $config['base_url'];
                         break;
                 }
                 $url = $host . $url;
             }
         }
     }
     if ($this->xpdo->getDebug() === true) {
         $this->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "modContext[" . $this->get('key') . "]->makeUrl({$id}) = {$url}");
     }
     return $url;
 }