home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / unitednuke / unitednuke.exe / html / includes / functions_validate.php < prev    next >
PHP Script  |  2004-01-10  |  11KB  |  244 lines

  1. <?php
  2. /***************************************************************************
  3.  *                          functions_validate.php
  4.  *                            -------------------
  5.  *   begin                : Saturday, Feb 13, 2001
  6.  *   copyright            : (C) 2001 The phpBB Group
  7.  *   email                : support@phpbb.com
  8.  *
  9.  *   $Id: functions_validate.php,v 1.6.2.12 2003/06/09 19:13:05 psotfx Exp $
  10.  *
  11.  *
  12.  ***************************************************************************/
  13. /***************************************************************************
  14. * phpbb2 forums port version 2.0.5 (c) 2003 - Nuke Cops (http://nukecops.com)
  15. *
  16. * Ported by Nuke Cops to phpbb2 standalone 2.0.5 Test
  17. * and debugging completed by the Elite Nukers and site members.
  18. *
  19. * You run this package at your sole risk. Nuke Cops and affiliates cannot
  20. * be held liable if anything goes wrong. You are advised to test this
  21. * package on a development system. Backup everything before implementing
  22. * in a production environment. If something goes wrong, you can always
  23. * backout and restore your backups.
  24. *
  25. * Installing and running this also means you agree to the terms of the AUP
  26. * found at Nuke Cops.
  27. *
  28. * This is version 2.0.5 of the phpbb2 forum port for PHP-Nuke. Work is based
  29. * on Tom Nitzschner's forum port version 2.0.6. Tom's 2.0.6 port was based
  30. * on the phpbb2 standalone version 2.0.3. Our version 2.0.5 from Nuke Cops is
  31. * now reflecting phpbb2 standalone 2.0.5 that fixes some bugs and the
  32. * invalid_session error message.
  33. ***************************************************************************/
  34. /***************************************************************************
  35.  *   This file is part of the phpBB2 port to Nuke 6.0 (c) copyright 2002
  36.  *   by Tom Nitzschner (tom@toms-home.com)
  37.  *   http://bbtonuke.sourceforge.net (or http://www.toms-home.com)
  38.  *
  39.  *   As always, make a backup before messing with anything. All code
  40.  *   release by me is considered sample code only. It may be fully
  41.  *   functual, but you use it at your own risk, if you break it,
  42.  *   you get to fix it too. No waranty is given or implied.
  43.  *
  44.  *   Please post all questions/request about this port on http://bbtonuke.sourceforge.net first,
  45.  *   then on my site. All original header code and copyright messages will be maintained
  46.  *   to give credit where credit is due. If you modify this, the only requirement is
  47.  *   that you also maintain all original copyright messages. All my work is released
  48.  *   under the GNU GENERAL PUBLIC LICENSE. Please see the README for more information.
  49.  *
  50.  ***************************************************************************/
  51. /***************************************************************************
  52.  *
  53.  *   This program is free software; you can redistribute it and/or modify
  54.  *   it under the terms of the GNU General Public License as published by
  55.  *   the Free Software Foundation; either version 2 of the License, or
  56.  *   (at your option) any later version.
  57.  *
  58.  ***************************************************************************/
  59.  
  60. //
  61. // Check to see if the username has been taken, or if it is disallowed.
  62. // Also checks if it includes the " character, which we don't allow in usernames.
  63. // Used for registering, changing names, and posting anonymously with a username
  64. //
  65. function validate_username($username)
  66. {
  67.         global $db, $lang, $userdata;
  68.  
  69.         // Remove doubled up spaces
  70.         $username = preg_replace('#\s+#', ' ', $username);
  71.         // Limit username length
  72.         $username = substr(str_replace("\'", "'", $username), 0, 25);
  73.         $username = str_replace("'", "''", $username);
  74.  
  75.         $sql = "SELECT username
  76.                 FROM " . USERS_TABLE . "
  77.                 WHERE LOWER(username) = '" . strtolower($username) . "'";
  78.         if ($result = $db->sql_query($sql))
  79.         {
  80.                 if ($row = $db->sql_fetchrow($result))
  81.                 {
  82.                         if (($userdata['session_logged_in'] && $row['username'] != $userdata['username']) || !$userdata['session_logged_in'])
  83.                         {
  84.                                 $db->sql_freeresult($result);
  85.                                 return array('error' => true, 'error_msg' => $lang['Username_taken']);
  86.                         }
  87.                 }
  88.         }
  89.         $db->sql_freeresult($result);
  90.  
  91.         $sql = "SELECT group_name
  92.                 FROM " . GROUPS_TABLE . "
  93.                 WHERE LOWER(group_name) = '" . strtolower($username) . "'";
  94.         if ($result = $db->sql_query($sql))
  95.         {
  96.                 if ($row = $db->sql_fetchrow($result))
  97.                 {
  98.                         $db->sql_freeresult($result);
  99.                         return array('error' => true, 'error_msg' => $lang['Username_taken']);
  100.                 }
  101.         }
  102.         $db->sql_freeresult($result);
  103.  
  104.         $sql = "SELECT disallow_username
  105.                 FROM " . DISALLOW_TABLE;
  106.         if ($result = $db->sql_query($sql))
  107.         {
  108.                 if ($row = $db->sql_fetchrow($result))
  109.                 {
  110.                         do
  111.                         {
  112.                                 if (preg_match("#\b(" . str_replace("\*", ".*?", phpbb_preg_quote($row['disallow_username'], '#')) . ")\b#i", $username))
  113.                                 {
  114.                                         $db->sql_freeresult($result);
  115.                                         return array('error' => true, 'error_msg' => $lang['Username_disallowed']);
  116.                                 }
  117.                         }
  118.                         while($row = $db->sql_fetchrow($result));
  119.                 }
  120.         }
  121.         $db->sql_freeresult($result);
  122.  
  123.         $sql = "SELECT word
  124.                 FROM  " . WORDS_TABLE;
  125.         if ($result = $db->sql_query($sql))
  126.         {
  127.                 if ($row = $db->sql_fetchrow($result))
  128.                 {
  129.                         do
  130.                         {
  131.                                 if (preg_match("#\b(" . str_replace("\*", ".*?", phpbb_preg_quote($row['word'], '#')) . ")\b#i", $username))
  132.                                 {
  133.                                         $db->sql_freeresult($result);
  134.                                         return array('error' => true, 'error_msg' => $lang['Username_disallowed']);
  135.                                 }
  136.                         }
  137.                         while ($row = $db->sql_fetchrow($result));
  138.                 }
  139.         }
  140.         $db->sql_freeresult($result);
  141.  
  142.         // Don't allow " and ALT-255 in username.
  143.     if (strstr($username, '"') || strstr($username, '"') || strstr($username, chr(160)) || ereg("[^a-zA-Z0-9_-]",$username))
  144.         {
  145.                 return array('error' => true, 'error_msg' => $lang['Username_invalid']);
  146.         }
  147.  
  148.         return array('error' => false, 'error_msg' => '');
  149. }
  150.  
  151. //
  152. // Check to see if email address is banned
  153. // or already present in the DB
  154. //
  155. function validate_email($email)
  156. {
  157.         global $db, $lang;
  158.  
  159.         if ($email != '')
  160.         {
  161.                 if (preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*?[a-z]+$/is', $email))
  162.                 {
  163.                         $sql = "SELECT ban_email
  164.                                 FROM " . BANLIST_TABLE;
  165.                         if ($result = $db->sql_query($sql))
  166.                         {
  167.                                 if ($row = $db->sql_fetchrow($result))
  168.                                 {
  169.                                         do
  170.                                         {
  171.                                                 $match_email = str_replace('*', '.*?', $row['ban_email']);
  172.                                                 if (preg_match('/^' . $match_email . '$/is', $email))
  173.                                                 {
  174.                                                         $db->sql_freeresult($result);
  175.                                                         return array('error' => true, 'error_msg' => $lang['Email_banned']);
  176.                                                 }
  177.                                         }
  178.                                         while($row = $db->sql_fetchrow($result));
  179.                                 }
  180.                         }
  181.                         $db->sql_freeresult($result);
  182.  
  183.                         $sql = "SELECT user_email
  184.                                 FROM " . USERS_TABLE . "
  185.                                 WHERE user_email = '" . str_replace("\'", "''", $email) . "'";
  186.                         if (!($result = $db->sql_query($sql)))
  187.                         {
  188.                                 message_die(GENERAL_ERROR, "Couldn't obtain user email information.", "", __LINE__, __FILE__, $sql);
  189.                         }
  190.  
  191.                         if ($row = $db->sql_fetchrow($result))
  192.                         {
  193.                                 return array('error' => true, 'error_msg' => $lang['Email_taken']);
  194.                         }
  195.                         $db->sql_freeresult($result);
  196.  
  197.                         return array('error' => false, 'error_msg' => '');
  198.                 }
  199.         }
  200.  
  201.         return array('error' => true, 'error_msg' => $lang['Email_invalid']);
  202. }
  203.  
  204. //
  205. // Does supplementary validation of optional profile fields. This expects common stuff like trim() and strip_tags()
  206. // to have already been run. Params are passed by-ref, so we can set them to the empty string if they fail.
  207. //
  208. function validate_optional_fields(&$icq, &$aim, &$msnm, &$yim, &$website, &$location, &$occupation, &$interests, &$sig)
  209. {
  210.         $check_var_length = array('aim', 'msnm', 'yim', 'location', 'occupation', 'interests', 'sig');
  211.  
  212.         for($i = 0; $i < count($check_var_length); $i++)
  213.         {
  214.                 if (strlen($$check_var_length[$i]) < 2)
  215.                 {
  216.                         $$check_var_length[$i] = '';
  217.                 }
  218.         }
  219.  
  220.         // ICQ number has to be only numbers.
  221.         if (!preg_match('/^[0-9]+$/', $icq))
  222.         {
  223.                 $icq = '';
  224.         }
  225.  
  226.         // website has to start with http://, followed by something with length at least 3 that
  227.         // contains at least one dot.
  228.         if ($website != "")
  229.         {
  230.                 if (!preg_match('#^http[s]?:\/\/#i', $website))
  231.                 {
  232.                         $website = 'http://' . $website;
  233.                 }
  234.  
  235.                 if (!preg_match('#^http[s]?\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $website))
  236.                 {
  237.                         $website = '';
  238.                 }
  239.         }
  240.  
  241.         return;
  242. }
  243.  
  244. ?>