opendrive.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. # Setup cURL
  29. $ch = curl_init($API_BASE . $url);
  30. curl_setopt_array($ch, array(
  31. CURLOPT_CAINFO => $CA_PATH,
  32. CURLOPT_POST => true,
  33. CURLOPT_RETURNTRANSFER => true,
  34. CURLOPT_HTTPHEADER => array(
  35. 'Content-Type: application/json'
  36. ),
  37. CURLOPT_POSTFIELDS => json_encode($data)
  38. ));
  39. # Send the request
  40. $response = curl_exec($ch);
  41. # Check for errors
  42. if ($response === FALSE){
  43. die(curl_error($ch));
  44. }
  45. # Return
  46. $responseData = json_decode($response, TRUE);
  47. return($responseData);
  48. }
  49. # Login to OD and return the SessionID
  50. function login() {
  51. global $API_VERSION;
  52. global $OD_USER;
  53. global $OD_PASSWD;
  54. $data = array(
  55. 'username' => $OD_USER,
  56. 'passwd' => $OD_PASSWD,
  57. 'version' => $API_VERISON
  58. );
  59. $response = curlPost('/session/login.json', $data);
  60. # Check the result
  61. if ($response['SessionID']) {
  62. return $response['SessionID'];
  63. }
  64. return false;
  65. }
  66. # Logout of OpenDrive
  67. function logout($session) {
  68. $data = array(
  69. 'session_id' => $session
  70. );
  71. $response = curlPost('/session/logout.json', $data);
  72. # Check the result
  73. if ($response['result'] == 'true') {
  74. return true;
  75. }
  76. return false;
  77. }
  78. # Return the ID of a folder path
  79. function folderID($session, $path) {
  80. $data = array(
  81. 'session_id' => $session,
  82. 'path' => $path
  83. );
  84. $response = curlPost('/folder/idbypath.json', $data);
  85. # Check the result
  86. if ($response['FolderId']) {
  87. return($response['FolderId']);
  88. }
  89. return false;
  90. }
  91. # Create a folder
  92. function mkFolder($session, $path) {
  93. # Find the parent ID (if any)
  94. $path = preg_replace('/\/+$/', '', $path);
  95. $path = preg_replace('/^\/+/', '', $path);
  96. $folder = basename($path);
  97. $parent = dirname($path);
  98. $parentID = 0;
  99. if ($parent != '.') {
  100. $parentID = folderID($session, $parent);
  101. if ($parentID === false) {
  102. return false;
  103. }
  104. }
  105. # POST
  106. $data = array(
  107. 'session_id' => $session,
  108. 'folder_name' => $folder,
  109. 'folder_sub_parent' => $parentID,
  110. 'folder_is_public' => 1
  111. );
  112. $response = curlPost('/folder.json', $data);
  113. # Check the result
  114. if ($response['FolderId']) {
  115. return($response['FolderId']);
  116. }
  117. return false;
  118. }
  119. function rmFolder($session, $path) {
  120. $id = folderID($session, $path);
  121. if (!$id) {
  122. return false;
  123. }
  124. $data = array(
  125. 'session_id' => $session,
  126. 'folder_id' => $id
  127. );
  128. $response = curlPost('/folder/trash.json', $data);
  129. # Check the result
  130. if ($response['DirUpdateTime']) {
  131. return true;
  132. }
  133. return false;
  134. }
  135. ?>