Advertisement
petrabarus

Untitled

Sep 2nd, 2011
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.84 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  *
  5.  */
  6. class User extends CActiveRecord {
  7.     //TODO: Reserved for future use
  8.     const USER_TYPE_USER = 0;
  9.     const USER_TYPE_DUMMY = 1;
  10.  
  11.     public $password_repeat;
  12.     public $email_repeat;
  13.     public $verifyCode;
  14.     private $original_password;
  15.  
  16.     public static function model($className=__CLASS__) {
  17.         return parent::model($className);
  18.     }
  19.  
  20.     public static function checkUsernameEmail($username) {
  21.         if (preg_match("/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/", $username)) {
  22.             $record = User::model()->exists('LOWER(email)=?', array(strtolower($username)));
  23.         } else {
  24.             $record = User::model()->exists('LOWER(username)=?', array(strtolower($username)));
  25.         }
  26.         return $record;
  27.     }
  28.  
  29.     public static function findbyUsernameEmail($username) {
  30.         if (preg_match("/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/", $username)) {
  31.             $record = User::model()->find('LOWER(email)=?', array(strtolower($username)));
  32.         } else {
  33.             $record = User::model()->find('LOWER(username)=?', array(strtolower($username)));
  34.         }
  35.         return $record;
  36.     }
  37.  
  38.     public function tableName() {
  39.         return '{{users}}';
  40.     }
  41.  
  42.     public function afterFind() {
  43.         $this->original_password = $this->password;
  44.         return parent::afterFind();
  45.     }
  46.  
  47.     public function beforeSave() {
  48.         if ($this->isNewRecord) {
  49.             $this->password = sha1($this->password);
  50.         } else {
  51.             if ($this->password != $this->original_password) {
  52.                 $this->password = sha1($this->password);
  53.             }
  54.         }
  55.         //
  56.         $notags = array('site_url', 'institution',
  57.             'institution_address', 'institution_phone',
  58.             'city');
  59.         foreach ($notags as $ntags) {
  60.             $this->$ntags = strip_tags($this->$ntags);
  61.             $this->$ntags = CHtml::encode($this->$ntags);
  62.         }
  63.         return parent::beforeSave();
  64.     }
  65.  
  66.     public function rules() {
  67.         return array(
  68.             array('username', 'length', 'max' => 35, 'min' => 3),
  69.             array('password', 'length', 'min' => 6),
  70.             array('username', 'match', 'pattern' => '/^[a-zA-Z][a-zA-Z0-9_]+$/', 'message' => '{attribute} is invalid. Only number, alphabet, and underscore allowed'),
  71.             array('full_name', 'match', 'pattern' => '/^[\p{L}\s]+$/', 'message' => '{attribute} is invalid. Only alphabet allowed'),
  72.             array('email', 'length', 'max' => 40),
  73.             array('email', 'email'),
  74.             array('username, email', 'unique', 'caseSensitive' => false),
  75.             array('verifyCode', 'captcha', 'allowEmpty' => !extension_loaded('gd'), 'on' => 'register'),
  76.             array('password', 'compare', 'on' => 'register, forgotPassword'),
  77.             array('password', 'compare', 'on' => 'register'),
  78.             //required
  79.             array('full_name', 'required', 'on' => 'register, create, setting, adminUpdate'),
  80.             array('username', 'required', 'on' => 'register, create, setting, adminUpdate'),
  81.             array('email', 'required', 'on' => 'register, create, setting, adminUpdate'),
  82.             array('password', 'required', 'on' => 'register, create, forgotPassword'),
  83.             array('password_repeat', 'required', 'on' => 'register, forgotPassword'),
  84.             array('email_repeat, verifyCode', 'required', 'on' => 'register'),
  85.             array('type', 'required', 'on' => 'adminUpdate'),
  86.             //safe
  87.             array('site_url, institution, institution_address, institution_phone, city, additional_information', 'safe'),
  88.         );
  89.     }
  90.  
  91.     public function relations() {
  92.         return array(
  93.             'groups' => array(self::MANY_MANY, 'Group', 'groups_users(user_id, group_id)'),
  94.         );
  95.     }
  96.  
  97.     public function getProfileUrl() {
  98.         return Yii::app()->createUrl('profile/view', array('id' => $this->id));
  99.     }
  100.  
  101.     public function getFullnameLink() {
  102.         return CHtml::link($this->full_name, $this->getProfileUrl());
  103.     }
  104.  
  105.     public function isDummy() {
  106.         return ($this->type == self::USER_TYPE_DUMMY);
  107.     }
  108.  
  109.     public static function getOnlineUsers($seconds = 300){
  110.         //SELECT COUNT(*) FROM `users` WHERE TIME_TO_SEC(TIMEDIFF(NOW(), last_activity)) < 300
  111.         $users = self::model()->findAll(array(
  112.             'select' => array('id', 'username', 'full_name'),
  113.             'condition' => "TIME_TO_SEC(TIMEDIFF(NOW(), last_activity)) < $seconds",
  114.         ));
  115.         return $users;
  116.     }
  117.  
  118.     public static function getOnlineUserCount($seconds = 300){
  119.         $usercount = self::model()->count(array(
  120.             'condition' => "TIME_TO_SEC(TIMEDIFF(NOW(), last_activity)) < $seconds",
  121.         ));
  122.         return $usercount;
  123.     }
  124.  
  125.     public function attributeLabels() {
  126.         return array(
  127.             'id' => 'ID',
  128.             'full_name' => 'Nama Panjang',
  129.             'username' => 'Username',
  130.             'email_repeat' => 'Ulang Email',
  131.             'password' => 'Sandi',
  132.             'password_repeat' => 'Ulang Sandi',
  133.             'verifyCode' => 'Kode Verifikasi',
  134.             'join_date' => 'Bergabung',
  135.             'site_url' => 'Alamat Situs',
  136.             'institution' => 'Institusi/Sekolah',
  137.             'institution_address' => 'Alamat Institusi/Sekolah',
  138.             'institution_phone' => 'Telepon Institusi/Sekolah',
  139.             'city' => 'Provinsi/Kota',
  140.         );
  141.     }
  142.  
  143.     public function generateActivationCode() {
  144.         $this->activation_code = CTextHelper::random('alnum', 32);
  145.         $this->save(false);
  146.     }
  147.  
  148. }
  149.  
  150. //end of file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement