PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/class/userutility.php

https://gitlab.com/VoyaTrax/vtCMS2
PHP | 329 lines | 230 code | 22 blank | 77 comment | 68 complexity | e3a3bfce32632eba517a33aa386ad8c4 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, MIT, GPL-3.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Xoops Form Class Elements
  4. *
  5. * You may not change or alter any portion of this comment or credits
  6. * of supporting developers from this source code or any supporting source code
  7. * which is considered copyrighted (c) material of the original comment or credit authors.
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
  13. * @license GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
  14. * @package kernel
  15. * @since 2.3.0
  16. * @author Taiwen Jiang <phppp@users.sourceforge.net>
  17. */
  18. defined('XOOPS_ROOT_PATH') || exit('Restricted access');
  19. /**
  20. * XoopsUserUtility
  21. *
  22. * @package Kernel
  23. * @author Taiwen Jiang <phppp@users.sourceforge.net>
  24. */
  25. class XoopsUserUtility
  26. {
  27. /**
  28. * XoopsUserUtility::sendWelcome
  29. *
  30. * @param mixed $user
  31. *
  32. * @return bool
  33. */
  34. public static function sendWelcome($user)
  35. {
  36. global $xoopsConfigUser, $xoopsConfig;
  37. if (empty($xoopsConfigUser)) {
  38. $config_handler = xoops_getHandler('config');
  39. $xoopsConfigUser = $config_handler->getConfigsByCat(XOOPS_CONF_USER);
  40. }
  41. if (empty($xoopsConfigUser['welcome_type'])) {
  42. return true;
  43. }
  44. if (!empty($user) && !is_object($user)) {
  45. $member_handler = xoops_getHandler('member');
  46. $user = $member_handler->getUser($user);
  47. }
  48. if (!is_object($user)) {
  49. return false;
  50. }
  51. xoops_loadLanguage('user');
  52. $xoopsMailer =& xoops_getMailer();
  53. if ($xoopsConfigUser['welcome_type'] == 1 || $xoopsConfigUser['welcome_type'] == 3) {
  54. $xoopsMailer->useMail();
  55. }
  56. if ($xoopsConfigUser['welcome_type'] == 2 || $xoopsConfigUser['welcome_type'] == 3) {
  57. $xoopsMailer->usePM();
  58. }
  59. $xoopsMailer->setTemplate('welcome.tpl');
  60. $xoopsMailer->setSubject(sprintf(_US_WELCOME_SUBJECT, $xoopsConfig['sitename']));
  61. $xoopsMailer->setToUsers($user);
  62. if ($xoopsConfigUser['reg_dispdsclmr'] && $xoopsConfigUser['reg_disclaimer']) {
  63. $xoopsMailer->assign('TERMSOFUSE', $xoopsConfigUser['reg_disclaimer']);
  64. } else {
  65. $xoopsMailer->assign('TERMSOFUSE', '');
  66. }
  67. return $xoopsMailer->send();
  68. }
  69. /**
  70. * $uname, $email, $pass = null, $vpass = null
  71. */
  72. /**
  73. * XoopsUserUtility::validate
  74. *
  75. * @return bool|string
  76. */
  77. public static function validate()
  78. {
  79. global $xoopsUser;
  80. $args = func_get_args();
  81. $args_num = func_num_args();
  82. $user = null;
  83. $uname = null;
  84. $email = null;
  85. $pass = null;
  86. $vpass = null;
  87. switch ($args_num) {
  88. case 1:
  89. $user = $args[0];
  90. break;
  91. case 2:
  92. list($uname, $email) = $args;
  93. break;
  94. case 3:
  95. list($user, $pass, $vpass) = $args;
  96. break;
  97. case 4:
  98. list($uname, $email, $pass, $vpass) = $args;
  99. break;
  100. default:
  101. return false;
  102. }
  103. if (is_object($user)) {
  104. $uname = $user->getVar('uname', 'n');
  105. $email = $user->getVar('email', 'n');
  106. }
  107. $config_handler = xoops_getHandler('config');
  108. $xoopsConfigUser = $config_handler->getConfigsByCat(XOOPS_CONF_USER);
  109. xoops_loadLanguage('user');
  110. $myts = MyTextSanitizer::getInstance();
  111. $xoopsUser_isAdmin = is_object($xoopsUser) && $xoopsUser->isAdmin();
  112. $stop = '';
  113. // Invalid email address
  114. if (!checkEmail($email)) {
  115. $stop .= _US_INVALIDMAIL . '<br />';
  116. }
  117. if (strrpos($email, ' ') > 0) {
  118. $stop .= _US_EMAILNOSPACES . '<br />';
  119. }
  120. // Check forbidden email address if current operator is not an administrator
  121. if (!$xoopsUser_isAdmin) {
  122. foreach ($xoopsConfigUser['bad_emails'] as $be) {
  123. if (!empty($be) && preg_match('/' . $be . '/i', $email)) {
  124. $stop .= _US_INVALIDMAIL . '<br />';
  125. break;
  126. }
  127. }
  128. }
  129. $uname = xoops_trim($uname);
  130. switch ($xoopsConfigUser['uname_test_level']) {
  131. case 0:
  132. // strict
  133. $restriction = '/[^a-zA-Z0-9\_\-]/';
  134. break;
  135. case 1:
  136. // medium
  137. $restriction = '/[^a-zA-Z0-9\_\-\<\>\,\.\$\%\#\@\!\\\'\']/';
  138. break;
  139. case 2:
  140. // loose
  141. $restriction = '/[\000-\040]/';
  142. break;
  143. }
  144. if (empty($uname) || preg_match($restriction, $uname)) {
  145. $stop .= _US_INVALIDNICKNAME . '<br />';
  146. }
  147. // Check uname settings if current operator is not an administrator
  148. if (!$xoopsUser_isAdmin) {
  149. if (strlen($uname) > $xoopsConfigUser['maxuname']) {
  150. $stop .= sprintf(_US_NICKNAMETOOLONG, $xoopsConfigUser['maxuname']) . '<br />';
  151. }
  152. if (strlen($uname) < $xoopsConfigUser['minuname']) {
  153. $stop .= sprintf(_US_NICKNAMETOOSHORT, $xoopsConfigUser['minuname']) . '<br />';
  154. }
  155. foreach ($xoopsConfigUser['bad_unames'] as $bu) {
  156. if (!empty($bu) && preg_match('/' . $bu . '/i', $uname)) {
  157. $stop .= _US_NAMERESERVED . '<br />';
  158. break;
  159. }
  160. }
  161. /**
  162. * if (strrpos($uname, ' ') > 0) {
  163. * $stop .= _US_NICKNAMENOSPACES . '<br />';
  164. * }
  165. */
  166. }
  167. $xoopsDB = XoopsDatabaseFactory::getDatabaseConnection();
  168. // Check if uname/email already exists if the user is a new one
  169. $uid = is_object($user) ? $user->getVar('uid') : 0;
  170. $sql = 'SELECT COUNT(*) FROM `' . $xoopsDB->prefix('users') . '` WHERE `uname` = ' . $xoopsDB->quote(addslashes($uname)) . (($uid > 0) ? " AND `uid` <> {$uid}" : '');
  171. $result = $xoopsDB->query($sql);
  172. list($count) = $xoopsDB->fetchRow($result);
  173. if ($count > 0) {
  174. $stop .= _US_NICKNAMETAKEN . '<br />';
  175. }
  176. $sql = 'SELECT COUNT(*) FROM `' . $xoopsDB->prefix('users') . '` WHERE `email` = ' . $xoopsDB->quote(addslashes($email)) . (($uid > 0) ? " AND `uid` <> {$uid}" : '');
  177. $result = $xoopsDB->query($sql);
  178. list($count) = $xoopsDB->fetchRow($result);
  179. if ($count > 0) {
  180. $stop .= _US_EMAILTAKEN . '<br />';
  181. }
  182. // If password is not set, skip password validation
  183. if ($pass === null && $vpass === null) {
  184. return $stop;
  185. }
  186. if (!isset($pass) || $pass == '' || !isset($vpass) || $vpass == '') {
  187. $stop .= _US_ENTERPWD . '<br />';
  188. }
  189. if (isset($pass) && ($pass != $vpass)) {
  190. $stop .= _US_PASSNOTSAME . '<br />';
  191. } elseif (($pass != '') && (strlen($pass) < $xoopsConfigUser['minpass'])) {
  192. $stop .= sprintf(_US_PWDTOOSHORT, $xoopsConfigUser['minpass']) . '<br />';
  193. }
  194. return $stop;
  195. }
  196. /**
  197. * Get client IP
  198. *
  199. * Adapted from PMA_getIp() [phpmyadmin project]
  200. *
  201. * @param bool $asString requiring integer or dotted string
  202. * @return mixed string or integer value for the IP
  203. */
  204. public static function getIP($asString = false)
  205. {
  206. // Gets the proxy ip sent by the user
  207. $proxy_ip = '';
  208. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  209. $proxy_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  210. } elseif (!empty($_SERVER['HTTP_X_FORWARDED'])) {
  211. $proxy_ip = $_SERVER['HTTP_X_FORWARDED'];
  212. } elseif (!empty($_SERVER['HTTP_FORWARDED_FOR'])) {
  213. $proxy_ip = $_SERVER['HTTP_FORWARDED_FOR'];
  214. } elseif (!empty($_SERVER['HTTP_FORWARDED'])) {
  215. $proxy_ip = $_SERVER['HTTP_FORWARDED'];
  216. } elseif (!empty($_SERVER['HTTP_VIA'])) {
  217. $proxy_ip = $_SERVER['HTTP_VIA'];
  218. } elseif (!empty($_SERVER['HTTP_X_COMING_FROM'])) {
  219. $proxy_ip = $_SERVER['HTTP_X_COMING_FROM'];
  220. } elseif (!empty($_SERVER['HTTP_COMING_FROM'])) {
  221. $proxy_ip = $_SERVER['HTTP_COMING_FROM'];
  222. }
  223. if (!empty($proxy_ip)) {
  224. $ip = new \Xmf\IPAddress($proxy_ip);
  225. if (false === $ip->asReadable()) {
  226. $ip = \Xmf\IPAddress::fromRequest();
  227. }
  228. } else {
  229. $ip = \Xmf\IPAddress::fromRequest();
  230. }
  231. // this really should return $ip->asBinary() instead of ip2long, but for IPv6, this will
  232. // return false when the ip2long() fails. Callers are not expecting binary strings.
  233. $the_IP = $asString ? $ip->asReadable() : ip2long($ip->asReadable());
  234. return $the_IP;
  235. }
  236. /**
  237. * XoopsUserUtility::getUnameFromIds()
  238. *
  239. * @param mixed $uid
  240. * @param mixed $usereal
  241. * @param mixed $linked
  242. * @return array
  243. */
  244. public static function getUnameFromIds($uid, $usereal = false, $linked = false)
  245. {
  246. if (!is_array($uid)) {
  247. $uid = array($uid);
  248. }
  249. $userid = array_map('intval', array_filter($uid));
  250. $myts = MyTextSanitizer::getInstance();
  251. $users = array();
  252. if (count($userid) > 0) {
  253. $xoopsDB = XoopsDatabaseFactory::getDatabaseConnection();
  254. $sql = 'SELECT uid, uname, name FROM ' . $xoopsDB->prefix('users') . ' WHERE level > 0 AND uid IN(' . implode(',', array_unique($userid)) . ')';
  255. if (!$result = $xoopsDB->query($sql)) {
  256. return $users;
  257. }
  258. while ($row = $xoopsDB->fetchArray($result)) {
  259. $uid = $row['uid'];
  260. if ($usereal && $row['name']) {
  261. $users[$uid] = $myts->htmlSpecialChars($row['name']);
  262. } else {
  263. $users[$uid] = $myts->htmlSpecialChars($row['uname']);
  264. }
  265. if ($linked) {
  266. $users[$uid] = '<a href="' . XOOPS_URL . '/userinfo.php?uid=' . $uid . '" title="' . $users[$uid] . '">' . $users[$uid] . '</a>';
  267. }
  268. }
  269. }
  270. if (in_array(0, $users, true)) {
  271. $users[0] = $myts->htmlSpecialChars($GLOBALS['xoopsConfig']['anonymous']);
  272. }
  273. return $users;
  274. }
  275. /**
  276. * XoopsUserUtility::getUnameFromId()
  277. *
  278. * @param mixed $userid
  279. * @param mixed $usereal
  280. * @param mixed $linked
  281. * @return string
  282. */
  283. public static function getUnameFromId($userid, $usereal = false, $linked = false)
  284. {
  285. $myts = MyTextSanitizer::getInstance();
  286. $userid = (int)$userid;
  287. $username = '';
  288. if ($userid > 0) {
  289. $member_handler = xoops_getHandler('member');
  290. $user = $member_handler->getUser($userid);
  291. if (is_object($user)) {
  292. if ($usereal && $user->getVar('name')) {
  293. $username = $user->getVar('name');
  294. } else {
  295. $username = $user->getVar('uname');
  296. }
  297. if (!empty($linked)) {
  298. $username = '<a href="' . XOOPS_URL . '/userinfo.php?uid=' . $userid . '" title="' . $username . '">' . $username . '</a>';
  299. }
  300. }
  301. }
  302. if (empty($username)) {
  303. $username = $myts->htmlSpecialChars($GLOBALS['xoopsConfig']['anonymous']);
  304. }
  305. return $username;
  306. }
  307. }