From ea4567f3734c685e1b1ee2073e6dd581eb613d35 Mon Sep 17 00:00:00 2001 From: Martin Konecny Date: Wed, 22 Aug 2012 13:59:37 -0400 Subject: [PATCH 1/5] Make user functions work more consistently isHost didn't work similar to isPM or isAdmin --- .../controllers/ScheduleController.php | 8 +++---- airtime_mvc/application/models/User.php | 21 +++++++++++-------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/airtime_mvc/application/controllers/ScheduleController.php b/airtime_mvc/application/controllers/ScheduleController.php index 83ee7cf91..5494d6d0b 100644 --- a/airtime_mvc/application/controllers/ScheduleController.php +++ b/airtime_mvc/application/controllers/ScheduleController.php @@ -238,7 +238,7 @@ class ScheduleController extends Zend_Controller_Action } $isAdminOrPM = $user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER)); - $isDJ = $user->isHost($instance->getShowId()); + $isDJ = $user->isHostOfShow($instance->getShowId()); $showStartLocalDT = Application_Common_DateHelper::ConvertToLocalDateTime($instance->getShowInstanceStart()); $showEndLocalDT = Application_Common_DateHelper::ConvertToLocalDateTime($instance->getShowInstanceEnd()); @@ -338,7 +338,7 @@ class ScheduleController extends Zend_Controller_Action return false; } - if($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER, UTYPE_HOST),$show->getShowId())) + if($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER) && $user->isHostOfShow($show->getShowId()))) $show->clearShow(); } @@ -403,7 +403,7 @@ class ScheduleController extends Zend_Controller_Action return false; } - if ($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER, UTYPE_HOST),$show->getShowId())) { + if ($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER) && $user->isHostOfShow($show->getShowId()))) { $show->removeGroupFromShow($group_id); } @@ -543,7 +543,7 @@ class ScheduleController extends Zend_Controller_Action } $isAdminOrPM = $user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER)); - $isDJ = $user->isHost($showInstance->getShowId()); + $isDJ = $user->isHostOfShow($showInstance->getShowId()); if (!($isAdminOrPM || $isDJ)) { return; diff --git a/airtime_mvc/application/models/User.php b/airtime_mvc/application/models/User.php index ffb137ad9..9788f7641 100644 --- a/airtime_mvc/application/models/User.php +++ b/airtime_mvc/application/models/User.php @@ -32,9 +32,17 @@ class Application_Model_User return $this->getType() == UTYPE_GUEST; } + public function isHostOfShow($showId) + { + $userId = $this->_userInstance->getDbId(); + return CcShowHostsQuery::create() + ->filterByDbShow($showId) + ->filterByDbHost($userId)->count() > 0; + } + public function isHost($showId) { - return $this->isUserType(UTYPE_HOST, $showId); + return $this->isUserType(UTYPE_HOST); } public function isPM() @@ -61,7 +69,7 @@ class Application_Model_User return $result; } - public function isUserType($type, $showId='') + public function isUserType($type) { if (is_array($type)) { $result = false; @@ -71,10 +79,7 @@ class Application_Model_User $result = $this->_userInstance->getDbType() === 'A'; break; case UTYPE_HOST: - $userId = $this->_userInstance->getDbId(); - $result = CcShowHostsQuery::create() - ->filterByDbShow($showId) - ->filterByDbHost($userId)->count() > 0; + $result = $this->_userInstance->getDbType() === 'H'; break; case UTYPE_PROGRAM_MANAGER: $result = $this->_userInstance->getDbType() === 'P'; @@ -89,9 +94,7 @@ class Application_Model_User case UTYPE_ADMIN: return $this->_userInstance->getDbType() === 'A'; case UTYPE_HOST: - $userId = $this->_userInstance->getDbId(); - - return CcShowHostsQuery::create()->filterByDbShow($showId)->filterByDbHost($userId)->count() > 0; + return $this->_userInstance->getDbId() === 'H'; case UTYPE_PROGRAM_MANAGER: return $this->_userInstance->getDbType() === 'P'; } From cef3ffb07b6029cb9a0e47f585d0d0927afec242 Mon Sep 17 00:00:00 2001 From: Martin Konecny Date: Wed, 22 Aug 2012 14:01:44 -0400 Subject: [PATCH 2/5] CC-4265: Keep track of how much memory each request is taking and log if over a threshold -fixed --- .../application/controllers/plugins/RabbitMqPlugin.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/airtime_mvc/application/controllers/plugins/RabbitMqPlugin.php b/airtime_mvc/application/controllers/plugins/RabbitMqPlugin.php index ad3042e53..c1ea6b1ba 100644 --- a/airtime_mvc/application/controllers/plugins/RabbitMqPlugin.php +++ b/airtime_mvc/application/controllers/plugins/RabbitMqPlugin.php @@ -11,5 +11,13 @@ class RabbitMqPlugin extends Zend_Controller_Plugin_Abstract Application_Model_RabbitMq::SendMessageToShowRecorder("update_recorder_schedule"); } } + + if (memory_get_peak_usage() > 25*pow(2, 20)) { + + Logging::debug("Peak memory usage: " + .(memory_get_peak_usage()/1000000) + ." MB while accessing URI ".$_SERVER['REQUEST_URI']); + Logging::debug("Should try to keep memory footprint under 25 MB"); + } } } From 24988616b3289ce5c64571dad73f2576f87a6185 Mon Sep 17 00:00:00 2001 From: Martin Konecny Date: Wed, 22 Aug 2012 14:03:11 -0400 Subject: [PATCH 3/5] CC-1665: Scheduled stream rebroadcasting and recording -current Library session object should not be stored as a property of the Controller class. -fixed --- .../controllers/LibraryController.php | 79 ++++++++++++++++--- .../controllers/PlaylistController.php | 16 ++-- 2 files changed, 76 insertions(+), 19 deletions(-) diff --git a/airtime_mvc/application/controllers/LibraryController.php b/airtime_mvc/application/controllers/LibraryController.php index a23f2f1a7..3734b1573 100644 --- a/airtime_mvc/application/controllers/LibraryController.php +++ b/airtime_mvc/application/controllers/LibraryController.php @@ -8,7 +8,6 @@ class LibraryController extends Zend_Controller_Action { protected $obj_sess = null; - protected $search_sess = null; public function init() { @@ -23,8 +22,6 @@ class LibraryController extends Zend_Controller_Action ->addActionContext('set-num-entries', 'json') ->initContext(); - $this->obj_sess = new Zend_Session_Namespace(UI_PLAYLISTCONTROLLER_OBJ_SESSNAME); - $this->search_sess = new Zend_Session_Namespace("search"); } public function indexAction() @@ -59,32 +56,90 @@ class LibraryController extends Zend_Controller_Action try { - if (isset($this->obj_sess->id)) { - Logging::info($this->obj_sess->type); + $obj_sess = new Zend_Session_Namespace(UI_PLAYLISTCONTROLLER_OBJ_SESSNAME); + //Application_Model_Library::changePlaylist(null, null); + if (isset($obj_sess->id)) { + Logging::info($obj_sess->type); $objInfo = Application_Model_Library::getObjInfo($this->obj_sess->type); - Logging::info($this->obj_sess->id); - $obj = new $objInfo['className']($this->obj_sess->id); + Logging::info($obj_sess->id); + $obj = new $objInfo['className']($obj_sess->id); $userInfo = Zend_Auth::getInstance()->getStorage()->read(); $user = new Application_Model_User($userInfo->id); $isAdminOrPM = $user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER)); if ($isAdminOrPM || $obj->getCreatorId() == $userInfo->id) { $this->view->obj = $obj; - if ($this->obj_sess->type == "block") { + if ($obj_sess->type == "block") { $form = new Application_Form_SmartBlockCriteria(); - $form->startForm($this->obj_sess->id); + $form->startForm($obj_sess->id); $this->view->form = $form; } } $formatter = new LengthFormatter($obj->getLength()); $this->view->length = $formatter->format(); - $this->view->type = $this->obj_sess->type; + $this->view->type = $obj_sess->type; } } catch (PlaylistNotFoundException $e) { - $this->playlistNotFound($this->obj_sess->type); + $this->playlistNotFound($obj_sess->type); } catch (Exception $e) { - $this->playlistUnknownError($e); + $this->playlistNotFound($obj_sess->type); + //$this->playlistUnknownError($e); + } + } + + private function playlistNotFound($p_type) + { + $this->view->error = "{$p_type} not found"; + + Logging::info("{$p_type} not found"); + Application_Model_Library::changePlaylist(null, $p_type); + $this->createFullResponse(null); + } + + private function playlistUnknownError($e) + { + $this->view->error = "Something went wrong."; + + Logging::info("{$e->getFile()}"); + Logging::info("{$e->getLine()}"); + Logging::info("{$e->getMessage()}"); + } + + private function createFullResponse($obj = null, $isJson = false) + { + $isBlock = false; + $viewPath = 'playlist/playlist.phtml'; + if ($obj instanceof Application_Model_Block) { + $isBlock = true; + $viewPath = 'playlist/smart-block.phtml'; + } + + if (isset($obj)) { + $formatter = new LengthFormatter($obj->getLength()); + $this->view->length = $formatter->format(); + + if ($isBlock) { + $form = new Application_Form_SmartBlockCriteria(); + $form->removeDecorator('DtDdWrapper'); + $form->startForm($obj->getId()); + + $this->view->form = $form; + $this->view->obj = $obj; + $this->view->id = $obj->getId(); + if ($isJson) { + return $this->view->render($viewPath); + } else { + $this->view->html = $this->view->render($viewPath); + } + } else { + $this->view->obj = $obj; + $this->view->id = $obj->getId(); + $this->view->html = $this->view->render($viewPath); + unset($this->view->obj); + } + } else { + $this->view->html = $this->view->render($viewPath); } } diff --git a/airtime_mvc/application/controllers/PlaylistController.php b/airtime_mvc/application/controllers/PlaylistController.php index 8631af34c..9e3ea0380 100644 --- a/airtime_mvc/application/controllers/PlaylistController.php +++ b/airtime_mvc/application/controllers/PlaylistController.php @@ -2,7 +2,6 @@ class PlaylistController extends Zend_Controller_Action { - protected $obj_sess = null; public function init() { @@ -28,7 +27,6 @@ class PlaylistController extends Zend_Controller_Action ->addActionContext('get-block-info', 'json') ->initContext(); - $this->obj_sess = new Zend_Session_Namespace(UI_PLAYLISTCONTROLLER_OBJ_SESSNAME); } private function getPlaylist($p_type) @@ -36,8 +34,9 @@ class PlaylistController extends Zend_Controller_Action $obj = null; $objInfo = Application_Model_Library::getObjInfo($p_type); - if (isset($this->obj_sess->id)) { - $obj = new $objInfo['className']($this->obj_sess->id); + $obj_sess = new Zend_Session_Namespace(UI_PLAYLISTCONTROLLER_OBJ_SESSNAME); + if (isset($obj_sess->id)) { + $obj = new $objInfo['className']($obj_sess->id); $modified = $this->_getParam('modified', null); if ($obj->getLastModified("U") !== $modified) { @@ -205,14 +204,17 @@ class PlaylistController extends Zend_Controller_Action $userInfo = Zend_Auth::getInstance()->getStorage()->read(); $user = new Application_Model_User($userInfo->id); + + $obj_sess = new Zend_Session_Namespace(UI_PLAYLISTCONTROLLER_OBJ_SESSNAME); + try { - Logging::info("Currently active {$type} {$this->obj_sess->id}"); - if (in_array($this->obj_sess->id, $ids)) { + Logging::info("Currently active {$type} {$obj_sess->id}"); + if (in_array($obj_sess->id, $ids)) { Logging::info("Deleting currently active {$type}"); Application_Model_Library::changePlaylist(null, $type); } else { Logging::info("Not deleting currently active {$type}"); - $obj = new $objInfo['className']($this->obj_sess->id); + $obj = new $objInfo['className']($obj_sess->id); } if (strcmp($objInfo['className'], 'Application_Model_Playlist')==0) { From c8c257a3308258fc81539e126ca7d3248a748ffc Mon Sep 17 00:00:00 2001 From: Martin Konecny Date: Wed, 22 Aug 2012 14:04:01 -0400 Subject: [PATCH 4/5] CC-1665: Scheduled stream rebroadcasting and recording -change behaviour so that the webstream is not saved as soon as it it created. --- .../controllers/WebstreamController.php | 29 ++++++++++++------- airtime_mvc/application/models/Webstream.php | 4 ++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/airtime_mvc/application/controllers/WebstreamController.php b/airtime_mvc/application/controllers/WebstreamController.php index 5ab8213db..10944aa7b 100644 --- a/airtime_mvc/application/controllers/WebstreamController.php +++ b/airtime_mvc/application/controllers/WebstreamController.php @@ -16,10 +16,15 @@ class WebstreamController extends Zend_Controller_Action { $userInfo = Zend_Auth::getInstance()->getStorage()->read(); + if (!$this->isAuthorized(-1)) { + header("Status: 401 Not Authorized"); + return; + } + $webstream = new CcWebstream(); //we're not saving this primary key in the DB so it's OK - //$webstream->setDbId(-1); + $webstream->setDbId(-1); $webstream->setDbName("Untitled Webstream"); $webstream->setDbDescription(""); $webstream->setDbUrl("http://"); @@ -28,8 +33,9 @@ class WebstreamController extends Zend_Controller_Action $webstream->setDbCreatorId($userInfo->id); $webstream->setDbUtime(new DateTime("now", new DateTimeZone('UTC'))); $webstream->setDbMtime(new DateTime("now", new DateTimeZone('UTC'))); - $webstream->save(); + //$webstream->save(); + /* $type = "stream"; $objInfo = Application_Model_Library::getObjInfo($type); @@ -39,6 +45,7 @@ class WebstreamController extends Zend_Controller_Action $type = "stream"; Application_Model_Library::changePlaylist($obj->getId(), $type); + */ $this->view->obj = new Application_Model_Webstream($webstream); $this->view->action = "new"; @@ -81,7 +88,7 @@ class WebstreamController extends Zend_Controller_Action } - public function isAuthorized($id) + public function isAuthorized($webstream_id) { $hasPermission = false; $user = Application_Model_User::getCurrentUser(); @@ -89,15 +96,18 @@ class WebstreamController extends Zend_Controller_Action $hasPermission = true; } - if (!$hasPermission) { - if ($id != -1) { - $webstream = CcWebstreamQuery::create()->findPK($id); + if ($user->isHost()) { + if ($webstream_id != -1) { + $webstream = CcWebstreamQuery::create()->findPK($webstream_id); //we are updating a playlist. Ensure that if the user is a host/dj, that he has the correct permission. $user = Application_Model_User::getCurrentUser(); if ($webstream->getDbCreatorId() == $user->getId()) { $hasPermission = true; } + } else { + //we are creating a new stream. Don't need to check whether the DJ/Host owns the stream + $hasPermission = true; } } @@ -108,10 +118,6 @@ class WebstreamController extends Zend_Controller_Action { $request = $this->getRequest(); - - $user = Application_Model_User::getCurrentUser(); - $hasPermission = $user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER, UTYPE_HOST)); - $id = $request->getParam("id"); $parameters = array(); @@ -131,6 +137,9 @@ class WebstreamController extends Zend_Controller_Action try { if (Application_Model_Webstream::isValid($analysis)) { $streamId = Application_Model_Webstream::save($parameters, $mime, $di); + + Application_Model_Library::changePlaylist($streamId, "stream"); + $this->view->statusMessage = "
Webstream saved.
"; $this->view->streamId = $streamId; } else { diff --git a/airtime_mvc/application/models/Webstream.php b/airtime_mvc/application/models/Webstream.php index 25279686e..71c5a110a 100644 --- a/airtime_mvc/application/models/Webstream.php +++ b/airtime_mvc/application/models/Webstream.php @@ -7,9 +7,11 @@ class Application_Model_Webstream implements Application_Model_LibraryEditable public function __construct($webstream) { //TODO: hacky... - Logging::info("x ".$webstream); if (is_int($webstream)) { $this->webstream = CcWebstreamQuery::create()->findPK($webstream); + if (is_null($this->webstream)) { + throw new Exception(); + } } else { $this->webstream = $webstream; } From 91f13a62796267be7a95f2a3a6ab12828fcb40ec Mon Sep 17 00:00:00 2001 From: Martin Konecny Date: Wed, 22 Aug 2012 15:34:59 -0400 Subject: [PATCH 5/5] CC-1665: Scheduled stream rebroadcasting and recording -current Library session object should not be stored as a property of the Controller class. -fixed --- .../controllers/LibraryController.php | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/airtime_mvc/application/controllers/LibraryController.php b/airtime_mvc/application/controllers/LibraryController.php index 3734b1573..4cf664a09 100644 --- a/airtime_mvc/application/controllers/LibraryController.php +++ b/airtime_mvc/application/controllers/LibraryController.php @@ -7,8 +7,6 @@ require_once 'formatters/BitrateFormatter.php'; class LibraryController extends Zend_Controller_Action { - protected $obj_sess = null; - public function init() { $ajaxContext = $this->_helper->getHelper('AjaxContext'); @@ -88,7 +86,7 @@ class LibraryController extends Zend_Controller_Action } } - private function playlistNotFound($p_type) + protected function playlistNotFound($p_type) { $this->view->error = "{$p_type} not found"; @@ -97,7 +95,7 @@ class LibraryController extends Zend_Controller_Action $this->createFullResponse(null); } - private function playlistUnknownError($e) + protected function playlistUnknownError($e) { $this->view->error = "Something went wrong."; @@ -106,7 +104,7 @@ class LibraryController extends Zend_Controller_Action Logging::info("{$e->getMessage()}"); } - private function createFullResponse($obj = null, $isJson = false) + protected function createFullResponse($obj = null, $isJson = false) { $isBlock = false; $viewPath = 'playlist/playlist.phtml'; @@ -166,17 +164,18 @@ class LibraryController extends Zend_Controller_Action $file = Application_Model_StoredFile::Recall($id); - if (isset($this->obj_sess->id) && $screen == "playlist") { + $obj_sess = new Zend_Session_Namespace(UI_PLAYLISTCONTROLLER_OBJ_SESSNAME); + if (isset($obj_sess->id) && $screen == "playlist") { // if the user is not admin or pm, check the creator and see if this person owns the playlist or Block - if ($this->obj_sess->type == 'playlist') { - $obj = new Application_Model_Playlist($this->obj_sess->id); + if ($obj_sess->type == 'playlist') { + $obj = new Application_Model_Playlist($obj_sess->id); } else { - $obj = new Application_Model_Block($this->obj_sess->id); + $obj = new Application_Model_Block($obj_sess->id); } if ($isAdminOrPM || $obj->getCreatorId() == $user->getId()) { - if ($this->obj_sess->type === "playlist") { + if ($obj_sess->type === "playlist") { $menu["pl_add"] = array("name"=> "Add to Playlist", "icon" => "add-playlist", "icon" => "copy"); - } else if ($this->obj_sess->type === "block") { + } else if ($obj_sess->type === "block") { $menu["pl_add"] = array("name"=> "Add to Smart Block", "icon" => "add-playlist", "icon" => "copy"); } } @@ -197,13 +196,13 @@ class LibraryController extends Zend_Controller_Action unset($menu["play"]); } if (($isAdminOrPM || $obj->getCreatorId() == $user->getId()) && $screen == "playlist") { - if ($this->obj_sess->type === "playlist") { + if ($obj_sess->type === "playlist") { $menu["pl_add"] = array("name"=> "Add to Playlist", "icon" => "add-playlist", "icon" => "copy"); } } } - if ($this->obj_sess->id !== $id && $screen == "playlist") { + if ($obj_sess->id !== $id && $screen == "playlist") { if ($isAdminOrPM || $obj->getCreatorId() == $user->getId()) { $menu["edit"] = array("name"=> "Edit", "icon" => "edit"); } @@ -215,9 +214,9 @@ class LibraryController extends Zend_Controller_Action $webstream = CcWebstreamQuery::create()->findPK($id); $obj = new Application_Model_Webstream($webstream); - if (isset($this->obj_sess->id) && $screen == "playlist") { + if (isset($obj_sess->id) && $screen == "playlist") { if ($isAdminOrPM || $obj->getCreatorId() == $user->getId()) { - if ($this->obj_sess->type === "playlist") { + if ($obj_sess->type === "playlist") { $menu["pl_add"] = array("name"=> "Add to Playlist", "icon" => "add-playlist", "icon" => "copy"); } }