1 <?php
2 3 4 5 6 7 8 9
10
11 class UserModule extends CWebModule
12 {
13 14 15 16
17 public $user_page_size = 10;
18
19 20 21 22
23 public $fields_page_size = 10;
24
25 26 27 28
29 public $hash='md5';
30
31 32 33 34
35 public $sendActivationMail=true;
36
37 38 39 40
41 public $loginNotActiv=false;
42
43 44 45 46
47 public $activeAfterRegister=false;
48
49 50 51 52
53 public $autoLogin=true;
54
55 public $registrationUrl = array("/user/registration");
56 public $recoveryUrl = array("/user/recovery/recovery");
57 public $loginUrl = array("/user/login");
58 public $logoutUrl = array("/user/logout");
59 public $profileUrl = array("/user/profile");
60 public $returnUrl = array("/user/profile");
61 public $returnLogoutUrl = array("/user/login");
62
63
64 65 66 67
68 public $rememberMeTime = 2592000;
69
70 public $fieldsMessage = '';
71
72 73 74 75 76
77 public $relations = array();
78
79 80 81 82
83 public $profileRelations = array();
84
85 86 87
88 public $captcha = array('registration'=>true);
89
90 91 92
93
94
95 public $tableUsers = '{{users}}';
96 public $tableProfiles = '{{profiles}}';
97 public $tableProfileFields = '{{profiles_fields}}';
98
99 public $defaultScope = array(
100 'with'=>array('profile'),
101 );
102
103 static private $_user;
104 static private $_users=array();
105 static private $_userByName=array();
106 static private $_admin;
107 static private $_admins;
108
109 110 111 112
113 public $componentBehaviors=array();
114
115 public function init()
116 {
117
118
119
120
121 $this->setImport(array(
122 'user.models.*',
123 'user.components.*',
124 ));
125 }
126
127 public function getBehaviorsFor($componentName){
128 if (isset($this->componentBehaviors[$componentName])) {
129 return $this->componentBehaviors[$componentName];
130 } else {
131 return array();
132 }
133 }
134
135 public function beforeControllerAction($controller, $action)
136 {
137 if(parent::beforeControllerAction($controller, $action))
138 {
139
140
141 return true;
142 }
143 else
144 return false;
145 }
146
147 148 149 150 151 152
153 public static function t($str='',$params=array(),$dic='user') {
154 if (Yii::t("UserModule", $str)==$str)
155 return Yii::t("UserModule.".$dic, $str, $params);
156 else
157 return Yii::t("UserModule", $str, $params);
158 }
159
160 161 162
163 public static function encrypting($string="") {
164 $hash = Yii::app()->getModule('user')->hash;
165 if ($hash=="md5")
166 return md5($string);
167 if ($hash=="sha1")
168 return sha1($string);
169 else
170 return hash($hash,$string);
171 }
172
173 174 175 176
177 public static function doCaptcha($place = '') {
178 if(!extension_loaded('gd'))
179 return false;
180 if (in_array($place, Yii::app()->getModule('user')->captcha))
181 return Yii::app()->getModule('user')->captcha[$place];
182 return false;
183 }
184
185 186 187 188
189 public static function isAdmin() {
190 if(Yii::app()->user->isGuest)
191 return false;
192 else {
193 if (!isset(self::$_admin)) {
194 if(self::user()->superuser)
195 self::$_admin = true;
196 else
197 self::$_admin = false;
198 }
199 return self::$_admin;
200 }
201 }
202
203 204 205 206
207 public static function getAdmins() {
208 if (!self::$_admins) {
209 $admins = User::model()->active()->superuser()->findAll();
210 $return_name = array();
211 foreach ($admins as $admin)
212 array_push($return_name,$admin->username);
213 self::$_admins = ($return_name)?$return_name:array('');
214 }
215 return self::$_admins;
216 }
217
218 219 220
221 public static function sendMail($email,$subject,$message) {
222 $adminEmail = Yii::app()->params['adminEmail'];
223 $headers = "MIME-Version: 1.0\r\nFrom: $adminEmail\r\nReply-To: $adminEmail\r\nContent-Type: text/html; charset=utf-8";
224 $message = wordwrap($message, 70);
225 $message = str_replace("\n.", "\n..", $message);
226 return mail($email,'=?UTF-8?B?'.base64_encode($subject).'?=',$message,$headers);
227 }
228
229 230 231
232 public function sendMailToUser($user_id,$subject,$message,$from='') {
233 $user = User::model()->findbyPk($user_id);
234 if (!$from) $from = Yii::app()->params['adminEmail'];
235 $headers="From: ".$from."\r\nReply-To: ".Yii::app()->params['adminEmail'];
236 return mail($user->email,'=?UTF-8?B?'.base64_encode($subject).'?=',$message,$headers);
237 }
238
239 240 241 242 243
244 public static function user($id=0,$clearCache=false) {
245 if (!$id&&!Yii::app()->user->isGuest)
246 $id = Yii::app()->user->id;
247 if ($id) {
248 if (!isset(self::$_users[$id])||$clearCache)
249 self::$_users[$id] = User::model()->with(array('profile'))->findbyPk($id);
250 return self::$_users[$id];
251 } else return false;
252 }
253
254 255 256 257 258
259 public static function getUserByName($username) {
260 if (!isset(self::$_userByName[$username])) {
261 $_userByName[$username] = User::model()->findByAttributes(array('username'=>$username));
262 }
263 return $_userByName[$username];
264 }
265
266 267 268 269 270
271 public function users() {
272 return User;
273 }
274 }
275