3
* Copyright Netvibes 2006-2009.
4
* This file is part of Exposition PHP Lib.
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.
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.
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/>.
21
require_once 'Zend/Json.php';
22
require_once 'Zend/Http/Client.php';
25
* Proxy to handle Ajax calls.
30
* HTTP Request User Agent.
32
const USER_AGENT = 'Netvibes Exposition Proxy';
51
* @var Zend_Http_Client
53
private $_client = null;
58
* @var Zend_Http_Response
60
private $_response = null;
67
private $_method = 'GET';
74
private $_type = 'text';
77
* JavaScript callback function.
81
private $_callback = null;
84
* Mime types to be used in responses headers.
88
private $_mimeTypes = array(
90
'html' => 'text/html',
91
'feed' => 'application/json',
92
'json' => 'application/json',
93
'text' => 'text/plain'
101
private $_key = null;
108
private $_cache = null;
115
private $_cachetime = 0;
121
* @param array $options
123
public function __construct($url, $options = array())
126
throw new Exception('No URL has been set');
130
$this->_key = 'ajax_' . md5($this->_url);
132
foreach ($options as $name => $value) {
133
if ($name == 'type') {
134
$this->_type = $value;
135
} else if ($name == 'cachetime') {
136
$this->_cachetime = $value;
141
'useragent' => self::USER_AGENT,
145
$this->_client = new Zend_Http_Client($this->_url, $options);
147
$registry = Zend_Registry::getInstance();
148
$this->_cache = $registry['cache'];
152
* Sets the HTTP client options.
154
* @param array $options
156
public function setHttpClientOptions($options)
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);
163
$this->_client->$setter($value);
169
* Checks if the result is cachable.
171
* @return True if the result is cachable, otherwise false.
173
public function isCachable()
175
return ($this->_method == 'GET' && $this->_response->getStatus() == 200);
179
* Retrieves the response body.
181
* @return The response body
183
public function getBody()
185
$cache = $this->getCache();
187
$body = $cache->load($this->_key . '_body');
191
$this->_response = $this->_client->request();
192
} catch (Zend_Http_Client_Exception $e) {
193
error_log("Http exception via AjaxProxy on " . $this->_url);
196
if ($this->_response->getStatus() == 503) { // Service unavailable
197
header('HTTP/1.1 ' . $this->_response->getStatus() . ' ' . $this->_response->getMessage());
200
$body = $this->_response->getBody();
201
if ($cache && $this->isCachable() && strlen($body) < 200000) {
202
$cache->save($body, $this->_key . '_body');
209
* Sends the Ajax reponse.
211
public function sendResponse()
213
$body = $this->getBody();
214
// experimental, encode utf8 automatically
215
if (!$this->isUtf8Encoded($body)) {
216
$body = utf8_encode($body);
219
$mimeType = $this->_mimeTypes[$this->_type];
220
header("Content-Type: $mimeType");
225
* Detects if a string is UTF-8 encoded.
226
* @see http://us2.php.net/mb_detect_encoding
228
* @param string $string
229
* @return boolean True is the string is UTF-8 encoded, otherwise false.
231
public static function isUtf8Encoded($string)
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
246
* Retrieves the cache instance.
250
public function getCache()
252
return $this->_cache;