From 7f69b1adf1e823843e33389ac23cac1e0bae78aa Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 10 Feb 2019 09:58:10 +0000 Subject: [PATCH] Bugfix for #726. Fixes onAirLightAction returning 500 error --- .../application/controllers/ApiController.php | 147 +++++++++--------- 1 file changed, 74 insertions(+), 73 deletions(-) diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index bee424f85..8891adc42 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -15,12 +15,12 @@ class ApiController extends Zend_Controller_Action } //Ignore API key and session authentication for these APIs: - $ignoreAuth = array("live-info", - "live-info-v2", - "week-info", - "station-metadata", + $ignoreAuth = array("live-info", + "live-info-v2", + "week-info", + "station-metadata", "station-logo", - "show-history-feed", + "show-history-feed", "item-history-feed", "shows", "show-tracks", @@ -178,6 +178,7 @@ class ApiController extends Zend_Controller_Action //Used by the SaaS monitoring public function onAirLightAction() { + $request = $this->getRequest(); $this->view->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); @@ -188,9 +189,9 @@ class ApiController extends Zend_Controller_Action $range = Application_Model_Schedule::GetPlayOrderRange(); - $isItemCurrentlyScheduled = !is_null($range["current"]) && count($range["currentShow"]) > 0 ? true : false; + $isItemCurrentlyScheduled = !is_null($range["tracks"]["current"]) && count($range["tracks"]["current"]) > 0 ? true : false; - $isCurrentItemPlaying = $range["current"]["media_item_played"] ? true : false; + $isCurrentItemPlaying = $range["tracks"]["current"]["media_item_played"] ? true : false; if ($isItemCurrentlyScheduled || Application_Model_Preference::GetSourceSwitchStatus("live_dj") == "on" || @@ -212,7 +213,7 @@ class ApiController extends Zend_Controller_Action $this->returnJsonOrJsonp($request, $result); } - + /** * Retrieve the currently playing show as well as upcoming shows. * Number of shows returned and the time interval in which to @@ -234,18 +235,18 @@ class ApiController extends Zend_Controller_Action // disable the view and the layout $this->view->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); - + $request = $this->getRequest(); - + $utcTimeNow = gmdate(DEFAULT_TIMESTAMP_FORMAT); $utcTimeEnd = ""; // if empty, getNextShows will use interval instead of end of day - + // default to the station timezone $timezone = Application_Model_Preference::GetDefaultTimezone(); $userDefinedTimezone = strtolower($request->getParam('timezone')); $upcase = false; // only upcase the timezone abbreviations $this->updateTimezone($userDefinedTimezone, $timezone, $upcase); - + $type = $request->getParam('type'); $limit = $request->getParam('limit'); if ($limit == "" || !is_numeric($limit)) { @@ -255,12 +256,12 @@ class ApiController extends Zend_Controller_Action * we are using two entirely different codepaths for very similar functionality (type = endofday * vs type = interval). Needs to be fixed for 2.3 - MK */ if ($type == "endofday") { - + // make getNextShows use end of day $end = Application_Common_DateHelper::getTodayStationEndDateTime(); $end->setTimezone(new DateTimeZone("UTC")); $utcTimeEnd = $end->format(DEFAULT_TIMESTAMP_FORMAT); - + $result = array( "env" => APPLICATION_ENV, "schedulerTime" => $utcTimeNow, @@ -282,7 +283,7 @@ class ApiController extends Zend_Controller_Action array("starts", "ends", "start_timestamp","end_timestamp"), $timezone ); - + //Convert the UTC scheduler time ("now") to the user-defined timezone. $result["schedulerTime"] = Application_Common_DateHelper::UTCStringToTimezoneString($result["schedulerTime"], $timezone); $result["timezone"] = $upcase ? strtoupper($timezone) : $timezone; @@ -326,13 +327,13 @@ class ApiController extends Zend_Controller_Action $this->_helper->viewRenderer->setNoRender(true); $request = $this->getRequest(); - + // default to the station timezone $timezone = Application_Model_Preference::GetDefaultTimezone(); $userDefinedTimezone = strtolower($request->getParam('timezone')); $upcase = false; // only upcase the timezone abbreviations $this->updateTimezone($userDefinedTimezone, $timezone, $upcase); - + $daysToRetrieve = $request->getParam('days'); $showsToRetrieve = $request->getParam('shows'); if ($daysToRetrieve == "" || !is_numeric($daysToRetrieve)) { @@ -341,7 +342,7 @@ class ApiController extends Zend_Controller_Action if ($showsToRetrieve == "" || !is_numeric($showsToRetrieve)) { $showsToRetrieve = self::DEFAULT_SHOWS_TO_RETRIEVE; } - + // set the end time to the day's start n days from now. // days=1 will return shows until the end of the current day, // days=2 will return shows until the end of tomorrow, etc. @@ -350,7 +351,7 @@ class ApiController extends Zend_Controller_Action $utcTimeEnd = $end->format(DEFAULT_TIMESTAMP_FORMAT); $result = Application_Model_Schedule::GetPlayOrderRange($utcTimeEnd, $showsToRetrieve); - + // apply user-defined timezone, or default to station $this->applyLiveTimezoneAdjustments($result, $timezone, $upcase); @@ -359,7 +360,7 @@ class ApiController extends Zend_Controller_Action // convert image paths to point to api endpoints WidgetHelper::findAndConvertPaths($result); - + // used by caller to determine if the airtime they are running or widgets in use is out of date. $result["station"]["AIRTIME_API_VERSION"] = AIRTIME_API_VERSION; @@ -370,12 +371,12 @@ class ApiController extends Zend_Controller_Action exit; } } - + /** - * Check that the value for the timezone the user gave is valid. - * If it is, override the default (station) timezone. + * Check that the value for the timezone the user gave is valid. + * If it is, override the default (station) timezone. * If it's an abbreviation (pst, edt) we upcase the output. - * + * * @param string $userDefinedTimezone the requested timezone value * @param string $timezone the default timezone * @param boolean $upcase whether the timezone output should be upcased @@ -396,28 +397,28 @@ class ApiController extends Zend_Controller_Action $timezone = $userDefinedTimezone; } } - + /** - * If the user passed in a timezone parameter, adjust timezone-dependent + * If the user passed in a timezone parameter, adjust timezone-dependent * variables in the result to reflect the given timezone. - * + * * @param array $result reference to the object to send back to the user * @param string $timezone the user's timezone parameter value * @param boolean $upcase whether the timezone output should be upcased */ - private function applyLiveTimezoneAdjustments(&$result, $timezone, $upcase) + private function applyLiveTimezoneAdjustments(&$result, $timezone, $upcase) { Application_Common_DateHelper::convertTimestampsToTimezone( $result, array("starts", "ends", "start_timestamp","end_timestamp"), $timezone ); - + //Convert the UTC scheduler time ("now") to the user-defined timezone. $result["station"]["schedulerTime"] = Application_Common_DateHelper::UTCStringToTimezoneString($result["station"]["schedulerTime"], $timezone); $result["station"]["timezone"] = $upcase ? strtoupper($timezone) : $timezone; } - + public function weekInfoAction() { if (Application_Model_Preference::GetAllow3rdPartyApi() || $this->checkAuth()) { @@ -438,11 +439,11 @@ class ApiController extends Zend_Controller_Action exit; } } - + /** * API endpoint to display the show logo */ - public function showLogoAction() + public function showLogoAction() { // Disable the view and the layout $this->view->layout()->disableLayout(); @@ -459,7 +460,7 @@ class ApiController extends Zend_Controller_Action if (empty($show)) { throw new ZendActionHttpException($this, 400, "ERROR: No show with ID $showId exists."); } - + $path = $show->getDbImagePath(); try { $mime_type = mime_content_type($path); @@ -488,9 +489,9 @@ class ApiController extends Zend_Controller_Action header('HTTP/1.0 401 Unauthorized'); print _('You are not allowed to access this resource. '); exit; - } + } } - + /** * API endpoint to provide station metadata */ @@ -500,18 +501,18 @@ class ApiController extends Zend_Controller_Action // disable the view and the layout $this->view->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); - + $CC_CONFIG = Config::getConfig(); $baseDir = Application_Common_OsPath::formatDirectoryWithDirectorySeparators($CC_CONFIG['baseDir']); $path = 'http://'.$_SERVER['HTTP_HOST'].$baseDir."api/station-logo"; - + $result["name"] = Application_Model_Preference::GetStationName(); $result["logo"] = $path; $result["description"] = Application_Model_Preference::GetStationDescription(); $result["timezone"] = Application_Model_Preference::GetDefaultTimezone(); $result["locale"] = Application_Model_Preference::GetDefaultLocale(); $result["stream_data"] = Application_Model_StreamSetting::getEnabledStreamData(); - + // used by caller to determine if the airtime they are running or widgets in use is out of date. $result['AIRTIME_API_VERSION'] = AIRTIME_API_VERSION; @@ -526,27 +527,27 @@ class ApiController extends Zend_Controller_Action /** * API endpoint to display the current station logo */ - public function stationLogoAction() + public function stationLogoAction() { if (Application_Model_Preference::GetAllow3rdPartyApi() || $this->checkAuth()) { // disable the view and the layout $this->view->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); - + $logo = Application_Model_Preference::GetStationLogo(); // if there's no logo, just die - redirects to a 404 if (!$logo || $logo === '') { return; } - + // we're passing this as an image instead of using it in a data uri, so decode it $blob = base64_decode($logo); - + // use finfo to get the mimetype from the decoded blob $f = finfo_open(); $mime_type = finfo_buffer($f, $blob, FILEINFO_MIME_TYPE); finfo_close($f); - + header("Content-Type: " . $mime_type); echo $blob; } else { @@ -555,7 +556,7 @@ class ApiController extends Zend_Controller_Action exit; } } - + public function scheduleAction() { $this->view->layout()->disableLayout(); @@ -571,13 +572,13 @@ class ApiController extends Zend_Controller_Action public function notifyMediaItemStartPlayAction() { $media_id = $this->_getParam("media_id"); - + // We send a fake media id when playing on-demand ads; // in this case, simply return if ($media_id === '0' || $media_id === '-1') { return; } - + Logging::debug("Received notification of new media item start: $media_id"); Application_Model_Schedule::UpdateMediaPlayedStatus($media_id); @@ -619,7 +620,7 @@ class ApiController extends Zend_Controller_Action $this->_helper->json->sendJson(array("status"=>1, "message"=>"")); } - + public function recordedShowsAction() { $utcTimezone = new DateTimeZone("UTC"); @@ -637,7 +638,7 @@ class ApiController extends Zend_Controller_Action $this->view->server_timezone = Application_Model_Preference::GetDefaultTimezone(); $rows = Application_Model_Show::getCurrentShow(); - + if (count($rows) > 0) { $this->view->is_recording = ($rows[0]['record'] == 1); } @@ -664,7 +665,7 @@ class ApiController extends Zend_Controller_Action try { $show_inst = new Application_Model_ShowInstance($show_instance_id); $show_inst->setRecordedFile($file_id); - } + } catch (Exception $e) { //we've reached here probably because the show was //cancelled, and therefore the show instance does not exist @@ -717,7 +718,7 @@ class ApiController extends Zend_Controller_Action if ($md['is_record'] != 0) { $this->uploadRecordedActionParam($md['MDATA_KEY_TRACKNUMBER'], $file->getId()); } - + } elseif ($mode == "modify") { $filepath = $md['MDATA_KEY_FILEPATH']; $file = Application_Model_StoredFile::RecallByFilepath($filepath, $con); @@ -830,7 +831,7 @@ class ApiController extends Zend_Controller_Action } catch (Exception $e) { Logging::warn($e->getMessage()); Logging::warn(gettype($e)); - } + } // We tack on the 'key' back to every request in case the would like to associate // his requests with particular responses $response['key'] = $k; @@ -1085,7 +1086,7 @@ class ApiController extends Zend_Controller_Action } elseif ($djtype == "dj") { //check against show dj auth $showInfo = Application_Model_Show::getCurrentShow(); - + // there is current playing show if (isset($showInfo[0]['id'])) { $current_show_id = $showInfo[0]['id']; @@ -1139,12 +1140,12 @@ class ApiController extends Zend_Controller_Action $this->_helper->json->sendJson($rows); } - + public function getFilesWithoutSilanValueAction() { //connect to db and get get sql $rows = Application_Model_StoredFile::getAllFilesWithoutSilan(); - + $this->_helper->json->sendJson($rows); } @@ -1163,7 +1164,7 @@ class ApiController extends Zend_Controller_Action $this->_helper->json->sendJson(array()); } - + public function updateCueValuesBySilanAction() { $request = $this->getRequest(); @@ -1213,12 +1214,12 @@ class ApiController extends Zend_Controller_Action $data = $request->getParam("data"); $media_id = intval($request->getParam("media_id")); $data_arr = json_decode($data); - + //$media_id is -1 sometimes when a stream has stopped playing if (!is_null($media_id) && $media_id > 0) { if (isset($data_arr->title)) { - + $data_title = substr($data_arr->title, 0, 1024); $previous_metadata = CcWebstreamMetadataQuery::create() @@ -1235,20 +1236,20 @@ class ApiController extends Zend_Controller_Action } if ($do_insert) { - + $startDT = new DateTime("now", new DateTimeZone("UTC")); - + $webstream_metadata = new CcWebstreamMetadata(); $webstream_metadata->setDbInstanceId($media_id); $webstream_metadata->setDbStartTime($startDT); $webstream_metadata->setDbLiquidsoapData($data_title); $webstream_metadata->save(); - + $historyService = new Application_Service_HistoryService(); $historyService->insertWebstreamMetadata($media_id, $startDT, $data_arr); } } - } + } $this->view->response = $data; $this->view->media_id = $media_id; @@ -1271,11 +1272,11 @@ class ApiController extends Zend_Controller_Action Application_Model_ListenerStat::insertDataPoints($data); $this->view->data = $data; } - + public function updateStreamSettingTableAction() { $request = $this->getRequest(); $data = json_decode($request->getParam("data"), true); - + foreach ($data as $k=>$v) { Application_Model_StreamSetting::SetListenerStatError($k, $v); } @@ -1317,9 +1318,9 @@ class ApiController extends Zend_Controller_Action $request = $this->getRequest(); $params = $request->getParams(); $userId = $request->getParam("user_id", null); - + list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($request); - + $historyService = new Application_Service_HistoryService(); $shows = $historyService->getShowList($startsDT, $endsDT, $userId); @@ -1343,8 +1344,8 @@ class ApiController extends Zend_Controller_Action $params = $request->getParams(); $showId = $request->getParam("show_id", null); $results = array(); - - if (empty($showId)) { + + if (empty($showId)) { $shows = CcShowQuery::create()->find(); foreach($shows as $show) { $results[] = $show->getShowInfo(); @@ -1361,19 +1362,19 @@ class ApiController extends Zend_Controller_Action Logging::info($e->getMessage()); } } - + /** * display show schedule for given show_id * * @return json array */ - public function showSchedulesAction() + public function showSchedulesAction() { try { $request = $this->getRequest(); $params = $request->getParams(); $showId = $request->getParam("show_id", null); - + list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($request); if ((!isset($showId)) || (!is_numeric($showId))) { @@ -1382,7 +1383,7 @@ class ApiController extends Zend_Controller_Action array("jsonrpc" => "2.0", "error" => array("code" => 400, "message" => "missing invalid type for required show_id parameter. use type int.".$showId)) ); } - + $shows = Application_Model_Show::getShows($startsDT, $endsDT, FALSE, $showId); // is this a valid show? @@ -1400,7 +1401,7 @@ class ApiController extends Zend_Controller_Action } } - + /** * displays track listing for given instance_id * @@ -1421,7 +1422,7 @@ class ApiController extends Zend_Controller_Action $showInstance = new Application_Model_ShowInstance($instanceId); $showInstanceContent = $showInstance->getShowListContent($prefTimezone); - + // is this a valid show instance with content? if (empty($showInstanceContent)) { $this->_helper->json->sendJson(