~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/ecosystem/workspace/ecosystem/uwa-server-original/server/lib/Exposition/Proxy.php

  • Committer: parra
  • Date: 2010-03-15 02:39:02 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1433
merged gelee at svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Copyright Netvibes 2006-2009.
 
4
 * This file is part of Exposition PHP Lib.
 
5
 * 
 
6
 * Exposition PHP Lib is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU Lesser General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 * 
 
11
 * Exposition PHP Lib is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU Lesser General Public License for more details.
 
15
 * 
 
16
 * You should have received a copy of the GNU Lesser General Public License
 
17
 * along with Exposition PHP Lib.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
 
 
21
require_once 'Zend/Json.php';
 
22
require_once 'Zend/Http/Client.php';
 
23
 
 
24
/**
 
25
 * Proxy to handle Ajax calls.
 
26
 */
 
27
class Proxy
 
28
{
 
29
    /**
 
30
     * HTTP Request User Agent.
 
31
     */
 
32
    const USER_AGENT = 'Netvibes Exposition Proxy';
 
33
 
 
34
    /**
 
35
     * Response body.
 
36
     *
 
37
     * @var string
 
38
     */
 
39
    public $body = null;
 
40
 
 
41
    /**
 
42
     * Target URL.
 
43
     *
 
44
     * @var string
 
45
     */
 
46
    private $_url = null;
 
47
 
 
48
    /**
 
49
     * HTTP Client.
 
50
     *
 
51
     * @var Zend_Http_Client
 
52
     */
 
53
    private $_client = null;
 
54
 
 
55
    /**
 
56
     * HTTP Response.
 
57
     *
 
58
     * @var Zend_Http_Response
 
59
     */
 
60
    private $_response = null;
 
61
 
 
62
    /**
 
63
     * HTTP method.
 
64
     *
 
65
     * @var string
 
66
     */
 
67
    private $_method = 'GET';
 
68
 
 
69
    /**
 
70
     * Reponse type.
 
71
     *
 
72
     * @var string
 
73
     */
 
74
    private $_type = 'text';
 
75
 
 
76
    /**
 
77
     * JavaScript callback function.
 
78
     *
 
79
     * @var array
 
80
     */
 
81
    private $_callback = null;
 
82
 
 
83
    /**
 
84
     * Mime types to be used in responses headers.
 
85
     *
 
86
     * @var array
 
87
     */
 
88
    private $_mimeTypes = array(
 
89
        'xml'  => 'text/xml',
 
90
        'html' => 'text/html',
 
91
        'feed' => 'application/json',
 
92
        'json' => 'application/json',
 
93
        'text' => 'text/plain'
 
94
    );
 
95
 
 
96
    /**
 
97
     * Cache key.
 
98
     *
 
99
     * @var string
 
100
     */
 
101
    private $_key = null;
 
102
 
 
103
    /**
 
104
     * Cache object.
 
105
     *
 
106
     * @var Zend_Cache
 
107
     */
 
108
    private $_cache = null;
 
109
 
 
110
    /**
 
111
     * Cache lifetime.
 
112
     *
 
113
     * @var integer
 
114
     */
 
115
    private $_cachetime = 0;
 
116
 
 
117
    /**
 
118
     * Constructor.
 
119
     *
 
120
     * @param string $url
 
121
     * @param array  $options
 
122
     */
 
123
    public function __construct($url, $options = array())
 
124
    {
 
125
        if (empty($url)) {
 
126
            throw new Exception('No URL has been set');
 
127
        }
 
128
 
 
129
        $this->_url = $url;
 
130
        $this->_key = 'ajax_' . md5($this->_url);
 
131
 
 
132
        foreach ($options as $name => $value) {
 
133
            if ($name == 'type') {
 
134
                $this->_type = $value;
 
135
            } else if ($name == 'cachetime') {
 
136
                $this->_cachetime = $value;
 
137
            }
 
138
        }
 
139
 
 
140
        $options = array(
 
141
            'useragent'    => self::USER_AGENT,
 
142
            'timeout'      => '60',
 
143
            'maxredirects' => 5
 
144
        );
 
145
        $this->_client = new Zend_Http_Client($this->_url, $options);
 
146
 
 
147
        $registry = Zend_Registry::getInstance();
 
148
        $this->_cache = $registry['cache'];
 
149
    }
 
150
 
 
151
    /**
 
152
     * Sets the HTTP client options.
 
153
     *
 
154
     * @param array $options
 
155
     */
 
156
    public function setHttpClientOptions($options)
 
157
    {
 
158
        foreach ($options as $name => $value) {
 
159
            $setter = 'set' . ucfirst($name);
 
160
            if ($setter == 'setAuth') {
 
161
                $this->_client->$setter($value['username'], $value['password'], Zend_Http_Client::AUTH_BASIC);
 
162
            } else {
 
163
                $this->_client->$setter($value);
 
164
            }
 
165
        }
 
166
    }
 
167
 
 
168
    /**
 
169
     * Checks if the result is cachable.
 
170
     *
 
171
     * @return True if the result is cachable, otherwise false.
 
172
     */
 
173
    public function isCachable()
 
174
    {
 
175
        return ($this->_method == 'GET' && $this->_response->getStatus() == 200);
 
176
    }
 
177
 
 
178
    /**
 
179
     * Retrieves the response body.
 
180
     *
 
181
     * @return The response body
 
182
     */
 
183
    public function getBody()
 
184
    {
 
185
        $cache = $this->getCache();
 
186
        if ($cache) {
 
187
            $body = $cache->load($this->_key . '_body');
 
188
        }
 
189
        if (empty($body)) {
 
190
            try {
 
191
                $this->_response = $this->_client->request();
 
192
            } catch (Zend_Http_Client_Exception $e) {
 
193
                error_log("Http exception via AjaxProxy on " . $this->_url);
 
194
                return null;
 
195
            }
 
196
            if ($this->_response->getStatus() == 503) { // Service unavailable
 
197
                header('HTTP/1.1 ' . $this->_response->getStatus() . ' ' . $this->_response->getMessage());
 
198
                exit;
 
199
            }
 
200
            $body = $this->_response->getBody();
 
201
            if ($cache && $this->isCachable() && strlen($body) < 200000) {
 
202
                $cache->save($body, $this->_key . '_body');
 
203
            }
 
204
        }
 
205
        return $body;
 
206
    }
 
207
 
 
208
    /**
 
209
     * Sends the Ajax reponse.
 
210
     */
 
211
    public function sendResponse()
 
212
    {
 
213
        $body = $this->getBody();
 
214
        // experimental, encode utf8 automatically
 
215
        if (!$this->isUtf8Encoded($body)) {
 
216
            $body = utf8_encode($body);
 
217
        }
 
218
 
 
219
        $mimeType = $this->_mimeTypes[$this->_type];
 
220
        header("Content-Type: $mimeType");
 
221
        echo $body;
 
222
    }
 
223
 
 
224
    /**
 
225
     * Detects if a string is UTF-8 encoded.
 
226
     * @see http://us2.php.net/mb_detect_encoding
 
227
     *
 
228
     * @param string $string
 
229
     * @return boolean True is the string is UTF-8 encoded, otherwise false.
 
230
     */
 
231
    public static function isUtf8Encoded($string)
 
232
    {
 
233
        $match = preg_match('%(?:
 
234
            [\xC2-\xDF][\x80-\xBF]              # non-overlong 2-byte
 
235
            |\xE0[\xA0-\xBF][\x80-\xBF]         # excluding overlongs
 
236
            |[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
 
237
            |\xED[\x80-\x9F][\x80-\xBF]         # excluding surrogates
 
238
            |\xF0[\x90-\xBF][\x80-\xBF]{2}      # planes 1-3
 
239
            |[\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
 
240
            |\xF4[\x80-\x8F][\x80-\xBF]{2}      # plane 16
 
241
            )+%xs', $string);
 
242
        return ($match > 0);
 
243
    }
 
244
 
 
245
    /**
 
246
     * Retrieves the cache instance.
 
247
     *
 
248
     * @return Zend_Cache
 
249
     */
 
250
    public function getCache()
 
251
    {
 
252
        return $this->_cache;
 
253
    }
 
254
}