1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
<?php
/**
* Copyright 2014-2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package OpenXchange
*/
/**
* Horde_OpenXchange_Base is the base class for HTTP API requests to an
* Open-Xchange server.
*
* @link http://oxpedia.org/wiki/index.php?title=HTTP_API
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2014-2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package OpenXchange
*/
abstract class Horde_OpenXchange_Base
{
/**
* Return private resources.
*
* @see listResources()
*/
const RESOURCE_PRIVATE = 'private';
/**
* Return public resources.
*
* @see listResources()
*/
const RESOURCE_PUBLIC = 'public';
/**
* The folder category.
*
* @var string
*/
protected $_folderType;
/**
* Any parameters.
*
* @var array
*/
protected $_params;
/**
* HTTP client
*
* @var Horde_Http_Client
*/
protected $_client;
/**
* Base URI of the API endpoint.
*
* @var string
*/
protected $_uri;
/**
* All cookies to sent with OX requests.
*
* @var array
*/
protected $_cookies = array();
/**
* The current session ID.
*
* @var string
*/
protected $_session;
/**
* Constructor.
*
* @param array $params List of optional parameters:
* - client: (Horde_Http_Client) An HTTP client.
* - endpoint: (string) The URI of the OX API
* endpoint.
* - user: (string) Authentication user.
* - password: (string) Authentication password.
*/
public function __construct(array $params = array())
{
if (isset($params['client'])) {
$this->_client = $params['client'];
unset($params['client']);
} else {
$this->_client = new Horde_Http_Client(array('request.timeout' => 10));
}
$this->_params = array_merge(
array('endpoint' => 'http://localhost/ajax'),
$params
);
$this->_uri = $this->_params['endpoint'];
}
/**
* Logs a user in.
*
* @param string $user A user name.
* @param string $password A password.
*
* @throws Horde_OpenXchange_Exception.
*/
public function login($user, $password)
{
$this->_params['user'] = $user;
$this->_params['password'] = $password;
$this->_login();
}
/**
* Logs the current user out.
*
* @throws Horde_OpenXchange_Exception.
*/
public function logout()
{
if (!isset($this->_session)) {
return;
}
$response = $this->_request(
'GET',
'login',
array('action' => 'logout'),
array('session' => $this->_session)
);
unset($this->_session);
$this->_cookies = array();
}
/**
* Returns information about a system user.
*
* @param integer $id A user ID.
*
* @return array User information hash.
* @throws Horde_OpenXchange_Exception.
*/
public function getUser($id)
{
$user = $this->_request(
'GET',
'user',
array(
'action' => 'get',
'session' => $this->_session,
'id' => $id,
)
);
return $user['data'];
}
/**
* Returns information about a system group.
*
* @param integer $id A group ID.
*
* @return array Group information hash.
* @throws Horde_OpenXchange_Exception.
*/
public function getGroup($id)
{
$group = $this->_request(
'GET',
'group',
array(
'action' => 'get',
'session' => $this->_session,
'id' => $id,
)
);
return $group;
}
/**
* Returns user configuration.
*
* @param string $config A configuration namespace.
*
* @return mixed Configuration contents.
* @throws Horde_OpenXchange_Exception.
*/
public function getConfig($config)
{
$content = $this->_request(
'GET',
'config/' . $config,
array('session' => $this->_session)
);
switch ($content['data']) {
case 'true':
return true;
case 'false':
return false;
case 'null':
return null;
default:
return $content['data'];
}
}
/**
* Returns a list of visible groupware resources.
*
* @param string $type An resource type, one of the RESOURCE_* constants.
*
* @return array List of resources with resource IDs as keys and
* information hashes as values.
* @throws Horde_OpenXchange_Exception.
*/
public function listResources($type = self::RESOURCE_PRIVATE)
{
$this->_login();
$response = $this->_request(
'PUT',
'folders',
array(
'action' => 'allVisible',
'session' => $this->_session,
'content_type' => $this->_folderType,
'columns' => '1,300,306,308'
)
);
$resources = $users = $groups = array();
foreach ($response['data'][$type] as $resource) {
$info = array(
'label' => $resource[1],
'default' => $resource[3],
);
foreach ($resource[2] as $perm) {
// http://oxpedia.org/wiki/index.php?title=HTTP_API#PermissionFlags
$permission = 0;
if ($perm['bits'] & 127) {
$permission |= Horde_Perms::SHOW;
}
if ((($perm['bits'] >> 7) & 127) > 1) {
$permission |= Horde_Perms::READ;
}
if ((($perm['bits'] >> 14) & 127) > 1) {
$permission |= Horde_Perms::EDIT;
}
if ((($perm['bits'] >> 21) & 127) > 1) {
$permission |= Horde_Perms::DELETE;
}
if ($perm['group']) {
if (!isset($groups[$perm['entity']])) {
$group = $this->getGroup($perm['entity']);
$groups[$perm['entity']] = $group['name'];
}
$info['permission']['group'][$groups[$perm['entity']]] = $perm['bits'];
$info['hordePermission']['group'][$groups[$perm['entity']]] = $permission;
} else {
if (!isset($users[$perm['entity']])) {
$user = $this->getUser($perm['entity']);
$users[$perm['entity']] = $user['login_info'];
}
$info['permission']['user'][$users[$perm['entity']]] = $perm['bits'];
$info['hordePermission']['user'][$users[$perm['entity']]] = $permission;
}
}
$resources[$resource[0]] = $info;
}
return $resources;
}
/**
* Logs a user in, if necessary.
*
* @throws Horde_OpenXchange_Exception.
*/
protected function _login()
{
if (isset($this->_session)) {
return;
}
if (!isset($this->_params['user']) ||
!isset($this->_params['password'])) {
throw new LogicException('User name or password missing');
}
$response = $this->_request(
'POST',
'login',
array('action' => 'login'),
array(
'name' => $this->_params['user'],
'password' => $this->_params['password']
)
);
$this->_session = $response['session'];
}
/**
* Sends a request and parses the response.
*
* @param string $method A HTTP request method (uppercase).
* @param string $namespace An API namespace.
* @param array $params URL parameters.
* @param array|string $data Request data.
*
* @return array The decoded result data or null if no data has been
* returned but the request was still successful.
* @throws Horde_OpenXchange_Exception.
*/
protected function _request($method, $namespace, $params, $data = array())
{
$uri = new Horde_Url($this->_uri . '/' . $namespace, true);
try {
$headers = array();
if (isset($this->_cookies)) {
$headers['Cookie'] = implode('; ', $this->_cookies);
}
if ($method == 'GET') {
$params = array_merge($params, $data);
$data = null;
}
$response = $this->_client->request(
$method,
(string)$uri->add($params),
$data,
$headers
);
if ($cookies = $response->getHeader('set-cookie')) {
if (!is_array($cookies)) {
$cookies = array($cookies);
}
foreach ($cookies as $cookie) {
$cookie = preg_split('/;\s*/', $cookie);
for ($i = 1, $c = count($cookie); $i < $c; $i++) {
list($key, $value) = explode('=', $cookie[$i]);
if ($key == 'Expires') {
$expire = new Horde_Date($value);
if ($expire->before(time())) {
continue 2;
}
break;
}
}
$this->_cookies[] = $cookie[0];
}
}
$body = $response->getBody();
$data = json_decode($body, true);
if (!$data) {
if ($response->code == 200) {
return;
}
throw new Horde_OpenXchange_Exception($body);
}
if (isset($data['error'])) {
$e = new Horde_OpenXchange_Exception($data['error']);
$e->details = $data;
throw $e;
}
return $data;
} catch (Horde_Http_Exception $e) {
throw new Horde_OpenXchange_Exception($e);
}
}
}
|