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.3.0.62
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 /**
73 * Class constructor.
74 *
75 * @param string Application name
76 * @param string Application vendor
77 * @param string Application version
78 * @return void
79 * @throws KerioApiException
80 */
81 public function __construct($name, $vendor, $version) {
82 parent::__construct($name, $vendor, $version);
83 }
84
85 /**
86 * @see class/KerioApi::login()
87 */
88 public function login($hostname, $username, $password) {
89 $this->application = 'CLIENT';
90 $response = parent::login($hostname, $username, $password);
91 if ($response['tenant']) {
92 $this->tenant = $response['tenant'];
93 $this->jsonRpc['api'] = sprintf('/%s%s', $response['tenant'], $this->jsonRpc['api']);
94 }
95 return $response;
96 }
97
98 /**
99 * @see class/KerioApi::logout()
100 */
101 public function logout() {
102 $response = parent::logout();
103 $this->jsonRpc['api'] = '/server/data';
104 }
105
106 /**
107 * Get tenant.
108 *
109 * @param void
110 * @return string
111 */
112 public function getTenant() {
113 return $this->tenant;
114 }
115
116 /**
117 * Get headers for PUT request.
118 *
119 * @param string Request body
120 * @return string Request body
121 */
122 protected function getHttpPutRequest($data) {
123 $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']);
124 $this->headers['Accept:'] = '*/*';
125 $this->headers['Content-Type:'] = sprintf('application/k-upload');
126
127 return $data;
128 }
129
130 /**
131 * Put a file to server.
132 *
133 * @param string Absolute path to file
134 * @param integer Reference ID where uploaded file belongs to
135 * @return array Result
136 * @throws KerioApiException
137 */
138 public function uploadFile($filename, $id = null) {
139 $data = @file_get_contents($filename);
140
141 $this->file['filename'] = basename($filename);
142 $this->file['parentId'] = $id;
143 $this->file['lenght'] = strlen($data);
144
145 if ($data) {
146 $this->debug(sprintf('Uploading file %s to item %d', $filename, $id));
147 $json_response = $this->send('PUT', $data);
148 }
149 else {
150 throw new KerioApiException(sprintf('Unable to open file %s', $filename));
151 }
152
153 $response = json_decode($json_response, TRUE);
154 return $response['result'];
155 }
156 }
157