| 1 | <?php
 | 
  
    | 2 | /**
 | 
  
    | 3 |  *
 | 
  
    | 4 |  * @category        admin
 | 
  
    | 5 |  * @package         users
 | 
  
    | 6 |  * @author          Ryan Djurovich, WebsiteBaker Project
 | 
  
    | 7 |  * @copyright       2009-2011, Website Baker Org. e.V.
 | 
  
    | 8 |  * @link			http://www.websitebaker2.org/
 | 
  
    | 9 |  * @license         http://www.gnu.org/licenses/gpl.html
 | 
  
    | 10 |  * @platform        WebsiteBaker 2.8.x
 | 
  
    | 11 |  * @requirements    PHP 5.2.2 and higher
 | 
  
    | 12 |  * @version         $Id: add.php 2098 2014-02-11 01:37:03Z darkviper $
 | 
  
    | 13 |  * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/admin/users/add.php $
 | 
  
    | 14 |  * @lastmodified    $Date: 2014-02-11 02:37:03 +0100 (Tue, 11 Feb 2014) $
 | 
  
    | 15 |  *
 | 
  
    | 16 |  */
 | 
  
    | 17 | 
 | 
  
    | 18 | /* -------------------------------------------------------- */
 | 
  
    | 19 | // Must include code to stop this file being accessed directly
 | 
  
    | 20 | if(!defined('WB_URL')) {
 | 
  
    | 21 | 	require_once(dirname(dirname(dirname(__FILE__))).'/framework/globalExceptionHandler.php');
 | 
  
    | 22 | 	throw new IllegalFileException();
 | 
  
    | 23 | }
 | 
  
    | 24 | /* -------------------------------------------------------- */
 | 
  
    | 25 | 
 | 
  
    | 26 | 	function add_user($admin, $aActionRequest)
 | 
  
    | 27 | 	{
 | 
  
    | 28 | 		$oDb = WbDatabase::getInstance();
 | 
  
    | 29 | 		$oTrans = Translate::getInstance();
 | 
  
    | 30 |         $oTrans->enableAddon('admin\\users');
 | 
  
    | 31 |         $bRetVal = false;
 | 
  
    | 32 |         $iMinPassLength = 6;
 | 
  
    | 33 | 
 | 
  
    | 34 |         if( !$admin->checkFTAN() )
 | 
  
    | 35 |         {
 | 
  
    | 36 | //        	$admin->print_header();
 | 
  
    | 37 |         	msgQueue::add($oTrans->MESSAGE_GENERIC_SECURITY_ACCESS);
 | 
  
    | 38 |             return $bRetVal;
 | 
  
    | 39 |         }
 | 
  
    | 40 | 
 | 
  
    | 41 |         // Get details entered
 | 
  
    | 42 |         $groups_id = (isset($aActionRequest['groups'])) ? implode(",", $admin->add_slashes($aActionRequest['groups'])) : '';
 | 
  
    | 43 |         $groups_id = trim($groups_id, ','); // there will be an additional ',' when "Please Choose" was selected, too
 | 
  
    | 44 |         $active = intval(strip_tags($admin->StripCodeFromText($aActionRequest['active'][0])));
 | 
  
    | 45 |         $username_fieldname = strip_tags($admin->StripCodeFromText($aActionRequest['username_fieldname']));
 | 
  
    | 46 |         $username = strtolower(strip_tags($admin->StripCodeFromText($aActionRequest[$username_fieldname])));
 | 
  
    | 47 |         $password = strip_tags($admin->StripCodeFromText($aActionRequest['password']));
 | 
  
    | 48 |         $password2 = strip_tags($admin->StripCodeFromText($aActionRequest['password2']));
 | 
  
    | 49 |         $display_name = strip_tags($admin->StripCodeFromText($aActionRequest['display_name']));
 | 
  
    | 50 |         $email = strip_tags($admin->StripCodeFromText($aActionRequest['email']));
 | 
  
    | 51 |         $home_folder = strip_tags($admin->StripCodeFromText($aActionRequest['home_folder']));
 | 
  
    | 52 | 
 | 
  
    | 53 |         $language = DEFAULT_LANGUAGE;
 | 
  
    | 54 |         $timezone = -72000;
 | 
  
    | 55 |         $date_format = DEFAULT_DATE_FORMAT;
 | 
  
    | 56 |         $time_format = DEFAULT_TIME_FORMAT;
 | 
  
    | 57 |         $confirm_code = '';
 | 
  
    | 58 |         $confirm_timeout = 0;
 | 
  
    | 59 |         $remember_key = '';
 | 
  
    | 60 |         $login_ip = '';
 | 
  
    | 61 |         $last_reset = 0;
 | 
  
    | 62 |         $login_when = 0;
 | 
  
    | 63 | 
 | 
  
    | 64 |         // Check values
 | 
  
    | 65 |         // Check values
 | 
  
    | 66 |         if($groups_id == "") {
 | 
  
    | 67 |         	msgQueue::add($oTrans->MESSAGE_USERS_NO_GROUP);
 | 
  
    | 68 |         } else {
 | 
  
    | 69 |             $aGroups_id = explode(',', $groups_id);
 | 
  
    | 70 |             //if user is in administrator-group, get this group else just get the first one
 | 
  
    | 71 |             if($admin->is_group_match($groups_id,'1')) { $group_id = 1; } else { $group_id = intval($aGroups_id[0]); }
 | 
  
    | 72 |         }
 | 
  
    | 73 | 
 | 
  
    | 74 |         if(!preg_match('/^[a-z]{1}[a-z0-9_-]{2,}$/i', $username)) {
 | 
  
    | 75 |         	msgQueue::add($oTrans->MESSAGE_USERS_NAME_INVALID_CHARS);
 | 
  
    | 76 |         }
 | 
  
    | 77 | 
 | 
  
    | 78 | 		$sql  = 'SELECT COUNT(*) FROM `'.$oDb->TablePrefix.'users` '.
 | 
  
    | 79 |                 'WHERE `username` LIKE \''.$username.'\' ';
 | 
  
    | 80 |         // Check if username already exists
 | 
  
    | 81 |         if( ($iFoundUser = $oDb->getOne($sql)) != null ) {
 | 
  
    | 82 |             if($iFoundUser) {
 | 
  
    | 83 |             	msgQueue::add($oTrans->MESSAGE_USERS_USERNAME_TAKEN);
 | 
  
    | 84 |             }
 | 
  
    | 85 |         }
 | 
  
    | 86 | 
 | 
  
    | 87 |     	if(strlen($password) < $iMinPassLength ) {
 | 
  
    | 88 |     		msgQueue::add($oTrans->MESSAGE_USERS_PASSWORD_TOO_SHORT);
 | 
  
    | 89 |     	}
 | 
  
    | 90 | 
 | 
  
    | 91 | 		$pattern = '/[^'.$admin->password_chars.']/';
 | 
  
    | 92 | 		if (preg_match($pattern, $password)) {
 | 
  
    | 93 | 			msgQueue::add($oTrans->MESSAGE_PREFERENCES_INVALID_CHARS);
 | 
  
    | 94 |     	}
 | 
  
    | 95 | 
 | 
  
    | 96 |     	if(($password != $password2) ) {
 | 
  
    | 97 |     		msgQueue::add($oTrans->MESSAGE__USERS_PASSWORD_MISMATCH);
 | 
  
    | 98 |     	}
 | 
  
    | 99 | 
 | 
  
    | 100 | //
 | 
  
    | 101 | // check that display_name is unique in whoole system (prevents from User-faking)
 | 
  
    | 102 |     	$sql  = 'SELECT COUNT(*) FROM `'.$oDb->TablePrefix.'users` ';
 | 
  
    | 103 |     	$sql .= 'WHERE `user_id` <> '.(int)$admin->get_user_id().' AND `display_name` LIKE "'.$display_name.'"';
 | 
  
    | 104 |     	if( ($iFoundUser = intval($oDb->getOne($sql))) > 0 ){
 | 
  
    | 105 |     	   msgQueue::add($oTrans->MESSAGE_USERS_USERNAME_TAKEN.' ('.$oTrans->TEXT_DISPLAY_NAME.')');
 | 
  
    | 106 |         } else {
 | 
  
    | 107 |             if($display_name == '') {
 | 
  
    | 108 |         	   msgQueue::add($oTrans->MESSAGE_GENERIC_FILL_IN_ALL.' ('.$oTrans->TEXT_DISPLAY_NAME.')');
 | 
  
    | 109 |             }
 | 
  
    | 110 |        }
 | 
  
    | 111 | 
 | 
  
    | 112 |         if(findStringInFileList($display_name, dirname(__FILE__).'/disallowedNames')) {
 | 
  
    | 113 |             msgQueue::add( $oTrans->TEXT_ERROR.' '.$oTrans->TEXT_DISPLAY_NAME.' ('.$display_name.')' );
 | 
  
    | 114 |         }
 | 
  
    | 115 | 
 | 
  
    | 116 |         if($email != "")
 | 
  
    | 117 |         {
 | 
  
    | 118 |         	if($admin->validate_email($email) == false)
 | 
  
    | 119 |             {
 | 
  
    | 120 |                 msgQueue::add($oTrans->MESSAGE_USERS_INVALID_EMAIL.' ('.$email.')');
 | 
  
    | 121 |         	}
 | 
  
    | 122 |         } else { // e-mail must be present
 | 
  
    | 123 |         	msgQueue::add($oTrans->MESSAGE_SIGNUP_NO_EMAIL);
 | 
  
    | 124 |         }
 | 
  
    | 125 | 
 | 
  
    | 126 | 		$sql  = 'SELECT COUNT(*) FROM `'.$oDb->TablePrefix.'users` '.
 | 
  
    | 127 |                 'WHERE `email` LIKE \''.$email.'\' ';
 | 
  
    | 128 | 
 | 
  
    | 129 |         // Check if the email already exists
 | 
  
    | 130 |         if( ($iFoundUser = $oDb->getOne($sql)) != null ) {
 | 
  
    | 131 |             if($iFoundUser) {
 | 
  
    | 132 |             	if(isset($oTrans->MESSAGE_USERS_EMAIL_TAKEN))
 | 
  
    | 133 |                 {
 | 
  
    | 134 |             		msgQueue::add($oTrans->MESSAGE_USERS_EMAIL_TAKEN.' ('.$email.')');
 | 
  
    | 135 |             	} else {
 | 
  
    | 136 |             		msgQueue::add($oTrans->MESSAGE_USERS_INVALID_EMAIL.' ('.$email.')');
 | 
  
    | 137 |             	}
 | 
  
    | 138 |             }
 | 
  
    | 139 |         }
 | 
  
    | 140 | 
 | 
  
    | 141 | 		if( ($msg = msgQueue::getError()) == '')
 | 
  
    | 142 | 		{
 | 
  
    | 143 |             //if user is in administrator-group, get this group else just get the first one
 | 
  
    | 144 |             if($admin->is_group_match($groups_id,'1')) { $group_id = 1; $groups_id = '1'; }
 | 
  
    | 145 | 
 | 
  
    | 146 |             if(defined('HOME_FOLDERS') && HOME_FOLDERS) {
 | 
  
    | 147 |                 // Include the WB functions file
 | 
  
    | 148 |                 if(!function_exists('media_filename')) { require(WB_PATH.'/framework/functions.php'); }
 | 
  
    | 149 | 
 | 
  
    | 150 |                 // Remove bad characters
 | 
  
    | 151 |                 $sHomeFolder = WB_PATH.MEDIA_DIRECTORY.'/home/'.( media_filename($username) );
 | 
  
    | 152 |                 if ( sizeof(createFolderProtectFile( $sHomeFolder )) )
 | 
  
    | 153 |                 {
 | 
  
    | 154 |                 	msgQueue::add($oTrans->MESSAGE_MEDIA_DIR_NOT_MADE.' ('.basename($sHomeFolder).') ' );
 | 
  
    | 155 |                 }
 | 
  
    | 156 |             }
 | 
  
    | 157 |             // Inser the user into the database
 | 
  
    | 158 | 			$sql  = 'INSERT INTO `'.$oDb->TablePrefix.'users` SET '.
 | 
  
    | 159 |                     '`group_id`     = '.intval($group_id).', '.
 | 
  
    | 160 |                     '`groups_id`    = \''.$oDb->escapeString($groups_id).'\', '.
 | 
  
    | 161 |                     '`active`       = '.intval($active).', '.
 | 
  
    | 162 |                     '`username`     = \''.$oDb->escapeString($username).'\', '.
 | 
  
    | 163 |                     '`password`     = \''.md5($password).'\', '.
 | 
  
    | 164 |                     '`confirm_code` = \''.$oDb->escapeString($confirm_code).'\', '.
 | 
  
    | 165 |                     '`confirm_timeout` = '.intval($confirm_timeout).', '.
 | 
  
    | 166 |                     '`remember_key` = \''.$oDb->escapeString($remember_key).'\', '.
 | 
  
    | 167 |                     '`last_reset`   = '.intval($last_reset).', '.
 | 
  
    | 168 |                     '`display_name` = \''.$oDb->escapeString($display_name).'\', '.
 | 
  
    | 169 |                     '`email`        = \''.$oDb->escapeString($email).'\', '.
 | 
  
    | 170 |                     '`timezone`     = '.intval($timezone).', '.
 | 
  
    | 171 |                     '`date_format`  = \''.$oDb->escapeString($date_format).'\', '.
 | 
  
    | 172 |                     '`time_format`  = \''.$oDb->escapeString($time_format).'\', '.
 | 
  
    | 173 |                     '`language`     = \''.$oDb->escapeString($language).'\', '.
 | 
  
    | 174 |                     '`home_folder`  = \''.$oDb->escapeString($home_folder).'\', '.
 | 
  
    | 175 |                     '`login_when`   = '.intval($login_when).', '.
 | 
  
    | 176 |                     '`login_ip`     = \''.$oDb->escapeString($login_ip).'\' '.
 | 
  
    | 177 |                     '';
 | 
  
    | 178 |             if($oDb->doQuery($sql)) {
 | 
  
    | 179 |             	msgQueue::add($oTrans->MESSAGE_USERS_ADDED, true);
 | 
  
    | 180 | 		        $bRetVal = true;
 | 
  
    | 181 |             }
 | 
  
    | 182 |             if($oDb->isError()) {
 | 
  
    | 183 |                 msgQueue::add( implode('<br />',explode(';',$oDb->getError())) );
 | 
  
    | 184 |             }
 | 
  
    | 185 |         } else {
 | 
  
    | 186 |         	msgQueue::add($oTrans->HEADING_ADD_USER.' '.$oTrans->MESSAGE_GENERIC_NOT_COMPARE);
 | 
  
    | 187 | 
 | 
  
    | 188 | 		}
 | 
  
    | 189 | 		return $bRetVal;
 | 
  
    | 190 |    }
 |