XOOPS  2.6.0
userutility.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
24 {
32  public static function sendWelcome($user)
33  {
35 
36  if (!$xoops->getConfig('welcome_type')) {
37  return true;
38  }
39 
40  if (!empty($user) && !is_object($user)) {
41  $member_handler = $xoops->getHandlerMember();
42  $user = $member_handler->getUser($user);
43  }
44  if (!is_object($user)) {
45  return false;
46  }
47 
48  $xoopsMailer = $xoops->getMailer();
49  if ($xoops->getConfig('welcome_type') == 1 || $xoops->getConfig('welcome_type') == 3) {
50  $xoopsMailer->useMail();
51  }
52  if ($xoops->getConfig('welcome_type') == 2 || $xoops->getConfig('welcome_type') == 3) {
53  $xoopsMailer->usePM();
54  }
55  $xoopsMailer->setTemplate('welcome.tpl');
56  $xoopsMailer->setSubject(sprintf(XoopsLocale::F_WELCOME_TO, $xoops->getConfig('sitename')));
57  $xoopsMailer->setToUsers($user);
58  if ($xoops->getConfig('reg_disclaimer')) {
59  $xoopsMailer->assign('TERMSOFUSE', $xoops->getConfig('reg_disclaimer'));
60  } else {
61  $xoopsMailer->assign('TERMSOFUSE', '');
62  }
63  return $xoopsMailer->send();
64  }
65 
71  public static function validate()
72  {
74  $args = func_get_args();
75  $args_num = func_num_args();
76 
77  /* @var $user XoopsUser|null */
78  $user = null;
79  $uname = null;
80  $email = null;
81  $pass = null;
82  $vpass = null;
83 
84  switch ($args_num) {
85  case 1:
86  $user = $args[0];
87  break;
88  case 2:
89  list ($uname, $email) = $args;
90  break;
91  case 3:
92  list ($user, $pass, $vpass) = $args;
93  break;
94  case 4:
95  list ($uname, $email, $pass, $vpass) = $args;
96  break;
97  default:
98  return false;
99  }
100  if (is_object($user)) {
101  $uname = $user->getVar('uname', 'n');
102  $email = $user->getVar('email', 'n');
103  }
104 
105  //$user = empty($user) ? null : trim($user);
106  $uname = empty($uname) ? null : trim($uname);
107  $email = empty($email) ? null : trim($email);
108  $pass = empty($pass) ? null : trim($pass);
109  $vpass = empty($vpass) ? null : trim($vpass);
110 
111  $xoops->getConfigs();
112 
113  $stop = '';
114  // Invalid email address
115  if (!$xoops->checkEmail($email)) {
116  $stop .= XoopsLocale::E_INVALID_EMAIL . '<br />';
117  }
118  if (strrpos($email, ' ') > 0) {
120  }
121  // Check forbidden email address if current operator is not an administrator
122  if (!$xoops->userIsAdmin) {
123  $bad_emails = $xoops->getConfig('bad_emails');
124  if (!empty($bad_emails)) {
125  foreach ($bad_emails as $be) {
126  if (!empty($be) && preg_match('/' . $be . '/i', $email)) {
127  $stop .= XoopsLocale::E_INVALID_EMAIL . '<br />';
128  break;
129  }
130  }
131  }
132  }
134  $restriction = '';
135  switch ($xoops->getConfig('uname_test_level')) {
136  case 0:
137  // strict
138  $restriction = '/[^a-zA-Z0-9\_\-]/';
139  break;
140  case 1:
141  // medium
142  $restriction = '/[^a-zA-Z0-9\_\-<>\,\.\$\%\#\@\!\\\'\']/';
143  break;
144  case 2:
145  // loose
146  $restriction = '/[\000-\040]/';
147  break;
148  }
149  if (empty($uname) || preg_match($restriction, $uname)) {
150  $stop .= XoopsLocale::E_INVALID_USERNAME . '<br />';
151  }
152  // Check uname settings if current operator is not an administrator
153  if (!$xoops->userIsAdmin) {
154  $maxuname = $xoops->getConfig('maxuname');
155  if (!empty($maxuname) && mb_strlen($uname) > $maxuname) {
156  $stop .= sprintf(XoopsLocale::EF_USERNAME_MUST_BE_LESS_THAN, $maxuname) . '<br />';
157  }
158  $minuname = $xoops->getConfig('minuname');
159  if (!empty($minuname) && mb_strlen($uname) < $minuname) {
160  $stop .= sprintf(XoopsLocale::EF_USERNAME_MUST_BE_MORE_THAN, $minuname) . '<br />';
161  }
162  $bad_unames = $xoops->getConfig('bad_unames');
163  if (!empty($bad_unames)) {
164  foreach ($bad_unames as $bu) {
165  if (!empty($bu) && preg_match('/' . $bu . '/i', $uname)) {
166  $stop .= XoopsLocale::E_NAME_IS_RESERVED . '<br />';
167  break;
168  }
169  }
170  }
171  }
172  // Check if uname/email already exists if the user is a new one
173  $uid = is_object($user) ? $user->getVar('uid') : 0;
174 
175  $user_handler = $xoops->getHandlerUser();
177 
178  $criteria = new CriteriaCompo(new Criteria('uname', $myts->addSlashes($uname)));
179  if ($uid > 0) {
180  $criteria->add(new Criteria('uid', $uid, '<>'));
181  }
182  $count = $user_handler->getCount($criteria);
183  if ($count > 0) {
184  $stop .= XoopsLocale::E_USERNAME_TAKEN . '<br />';
185  }
186 
187  $criteria = new CriteriaCompo(new Criteria('email', $myts->addSlashes($email)));
188  if ($uid > 0) {
189  $criteria->add(new Criteria('uid', $uid, '<>'));
190  }
191  $count = $user_handler->getCount($criteria);
192  if ($count > 0) {
193  $stop .= XoopsLocale::E_EMAIL_TAKEN . '<br />';
194  }
195 
196  // If password is not set, skip password validation
197  if ($pass === null && $vpass === null) {
198  return $stop;
199  }
200 
201  if (empty($pass) || empty($vpass)) {
202  $stop .= XoopsLocale::E_MUST_PROVIDE_PASSWORD . '<br />';
203  }
204  if (isset($pass) && isset($vpass) && ($pass != $vpass)) {
205  $stop .= XoopsLocale::E_PASSWORDS_MUST_MATCH . '<br />';
206  } else {
207  $minpass = $xoops->getConfig('minpass');
208  if (($pass != '') && (!empty($minpass)) && (mb_strlen($pass) < $minpass)) {
209  $stop .= sprintf(XoopsLocale::EF_PASSWORD_MUST_BE_GREATER_THAN, $minpass) . '<br />';
210  }
211  }
212  return $stop;
213  }
214 
224  public static function getIP($asString = false)
225  {
226  // Gets the proxy ip sent by the user
227  $proxy_ip = '';
228  if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
229  $proxy_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
230  } else {
231  if (!empty($_SERVER['HTTP_X_FORWARDED'])) {
232  $proxy_ip = $_SERVER['HTTP_X_FORWARDED'];
233  } else {
234  if (!empty($_SERVER['HTTP_FORWARDED_FOR'])) {
235  $proxy_ip = $_SERVER['HTTP_FORWARDED_FOR'];
236  } else {
237  if (!empty($_SERVER['HTTP_FORWARDED'])) {
238  $proxy_ip = $_SERVER['HTTP_FORWARDED'];
239  } else {
240  if (!empty($_SERVER['HTTP_VIA'])) {
241  $proxy_ip = $_SERVER['HTTP_VIA'];
242  } else {
243  if (!empty($_SERVER['HTTP_X_COMING_FROM'])) {
244  $proxy_ip = $_SERVER['HTTP_X_COMING_FROM'];
245  } else {
246  if (!empty($_SERVER['HTTP_COMING_FROM'])) {
247  $proxy_ip = $_SERVER['HTTP_COMING_FROM'];
248  }
249  }
250  }
251  }
252  }
253  }
254  }
255  if (!empty($proxy_ip) && preg_match('/^([0-9]{1,3}\.){3,3}[0-9]{1,3}/', $proxy_ip, $regs) && count($regs) > 0) {
256  $the_IP = $regs[0];
257  } else {
258  $the_IP = $_SERVER['REMOTE_ADDR'];
259  }
260 
261  $the_IP = ($asString) ? $the_IP : ip2long($the_IP);
262 
263  return $the_IP;
264  }
265 
275  public static function getUnameFromIds($uids, $usereal = false, $linked = false)
276  {
278  if (!is_array($uids)) {
279  $uids = array($uids);
280  }
281  $userids = array_map('intval', array_filter($uids));
282 
284  $users = array();
285  if (count($userids) > 0) {
286  $criteria = new CriteriaCompo(new Criteria('level', 0, '>'));
287  $criteria->add(new Criteria('uid', "('" . implode(',', array_unique($userids)) . "')", 'IN'));
288 
289  $user_handler = $xoops->getHandlerUser();
290  if (!$rows = $user_handler->getAll($criteria, array('uid', 'uname', 'name'), false, true)) {
291  return $users;
292  }
293  foreach ($rows as $uid => $row) {
294  if ($usereal && $row['name']) {
295  $users[$uid] = $myts->htmlSpecialChars($row['name']);
296  } else {
297  $users[$uid] = $myts->htmlSpecialChars($row['uname']);
298  }
299  if ($linked) {
300  $users[$uid] = '<a href="' . \XoopsBaseConfig::get('url') . '/userinfo.php?uid='
301  . $uid . '" title="' . $users[$uid] . '">' . $users[$uid] . '</a>';
302  }
303  }
304  }
305  if (in_array(0, $users, true)) {
306  $users[0] = $myts->htmlSpecialChars($xoops->getConfig('anonymous'));
307  }
308  return $users;
309  }
310 
320  public static function getUnameFromId($userid, $usereal = false, $linked = false)
321  {
324  $userid = intval($userid);
325  $username = '';
326  if ($userid > 0) {
327  $member_handler = $xoops->getHandlerMember();
328  $user = $member_handler->getUser($userid);
329  if (is_object($user)) {
330  if ($usereal && $user->getVar('name')) {
331  $username = $user->getVar('name');
332  } else {
333  $username = $user->getVar('uname');
334  }
335  if (!empty($linked)) {
336  $username = '<a href="' . \XoopsBaseConfig::get('url') . '/userinfo.php?uid='
337  . $userid . '" title="' . $username . '">' . $username . '</a>';
338  }
339  }
340  }
341  if (empty($username)) {
342  $username = $myts->htmlSpecialChars($xoops->getConfig('anonymous'));
343  }
344  return $username;
345  }
346 }
const E_MUST_PROVIDE_PASSWORD
Definition: en_US.php:348
const E_INVALID_EMAIL
Definition: en_US.php:334
$user_handler
Definition: findusers.php:52
static getUnameFromId($userid, $usereal=false, $linked=false)
if($uname== ''||$pass== '') $member_handler
Definition: checklogin.php:44
const EF_PASSWORD_MUST_BE_GREATER_THAN
Definition: en_US.php:266
const F_WELCOME_TO
Definition: en_US.php:448
static validate()
Definition: userutility.php:71
static getInstance()
Definition: Xoops.php:160
$user
Definition: checklogin.php:47
$_SERVER['REQUEST_URI']
const E_INVALID_USERNAME
Definition: en_US.php:338
static getUnameFromIds($uids, $usereal=false, $linked=false)
$vpass
Definition: register.php:74
static sendWelcome($user)
Definition: userutility.php:32
const E_EMAIL_SHOULD_NOT_CONTAIN_SPACES
Definition: en_US.php:321
$xoops
Definition: admin.php:25
const E_EMAIL_TAKEN
Definition: en_US.php:322
static get($name)
static getIP($asString=false)
const E_USERNAME_TAKEN
Definition: en_US.php:373
if(!$xoops->isUser()) $uid
Definition: index.php:31
const E_NAME_IS_RESERVED
Definition: en_US.php:349
const E_PASSWORDS_MUST_MATCH
Definition: en_US.php:359
static trim($text)
Definition: Abstract.php:272
const EF_USERNAME_MUST_BE_MORE_THAN
Definition: en_US.php:276
$uname
Definition: checklogin.php:37
$criteria
$pass
Definition: checklogin.php:38
$myts
Definition: edituser.php:38
const EF_USERNAME_MUST_BE_LESS_THAN
Definition: en_US.php:275
$email
Definition: lostpass.php:32