PageRenderTime 31ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/api/rest/file.php

https://gitlab.com/x33n/respond
PHP | 591 lines | 330 code | 157 blank | 104 comment | 47 complexity | 6d767b0639f1b586764c7651270b78ea MD5 | raw file
  1. <?php
  2. /**
  3. * A protected API call to retrieve the current site
  4. * @uri /file/post
  5. */
  6. class FilePostResource extends Tonic\Resource {
  7. /**
  8. * @method POST
  9. */
  10. function get() {
  11. // get token
  12. $token = Utilities::ValidateJWTToken(apache_request_headers());
  13. // check if token is not null
  14. if($token != NULL){
  15. // get a reference to the site, user
  16. $site = Site::GetBySiteId($token->SiteId);
  17. parse_str($this->request->data, $request); // parse request
  18. $overwrite = NULL;
  19. $folder = 'files';
  20. if(isset($_REQUEST['overwrite'])){
  21. $overwrite = $_REQUEST['overwrite'];
  22. }
  23. if(isset($_REQUEST['folder'])){
  24. $folder = $_REQUEST['folder'];
  25. }
  26. $arr = array();
  27. // Get uploaded file info
  28. $filename = $_FILES['file']['name'];
  29. $file = $_FILES['file']['tmp_name'];
  30. $contentType = $_FILES['file']['type'];
  31. $size = intval($_FILES['file']['size']/1024);
  32. // overwrite if applicable
  33. if($overwrite != NULL){
  34. $filename = $overwrite;
  35. }
  36. $parts = explode(".", $filename);
  37. $ext = end($parts); // get extension
  38. $ext = strtolower($ext); // convert to lowercase
  39. $thumbnail = 't-'.$filename;
  40. // allowed filetypes
  41. $allowed = explode(',', ALLOWED_FILETYPES);
  42. // trim and lowercase all items in the aray
  43. $allowed = array_map('trim', $allowed);
  44. $allowed = array_map('strtolower', $allowed);
  45. // save image
  46. if($ext=='png' || $ext=='jpg' || $ext=='gif' || $ext == 'svg'){ // upload image
  47. $arr = Image::SaveImageWithThumb($site, $filename, $file, $folder);
  48. // set local URL
  49. $url = $site['Domain'];
  50. // set URL if on S3
  51. if(FILES_ON_S3 == true){
  52. $url = str_replace('{{bucket}}', $site['Bucket'], S3_URL);
  53. $url = str_replace('{{site}}', $site['FriendlyId'], $url);
  54. }
  55. // create array
  56. $arr = array(
  57. 'filename' => $filename,
  58. 'fullUrl' => $url.'/'.$folder.'/'.$filename,
  59. 'thumbUrl' => $site['Domain'].'/'.$folder.'/thumbs/'.$filename,
  60. 'extension' => $ext,
  61. 'isImage' => true,
  62. 'width' => $arr['width'],
  63. 'height' => $arr['height'],
  64. );
  65. }
  66. else if(in_array($ext, $allowed)){ // save file if it is allowed
  67. // save file to directory
  68. $directory = SITES_LOCATION.'/'.$site['FriendlyId'].'/'.$folder.'/';
  69. // set url
  70. $url = $site['Domain'];
  71. // set URL if on S3
  72. if(FILES_ON_S3 == true){
  73. // meta is blank for non-images
  74. $meta = array();
  75. $type = $contentType;
  76. echo 'test';
  77. echo $filename;
  78. // save file with meta-data
  79. S3::SaveContents($site, $type, $filename, $file, $meta, $folder);
  80. $url = str_replace('{{bucket}}', $site['Bucket'], S3_URL);
  81. $url = str_replace('{{site}}', $site['FriendlyId'], $url);
  82. }
  83. else{
  84. // upload file
  85. Utilities::SaveFile($directory, $filename, $file);
  86. }
  87. $arr = array(
  88. 'filename' => $filename,
  89. 'fullUrl' => $url.'/'.$folder.'/'.$filename,
  90. 'thumbUrl' => NULL,
  91. 'extension' => $ext,
  92. 'isImage' => false,
  93. 'width' => -1,
  94. 'height' => -1
  95. );
  96. }
  97. else{
  98. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  99. }
  100. // return a json response
  101. $response = new Tonic\Response(Tonic\Response::OK);
  102. $response->contentType = 'application/json';
  103. $response->body = json_encode($arr);
  104. return $response;
  105. }
  106. else{
  107. // return an unauthorized exception (401)
  108. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  109. }
  110. }
  111. }
  112. /**
  113. * A protected API call to retrieve images from the site
  114. * @uri /image/list/all
  115. */
  116. class ImageListResource extends Tonic\Resource {
  117. /**
  118. * @method GET
  119. */
  120. function get() {
  121. // get token
  122. $token = Utilities::ValidateJWTToken(apache_request_headers());
  123. // check if token is not null
  124. if($token != NULL){
  125. // get a reference to the site, user
  126. $site = Site::GetBySiteId($token->SiteId);
  127. $arr = array();
  128. if(FILES_ON_S3 == true){
  129. $arr = S3::ListFiles($site, true);
  130. }
  131. else{
  132. $directory = SITES_LOCATION.'/'.$site['FriendlyId'].'/files/';
  133. //get all image files with a .html ext
  134. $files = glob($directory . "*.*");
  135. $arr = array();
  136. $image_exts = array('gif', 'png', 'jpg', 'svg');
  137. //print each file name
  138. foreach($files as $file){
  139. $f_arr = explode("/",$file);
  140. $count = count($f_arr);
  141. $filename = $f_arr[$count-1];
  142. // get extension
  143. $parts = explode(".", $filename);
  144. $ext = end($parts); // get extension
  145. $ext = strtolower($ext); // convert to lowercase
  146. // is image
  147. $is_image = in_array($ext, $image_exts);
  148. if($is_image==true){
  149. list($width, $height, $type, $attr) = Image::getImageInfo($directory.$filename);
  150. $size = filesize($directory.$filename);
  151. $file = array(
  152. 'filename' => $filename,
  153. 'fullUrl' => $site['Domain'].'/files/'.$filename,
  154. 'thumbUrl' => $site['Domain'].'/files/thumbs/'.$filename,
  155. 'extension' => $ext,
  156. 'size' => number_format($size / 1048576, 2),
  157. 'isImage' => $is_image,
  158. 'width' => $width,
  159. 'height' => $height
  160. );
  161. array_push($arr, $file);
  162. }
  163. }
  164. }
  165. // return a json response
  166. $response = new Tonic\Response(Tonic\Response::OK);
  167. $response->contentType = 'application/json';
  168. $response->body = json_encode($arr);
  169. return $response;
  170. }
  171. else{
  172. // return an unauthorized exception (401)
  173. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  174. }
  175. }
  176. }
  177. /**
  178. * A protected API call to retrieve the current site
  179. * @uri /file/list
  180. */
  181. class FileListAllResource extends Tonic\Resource {
  182. /**
  183. * @method GET
  184. */
  185. function get() {
  186. // get token
  187. $token = Utilities::ValidateJWTToken(apache_request_headers());
  188. // check if token is not null
  189. if($token != NULL){
  190. // get a reference to the site, user
  191. $site = Site::GetBySiteId($token->SiteId);
  192. $arr = array();
  193. if(FILES_ON_S3 == true){
  194. $arr = S3::ListFiles($site);
  195. }
  196. else{
  197. $directory = SITES_LOCATION.'/'.$site['FriendlyId'].'/files/';
  198. //get all image files with a .html ext
  199. $files = glob($directory . "*.*");
  200. $arr = array();
  201. $image_exts = array('gif', 'png', 'jpg', 'svg');
  202. //print each file name
  203. foreach($files as $file){
  204. $f_arr = explode("/",$file);
  205. $count = count($f_arr);
  206. $filename = $f_arr[$count-1];
  207. // get extension
  208. $parts = explode(".", $filename);
  209. $ext = end($parts); // get extension
  210. $ext = strtolower($ext); // convert to lowercase
  211. // is image
  212. $isImage = in_array($ext, $image_exts);
  213. // get size of file
  214. $size = filesize($file);
  215. if($isImage==true){
  216. $width = 0;
  217. $height = 0;
  218. try{
  219. list($width, $height, $type, $attr) = Image::getImageInfo($directory.$filename);
  220. }
  221. catch(Exception $e){}
  222. $file = array(
  223. 'filename' => $filename,
  224. 'fullUrl' => $site['Domain'].'/files/'.$filename,
  225. 'thumbUrl' => $site['Domain'].'/files/thumbs/'.$filename,
  226. 'extension' => $ext,
  227. 'isImage' => $isImage,
  228. 'width' => $width,
  229. 'height' => $height,
  230. 'size' => number_format($size / 1048576, 2)
  231. );
  232. array_push($arr, $file);
  233. }
  234. else if($is_thumb==false){
  235. $file = array(
  236. 'filename' => $filename,
  237. 'fullUrl' => $site['Domain'].'/files/'.$filename,
  238. 'thumbUrl' => $site['Domain'].'/files/thumbs/'.$filename,
  239. 'thumbUrl' => 'n/a',
  240. 'extension' => $ext,
  241. 'isImage' => $isImage,
  242. 'width' => NULL,
  243. 'height' => NULL,
  244. 'size' => number_format($size / 1048576, 2)
  245. );
  246. array_push($arr, $file);
  247. }
  248. }
  249. }
  250. // return a json response
  251. $response = new Tonic\Response(Tonic\Response::OK);
  252. $response->contentType = 'application/json';
  253. $response->body = json_encode($arr);
  254. return $response;
  255. }
  256. else{
  257. // return an unauthorized exception (401)
  258. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  259. }
  260. }
  261. }
  262. /**
  263. * A protected API call to retrieve the current site
  264. * @uri /download/list
  265. */
  266. class DownloadListAllResource extends Tonic\Resource {
  267. /**
  268. * @method GET
  269. */
  270. function get() {
  271. // get token
  272. $token = Utilities::ValidateJWTToken(apache_request_headers());
  273. // check if token is not null
  274. if($token != NULL){
  275. // get a reference to the site, user
  276. $site = Site::GetBySiteId($token->SiteId);
  277. $arr = array();
  278. if(FILES_ON_S3 == true){
  279. $arr = S3::ListFiles($site, false, 'downloads');
  280. }
  281. else{
  282. $directory = SITES_LOCATION.'/'.$site['FriendlyId'].'/downloads/';
  283. //get all image files with a .html ext
  284. $files = glob($directory . "*.*");
  285. $arr = array();
  286. $image_exts = array('gif', 'png', 'jpg', 'svg');
  287. //print each file name
  288. foreach($files as $file){
  289. $f_arr = explode("/",$file);
  290. $count = count($f_arr);
  291. $filename = $f_arr[$count-1];
  292. // get extension
  293. $parts = explode(".", $filename);
  294. $ext = end($parts); // get extension
  295. $ext = strtolower($ext); // convert to lowercase
  296. // is image
  297. $isImage = in_array($ext, $image_exts);
  298. // get size of file
  299. $size = filesize($file);
  300. if($isImage==true){
  301. $width = 0;
  302. $height = 0;
  303. try{
  304. list($width, $height, $type, $attr) = Image::getImageInfo($directory.$filename);
  305. }
  306. catch(Exception $e){}
  307. $file = array(
  308. 'filename' => $filename,
  309. 'fullUrl' => $site['Domain'].'/downloads/'.$filename,
  310. 'thumbUrl' => $site['Domain'].'/downloads/thumbs/'.$filename,
  311. 'extension' => $ext,
  312. 'isImage' => $isImage,
  313. 'width' => $width,
  314. 'height' => $height,
  315. 'size' => number_format($size / 1048576, 2)
  316. );
  317. array_push($arr, $file);
  318. }
  319. else if($is_thumb==false){
  320. $file = array(
  321. 'filename' => $filename,
  322. 'fullUrl' => $site['Domain'].'/downloads/'.$filename,
  323. 'thumbUrl' => $site['Domain'].'/downloads/thumbs/'.$filename,
  324. 'thumbUrl' => 'n/a',
  325. 'extension' => $ext,
  326. 'isImage' => $isImage,
  327. 'width' => NULL,
  328. 'height' => NULL,
  329. 'size' => number_format($size / 1048576, 2)
  330. );
  331. array_push($arr, $file);
  332. }
  333. }
  334. }
  335. // return a json response
  336. $response = new Tonic\Response(Tonic\Response::OK);
  337. $response->contentType = 'application/json';
  338. $response->body = json_encode($arr);
  339. return $response;
  340. }
  341. else{
  342. // return an unauthorized exception (401)
  343. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  344. }
  345. }
  346. }
  347. /**
  348. * A protected API call to retrieve the size in MB of files stored on the site
  349. * @uri /file/retrieve/size
  350. */
  351. class FileRetrieveSizeResource extends Tonic\Resource {
  352. /**
  353. * @method GET
  354. */
  355. function get() {
  356. // get token
  357. $token = Utilities::ValidateJWTToken(apache_request_headers());
  358. // check if token is not null
  359. if($token != NULL){
  360. // get a reference to the site, user
  361. $site = Site::GetBySiteId($token->SiteId);
  362. $arr = array();
  363. if(FILES_ON_S3 == true){
  364. $total_size = S3::RetrieveFilesSize($site);
  365. }
  366. else{
  367. $directory = SITES_LOCATION.'/'.$site['FriendlyId'].'/files/';
  368. //get all files in the directory
  369. $files = glob($directory . "*.*");
  370. $total_size = 0;
  371. //print each file name
  372. foreach($files as $file){
  373. // get size of file
  374. $total_size = $total_size + filesize($file);
  375. }
  376. $total_size = round(($total_size / 1024 / 1024), 2);
  377. }
  378. // return a json response
  379. $response = new Tonic\Response(Tonic\Response::OK);
  380. $response->contentType = 'text/html';
  381. $response->body = $total_size;
  382. return $response;
  383. }
  384. else{
  385. // return an unauthorized exception (401)
  386. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  387. }
  388. }
  389. }
  390. /**
  391. * A protected API call to retrieve the current site
  392. * @uri /file/remove
  393. */
  394. class FileRemoveResource extends Tonic\Resource {
  395. /**
  396. * @method POST
  397. */
  398. function get() {
  399. // get token
  400. $token = Utilities::ValidateJWTToken(apache_request_headers());
  401. // check if token is not null
  402. if($token != NULL){
  403. // get a reference to the site, user
  404. $site = Site::GetBySiteId($token->SiteId);
  405. parse_str($this->request->data, $request); // parse request
  406. $filename = $request['filename'];
  407. $folder = 'files';
  408. if(isset($_REQUEST['folder'])){
  409. $folder = $_REQUEST['folder'];
  410. }
  411. if(FILES_ON_S3 == true){ // remove file on S3
  412. S3::RemoveFile($site, $filename, $folder);
  413. }
  414. else{ // remove local file
  415. // remove file
  416. $path = SITES_LOCATION.'/'.$site['FriendlyId'].'/'.$folder.'/'.$filename;
  417. if(file_exists($path)){
  418. $path = unlink($path);
  419. }
  420. // remove thumb
  421. $path = SITES_LOCATION.'/'.$site['FriendlyId'].'/'.$folder.'/thumbs/'.$filename;
  422. if(file_exists($path)){
  423. $path = unlink($path);
  424. }
  425. }
  426. return new Tonic\Response(Tonic\Response::OK);
  427. }
  428. else{
  429. // return an unauthorized exception (401)
  430. return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
  431. }
  432. }
  433. }
  434. ?>