protected function encodeData($data) { global $arr_response; if ($_REQUEST['response_format'] == 'xml') { header('Content-type: text/xml'); $response = SiteLibrary::array2xml($arr_response); } else { //Do a pretty print if available in the running php/json version compilation if ($_REQUEST['response_print'] == 'pretty') { if (defined(JSON_PRETTY_PRINT)) { $response = json_encode($arr_response, JSON_PRETTY_PRINT); } else { $response = json_encode($arr_response); } } else { $response = json_encode($arr_response); } if (isset($_REQUEST['callback'])) { header('Content-type: application/x-javascript'); $response = $_REQUEST['callback'] . '(' . $response . ')'; } else { header('Content-type: text/plain'); } } return $response; }
/** * Displays the login page */ public function actionLogin($sh = '', $t = '') { $this->model = new LoginForm(); // if it is ajax validation request if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') { echo CActiveForm::validate($this->model); Yii::app()->end(); } // collect user input data if (isset($_POST['LoginForm'])) { $this->model->attributes = $_POST['LoginForm']; // validate user input and redirect to the previous page if valid if ($this->model->validate() && $this->model->login()) { //Assign the subject to the new user if he/she registered after adding a subject if ($sh and $t) { //Allow asignment only within 15 minutes since subject added if (SiteLibrary::utc_time() - $t < 900) { Subject::model()->updateAll(array('user_id' => Yii::app()->user->id), 'time_submitted=:time_submitted AND hash=:hash', array(':time_submitted' => $t, ':hash' => $sh)); } $this->redirect(array('mysub/' . Yii::app()->user->name)); } else { $this->redirect(Yii::app()->user->returnUrl); } } } // display the login form $this->render('login', array('model' => $this->model)); }
public function filterMenu($filterChain) { //.$this->action->Id $arr_titles = array(); $arr_titles['index'] = 'Mysub'; $this->menu = array(array('label' => $arr_titles['index'], 'url' => array('mysub/index'))); SiteLibrary::remove_current_url_menu($this, $arr_titles); $filterChain->run(); }
public function filterMenu($filterChain) { $arr_titles = array(); $arr_titles['index'] = 'My Applications'; $arr_titles['create'] = 'Create Application'; $arr_titles['update'] = 'Update Application'; //Notice Update option is not on the menu, this is just for the breadcrumbs $this->breadcrumbs = array('Developers' => array('developers/'), 'My Applications' => array('index'), $arr_titles[$this->action->Id]); $this->menu = array(array('label' => $arr_titles['index'], 'url' => array('developers/myapps/index')), array('label' => $arr_titles['create'], 'url' => array('developers/myapps/create'))); SiteLibrary::remove_current_url_menu($this, $arr_titles); $filterChain->run(); }
public function filterMenu($filterChain) { //.$this->action->Id $arr_titles = array(); $arr_titles['index'] = 'Home'; $arr_titles['quickguide'] = 'Quick Guide'; $arr_titles['api'] = 'API'; $arr_titles['authentication'] = 'Authentication'; $this->breadcrumbs = array('Developers' => array('developers/'), 'Documentation' => array('index'), $arr_titles[$this->action->Id]); $this->menu = array(array('label' => $arr_titles['index'], 'url' => array('developers/documentation')), array('label' => $arr_titles['quickguide'], 'url' => array('developers/documentation/quickguide')), array('label' => $arr_titles['api'], 'url' => array('developers/documentation/api')), array('label' => $arr_titles['authentication'], 'url' => array('developers/documentation/authentication'))); SiteLibrary::remove_current_url_menu($this, $arr_titles); $filterChain->run(); }
/** * Get the current time and time remaining */ public function actionGettime() { global $arr_response; $live_subject = Yii::app()->db->createCommand()->select('*')->from('live_subject')->queryRow(); //returns an array, not an object $utc_time = SiteLibrary::utc_time(); $arr_data['current_time'] = $utc_time; $arr_data['current_time_h'] = date("H", $utc_time); $arr_data['current_time_m'] = date("i", $utc_time); $arr_data['current_time_s'] = date("s", $utc_time); $arr_data['time_remaining'] = $live_subject['scheduled_time'] + Yii::app()->params['subject_interval'] * 60 - $utc_time + 2; //lets give some seconds rage in case cron gets delayed $arr_response = array_merge($arr_response, $arr_data); }
public function filterMenu($filterChain) { //.$this->action->Id $arr_titles = array(); $arr_titles['index'] = 'Home'; $arr_titles['quickguide'] = 'Quick Guide'; $arr_titles['documentation'] = 'API Documentation'; $arr_titles['faq'] = 'FAQ'; $arr_titles['blog'] = 'Blog'; $arr_titles['community'] = 'Community'; $arr_titles['source'] = 'Source / Version Control'; $arr_titles['issue'] = 'Issue Tracker'; $arr_titles['terms'] = 'API Terms Of Use'; $arr_titles['contact'] = 'Contact Us'; /*$this->breadcrumbs=array( Yii::t('site','User')=>array('index'), $arr_titles[$this->action->Id] , );*/ $this->menu = array(array('label' => $arr_titles['index'], 'url' => array('developers/')), array('label' => $arr_titles['contact'], 'url' => array('developers/default/contact'))); SiteLibrary::remove_current_url_menu($this, $arr_titles); $filterChain->run(); }
/** * Logs in the user using the given username and password in the model. * Notice username instead of username: http://www.larryullman.com/2010/01/07/custom-authentication-using-the-yii-framework/ * @return boolean whether login is successful */ public function login() { if ($this->_identity === null) { $this->_identity = new UserIdentity($this->username, $this->password); $this->_identity->authenticate(); } if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) { $duration = $this->rememberMe ? 3600 * 24 * 30 : 0; // 30 days Yii::app()->user->login($this->_identity, $duration); $user = User::model()->findByPk($this->_identity->getId()); $user->scenario = 'login'; //to control when to update record modified time $user->ip_last_access = $_SERVER['REMOTE_ADDR']; $user->time_last_access = SiteLibrary::utc_time(); $user->save(); // Update the last access time return true; } else { return false; } }
/** * Executed by cron to update the country_id column in the log table * (because doing so in each record insertion would impact user navigation performance) * so we use a cron instead. */ public function actionUpdateCountryLog() { if ($_SERVER['REMOTE_ADDR'] != '127.0.0.1') { die; } //Only allow to run this locally $this->no_log = true; //This is just to control the logging functionality $cached_ips = array(); $cached_country = array(); $ip = ''; $command = Yii::app()->db->createCommand(); $country_logs = Yii::app()->db->createCommand()->select('id, client_ip, request_ip, client_country_id, request_country_id')->from('log_detail')->where("cronned = 0 AND request_ip <> '127.0.0.1'")->order('id ASC')->limit(100)->queryAll(); foreach ($country_logs as $country_log) { if (strlen($country_log['request_ip']) > 7) { //4x# 4x. minimum valid ip $ip = $country_log['request_ip']; if (!in_array($ip, $cached_ips)) { //don't overuse this service unnecessary $cached_ips[] = $ip; if ($country = SiteLibrary::get_country_from_ip($ip)) { $command->update('log_detail', array('cronned' => 1, 'request_country_id' => $country->id), 'id=:id', array(':id' => $country_log['id'])); $cached_country[$ip] = $country->id; } } else { $command->update('log_detail', array('cronned' => 1, 'request_country_id' => $cached_country[$ip]), 'id=:id', array(':id' => $country_log['id'])); } } if (strlen($country_log['client_ip']) > 7) { //4x# 4x. minimum valid ip if (strstr($country_log['client_ip'], ',')) { $country_log['client_ip'] = str_replace(' ', '', $country_log['client_ip']); $ips = explode(",", $country_log['client_ip']); reset($ips); //get the first ip, is supposed to be where the request originated from $ip = current($ips); } else { $ip = $country_log['client_ip']; } if (!in_array($ip, $cached_ips)) { //don't overuse this service unnecessary $cached_ips[] = $ip; if ($country = SiteLibrary::get_country_from_ip($ip)) { $command->update('log_detail', array('cronned' => 1, 'client_country_id' => $country->id), 'id=:id', array(':id' => $country_log['id'])); $cached_country[$ip] = $country->id; } } else { $command->update('log_detail', array('cronned' => 1, 'client_country_id' => $cached_country[$ip]), 'id=:id', array(':id' => $country_log['id'])); } } //Wether country was updated or not we should mark it as cronned so we don't process this record again $command->update('log_detail', array('cronned' => 1), 'id=:id', array(':id' => $country_log['id'])); } }
/** * Log each appropiate request on the application * */ private function log_request() { $command = Yii::app()->db->createCommand(); //Do NOT log some specific actions in some specific conditions //ie:We should not be logging each user subject fetch unless there is a new subject(that would be a line in the log every 10 sec) if ($this->action->Id == 'js') { $this->no_log = true; } if (Log::is_bot($_SERVER['HTTP_USER_AGENT'])) { $this->no_log = true; } ///Dont log if its a bot request if ($this->no_log == false) { try { $command->insert('log', array('time' => SiteLibrary::utc_time(), 'session_id' => Yii::app()->getSession()->getSessionID(), 'user_id' => (int) Yii::app()->user->id, 'controller' => $this->id, 'action' => $this->action->Id, 'uri' => Yii::App()->request->getRequestUri(), 'model' => get_class($this->model), 'model_id' => isset($this->model->id) ? (int) $this->model->id : 0, 'theme' => Yii::app()->getTheme() ? strtolower(substr(Yii::app()->getTheme()->getName(), 0, 2)) : 're')); //Get real clients ip if from a proxy $client_ip = ""; $client_host = !empty($_SERVER["HTTP_X_FORWARDED_HOST"]) ? $_SERVER["HTTP_X_FORWARDED_HOST"] : ""; if (!empty($_SERVER["HTTP_CLIENT_IP"])) { $client_ip = $_SERVER["HTTP_CLIENT_IP"]; //first verify ip from share internet } elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) { $client_ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; //if not then for a proxy user } else { } //Only the first request should go here //if(Yii::App()->request->getCookies()->getCount() == 0)//If cookies are not set this might be the first request if (Yii::app()->session->get('site_loaded') != "yes") { $command->insert('log_detail', array('log_id' => Yii::app()->db->getLastInsertID(), 'session' => Yii::app()->getSession()->getSessionID(), 'client_ip' => $client_ip, 'client_host' => $client_host, 'request_ip' => $_SERVER['REMOTE_ADDR'], 'request_host' => $_SERVER['REMOTE_HOST'], 'agent' => $_SERVER['HTTP_USER_AGENT'], 'referer' => $_SERVER['HTTP_REFERER'], 'charset' => $_SERVER['HTTP_ACCEPT_CHARSET'], 'language' => $_SERVER['HTTP_ACCEPT_LANGUAGE'], 'device' => substr(SiteLibrary::get_device(), 0, 2))); } } catch (CException $e) { Yii::log($e, 'warning', 'system.web.Controller'); } //'params'=>serialize($this->actionParams) } }
/** * Adds one point(either like or dislike) for the current model. * @param int $comment_id of the comment * @param int $vote wether like or dislike * @param int $user_id the user id * @return Array with the success(boolean indicating success or not), comment_id, likes and dislikes count */ public function add_vote($comment_id, $vote, $user_id) { $model = Comment::model()->findByPk((int) $comment_id); if ($model === null) { return array('success' => false, 'message' => Yii::t('comment', 'The comment_id was not found.')); } $likes = $model->likes; $dislikes = $model->dislikes; $model2 = new CommentVote(); $model2->comment_id = $comment_id; $model2->user_id = $user_id; $model2->vote = $vote == "like" ? 1 : 0; $model2->time = SiteLibrary::utc_time(); if (!$model2->save()) { return array('success' => false, 'message' => Yii::t('comment', 'Only one vote per user allowed.')); } if ($vote == "like") { $model->likes = $model->likes + 1; $likes = $model->likes; } else { $model->dislikes = $model->dislikes + 1; $dislikes = $model->dislikes; } $model->save(); //Update Live comments table if needed(if record doesnt exists, it simply wont update anything) Yii::app()->db->createCommand()->update('live_comment', array('likes' => $likes, 'dislikes' => $dislikes), 'comment_id=:comment_id', array(':comment_id' => $comment_id)); return array('success' => true, 'comment_id' => $comment_id, 'likes' => $likes, 'dislikes' => $dislikes); }
/** * Generates the proper html depending on the content_type_id * @param object $subject the an instance of the Subject class * @param string $mode in wich to return information(html or array of data) * @return mixed string the html content OR array with content values */ public function subject_content($subject, $mode = 'html') { switch ($subject->content_type_id) { case 1: if ($subject->content_image->url) { $img_url = $subject->content_image->url; } else { $img_url = Yii::app()->getRequest()->getBaseUrl(true) . '/' . $subject->content_image->path . '/'; $filename = $subject->content_image->id . '.' . $subject->content_image->extension; $img_url .= Yii::app()->getTheme()->name == 'mobile' ? "small_" . $filename : $filename; } $html = '<img src="' . $img_url . '" class="content_image">'; //Yii::app()->getRequest()->getHostInfo(). if ($subject->content_image->url) { $parsed_url = parse_url($subject->content_image->url); $html .= "<br><span>Image source: " . $parsed_url['scheme'] . '://' . $parsed_url['host']; } $arr_content['image'] = $img_url; break; case 2: $html = SiteHelper::formatted($subject->content_text->text); break; case 3: preg_match_all('#\\bhttps?://[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^[:punct:]\\s]|/))#', $subject->content_video->embed_code, $match); //would be nice to use yii validator to see if its an url but: http://code.google.com/p/yii/issues/detail?id=1324 if (SiteLibrary::valid_url($match[0][0])) { //if its an url(if we could extract an url from the content) $parsed_url = parse_url($match[0][0]); $query_arr = SiteLibrary::parse_url_query($parsed_url['query']); if (stristr($parsed_url['host'], 'youtube.com')) { //and its from youtube //get the V value $parsed_url['query']; if (array_key_exists('v', $query_arr)) { //set ?wmode=opaque because so that the the #header_top div does not get bellow the movie //http://stackoverflow.com/questions/3820325/overlay-opaque-div-over-youtube-iframe $time = $query_arr['t'] ? '#at=' . $query_arr['t'] : ''; //#at=** is the correct syntax for embed url, ie clic on the youtube logo while wathching a video on X seconds: it will open a new window with that param $html = '<iframe width="640" height="390" src="http://www.youtube.com/embed/' . $query_arr['v'] . '?wmode=opaque' . $time . '" frameborder="0" allowfullscreen></iframe>'; } else { $time = $query_arr['t'] ? '#at=' . $query_arr['t'] : ''; //#at=** is the correct syntax for embed url, ie clic on the youtube logo while wathching a video on X seconds: it will open a new window with that param $vid_url = str_replace("/v/", "", $parsed_url['path']); //remove the old embed code variable just in case it is present if (strpos($vid_url, "?")) { $vid_url = substr($vid_url, 0, strpos($vid_url, "?")); } elseif (strpos($vid_url, "&")) { $vid_url = substr($vid_url, 0, strpos($vid_url, "&")); } //remove the old params $vid_url = str_replace("/embed/", "", $vid_url); //remove the old embed code variable just in case it is present $html = '<iframe width="640" height="390" src="http://www.youtube.com/embed/' . $vid_url . '?wmode=opaque' . $time . '" frameborder="0" allowfullscreen></iframe>'; } } elseif (stristr($parsed_url['host'], 'youtu.be')) { //and its from youtube shortly $time = $query_arr['t'] ? '#at=' . $query_arr['t'] : ''; //#at=** is the correct syntax for embed url, ie clic on the youtube logo while wathching a video on X seconds: it will open a new window with that param $html = '<iframe width="640" height="390" src="http://www.youtube.com/embed/' . $parsed_url['path'] . '?wmode=opaque' . $time . '" frameborder="0" allowfullscreen></iframe>'; } elseif (stristr($parsed_url['host'], 'dailymotion.com')) { //the code is before the first undersore for the video source(pending verify if thats the syntax for all cases) if ($last_code_pos = stripos($parsed_url['path'], '_')) { $video_code = substr($parsed_url['path'], 1, $last_code_pos - 1); if ($video_code) { $html = '<iframe frameborder="0" width="640" height="390" src="http://www.dailymotion.com/embed/' . $video_code . '"></iframe>'; } //$video_code already contains: /videdo/ } } elseif (stristr($parsed_url['host'], 'vimeo.com')) { if ($parsed_url['path']) { //nice, we can play with params here, title, portrait, etc $html = '<iframe src="http://player.vimeo.com/video' . $parsed_url['path'] . '?title=0&byline=0&portrait=0" width="640" height="390" frameborder="0"></iframe>'; } } else { $html = SiteHelper::formatted($subject->content_video->embed_code); } } else { $html = SiteHelper::formatted($subject->content_video->embed_code); } //Get the iframe source url and set the image url for ogtags //Also set opaque property for youtube videos //src="...." second match (((?!").)*) searches for any string NOT containing " as that is our delimitter used in third match $pattern = '/(src=")(((?!").)*)(")/i'; preg_match($pattern, $html, $matches); //position 0 is the full match $arr_content['url'] = $matches[2]; if (SiteLibrary::valid_url($arr_content['url'])) { //if its an url $parsed_url = parse_url($arr_content['url']); $query_arr = SiteLibrary::parse_url_query($parsed_url['query']); if (stripos($parsed_url['host'], 'youtube.com')) { $pattern2 = strpos($arr_content['url'], '?') ? '/(embed\\/)(.+)(\\?)/i' : '/(embed\\/)(.+)$/i'; //the url can have extra params (a ? mark to pass extra params to the video) preg_match($pattern2, $arr_content['url'], $matches); //position 0 is the full match $arr_content['code'] = $matches[2]; $arr_content['image'] = 'http://img.youtube.com/vi/' . $arr_content['code'] . '/default.jpg'; //Intercept the content html iframe url of youtube videos and set opaque property if not set if (!strpos($arr_content['url'], 'wmode=opaque')) { $opaque_mode = strpos($arr_content['url'], '?') ? '&wmode=opaque' : '?wmode=opaque'; $html = preg_replace('/(.*)(src=")(((?!").)*)(")(.*)/i', '${1}$2' . $arr_content['url'] . $opaque_mode . '$5$6', $html); } } //TODO: get schema for images in other providers } //intercept the content html and resize any width or height depending on current theme if (Yii::app()->getTheme()->name == 'mobile') { $max_width = 250; $max_height = 190; } else { $max_width = 640; $max_height = 440; } $pattern = '/(width=")(\\d+)(")/i'; //Replace: width="***" ---> width="MOBILE_WIDTH" preg_match($pattern, $html, $matches); //width will fall in thrid position as position 0 is the full match(see func definition) if ((int) $matches[2] > $max_width) { $html = preg_replace($pattern, '${1}' . $max_width . '$3', $html); } $pattern = '/(height=")(\\d+)(")/i'; preg_match($pattern, $html, $matches); //width will fall in thrid position as position 0 is the full match(see func definition) if ((int) $matches[2] > $max_height) { $html = preg_replace($pattern, '${1}' . $max_height . '$3', $html); } $replacement = '${1}190$3'; //TODO: http://css-tricks.com/7066-fluid-width-youtube-videos/ break; } return $mode == 'array' ? $arr_content : $html; }
public function get_time_intervals($type = 'day') { if ($type == 'ymd') { $utc_time = SiteLibrary::utc_time(); //A 30 days iteration from NOW for ($i = 0; $i < 30; $i++) { $next_date = strtotime("+" . $i . " days", $utc_time); $next_date_formatted = date("Y", $next_date) . "/" . date("m", $next_date) . "/" . date("d", $next_date); $next_date_formatted_txt = date("Y", $next_date) . " / " . date("m", $next_date) . " / " . date("d", $next_date); if ($i == 0) { $next_date_formatted_txt = Yii::t('site', 'Today'); } if ($i == 1) { $next_date_formatted_txt = Yii::t('site', 'Tomorrow'); } $dates[$next_date_formatted] = $next_date_formatted_txt; } return $dates; } elseif ($type == 'day') { return array('1' => 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31); //set first element to one so that array does not initializes it value on 0 } elseif ($type == 'hour') { return array(0 => '12 AM', 1 => '01 AM', 2 => '02 AM', 3 => '03 AM', 4 => '04 AM', 5 => '05 AM', 6 => '06 AM', 7 => '07 AM', 8 => '08 AM', 9 => '09 AM', 10 => '10 AM', 11 => '11 AM', 12 => '12 PM', 13 => '01 PM', 14 => '02 PM', 15 => '03 PM', 16 => '04 PM', 17 => '05 PM', 18 => '06 PM', 19 => '07 PM', 20 => '08 PM', 21 => '09 PM', 22 => '10 PM', 23 => '11 PM'); } elseif ($type == 'minute') { return array('00' => '00', '05' => '05', '10' => 10, '15' => 15, '20' => 20, '25' => 25, '30' => 30, '35' => 35, '40' => 40, '45' => 45, '50' => 50, '55' => 55); } else { return false; } }
/** * Next step after password reset has been requested. The user types in the new password. */ public function actionResetPasswordNext($reset_hash) { if (!($this->model = User::model()->find('reset_hash=:reset_hash AND reset_time>:reset_time', array(':reset_hash' => $reset_hash, ':reset_time' => SiteLibrary::utc_time() - 604800)))) { //expires in 1 week throw new CHttpException(404, Yii::t('user', 'Sorry but the reset code in the link is incorrect or has expired, or you have already reset your password. Please repeat the process or contact us.')); } $this->model->scenario = 'resetpasswordnext'; if (isset($_POST['User'])) { $this->model->attributes = $_POST['User']; $this->model->salt = $this->model->generateSalt(); //lets give it a new salt also, just in case $this->model->password = $this->model->hashPassword($this->model->newpassword, $this->model->salt); $this->model->reset_hash = rand(1000, 9000) . "_" . SiteLibrary::utc_time() . "_" . $this->model->reset_hash; if ($this->model->save()) { Yii::app()->user->setFlash('layout_flash_success', Yii::t('user', 'Your password has been changed successfully. You may now login with your new password.')); $this->redirect(array('site/login')); } else { $this->model->password = $_POST['User']['password']; } } $this->render('resetpasswordnext', array('model' => $this->model)); }
tick=window.setTimeout("clock()",1000); } } clock(); </script> <p id="time_remaining" style="text-align: center;font-size: 30px; font-family: Impact"><?php echo Yii::t('subject', 'Time remaining'); ?> </p> <?php } else { //$model->position > SiteLibrary::utc_time() if (SiteLibrary::utc_time() - $model->show_time <= Yii::app()->params['subject_interval'] * 60) { $redirect_url = Yii::app()->getRequest()->getBaseUrl(true); } else { $redirect_url = Yii::app()->getRequest()->getBaseUrl(true) . '/sub/' . $model->urn; } ?> <script> tick=window.setTimeout(function (){ top.location="<?php echo $redirect_url; ?> "; },5000); </script> <p style="color:red; text-align: center;font-size: 30px; font-family: Impact"><?php echo Yii::t('subject', 'Subject already shown on: {date} UTC', array('{date}' => date("Y/m/d", $model->position) . ' ' . date("H", $model->position) . ':' . date("i", $model->position))); ?> </p>
</div> <div class="row"> <?php echo $form->labelEx($model, 'datetime'); ?> <?php echo $form->error($model, 'user_position'); ?> <?php echo $form->DropDownList($model, 'user_position_ymd', SiteLibrary::get_time_intervals('ymd')); ?> <?php echo $form->DropDownList($model, 'user_position_hour', SiteLibrary::get_time_intervals('hour')); ?> :<?php echo $form->DropDownList($model, 'user_position_minute', SiteLibrary::get_time_intervals('minute')); ?> <span><?php echo $form->CheckBox($model, 'user_position_anydatetime'); echo Yii::t('subject', 'Any date and time.'); ?> </span> </div> <?php if (extension_loaded('gd') and $this->action->id == 'add') { ?> <div class="row"> <?php echo $form->labelEx($model, 'verifyCode'); ?>
/** * Time Board. */ public function actionTimeboard($id = null, $day = null, $hour = null, $minute = null) { if (Yii::app()->user->checkAccess('subject_manage')) { $utc_time = SiteLibrary::utc_time(); //If there are any position changes update the timeboard first if ($id and $day and isset($hour) and isset($minute)) { //hour and minute can be 0 thats why we use isset instead of simple if //if day is less than today then set month as next future month, if ($day < (int) date("j", $utc_time)) { $month = date("m", $utc_time) == '12' ? 1 : (int) date("m", $utc_time) + 1; $year = (int) date("Y", $utc_time) + 1; } else { $month = date("m", $utc_time); $year = date("Y", $utc_time); } $position = strtotime($year . "-" . $month . "-" . $day . " " . $hour . ":" . $minute . ":00", $utc_time); //$position = strtotime("2012-06-10 14:28"); //echo $position. $year."-".$month."-".$day." ".$hour.":".$minute.":00"; //die($position); Subject::set_position($id, $position); } $this->model = new Subject('manage'); $this->model->unsetAttributes(); // clear any default values $this->model->authorized = 1; $this->model->approved = 1; $this->model->disabled = 0; $this->model->deleted = 0; $this->model->position = ">=" . SiteLibrary::utc_time_interval(); if (isset($_GET['Subject'])) { $this->model->attributes = $_GET['Subject']; } $live_subject = Yii::app()->db->createCommand()->select('*')->from('live_subject')->queryRow(); //if(! isset($this->model->disabled)) $this->model->disabled = 0;//Set to view only NOT disabled subjects by default(notice isset insted of a simple if) $this->render('timeboard', array('model' => $this->model, 'live_subject' => $live_subject)); } else { throw new CHttpException(403, Yii::t('subject', 'You are not allowed to manage subjects.')); } }
/** * Adds one point(either like or dislike) for the current model. * @param int $subject_id of the subject * @param int $vote wether like or dislike * @param int $user_id the user id * @return Array with the subject_id, likes and dislikes count */ public function add_vote($subject_id, $vote, $user_id) { $model = Subject::model()->findByPk((int) $subject_id); if ($model === null) { return array('success' => false, 'message' => Yii::t('subject', 'The subject_id was not found.')); } $likes = $model->likes; $dislikes = $model->dislikes; $model2 = new SubjectVote(); $model2->subject_id = $subject_id; $model2->user_id = $user_id; $model2->vote = $vote == "like" ? 1 : 0; $model2->time = SiteLibrary::utc_time(); if (!$model2->save()) { return array('success' => false, 'message' => Yii::t('subject', 'Only one vote per user allowed.')); } if ($vote == "like") { $model->likes = $model->likes + 1; $likes = $model->likes; } else { $model->dislikes = $model->dislikes + 1; $dislikes = $model->dislikes; } $model->save(); //Update Live subjects table if needed //Notice we are sending the subject id parameter as condition(if record doesnt exists, it simply wont update anything) Yii::app()->db->createCommand()->update('live_subject', array('subject_data' => serialize($model)), 'subject_id=:subject_id', array(':subject_id' => $subject_id)); return array('success' => true, 'subject_id' => $subject_id, 'likes' => $likes, 'dislikes' => $dislikes); }
/** * Do some things prior to save * */ public function beforeSave() { //If its a new record if ($this->getIsNewRecord()) { $this->salt = $this->generateSalt(); $this->password = $this->hashPassword($this->password, $this->salt); } if ($this->scenario != 'login') { $this->time_modified = SiteLibrary::utc_time(); } //login also saves data return true; }
?> <script type="text/javascript"> var element1 = document.createElement("link"); element1.type="text/css"; element1.rel = "stylesheet"; element1.href = "<?php echo Yii::app()->getRequest()->getBaseUrl(true); ?> /css/core-<?php echo filemtime($filepath . '/css/core.css'); ?> .css"; document.getElementsByTagName("head")[0].appendChild(element1); <?php $time = SiteLibrary::utc_time(); ?> var utc_time = <?php echo $time; ?> ; var utc_hour = <?php echo date("H", $time); ?> ; var utc_min = <?php echo date("i", $time); ?> ; var utc_sec = <?php
</h3> <?php $comments = Comment::model()->with('user', 'country')->findAll("subject_id = {$model->id}"); $total_comments = count($comments); if ($total_comments == 0) { echo "<h4>" . Yii::t('subject', 'NO COMMENTS') . "</h4>"; } foreach ($comments as $comment) { ?> <div class="comment" id="c<?php echo $comment->id; ?> "> <div class="comment_info"> <?php $time_since_comment = SiteLibrary::time_since(SiteLibrary::utc_time() - $comment->time); echo '<span class="comment_number">' . str_pad($comment->comment_number, 2, '0', STR_PAD_LEFT) . '</span>' . '<span class="comment_country">' . $comment->country->code . '</span>' . ' <span>' . CHtml::link($comment->user->username, array('mysub/' . $comment->user->username)) . '</span>' . ' <span title="' . date("Y/m/d H:i", $comment->time) . ' UTC ' . '">' . Yii::t('comment', '{time_number} {time_name} ago', array('{time_number}' => $time_since_comment[0], '{time_name}' => Yii::t('site', $time_since_comment[1]))) . '</span>'; echo SiteHelper::comment_vote($comment->id, $comment->likes, $comment->dislikes); ?> </div> <div class="comment_content"> <?php echo nl2br(CHtml::encode($comment->comment)); ?> </div> </div><!-- comment --> <?php } ?>