Format code using php-cs-fixer

This commit is contained in:
jo 2021-10-11 16:10:47 +02:00
parent 43d7dc92cd
commit d52c6184b9
352 changed files with 17473 additions and 17041 deletions

View file

@ -11,15 +11,13 @@ class Rest_MediaController extends Zend_Rest_Controller
}
/**
* headAction is needed as it is defined as an abstract function in the base controller
*
* @return void
* headAction is needed as it is defined as an abstract function in the base controller.
*/
public function headAction()
{
Logging::info("HEAD action received");
Logging::info('HEAD action received');
}
public function indexAction()
{
$totalFileCount = CcFilesQuery::create()->count();
@ -39,29 +37,29 @@ class Rest_MediaController extends Zend_Rest_Controller
->filterByDbImportStatus(0)
->setLimit($limit)
->setOffset($offset)
->orderBy($sortColumn, $sortDir);
//->orderByDbId();
->orderBy($sortColumn, $sortDir)
;
//->orderByDbId();
$queryCount = $query->count();
$queryResult = $query->find();
$files_array = array();
foreach ($queryResult as $file)
{
$files_array = [];
foreach ($queryResult as $file) {
array_push($files_array, CcFiles::sanitizeResponse($file));
}
$this->getResponse()
->setHttpResponseCode(200)
->setHeader('X-TOTAL-COUNT', $totalFileCount)
->appendBody(json_encode($files_array));
/** TODO: Use this simpler code instead after we upgrade to Propel 1.7 (Airtime 2.6.x branch):
$this->getResponse()
->setHttpResponseCode(200)
->appendBody(json_encode(CcFilesQuery::create()->find()->toArray(BasePeer::TYPE_FIELDNAME)));
*/
->appendBody(json_encode($files_array))
;
/* TODO: Use this simpler code instead after we upgrade to Propel 1.7 (Airtime 2.6.x branch):
* $this->getResponse()
* ->setHttpResponseCode(200)
* ->appendBody(json_encode(CcFilesQuery::create()->find()->toArray(BasePeer::TYPE_FIELDNAME)));
*/
}
public function downloadAction()
@ -73,28 +71,30 @@ class Rest_MediaController extends Zend_Rest_Controller
// In case the download fails
$counterIncremented = false;
try {
$this->getResponse()
->setHttpResponseCode(200);
->setHttpResponseCode(200)
;
$inline = false;
// SAAS-1081 - download counter for station podcast downloads
if ($key = $this->getRequest()->getParam("download_key", false)) {
if ($key = $this->getRequest()->getParam('download_key', false)) {
Application_Model_Preference::incrementStationPodcastDownloadCounter();
$counterIncremented = true;
}
Application_Service_MediaService::streamFileDownload($id, $inline);
}
catch (LibreTimeFileNotFoundException $e) {
} catch (LibreTimeFileNotFoundException $e) {
$this->fileNotFoundResponse();
Logging::error($e->getMessage());
}
catch (Exception $e) {
if ($counterIncremented) Application_Model_Preference::decrementStationPodcastDownloadCounter();
} catch (Exception $e) {
if ($counterIncremented) {
Application_Model_Preference::decrementStationPodcastDownloadCounter();
}
$this->unknownErrorResponse();
Logging::error($e->getMessage());
}
}
public function getAction()
{
$id = $this->getId();
@ -105,18 +105,17 @@ class Rest_MediaController extends Zend_Rest_Controller
try {
$this->getResponse()
->setHttpResponseCode(200)
->appendBody(json_encode(CcFiles::getSanitizedFileById($id)));
}
catch (LibreTimeFileNotFoundException $e) {
->appendBody(json_encode(CcFiles::getSanitizedFileById($id)))
;
} catch (LibreTimeFileNotFoundException $e) {
$this->fileNotFoundResponse();
Logging::error($e->getMessage());
}
catch (Exception $e) {
} catch (Exception $e) {
$this->unknownErrorResponse();
Logging::error($e->getMessage());
}
}
public function postAction()
{
//If we do get an ID on a POST, then that doesn't make any sense
@ -124,7 +123,8 @@ class Rest_MediaController extends Zend_Rest_Controller
if ($id = $this->_getParam('id', false)) {
$resp = $this->getResponse();
$resp->setHttpResponseCode(400);
$resp->appendBody("ERROR: ID should not be specified when using POST. POST is only used for file creation, and an ID will be chosen by Airtime");
$resp->appendBody('ERROR: ID should not be specified when using POST. POST is only used for file creation, and an ID will be chosen by Airtime');
return;
}
@ -135,7 +135,7 @@ class Rest_MediaController extends Zend_Rest_Controller
// this error should not really get hit, letting the user know if it does is nice for debugging
// see: https://github.com/LibreTime/libretime/issues/3#issuecomment-281143417
if (!$upload->isValid('file')) {
throw new Exception("invalid file uploaded");
throw new Exception('invalid file uploaded');
}
$fileInfo = $upload->getFileInfo('file');
// this should have more info on any actual faults detected by php
@ -145,18 +145,17 @@ class Rest_MediaController extends Zend_Rest_Controller
$sanitizedFile = CcFiles::createFromUpload($fileInfo);
$this->getResponse()
->setHttpResponseCode(201)
->appendBody(json_encode($sanitizedFile));
}
catch (InvalidMetadataException $e) {
->appendBody(json_encode($sanitizedFile))
;
} catch (InvalidMetadataException $e) {
$this->invalidDataResponse();
Logging::error($e->getMessage());
}
catch (OverDiskQuotaException $e) {
} catch (OverDiskQuotaException $e) {
$this->getResponse()
->setHttpResponseCode(400)
->appendBody("ERROR: Disk Quota reached.");
}
catch (Exception $e) {
->appendBody('ERROR: Disk Quota reached.')
;
} catch (Exception $e) {
$this->serviceUnavailableResponse();
Logging::error($e->getMessage() . "\n" . $e->getTraceAsString());
}
@ -175,17 +174,15 @@ class Rest_MediaController extends Zend_Rest_Controller
$this->getResponse()
->setHttpResponseCode(201)
->appendBody(json_encode($sanitizedFile));
}
catch (InvalidMetadataException $e) {
->appendBody(json_encode($sanitizedFile))
;
} catch (InvalidMetadataException $e) {
$this->invalidDataResponse();
Logging::error($e->getMessage());
}
catch (LibreTimeFileNotFoundException $e) {
} catch (LibreTimeFileNotFoundException $e) {
$this->fileNotFoundResponse();
Logging::error($e->getMessage());
}
catch (Exception $e) {
} catch (Exception $e) {
$this->unknownErrorResponse();
Logging::error($e->getMessage());
}
@ -197,45 +194,49 @@ class Rest_MediaController extends Zend_Rest_Controller
if (!$id) {
return;
}
try {
CcFiles::deleteById($id);
$this->getResponse()
->setHttpResponseCode(204);
}
catch (LibreTimeFileNotFoundException $e) {
->setHttpResponseCode(204)
;
} catch (LibreTimeFileNotFoundException $e) {
$this->fileNotFoundResponse();
Logging::error($e->getMessage());
}
catch (Exception $e) {
$this->unknownErrorResponse();
Logging::error($e->getMessage());
}
}
/**
* Publish endpoint for individual media items
*/
public function publishAction() {
$id = $this->getId();
try {
// Is there a better way to do this?
$data = json_decode($this->getRequest()->getRawBody(), true)["sources"];
Application_Service_PublishService::publish($id, $data);
$this->getResponse()
->setHttpResponseCode(200);
} catch (Exception $e) {
$this->unknownErrorResponse();
Logging::error($e->getMessage());
}
}
public function publishSourcesAction() {
/**
* Publish endpoint for individual media items.
*/
public function publishAction()
{
$id = $this->getId();
try {
// Is there a better way to do this?
$data = json_decode($this->getRequest()->getRawBody(), true)['sources'];
Application_Service_PublishService::publish($id, $data);
$this->getResponse()
->setHttpResponseCode(200)
;
} catch (Exception $e) {
$this->unknownErrorResponse();
Logging::error($e->getMessage());
}
}
public function publishSourcesAction()
{
$id = $this->_getParam('id', false);
$sources = Application_Service_PublishService::getSourceLists($id);
$this->getResponse()
->setHttpResponseCode(200)
->appendBody(json_encode($sources));
->appendBody(json_encode($sources))
;
}
private function getId()
@ -243,9 +244,11 @@ class Rest_MediaController extends Zend_Rest_Controller
if (!$id = $this->_getParam('id', false)) {
$resp = $this->getResponse();
$resp->setHttpResponseCode(400);
$resp->appendBody("ERROR: No file ID specified.");
$resp->appendBody('ERROR: No file ID specified.');
return false;
}
}
return $id;
}
@ -253,28 +256,27 @@ class Rest_MediaController extends Zend_Rest_Controller
{
$resp = $this->getResponse();
$resp->setHttpResponseCode(404);
$resp->appendBody("ERROR: Media not found.");
$resp->appendBody('ERROR: Media not found.');
}
private function importFailedResponse()
{
$resp = $this->getResponse();
$resp->setHttpResponseCode(200);
$resp->appendBody("ERROR: Import Failed.");
$resp->appendBody('ERROR: Import Failed.');
}
private function unknownErrorResponse()
{
$resp = $this->getResponse();
$resp->setHttpResponseCode(400);
$resp->appendBody("An unknown error occurred.");
$resp->appendBody('An unknown error occurred.');
}
private function serviceUnavailableResponse()
{
$resp = $this->getResponse();
$resp->setHttpResponseCode(400);
$resp->appendBody("An error occurred while processing your upload. Please try again in a few minutes.");
$resp->appendBody('An error occurred while processing your upload. Please try again in a few minutes.');
}
}

View file

@ -2,7 +2,6 @@
class Rest_PodcastController extends Zend_Rest_Controller
{
public function init()
{
$this->view->layout()->disableLayout();
@ -13,13 +12,11 @@ class Rest_PodcastController extends Zend_Rest_Controller
}
/**
* headAction is needed as it is defined as an abstract function in the base controller
*
* @return void
* headAction is needed as it is defined as an abstract function in the base controller.
*/
public function headAction()
{
Logging::info("HEAD action received");
Logging::info('HEAD action received');
}
public function indexAction()
@ -38,20 +35,24 @@ class Rest_PodcastController extends Zend_Rest_Controller
// Don't return the Station podcast - we fetch it separately
->filterByDbId($stationPodcastId, Criteria::NOT_EQUAL)
->leftJoinImportedPodcast()
->withColumn('auto_ingest_timestamp');
->withColumn('auto_ingest_timestamp')
;
$total = $result->count();
if ($limit > 0) { $result->setLimit($limit); }
if ($limit > 0) {
$result->setLimit($limit);
}
$result->setOffset($offset)
->orderBy($sortColumn, $sortDir);
->orderBy($sortColumn, $sortDir)
;
$result = $result->find();
$podcastArray = $result->toArray(null, false, BasePeer::TYPE_FIELDNAME);
$this->getResponse()
->setHttpResponseCode(200)
->setHeader('X-TOTAL-COUNT', $total)
->appendBody(json_encode($podcastArray));
->appendBody(json_encode($podcastArray))
;
}
public function getAction()
@ -64,12 +65,12 @@ class Rest_PodcastController extends Zend_Rest_Controller
try {
$this->getResponse()
->setHttpResponseCode(200)
->appendBody(json_encode(Application_Service_PodcastService::getPodcastById($id)));
->appendBody(json_encode(Application_Service_PodcastService::getPodcastById($id)))
;
} catch (PodcastNotFoundException $e) {
$this->podcastNotFoundResponse();
Logging::error($e->getMessage());
} catch (Exception $e) {
}
}
@ -80,28 +81,28 @@ class Rest_PodcastController extends Zend_Rest_Controller
if ($id = $this->_getParam('id', false)) {
$resp = $this->getResponse();
$resp->setHttpResponseCode(400);
$resp->appendBody("ERROR: ID should not be specified when using POST. POST is only used for podcast creation, and an ID will be chosen by Airtime");
$resp->appendBody('ERROR: ID should not be specified when using POST. POST is only used for podcast creation, and an ID will be chosen by Airtime');
return;
}
try {
$requestData = $this->getRequest()->getPost();
$podcast = Application_Service_PodcastService::createFromFeedUrl($requestData["url"]);
$podcast = Application_Service_PodcastService::createFromFeedUrl($requestData['url']);
$path = 'podcast/podcast.phtml';
$this->view->podcast = $podcast;
$this->_helper->json->sendJson(array(
"podcast"=>json_encode($podcast),
"html"=>$this->view->render($path),
));
}
catch (InvalidPodcastException $e) {
$this->_helper->json->sendJson([
'podcast' => json_encode($podcast),
'html' => $this->view->render($path),
]);
} catch (InvalidPodcastException $e) {
$this->getResponse()
->setHttpResponseCode(400)
->appendBody("Invalid podcast!");
}
catch (Exception $e) {
->appendBody('Invalid podcast!')
;
} catch (Exception $e) {
Logging::error($e->getMessage());
$this->unknownErrorResponse();
}
@ -120,13 +121,12 @@ class Rest_PodcastController extends Zend_Rest_Controller
$this->getResponse()
->setHttpResponseCode(201)
->appendBody(json_encode($podcast));
}
catch (PodcastNotFoundException $e) {
->appendBody(json_encode($podcast))
;
} catch (PodcastNotFoundException $e) {
$this->podcastNotFoundResponse();
Logging::error($e->getMessage());
}
catch (Exception $e) {
} catch (Exception $e) {
$this->unknownErrorResponse();
Logging::error($e->getMessage());
}
@ -142,26 +142,28 @@ class Rest_PodcastController extends Zend_Rest_Controller
try {
Application_Service_PodcastService::deletePodcastById($id);
$this->getResponse()
->setHttpResponseCode(204);
}
catch (PodcastNotFoundException $e) {
->setHttpResponseCode(204)
;
} catch (PodcastNotFoundException $e) {
$this->podcastNotFoundResponse();
Logging::error($e->getMessage());
}
catch (Exception $e) {
} catch (Exception $e) {
$this->unknownErrorResponse();
Logging::error($e->getMessage());
}
}
/**
* Endpoint for performing bulk actions (deleting multiple podcasts, opening multiple editors)
* Endpoint for performing bulk actions (deleting multiple podcasts, opening multiple editors).
*/
public function bulkAction() {
public function bulkAction()
{
if ($this->_request->getMethod() != HttpRequestType::POST) {
$this->getResponse()
->setHttpResponseCode(405)
->appendBody("ERROR: Method not accepted");
->appendBody('ERROR: Method not accepted')
;
return;
}
@ -170,33 +172,34 @@ class Rest_PodcastController extends Zend_Rest_Controller
$responseBody = [];
// XXX: Should this be a map of HttpRequestType => function call instead? Would be a bit cleaner
switch($method) {
switch ($method) {
case HttpRequestType::DELETE:
foreach($ids as $id) {
foreach ($ids as $id) {
Application_Service_PodcastService::deletePodcastById($id);
}
break;
case HttpRequestType::GET:
$path = 'podcast/podcast.phtml';
foreach($ids as $id) {
$responseBody[] = array(
"podcast" => json_encode(Application_Service_PodcastService::getPodcastById($id)),
"html" => $this->view->render($path)
);
foreach ($ids as $id) {
$responseBody[] = [
'podcast' => json_encode(Application_Service_PodcastService::getPodcastById($id)),
'html' => $this->view->render($path),
];
}
break;
}
$this->_helper->json->sendJson($responseBody);
}
/**
* Endpoint for triggering the generation of a smartblock and playlist to match the podcast name
* Endpoint for triggering the generation of a smartblock and playlist to match the podcast name.
*/
public function smartblockAction() {
public function smartblockAction()
{
$title = $this->_getParam('title', []);
$id = $this->_getParam('id', []);
if (!$id) {
@ -208,22 +211,21 @@ class Rest_PodcastController extends Zend_Rest_Controller
Application_Service_PodcastService::createPodcastSmartblockAndPlaylist($podcast, $title);
}
/**
* @throws PodcastNotFoundException
*
* @deprecated
*/
public function stationAction() {
public function stationAction()
{
$stationPodcastId = Application_Model_Preference::getStationPodcastId();
$podcast = Application_Service_PodcastService::getPodcastById($stationPodcastId);
$path = 'podcast/station.phtml';
$this->view->podcast = $podcast;
$this->_helper->json->sendJson(array(
"podcast" => json_encode($podcast),
"html" => $this->view->render($path)
));
$this->_helper->json->sendJson([
'podcast' => json_encode($podcast),
'html' => $this->view->render($path),
]);
}
private function getId()
@ -231,9 +233,11 @@ class Rest_PodcastController extends Zend_Rest_Controller
if (!$id = $this->_getParam('id', false)) {
$resp = $this->getResponse();
$resp->setHttpResponseCode(400);
$resp->appendBody("ERROR: No podcast ID specified.");
$resp->appendBody('ERROR: No podcast ID specified.');
return false;
}
return $id;
}
@ -241,14 +245,13 @@ class Rest_PodcastController extends Zend_Rest_Controller
{
$resp = $this->getResponse();
$resp->setHttpResponseCode(500);
$resp->appendBody("An unknown error occurred.");
$resp->appendBody('An unknown error occurred.');
}
private function podcastNotFoundResponse()
{
$resp = $this->getResponse();
$resp->setHttpResponseCode(404);
$resp->appendBody("ERROR: Podcast not found.");
$resp->appendBody('ERROR: Podcast not found.');
}
}

View file

@ -2,7 +2,6 @@
class Rest_PodcastEpisodesController extends Zend_Rest_Controller
{
/**
* @var Application_Service_PodcastEpisodeService
*/
@ -18,13 +17,11 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
}
/**
* headAction is needed as it is defined as an abstract function in the base controller
*
* @return void
* headAction is needed as it is defined as an abstract function in the base controller.
*/
public function headAction()
{
Logging::info("HEAD action received");
Logging::info('HEAD action received');
}
public function indexAction()
@ -34,10 +31,12 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
if (!$id) {
return;
}
try {
$totalPodcastEpisodesCount = PodcastEpisodesQuery::create()
->filterByDbPodcastId($id)
->count();
->count()
;
// Check if offset and limit were sent with request.
// Default limit to zero and offset to $totalFileCount
@ -51,8 +50,8 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
$this->getResponse()
->setHttpResponseCode(201)
->setHeader('X-TOTAL-COUNT', $totalPodcastEpisodesCount)
->appendBody(json_encode($this->_service->getPodcastEpisodes($id, $offset, $limit, $sortColumn, $sortDir)));
->appendBody(json_encode($this->_service->getPodcastEpisodes($id, $offset, $limit, $sortColumn, $sortDir)))
;
} catch (PodcastNotFoundException $e) {
$this->podcastNotFoundResponse();
Logging::error($e->getMessage());
@ -78,8 +77,8 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
try {
$this->getResponse()
->setHttpResponseCode(201)
->appendBody(json_encode($this->_service->getPodcastEpisodeById($episodeId)));
->appendBody(json_encode($this->_service->getPodcastEpisodeById($episodeId)))
;
} catch (PodcastNotFoundException $e) {
$this->podcastNotFoundResponse();
Logging::error($e->getMessage());
@ -99,8 +98,9 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
if ($episodeId = $this->_getParam('episode_id', false)) {
$resp = $this->getResponse();
$resp->setHttpResponseCode(400);
$resp->appendBody("ERROR: Episode ID should not be specified when using POST. POST is only used for "
. "importing podcast episodes, and an episode ID will be chosen by Airtime");
$resp->appendBody('ERROR: Episode ID should not be specified when using POST. POST is only used for '
. 'importing podcast episodes, and an episode ID will be chosen by Airtime');
return;
}
@ -112,16 +112,15 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
try {
$requestData = json_decode($this->getRequest()->getRawBody(), true);
$episode = $this->_service->importEpisode($id, $requestData["episode"]);
$episode = $this->_service->importEpisode($id, $requestData['episode']);
$this->getResponse()
->setHttpResponseCode(201)
->appendBody(json_encode($episode));
->appendBody(json_encode($episode))
;
} catch (Exception $e) {
$this->unknownErrorResponse();
Logging::error($e->getMessage());
}
}
public function deleteAction()
@ -139,7 +138,8 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
try {
$this->_service->deletePodcastEpisodeById($episodeId);
$this->getResponse()
->setHttpResponseCode(204);
->setHttpResponseCode(204)
;
} catch (PodcastEpisodeNotFoundException $e) {
$this->podcastEpisodeNotFoundResponse();
Logging::error($e->getMessage());
@ -151,7 +151,6 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
public function putAction()
{
}
private function getId()
@ -159,9 +158,11 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
if (!$id = $this->_getParam('id', false)) {
$resp = $this->getResponse();
$resp->setHttpResponseCode(400);
$resp->appendBody("ERROR: No podcast ID specified.");
$resp->appendBody('ERROR: No podcast ID specified.');
return false;
}
return $id;
}
@ -170,9 +171,11 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
if (!$episodeId = $this->_getParam('episode_id', false)) {
$resp = $this->getResponse();
$resp->setHttpResponseCode(400);
$resp->appendBody("ERROR: No podcast episode ID specified.");
$resp->appendBody('ERROR: No podcast episode ID specified.');
return false;
}
return $episodeId;
}
@ -180,21 +183,20 @@ class Rest_PodcastEpisodesController extends Zend_Rest_Controller
{
$resp = $this->getResponse();
$resp->setHttpResponseCode(400);
$resp->appendBody("An unknown error occurred.");
$resp->appendBody('An unknown error occurred.');
}
private function podcastNotFoundResponse()
{
$resp = $this->getResponse();
$resp->setHttpResponseCode(404);
$resp->appendBody("ERROR: Podcast not found.");
$resp->appendBody('ERROR: Podcast not found.');
}
private function podcastEpisodeNotFoundResponse()
{
$resp = $this->getResponse();
$resp->setHttpResponseCode(404);
$resp->appendBody("ERROR: Podcast episode not found.");
$resp->appendBody('ERROR: Podcast episode not found.');
}
}

View file

@ -1,64 +1,60 @@
<?php
/**
* Class Rest_RouteController
* Class Rest_RouteController.
*
* Taken from https://github.com/aporat/Application_Rest_Controller_Route
* to enable hierarchy routing
*/
class Rest_RouteController extends Zend_Controller_Router_Route
{
/**
* @var Zend_Controller_Front
*/
protected $_front;
protected $_actionKey = 'action';
protected $_actionKey = 'action';
/**
* Prepares the route for mapping by splitting (exploding) it
* to a corresponding atomic parts. These parts are assigned
* a position which is later used for matching and preparing values.
*
* @param Zend_Controller_Front $front Front Controller object
* @param string $route Map used to match with later submitted URL path
* @param array $defaults Defaults for map variables with keys as variable names
* @param array $reqs Regular expression requirements for variables (keys as variable names)
* @param Zend_Translate $translator Translator to use for this instance
* @param Zend_Controller_Front $front Front Controller object
* @param string $route Map used to match with later submitted URL path
* @param array $defaults Defaults for map variables with keys as variable names
* @param array $reqs Regular expression requirements for variables (keys as variable names)
* @param Zend_Translate $translator Translator to use for this instance
* @param null|mixed $locale
*/
public function __construct(Zend_Controller_Front $front, $route, $defaults = array(), $reqs = array(), Zend_Translate $translator = null, $locale = null)
public function __construct(Zend_Controller_Front $front, $route, $defaults = [], $reqs = [], Zend_Translate $translator = null, $locale = null)
{
$this->_front = $front;
$this->_front = $front;
$this->_dispatcher = $front->getDispatcher();
parent::__construct($route, $defaults, $reqs, $translator, $locale);
}
/**
* Matches a user submitted path with parts defined by a map. Assigns and
* returns an array of variables on a successful match.
*
* @param string $path Path used to match against this routing map
* @param string $path Path used to match against this routing map
* @param mixed $partial
*
* @return array|false An array of assigned values or a false on a mismatch
*/
public function match($path, $partial = false)
{
$return = parent::match($path, $partial);
// add the RESTful action mapping
if ($return) {
$request = $this->_front->getRequest();
$path = $request->getPathInfo();
$path = $request->getPathInfo();
$params = $request->getParams();
$path = trim($path, self::URI_DELIMITER);
$path = trim($path, self::URI_DELIMITER);
if ($path != '') {
$path = explode(self::URI_DELIMITER, $path);
@ -72,7 +68,7 @@ class Rest_RouteController extends Zend_Controller_Router_Route
if ($requestMethod != 'get') {
if ($request->getParam('_method')) {
$return[$this->_actionKey] = strtolower($request->getParam('_method'));
} elseif ( $request->getHeader('X-HTTP-Method-Override') ) {
} elseif ($request->getHeader('X-HTTP-Method-Override')) {
$return[$this->_actionKey] = strtolower($request->getHeader('X-HTTP-Method-Override'));
} else {
$return[$this->_actionKey] = $requestMethod;
@ -80,15 +76,17 @@ class Rest_RouteController extends Zend_Controller_Router_Route
// Map PUT and POST to actual create/update actions
// based on parameter count (posting to resource or collection)
switch( $return[$this->_actionKey] ){
switch ($return[$this->_actionKey]) {
case 'post':
$return[$this->_actionKey] = 'post';
break;
case 'put':
$return[$this->_actionKey] = 'put';
break;
}
} else {
// if the last argument in the path is a numeric value, consider this request a GET of an item
$lastParam = array_pop($path);
@ -98,11 +96,8 @@ class Rest_RouteController extends Zend_Controller_Router_Route
$return[$this->_actionKey] = 'index';
}
}
}
return $return;
}
}
}

View file

@ -7,14 +7,16 @@
* 18/09/2014 : v1.1 Changed auth references to static calls
* 06/02/2015 : v1.2 Changed endpoints to be more RESTful, changed classname to
* better reflect functionality
* 09/02/2015 : v1.2.1 Added more comments
* 09/02/2015 : v1.2.1 Added more comments.
*
* @author sourcefabric
*
* @version 1.2.1
*/
class Rest_ShowImageController extends Zend_Rest_Controller {
public function init() {
class Rest_ShowImageController extends Zend_Rest_Controller
{
public function init()
{
// Remove layout dependencies
$this->view->layout()->disableLayout();
// Remove reliance on .phtml files to render requests
@ -22,53 +24,59 @@ class Rest_ShowImageController extends Zend_Rest_Controller {
}
/**
* headAction is needed as it is defined as an abstract function in the base controller
*
* @return void
* headAction is needed as it is defined as an abstract function in the base controller.
*/
public function headAction()
{
Logging::info("HEAD action received");
Logging::info('HEAD action received');
}
public function indexAction() {
Logging::info("INDEX action received");
public function indexAction()
{
Logging::info('INDEX action received');
}
public function getAction() {
Logging::info("GET action received");
public function getAction()
{
Logging::info('GET action received');
}
public function putAction() {
Logging::info("PUT action received");
public function putAction()
{
Logging::info('PUT action received');
}
/**
* RESTful POST endpoint; used when uploading show images
* RESTful POST endpoint; used when uploading show images.
*/
public function postAction() {
public function postAction()
{
$showId = $this->getShowId();
if (!$showId) {
$this->getResponse()
->setHttpResponseCode(400)
->appendBody("No show ID provided");
->appendBody('No show ID provided')
;
return;
}
try {
$path = $this->processUploadedImage($showId, $_FILES["file"]["tmp_name"]);
$path = $this->processUploadedImage($showId, $_FILES['file']['tmp_name']);
} catch (Exception $e) {
$this->getResponse()
->setHttpResponseCode(500)
->appendBody("Error processing image: " . $e->getMessage());
->appendBody('Error processing image: ' . $e->getMessage())
;
return;
}
$show = CcShowQuery::create()->findPk($showId);
$con = Propel::getConnection();
try {
$con->beginTransaction();
@ -80,24 +88,28 @@ class Rest_ShowImageController extends Zend_Rest_Controller {
$con->rollBack();
$this->getResponse()
->setHttpResponseCode(500)
->appendBody("Couldn't add show image: " . $e->getMessage());
->appendBody("Couldn't add show image: " . $e->getMessage())
;
}
$this->getResponse()
->setHttpResponseCode(201);
->setHttpResponseCode(201)
;
}
/**
* RESTful DELETE endpoint; used when deleting show images
* RESTful DELETE endpoint; used when deleting show images.
*/
public function deleteAction() {
public function deleteAction()
{
$showId = $this->getShowId();
if (!$showId) {
$this->getResponse()
->setHttpResponseCode(400)
->appendBody("No show ID provided");
->appendBody('No show ID provided')
;
return;
}
@ -106,12 +118,14 @@ class Rest_ShowImageController extends Zend_Rest_Controller {
} catch (Exception $e) {
$this->getResponse()
->setHttpResponseCode(500)
->appendBody("Error processing image: " . $e->getMessage());
->appendBody('Error processing image: ' . $e->getMessage())
;
}
$show = CcShowQuery::create()->findPk($showId);
$con = Propel::getConnection();
try {
$con->beginTransaction();
@ -123,114 +137,125 @@ class Rest_ShowImageController extends Zend_Rest_Controller {
$con->rollBack();
$this->getResponse()
->setHttpResponseCode(500)
->appendBody("Couldn't remove show image: " . $e->getMessage());
->appendBody("Couldn't remove show image: " . $e->getMessage())
;
}
$this->getResponse()
->setHttpResponseCode(201);
->setHttpResponseCode(201)
;
}
/**
* Verify and process an uploaded image file, copying it into
* .../stor/imported/:owner-id/show-images/:show-id/ to differentiate between
* individual users and shows
* individual users and shows.
*
* @param int $showId the ID of the show we're adding the image to
* @param string $tempFilePath temporary filepath assigned to the upload generally of the form /tmp/:tmp_name
*
* @throws Exception
* - when a file with an unsupported file extension is uploaded or an
* error occurs in copyFileToStor
* - when a file with an unsupported file extension is uploaded or an
* error occurs in copyFileToStor
*
* @return string the path to the new location for the file
*/
private function processUploadedImage($showId, $tempFilePath) {
private function processUploadedImage($showId, $tempFilePath)
{
$ownerId = RestAuth::getOwnerId();
//Only accept files with a file extension that we support.
$fileExtension = $this->getFileExtension($tempFilePath);
if (!in_array(strtolower($fileExtension), explode(",", "jpg,png,gif,jpeg"))) {
if (!in_array(strtolower($fileExtension), explode(',', 'jpg,png,gif,jpeg'))) {
@unlink($tempFilePath);
throw new Exception("Bad file extension.");
throw new Exception('Bad file extension.');
}
$storDir = Application_Model_MusicDir::getStorDir();
$importedStorageDirectory = $storDir->getDirectory() . "imported/" . $ownerId . "/show-images/" . $showId;
$importedStorageDirectory = $storDir->getDirectory() . 'imported/' . $ownerId . '/show-images/' . $showId;
try {
$importedStorageDirectory = $this->copyFileToStor($tempFilePath, $importedStorageDirectory, $fileExtension);
} catch (Exception $e) {
@unlink($tempFilePath);
throw new Exception("Failed to copy file: " . $e->getMessage());
throw new Exception('Failed to copy file: ' . $e->getMessage());
}
return $importedStorageDirectory;
}
/**
* Check the MIME type of an uploaded file to determine what extension it should have
* Check the MIME type of an uploaded file to determine what extension it should have.
*
* @param $tempFilePath the file path to the uploaded file in /tmp
*
* @return string the file extension for the new file based on its MIME type
*/
private function getFileExtension($tempFilePath) {
private function getFileExtension($tempFilePath)
{
// Don't trust the extension - get the MIME-type instead
$fileInfo = finfo_open();
$mime = finfo_file($fileInfo, $tempFilePath, FILEINFO_MIME_TYPE);
return $this->getExtensionFromMime($mime);
}
/**
* Use a hardcoded list of accepted MIME types to return a file extension
* Use a hardcoded list of accepted MIME types to return a file extension.
*
* @param $mime the MIME type of the file
*
* @return string the file extension based on the given MIME type
*/
private function getExtensionFromMime($mime) {
$extensions = array(
private function getExtensionFromMime($mime)
{
$extensions = [
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/gif' => 'gif'
);
'image/gif' => 'gif',
];
return $extensions[$mime];
}
/**
* Copy a given file in /tmp to the user's stor directory
* Copy a given file in /tmp to the user's stor directory.
*
* @param string $tempFilePath the path to the file in /tmp
* @param string $importedStorageDirectory the path to the new location for the file
* @param string $fileExtension the file's extension based on its MIME type
*
* @return string the new full path to the file in stor
* @throws Exception if either the storage directory does not exist and cannot be
* created, the storage directory does not have write permissions
* enabled, or the user's hard drive does not have enough space to
* store the file
*
* @return string the new full path to the file in stor
*/
private function copyFileToStor($tempFilePath, $importedStorageDirectory, $fileExtension) {
private function copyFileToStor($tempFilePath, $importedStorageDirectory, $fileExtension)
{
$image_file = $tempFilePath;
// check if show image dir exists and if not, create one
if (!file_exists($importedStorageDirectory)) {
if (!mkdir($importedStorageDirectory, 0777, true)) {
throw new Exception("Failed to create storage directory.");
throw new Exception('Failed to create storage directory.');
}
}
if (chmod($image_file, 0644) === false) {
Logging::info("Warning: couldn't change permissions of $image_file to 0644");
Logging::info("Warning: couldn't change permissions of {$image_file} to 0644");
}
$newFileName = substr($tempFilePath, strrpos($tempFilePath, "/")) . "." . $fileExtension;
$newFileName = substr($tempFilePath, strrpos($tempFilePath, '/')) . '.' . $fileExtension;
// Did all the checks for real, now trying to copy
$image_stor = Application_Common_OsPath::join($importedStorageDirectory, $newFileName);
Logging::info("Adding image: " . $image_stor);
Logging::info("copyFileToStor: moving file $image_file to $image_stor");
Logging::info('Adding image: ' . $image_stor);
Logging::info("copyFileToStor: moving file {$image_file} to {$image_stor}");
if (@rename($image_file, $image_stor) === false) {
//something went wrong likely there wasn't enough space in .
@ -239,9 +264,9 @@ class Rest_ShowImageController extends Zend_Rest_Controller {
//is enough disk space .
unlink($image_file); //remove the file after failed rename
throw new Exception("The file was not uploaded, this error can occur if the computer "
. "hard drive does not have enough disk space or the stor "
. "directory does not have correct write permissions.");
throw new Exception('The file was not uploaded, this error can occur if the computer '
. 'hard drive does not have enough disk space or the stor '
. 'directory does not have correct write permissions.');
}
return $image_stor;
@ -249,51 +274,58 @@ class Rest_ShowImageController extends Zend_Rest_Controller {
// Should this be an endpoint instead?
/**
* Delete any images belonging to the show with the given ID
* Delete any images belonging to the show with the given ID.
*
* @param int $showId the ID of the show we're deleting images from
*
* @return bool true if the images were successfully deleted, otherwise false
*/
public static function deleteShowImagesFromStor($showId) {
public static function deleteShowImagesFromStor($showId)
{
$ownerId = RestAuth::getOwnerId();
$storDir = Application_Model_MusicDir::getStorDir();
$importedStorageDirectory = $storDir->getDirectory() . "imported/" . $ownerId . "/show-images/" . $showId;
$importedStorageDirectory = $storDir->getDirectory() . 'imported/' . $ownerId . '/show-images/' . $showId;
Logging::info("Deleting images from " . $importedStorageDirectory);
Logging::info('Deleting images from ' . $importedStorageDirectory);
// to be safe in case image uploading functionality is extended later
if (!file_exists($importedStorageDirectory)) {
Logging::info("No uploaded images for show with id " . $showId);
Logging::info('No uploaded images for show with id ' . $showId);
return true;
} else {
return self::delTree($importedStorageDirectory);
}
return self::delTree($importedStorageDirectory);
}
// from a note @ http://php.net/manual/en/function.rmdir.php
private static function delTree($dir) {
$files = array_diff(scandir($dir), array('.', '..'));
private static function delTree($dir)
{
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
(is_dir("$dir/$file")) ? self::delTree("$dir/$file") : unlink("$dir/$file");
(is_dir("{$dir}/{$file}")) ? self::delTree("{$dir}/{$file}") : unlink("{$dir}/{$file}");
}
return rmdir($dir);
}
/**
* Fetch the id parameter from the request.
* @return boolean|int false if the show id wasn't
* provided, otherwise returns the id
*
* @return bool|int false if the show id wasn't
* provided, otherwise returns the id
*/
private function getShowId() {
private function getShowId()
{
if (!($id = $this->_getParam('id', false))) {
$resp = $this->getResponse();
$resp->setHttpResponseCode(400);
$resp->appendBody("ERROR: No show ID specified.");
$resp->appendBody('ERROR: No show ID specified.');
return false;
}
return $id;
}
}
}