XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
userutility.php
Go to the documentation of this file.
1 <?php
20 defined('XOOPS_ROOT_PATH') or die('Restricted access');
21 
29 {
36  function sendWelcome($user)
37  {
39 
40  if (empty($xoopsConfigUser)) {
41  $config_handler =& xoops_gethandler('config');
42  $xoopsConfigUser = $config_handler->getConfigsByCat(XOOPS_CONF_USER);
43  }
44  if (empty($xoopsConfigUser['welcome_type'])) {
45  return true;
46  }
47 
48  if (!empty($user) && !is_object($user)) {
49  $member_handler =& xoops_gethandler('member');
50  $user =& $member_handler->getUser($user);
51  }
52  if (!is_object($user)) {
53  return false;
54  }
55 
56  xoops_loadLanguage('user');
57  $xoopsMailer =& xoops_getMailer();
58  if ($xoopsConfigUser['welcome_type'] == 1 || $xoopsConfigUser['welcome_type'] == 3) {
59  $xoopsMailer->useMail();
60  }
61  if ($xoopsConfigUser['welcome_type'] == 2 || $xoopsConfigUser['welcome_type'] == 3) {
62  $xoopsMailer->usePM();
63  }
64  $xoopsMailer->setTemplate('welcome.tpl');
65  $xoopsMailer->setSubject(sprintf(_US_WELCOME_SUBJECT, $xoopsConfig['sitename']));
66  $xoopsMailer->setToUsers($user);
67  if ($xoopsConfigUser['reg_dispdsclmr'] && $xoopsConfigUser['reg_disclaimer']) {
68  $xoopsMailer->assign('TERMSOFUSE', $xoopsConfigUser['reg_disclaimer']);
69  } else {
70  $xoopsMailer->assign('TERMSOFUSE', '');
71  }
72  return $xoopsMailer->send();
73  }
82  function validate()
83  {
84  global $xoopsUser;
85 
86  $args = func_get_args();
87  $args_num = func_num_args();
88 
89  $user = null;
90  $uname = null;
91  $email = null;
92  $pass = null;
93  $vpass = null;
94 
95  switch ($args_num) {
96  case 1:
97  $user = $args[0];
98  break;
99  case 2:
100  list ($uname, $email) = $args;
101  break;
102  case 3:
103  list ($user, $pass, $vpass) = $args;
104  break;
105  case 4:
106  list ($uname, $email, $pass, $vpass) = $args;
107  break;
108  default:
109  return false;
110  }
111  if (is_object($user)) {
112  $uname = $user->getVar('uname', 'n');
113  $email = $user->getVar('email', 'n');
114  }
115 
116  $config_handler =& xoops_gethandler('config');
118 
119  xoops_loadLanguage('user');
121 
122  $xoopsUser_isAdmin = is_object($xoopsUser) && $xoopsUser->isAdmin();
123  $stop = '';
124  // Invalid email address
125  if (!checkEmail($email)) {
126  $stop .= _US_INVALIDMAIL . '<br />';
127  }
128  if (strrpos($email, ' ') > 0) {
129  $stop .= _US_EMAILNOSPACES . '<br />';
130  }
131  // Check forbidden email address if current operator is not an administrator
132  if (!$xoopsUser_isAdmin) {
133  foreach ($xoopsConfigUser['bad_emails'] as $be) {
134  if (!empty($be) && preg_match('/' . $be . '/i', $email)) {
135  $stop .= _US_INVALIDMAIL . '<br />';
136  break;
137  }
138  }
139  }
141  switch ($xoopsConfigUser['uname_test_level']) {
142  case 0:
143  // strict
144  $restriction = '/[^a-zA-Z0-9\_\-]/';
145  break;
146  case 1:
147  // medium
148  $restriction = '/[^a-zA-Z0-9\_\-<>\,\.\$\%\#\@\!\\\'\']/';
149  break;
150  case 2:
151  // loose
152  $restriction = '/[\000-\040]/';
153  break;
154  }
155  if (empty($uname) || preg_match($restriction, $uname)) {
156  $stop .= _US_INVALIDNICKNAME . '<br />';
157  }
158  // Check uname settings if current operator is not an administrator
159  if (!$xoopsUser_isAdmin) {
160  if (strlen($uname) > $xoopsConfigUser['maxuname']) {
161  $stop .= sprintf(_US_NICKNAMETOOLONG, $xoopsConfigUser['maxuname']) . '<br />';
162  }
163  if (strlen($uname) < $xoopsConfigUser['minuname']) {
164  $stop .= sprintf(_US_NICKNAMETOOSHORT, $xoopsConfigUser['minuname']) . '<br />';
165  }
166  foreach ($xoopsConfigUser['bad_unames'] as $bu) {
167  if (!empty($bu) && preg_match('/' . $bu . '/i', $uname)) {
168  $stop .= _US_NAMERESERVED . '<br />';
169  break;
170  }
171  }
177  }
179  // Check if uname/email already exists if the user is a new one
180  $uid = is_object($user) ? $user->getVar('uid') : 0;
181  $sql = 'SELECT COUNT(*) FROM `' . $xoopsDB->prefix('users') . '` WHERE `uname` = ' . $xoopsDB->quote(addslashes($uname)) . (($uid > 0) ? " AND `uid` <> {$uid}" : '');
182  $result = $xoopsDB->query($sql);
183  list ($count) = $xoopsDB->fetchRow($result);
184  if ($count > 0) {
185  $stop .= _US_NICKNAMETAKEN . '<br />';
186  }
187  $sql = 'SELECT COUNT(*) FROM `' . $xoopsDB->prefix('users') . '` WHERE `email` = ' . $xoopsDB->quote(addslashes($email)) . (($uid > 0) ? " AND `uid` <> {$uid}" : '');
188  $result = $xoopsDB->query($sql);
189  list ($count) = $xoopsDB->fetchRow($result);
190  if ($count > 0) {
191  $stop .= _US_EMAILTAKEN . '<br />';
192  }
193  // If password is not set, skip password validation
194  if ($pass === null && $vpass === null) {
195  return $stop;
196  }
197 
198  if (!isset($pass) || $pass == '' || !isset($vpass) || $vpass == '') {
199  $stop .= _US_ENTERPWD . '<br />';
200  }
201  if ((isset($pass)) && ($pass != $vpass)) {
202  $stop .= _US_PASSNOTSAME . '<br />';
203  } else if (($pass != '') && (strlen($pass) < $xoopsConfigUser['minpass'])) {
204  $stop .= sprintf(_US_PWDTOOSHORT, $xoopsConfigUser['minpass']) . '<br />';
205  }
206  return $stop;
207  }
208 
217  function getIP($asString = false)
218  {
219  // Gets the proxy ip sent by the user
220  $proxy_ip = '';
221  if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
222  $proxy_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
223  } else
224  if (!empty($_SERVER['HTTP_X_FORWARDED'])) {
225  $proxy_ip = $_SERVER['HTTP_X_FORWARDED'];
226  } else
227  if (! empty($_SERVER['HTTP_FORWARDED_FOR'])) {
228  $proxy_ip = $_SERVER['HTTP_FORWARDED_FOR'];
229  } else
230  if (!empty($_SERVER['HTTP_FORWARDED'])) {
231  $proxy_ip = $_SERVER['HTTP_FORWARDED'];
232  } else
233  if (!empty($_SERVER['HTTP_VIA'])) {
234  $proxy_ip = $_SERVER['HTTP_VIA'];
235  } else
236  if (!empty($_SERVER['HTTP_X_COMING_FROM'])) {
237  $proxy_ip = $_SERVER['HTTP_X_COMING_FROM'];
238  } else
239  if (!empty($_SERVER['HTTP_COMING_FROM'])) {
240  $proxy_ip = $_SERVER['HTTP_COMING_FROM'];
241  }
242  if (!empty($proxy_ip) && $is_ip = preg_match('/^([0-9]{1,3}.){3,3}[0-9]{1,3}/', $proxy_ip, $regs) && count($regs) > 0) {
243  $the_IP = $regs[0];
244  } else {
245  $the_IP = $_SERVER['REMOTE_ADDR'];
246  }
247 
248  $the_IP = ($asString) ? $the_IP : ip2long($the_IP);
249 
250  return $the_IP;
251  }
252 
261  function getUnameFromIds($uid, $usereal = false, $linked = false)
262  {
263  if (!is_array($uid)) {
264  $uid = array($uid);
265  }
266  $userid = array_map('intval', array_filter($uid));
267 
269  $users = array();
270  if (count($userid) > 0) {
272  $sql = 'SELECT uid, uname, name FROM ' . $xoopsDB->prefix('users') . ' WHERE level > 0 AND uid IN(' . implode(',', array_unique($userid)) . ')';
273  if (!$result = $xoopsDB->query($sql)) {
274  return $users;
275  }
276  while ($row = $xoopsDB->fetchArray($result)) {
277  $uid = $row['uid'];
278  if ($usereal && $row['name']) {
279  $users[$uid] = $myts->htmlSpecialChars($row['name']);
280  } else {
281  $users[$uid] = $myts->htmlSpecialChars($row['uname']);
282  }
283  if ($linked) {
284  $users[$uid] = '<a href="' . XOOPS_URL . '/userinfo.php?uid=' . $uid . '" title="' . $users[$uid] . '">' . $users[$uid] . '</a>';
285  }
286  }
287  }
288  if (in_array(0, $users, true)) {
289  $users[0] = $myts->htmlSpecialChars($GLOBALS['xoopsConfig']['anonymous']);
290  }
291  return $users;
292  }
293 
302  function getUnameFromId($userid, $usereal = false, $linked = false)
303  {
305  $userid = intval($userid);
306  $username = '';
307  if ($userid > 0) {
308  $member_handler = &xoops_gethandler('member');
309  $user =& $member_handler->getUser($userid);
310  if (is_object($user)) {
311  if ($usereal && $user->getVar('name')) {
312  $username = $user->getVar('name');
313  } else {
314  $username = $user->getVar('uname');
315  }
316  if (!empty($linked)) {
317  $username = '<a href="' . XOOPS_URL . '/userinfo.php?uid=' . $userid . '" title="' . $username . '">' . $username . '</a>';
318  }
319  }
320  }
321  if (empty($username)) {
322  $username = $myts->htmlSpecialChars($GLOBALS['xoopsConfig']['anonymous']);
323  }
324  return $username;
325  }
326 }
327 
328 ?>