Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. <?php
  2.  
  3. require_once WWW_ROOT . 'dao' . DS . 'ImageDAO.php';
  4. require_once WWW_ROOT . 'classes' . DS . 'Token.php';
  5.  
  6. use PHPassLib\Hash\BCrypt;
  7. use Eventviva\ImageResize;
  8.  
  9. //401 unauthorized
  10. //403 forbidden
  11.  
  12. $base = '/api/images';
  13.  
  14. $app->delete($base.'/{id}', function($request, $response, $args){
  15.  
  16. // TODO
  17.  
  18. $deleted = $imageDAO->delete($args['id']);
  19.  
  20. if(empty($deleted)){
  21. $response = $response->withStatus(400);
  22. return $response;
  23. }
  24.  
  25. $response->getBody()->write(json_encode(array()));
  26. return $response;
  27.  
  28. });
  29.  
  30. $app->get($base, function($request, $response, $args){
  31.  
  32. $token = new Token();
  33. $token->setFromRequest($request);
  34.  
  35. if(!$token->verify()){
  36. //401: unauthorized
  37. $response = $response->withStatus(401);
  38. return $response;
  39. }
  40.  
  41. $query = $request->getQueryParams();
  42.  
  43. if(empty($query) && !$token->isAdmin()){
  44. //403: forbidden
  45. $response = $response->withStatus(403);
  46. return $response;
  47. }
  48.  
  49. $imageDAO = new ImageDAO();
  50. $data = array();
  51.  
  52. if(!empty($query) && !empty($query['userid'])){
  53. if($token->sameUserId($query['userid']) && !$token->isAdmin()) {
  54. $response = $response->withStatus(403);
  55. return $response;
  56. }
  57. $data['images'] = $imageDAO->selectByUserId($query['userid']);
  58. } else {
  59. $data['images'] = $imageDAO->selectAll();
  60. }
  61.  
  62. $response->getBody()->write(json_encode($data));
  63. return $response->withHeader('Content-Type','application/json');
  64.  
  65. });
  66.  
  67. $app->get($base.'/{id}', function($request, $response, $args){
  68.  
  69. $imageDAO = new ImageDAO();
  70. $image = $imageDAO->selectById($args['id']);
  71.  
  72. $response->getBody()->write(json_encode($image));
  73. return $response->withHeader('Content-Type','application/json');
  74.  
  75. });
  76.  
  77. $app->post($base, function($request, $response, $args){
  78.  
  79. //Token van de request omzetten in een token die we kunnen gebruiken om te checken of de user correct is (rechten heeft)
  80. $token = new Token();
  81. $token->setFromRequest($request);
  82.  
  83. if(!$token->verify()){
  84. $response = $response->withStatus(401);
  85. return $response;
  86. }
  87.  
  88. $body = $request->getParsedBody();
  89. $file = $_FILES['file'];
  90.  
  91. $errors = array();
  92.  
  93. if(empty($file)){
  94. $errors[] = 'Please provide a file to upload';
  95. }
  96.  
  97. if(empty($token->getUser()->id)){
  98. $errors[] = 'Please provide a userId';
  99. }
  100.  
  101. if(!empty($errors)){
  102. $response->getBody()->write(json_encode(array('errors' => $errors)));
  103. $response = $response->withStatus(400);
  104. return $response;
  105. }
  106.  
  107. $isImage = getimagesize($file['tmp_name']);
  108.  
  109. if(!$isImage){
  110. $errors = array();
  111. $errors[] = 'File must be an image';
  112.  
  113. $response->getBody()->write(json_encode(array('errors' => $errors)));
  114. $response = $response->withStatus(400);
  115. return $response;
  116. }
  117.  
  118. $imageDAO = new ImageDAO();
  119.  
  120. $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
  121.  
  122. $filename = $token->getUser()->id . '_' . uniqid() . '.' .$ext;
  123.  
  124. $original = 'uploads' . DS . $filename;
  125. $thumb = 'uploads' . DS . 'th_' . $filename;
  126. $hash = md5_file($file['tmp_name']);
  127.  
  128. $existing = $imageDAO->selectByHash($hash);
  129.  
  130. //existing niet leeg? Afbeelding al geupload
  131. if(!empty($existing)){
  132. if($imageDAO->selectByHashAndUserId($hash, $token->getUser()->id)) {
  133. $errors = array();
  134. $errors[] = 'File already uploaded';
  135.  
  136. $response->getBody()->write(json_encode(array('errors' => $errors)));
  137. $response = $response->withStatus(400);
  138. return $response;
  139. }
  140.  
  141. $original = $existing[0]['original'];
  142. $thumb = $existing[0]['thumb'];
  143. $hash = $existing[0]['hash'];
  144. } else {
  145. $image = new ImageResize($file['tmp_name']);
  146. $image->crop(200, 200);
  147. $image->save(WWW_ROOT . DS . $thumb);
  148.  
  149. move_uploaded_file($file['tmp_name'], WWW_ROOT . DS . $original);
  150. }
  151.  
  152. $body['original'] = $original;
  153. $body['thumb'] = $thumb;
  154. $body['hash'] = $hash;
  155. $body['userId'] = $token->getUser()->id;
  156.  
  157. $insertedImage = $imageDAO->insert($body);
  158.  
  159. if(empty($insertedImage)){
  160. $errors = $imageDAO->getValidationErrors($body);
  161. $response->getBody()->write(json_encode(array('errors' => $errors)));
  162. $response = $response->withStatus(400);
  163. } else {
  164. $response->getBody()->write(json_encode($insertedImage));
  165. $response = $response->withStatus(201);
  166. }
  167.  
  168. return $response->withHeader('Content-Type', 'application/json');
  169. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement