View file veppa_wallpaper/sys/app/models/User.php

File size: 5.21Kb
<?php
/**
 * Free Wallpaper Script
 *
 * Free Wallpaper Script by Vepa Halliyev is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
 *
 * @package		Free Wallpaper Script
 * @author		Vepa Halliyev
 * @copyright	Copyright (c) 2009, Vepa Halliyev, veppa.com.
 * @license		http://www.veppa.com/free-wallpaper-script/
 * @link		http://www.veppa.com/free-wallpaper-script/
 * @since		Version 1.0
 * @filesource
 */
/**
 * class User
 *
 * @author Vepa Halliyev <veppa.com>
 * @since  0.1
 */

class User extends Record
{
	const TABLE_NAME = 'user';
	
	const PERMISSION_PENDING = 0;
    const PERMISSION_ADMIN = 1;
    const PERMISSION_MODERATOR = 2;
    const PERMISSION_USER = 3;
	
	private static $cols = array(
		'id' => 1,                                           
		'username' => 1,                                           
		'email' => 1,                                           
		'password' => 1,                                           
		'level' => 1,              
		'ip' => 1,                                           
		'activation' => 1,
		'web' => 1,                                           
		'logged_at' => 1,                          
		'added_at' => 1,                        
		'added_by' => 1 
	);


	public function __construct($data = false, $locale_db = false)
	{
		$this->setColumns(self::$cols);
		parent::__construct($data,$locale_db);
	}

	
	function beforeInsert()
	{
		// create username if not set
		if(!strlen($this->username))
		{
			list($this->username,) = explode('@',$this->email);
		}
		
		// create acutivation code if not preset to 0
		if(!isset($this->activation))
		{
			$this->activation = self::genActivationCode();
		}
		
		$this->added_at = REQUEST_TIME;
		$this->ip = Controller::input()->ip_address();
		$this->added_by = AuthUser::$user->id;
		
		return true;
	}
	
	static function genActivationCode()
	{
		return 'a'.dechex(rand(100000000, 4294967295)).'n';
	}
	

	static function gravatar($email,$size="35",$type='identicon')
	{
		return 'http://www.gravatar.com/avatar/'.md5($email).'.jpg?s='.$size.'&amp;d='.$type;		
	}

	static function profileLink($id)
	{
		return get_url('profile/'.$id.'/');
	}

	static function findBy($field,$value)
	{
		return self::findByIdFrom('User',$value,$field,MAIN_DB);
	}


	/**
	 * Adds user fields to records
	 *
	 * @param array $records of objects
	 * @param string $id_field
	 * @param string $get_fields comma seperated
	 */
	static public function appendUserInfo($records,$id_field='k_yazar_id',$get_fields='*')
	{
		if($records)
		{
			if(!is_array($records))
			{
				$records = array($records);
			}
				
			// get ids
			foreach($records as $r)
			{
				$u_ids[$r->{$id_field}] = 1;
			}
		}
			
		if($u_ids)
		{
			$where = "id IN ('".implode("','",array_keys($u_ids))."')";
			$sql = "SELECT $get_fields
					FROM ".TABLE_PREFIX."users
					WHERE $where ";

			$users = Record::query($sql, array(),MAIN_DB);
			if($users)
			{
				foreach($users as $u)
				{
					$arr_u[$u->id] = $u;
				}
				foreach($records as $r)
				{
					$r->user = $arr_u[$r->{$id_field}];
				}
			}
		}
	}

	static public function checkUserPassword($pwd)
	{
		if(!strlen($pwd))
		{
			return false;
		}

		// check if user password is right
		$user = User::findOneFrom('User','id=?',array(AuthUser::$user->id),MAIN_DB,'password');

		return ($user && $user->password === md5($pwd));
	}


	static function deleteUserAccount($id)
	{

		$id = (int) $id;

		$user = User::findBy('id',$id);
			
		if(!$user)
		{
			return false;
		}

		// user tablosundan sil
		Record::deleteWhere('User',"id=?",array($id),MAIN_DB);

		return true;
			
	}

	function clearNotActivated()
	{
		$USER_SIL_TIME = REQUEST_TIME - 172800; //aktif olmayan uyelere verilen sure 48 saat
		return self::deleteWhere('User',"activation<>'0' AND added_at<?",array($USER_SIL_TIME),MAIN_DB);
	}

	
	function _validate_user_email($str,$val=0)
	{
		// check if this email address is not used before 
		// if it is not email address of current user
		$user = self::findByIdFrom('User',$str,'email',MAIN_DB);
		if($user)
		{
			
			// check if not current users email adress
			if($val==0 || $user->id != $val)
			{
				// this email address is not unique
				$validation = Validation::getInstance();
				
				// check if user pending activation
				if($user->activation === '0')
				{
					// this email is registered and used 
					$validation->set_message('_validate_user_email', __('%s is already registered.'));					
				}
				else
				{
					$validation->set_message('_validate_user_email', __('%s is already registered but not activated yet. Please chack your email address for activation. If you did not recieve activation email then click <a href="{url}">here</a> to ricieve new one.',
						array('{url}'=>get_url('login/resendActivation/'.$user->id.'/'.md5($str).'/'))));					
				}
				
				return false;
			}
		}
		
		
		return true;
	}
	
	function getLevel($level)
	{
		$levels = array(
			0	=>	 __('Pending'),
			1	=>	 __('Admin'),
			2	=>	 __('Moderator'),
			3	=>	 __('User')
		);
		return $levels[$level];
	}
	
} // end User class