2012-07-18 03:07:57 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Application_Model_Webstream{
|
|
|
|
|
2012-07-31 03:48:04 +02:00
|
|
|
private $id;
|
|
|
|
|
2012-08-10 18:40:28 +02:00
|
|
|
public function __construct($webstream)
|
2012-07-31 03:48:04 +02:00
|
|
|
{
|
2012-08-10 18:40:28 +02:00
|
|
|
$this->webstream = $webstream;
|
2012-07-31 03:48:04 +02:00
|
|
|
}
|
|
|
|
|
2012-08-10 06:57:03 +02:00
|
|
|
public function getOrm()
|
|
|
|
{
|
|
|
|
return $this->webstream;
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:34 +02:00
|
|
|
public function getName()
|
2012-07-26 00:41:52 +02:00
|
|
|
{
|
2012-08-03 21:59:34 +02:00
|
|
|
return $this->webstream->getDbName();
|
2012-07-18 03:07:57 +02:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:34 +02:00
|
|
|
public function getId()
|
2012-07-26 00:41:52 +02:00
|
|
|
{
|
2012-08-03 21:59:34 +02:00
|
|
|
return $this->webstream->getDbId();
|
2012-07-18 03:07:57 +02:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:34 +02:00
|
|
|
public function getLastModified($p_type)
|
2012-07-26 00:41:52 +02:00
|
|
|
{
|
2012-07-18 03:07:57 +02:00
|
|
|
return "modified";
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:34 +02:00
|
|
|
public function getDefaultLength()
|
|
|
|
{
|
2012-08-03 22:18:45 +02:00
|
|
|
$dateString = $this->webstream->getDbLength();
|
|
|
|
$arr = explode(":", $dateString);
|
|
|
|
if (count($arr) == 3) {
|
|
|
|
list($hours, $min, $sec) = $arr;
|
|
|
|
$di = new DateInterval("PT{$hours}H{$min}M{$sec}S");
|
2012-08-04 00:06:47 +02:00
|
|
|
return $di->format("%Hh %Im");
|
2012-08-03 22:18:45 +02:00
|
|
|
}
|
|
|
|
return "";
|
2012-08-03 21:59:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription()
|
2012-07-26 00:41:52 +02:00
|
|
|
{
|
2012-08-03 21:59:34 +02:00
|
|
|
return $this->webstream->getDbDescription();
|
2012-07-18 03:07:57 +02:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:34 +02:00
|
|
|
public function getUrl()
|
2012-07-26 00:41:52 +02:00
|
|
|
{
|
2012-08-03 21:59:34 +02:00
|
|
|
return $this->webstream->getDbUrl();
|
2012-07-18 03:07:57 +02:00
|
|
|
}
|
2012-07-19 00:27:39 +02:00
|
|
|
|
2012-07-31 03:48:04 +02:00
|
|
|
public function getMetadata()
|
|
|
|
{
|
2012-08-10 20:37:32 +02:00
|
|
|
$subjs = CcSubjsQuery::create()->findPK($this->webstream->getDbCreatorId());
|
2012-07-31 03:48:04 +02:00
|
|
|
|
|
|
|
$username = $subjs->getDbLogin();
|
|
|
|
return array(
|
2012-08-03 21:59:34 +02:00
|
|
|
"name" => $this->webstream->getDbName(),
|
|
|
|
"length" => $this->webstream->getDbLength(),
|
|
|
|
"description" => $this->webstream->getDbDescription(),
|
2012-07-31 03:48:04 +02:00
|
|
|
"login"=> $username,
|
2012-08-03 21:59:34 +02:00
|
|
|
"url" => $this->webstream->getDbUrl(),
|
2012-07-31 03:48:04 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function deleteStreams($p_ids, $p_userId)
|
|
|
|
{
|
|
|
|
$leftOver = self::streamsNotOwnedByUser($p_ids, $p_userId);
|
|
|
|
if (count($leftOver) == 0) {
|
|
|
|
CcWebstreamQuery::create()->findPKs($p_ids)->delete();
|
|
|
|
} else {
|
|
|
|
throw new Exception("Invalid user permissions");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function returns that are not owen by $p_user_id among $p_ids
|
|
|
|
private static function streamsNotOwnedByUser($p_ids, $p_userId)
|
|
|
|
{
|
|
|
|
$ownedByUser = CcWebstreamQuery::create()->filterByDbCreatorId($p_userId)->find()->getData();
|
|
|
|
$ownedStreams = array();
|
|
|
|
foreach ($ownedByUser as $pl) {
|
|
|
|
if (in_array($pl->getDbId(), $p_ids)) {
|
|
|
|
$ownedStreams[] = $pl->getDbId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$leftOvers = array_diff($p_ids, $ownedStreams);
|
|
|
|
return $leftOvers;
|
|
|
|
}
|
2012-08-02 21:45:15 +02:00
|
|
|
|
2012-08-10 18:40:28 +02:00
|
|
|
public static function analyzeFormData($parameters)
|
2012-08-02 21:45:15 +02:00
|
|
|
{
|
|
|
|
$valid = array("length" => array(true, ''),
|
2012-08-03 21:59:34 +02:00
|
|
|
"url" => array(true, ''),
|
|
|
|
"name" => array(true, ''));
|
2012-08-02 21:45:15 +02:00
|
|
|
|
2012-08-10 18:40:28 +02:00
|
|
|
$length = $parameters["length"];
|
2012-08-02 21:45:15 +02:00
|
|
|
$result = preg_match("/^([0-9]{1,2})h ([0-5][0-9])m$/", $length, $matches);
|
|
|
|
if (!$result == 1 || !count($matches) == 3) {
|
|
|
|
$valid['length'][0] = false;
|
|
|
|
$valid['length'][1] = 'Length should be of form "00h 00m"';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-10 18:40:28 +02:00
|
|
|
$url = $parameters["url"];
|
2012-08-10 18:42:56 +02:00
|
|
|
//simple validator that checks to make sure that the url starts with
|
|
|
|
//http(s),
|
|
|
|
//and that the domain is at least 1 letter long
|
|
|
|
$result = preg_match("/^(http|https):\/\/.+/", $url, $matches);
|
2012-08-02 21:45:15 +02:00
|
|
|
|
|
|
|
if ($result == 0) {
|
|
|
|
$valid['url'][0] = false;
|
2012-08-13 18:59:26 +02:00
|
|
|
$valid['url'][1] = 'URL should be of form "http://domain"';
|
|
|
|
} else {
|
2012-08-02 21:45:15 +02:00
|
|
|
|
2012-08-13 18:59:26 +02:00
|
|
|
try {
|
|
|
|
$mime = Application_Model_Webstream::discoverStreamMime($url);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$valid['url'][0] = false;
|
|
|
|
$valid['url'][1] = $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
2012-08-02 21:45:15 +02:00
|
|
|
|
2012-08-10 18:40:28 +02:00
|
|
|
$name = $parameters["name"];
|
2012-08-02 21:45:15 +02:00
|
|
|
if (strlen($name) == 0) {
|
|
|
|
$valid['name'][0] = false;
|
|
|
|
$valid['name'][1] = 'Webstream name cannot be empty';
|
|
|
|
}
|
|
|
|
|
2012-08-10 18:40:28 +02:00
|
|
|
$id = $parameters["id"];
|
2012-08-03 21:59:34 +02:00
|
|
|
|
|
|
|
if (!is_null($id)) {
|
|
|
|
// user has performed a create stream action instead of edit
|
|
|
|
// stream action. Check if user has the rights to edit this stream.
|
|
|
|
|
|
|
|
Logging::log("CREATE");
|
|
|
|
} else {
|
|
|
|
Logging::log("EDIT");
|
|
|
|
}
|
|
|
|
|
2012-08-13 18:59:26 +02:00
|
|
|
return array($valid, $mime);
|
2012-08-02 21:45:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function isValid($analysis)
|
|
|
|
{
|
|
|
|
foreach ($analysis as $k => $v) {
|
2012-08-10 06:57:03 +02:00
|
|
|
if ($v[0] === false) {
|
2012-08-02 21:45:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-31 03:48:04 +02:00
|
|
|
|
2012-08-13 18:59:26 +02:00
|
|
|
private static function discoverStreamMime($url)
|
2012-08-10 06:57:03 +02:00
|
|
|
{
|
2012-08-13 18:59:26 +02:00
|
|
|
$headers = get_headers($url);
|
|
|
|
$mime = null;
|
|
|
|
foreach ($headers as $h) {
|
|
|
|
if (preg_match("/^content-type:/i", $h)) {
|
|
|
|
list(, $value) = explode(":", $h, 2);
|
|
|
|
$mime = trim($value);
|
|
|
|
}
|
|
|
|
if (preg_match("/^content-length:/i", $h)) {
|
|
|
|
//if content-length appears, this is not a web stream!!!!
|
|
|
|
//Aborting the save process.
|
|
|
|
throw new Exception("Invalid webstream - This appears to be a file download.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($mime)) {
|
|
|
|
throw new Exception("No MIME type found for webstream.");
|
|
|
|
} else {
|
|
|
|
if (!preg_match("/(mpeg|ogg)/", $mime)) {
|
|
|
|
throw new Exception("Unrecognized stream type: $mime");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-10 06:57:03 +02:00
|
|
|
return $mime;
|
|
|
|
}
|
|
|
|
|
2012-08-13 18:59:26 +02:00
|
|
|
public static function save($parameters, $mime)
|
2012-07-26 00:41:52 +02:00
|
|
|
{
|
|
|
|
$userInfo = Zend_Auth::getInstance()->getStorage()->read();
|
|
|
|
|
2012-08-10 18:40:28 +02:00
|
|
|
$length = $parameters["length"];
|
2012-08-02 21:45:15 +02:00
|
|
|
$result = preg_match("/^([0-9]{1,2})h ([0-5][0-9])m$/", $length, $matches);
|
2012-08-03 21:59:34 +02:00
|
|
|
|
2012-08-02 21:45:15 +02:00
|
|
|
if ($result == 1 && count($matches) == 3) {
|
|
|
|
$hours = $matches[1];
|
|
|
|
$minutes = $matches[2];
|
|
|
|
$di = new DateInterval("PT{$hours}H{$minutes}M");
|
|
|
|
$dblength = $di->format("%H:%I");
|
|
|
|
} else {
|
|
|
|
//This should never happen because we should have already validated
|
|
|
|
//in the controller
|
|
|
|
throw new Exception("Invalid date format: $length");
|
|
|
|
}
|
2012-08-03 21:59:34 +02:00
|
|
|
|
2012-08-10 06:57:03 +02:00
|
|
|
$webstream = new CcWebstream();
|
2012-08-10 18:40:28 +02:00
|
|
|
$webstream->setDbName($parameters["name"]);
|
|
|
|
$webstream->setDbDescription($parameters["description"]);
|
|
|
|
$webstream->setDbUrl($parameters["url"]);
|
2012-07-26 00:41:52 +02:00
|
|
|
|
|
|
|
$webstream->setDbLength($dblength);
|
2012-07-31 03:48:04 +02:00
|
|
|
$webstream->setDbCreatorId($userInfo->id);
|
2012-07-27 20:47:15 +02:00
|
|
|
$webstream->setDbUtime(new DateTime("now", new DateTimeZone('UTC')));
|
|
|
|
$webstream->setDbMtime(new DateTime("now", new DateTimeZone('UTC')));
|
2012-08-10 06:57:03 +02:00
|
|
|
|
2012-08-10 18:40:28 +02:00
|
|
|
$ws = new Application_Model_Webstream($webstream);
|
2012-08-10 06:57:03 +02:00
|
|
|
|
2012-08-13 18:59:26 +02:00
|
|
|
$webstream->setDbMime($mime);
|
2012-08-10 06:57:03 +02:00
|
|
|
$webstream->save();
|
2012-07-19 00:27:39 +02:00
|
|
|
}
|
2012-07-18 03:07:57 +02:00
|
|
|
}
|