Example #1
0
 /**
  * Decodes a textual representation for a duration
  *
  * @param $s input duration (numeric=seconds, hh:mm:ss=decode to seconds)
  */
 function set($s)
 {
     if (!$s) {
         return;
     }
     if (is_numeric($s)) {
         $this->value = $s;
         return;
     }
     if (is_duration($s)) {
         $this->value = parse_duration($s);
         return;
     }
     $a = explode(':', $s);
     if (count($a) == 3) {
         //handle "00:03:39.00"
         $this->value = $a[0] * 3600 + $a[1] * 60 + $a[2];
         return;
     }
     if (count($a) == 2) {
         //handle "04:29"
         $this->value = $a[0] * 60 + $a[1];
         return;
     }
     dtrace();
     die('Duration->set( ' . $s . ' ) FAIL');
     //$this->duration = $s;
 }
Example #2
0
 /**
  * @param $duration_mode "1d", "2w", "2m"
  */
 static function add($type, $owner, $text, $duration_mode = '', $start_mode = '')
 {
     $length = parse_duration($duration_mode);
     if (!$length) {
         throw new \Exception('odd duration ' . $duration_mode);
     }
     switch ($start_mode) {
         case 'thismonday':
             $dayofweek = date('N');
             $mon = date('n');
             $day = date('j');
             $timeStart = mktime(6, 0, 0, $mon, $day - $dayofweek + 1);
             //06:00 Monday current week
             break;
         case 'nextmonday':
             $dayofweek = date('N');
             $mon = date('n');
             $day = date('j');
             $timeStart = mktime(6, 0, 0, $mon, $day - $dayofweek + 1 + 7);
             //06:00 Monday next week
             break;
         case 'nextfree':
             $q = 'SELECT time_end FROM tblPolls' . ' WHERE owner = ? AND deleted_by = ?' . ' ORDER BY time_start DESC' . ' LIMIT 1';
             $data = Sql::pSelectRow($q, 'ii', $owner, 0);
             $timeStart = $data ? ts($data['timeEnd']) : time();
             break;
         default:
             throw new \Exception('eexp');
     }
     $timeEnd = $timeStart + $length;
     return self::addPollExactPeriod($type, $owner, $text, $timeStart, $timeEnd);
 }
 public static function init($room_id, $div_name, $form_id)
 {
     $header = XhtmlHeader::getInstance();
     $header->includeJs('http://yui.yahooapis.com/3.4.1/build/yui/yui-min.js');
     $session = SessionHandler::getInstance();
     $interval = 1000 * parse_duration('1s');
     // milliseconds
     $locale = 'sv-SE';
     $header->registerJsFunction('function scroll_to_bottom(div)' . '{' . 'var elm = get_el(div);' . 'try {' . 'elm.scrollTop = elm.scrollHeight;' . '} catch(e) {' . 'var f = document.createElement("input");' . 'if (f.setAttribute) f.setAttribute("type","text");' . 'if (elm.appendChild) elm.appendChild(f);' . 'f.style.width = "0px";' . 'f.style.height = "0px";' . 'if (f.focus) f.focus();' . 'if (elm.removeChild) elm.removeChild(f);' . '}' . '}');
     $header->embedJs('YUI({lang:"' . $locale . '"}).use("io-form","node","json-parse","datatype-date", function(Y)' . '{' . 'Y.on("load", function() {' . 'Init();' . '});' . 'function Init(ts)' . '{' . 'var latest;' . 'if (typeof ts === "undefined") {' . 'var uri = "/u/chatroom/update/" + ' . $room_id . ';' . '} else {' . 'var uri = "/u/chatroom/update/" + ' . $room_id . ' + "?ts=" + ts;' . '}' . 'function complete(id, o)' . '{' . 'var data = o.responseText;' . 'var node = Y.one("#' . $div_name . '");' . 'try {' . 'var data = Y.JSON.parse(data);' . '} catch (e) {' . 'console.log("invalid data from " + uri);' . 'return;' . '}' . 'if (typeof ts === "undefined")' . 'node.setContent("");' . 'for (var i = data.length-1; i >= 0; --i) {' . 'var p = data[i];' . 'if ((typeof ts === "undefined") || p.from != ' . $session->id . ')' . 'msg_render(p,node);' . '}' . 'if (data.length)' . 'scroll_to_bottom("' . $div_name . '");' . 'latest = data[0] ? data[0].ts : ts;' . 'setTimeout(Init,' . $interval . ',latest);' . '};' . 'Y.once("io:complete",complete,Y);' . 'var request = Y.io(uri);' . '}' . 'Y.one("#' . $form_id . '").on("submit", function(e)' . '{' . 'e.preventDefault();' . 'e.stopPropagation();' . 'frm = get_el( this.get("id") );' . 'if (!frm.msg.value)' . 'return false;' . 'var uri = "/u/chatroom/send/" + ' . $room_id . ' + "?m=" + frm.msg.value;' . 'var request = Y.io(uri);' . 'var node = Y.one("#' . $div_name . '");' . 'var p = {' . '"name":"' . $session->username . '",' . '"from":' . $session->id . ',' . '"msg":frm.msg.value,' . '"ts":new Date().getTime()/1000' . '};' . 'msg_render(p,node);' . 'scroll_to_bottom("' . $div_name . '");' . 'frm.msg.value = "";' . 'return false;' . '});' . 'function msg_render(p,node)' . '{' . 'var d = new Date(p.ts*1000);' . 'var today = new Date( new Date().getFullYear(), new Date().getMonth(), new Date().getDate(),0,0,0);' . 'node.append("[");' . 'if (d >= today) {' . 'node.append( Y.DataType.Date.format(d, {format:"%H:%M"}) );' . '} else {' . 'node.append( Y.DataType.Date.format(d, {format:"%a %d %b %H:%M"}) );' . '}' . 'node.append("]&nbsp;");' . 'var who = Y.Node.create("<span class=\\"yui3-hastooltip\\" id=\\"tt_usr_"+p.from+"\\">"+p.name+"</span>");' . 'who.addClass("yui3-hastooltip");' . 'node.append(who);' . 'node.append(": "+p.msg+"<br/>");' . '}' . '});');
 }
Example #4
0
 /** @return true if token is expired or dont exists */
 public static function isExpired($name, $val, $duration)
 {
     $ts = Setting::getTimeSaved(TOKEN, $name, $val);
     if (!$ts) {
         return true;
     }
     if (ts($ts) < time() - parse_duration($duration)) {
         return true;
     }
     return false;
 }
Example #5
0
 public function set($key, $val = '', $expire_time = 3600)
 {
     if (strlen($key) > $this->maxlen) {
         throw new \Exception('Key length too long (len ' . strlen($key) . ', max ' . $this->maxlen . '): ' . $key);
     }
     if ($expire_time) {
         if (!is_duration($expire_time)) {
             throw new \Exception('bad expire time');
         }
         $expire_time = parse_duration($expire_time);
     }
     $this->connect();
     //       $key = str_replace(' ', '_', $key);
     $ret = $this->redis->setex($key, $expire_time, $val);
     if ($this->debug) {
         echo 'TempStoreRedis SET "' . $key . '" = "' . substr($val, 0, 200) . '"... (' . $expire_time . ' sec)' . ln();
     }
     /*
             if (!$ret)
                 throw new \Exception ('SET failed');
     */
     return $ret;
 }
    define('MAIN_PAGE', 'Tool Box');
}
if (!defined('SUB_PAGE')) {
    define('SUB_PAGE', 'Repair video duration');
}
$params = array('duration' => '1', 'duration_op' => '<=', 'status' => 'Successful');
$videos = get_videos($params);
$fixed_array = array();
if ($_POST['fix_duration'] || $_POST['mark_failed'] || $_POST['mark_delete']) {
    foreach ($videos as $video) {
        $log = get_file_details($video['file_name']);
        if ($log && $_POST['fix_duration']) {
            //$duration = $log['output_duration'];
            //if(!$duration)
            //	$duration = $log['duration'];
            $duration = parse_duration(LOGS_DIR . '/' . $video['file_name'] . '.log');
            if (!$duration) {
                e("Can't do anything about \"" . $video['title'] . "\"");
            } else {
                $db->update(tbl('video'), array('duration'), array($duration), "videoid='" . $video['videoid'] . "'");
                $fixed_array[$video['file_name']] = 'yes';
                e("Succesfully updated duration of \"" . $video['title'] . "\" to " . SetTime($duration), 'm');
            }
        }
        if (!$log && $_POST['mark_failed']) {
            $db->update(tbl("video"), array("status", "failed_reason"), array('Failed', "Unable to get video duration"), " file_name='" . $video['file_name'] . "'");
            e("\"" . $video['title'] . "\" status has been changed to Failed", "m");
        }
        if (!$log && $_POST['mark_delete']) {
            $db->update(tbl("video"), array("status", "failed_reason"), array('Failed', "Unable to get video duration"), " file_name='" . $video['file_name'] . "'");
            $cbvideo->delete_video($video['videoid']);
Example #7
0
 /**
  * @param $s cache time in seconds or as string "4h", max 2592000 (30 days)
  */
 function setCacheTime($s)
 {
     $this->cache_time = parse_duration($s);
 }
Example #8
0
 /**
  * @param $s cache time in seconds; max 2592000 (30 days)
  */
 function setTimeout($s)
 {
     $this->expire_time = parse_duration($s);
 }
Example #9
0
     $list = ChatRoom::getList();
     foreach ($list as $cr) {
         echo ahref('u/chatroom/show/' . $cr->id, $cr->name) . '<br/>';
     }
     break;
 case 'update':
     // JSON - returns messages in chatroom (since ts)
     // child = room id
     $page->setMimeType('text/plain');
     if (!$this->child || !is_numeric($this->child)) {
         die('hey');
     }
     $ts = 0;
     if (isset($_GET['ts']) && is_numeric($_GET['ts'])) {
         $ts = $_GET['ts'];
         $max_age = parse_duration('7d');
         // empty json string on old queries
         if ($ts < microtime(true) - $max_age) {
             echo '[]';
             return;
         }
     }
     $res = ChatMessage::getRecent($this->child, $ts, 25);
     $out = array();
     foreach ($res as $r) {
         $out[] = array('name' => User::get($r->from)->name, 'from' => $r->from, 'msg' => $r->msg, 'ts' => $r->microtime);
     }
     echo json_encode($out);
     break;
 case 'send':
     // XHR - writes a message to chatroom
Example #10
0
/**
 * Function used to update processed video
 * @param Files details
 */
function update_processed_video($file_array, $status = 'Successful', $ingore_file_status = false, $failed_status = '')
{
    global $db;
    $file = $file_array['cqueue_name'];
    $array = explode('-', $file);
    if (!empty($array[0])) {
        $file_name = $array[0];
    }
    $file_name = $file;
    $file_path = VIDEOS_DIR . '/' . $file_array['cqueue_name'] . '.flv';
    $file_size = @filesize($file_path);
    if (file_exists($file_path) && $file_size > 0 && !$ingore_file_status) {
        $stats = get_file_details($file_name);
        //$duration = $stats['output_duration'];
        //if(!$duration)
        //  $duration = $stats['duration'];
        $duration = parse_duration(LOGS_DIR . '/' . $file_array['cqueue_name'] . '.log');
        $db->update(tbl("video"), array("status", "duration", "failed_reason"), array($status, $duration, $failed_status), " file_name='" . $file_name . "'");
    } else {
        //$duration = $stats['output_duration'];
        //if(!$duration)
        //  $duration = $stats['duration'];
        $result = db_select("SELECT * FROM " . tbl("video") . " WHERE file_name = '{$file_name}'");
        if ($result) {
            foreach ($result as $result1) {
                $str = '/' . $result1['file_directory'] . '/';
                $duration = parse_duration(LOGS_DIR . $str . $file_array['cqueue_name'] . '.log');
            }
        }
        $db->update(tbl("video"), array("status", "duration", "failed_reason"), array($status, $duration, $failed_status), " file_name='" . $file_name . "'");
    }
}
Example #11
0
 function setTimeout($n)
 {
     if (!is_duration($n)) {
         throw new \Exception('bad timeout: ' . $n);
     }
     $this->timeout = parse_duration($n);
 }
/**
 * Function used to update processed video
 * @param Files details
 * @deprecated 2012 3.0
 */
function update_processed_video($file_array, $status = 'Successful', $ingore_file_status = false, $failed_status = '')
{
    global $db;
    $file = $file_array['cqueue_name'];
    $array = explode('-', $file);
    if (!empty($array[0])) {
        $file_name = $array[0];
    }
    $file_name = $file;
    $file_path = VIDEOS_DIR . '/' . $file_array['cqueue_name'] . '.flv';
    $file_size = @filesize($file_path);
    if (file_exists($file_path) && $file_size > 0 && !$ingore_file_status) {
        $stats = get_file_details($file_name);
        //$duration = $stats['output_duration'];
        //if(!$duration)
        //	$duration = $stats['duration'];
        $duration = parse_duration(LOGS_DIR . '/' . $file_array['cqueue_name'] . '.log');
        $db->update(tbl("video"), array("status", "duration", "failed_reason"), array($status, $duration, $failed_status), " file_name='" . $file_name . "'");
    } else {
        $stats = get_file_details($file_name);
        //$duration = $stats['output_duration'];
        //if(!$duration)
        //	$duration = $stats['duration'];
        $duration = parse_duration(LOGS_DIR . '/' . $file_array['cqueue_name'] . '.log');
        $db->update(tbl("video"), array("status", "duration", "failed_reason"), array('Failed', $duration, $failed_status), " file_name='" . $file_name . "'");
    }
}
Example #13
0
/**
 * Converts a timespan into human-readable text
 *
 * @param $secs number of seconds to present
 * @return returns a sting like: 4h10m3s
 */
function shortTimePeriod($secs)
{
    if (is_float($secs)) {
        $secs = ceil($secs);
    }
    if (is_duration($secs)) {
        $secs = parse_duration($secs);
    }
    $retval = '';
    //years
    $a = date('Y', $secs) - 1970;
    if ($a == 1) {
        $retval = $a . ' year, ';
    } else {
        if ($a > 0) {
            $retval = $a . ' years, ';
        }
    }
    $secs -= $a * 60 * 60 * 24 * 30 * 365;
    //months
    $a = date('n', $secs) - 1;
    if ($a == 1) {
        $retval .= $a . ' month, ';
    } else {
        if ($a > 0) {
            $retval .= $a . ' months, ';
        }
    }
    $secs -= $a * 60 * 60 * 24 * 30;
    //days
    $a = date('j', $secs) - 1;
    if ($a == 1) {
        $retval .= $a . ' day, ';
    } else {
        if ($a > 0) {
            $retval .= $a . ' days, ';
        }
    }
    $secs -= $a * 60 * 60 * 24;
    //hours
    $a = date('H', $secs) - 1;
    if ($a > 0) {
        $retval .= $a . 'h';
    }
    $secs -= $a * 60 * 60;
    //minutes
    $a = date('i', $secs) - 0;
    if ($a > 0) {
        $retval .= $a . 'm';
    }
    $secs -= $a * 60;
    //seconds
    $a = date('s', $secs) - 0;
    if ($a > 0) {
        $retval .= $a . 's';
    }
    if (substr($retval, -2) == ', ') {
        $retval = substr($retval, 0, -2);
    }
    if ($retval == '') {
        $retval = '0s';
    }
    return $retval;
}
Example #14
0
if (is_hms('123.123.123')) {
    echo "FAIL 53\n";
}
if (!is_hms('12:44:11,21')) {
    echo "FAIL 54\n";
}
if (!is_hms('00:26:36,595')) {
    echo "FAIL 55\n";
}
if (!is_hms('00:00:0,500')) {
    echo "FAIL 56\n";
}
if (parse_duration('4h') != 14400) {
    echo "FAIL 60\n";
}
if (parse_duration('-4h') != -14400) {
    echo "FAIL 61\n";
}
if (num_days('2010-03-04', '2010-03-04') != 1) {
    echo "FAIL 70\n";
}
if (num_days('2010-03-04', '2010-03-06') != 3) {
    echo "FAIL 71\n";
}
if (num_days('2010-01-01', '2010-02-24') != 55) {
    echo "FAIL 72\n";
}
if (num_days('2010-03-01', '2010-03-31') != 31) {
    echo "FAIL 73\n";
}
if (num_years('1980-05-26', '1990-02-12') != 9) {