KerioWorkspaceApi.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. * Administration API for Kerio Workspace.
  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 KerioWorkspaceApi('Sample application', 'Company Ltd.', '1.0');
  31. *
  32. * try {
  33. * $api->login('workspace.company.tld', 'admin', '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 KerioWorkspaceApi 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' => 4060,
  54. 'api' => '/admin/api/jsonrpc/'
  55. );
  56. private $file = array();
  57. /**
  58. * Class constructor.
  59. *
  60. * @param string Application name
  61. * @param string Application vendor
  62. * @param string Application version
  63. * @return void
  64. * @throws KerioApiException
  65. */
  66. public function __construct($name, $vendor, $version) {
  67. parent::__construct($name, $vendor, $version);
  68. }
  69. /**
  70. * Set component Web Administration.
  71. *
  72. * @param void
  73. * @return void
  74. */
  75. public function setComponentAdmin() {
  76. $this->setJsonRpc('2.0', 4060, '/admin/api/jsonrpc/');
  77. }
  78. /**
  79. * Set component Workspace Client.
  80. *
  81. * @param void
  82. * @return void
  83. */
  84. public function setComponentClient() {
  85. $this->setJsonRpc('2.0', 443, '/server/data');
  86. }
  87. /**
  88. * Get constants defined by product.
  89. *
  90. * @param void
  91. * @return array Array of constants
  92. */
  93. public function getConstants() {
  94. $response = $this->sendRequest('Server.getProductInfo');
  95. return $response['constants'];
  96. }
  97. /**
  98. * Get headers for PUT request.
  99. *
  100. * @param string Request body
  101. * @return string Request body
  102. */
  103. protected function getHttpPutRequest($data) {
  104. $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']);
  105. $this->headers['Accept:'] = '*/*';
  106. $this->headers['Content-Type:'] = sprintf('application/k-upload');
  107. return $data;
  108. }
  109. /**
  110. * Put a file to server.
  111. *
  112. * @param string Absolute path to file
  113. * @param integer Reference ID where uploaded file belongs to
  114. * @return array Result
  115. * @throws KerioApiException
  116. */
  117. public function uploadFile($filename, $id = null) {
  118. $data = @file_get_contents($filename);
  119. $this->file['filename'] = basename($filename);
  120. $this->file['parentId'] = $id;
  121. $this->file['lenght'] = strlen($data);
  122. if ($data) {
  123. $this->debug(sprintf('Uploading file %s to item %d', $filename, $id));
  124. $json_response = $this->send('PUT', $data);
  125. }
  126. else {
  127. throw new KerioApiException(sprintf('Unable to open file %s', $filename));
  128. }
  129. $response = json_decode($json_response, TRUE);
  130. return $response['result'];
  131. }
  132. }