1: <?php
2: /*****************************************************************************************
3: * X2Engine Open Source Edition is a customer relationship management program developed by
4: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
5: *
6: * This program is free software; you can redistribute it and/or modify it under
7: * the terms of the GNU Affero General Public License version 3 as published by the
8: * Free Software Foundation with the addition of the following permission added
9: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
11: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12: *
13: * This program is distributed in the hope that it will be useful, but WITHOUT
14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16: * details.
17: *
18: * You should have received a copy of the GNU Affero General Public License along with
19: * this program; if not, see http://www.gnu.org/licenses or write to the Free
20: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21: * 02110-1301 USA.
22: *
23: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
24: * California 95067, USA. or at email address contact@x2engine.com.
25: *
26: * The interactive user interfaces in modified source and object code versions
27: * of this program must display Appropriate Legal Notices, as required under
28: * Section 5 of the GNU Affero General Public License version 3.
29: *
30: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31: * these Appropriate Legal Notices must retain the display of the "Powered by
32: * X2Engine" logo. If the display of the logo is not reasonably feasible for
33: * technical reasons, the Appropriate Legal Notices must display the words
34: * "Powered by X2Engine".
35: *****************************************************************************************/
36:
37: /**
38: * This is the model class for table "x2_sessions".
39: *
40: * @package application.models
41: * @property integer $id
42: * @property string $user
43: * @property integer $lastUpdated
44: * @property string $IP
45: * @property integer $status
46: */
47: class Session extends CActiveRecord {
48:
49: /**
50: * Returns the static model of the specified AR class.
51: * @return Session the static model class
52: */
53: public static function model($className=__CLASS__) {
54: return parent::model($className);
55: }
56:
57: /**
58: * @return string the associated database table name
59: */
60: public function tableName() {
61: return 'x2_sessions';
62: }
63:
64: /**
65: * @return array relational rules.
66: */
67: public function relations() {
68: // NOTE: you may need to adjust the relation name and the related
69: // class name for the relations automatically generated below.
70: return array();
71: }
72:
73: /**
74: * @return array customized attribute labels (name=>label)
75: */
76: public function attributeLabels() {
77: return array(
78: 'id' => Yii::t('app','Sesesion ID'),
79: 'user' => Yii::t('app','User'),
80: 'lastUpdated' => Yii::t('app','Last Updated'),
81: 'IP' => Yii::t('app','IP Address'),
82: 'status' => Yii::t('app','Login Status'),
83: );
84: }
85:
86: /**
87: * Retrieves a list of models based on the current search/filter conditions.
88: * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
89: */
90: public function search() {
91: // Warning: Please modify the following code to remove attributes that
92: // should not be searched.
93:
94: $criteria=new CDbCriteria;
95:
96: $criteria->compare('id',$this->id);
97: $criteria->compare('user',$this->user,true);
98: $criteria->compare('IP',$this->IP,true);
99: $criteria->compare('lastUpdated',$this->lastUpdated);
100: $criteria->compare('status',$this->status);
101:
102: return new CActiveDataProvider(get_class($this), array(
103: 'criteria'=>$criteria,
104: ));
105: }
106:
107: public static function getOnlineUsers($useTimeout = false) {
108: // $sessions = Session::model()->findAllByAttributes(array('status'=>1));
109: // $temp = array();
110: // foreach($sessions as $session)
111: // $temp[] = $session->user;
112: // return $temp;
113:
114: $query = Yii::app()->db->createCommand()
115: ->selectDistinct('user')
116: ->from('x2_sessions')
117: ->where('status=1');
118: if($useTimeout)
119: $query = $query->where('lastUpdated > "'.(time()-900).'"');
120:
121: return $query->queryColumn();
122: }
123:
124: /**
125: * @param string $username
126: * @return bool true if user has a recently updated session record, false otherwise
127: */
128: public static function isOnline ($username) {
129: $record = Yii::app()->db->createCommand()
130: ->select('*')
131: ->from('x2_sessions')
132: ->where('status=1 and user=:username and lastUpdated > "'.(time () - 900).
133: '"', array ('username' => $username))
134: ->queryAll ();
135:
136: return (!empty ($record));
137: }
138:
139: /**
140: * Clear session records which have timed out. Log the timeout.
141: */
142: public static function cleanUpSessions () {
143: // Only select users with active sessions to clear out, in case there are
144: // dozens of inactive users, to make things more efficient:
145: $users = Yii::app()->db->createCommand()
146: ->select('x2_users.id,x2_users.username')
147: ->from('x2_users')
148: ->rightJoin('x2_sessions', 'x2_sessions.user = x2_users.username')
149: ->where('x2_users.username IS NOT NULL AND x2_users.username != ""')
150: ->queryAll();
151: foreach($users as $user){
152: $timeout = Roles::getUserTimeout($user['id']);
153: $sessions = X2Model::model('Session')->findAllByAttributes(
154: array('user' => $user['username']),
155: 'lastUpdated < :cutoff',
156: array(':cutoff' => time() - $timeout));
157: foreach($sessions as $session){
158: SessionLog::logSession($session->user, $session->id, 'passiveTimeout');
159: $session->delete();
160: }
161: }
162:
163: // check timeout on sessions not corresponding to any existing user
164: $defaultTimeout = 900;
165: self::model ()->deleteAll (
166: array (
167: 'condition' => 'lastUpdated < :cutoff and
168: user not in (select distinct (username) from x2_users)',
169: 'params' => array (':cutoff' => time () - $defaultTimeout)
170: )
171: );
172: }
173: }
174: