/**
  * UserController constructor.
  * @param $path
  * @param $method
  * @param $body
  */
 public function __construct($path, $method, $body)
 {
     $this->body = $body;
     $this->method = $method;
     $this->path = $path;
     if (count($this->path) != 0) {
         if (count($this->path) == 1 && is_numeric($path[0])) {
             $this->id = $path[0];
         } elseif (count($this->path) == 2 && is_numeric($path[0]) && $path[1] == "password") {
             $this->id = $path[0];
             $path = trimPath($path, 2);
             $this->controller = new PasswordController($path, $method, $body, $this->id);
         } elseif (count($this->path) == 1 && $path[0] == "reset-password") {
             $this->id = $path[0];
             $path = trimPath($path, 2);
             $this->controller = new ResetPasswordController($path, $method, $body);
         } elseif (count($this->path) == 1 && $path[0] == "login") {
             $this->id = $path[0];
             $path = trimPath($path, 1);
             $this->controller = new LoginController($path, $method, $body);
         } else {
             $this->controller = new ErrorController(new InvalidPathError());
         }
     }
 }
 /**
  * APIV1Controller constructor.
  * @param $path
  * @param $method
  * @param $body
  */
 public function __construct($path, $method, $body)
 {
     $part = $path[0];
     $path = trimPath($path, 1);
     // Choose path
     switch ($part) {
         case 'documents':
             $this->controller = new DocumentController($path, $method, $body);
             break;
         case 'topics':
             $this->controller = new TopicController($path, $method, $body);
             break;
         case 'users':
             $this->controller = new UserController($path, $method, $body);
             break;
         case 'target-groups':
             $this->controller = new TargetGroupController($path, $method, $body);
             break;
         case 'link-categories':
             $this->controller = new LinkCategoryController($path, $method, $body);
             break;
         case 'actions':
             $this->controller = new ActionController($path, $method, $body);
             break;
         case 'mandatory':
             $this->controller = new MandatoryController($path, $method, $body);
             break;
         case 'status':
             $this->controller = new StatusController($path, $method, $body);
             break;
         case 'document-types':
             $this->controller = new DocumentTypeController($path, $method, $body);
             break;
         case 'document-fields':
             $this->controller = new DocumentFieldController($path, $method, $body);
             break;
         default:
             $this->controller = new ErrorController(new InvalidPathError());
             break;
     }
 }
 /**
  * MainController constructor.
  * @param $path
  * @param $method
  * @param $body
  */
 public function __construct($path, $method, $body)
 {
     $this->path = $path;
     if (count($path)) {
         $this->api_version = $path[0];
     } else {
         $this->api_version = "";
     }
     // Check if the url ended with '/', if se delete
     if (end($path) == '') {
         $ak = array_keys($path);
         unset($path[end($ak)]);
     }
     $path = trimPath($path, 1);
     // Authenticate user
     $this->authenticateUser();
     $requires_authentication = true;
     // GET, OPTIONS and POST to '/users/reset-password' does not require authentication
     if ($method == Response::REQUEST_METHOD_GET || $method == Response::REQUEST_METHOD_OPTIONS || $method == Response::REQUEST_METHOD_POST && $path[0] == 'users' && $path[1] == 'reset-password') {
         if ($method == Response::REQUEST_METHOD_GET && $path[0] == 'users') {
             $requires_authentication = true;
         } else {
             $requires_authentication = false;
         }
     }
     // If user is not set and method requires authentication, returns AuthenticationError
     if (!array_key_exists('CURRENT_USER', $GLOBALS) && $requires_authentication) {
         $this->controller = new ErrorController(new AuthenticationError($method));
     } else {
         // Chose API version
         switch ($this->api_version) {
             case 'v1':
                 $this->controller = new APIV1Controller($path, $method, $body);
                 break;
             default:
                 $this->controller = new ErrorController(new InvalidPathError());
                 break;
         }
     }
 }
<?php

require_once __DIR__ . '/../db_info.php';
require_once "MainController.php";
require_once "v1/errors/InvalidJSONError.php";
require_once 'utils.php';
// Headers
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: accept, authorization, content-type");
header("Access-Control-Allow-Methods: PUT, DELETE, POST, GET, OPTIONS");
// Store request method
$method = $_SERVER['REQUEST_METHOD'];
// Store path
if (array_key_exists("PATH_INFO", $_SERVER)) {
    $path = explode('/', $_SERVER['PATH_INFO']);
    $path = trimPath($path, 1);
} else {
    $path = array();
}
// Store payload - JSON that is sent with the request
$payload = file_get_contents('php://input');
if ($payload) {
    $body = json_decode($payload, true);
} else {
    $body = array();
}
// If there are no errors in the JSON, instantiate MainController
if (json_last_error() == JSON_ERROR_NONE) {
    $req = new MainController($path, $method, $body);
    $response = $req->getResponse();
} else {
     // trimPath function
     function trimPath($path, $depth)
     {
         $path = explode('/', $path);
         $np = '/';
         for ($i = $depth; $i < count($path); $i++) {
             $np .= $path[$i] . '/';
         }
         return $np;
     }
     // Find FTP path
     $i = 1;
     $path = $_POST['ftp_path'];
     // Set max tries to 15
     for ($i = 1; $i < 15; $i++) {
         if (@ftp_chdir($conn_id, trimPath($path, $i))) {
             $log[] = "Successfully connected to FTP server";
             $i = 15;
         }
     }
 } else {
     $errors[] = "Fatal: couldn't connect to the FTP server. Perform chmod() manually.";
     $err++;
 }
 // Count the ftp_chmod() successes
 $ftp_chmod = 0;
 $errfile = array();
 // Perform the ftp_chmod command
 if (@ftp_chmod($conn_id, 0666, "./.htaccess")) {
     $ftp_chmod++;
 } else {