Location: PHPKode > projects > Content*Builder > contentbuilder/lib/CB_user.class.php
<?php
require_once("PEAR.php");

/**
 * CB_user
 * 
 * @package 
 * @author FoG
 * @copyright Copyright (c) 2004
 * @version $Id: CB_user.class.php,v 1.1 2004/09/29 00:35:53 cb_fog Exp $
 * @access public
 **/
class CB_user {
	var $userIDSet = false;
	
	/**
	* @var $userID ID des zu bearbeitenden Users
	*/
	var $userID;
	
	/**
	* @var $userOption Optionen Objekt des zu bearbeitenden Users
	*/
	var $userOption;
	
	/**
	* @var $cbUserID ID des bearbeitenden Users
	*/
	var $cbUserID;
	
	/**
	* @var $cbPerms Rechte des bearbeitenden Users
	*/
	var $cbPerms;
	
	/**
	 * CB_user::factory()
	 * 
	 * @param string $userID    ID des zu bearbeitenden Users
	 * @param mixed  $cbUserID  ID des bearbeitenden Users
	 * @param mixed  $perms     Rechte des bearbeitenden Users
	 * @return object $cbUserManageObject
	 **/
	function CB_user($userID = false, $cbUserID = false, $perms = false) {
		if($userID != false) {
			$this->userIDSet = true;
		}
		
		/*
		* Ist $cbUserID nicht übergeben worden, dann modifiziert 
		* der User seine eigenen Daten
		*/
		if($cbUserID === false) {
			$cbUserID = $userID;
		}
		
		/*
		* Wenn perms nicht gesetzt sind, dann abrufen
		*/
		if($perms === false && ($cbUserID != $userID)) {
			$permObject =& new CB_perm($cbUserID);
			$perms = $permObject->getPattern();
		}
		
		/*
		* Initialisieren des Optionen Objekts für den aktuellen 
		* User und seine Optionen.
		*/
		$editUserOption =& new CBOption($userID);
		$editUserOption->setModule("user");
		$editUserOption->getOptionList();
		
		$this->userOption  = $editUserOption;
		$this->userID      = $userID;
		$this->cbUserID    = $cbUserID;
		$this->perms       = $perms;
	}
	
	
	/**
	 * CB_user::hasEditPerm()
	 * 
	 * @return boolean $result Hat der User Rechte, um User zu bearbeiten (true / false)
	 **/
	function hasEditPerm() {
		$hasEditPerms = 0;
		if((USR_MNG & $this->perms) || (ADMIN & $this->perms) || ($this->cbUserID == $this->userID)) {
			$hasEditPerms = 1;
		}
		
		return $hasEditPerms;
	}
	
	/**
	 * CB_user::setPermanentWhitelist()
	 * 
	 * @param array $permanentArray Plugin Liste die angezeigt werden soll
	 * @return 
	 **/
	function setPermanentWhitelist($permanentArray) {
		if($this->hasEditPerm()) {
			$_tempWLString = "";
			if(is_array($permanentArray)) {
				$multiple = false;
				foreach($permanentArray as $v) {
					if($multiple) {
						$_tempWLString .= "|^|";
					}
					$_tempWLString .= $v;
					$multiple = true;
				}
			} else {
				$_tempWLString = "noPermanent";
			}
			
			$this->userOption->updateOption("permanentWhitelist", $_tempWLString, $this->userID);
			$this->userOption->getOptionList();
			
			return explode("|^|", $_tempWLString);
		} else {
			return new PEAR_Error("No Permission to manage user settings", "cannot_modify");
		}
	}
	
	function setNewPassword($pass1, $pass2) {
		if($this->hasEditPerm()) {
			if($pass1 == $pass2) {
				$pass = md5($pass1);
				$result = mysql_query("UPDATE ".TABLE."_user SET passwort = '$pass' WHERE userid = '".$this->userID."'");			
			} else {
				$result = new PEAR_Error("The passwords are not equal.", "passwords_dont_match");
			}
		} else {
			$result = PEAR_Error("No Permission to manage user settings", "cannot_modify");
		}
		return $result;
	}
	
	function setGeneralInformation($infoArr) {
		if($this->hasEditPerm()) {
			if(!preg_match("/^([\w\.\-\_]+)*@([\w\.\-\_]+)+([a-zA-Z]{2,3})$/", $infoArr['email'])) { 
				$infoArr['email'] = ""; 
				$result = new PEAR_Error("The passwords are not equal.", "invalid-email");
			}
			
			$infoArr = $this->_getSafeArrayEntries($infoArr);
			
			if($this->userIDSet !== false) {
				$usrUpdate     = mysql_query("UPDATE ".TABLE."_user SET name = '".$infoArr['username']."', fullname = '".$infoArr['fullname']."', initial = '".$infoArr['initial']."', email = '".$infoArr['email']."' WHERE userid = '".$this->userID."'");
				$profileUpdate = mysql_query("UPDATE ".TABLE."_profile SET email = '".$infoArr['email']."' WHERE userid = '".$this->userID."'");
			} else {
				$dateadd = date("Y-m-d H:i:s");
				$id = md5($dateadd); // UserID wird aus dem aktuellen Datum erzeugt.
				$usrInsert       = mysql_query("INSERT INTO ".TABLE."_user (name, fullname, initial, email, userid, dateadd, del) VALUES ('".$infoArr['username']."', '".$infoArr['fullname']."', '".$infoArr['initial']."', '".$infoArr['email']."', '$id', '$dateadd', '0')");
				$permInsert      = mysql_query("INSERT INTO ".TABLE."_perms (permID) VALUES ('$id')");
				$profileInsert   = mysql_query("INSERT INTO ".TABLE."_profile (userid, name, email) VALUES ('$id', '".$infoArr['username']."', '".$infoArr['email']."')");
				$this->userID    = $id;
				$this->userIDSet = true;
				$result = $id;
			}
		} else {
			$result = PEAR_Error("No Permission to manage user settings", "cannot_modify");
		}
		return $result;
	}
	
	function setProfile($infoArr) {
		if($this->hasEditPerm()) {
			$checkProfile = mysql_query("SELECT userid FROM ".TABLE."_profile WHERE userid = '".$this->userID."'");
			if(mysql_num_rows($checkProfile) == 0) {
				$profileInsert = mysql_query("INSERT INTO ".TABLE."_profile (userid) VALUES ('".$this->userID."')");
			}
			
			if(!ereg("(^[1-9]?[0-9]*$)", $infoArr['icq'])) { 
				unset($infoArr['icq']);
			}
			
			$infoArr['birthday'] = $infoArr['birth_year']."-".$infoArr['birth_month']."-".$infoArr['birth_day'];
			unset($infoArr['birth_day']);
			unset($infoArr['birth_month']);
			unset($infoArr['birth_year']);
			
			$infoArr = $this->_getSafeArrayEntries($infoArr);
			
			$updateString = $this->_getQueryValueString($infoArr);
			
			if($updateString != false) {
				$profileUpdate = mysql_query("UPDATE ".TABLE."_profile $updateString WHERE userid = '".$this->userID."'");
			}
		} else {
			$result = PEAR_Error("No Permission to manage user settings", "cannot_modify");
		}
		return $result;
	}
	
	function getUserID() {
		return $this->userID;
	}
	
	function _getSafeArrayEntries($array) {
		foreach($array as $key => $value) {
			$array[$key] = mysql_escape_string($value);
		}
		return $array;
	}
	
	function _getQueryValueString($array, $type = "update") {
		if($type == "insert") {
			foreach($array as $key => $value) {
				$fields[] = $key;
				$values[] = "'$value'";
			}
			
			if(count($fields) != 0 && count($values) != 0) {
				$fieldString = "(".implode(", ", $fields).")";
				$valueString = "(".implode(", ", $values).")";
				
				$updateString = $fieldString." VALUES ".$valueString;
			} else {
				$updateString = false;
			}
		} else {
			if(is_array($array)) {
				foreach($array as $key => $value) {
					$values[] =  "$key = '$value'";
				}
				
				if(count($values) != 0) {
					$updateString = "SET ".implode(", ", $values)."";
				}
			} else {
				$updateString = false;
			}
		}
		return $updateString;
	}
}
?>
Return current item: Content*Builder