1<?php
2/**
3 * Extend the base URL class to allow for use with the URL parameter scheme
4 * used in Horde's smartmobile framework.
5 *
6 * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
7 *
8 * See the enclosed file COPYING for license information (LGPL). If you
9 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
10 *
11 * @author   Michael Slusarz <slusarz@horde.org>
12 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
13 * @category Horde
14 * @package  Core
15 */
16
17/**
18 * Extend the base URL class to allow for use with the URL parameter scheme
19 * used in Horde's smartmobile framework.
20 *
21 * @author   Michael Slusarz <slusarz@horde.org>
22 * @category Horde
23 * @package  Core
24 */
25class Horde_Core_Smartmobile_Url extends Horde_Url
26{
27    /**
28     * The URL used as the base for the smartmobile anchor.
29     *
30     * @var Horde_Url
31     */
32    protected $_baseUrl;
33
34    /**
35     * Constructor.
36     *
37     * @param Horde_Url $url   The basic URL.
38     * @param boolean $raw     Whether to output the URL in the raw URL format
39     *                         or HTML-encoded.
40     */
41    public function __construct($url = null, $raw = null)
42    {
43        if (is_null($url)) {
44            $url = new Horde_Url();
45        }
46        if (!($url instanceof Horde_Url)) {
47            throw new InvalidArgumentException('First argument to Horde_Core_Smartmobile_Url constructor must be a Horde_Url object');
48        }
49
50        $query = '';
51
52        /* Smartmobile URLs carry around query information in fragment, so
53         * copy any information found in the incoming URL. */
54        if (strlen($url->anchor)) {
55            $anchor = parse_url($url->anchor);
56            if (isset($anchor['query'])) {
57                $this->anchor = $anchor['path'];
58                $query = '?' . $anchor['query'];
59            } else {
60                $this->anchor = $url->anchor;
61            }
62            $url->anchor = '';
63        }
64
65        $this->_baseUrl = $url;
66        parent::__construct($query, $raw);
67    }
68
69    /**
70     * Creates the full URL string.
71     *
72     * @param boolean $raw   Whether to output the URL in the raw URL format
73     *                       or HTML-encoded.
74     * @param boolean $full  Output the full URL?
75     *
76     * @return string  The string representation of this object.
77     */
78    public function toString($raw = false, $full = true)
79    {
80        if ($this->toStringCallback || !strlen($this->anchor)) {
81            $baseUrl = $this->_baseUrl->copy();
82            $baseUrl->parameters = array_merge($baseUrl->parameters,
83                                               $this->parameters);
84            if (strlen($this->pathInfo)) {
85                $baseUrl->pathInfo = $this->pathInfo;
86            }
87            return $baseUrl->toString($raw, $full);
88        }
89
90        $url = $this->_baseUrl->toString($raw, $full);
91
92        if (strlen($this->pathInfo)) {
93            $url = rtrim($url, '/');
94            $url .= '/' . $this->pathInfo;
95        }
96
97        if ($this->anchor) {
98            $url .= '#' . ($raw ? $this->anchor : rawurlencode($this->anchor));
99        }
100
101        if ($params = $this->_getParameters()) {
102            $url .= '?' . implode($raw ? '&' : '&amp;', $params);
103        }
104
105        return strval($url);
106    }
107
108}
109