opendrive.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. # Cleanup a path for better matching at OpenDrive
  25. function pathCleanup($path) {
  26. $path = preg_replace('/\/+$/', '', $path);
  27. $path = preg_replace('/^\/+/', '', $path);
  28. return $path;
  29. }
  30. # Return the clean parent path of a provided path
  31. function parentPath($path) {
  32. return dirname(pathCleanup($path));
  33. }
  34. # Return the clean base name of a provided path
  35. function basePath($path) {
  36. return basename(pathCleanup($path));
  37. }
  38. # POST the provided data to OpenDrive
  39. function curlPost($url, $data) {
  40. global $API_BASE;
  41. global $CA_PATH;
  42. global $DEBUG;
  43. # Setup cURL
  44. $ch = curl_init($API_BASE . $url);
  45. curl_setopt_array($ch, array(
  46. CURLOPT_VERBOSE => $DEBUG,
  47. CURLOPT_CAINFO => $CA_PATH,
  48. CURLOPT_POST => true,
  49. CURLOPT_RETURNTRANSFER => true,
  50. CURLOPT_HTTPHEADER => array(
  51. 'Content-Type: application/json'
  52. ),
  53. CURLOPT_POSTFIELDS => json_encode($data)
  54. ));
  55. # Send the request
  56. $response = curl_exec($ch);
  57. # Check for errors
  58. if ($response === FALSE){
  59. die(curl_error($ch));
  60. }
  61. # Return
  62. $responseData = json_decode($response, TRUE);
  63. return($responseData);
  64. }
  65. # GET from OpenDrive
  66. function curlGet($url) {
  67. global $API_BASE;
  68. global $CA_PATH;
  69. global $DEBUG;
  70. # Setup cURL
  71. $ch = curl_init($API_BASE . $url);
  72. curl_setopt_array($ch, array(
  73. CURLOPT_VERBOSE => $DEBUG,
  74. CURLOPT_CAINFO => $CA_PATH,
  75. CURLOPT_RETURNTRANSFER => true
  76. ));
  77. # Send the request
  78. $response = curl_exec($ch);
  79. # Check for errors
  80. if ($response === FALSE){
  81. die(curl_error($ch));
  82. }
  83. # Return
  84. $responseData = json_decode($response, TRUE);
  85. return($responseData);
  86. }
  87. # Login to OD and return the SessionID
  88. function login() {
  89. global $API_VERSION;
  90. global $OD_USER;
  91. global $OD_PASSWD;
  92. $data = array(
  93. 'username' => $OD_USER,
  94. 'passwd' => $OD_PASSWD,
  95. 'version' => $API_VERISON
  96. );
  97. $response = curlPost('/session/login.json', $data);
  98. # Check the result
  99. if ($response['SessionID']) {
  100. return $response['SessionID'];
  101. }
  102. return false;
  103. }
  104. # Logout of OpenDrive
  105. function logout($session) {
  106. $data = array(
  107. 'session_id' => $session
  108. );
  109. $response = curlPost('/session/logout.json', $data);
  110. # Check the result
  111. if ($response['result'] == 'true') {
  112. return true;
  113. }
  114. return false;
  115. }
  116. # Return the ID of a folder
  117. function folderID($session, $path) {
  118. $data = array(
  119. 'session_id' => $session,
  120. 'path' => $path
  121. );
  122. $response = curlPost('/folder/idbypath.json', $data);
  123. # Check the result
  124. if ($response['FolderId']) {
  125. return($response['FolderId']);
  126. }
  127. return false;
  128. }
  129. # Find the parent ID for a given path
  130. function parentID($session, $path) {
  131. $parent = parentPath($path);
  132. $parentID = 0;
  133. if ($parent != '.') {
  134. $parentID = folderID($session, $parent);
  135. }
  136. if ($parentID === false) {
  137. return false;
  138. }
  139. return $parentID;
  140. }
  141. # Create a folder
  142. function mkFolder($session, $path) {
  143. # Find the parent ID (if any)
  144. $parentID = parentID($session, $path);
  145. if ($parentID === false) {
  146. return false;
  147. }
  148. # POST
  149. $data = array(
  150. 'session_id' => $session,
  151. 'folder_name' => basePath($path),
  152. 'folder_sub_parent' => $parentID,
  153. 'folder_is_public' => 1
  154. );
  155. $response = curlPost('/folder.json', $data);
  156. # Check the result
  157. if ($response['FolderId']) {
  158. return($response['FolderId']);
  159. }
  160. return false;
  161. }
  162. # Trash a folder
  163. function rmFolder($session, $path) {
  164. $id = folderID($session, $path);
  165. if (!$id) {
  166. return false;
  167. }
  168. $data = array(
  169. 'session_id' => $session,
  170. 'folder_id' => $id
  171. );
  172. $response = curlPost('/folder/trash.json', $data);
  173. # Check the result
  174. if ($response['DirUpdateTime']) {
  175. return true;
  176. }
  177. return false;
  178. }
  179. # Return the ID of a file
  180. function fileID($session, $path) {
  181. $data = array(
  182. 'session_id' => $session,
  183. 'path' => $path
  184. );
  185. $response = curlPost('/file/idbypath.json', $data);
  186. # Check the result
  187. if ($response['FileId']) {
  188. return($response['FileId']);
  189. }
  190. return false;
  191. }
  192. # Return all file metadata
  193. function fileInfo($session, $path) {
  194. $id = fileID($session, $path);
  195. if (!$id) {
  196. return false;
  197. }
  198. # GET
  199. $response = curlGet('/file/info.json/' . $session . '/' . $id);
  200. # Check the result
  201. if ($response['FileId']) {
  202. return($response);
  203. }
  204. return false;
  205. }
  206. # Upload the provided file to the given path
  207. function fileUpload($session, $path, $file) {
  208. $data = array(
  209. 'session_id' => $session,
  210. 'folder_id' => $folderID,
  211. 'file_name' => $name,
  212. 'file_size' => $size
  213. );
  214. }
  215. ?>