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 * Samepage.io.
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 SamepageApi('Sample Application', 'Company Ltd.', '1.0');
33 *
34 * try {
35 * $api->login('samepage.io', 'user@company.tld', '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 SamepageApi 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' => 443,
57 'api' => '/server/data'
58 );
59
60 /**
61 * File info, for upload
62 * @var array
63 */
64 private $file = array();
65
66 /**
67 * Tenant info
68 * @var string
69 */
70 private $tenant = '';
71
72 private $endpoint = '';
73
74 /**
75 * Class constructor.
76 *
77 * @param string Application name
78 * @param string Application vendor
79 * @param string Application version
80 * @return void
81 * @throws KerioApiException
82 */
83 public function __construct($name, $vendor, $version) {
84 parent::__construct($name, $vendor, $version);
85 }
86
87 /**
88 * @see class/KerioApi::login()
89 */
90 public function login($hostname, $username, $password) {
91 $this->application = 'CLIENT';
92 $response = parent::login($hostname, $username, $password);
93 if ($response['tenant']) {
94 $this->setTenant($response['tenant']);
95 }
96 return $response;
97 }
98
99 /**
100 * @see class/KerioApi::logout()
101 */
102 public function logout() {
103 $response = parent::logout();
104 $this->jsonRpc['api'] = '/server/data';
105 }
106
107 /**
108 * Get tenant.
109 *
110 * @param void
111 * @return string
112 */
113 public function getTenant() {
114 return $this->tenant;
115 }
116
117 /**
118 * Set tenant.
119 *
120 * @param string
121 * @return void
122 */
123 public function setTenant($tenantId) {
124 $this->tenant = $tenantId;
125 $this->jsonRpc['api'] = sprintf('/%s%s', $this->tenant, '/server/data');
126 }
127
128 /**
129 * Get headers for PUT request.
130 *
131 * @param string Request body
132 * @return string Request body
133 */
134 protected function getHttpPutRequest($data) {
135 $this->headers['POST'] = sprintf('%s&filename=%s&parentId=%d&lenght=%d HTTP/1.1', $this->endpoint, rawurlencode($this->file['filename']), $this->file['parentId'], $this->file['lenght']);
136 $this->headers['Accept:'] = '*/*';
137 $this->headers['Content-Type:'] = sprintf('application/k-upload');
138
139 return $data;
140 }
141
142 /**
143 * Put a file to server.
144 *
145 * @param string Absolute path to file
146 * @param integer Reference ID where uploaded file belongs to
147 * @return array Result
148 * @throws KerioApiException
149 */
150 public function uploadFile($filename, $id = null) {
151 $data = @file_get_contents($filename);
152
153 $this->endpoint = sprintf('%s?method=Files.create', $this->jsonRpc['api']);
154
155 $this->file['filename'] = basename($filename);
156 $this->file['parentId'] = $id;
157 $this->file['lenght'] = strlen($data);
158
159 if ($data) {
160 $this->debug(sprintf('Uploading file %s to item %d', $filename, $id));
161 $json_response = $this->send('PUT', $data);
162 }
163 else {
164 throw new KerioApiException(sprintf('Unable to open file %s', $filename));
165 }
166
167 $response = json_decode($json_response, TRUE);
168 return $response['result'];
169 }
170 }
171