SamepageApi.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. require_once(dirname(__FILE__) . '/class/KerioApi.php');
  15. /**
  16. * Samepage.io.
  17. * STATUS: In progress, might change in the future
  18. *
  19. * This class implements product-specific methods and properties and currently is under development.
  20. * Class is not intended for stable use yet.
  21. * Functionality might not be fully verified, documented, or even supported.
  22. *
  23. * Please note that changes can be made without further notice.
  24. *
  25. * Example:
  26. * <code>
  27. * <?php
  28. * require_once(dirname(__FILE__) . '/src/KerioWorkspaceApi.php');
  29. *
  30. * $api = new SamepageApi('Sample Application', 'Company Ltd.', '1.0');
  31. *
  32. * try {
  33. * $api->login('samepage.io', 'user@company.tld', 'SecretPassword');
  34. * $api->sendRequest('...');
  35. * $api->logout();
  36. * } catch (KerioApiException $error) {
  37. * print $error->getMessage();
  38. * }
  39. * ?>
  40. * </code>
  41. *
  42. * @copyright Copyright &copy; 2012-2012 Kerio Technologies s.r.o.
  43. * @license http://www.kerio.com/developers/license/sdk-agreement
  44. * @version 1.4.0.234
  45. */
  46. class SamepageApi extends KerioApi {
  47. /**
  48. * Defines default product-specific JSON-RPC settings
  49. * @var array
  50. */
  51. protected $jsonRpc = array(
  52. 'version' => '2.0',
  53. 'port' => 443,
  54. 'api' => '/server/data'
  55. );
  56. /**
  57. * File info, for upload
  58. * @var array
  59. */
  60. private $file = array();
  61. /**
  62. * Tenant info
  63. * @var string
  64. */
  65. private $tenant = '';
  66. private $endpoint = '';
  67. /**
  68. * Class constructor.
  69. *
  70. * @param string Application name
  71. * @param string Application vendor
  72. * @param string Application version
  73. * @return void
  74. * @throws KerioApiException
  75. */
  76. public function __construct($name, $vendor, $version) {
  77. parent::__construct($name, $vendor, $version);
  78. }
  79. /**
  80. * @see class/KerioApi::login()
  81. */
  82. public function login($hostname, $username, $password) {
  83. $this->application = 'CLIENT';
  84. $response = parent::login($hostname, $username, $password);
  85. if ($response['tenant']) {
  86. $this->setTenant($response['tenant']);
  87. }
  88. return $response;
  89. }
  90. /**
  91. * @see class/KerioApi::logout()
  92. */
  93. public function logout() {
  94. $response = parent::logout();
  95. $this->jsonRpc['api'] = '/server/data';
  96. }
  97. /**
  98. * Get tenant.
  99. *
  100. * @param void
  101. * @return string
  102. */
  103. public function getTenant() {
  104. return $this->tenant;
  105. }
  106. /**
  107. * Set tenant.
  108. *
  109. * @param string
  110. * @return void
  111. */
  112. public function setTenant($tenantId) {
  113. $this->tenant = $tenantId;
  114. $this->jsonRpc['api'] = sprintf('/%s%s', $this->tenant, '/server/data');
  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&filename=%s&parentId=%d&lenght=%d HTTP/1.1', $this->endpoint, rawurlencode($this->file['filename']), $this->file['parentId'], $this->file['lenght']);
  124. $this->headers['Accept:'] = '*/*';
  125. $this->headers['Content-Type:'] = sprintf('application/k-upload');
  126. return $data;
  127. }
  128. /**
  129. * Put a file to server.
  130. *
  131. * @param string Absolute path to file
  132. * @param integer Reference ID where uploaded file belongs to
  133. * @return array Result
  134. * @throws KerioApiException
  135. */
  136. public function uploadFile($filename, $id = null) {
  137. $data = @file_get_contents($filename);
  138. $this->endpoint = sprintf('%s?method=Files.create', $this->jsonRpc['api']);
  139. $this->file['filename'] = basename($filename);
  140. $this->file['parentId'] = $id;
  141. $this->file['lenght'] = strlen($data);
  142. if ($data) {
  143. $this->debug(sprintf('Uploading file %s to item %d', $filename, $id));
  144. $json_response = $this->send('PUT', $data);
  145. }
  146. else {
  147. throw new KerioApiException(sprintf('Unable to open file %s', $filename));
  148. }
  149. $response = json_decode($json_response, TRUE);
  150. return $response['result'];
  151. }
  152. }