diff --git a/airtime_mvc/application/common/WidgetHelper.php b/airtime_mvc/application/common/WidgetHelper.php new file mode 100644 index 000000000..22cb6ceef --- /dev/null +++ b/airtime_mvc/application/common/WidgetHelper.php @@ -0,0 +1,169 @@ +setTimezone($utcTimezone); + $utcDayStart = $weekStartDateTime->format("Y-m-d H:i:s"); + for ($i = 0; $i < 14; $i++) { + //have to be in station timezone when adding 1 day for daylight savings. + $weekStartDateTime->setTimezone(new DateTimeZone($timezone)); + $weekStartDateTime->add(new DateInterval('P1D')); + + //convert back to UTC to get the actual timestamp used for search. + $weekStartDateTime->setTimezone($utcTimezone); + + $utcDayEnd = $weekStartDateTime->format("Y-m-d H:i:s"); + $shows = Application_Model_Show::getNextShows($utcDayStart, "ALL", $utcDayEnd); + $utcDayStart = $utcDayEnd; + + // convert to user-defined timezone, or default to station + Application_Common_DateHelper::convertTimestampsToTimezone( + $shows, + array("starts", "ends", "start_timestamp","end_timestamp"), + $timezone + ); + + $result[$dow[$i]] = $shows; + + // XSS exploit prevention + self::convertSpecialChars($result, array("name", "url")); + // convert image paths to point to api endpoints + self::findAndConvertPaths($result); + } + + return $result; + } + + // Second version of this function. + // Removing "next" days and creating two weekly arrays + public static function getWeekInfoV2($timezone) + { + //weekStart is in station time. + $weekStartDateTime = Application_Common_DateHelper::getWeekStartDateTime(); + + $dow = array("monday", "tuesday", "wednesday", "thursday", "friday", + "saturday", "sunday"); + $maxNumOFWeeks = 2; + + $result = array(); + + // default to the station timezone + $timezone = Application_Model_Preference::GetDefaultTimezone(); + $userDefinedTimezone = strtolower($timezone); + // if the timezone defined by the user exists, use that + if (array_key_exists($userDefinedTimezone, timezone_abbreviations_list())) { + $timezone = $userDefinedTimezone; + } + $utcTimezone = new DateTimeZone("UTC"); + + $weekStartDateTime->setTimezone($utcTimezone); + $utcDayStart = $weekStartDateTime->format("Y-m-d H:i:s"); + $weekCounter = 0; + while ($weekCounter < $maxNumOFWeeks) { + for ($i = 0; $i < 7; $i++) { + $dateParse = date_parse($weekStartDateTime->format("Y-m-d H:i:s")); + //have to be in station timezone when adding 1 day for daylight savings. + $weekStartDateTime->setTimezone(new DateTimeZone($timezone)); + $weekStartDateTime->add(new DateInterval('P1D')); + + //convert back to UTC to get the actual timestamp used for search. + $weekStartDateTime->setTimezone($utcTimezone); + + $utcDayEnd = $weekStartDateTime->format("Y-m-d H:i:s"); + $shows = Application_Model_Show::getNextShows($utcDayStart, "ALL", $utcDayEnd); + $utcDayStart = $utcDayEnd; + + // convert to user-defined timezone, or default to station + Application_Common_DateHelper::convertTimestampsToTimezone( + $shows, + array("starts", "ends", "start_timestamp", "end_timestamp"), + $timezone + ); + + + foreach($shows as &$show) { + $startParseDate = date_parse($show['starts']); + $show["show_start_hour"] = str_pad($startParseDate["hour"], 2, "0").":".str_pad($startParseDate["minute"], 2, 0); + + $endParseDate = date_parse($show['ends']); + $show["show_end_hour"] = str_pad($endParseDate["hour"], 2, 0).":".str_pad($endParseDate["minute"],2, 0); + } + $result[$weekCounter][$dow[$i]]["dayOfMonth"] = $dateParse["day"]; + $result[$weekCounter][$dow[$i]]["dayOfWeek"] = strtoupper(substr($dow[$i], 0, 3)); + $result[$weekCounter][$dow[$i]]["shows"] = $shows; + + // XSS exploit prevention + self::convertSpecialChars($result, array("name", "url")); + // convert image paths to point to api endpoints + self::findAndConvertPaths($result); + } + $weekCounter += 1; + } + + return $result; + } + + /** + * Go through a given array and sanitize any potentially exploitable fields + * by passing them through htmlspecialchars + * + * @param unknown $arr the array to sanitize + * @param unknown $keys indexes of values to be sanitized + */ + public static function convertSpecialChars(&$arr, $keys) + { + foreach ($arr as &$a) { + if (is_array($a)) { + foreach ($keys as &$key) { + if (array_key_exists($key, $a)) { + $a[$key] = htmlspecialchars($a[$key]); + } + } + self::convertSpecialChars($a, $keys); + } + } + } + + /** + * Recursively find image_path keys in the various $result subarrays, + * and convert them to point to the show-logo endpoint + * + * @param unknown $arr the array to search + */ + public static function findAndConvertPaths(&$arr) + { + $CC_CONFIG = Config::getConfig(); + $baseDir = Application_Common_OsPath::formatDirectoryWithDirectorySeparators($CC_CONFIG['baseDir']); + + foreach ($arr as &$a) { + if (is_array($a)) { + if (array_key_exists("image_path", $a)) { + $a["image_path"] = $a["image_path"] && $a["image_path"] !== '' ? + "http://".$_SERVER['HTTP_HOST'].$baseDir."api/show-logo?id=".$a["id"] : ''; + } else { + self::findAndConvertPaths($a); + } + } + } + } +} \ No newline at end of file diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index 11f6f7889..d77e2c509 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -1,4 +1,5 @@ convertSpecialChars($result, array("name", "url")); + WidgetHelper::convertSpecialChars($result, array("name", "url")); // apply user-defined timezone, or default to station Application_Common_DateHelper::convertTimestampsToTimezone( $result['currentShow'], @@ -216,7 +217,7 @@ class ApiController extends Zend_Controller_Action $result["timezone"] = $upcase ? strtoupper($timezone) : $timezone; $result["timezoneOffset"] = Application_Common_DateHelper::getTimezoneOffset($timezone); // convert image paths to point to api endpoints - $this->findAndConvertPaths($result); + WidgetHelper::findAndConvertPaths($result); // 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; @@ -286,11 +287,11 @@ class ApiController extends Zend_Controller_Action $result = Application_Model_Schedule::GetPlayOrderRange($utcTimeEnd, $showsToRetrieve); // XSS exploit prevention - $this->convertSpecialChars($result, array("name", "url")); + WidgetHelper::convertSpecialChars($result, array("name", "url")); // apply user-defined timezone, or default to station $this->applyLiveTimezoneAdjustments($result, $timezone, $upcase); // convert image paths to point to api endpoints - $this->findAndConvertPaths($result); + 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; @@ -364,55 +365,11 @@ class ApiController extends Zend_Controller_Action $this->view->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); - //weekStart is in station time. - $weekStartDateTime = Application_Common_DateHelper::getWeekStartDateTime(); - - $dow = array("monday", "tuesday", "wednesday", "thursday", "friday", - "saturday", "sunday", "nextmonday", "nexttuesday", "nextwednesday", - "nextthursday", "nextfriday", "nextsaturday", "nextsunday"); + $result = WidgetHelper::getWeekInfo($this->getRequest()->getParam("timezone")); - $result = array(); - - // default to the station timezone - $timezone = Application_Model_Preference::GetDefaultTimezone(); - $userDefinedTimezone = strtolower($this->getRequest()->getParam("timezone")); - // if the timezone defined by the user exists, use that - if (array_key_exists($userDefinedTimezone, timezone_abbreviations_list())) { - $timezone = $userDefinedTimezone; - } - $utcTimezone = new DateTimeZone("UTC"); - - $weekStartDateTime->setTimezone($utcTimezone); - $utcDayStart = $weekStartDateTime->format("Y-m-d H:i:s"); - for ($i = 0; $i < 14; $i++) { - //have to be in station timezone when adding 1 day for daylight savings. - $weekStartDateTime->setTimezone(new DateTimeZone($timezone)); - $weekStartDateTime->add(new DateInterval('P1D')); - - //convert back to UTC to get the actual timestamp used for search. - $weekStartDateTime->setTimezone($utcTimezone); - - $utcDayEnd = $weekStartDateTime->format("Y-m-d H:i:s"); - $shows = Application_Model_Show::getNextShows($utcDayStart, "ALL", $utcDayEnd); - $utcDayStart = $utcDayEnd; - - // convert to user-defined timezone, or default to station - Application_Common_DateHelper::convertTimestampsToTimezone( - $shows, - array("starts", "ends", "start_timestamp","end_timestamp"), - $timezone - ); - - $result[$dow[$i]] = $shows; - } - - // XSS exploit prevention - $this->convertSpecialChars($result, array("name", "url")); - // convert image paths to point to api endpoints - $this->findAndConvertPaths($result); - //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; + header("Content-type: text/javascript"); if (version_compare(phpversion(), '5.4.0', '<')) { @@ -429,50 +386,6 @@ class ApiController extends Zend_Controller_Action } } - /** - * Go through a given array and sanitize any potentially exploitable fields - * by passing them through htmlspecialchars - * - * @param unknown $arr the array to sanitize - * @param unknown $keys indexes of values to be sanitized - */ - private function convertSpecialChars(&$arr, $keys) - { - foreach ($arr as &$a) { - if (is_array($a)) { - foreach ($keys as &$key) { - if (array_key_exists($key, $a)) { - $a[$key] = htmlspecialchars($a[$key]); - } - } - $this->convertSpecialChars($a, $keys); - } - } - } - - /** - * Recursively find image_path keys in the various $result subarrays, - * and convert them to point to the show-logo endpoint - * - * @param unknown $arr the array to search - */ - private function findAndConvertPaths(&$arr) - { - $CC_CONFIG = Config::getConfig(); - $baseDir = Application_Common_OsPath::formatDirectoryWithDirectorySeparators($CC_CONFIG['baseDir']); - - foreach ($arr as &$a) { - if (is_array($a)) { - if (array_key_exists("image_path", $a)) { - $a["image_path"] = $a["image_path"] && $a["image_path"] !== '' ? - "http://".$_SERVER['HTTP_HOST'].$baseDir."api/show-logo?id=".$a["id"] : ''; - } else { - $this->findAndConvertPaths($a); - } - } - } - } - /** * API endpoint to display the show logo */ diff --git a/airtime_mvc/application/controllers/EmbedController.php b/airtime_mvc/application/controllers/EmbedController.php index 08f83aaad..4265eb866 100644 --- a/airtime_mvc/application/controllers/EmbedController.php +++ b/airtime_mvc/application/controllers/EmbedController.php @@ -1,4 +1,5 @@ view->css = Application_Common_HTTPHelper::getStationUrl() . "widgets/css/airtime-widgets.css?".$CC_CONFIG['airtime_version']; $this->view->jquery = Application_Common_HTTPHelper::getStationUrl() . "widgets/js/jquery-1.6.1.min.js?".$CC_CONFIG['airtime_version']; $this->view->jquery_custom = Application_Common_HTTPHelper::getStationUrl() . "widgets/js/jquery-ui-1.8.10.custom.min.js?".$CC_CONFIG['airtime_version']; - $this->view->widget_js = Application_Common_HTTPHelper::getStationUrl() . "widgets/js/jquery.showinfo.js?".$CC_CONFIG['airtime_version']; + //$this->view->widget_js = Application_Common_HTTPHelper::getStationUrl() . "widgets/js/jquery.showinfo.js?".$CC_CONFIG['airtime_version']; + + $result = WidgetHelper::getWeekInfoV2($this->getRequest()->getParam("timezone")); + //Logging::info($result); + $this->view->scheduleDataWeek1 = $result[0]; + $this->view->scheduleDataWeek2 = $result[1]; } } diff --git a/airtime_mvc/application/views/scripts/embed/weekly-program.phtml b/airtime_mvc/application/views/scripts/embed/weekly-program.phtml index a932a86e6..9cb1814b7 100644 --- a/airtime_mvc/application/views/scripts/embed/weekly-program.phtml +++ b/airtime_mvc/application/views/scripts/embed/weekly-program.phtml @@ -8,20 +8,116 @@
- + + + +