1 <?php
2 /**
3 * This file is part of the kerio-api-php.
4 *
5 * Copyright (c) Kerio Technologies s.r.o.
6 *
7 * For the full copyright and license information, please view
8 * the file license.txt that was distributed with this source code
9 * or visit Developer Zone. (http://www.kerio.com/developers)
10 *
11 * Do not modify this source code.
12 * Any changes may be overwritten by a new version.
13 */
14
15 require_once(dirname(__FILE__) . '/class/KerioApi.php');
16
17 /**
18 * Administration API for Kerio Workspace.
19 * STATUS: In progress, might change in the future
20 *
21 * This class implements product-specific methods and properties and currently is under development.
22 * Class is not intended for stable use yet.
23 * Functionality might not be fully verified, documented, or even supported.
24 *
25 * Please note that changes can be made without further notice.
26 *
27 * Example:
28 * <code>
29 * <?php
30 * require_once(dirname(__FILE__) . '/src/KerioWorkspaceApi.php');
31 *
32 * $api = new KerioWorkspaceApi('Sample application', 'Company Ltd.', '1.0');
33 *
34 * try {
35 * $api->login('workspace.company.tld', 'admin', 'SecretPassword');
36 * $api->sendRequest('...');
37 * $api->logout();
38 * } catch (KerioApiException $error) {
39 * print $error->getMessage();
40 * }
41 * ?>
42 * </code>
43 *
44 * @copyright Copyright © 2012-2012 Kerio Technologies s.r.o.
45 * @license http://www.kerio.com/developers/license/sdk-agreement
46 * @version 1.4.0.234
47 */
48 class KerioWorkspaceApi extends KerioApi {
49
50 /**
51 * Defines default product-specific JSON-RPC settings
52 * @var array
53 */
54 protected $jsonRpc = array(
55 'version' => '2.0',
56 'port' => 4060,
57 'api' => '/admin/api/jsonrpc/'
58 );
59
60 private $file = array();
61
62 /**
63 * Class constructor.
64 *
65 * @param string Application name
66 * @param string Application vendor
67 * @param string Application version
68 * @return void
69 * @throws KerioApiException
70 */
71 public function __construct($name, $vendor, $version) {
72 parent::__construct($name, $vendor, $version);
73 }
74
75 /**
76 * Set component Web Administration.
77 *
78 * @param void
79 * @return void
80 */
81 public function setComponentAdmin() {
82 $this->setJsonRpc('2.0', 4060, '/admin/api/jsonrpc/');
83 }
84
85 /**
86 * Set component Workspace Client.
87 *
88 * @param void
89 * @return void
90 */
91 public function setComponentClient() {
92 $this->setJsonRpc('2.0', 443, '/server/data');
93 }
94
95 /**
96 * Get constants defined by product.
97 *
98 * @param void
99 * @return array Array of constants
100 */
101 public function getConstants() {
102 $response = $this->sendRequest('Server.getProductInfo');
103 return $response['constants'];
104 }
105
106 /**
107 * Get headers for PUT request.
108 *
109 * @param string Request body
110 * @return string Request body
111 */
112 protected function getHttpPutRequest($data) {
113 $this->headers['POST'] = sprintf('%s?method=PutFile&filename=%s&parentId=%d&lenght=%d HTTP/1.1', $this->jsonRpc['api'], rawurlencode($this->file['filename']), $this->file['parentId'], $this->file['lenght']);
114 $this->headers['Accept:'] = '*/*';
115 $this->headers['Content-Type:'] = sprintf('application/k-upload');
116
117 return $data;
118 }
119
120 /**
121 * Put a file to server.
122 *
123 * @param string Absolute path to file
124 * @param integer Reference ID where uploaded file belongs to
125 * @return array Result
126 * @throws KerioApiException
127 */
128 public function uploadFile($filename, $id = null) {
129 $data = @file_get_contents($filename);
130
131 $this->file['filename'] = basename($filename);
132 $this->file['parentId'] = $id;
133 $this->file['lenght'] = strlen($data);
134
135 if ($data) {
136 $this->debug(sprintf('Uploading file %s to item %d', $filename, $id));
137 $json_response = $this->send('PUT', $data);
138 }
139 else {
140 throw new KerioApiException(sprintf('Unable to open file %s', $filename));
141 }
142
143 $response = json_decode($json_response, TRUE);
144 return $response['result'];
145 }
146 }
147