-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSamepageApi.php
170 lines (152 loc) · 4.15 KB
/
SamepageApi.php
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
<?php
/**
* This file is part of the kerio-api-php.
*
* Copyright (c) Kerio Technologies s.r.o.
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code
* or visit Developer Zone. (http://www.kerio.com/developers)
*
* Do not modify this source code.
* Any changes may be overwritten by a new version.
*/
require_once(dirname(__FILE__) . '/class/KerioApi.php');
/**
* Samepage.io.
* STATUS: In progress, might change in the future
*
* This class implements product-specific methods and properties and currently is under development.
* Class is not intended for stable use yet.
* Functionality might not be fully verified, documented, or even supported.
*
* Please note that changes can be made without further notice.
*
* Example:
* <code>
* <?php
* require_once(dirname(__FILE__) . '/src/KerioWorkspaceApi.php');
*
* $api = new SamepageApi('Sample Application', 'Company Ltd.', '1.0');
*
* try {
* $api->login('samepage.io', '[email protected]', 'SecretPassword');
* $api->sendRequest('...');
* $api->logout();
* } catch (KerioApiException $error) {
* print $error->getMessage();
* }
* ?>
* </code>
*
* @copyright Copyright © 2012-2012 Kerio Technologies s.r.o.
* @license http://www.kerio.com/developers/license/sdk-agreement
* @version 1.4.0.234
*/
class SamepageApi extends KerioApi {
/**
* Defines default product-specific JSON-RPC settings
* @var array
*/
protected $jsonRpc = array(
'version' => '2.0',
'port' => 443,
'api' => '/server/data'
);
/**
* File info, for upload
* @var array
*/
private $file = array();
/**
* Tenant info
* @var string
*/
private $tenant = '';
private $endpoint = '';
/**
* Class constructor.
*
* @param string Application name
* @param string Application vendor
* @param string Application version
* @return void
* @throws KerioApiException
*/
public function __construct($name, $vendor, $version) {
parent::__construct($name, $vendor, $version);
}
/**
* @see class/KerioApi::login()
*/
public function login($hostname, $username, $password) {
$this->application = 'CLIENT';
$response = parent::login($hostname, $username, $password);
if ($response['tenant']) {
$this->setTenant($response['tenant']);
}
return $response;
}
/**
* @see class/KerioApi::logout()
*/
public function logout() {
$response = parent::logout();
$this->jsonRpc['api'] = '/server/data';
}
/**
* Get tenant.
*
* @param void
* @return string
*/
public function getTenant() {
return $this->tenant;
}
/**
* Set tenant.
*
* @param string
* @return void
*/
public function setTenant($tenantId) {
$this->tenant = $tenantId;
$this->jsonRpc['api'] = sprintf('/%s%s', $this->tenant, '/server/data');
}
/**
* Get headers for PUT request.
*
* @param string Request body
* @return string Request body
*/
protected function getHttpPutRequest($data) {
$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']);
$this->headers['Accept:'] = '*/*';
$this->headers['Content-Type:'] = sprintf('application/k-upload');
return $data;
}
/**
* Put a file to server.
*
* @param string Absolute path to file
* @param integer Reference ID where uploaded file belongs to
* @return array Result
* @throws KerioApiException
*/
public function uploadFile($filename, $id = null) {
$data = @file_get_contents($filename);
$this->endpoint = sprintf('%s?method=Files.create', $this->jsonRpc['api']);
$this->file['filename'] = basename($filename);
$this->file['parentId'] = $id;
$this->file['lenght'] = strlen($data);
if ($data) {
$this->debug(sprintf('Uploading file %s to item %d', $filename, $id));
$json_response = $this->send('PUT', $data);
}
else {
throw new KerioApiException(sprintf('Unable to open file %s', $filename));
}
$response = json_decode($json_response, TRUE);
return $response['result'];
}
}