/ItellaEmmi.php

https://github.com/janipalsamaki/EMMi-client-PHP-API · PHP · 242 lines · 128 code · 64 blank · 50 comment · 9 complexity · e448311599c8e5c00d2bb766a5409f4f MD5 · raw file

  1. <?php
  2. /**
  3. * ItellaEmmi class
  4. * Used to query the EMMi SOAP service
  5. */
  6. class ItellaEmmi {
  7. protected $client; // SOAP client
  8. protected $service_id; // Used service ID
  9. protected $username; // Username
  10. protected $password; // Password
  11. protected $session_id; // Unique session ID
  12. protected $service_uri; // URL of the service wsdl
  13. protected $wsdl_uri;
  14. protected $file_download_uri; // File download uri
  15. protected $conversion_id; // Used conversion ID
  16. // Fixed path to the file download service
  17. const FILE_PATH = "/file/Download.ashx";
  18. // Some error codes
  19. const FILE_NOT_FOUND = -1; // File or conversion was not found
  20. const FILE_WAIT = -2; // Try again later
  21. /**
  22. * Constructor
  23. * @param string $service_id
  24. * @param string $username
  25. * @param string $password
  26. * @param string $wsdl
  27. */
  28. public function __construct($service_id, $username, $password, $service_uri) {
  29. $this->service_id = $service_id;
  30. $this->username = $username;
  31. $this->password = $password;
  32. $this->service_uri = $service_uri;
  33. $this->wsdl_uri = $service_uri . "?wsdl";
  34. $this->file_download_uri =
  35. substr($this->service_uri, 0, strpos($this->service_uri, "/", strrpos($this->service_uri, "/"))) .
  36. self::FILE_PATH;
  37. // Create the SOAP client class
  38. $this->client = new SoapClient($this->wsdl_uri);
  39. // Authenticate to the server
  40. $result = $this->client->AuthenticateService(
  41. array(
  42. "serviceId" => $this->service_id,
  43. "userName" => $this->username,
  44. "password" => $this->password,
  45. )
  46. );
  47. $this->conversion_id = 0; // original
  48. // store session_id for further use
  49. $this->session_id = $result->AuthenticateServiceResult;
  50. }
  51. public function report() {
  52. var_dump($this);
  53. }
  54. /**
  55. * Set the conversion id to be used
  56. */
  57. public function setConversionId($conversion_id) {
  58. $this->conversion_id = $conversion_id;
  59. }
  60. /**
  61. * Get a file by something that is in any field
  62. * @param string $id
  63. */
  64. public function searchFiles(array $criterias) {
  65. $result = $this->client->SearchFiles(
  66. array(
  67. "sessionId" => $this->session_id,
  68. "serviceId" => $this->service_id,
  69. "criterias" => $criterias,
  70. )
  71. );
  72. if (isset($result->SearchFilesResult->FileElement)) {
  73. $result = $result->SearchFilesResult->FileElement;
  74. } else {
  75. return array();
  76. }
  77. if (!is_array($result)) {
  78. $result = array($result);
  79. }
  80. return $result;
  81. }
  82. /**
  83. * Get given files download uri
  84. */
  85. public function getFileDownloadUri($file) {
  86. $active_version = $file->ActiveVersion;
  87. if (!$file) {
  88. return false;
  89. }
  90. // Build the URL for the file as described in emmi documentation
  91. $data = array(
  92. "a" => "CONVERSION",
  93. "s" => $this->service_id,
  94. "fv" => $active_version->Id,
  95. "coid" => $this->conversion_id,
  96. "sid" => $this->session_id,
  97. );
  98. return $this->file_download_uri . "?" . http_build_query($data);
  99. }
  100. /**
  101. * Download the given file
  102. * @param object $file
  103. * @return error code or file contents
  104. */
  105. public function downloadFile($file) {
  106. $uri = $this->getFileDownloadUri($file);
  107. if (!$uri) {
  108. return false;
  109. }
  110. $name = $file->ActiveVersion->Id . '_' . $this->conversion_id;
  111. $extension = $file->ActiveVersion->Extension;
  112. $filename = file_directory_temp() . "/emmi_" . $name . "." . $extension;
  113. // Some simple caching
  114. if (file_exists($filename))
  115. return $filename;
  116. // User CURL to download the file
  117. // so that we can get HTTP statuses back
  118. // and use them to figure out whats going on
  119. $ch = curl_init();
  120. curl_setopt_array($ch,
  121. array(
  122. CURLOPT_URL => $uri,
  123. CURLOPT_RETURNTRANSFER => 1
  124. )
  125. );
  126. // Get the contents of the file
  127. $contents = curl_exec($ch);
  128. // ...and the return code
  129. $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  130. curl_close($ch);
  131. // ...and act accordingly
  132. switch ($code) {
  133. case 404:
  134. // not found
  135. case 501: // invalid conversion
  136. return self::FILE_NOT_FOUND;
  137. case 591:
  138. // käynnistetty
  139. case 592:
  140. // odottaa
  141. case 593:
  142. // käynnissä
  143. return self::FILE_WAIT;
  144. }
  145. file_put_contents($filename, $contents);
  146. return $filename;
  147. }
  148. /**
  149. * Get conversions IDs
  150. * For this to work there must be atleast one file that was modified within
  151. * a week
  152. */
  153. public function getConversionIDs() {
  154. $files = array();
  155. $result = array();
  156. foreach ($files as $file) {
  157. $conversions = $file->ActiveVersion->Conversions->ConversionOption;
  158. if (!is_array($conversions)) {
  159. $conversions = array($conversions);
  160. }
  161. // collect results
  162. foreach ($conversions as $conv) {
  163. $name = "";
  164. // Loop through the multilingual names and concat them
  165. $lang = $conv->Name->ValueItems->MultilingualValueItem;
  166. if (!is_array($lang)) {
  167. $lang = array($lang);
  168. }
  169. foreach ($lang as $val) {
  170. $name .= $val->Value . ", ";
  171. }
  172. $name = substr($name, 0, -2);
  173. $result[$conv->Id] = $name;
  174. }
  175. }
  176. return $result;
  177. }
  178. };