opendrive.php 7.4 KB

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