opendrive.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. # Debug
  3. $DEBUG = false;
  4. if ($_ENV['DEBUG']) {
  5. $DEBUG = true;
  6. }
  7. # On-disk config
  8. require_once '.secrets';
  9. # Config
  10. $API_BASE = 'https://dev.opendrive.com/api/v1';
  11. $API_VERSION = 10;
  12. # Open the DB connection
  13. function dbOpen() {
  14. global $DB_STRING;
  15. global $DB_USER;
  16. global $DB_PASSWD;
  17. try {
  18. $dbh = new PDO($DB_STRING, $DB_USER, $DB_PASSWD);
  19. } catch (PDOException $e) {
  20. die('DB error: ' . $e->getMessage() . "\n");
  21. }
  22. return $dbh;
  23. }
  24. # POST the provided data to OpenDrive
  25. function curlPost($url, $data) {
  26. global $API_BASE;
  27. global $CA_PATH;
  28. global $DEBUG;
  29. # Setup cURL
  30. $ch = curl_init($API_BASE . $url);
  31. curl_setopt_array($ch, array(
  32. CURLOPT_VERBOSE => $DEBUG,
  33. CURLOPT_CAINFO => $CA_PATH,
  34. CURLOPT_POST => true,
  35. CURLOPT_RETURNTRANSFER => true,
  36. CURLOPT_HTTPHEADER => array(
  37. 'Content-Type: application/json'
  38. ),
  39. CURLOPT_POSTFIELDS => json_encode($data)
  40. ));
  41. # Send the request
  42. $response = curl_exec($ch);
  43. # Check for errors
  44. if ($response === FALSE){
  45. die(curl_error($ch));
  46. }
  47. # Return
  48. $responseData = json_decode($response, TRUE);
  49. return($responseData);
  50. }
  51. # GET from OpenDrive
  52. function curlGet($url) {
  53. global $API_BASE;
  54. global $CA_PATH;
  55. global $DEBUG;
  56. # Setup cURL
  57. $ch = curl_init($API_BASE . $url);
  58. curl_setopt_array($ch, array(
  59. CURLOPT_VERBOSE => $DEBUG,
  60. CURLOPT_CAINFO => $CA_PATH,
  61. CURLOPT_RETURNTRANSFER => true
  62. ));
  63. # Send the request
  64. $response = curl_exec($ch);
  65. # Check for errors
  66. if ($response === FALSE){
  67. die(curl_error($ch));
  68. }
  69. # Return
  70. $responseData = json_decode($response, TRUE);
  71. return($responseData);
  72. }
  73. # Login to OD and return the SessionID
  74. function login() {
  75. global $API_VERSION;
  76. global $OD_USER;
  77. global $OD_PASSWD;
  78. $data = array(
  79. 'username' => $OD_USER,
  80. 'passwd' => $OD_PASSWD,
  81. 'version' => $API_VERISON
  82. );
  83. $response = curlPost('/session/login.json', $data);
  84. # Check the result
  85. if ($response['SessionID']) {
  86. return $response['SessionID'];
  87. }
  88. return false;
  89. }
  90. # Logout of OpenDrive
  91. function logout($session) {
  92. $data = array(
  93. 'session_id' => $session
  94. );
  95. $response = curlPost('/session/logout.json', $data);
  96. # Check the result
  97. if ($response['result'] == 'true') {
  98. return true;
  99. }
  100. return false;
  101. }
  102. # Return the ID of a folder
  103. function folderID($session, $path) {
  104. $data = array(
  105. 'session_id' => $session,
  106. 'path' => $path
  107. );
  108. $response = curlPost('/folder/idbypath.json', $data);
  109. # Check the result
  110. if ($response['FolderId']) {
  111. return($response['FolderId']);
  112. }
  113. return false;
  114. }
  115. # Create a folder
  116. function mkFolder($session, $path) {
  117. # Find the parent ID (if any)
  118. $path = preg_replace('/\/+$/', '', $path);
  119. $path = preg_replace('/^\/+/', '', $path);
  120. $folder = basename($path);
  121. $parent = dirname($path);
  122. $parentID = 0;
  123. if ($parent != '.') {
  124. $parentID = folderID($session, $parent);
  125. if ($parentID === false) {
  126. return false;
  127. }
  128. }
  129. # POST
  130. $data = array(
  131. 'session_id' => $session,
  132. 'folder_name' => $folder,
  133. 'folder_sub_parent' => $parentID,
  134. 'folder_is_public' => 1
  135. );
  136. $response = curlPost('/folder.json', $data);
  137. # Check the result
  138. if ($response['FolderId']) {
  139. return($response['FolderId']);
  140. }
  141. return false;
  142. }
  143. # Trash a folder
  144. function rmFolder($session, $path) {
  145. $id = folderID($session, $path);
  146. if (!$id) {
  147. return false;
  148. }
  149. $data = array(
  150. 'session_id' => $session,
  151. 'folder_id' => $id
  152. );
  153. $response = curlPost('/folder/trash.json', $data);
  154. # Check the result
  155. if ($response['DirUpdateTime']) {
  156. return true;
  157. }
  158. return false;
  159. }
  160. # Return the ID of a file
  161. function fileID($session, $path) {
  162. $data = array(
  163. 'session_id' => $session,
  164. 'path' => $path
  165. );
  166. $response = curlPost('/file/idbypath.json', $data);
  167. # Check the result
  168. if ($response['FileId']) {
  169. return($response['FileId']);
  170. }
  171. return false;
  172. }
  173. # Return all file metadata
  174. function fileInfo($session, $path) {
  175. $id = fileID($session, $path);
  176. if (!$id) {
  177. return false;
  178. }
  179. # GET
  180. $response = curlGet('/file/info.json/' . $session . '/' . $id);
  181. # Check the result
  182. if ($response['FileId']) {
  183. return($response);
  184. }
  185. return false;
  186. }
  187. ?>