mirror of
https://github.com/sctn4elk/CustomerRewardsRESTAPI.git
synced 2025-01-10 05:04:29 -06:00
239 lines
8.3 KiB
PHP
239 lines
8.3 KiB
PHP
<?php
|
|
/**
|
|
* Description of ImageController
|
|
*
|
|
* @author Mike Howard
|
|
*/
|
|
|
|
header("Content-Type: multipart/form-data");
|
|
header("Access-Control-Allow-Origin: *");
|
|
|
|
require_once PD . "/Model/ImageModel.php";
|
|
|
|
class ImageController extends BaseController {
|
|
|
|
public $imageModel;
|
|
public $action;
|
|
private $imagePayload;
|
|
|
|
function __construct() {
|
|
parent::__construct();
|
|
$this->basename = "ImageController";
|
|
}
|
|
|
|
/*
|
|
* http://localhost/CustomerRewardsRESTAPI/index.php/image/process/upload
|
|
*/
|
|
public function processAction() {
|
|
$this->strErrorDesc = '';
|
|
$this->strErrorHeader = '';
|
|
try {
|
|
$this->requestMethod = $this->getServerRequestMethod();
|
|
$this->imageModel = new ImageModel();
|
|
|
|
switch($this->action) {
|
|
case "select":
|
|
$response = $this->selectImage();
|
|
break;
|
|
|
|
case "upload":
|
|
$response = $this->uploadImage();
|
|
break;
|
|
|
|
case "update":
|
|
$response = $this->updateImage();
|
|
break;
|
|
|
|
case "delete":
|
|
$response = $this->deleteImage();
|
|
break;
|
|
|
|
case "blob":
|
|
$response = $this->blobImage();
|
|
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)
|
|
);
|
|
}
|
|
}
|
|
|
|
private function selectImage(){
|
|
if ($this->checkRequestType('GET') == 'false') {
|
|
return $this->unprocessableRequestResponse("Request type is not GET");
|
|
}
|
|
|
|
$this->arrQueryStringParams = $this->getQueryStringParams();
|
|
|
|
if (isset($this->arrQueryStringParams['member_id'])) {
|
|
$response = $this->selectByIdAction();
|
|
} else {
|
|
$this->imageModel->limit = 20;
|
|
|
|
if (isset($this->arrQueryStringParams['limit'])) {
|
|
$this->imageModel->limit = $this->arrQueryStringParams['limit'];
|
|
}
|
|
$response = $this->imageModel->findAllImages();
|
|
unset($this->imageModel->limit);
|
|
}
|
|
return $response;
|
|
}
|
|
|
|
private function selectByIdAction(){
|
|
if (isset($this->arrQueryStringParams['member_id'])) {
|
|
$this->imageModel->memberId = $this->arrQueryStringParams['member_id'];
|
|
$response = $this->imageModel->findImageByMemberId();
|
|
unset($this->imageModel->memberId);
|
|
} else {
|
|
$response = $this->notFoundResponse("selectByIdAction");
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/* Upload the image and store on server as file */
|
|
private function uploadImage(){
|
|
if ($this->checkRequestType('POST') == 'false') {
|
|
return $this->unprocessableRequestResponse();
|
|
}
|
|
|
|
// reads the raw POST data
|
|
$jsonPayload = filter_input_array(INPUT_POST);
|
|
$input = json_decode($jsonPayload['MemberImageModel'], TRUE);
|
|
|
|
//get the member data
|
|
$this->imageModel->memberId = $input['CustomerID'];
|
|
$this->imageModel->imagePath = $input['ImagePath'];
|
|
$this->imageModel->imageName = $input['ImageName'];
|
|
$this->imageModel->imageType = $input['ImageType'];
|
|
|
|
//get the file data
|
|
$this->imagePayload = $_FILES['image'];
|
|
|
|
$this->imageModel->fileName = $this->imagePayload['name'];
|
|
if(empty($this->imageModel->fileName)){
|
|
return $this->notFoundResponse();
|
|
}
|
|
|
|
$this->imageModel->tempPath = $this->imagePayload['tmp_name'];
|
|
$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);
|
|
$this->imageModel->imageSize = filesize($this->imageModel->tempPath);
|
|
|
|
if (! $this->validateImage()) {
|
|
return $this->unprocessableEntityResponse();
|
|
}
|
|
|
|
if (! $this->handleImage()) {
|
|
return $this->notFoundResponse();
|
|
}
|
|
|
|
$memberImage = $this->imageModel->uploadTo . $this->imageModel->memberId;
|
|
if (!is_readable($memberImage)) {
|
|
return $this->notFoundResponse();
|
|
}
|
|
$this->imageModel->imageBlob = file_get_contents($memberImage);
|
|
|
|
$response = $this->imageModel->insertImage();
|
|
return $response;
|
|
}
|
|
|
|
private function handleImage() {
|
|
$moved = false;
|
|
if(move_uploaded_file($this->imageModel->tempPath, $this->imageModel->uploadTo . $this->imageModel->memberId . '.' . $this->imageModel->fileType)) {
|
|
$moved = true;
|
|
}
|
|
return $moved;
|
|
}
|
|
|
|
private function transferImage() {
|
|
$src = $this->imageModel->uploadTo;
|
|
$dest = "/server/location/upload/" . $this->imageModel->fileName;
|
|
$check = file_put_contents($dest, file_get_contents($src));
|
|
if($check != false){
|
|
$check = true;
|
|
}
|
|
|
|
/* Transfer between web servers
|
|
if ( isset($_FILES['uploadedfile']) ) {
|
|
$filename = $_FILES['uploadedfile']['tmp_name'];
|
|
$handle = fopen($filename, "r");
|
|
$data = fread($handle, filesize($filename));
|
|
$POST_DATA = array(
|
|
'file' => base64_encode($data)
|
|
);
|
|
|
|
$curl = curl_init();
|
|
curl_setopt($curl, CURLOPT_URL, '<span style="color: red;">http://extserver.com/handle.php</span>');
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
|
|
curl_setopt($curl, CURLOPT_POST, 1);
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
|
|
$response = curl_exec($curl);
|
|
curl_close ($curl);
|
|
}
|
|
*/
|
|
|
|
/* Separate file running on a different web server
|
|
$encoded_file = $_POST['file'];
|
|
$decoded_file = base64_decode($encoded_file);
|
|
//Now you can copy the uploaded file to your server.
|
|
file_put_contents('<span style="color: red;">subins</span>', $decoded_file);
|
|
*/
|
|
|
|
return $check;
|
|
}
|
|
|
|
private function updateImage(){
|
|
if ($this->checkRequestType('PUT') == 'false') {
|
|
return $this->unprocessableRequestResponse("Request type is not PUT");
|
|
}
|
|
|
|
return $this->unprocessableRequestResponse("updateImage is not implemented");
|
|
}
|
|
|
|
private function deleteImage(){
|
|
if ($this->checkRequestType('DELETE') == 'false') {
|
|
return $this->unprocessableRequestResponse("Request type is not DELETE");
|
|
}
|
|
|
|
return $this->unprocessableRequestResponse("deleteImage is not implemented");
|
|
}
|
|
|
|
private function blobImage(){
|
|
if ($this->checkRequestType('GET') == 'false') {
|
|
return $this->unprocessableRequestResponse("Request type is not GET");
|
|
}
|
|
|
|
return $this->unprocessableRequestResponse("blobImage is not implemented");
|
|
}
|
|
|
|
private function validateImage(){
|
|
$validtion = false;
|
|
if(in_array($this->imageModel->fileType, $this->imageModel->allowFileType)){
|
|
$validtion = true;
|
|
}
|
|
return $validtion;
|
|
}
|
|
}
|