2024-05-09 16:40:42 -05:00
|
|
|
<?php
|
2024-05-15 14:54:33 -05:00
|
|
|
header("Content-Type: multipart/form-data");
|
2024-05-14 11:38:57 -05:00
|
|
|
header("Access-Control-Allow-Origin: *");
|
|
|
|
|
|
|
|
require_once PD . "/Model/ImageModel.php";
|
2024-05-09 16:40:42 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Description of ImageController
|
|
|
|
*
|
2024-05-15 14:54:33 -05:00
|
|
|
* @author Mike Howard
|
2024-05-09 16:40:42 -05:00
|
|
|
*/
|
|
|
|
class ImageController extends BaseController {
|
|
|
|
|
2024-05-13 14:36:49 -05:00
|
|
|
public $imageModel;
|
2024-05-09 16:40:42 -05:00
|
|
|
public $action;
|
2024-05-14 11:38:57 -05:00
|
|
|
private $imagePayload;
|
2024-05-09 16:40:42 -05:00
|
|
|
|
|
|
|
function __construct() {
|
2024-05-13 14:36:49 -05:00
|
|
|
parent::__construct();
|
|
|
|
$this->basename = "ImageController";
|
2024-05-09 16:40:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* http://localhost/CustomerRewardsRESTAPI/index.php/image/process/upload
|
|
|
|
*/
|
|
|
|
public function processAction() {
|
|
|
|
$this->strErrorDesc = '';
|
|
|
|
$this->strErrorHeader = '';
|
|
|
|
try {
|
2024-05-13 14:36:49 -05:00
|
|
|
$this->requestMethod = $this->getServerRequestMethod();
|
2024-05-14 11:38:57 -05:00
|
|
|
$this->imageModel = new ImageModel();
|
|
|
|
|
2024-05-09 16:40:42 -05:00
|
|
|
switch($this->action) {
|
|
|
|
case "upload":
|
|
|
|
$response = $this->uploadImage();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "update":
|
|
|
|
$response = $this->updateImage();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "delete":
|
|
|
|
$response = $this->deleteImage();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$response = (object) ['Result' => 'Default'];
|
|
|
|
$this->strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action;
|
|
|
|
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$responseData = json_encode($response);
|
|
|
|
} catch (Error $e) {
|
|
|
|
$this->strErrorDesc = $e->getMessage().' Something went wrong in processAction! Please contact support.';
|
|
|
|
$this->strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
|
|
|
|
}
|
|
|
|
|
|
|
|
// send output
|
|
|
|
if (!$this->strErrorDesc) {
|
|
|
|
$this->sendOutput(
|
|
|
|
$responseData,
|
|
|
|
array('Content-Type: application/json', 'HTTP/1.1 200 OK')
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$this->sendOutput(json_encode(array('error' => $this->strErrorDesc)),
|
|
|
|
array('Content-Type: application/json', $this->strErrorHeader)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-14 11:38:57 -05:00
|
|
|
/* Upload the image and store on server as file */
|
2024-05-09 16:40:42 -05:00
|
|
|
private function uploadImage(){
|
|
|
|
if ($this->checkRequestType('POST') == 'false') {
|
2024-05-14 11:38:57 -05:00
|
|
|
return $this->unprocessableRequestResponse();
|
2024-05-09 16:40:42 -05:00
|
|
|
}
|
2024-05-13 23:12:50 -05:00
|
|
|
|
2024-05-20 14:45:54 -05:00
|
|
|
// reads the raw POST data and returns it as a string.
|
|
|
|
$jsonPayload = file_get_contents('php://input');
|
|
|
|
$input = json_decode($jsonPayload, TRUE);
|
|
|
|
|
|
|
|
//get the member data
|
|
|
|
$this->memberId = $input['CustomerID'];
|
|
|
|
$this->imageBlob = $input['ImageBlob'];
|
|
|
|
|
|
|
|
//get the file data
|
2024-05-15 14:54:33 -05:00
|
|
|
$this->imagePayload = $_FILES['image'];
|
|
|
|
|
|
|
|
$this->imageModel->fileName = $this->imagePayload['name'];
|
2024-05-14 11:38:57 -05:00
|
|
|
if(empty($this->imageModel->fileName)){
|
|
|
|
return $this->notFoundResponse();
|
|
|
|
}
|
|
|
|
|
2024-05-15 14:54:33 -05:00
|
|
|
$this->imageModel->tempPath = $this->imagePayload['tmp_name'];
|
2024-05-14 11:38:57 -05:00
|
|
|
$this->imageModel->basename = basename($this->imageModel->fileName);
|
|
|
|
$this->imageModel->originalPath = $this->imageModel->uploadTo.$this->imageModel->basename;
|
|
|
|
$this->imageModel->fileType = pathinfo($this->imageModel->originalPath, PATHINFO_EXTENSION);
|
2024-05-13 23:12:50 -05:00
|
|
|
|
2024-05-14 11:38:57 -05:00
|
|
|
if (! $this->validateImage()) {
|
2024-05-09 16:40:42 -05:00
|
|
|
return $this->unprocessableEntityResponse();
|
|
|
|
}
|
2024-05-20 14:45:54 -05:00
|
|
|
|
2024-05-14 11:38:57 -05:00
|
|
|
if (! $this->handleImage()) {
|
|
|
|
return $this->notFoundResponse();
|
|
|
|
}
|
2024-05-20 14:45:54 -05:00
|
|
|
|
|
|
|
$this->imageModel->insertImage();
|
2024-05-09 16:40:42 -05:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2024-05-14 11:38:57 -05:00
|
|
|
private function handleImage() {
|
|
|
|
$moved = false;
|
2024-05-20 14:45:54 -05:00
|
|
|
if(move_uploaded_file($this->imageModel->tempPath, $this->imageModel->uploadTo . $this->imageModel->fileName)) {
|
2024-05-14 11:38:57 -05:00
|
|
|
$moved = true;
|
2024-05-09 20:25:56 -05:00
|
|
|
}
|
2024-05-14 11:38:57 -05:00
|
|
|
return $moved;
|
2024-05-09 20:25:56 -05:00
|
|
|
}
|
|
|
|
|
2024-05-14 11:38:57 -05:00
|
|
|
private function validateImage(){
|
2024-05-10 21:37:50 -05:00
|
|
|
$validtion = false;
|
2024-05-14 11:38:57 -05:00
|
|
|
if(in_array($this->imageModel->fileType, $this->imageModel->allowFileType)){
|
2024-05-10 21:37:50 -05:00
|
|
|
$validtion = true;
|
|
|
|
}
|
|
|
|
return $validtion;
|
|
|
|
}
|
2024-05-09 16:40:42 -05:00
|
|
|
}
|