/** * 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)); }
/** * 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); }
/** * 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; } }
/** * 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) } }
/** * 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>
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; } }
/** * 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; }
/** * This is a cron that sets the next subject to be showed * and the next subject to be cached(subject_id and subject_id_2) * */ public function actionSetNextSubject() { if ($_SERVER['REMOTE_ADDR'] != '127.0.0.1') { die; } //Only allow to run this locally $command = Yii::app()->db->createCommand(); //If the table its empty by any reason(initial import), insert something to make the UPDATE work if (!$command->select('count(*) as num')->from('live_subject')->queryScalar()) { $command->insert('live_subject', array('subject_id' => 0, 'subject_id_2' => 0)); } //Position all subs on its time Subject::reschedule_positions(); $round_utc_time = SiteLibrary::utc_time_interval(); //Remote case: This update is just in case cron didn't run in x times of interva(s) //This frees up subs that never were used because they were fixed position but cron failed to run and time passed by Subject::model()->updateAll(array('position' => '0', 'user_position' => '0', 'manager_position' => '0'), 'position < ' . $round_utc_time . ' AND user_position < ' . $round_utc_time . ' AND manager_position < ' . $round_utc_time); $subject = Subject::model()->find(array('condition' => 'position >= ' . $round_utc_time . ' AND content_type_id <> 2 AND approved=1 AND authorized=1 AND disabled=0 AND deleted=0', 'order' => 'position ASC')); $live_subject = Yii::app()->db->createCommand()->select('*')->from('live_subject')->queryRow(); $command->delete('live_comment'); $command->update('live_subject', array('comment_id' => 0, 'comment_number' => 0)); //TEMPORAL:Refill the live_comments table with old comments about this subject if this subject is repeated $past_comments = Yii::app()->db->createCommand()->select('t1.id,code,time,comment,comment_number,username,likes,dislikes')->from('comment t1')->where('subject_id =' . $subject->id)->leftJoin('country t2', 'country_id=t2.id')->leftJoin('user t3', 'user_id=t3.id')->order('time ASC')->queryAll(); echo "<br>gggg"; print_r($past_comments); $i = 0; foreach ($past_comments as $past_comment) { $i++; $country_code = $past_comment['code'] ? $past_comment['code'] : "WW"; $command->insert('live_comment', array('comment_id' => $past_comment['id'], 'username' => $past_comment['username'], 'subject_id' => $subject->id, 'comment_country' => $country_code, 'comment_time' => $past_comment['time'], 'comment_text' => $past_comment['comment'], 'comment_number' => $i, 'likes' => $past_comment['likes'], 'dislikes' => $past_comment['dislikes'])); //we neet to use our own sequence because there might be repeated numbers $comment_id = $past_comment['id']; } if ($i > 0) { $command->update('live_subject', array('comment_id' => $comment_id, 'comment_number' => $i)); } $command->update('live_subject', array('subject_id' => $subject->id, 'scheduled_time' => SiteLibrary::utc_time_interval(), 'subject_data' => serialize($subject))); //Reset position as subject is going to live now Subject::model()->updateByPk($subject->id, array('show_time' => SiteLibrary::utc_time(), 'user_position' => 0, 'manager_position' => 0)); //Notify subject owner via email that his subject its gonna get LIVE $user = User::model()->findByPk($subject->user_id); if ($user->id != 1 and $user->notify_subject_live == 1) { $mail_message = Yii::t('subject', "Hi {username}, \nWe are writing to notify you that your subject got approved and that it is\ngoing to be placed in the live stream(Homepage) in the next 5 minutes.\nDetails\nSubject Title: {title}\nUploaded time: {uploaded_time} UTC\nCurrent time: {current_time} UTC (time of this message)\nEstimated time: {estimated_time} UTC (about 5 minutes)\nIt is even more cool if you chat with your friends about your upcomming subject.\nSo, invite them to go to samesub.com now, you still have 4 minutes.\nIf you do not want to receive this type of notification you can update the settings in\nyour user profile anytime you want.", array('{username}' => $user->username, '{title}' => $subject->title, '{uploaded_time}' => date("Y/m/d H:i", $subject->time_submitted), '{current_time}' => date("Y/m/d H:i", SiteLibrary::utc_time()), '{estimated_time}' => date("Y/m/d H:i", SiteLibrary::utc_time() + 300))); $mail_message .= "\n\n"; $mail_message .= Yii::t('site', "Thanks\nSincerely\nSamesub Team\nwww.samesub.com"); if (SiteLibrary::send_email($user->email, "Your subject is going LIVE", $mail_message)) { echo "An email has been sent."; } else { echo "Email could not be sent."; } } echo 'Done setting next subject_id_2 : ' . $subject->id; //There are some pages that need to be refreshed from the cache such as /subject/index, so that it content reflects the updated data. $optional_prefix = "index.php"; $cmd = Yii::app()->params['cache_refresher'] . ' "' . $optional_prefix . '/subject/index' . '"'; if (Yii::app()->params['cache_refresher']) { exec($cmd); } }
?> <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 } ?>
/** * 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 $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); }
/** * 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); }