opendrive.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. # Debug
  3. $DEBUG = 0;
  4. if ($_ENV['DEBUG']) {
  5. $DEBUG = intval($_ENV['DEBUG']);
  6. if (!$DEBUG) {
  7. $DEBUG = 1;
  8. }
  9. }
  10. # On-disk config
  11. require_once '.secrets';
  12. # Config
  13. $API_BASE = 'https://dev.opendrive.com/api/v1';
  14. $API_VERSION = 10;
  15. # PHP version support
  16. if (!function_exists('curl_file_create')) {
  17. function curl_file_create($filename, $mimetype = '', $postname = '') {
  18. return '@' . $filename . ';filename=' .
  19. ($postname ?: basename($filename)) .
  20. ($mimetype ? ';type=' . $mimetype : '');
  21. }
  22. }
  23. # Open the DB connection
  24. function dbOpen() {
  25. global $DB_STRING;
  26. global $DB_USER;
  27. global $DB_PASSWD;
  28. try {
  29. $dbh = new PDO($DB_STRING, $DB_USER, $DB_PASSWD);
  30. } catch (PDOException $e) {
  31. die('DB error: ' . $e->getMessage() . "\n");
  32. }
  33. return $dbh;
  34. }
  35. # Cleanup a path for better matching at OpenDrive
  36. function pathCleanup($path) {
  37. $path = preg_replace('/\/+$/', '', $path);
  38. $path = preg_replace('/^\/+/', '', $path);
  39. return $path;
  40. }
  41. # Return the clean parent path of a provided path
  42. function parentPath($path) {
  43. return dirname(pathCleanup($path));
  44. }
  45. # Return the clean base name of a provided path
  46. function basePath($path) {
  47. return basename(pathCleanup($path));
  48. }
  49. function curlPostRaw($url, $data, $header = NULL) {
  50. global $API_BASE;
  51. global $CA_PATH;
  52. global $DEBUG;
  53. # Setup cURL
  54. $ch = curl_init($API_BASE . $url);
  55. curl_setopt_array($ch, array(
  56. CURLOPT_CAINFO => $CA_PATH,
  57. CURLOPT_POST => true,
  58. CURLOPT_RETURNTRANSFER => true,
  59. CURLOPT_POSTFIELDS => $data
  60. ));
  61. if ($DEBUG >= 3) {
  62. curl_setopt($ch, CURLOPT_VERBOSE, true);
  63. }
  64. if ($header != NULL) {
  65. if (!is_array($header)) {
  66. $header = array($header);
  67. }
  68. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  69. }
  70. # Send the request
  71. if ($DEBUG >= 2) {
  72. echo "Request: \n";
  73. print_r($data);
  74. echo "\n";
  75. }
  76. $response = curl_exec($ch);
  77. # Check for errors
  78. if ($response === FALSE){
  79. die(curl_error($ch));
  80. }
  81. # Return
  82. return($response);
  83. }
  84. # POST the provided data to OpenDrive
  85. function curlPost($url, $data) {
  86. $data = json_encode($data);
  87. $response = curlPostRaw($url, $data, 'Content-Type: application/json');
  88. $responseData = json_decode($response, true);
  89. return($responseData);
  90. }
  91. # GET from OpenDrive
  92. function curlGet($url) {
  93. global $API_BASE;
  94. global $CA_PATH;
  95. global $DEBUG;
  96. # Setup cURL
  97. $ch = curl_init($API_BASE . $url);
  98. curl_setopt_array($ch, array(
  99. CURLOPT_CAINFO => $CA_PATH,
  100. CURLOPT_RETURNTRANSFER => true
  101. ));
  102. if ($DEBUG >= 3) {
  103. curl_setopt($ch, CURLOPT_VERBOSE, true);
  104. }
  105. # Send the request
  106. if ($DEBUG >= 2) {
  107. echo "Request: \n";
  108. print_r($data);
  109. echo "\n";
  110. }
  111. $response = curl_exec($ch);
  112. # Check for errors
  113. if ($response === FALSE){
  114. die(curl_error($ch));
  115. }
  116. # Return
  117. $responseData = json_decode($response, true);
  118. return($responseData);
  119. }
  120. # Login to OD and return the SessionID
  121. function login() {
  122. global $API_VERSION;
  123. global $OD_USER;
  124. global $OD_PASSWD;
  125. $data = array(
  126. 'username' => $OD_USER,
  127. 'passwd' => $OD_PASSWD,
  128. 'version' => $API_VERISON
  129. );
  130. $response = curlPost('/session/login.json', $data);
  131. # Check the result
  132. if ($response['SessionID']) {
  133. return $response['SessionID'];
  134. }
  135. return false;
  136. }
  137. # Logout of OpenDrive
  138. function logout($session) {
  139. $data = array(
  140. 'session_id' => $session
  141. );
  142. $response = curlPost('/session/logout.json', $data);
  143. # Check the result
  144. if ($response['result'] == 'true') {
  145. return true;
  146. }
  147. return false;
  148. }
  149. # Return the ID of a folder
  150. function folderID($session, $path) {
  151. $data = array(
  152. 'session_id' => $session,
  153. 'path' => $path
  154. );
  155. $response = curlPost('/folder/idbypath.json', $data);
  156. # Check the result
  157. if ($response['FolderId']) {
  158. return($response['FolderId']);
  159. }
  160. return false;
  161. }
  162. # Find the parent ID for a given path
  163. function parentID($session, $path) {
  164. $parent = parentPath($path);
  165. $parentID = 0;
  166. if ($parent != '.') {
  167. $parentID = folderID($session, $parent);
  168. }
  169. return $parentID;
  170. }
  171. # Create a folder
  172. function mkFolder($session, $path) {
  173. global $DEBUG;
  174. # Find the parent ID (if any)
  175. $parentID = parentID($session, $path);
  176. if ($parentID === false) {
  177. if ($DEBUG) {
  178. echo 'Invalid parent for path: ' . $path . "\n";
  179. }
  180. return false;
  181. }
  182. # POST
  183. $data = array(
  184. 'session_id' => $session,
  185. 'folder_name' => basePath($path),
  186. 'folder_sub_parent' => $parentID,
  187. 'folder_is_public' => 1
  188. );
  189. $response = curlPost('/folder.json', $data);
  190. # Check the result
  191. if ($response['FolderId']) {
  192. return($response['FolderId']);
  193. }
  194. return false;
  195. }
  196. # Trash a folder
  197. function rmFolder($session, $path) {
  198. $id = folderID($session, $path);
  199. if (!$id) {
  200. return false;
  201. }
  202. $data = array(
  203. 'session_id' => $session,
  204. 'folder_id' => $id
  205. );
  206. $response = curlPost('/folder/trash.json', $data);
  207. # Check the result
  208. if ($response['DirUpdateTime']) {
  209. return true;
  210. }
  211. return false;
  212. }
  213. # Return the ID of a file
  214. function fileID($session, $path) {
  215. $data = array(
  216. 'session_id' => $session,
  217. 'path' => $path
  218. );
  219. $response = curlPost('/file/idbypath.json', $data);
  220. # Check the result
  221. if ($response['FileId']) {
  222. return($response['FileId']);
  223. }
  224. return false;
  225. }
  226. # Return all file metadata
  227. function fileInfo($session, $path) {
  228. $id = fileID($session, $path);
  229. if (!$id) {
  230. return false;
  231. }
  232. # GET
  233. $response = curlGet('/file/info.json/' . $session . '/' . $id);
  234. # Check the result
  235. if ($response['FileId']) {
  236. return($response);
  237. }
  238. return false;
  239. }
  240. # Upload the provided file to the given path
  241. function fileUpload($session, $path, $file) {
  242. global $DEBUG;
  243. # Find the local file
  244. $stat = @stat($file);
  245. if ($stat === false) {
  246. if ($DEBUG) {
  247. echo 'Unable to stat: ' . $file . "\n";
  248. }
  249. return false;
  250. }
  251. # Find the parent ID
  252. $parentID = parentID($session, $path);
  253. if ($parentID === false) {
  254. if ($DEBUG) {
  255. echo 'No parent for path: ' . $path . "\n";
  256. }
  257. return false;
  258. }
  259. # Allocate
  260. $data = array(
  261. 'session_id' => $session,
  262. 'folder_id' => $parentID,
  263. 'file_name' => basePath($path),
  264. 'file_size' => $stat['size']
  265. );
  266. $response = curlPost('/upload/create_file.json', $data);
  267. $id = $response['FileId'];
  268. if (!$id) {
  269. if ($DEBUG) {
  270. echo 'Unable to allocate: ' . $path . "\n";
  271. }
  272. return false;
  273. }
  274. # Open
  275. $data = array(
  276. 'session_id' => $session,
  277. 'file_id' => $id,
  278. 'file_size' => $stat['size']
  279. );
  280. $response = curlPost('/upload/open_file_upload.json', $data);
  281. $tmpPath = $response['TempLocation'];
  282. if (!$tmpPath) {
  283. if ($DEBUG) {
  284. echo 'Unable to open: ' . $path . "\n";
  285. }
  286. return false;
  287. }
  288. # Upload
  289. $cfile = curl_file_create($file, 'application/octet-stream', basePath($file));
  290. $data = array(
  291. 'session_id' => $session,
  292. 'file_id' => $id,
  293. 'temp_location' => $tmpPath,
  294. 'chunk_offset' => 0,
  295. 'chunk_size' => $stat['size'],
  296. 'file_data' => $cfile
  297. );
  298. $response = curlPostRaw('/upload/upload_file_chunk.json', $data, 'Expect:');
  299. if ($response) {
  300. $response = json_decode($response, true);
  301. }
  302. if (!$response || $response['TotalWritten'] != $stat['size']) {
  303. if ($DEBUG) {
  304. echo 'Unable to upload: ' . $path . "\n";
  305. }
  306. return false;
  307. }
  308. # Close
  309. $data = array(
  310. 'session_id' => $session,
  311. 'file_id' => $id,
  312. 'temp_location' => $tmpPath,
  313. 'file_time' => $stat['mtime'],
  314. 'file_size' => $stat['size']
  315. );
  316. $response = curlPost('/upload/close_file_upload.json', $data);
  317. # Ignore 500 errors on close. It works anyway.
  318. if ($response && !$response['DirUpdateTime']) {
  319. if ($DEBUG) {
  320. echo 'Unable to close: ' . $path . "\n";
  321. }
  322. return false;
  323. }
  324. # Return the file size on success
  325. return $stat['size'];
  326. }
  327. ?>