Subversion Repository Public Repository

Nextrek

Diff Revisions 506 vs 507 for /Android/SmartCharging/endPoints/upload_avatar.php

Diff revisions: vs.
  @@ -1,22 +1,7 @@
1 1 <?php
2 2
3 - require_once("UserRepository.php");
4 -
5 - if (!array_key_exists("user_id", $_POST) || !array_key_exists("url", $_POST)) {
6 - $risposta["return"] = 1;
7 - $risposta["avatar_path"] = "";
8 - echo json_encode($risposta);
9 - die();
10 - }
11 -
12 - if(isset($_FILES['avatar_file']))
13 - {
14 - $file = $_FILES['avatar_file'];
15 - if($file['error'] == UPLOAD_ERR_OK and is_uploaded_file($file['tmp_name']))
16 - {
17 - move_uploaded_file($file['tmp_name'], $_POST["url"]);
18 - }
19 - }
3 + require_once("repositories/UserRepository.php");
4 + require_once("constants.php");
20 5
21 6 $servername = "localhost";
22 7 $username = "smartcharging";
  @@ -31,15 +16,37 @@
31 16 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
32 17 //echo "Connesso con successo al DB";
33 18
19 + if (!array_key_exists("token", $_POST) || !UserRepository::checkToken($_POST["token"], $conn)) {
20 + $risposta["return"] = -1;
21 + echo json_encode($risposta);
22 + die();
23 + }
24 +
25 + if (!array_key_exists("user_id", $_POST) || !array_key_exists("avatar_name", $_POST)) {
26 + $risposta["return"] = 1;
27 + $risposta["avatar_name"] = "";
28 + echo json_encode($risposta);
29 + die();
30 + }
31 +
32 + if(isset($_FILES['avatar_file']))
33 + {
34 + $file = $_FILES['avatar_file'];
35 + if($file['error'] == UPLOAD_ERR_OK and is_uploaded_file($file['tmp_name']))
36 + {
37 + move_uploaded_file($file['tmp_name'], $_SERVER['DOCUMENT_ROOT'].AVATAR_PATH.$_POST["avatar_name"]);
38 + }
39 + }
40 +
34 41 $user_repo = new UserRepository($conn);
35 - $risposta = $user_repo->setAvatar($_POST["user_id"], $_POST["url"]);
42 + $risposta = $user_repo->setAvatar($_POST["user_id"], $_POST["avatar_name"]);
36 43
37 44 }
38 45 catch(PDOException $e)
39 46 {
40 47 echo $e->getMessage();
41 48 $risposta["return"] = 1;
42 - $risposta["avatar_path"] = "";
49 + $risposta["avatar_name"] = "";
43 50 }
44 51
45 52 echo json_encode($risposta);