2010-03-22 15:45:47 +00:00
< ? php
/*
* e107 website system
*
2011-09-14 11:30:58 +00:00
* Copyright ( C ) 2008 - 2011 e107 Inc ( e107 . org )
2010-03-22 15:45:47 +00:00
* Released under the terms and conditions of the
* GNU General Public License ( http :// www . gnu . org / licenses / gpl . txt )
*
* User Model
*
* $URL $
* $Id $
*/
2010-05-02 18:41:20 +00:00
/**
2010-05-05 15:05:32 +00:00
* @ package e107
2010-05-28 22:10:20 +00:00
* @ subpackage e107_handlers
2010-05-05 15:05:32 +00:00
* @ version $Id $
* @ author SecretR
*
* Front - end User Models
2010-05-02 18:41:20 +00:00
*/
2010-05-05 15:05:32 +00:00
if ( ! defined ( 'e107_INIT' ))
{
exit ;
}
2012-02-07 16:37:44 +00:00
class e_user_model extends e_admin_model
2010-05-02 18:41:20 +00:00
{
/**
2010-05-05 15:05:32 +00:00
* Describes all model data , used as _FIELD_TYPE array as well
2010-05-02 18:41:20 +00:00
* @ var array
*/
2010-05-05 15:05:32 +00:00
protected $_data_fields = array (
'user_id' => 'integer' ,
'user_name' => 'string' ,
'user_loginname' => 'string' ,
'user_customtitle' => 'string' ,
'user_password' => 'string' ,
'user_sess' => 'string' ,
'user_email' => 'string' ,
'user_signature' => 'string' ,
'user_image' => 'string' ,
'user_hideemail' => 'integer' ,
'user_join' => 'integer' ,
'user_lastvisit' => 'integer' ,
'user_currentvisit' => 'integer' ,
'user_lastpost' => 'integer' ,
'user_chats' => 'integer' ,
'user_comments' => 'integer' ,
'user_ip' => 'string' ,
'user_ban' => 'integer' ,
'user_prefs' => 'string' ,
'user_visits' => 'integer' ,
'user_admin' => 'integer' ,
'user_login' => 'string' ,
'user_class' => 'string' ,
'user_perms' => 'string' ,
'user_realm' => 'string' ,
'user_pwchange' => 'integer' ,
'user_xup' => 'string' ,
);
2010-05-02 18:41:20 +00:00
/**
2010-05-05 15:05:32 +00:00
* Validate required fields
* @ var array
2010-05-02 18:41:20 +00:00
*/
2010-05-05 15:05:32 +00:00
protected $_validation_rules = array (
'user_name' => array ( 'string' , '1' , 'LAN_USER_01' , 'LAN_USER_HELP_01' ), // TODO - regex
'user_loginname' => array ( 'string' , '1' , 'LAN_USER_02' , 'LAN_USER_HELP_02' ), // TODO - regex
2015-09-01 15:52:22 -07:00
'user_password' => array ( 'compare' , '5' , 'LAN_PASSWORD' , 'LAN_USER_HELP_05' ), // TODO - pref - modify it somewhere below - prepare_rules()?
'user_email' => array ( 'email' , '' , 'LAN_EMAIL' , 'LAN_USER_HELP_08' ),
2010-05-05 15:05:32 +00:00
);
2010-05-02 18:41:20 +00:00
/**
2010-05-05 15:05:32 +00:00
* Validate optional fields - work in progress , not working yet
* @ var array
2010-05-02 18:41:20 +00:00
*/
2010-05-05 15:05:32 +00:00
protected $_optional_rules = array (
'user_customtitle' => array ( 'string' , '1' , 'LAN_USER_01' ), // TODO - regex
);
2010-05-02 18:41:20 +00:00
2010-03-22 15:45:47 +00:00
/**
* @ see e_model
* @ var string
*/
protected $_db_table = 'user' ;
/**
* @ see e_model
* @ var string
*/
protected $_field_id = 'user_id' ;
/**
* @ see e_model
* @ var string
*/
protected $_message_stack = 'user' ;
2010-05-05 15:05:32 +00:00
2010-05-15 17:33:11 +00:00
/**
* User class as set in user Adminsitration
*
* @ var integer
*/
protected $_memberlist_access = null ;
2010-04-28 15:44:46 +00:00
/**
* Extended data
2010-05-05 15:05:32 +00:00
*
2010-04-28 15:44:46 +00:00
* @ var e_user_extended_model
*/
protected $_extended_model = null ;
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
/**
* Extended structure
2010-05-05 15:05:32 +00:00
*
2010-05-02 18:41:20 +00:00
* @ var e_user_extended_structure
*/
protected $_extended_structure = null ;
2010-05-05 15:05:32 +00:00
2010-05-13 15:47:31 +00:00
/**
* User preferences model
* @ var e_user_pref
*/
protected $_user_config = null ;
2010-05-02 18:41:20 +00:00
/**
* User model of current editor
* @ var e_user_model
*/
protected $_editor = null ;
2010-12-11 15:37:39 +00:00
protected $_class_list ;
2010-05-05 15:05:32 +00:00
2010-05-15 17:33:11 +00:00
/**
* Constructor
* @ param array $data
* @ return void
*/
public function __construct ( $data = array ())
{
$this -> _memberlist_access = e107 :: getPref ( 'memberlist_access' );
parent :: __construct ( $data );
}
2010-05-02 18:41:20 +00:00
/**
* Always return integer
2010-05-05 15:05:32 +00:00
*
2010-05-02 18:41:20 +00:00
* @ see e107_handlers / e_model #getId()
2010-04-28 15:44:46 +00:00
*/
2010-05-02 18:41:20 +00:00
public function getId ()
{
return ( integer ) parent :: getId ();
}
2010-05-05 15:05:32 +00:00
2012-12-11 10:05:02 +02:00
/**
* Try display name , fall back to login name when empty ( shouldn ' t happen )
*/
2010-05-15 17:33:11 +00:00
final public function getName ( $anon = false )
{
2012-12-10 01:20:38 +02:00
if ( $this -> isUser ())
{
2012-12-11 10:05:02 +02:00
return ( $this -> get ( 'user_name' ) ? $this -> get ( 'user_name' ) : $this -> get ( 'user_loginname' ));
2012-12-10 01:20:38 +02:00
}
return $anon ;
2010-05-15 17:33:11 +00:00
}
2012-12-11 10:05:02 +02:00
/**
* Display name getter . Use it as DB field name will be changed soon .
*/
final public function getDisplayName ()
{
return $this -> get ( 'user_name' );
}
/**
* Login name getter . Use it as DB field name will be changed soon .
*/
final public function getLoginName ()
{
return $this -> get ( 'user_loginname' );
}
2015-02-15 02:37:36 -08:00
2012-12-11 10:05:02 +02:00
/**
* Real name getter . Use it as DB field name will be changed soon .
* @ param bool $strict if false , fall back to Display name when empty
2015-02-15 02:37:36 -08:00
* @ return mixed
2012-12-11 10:05:02 +02:00
*/
final public function getRealName ( $strict = false )
{
if ( $strict ) return $this -> get ( 'user_login' );
return ( $this -> get ( 'user_login' ) ? $this -> get ( 'user_login' ) : $this -> get ( 'user_name' ));
}
2010-05-15 17:33:11 +00:00
2010-05-02 18:41:20 +00:00
final public function getAdminId ()
{
return ( $this -> isAdmin () ? $this -> getId () : false );
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
final public function getAdminName ()
{
2010-05-14 18:45:51 +00:00
return ( $this -> isAdmin () ? $this -> get ( 'user_name' ) : false );
2010-05-02 18:41:20 +00:00
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
final public function getAdminEmail ()
{
2010-05-14 18:45:51 +00:00
return ( $this -> isAdmin () ? $this -> get ( 'user_email' ) : false );
2010-05-02 18:41:20 +00:00
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
final public function getAdminPwchange ()
{
2010-05-14 18:45:51 +00:00
return ( $this -> isAdmin () ? $this -> get ( 'user_pwchange' ) : false );
2010-05-02 18:41:20 +00:00
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
final public function getAdminPerms ()
{
2010-05-14 18:45:51 +00:00
return ( $this -> isAdmin () ? $this -> get ( 'user_perms' ) : false );
2010-05-02 18:41:20 +00:00
}
2010-05-05 15:05:32 +00:00
2016-12-13 21:31:59 +01:00
final public function getTimezone ()
{
2016-12-13 21:42:57 +01:00
// If timezone is not set, we return an empty string in order to use the
// default timezone is set for e107.
return ( $this -> get ( 'user_timezone' ) ? $this -> get ( 'user_timezone' ) : '' );
2016-12-13 21:31:59 +01:00
}
2010-10-26 07:41:20 +00:00
/**
* DEPRECATED - will be removed or changed soon ( see e_session )
* @ return string
*/
2010-05-19 15:28:52 +00:00
public function getToken ()
{
if ( null === $this -> get ( 'user_token' ))
{
2010-10-26 07:41:20 +00:00
//$this->set('user_token', md5($this->get('user_password').$this->get('user_lastvisit').$this->get('user_pwchange').$this->get('user_class')));
$this -> set ( 'user_token' , e107 :: getSession () -> getFormToken ( false ));
2010-05-19 15:28:52 +00:00
}
return $this -> get ( 'user_token' );
}
2012-12-10 01:20:38 +02:00
public static function randomKey ()
{
return md5 ( uniqid ( rand (), 1 ));
}
2010-05-19 15:28:52 +00:00
2010-05-02 18:41:20 +00:00
public function isCurrent ()
{
return false ;
}
final public function isAdmin ()
{
2010-05-14 18:45:51 +00:00
return ( $this -> get ( 'user_admin' ) ? true : false );
2010-05-02 18:41:20 +00:00
}
2010-05-05 15:05:32 +00:00
2016-05-18 11:16:34 -07:00
final public function isNewUser ()
{
$new_user_period = e107 :: getPref ( 'user_new_period' , 0 );
if ( empty ( $new_user_period )) { return false ; }
return (( $this -> get ( 'user_join' ) > strtotime ( $new_user_period . " days ago " )) ? true : false );
}
2016-05-18 12:10:03 -07:00
final public function isBot ()
{
$userAgent = $_SERVER [ 'HTTP_USER_AGENT' ];
if ( empty ( $userAgent ))
{
return false ;
}
$botlist = array ( " googlebot " , " Bingbot " , 'slurp' , 'baidu' , 'ichiro' , 'nutch' , 'yacy' , " Teoma " ,
" alexa " , " froogle " , " Gigabot " , " inktomi " ,
" looksmart " , " URL_Spider_SQL " , " Firefly " , " NationalDirectory " ,
" Ask Jeeves " , " TECNOSEEK " , " InfoSeek " , " WebFindBot " , " girafabot " ,
" crawler " , " www.galaxy.com " , " Scooter " , " msnbot " , " appie " , " FAST " , " WebBug " , " Spade " , " ZyBorg " , " rabaz " ,
" Baiduspider " , " Feedfetcher-Google " , " TechnoratiSnoop " , " Rankivabot " ,
" Mediapartners-Google " , " Sogou web spider " , " WebAlta Crawler " , " TweetmemeBot " ,
" Butterfly " , " Twitturls " , " Me.dium " , " Twiceler " );
foreach ( $botlist as $bot )
{
if ( stripos ( $userAgent , $bot ) !== false ){ return true ; }
}
return false ;
}
2010-05-02 18:41:20 +00:00
final public function isMainAdmin ()
{
return $this -> checkAdminPerms ( '0' );
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
final public function isUser ()
{
return ( $this -> getId () ? true : false );
}
2010-05-05 15:05:32 +00:00
2010-05-14 18:45:51 +00:00
final public function isGuest ()
{
return ( $this -> getId () ? false : true );
}
final public function hasBan ()
{
return (( integer ) $this -> get ( 'user_ban' ) === 1 ? true : false );
}
final public function hasRestriction ()
{
return (( integer ) $this -> get ( 'user_ban' ) === 0 ? false : true );
}
2010-05-02 18:41:20 +00:00
public function hasEditor ()
{
2010-05-13 15:47:31 +00:00
return ( null !== $this -> _editor );
2010-05-02 18:41:20 +00:00
}
2010-05-05 15:05:32 +00:00
2010-05-17 14:13:50 +00:00
final protected function _setClassList ()
2010-05-02 18:41:20 +00:00
{
$this -> _class_list = array ();
2010-05-05 15:05:32 +00:00
if ( $this -> isUser ())
2010-05-02 18:41:20 +00:00
{
2010-05-15 17:33:11 +00:00
if ( $this -> get ( 'user_class' ))
2010-05-02 18:41:20 +00:00
{
2010-05-17 14:13:50 +00:00
// list of all 'inherited' user classes, convert elements to integer
$this -> _class_list = array_map ( 'intval' , e107 :: getUserClass () -> get_all_user_classes ( $this -> get ( 'user_class' ), true ));
2010-05-02 18:41:20 +00:00
}
2016-05-18 11:16:34 -07:00
2010-05-02 18:41:20 +00:00
$this -> _class_list [] = e_UC_MEMBER ;
2016-05-18 11:16:34 -07:00
if ( $this -> isNewUser ())
{
$this -> _class_list [] = e_UC_NEWUSER ;
}
2010-05-05 15:05:32 +00:00
if ( $this -> isAdmin ())
2010-05-02 18:41:20 +00:00
{
$this -> _class_list [] = e_UC_ADMIN ;
}
2016-05-18 11:16:34 -07:00
2010-05-05 15:05:32 +00:00
if ( $this -> isMainAdmin ())
2010-05-02 18:41:20 +00:00
{
$this -> _class_list [] = e_UC_MAINADMIN ;
}
}
else
{
$this -> _class_list [] = e_UC_GUEST ;
2016-05-18 12:10:03 -07:00
if ( $this -> isBot ())
{
$this -> _class_list [] = e_UC_BOTS ;
}
2010-05-02 18:41:20 +00:00
}
2016-05-18 11:16:34 -07:00
2010-05-02 18:41:20 +00:00
$this -> _class_list [] = e_UC_READONLY ;
$this -> _class_list [] = e_UC_PUBLIC ;
2010-05-05 15:05:32 +00:00
2010-05-17 14:13:50 +00:00
// unique, rebuild indexes
$this -> _class_list = array_merge ( array_unique ( $this -> _class_list ));
2010-05-02 18:41:20 +00:00
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
final public function getClassList ( $toString = false )
{
2010-05-05 15:05:32 +00:00
if ( null === $this -> _class_list )
2010-05-02 18:41:20 +00:00
{
$this -> _setClassList ();
}
return ( $toString ? implode ( ',' , $this -> _class_list ) : $this -> _class_list );
}
2010-05-05 15:05:32 +00:00
2010-05-15 17:33:11 +00:00
final public function getClassRegex ()
{
return '(^|,)(' . str_replace ( ',' , '|' , $this -> getClassList ( true )) . ')(,|$)' ;
}
2010-05-02 18:41:20 +00:00
final public function checkClass ( $class , $allowMain = true )
{
// FIXME - replace check_class() here
return (( $allowMain && $this -> isMainAdmin ()) || check_class ( $class , $this -> getClassList (), 0 ));
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
final public function checkAdminPerms ( $perm_str )
{
// FIXME - method to replace getperms()
return ( $this -> isAdmin () && getperms ( $perm_str , $this -> getAdminPerms ()));
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
final public function checkEditorPerms ( $class = '' )
{
2010-05-05 15:05:32 +00:00
if ( ! $this -> hasEditor ())
return false ;
2010-05-02 18:41:20 +00:00
$editor = $this -> getEditor ();
2010-05-05 15:05:32 +00:00
if ( '' !== $class )
return ( $editor -> isAdmin () && $editor -> checkClass ( $class ));
2010-05-02 18:41:20 +00:00
return $editor -> isAdmin ();
}
2010-03-22 15:45:47 +00:00
2010-05-19 15:28:52 +00:00
/**
* Check passed value against current user token
2010-10-26 07:41:20 +00:00
* DEPRECATED - will be removed or changed soon ( see e_core_session )
2010-05-19 15:28:52 +00:00
* @ param string $token md5 sum of e . g . posted token
* @ return boolean
*/
final public function checkToken ( $token )
{
$utoken = $this -> getToken ();
return ( null !== $utoken && $token === md5 ( $utoken ));
}
2010-05-17 15:51:42 +00:00
/**
* Bad but required ( BC ) method of retrieving all user data
* It ' s here to be used from get_user_data () core function .
2010-05-19 15:28:52 +00:00
* DON ' T USE THEM BOTH unless you have VERY good reason to do it .
2010-05-17 15:51:42 +00:00
*
* @ return array
*/
public function getUserData ()
{
2010-05-28 13:24:25 +00:00
// revised - don't call extended object, no permission checks, just return joined user data
$ret = $this -> getData ();
// $ret = array_merge($this->getExtendedModel()->getExtendedData(), $this->getData());
2010-05-17 15:51:42 +00:00
if ( $ret [ 'user_perms' ] == '0.' ) $ret [ 'user_perms' ] = '0' ;
$ret [ 'user_baseclasslist' ] = $ret [ 'user_class' ];
$ret [ 'user_class' ] = $this -> getClassList ( true );
return $ret ;
}
2010-03-22 15:45:47 +00:00
/**
2010-05-28 13:24:25 +00:00
* Check if given field name is present in core user table structure
2010-03-22 15:45:47 +00:00
*
2010-05-28 13:24:25 +00:00
* @ param string $field
* @ param boolean $short
* @ return boolean
*/
public function isCoreField ( $field , $short = true )
{
if ( $short ) $field = 'user_' . $field ;
return isset ( $this -> _data_fields [ $field ]);
}
/**
* Check if given field name is present in extended user table structure
*
* @ param string $field
* @ param boolean $short
* @ return boolean
*/
public function isExtendedField ( $field , $short = true )
{
if ( $short ) $field = 'user_' . $field ;
if ( $this -> isCoreField ( $field , false ))
{
return false ;
}
return $this -> getExtendedModel () -> isField ( $field , false );
}
/**
* Get User value from core user table .
* This method doesn ' t perform any read permission cheks .
*
* @ param string $field
* @ param mixed $default
2010-05-13 15:47:31 +00:00
* @ param boolean $short if true , 'user_' prefix will be added to field name
2010-05-28 13:24:25 +00:00
* @ return mixed if field is not part of core user table returns null by default
2010-03-22 15:45:47 +00:00
*/
2010-05-28 13:24:25 +00:00
public function getCore ( $field , $default = null , $short = true )
2010-03-22 15:45:47 +00:00
{
2010-05-13 15:47:31 +00:00
if ( $short ) $field = 'user_' . $field ;
2010-05-28 13:24:25 +00:00
if ( $this -> isCoreField ( $field , false )) return $this -> get ( $field , $default );
return $default ;
2010-03-22 15:45:47 +00:00
}
/**
2010-05-28 13:24:25 +00:00
* Set User value ( core user field ) .
* This method doesn ' t perform any write permission cheks .
*
2010-03-22 15:45:47 +00:00
* @ param string $field
* @ param mixed $value
2010-05-13 15:47:31 +00:00
* @ param boolean $short if true , 'user_' prefix will be added to field name
2010-05-28 13:24:25 +00:00
* @ param boolean $strict if false no Applicable check will be made
2010-03-22 15:45:47 +00:00
* @ return e_user_model
*/
2010-05-28 13:24:25 +00:00
public function setCore ( $field , $value , $short = true , $strict = false )
2010-03-22 15:45:47 +00:00
{
2010-05-13 15:47:31 +00:00
if ( $short ) $field = 'user_' . $field ;
2010-05-28 13:24:25 +00:00
if ( $this -> isCoreField ( $field , false )) $this -> set ( $field , $value , $strict );
return $this ;
}
/**
* Get User extended value .
* This method doesn ' t perform any read permission cheks .
*
* @ param string $field
* @ param boolean $short if true , 'user_' prefix will be added to field name
* @ param boolean $raw get raw DB values ( no SQL query )
* @ return mixed
*/
public function getExtended ( $field , $short = true , $raw = true )
{
return $this -> getExtendedModel () -> getSystem ( $field , $short , $raw );
}
/**
* Set User extended value .
* This method doesn ' t perform any write permission cheks .
*
* @ param string $field
* @ param mixed $value
* @ param boolean $short if true , 'user_' prefix will be added to field name
* @ param boolean $strict if false no Applicable check will be made
* @ return e_user_model
*/
public function setExtended ( $field , $value , $short = true , $strict = false )
{
$this -> getExtendedModel () -> setSystem ( $field , $value , $short , $strict );
return $this ;
}
/**
* Get User extended value after checking read permissions against current Editor
*
* @ param string $field
* @ param boolean $short if true , 'user_' prefix will be added to field name
* @ param boolean $raw get raw DB values ( no SQL query )
* @ return mixed
*/
public function getExtendedFront ( $field , $short = true , $raw = false )
{
return $this -> getExtendedModel () -> getValue ( $field , $short , $raw );
}
/**
* Set User extended value after checking write permissions against current Editor .
*
* @ param string $field
* @ param mixed $value
* @ param boolean $short if true , 'user_' prefix will be added to field name
* @ return e_user_model
*/
public function setExtendedFront ( $field , $value , $short = true )
{
$this -> getExtendedModel () -> setValue ( $field , $value , $short );
return $this ;
}
/**
* Transparent front - end getter . It performs all required read / applicable permission checks
* against current editor / user . It doesn ' t distinguish core and extended fields .
* It grants BC .
* It 's what you' d need in all front - end parsing code ( e . g . shortcodes )
*
* @ param string $field
* @ param mixed $default
* @ param boolean $short if true , 'user_' prefix will be added to field name
* @ param boolean $rawExtended get raw DB values ( no SQL query ) - used only for extended fields
* @ return mixed if field is not readable returns null by default
*/
public function getValue ( $field , $default = null , $short = true , $rawExtended = false )
{
if ( $short )
{
$mfield = $field ;
$field = 'user_' . $field ;
}
else
{
$mfield = substr ( $field , 5 );
}
// check for BC/override method first e.g. getSingatureValue($default, $system = false, $rawExtended);
$method = 'get' . ucfirst ( $mfield ) . 'Value' ;
if ( method_exists ( $this , $method )) return $this -> $method ( $default , false , $rawExtended );
if ( $this -> isCoreField ( $field , false ))
{
if ( ! $this -> isReadable ( $field )) return $default ;
return $this -> getCore ( $field , $default , false );
}
return $this -> getExtendedFront ( $field , false , $rawExtended );
}
/**
* Transparent front - end setter . It performs all required write / applicable permission checks
* against current editor / user . It doesn ' t distinguish core and extended fields .
* It grants BC .
* It 's what you' d need on all user front - end manipulation events ( e . g . user settings page related code )
* NOTE : untrusted data should be provided via setPosted () method !
*
* @ param string $field
* @ param mixed $value
* @ param boolean $short if true , 'user_' prefix will be added to field name
* @ return e_user_model
*/
public function setValue ( $field , $value , $short = true )
{
if ( $short )
{
$mfield = $field ;
$field = 'user_' . $field ;
}
else
{
$mfield = substr ( $field , 5 );
}
// check for BC/override method first e.g. setSingatureValue($value, $system = false);
$method = 'set' . ucfirst ( $mfield ) . 'Value' ;
if ( method_exists ( $this , $method ))
{
$this -> $method ( $value , false );
return $this ;
}
if ( $this -> isCoreField ( $field , false ))
{
if ( $this -> isWritable ( $field )) $this -> setCore ( $field , $value , false , true );
}
else
{
$this -> setExtendedFront ( $field , $value , false );
}
return $this ;
}
/**
* Transparent system getter . It doesn ' t perform any read / applicable permission checks
* against current editor / user . It doesn ' t distinguish core and extended fields .
* It grants BC .
* It ' s here to serve in your application logic .
*
* @ param string $field
* @ param mixed $default
* @ param boolean $short if true , 'user_' prefix will be added to field name
* @ param boolean $rawExtended get raw DB values ( no SQL query ) - used only for extended fields
* @ return mixed
*/
public function getSystem ( $field , $default = null , $short = true , $rawExtended = true )
{
if ( $short )
{
$mfield = $field ;
$field = 'user_' . $field ;
}
else
{
$mfield = substr ( $field , 5 );
}
// check for BC/override method first e.g. getSingatureValue($default, $system = true, $rawExtended);
$method = 'get' . ucfirst ( $mfield ) . 'Value' ;
if ( method_exists ( $this , $method )) return $this -> $method ( $default , true , $rawExtended );
if ( $this -> isCoreField ( $field , false ))
{
return $this -> getCore ( $field , $default , false );
}
return $this -> getExtended ( $field , false , $rawExtended );
}
/**
* Transparent front - end setter . It doesn ' t perform any write / applicable permission checks
* against current editor / user . It doesn ' t distinguish core and extended fields .
* It ' s here to serve in your application logic .
* NOTE : untrusted data should be provided via setPosted () method !
*
* @ param string $field
* @ param mixed $value
* @ param boolean $short if true , 'user_' prefix will be added to field name
* @ param boolean $strict if false no Applicable check will be made
* @ return e_user_model
*/
public function setSystem ( $field , $value , $short = true , $strict = false )
{
if ( $short )
{
$mfield = $field ;
$field = 'user_' . $field ;
}
else
{
$mfield = substr ( $field , 5 );
}
// check for BC/override method first e.g. setSingatureValue($value, $system = true);
$method = 'set' . ucfirst ( $mfield ) . 'Value' ;
if ( method_exists ( $this , $method ))
{
$this -> $method ( $value , true );
return $this ;
}
if ( $this -> isCoreField ( $field , false ))
{
$this -> setCore ( $field , $value , false , $strict );
}
else
{
$this -> setExtended ( $field , $value , false , $strict );
}
return $this ;
}
/**
* Just an example override method . This method is auto - magically called by getValue / System
* getters .
* $rawExtended is not used ( here for example purposes only )
* If user_signature become extended field one day , we ' d need this method
* for real - it ' ll call extended getters to retrieve the required value .
*
* @ param mixed $default optional
* @ param boolean $system optional
* @ param boolean $rawExtended optional
* @ return mixed value
*/
public function getSignatureValue ( $default = null , $system = false , $rawExtended = true )
{
if ( $system || $this -> isReadable ( 'user_signature' )) return $this -> getCore ( 'signature' , $default );
return $default ;
}
/**
* Just an example override method . This method is auto - magically called by setValue / System
* setters .
* If user_signature become extended field one day , we ' d need this method
* for real - it ' ll call extended setters to set the new signature value
*
* @ param string $value
* @ param boolean $system
* @ return e_user_model
*/
public function setSignatureValue ( $value , $system = false )
{
if ( $system || $this -> isWritable ( 'user_signature' )) $this -> setCore ( 'signature' , $value );
2010-05-13 15:47:31 +00:00
return $this ;
}
/**
* Get user preference
* @ param string $pref_name
* @ param mixed $default
* @ return mixed
*/
public function getPref ( $pref_name = null , $default = null )
{
if ( null === $pref_name ) return $this -> getConfig () -> getData ();
return $this -> getConfig () -> get ( $pref_name , $default );
}
/**
* Set user preference
* @ param string $pref_name
* @ param mixed $value
* @ return e_user_model
*/
public function setPref ( $pref_name , $value = null )
{
$this -> getConfig () -> set ( $pref_name , $value );
return $this ;
}
/**
* Get user preference ( advanced - slower )
* @ param string $pref_path
* @ param mixed $default
* @ param integer $index if number , value will be exploded by " \n " and corresponding index will be returned
* @ return mixed
*/
public function findPref ( $pref_path = null , $default = null , $index = null )
{
return $this -> getConfig () -> getData ( $pref_path , $default , $index );
}
/**
* Set user preference ( advanced - slower )
* @ param string $pref_path
* @ param mixed $value
* @ return e_user_model
*/
public function setPrefData ( $pref_path , $value = null )
{
$this -> getConfig () -> setData ( $pref_path , $value = null );
2010-03-22 15:45:47 +00:00
return $this ;
}
2012-07-31 07:32:00 +00:00
/**
* New - External login providers support
* @ return string Provider name
*/
public function getProviderName ()
{
if ( $this -> get ( 'user_xup' ))
{
return array_shift ( explode ( '_' , $this -> get ( 'user_xup' )));
}
return null ;
}
/**
* New - External login providers support
* @ return boolean Check if there is external provider data
*/
public function hasProviderName ()
{
return $this -> has ( 'user_xup' );
}
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
/**
* Get user extended model
2010-05-05 15:05:32 +00:00
*
2010-04-28 15:44:46 +00:00
* @ return e_user_extended_model
*/
2010-05-02 18:41:20 +00:00
public function getExtendedModel ()
2010-04-28 15:44:46 +00:00
{
2010-05-05 15:05:32 +00:00
if ( null === $this -> _extended_model )
2010-04-28 15:44:46 +00:00
{
2010-05-02 18:41:20 +00:00
$this -> _extended_model = new e_user_extended_model ( $this );
2010-04-28 15:44:46 +00:00
}
return $this -> _extended_model ;
}
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
/**
* Set user extended model
2010-05-05 15:05:32 +00:00
*
2010-04-28 15:44:46 +00:00
* @ param e_user_extended_model $extended_model
* @ return e_user_model
*/
2010-05-02 18:41:20 +00:00
public function setExtendedModel ( $extended_model )
2010-04-28 15:44:46 +00:00
{
$this -> _extended_model = $extended_model ;
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-05-13 15:47:31 +00:00
/**
* Get user config model
*
* @ return e_user_pref
*/
public function getConfig ()
{
if ( null === $this -> _user_config )
{
$this -> _user_config = new e_user_pref ( $this );
}
return $this -> _user_config ;
}
/**
* Set user config model
*
* @ param e_user_pref $user_config
* @ return e_user_model
*/
public function setConfig ( e_user_pref $user_config )
{
$this -> _user_config = $user_config ;
return $this ;
}
2010-04-28 15:44:46 +00:00
/**
2010-05-02 18:41:20 +00:00
* Get current user editor model
* @ return e_user_model
2010-04-28 15:44:46 +00:00
*/
2010-05-02 18:41:20 +00:00
public function getEditor ()
2010-04-28 15:44:46 +00:00
{
2010-05-02 18:41:20 +00:00
return $this -> _editor ;
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
2010-05-15 17:33:11 +00:00
* Set current user editor model
2010-05-02 18:41:20 +00:00
* @ return e_user_model
*/
2010-05-15 17:33:11 +00:00
public function setEditor ( e_user_model $user_model )
2010-05-02 18:41:20 +00:00
{
$this -> _editor = $user_model ;
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
* Check if passed field is writable
* @ param string $field
* @ return boolean
*/
public function isWritable ( $field )
{
2010-05-13 15:47:31 +00:00
$perm = false ;
$editor = $this -> getEditor ();
if ( $this -> getId () === $editor -> getId () || $editor -> isMainAdmin () || $editor -> checkAdminPerms ( '4' ))
$perm = true ;
return ( $perm && ! in_array ( $field , array ( $this -> getFieldIdName (), 'user_admin' , 'user_perms' , 'user_prefs' )));
2010-04-28 15:44:46 +00:00
}
2010-03-22 15:45:47 +00:00
2010-05-15 17:33:11 +00:00
/**
* Check if passed field is readable by the Editor
* @ param string $field
* @ return boolean
*/
public function isReadable ( $field )
{
$perm = false ;
$editor = $this -> getEditor ();
if ( $this -> getId () === $editor -> getId () || $editor -> isMainAdmin () || $editor -> checkAdminPerms ( '4' ))
$perm = true ;
return ( $perm || ( ! in_array ( $field , array ( 'user_admin' , 'user_perms' , 'user_prefs' , 'user_password' ) && $editor -> checkClass ( $this -> _memberlist_access ))));
}
2010-03-22 15:45:47 +00:00
/**
* Set current object as a target
2010-05-05 15:05:32 +00:00
*
2010-03-22 15:45:47 +00:00
* @ return e_user_model
*/
2010-05-02 18:41:20 +00:00
protected function setAsTarget ()
2010-03-22 15:45:47 +00:00
{
2010-05-13 15:47:31 +00:00
e107 :: setRegistry ( 'core/e107/user/' . $this -> getId (), $this );
2010-03-22 15:45:47 +00:00
return $this ;
}
/**
* Clear registered target
2010-05-05 15:05:32 +00:00
*
2010-03-22 15:45:47 +00:00
* @ return e_user_model
*/
2010-05-02 18:41:20 +00:00
protected function clearTarget ()
2010-03-22 15:45:47 +00:00
{
2010-05-13 15:47:31 +00:00
e107 :: setRegistry ( 'core/e107/user' . $this -> getId (), null );
2010-03-22 15:45:47 +00:00
return $this ;
}
/**
* @ see e_model #load($id, $force)
*/
public function load ( $user_id = 0 , $force = false )
{
2015-05-10 12:02:32 -07:00
$qry = " SELECT u.*, ue.* FROM #user AS u LEFT JOIN #user_extended as ue ON u.user_id=ue.user_extended_id WHERE u.user_id= { ID} " ;
2010-05-28 13:24:25 +00:00
$this -> setParam ( 'db_query' , $qry );
2010-03-22 15:45:47 +00:00
parent :: load ( $user_id , $force );
2010-05-05 15:05:32 +00:00
if ( $this -> getId ())
2010-04-28 15:44:46 +00:00
{
// no errors - register
2010-05-02 18:41:20 +00:00
$this -> setAsTarget ()
-> setEditor ( e107 :: getUser ()); //set current user as default editor
}
}
2010-05-05 15:05:32 +00:00
2010-05-13 15:47:31 +00:00
/**
* Additional security while applying posted
* data to user model
* @ return e_user_model
*/
2016-02-14 19:00:12 -08:00
public function mergePostedData ( $strict = true , $sanitize = true , $validate = true )
2010-05-13 15:47:31 +00:00
{
$posted = $this -> getPostedData ();
foreach ( $posted as $key => $value )
{
if ( ! $this -> isWritable ( $key ))
{
$this -> removePosted ( $key );
continue ;
}
$this -> _modifyPostedData ( $key , $value );
}
parent :: mergePostedData ( true , true , true );
return $this ;
}
protected function _modifyPostedData ( $key , $value )
{
// TODO - add more here
switch ( $key )
{
case 'password1' :
// compare validation rule
$this -> setPosted ( 'user_password' , array ( $value , $this -> getPosted ( 'password2' )));
break ;
}
}
2010-05-02 18:41:20 +00:00
/**
* Send model data to DB
*/
2012-07-31 07:32:00 +00:00
public function save ( $noEditorCheck = false , $force = false , $session = false )
2010-05-02 18:41:20 +00:00
{
2012-07-31 07:32:00 +00:00
if ( ! $noEditorCheck && ! $this -> checkEditorPerms ())
2010-05-02 18:41:20 +00:00
{
return false ; // TODO - message, admin log
2010-05-05 15:05:32 +00:00
}
2010-05-13 15:47:31 +00:00
// sync user prefs
$this -> getConfig () -> apply ();
// TODO - do the save manually in this order: validate() on user model, save() on extended fields, save() on user model
2010-05-05 15:05:32 +00:00
$ret = parent :: save ( true , $force , $session );
2012-02-07 16:37:44 +00:00
2010-05-05 15:05:32 +00:00
if ( false !== $ret && null !== $this -> _extended_model ) // don't load extended fields if not already used
{
2016-04-21 12:04:09 -07:00
$ret_e = $this -> _extended_model -> save ( true , $force , $session );
2010-05-05 15:05:32 +00:00
if ( false !== $ret_e )
{
return ( $ret_e + $ret );
}
return false ;
}
return $ret ;
}
public function saveDebug ( $extended = true , $return = false , $undo = true )
{
$ret = array ();
$ret [ 'CORE_FIELDS' ] = parent :: saveDebug ( true , $undo );
if ( $extended && null !== $this -> _extended_model )
2010-05-02 18:41:20 +00:00
{
2010-05-05 15:05:32 +00:00
$ret [ 'EXTENDED_FIELDS' ] = $this -> _extended_model -> saveDebug ( true , $undo );
2010-04-28 15:44:46 +00:00
}
2010-05-05 15:05:32 +00:00
if ( $return ) return $ret ;
print_a ( $ret );
2010-04-28 15:44:46 +00:00
}
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
public function destroy ()
{
$this -> clearTarget ()
-> removeData ();
2010-05-14 18:45:51 +00:00
$this -> _class_list = array ();
$this -> _editor = null ;
$this -> _extended_structure = null ;
$this -> _user_config = null ;
2010-05-05 15:05:32 +00:00
if ( null !== $this -> _extended_model )
2010-04-28 15:44:46 +00:00
{
$this -> _extended_model -> destroy ();
2010-05-14 18:45:51 +00:00
$this -> _extended_model = null ;
2010-04-28 15:44:46 +00:00
}
2010-03-22 15:45:47 +00:00
}
2015-05-12 13:53:47 -07:00
/**
* Add userclass to user and save .
* @ param null $userClassId
* @ return bool
*/
public function addClass ( $userClassId = null )
{
if ( empty ( $userClassId ))
{
return false ;
}
$curClasses = explode ( " , " , $this -> getData ( 'user_class' ));
$curClasses [] = $userClassId ;
$curClasses = array_unique ( $curClasses );
$insert = implode ( " , " , $curClasses );
//FIXME - @SecretR - I'm missing something here with setCore() etc.
// $this->setCore('user_class',$insert );
// $this->saveDebug(false);
$uid = $this -> getData ( 'user_id' );
return e107 :: getDb () -> update ( 'user' , " user_class=' " . $insert . " ' WHERE user_id = " . $uid . " LIMIT 1 " );
}
/**
* Remove a userclass from the user .
* @ param null $userClassId
* @ return bool
*/
public function removeClass ( $userClassId = null )
{
if ( empty ( $userClassId ))
{
return false ;
}
$curClasses = explode ( " , " , $this -> getData ( 'user_class' ));
foreach ( $curClasses as $k => $v )
{
if ( $v == $userClassId )
{
unset ( $curClasses [ $k ]);
}
}
$uid = $this -> getData ( 'user_id' );
$insert = implode ( " , " , $curClasses );
return e107 :: getDb () -> update ( 'user' , " user_class=' " . $insert . " ' WHERE user_id = " . $uid . " LIMIT 1 " );
}
2010-03-22 15:45:47 +00:00
}
2010-05-02 18:41:20 +00:00
// TODO - add some more useful methods, sc_* methods support
class e_system_user extends e_user_model
2010-03-22 15:45:47 +00:00
{
2014-08-16 17:19:50 -07:00
public $debug = false ;
2010-05-02 18:41:20 +00:00
/**
* Constructor
2010-05-05 15:05:32 +00:00
*
2010-05-02 18:41:20 +00:00
* @ param array $user_data trusted data , loaded from DB
* @ return void
*/
public function __construct ( $user_data = array ())
{
2010-07-24 16:13:42 +00:00
parent :: __construct ( $user_data );
2010-05-28 13:24:25 +00:00
$this -> setEditor ( e107 :: getUser ());
2010-05-02 18:41:20 +00:00
}
2010-05-05 15:05:32 +00:00
/**
* Returns always false
* Even if user data belongs to the current user , Current User interface
* is not available
*
* @ return boolean
*/
2010-04-28 15:44:46 +00:00
final public function isCurrent ()
{
2010-05-02 18:41:20 +00:00
// check against current system user
2010-05-05 15:05:32 +00:00
//return ($this->getId() && $this->getId() == e107::getUser()->getId());
return false ;
2010-04-28 15:44:46 +00:00
}
2012-07-31 07:32:00 +00:00
/**
* Send user email
* @ param mixed $userInfo array data or null for current logged in user or any object subclass of e_object ( @ see e_system_user :: renderEmail () for field requirements )
*/
2012-12-10 01:20:38 +02:00
public function email ( $type = 'email' , $options = array (), $userInfo = null )
2012-07-31 07:32:00 +00:00
{
if ( null === $userInfo )
{
$userInfo = $this -> getData ();
}
elseif ( is_object ( $userInfo ) && get_class ( $userInfo ) == 'e_object' || is_subclass_of ( $userInfo , 'e_object' ))
{
$userInfo = $userInfo -> getData ();
}
if ( empty ( $userInfo ) || ! vartrue ( $userInfo [ 'user_email' ])) return false ;
2012-12-10 01:20:38 +02:00
// plain password could be passed only via $options
unset ( $userInfo [ 'user_password' ]);
2012-07-31 07:32:00 +00:00
if ( $options && is_array ( $options ))
{
$userInfo = array_merge ( $options , $userInfo );
}
2012-12-09 04:03:28 +02:00
$eml = $this -> renderEmail ( $type , $userInfo );
2014-08-16 17:19:50 -07:00
if ( empty ( $eml ))
{
if ( $this -> debug )
{
2016-06-10 17:52:08 -07:00
echo '$eml returned nothing on Line ' . __LINE__ . ' of user_model.php using $type = ' . $type ;
2014-10-03 20:59:05 -07:00
print_a ( $userInfo );
2014-08-16 17:19:50 -07:00
}
return false ;
}
else
{
if ( $this -> debug )
{
echo '<h3>$eml array</h3>' ;
print_a ( $eml );
2016-06-10 17:52:08 -07:00
$temp = var_export ( $eml , true );
print_a ( $temp );
2014-08-16 17:19:50 -07:00
}
}
2012-07-31 07:32:00 +00:00
$mailer = e107 :: getEmail ();
2012-12-09 04:03:28 +02:00
$mailer -> template = $eml [ 'template' ];
2014-08-16 17:19:50 -07:00
2012-12-09 04:03:28 +02:00
2012-12-10 01:20:38 +02:00
// Custom e107 Header
if ( $userInfo [ 'user_id' ])
{
2014-08-16 17:19:50 -07:00
$eml [ 'e107_header' ] = $userInfo [ 'user_id' ];
// $mailer->AddCustomHeader("X-e107-id: {$userInfo['user_id']}");
2012-12-10 01:20:38 +02:00
}
2015-05-16 19:17:24 -07:00
if ( getperms ( '0' ) && E107_DEBUG_LEVEL > 0 )
{
e107 :: getMessage () -> addDebug ( " Email Debugger active. <b>Simulation Only!</b> " );
e107 :: getMessage () -> addDebug ( $mailer -> preview ( $eml ));
return true ;
}
2017-10-29 11:25:05 -07:00
if ( ! empty ( $options [ 'debug' ]))
{
return $mailer -> preview ( $eml );
}
2012-12-10 01:20:38 +02:00
2012-07-31 07:32:00 +00:00
return $mailer -> sendEmail ( $userInfo [ 'user_email' ], $userInfo [ 'user_name' ], $eml , false );
}
/**
* Render user email .
* Additional user fields :
* 'mail_subject' -> required when type is not signup
* 'mail_body' -> required when type is not signup
* 'mail_copy_to' -> optional , carbon copy , used when type is not signup
* 'mail_bcopy_to' -> optional , blind carbon copy , used when type is not signup
* 'mail_attach' -> optional , attach files , available for all types , additionally it overrides $SIGNUPEMAIL_ATTACHMENTS when type is signup
* 'mail_options' -> optional , available for all types , any additional valid mailer option as described in e107Email :: sendEmail () phpDoc help ( options above can override them )
* All standard user fields from the DB ( user_name , user_loginname , etc . )
*
2012-12-10 17:17:51 +02:00
* @ param string $type signup | notify | email | quickadd
2012-07-31 07:32:00 +00:00
* @ param array $userInfo
* @ return array
*/
public function renderEmail ( $type , $userInfo )
{
$pref = e107 :: getPref ();
$ret = array ();
2014-08-16 17:19:50 -07:00
$tp = e107 :: getParser ();
2014-10-03 20:59:05 -07:00
$mes = e107 :: getMessage ();
2012-07-31 07:32:00 +00:00
2014-08-16 17:19:50 -07:00
2012-07-31 07:32:00 +00:00
// mailer options
if ( isset ( $userInfo [ 'mail_options' ]) && is_array ( $userInfo [ 'mail_options' ]))
{
$ret = $userInfo [ 'mail_options' ];
}
2012-12-10 17:17:51 +02:00
// required for signup and quickadd email type
2012-12-10 01:20:38 +02:00
e107 :: coreLan ( 'signup' );
2014-08-16 17:19:50 -07:00
$EMAIL_TEMPLATE = e107 :: getCoreTemplate ( 'email' );
if ( ! is_array ( $EMAIL_TEMPLATE )) //BC Fixes. pre v2 alpha3.
2012-07-31 07:32:00 +00:00
{
2014-08-16 17:19:50 -07:00
// load from old location. (root of theme folder if it exists)
2016-06-01 12:33:15 -07:00
$SIGNUPEMAIL_SUBJECT = '' ;
$SIGNUPEMAIL_CC = '' ;
$SIGNUPEMAIL_BCC = '' ;
$SIGNUPEMAIL_ATTACHMENTS = '' ;
$SIGNUPEMAIL_TEMPLATE = '' ;
2014-08-16 17:19:50 -07:00
if ( file_exists ( THEME . 'email_template.php' ))
{
include ( THEME . 'email_template.php' );
}
else
{
// include core default.
include ( e107 :: coreTemplatePath ( 'email' ));
}
// BC Fixes.
2014-08-17 20:53:44 -07:00
$EMAIL_TEMPLATE [ 'signup' ][ 'subject' ] = $SIGNUPEMAIL_SUBJECT ;
$EMAIL_TEMPLATE [ 'signup' ][ 'cc' ] = $SIGNUPEMAIL_CC ;
$EMAIL_TEMPLATE [ 'signup' ][ 'bcc' ] = $SIGNUPEMAIL_BCC ;
$EMAIL_TEMPLATE [ 'signup' ][ 'attachments' ] = $SIGNUPEMAIL_ATTACHMENTS ;
$EMAIL_TEMPLATE [ 'signup' ][ 'body' ] = $SIGNUPEMAIL_TEMPLATE ;
2014-08-16 17:19:50 -07:00
2014-08-17 20:53:44 -07:00
$EMAIL_TEMPLATE [ 'quickadduser' ][ 'body' ] = $QUICKADDUSER_TEMPLATE [ 'email_body' ];
$EMAIL_TEMPLATE [ 'notify' ][ 'body' ] = $NOTIFY_TEMPLATE [ 'email_body' ];
2014-08-16 17:19:50 -07:00
2012-07-31 07:32:00 +00:00
}
$template = '' ;
switch ( $type )
{
case 'signup' :
2014-08-16 17:19:50 -07:00
$template = ( vartrue ( $SIGNUPPROVIDEREMAIL_TEMPLATE )) ? $SIGNUPPROVIDEREMAIL_TEMPLATE : $EMAIL_TEMPLATE [ 'signup' ][ 'body' ];
2014-08-17 20:53:44 -07:00
$ret [ 'template' ] = 'signup' ; // // false Don't allow additional headers (mailer) ??
2012-07-31 07:32:00 +00:00
break ;
2012-12-10 17:17:51 +02:00
case 'quickadd' :
2014-08-17 20:53:44 -07:00
$template = $EMAIL_TEMPLATE [ 'quickadduser' ][ 'body' ];
$ret [ 'template' ] = 'quickadduser' ; // Don't allow additional headers (mailer)
2012-12-10 17:17:51 +02:00
break ;
2012-07-31 07:32:00 +00:00
2014-08-16 17:19:50 -07:00
case 'notify' :
if ( vartrue ( $userInfo [ 'mail_body' ])) $template = $userInfo [ 'mail_body' ]; //$NOTIFY_HEADER.$userInfo['mail_body'].$NOTIFY_FOOTER;
2012-12-09 04:03:28 +02:00
$ret [ 'template' ] = 'notify' ;
2012-07-31 07:32:00 +00:00
break ;
2014-08-16 17:19:50 -07:00
case 'email' :
case 'default' :
2012-12-09 04:03:28 +02:00
if ( vartrue ( $userInfo [ 'mail_body' ])) $template = $userInfo [ 'mail_body' ]; //$EMAIL_HEADER.$userInfo['mail_body'].$EMAIL_FOOTER;
2014-08-16 17:19:50 -07:00
$ret [ 'template' ] = 'default' ;
2012-07-31 07:32:00 +00:00
break ;
}
2014-10-03 20:59:05 -07:00
if ( ! $template )
{
$mes -> addDebug ( '$template is empty in user_model.php line 1171.' ); // Debug only, do not translate.
return array ();
}
2012-07-31 07:32:00 +00:00
2016-04-04 18:00:07 -07:00
//
2012-12-10 17:17:51 +02:00
2012-07-31 07:32:00 +00:00
// signup email only
if ( $type == 'signup' )
{
2014-08-16 17:19:50 -07:00
$HEAD = '' ;
$FOOT = '' ;
2016-04-04 18:00:07 -07:00
$pass_show = e107 :: pref ( 'core' , 'user_reg_secureveri' , false );
2014-08-16 17:19:50 -07:00
2013-04-30 17:01:02 +03:00
$ret [ 'e107_header' ] = $userInfo [ 'user_id' ];
2014-08-17 20:53:44 -07:00
2014-08-16 17:19:50 -07:00
if ( vartrue ( $EMAIL_TEMPLATE [ 'signup' ][ 'cc' ])) { $ret [ 'email_copy_to' ] = $EMAIL_TEMPLATE [ 'signup' ][ 'cc' ]; }
if ( vartrue ( $EMAIL_TEMPLATE [ 'signup' ][ 'bcc' ])) { $ret [ 'email_bcopy_to' ] = $EMAIL_TEMPLATE [ 'signup' ][ 'bcc' ]; }
2013-04-30 17:01:02 +03:00
if ( vartrue ( $userInfo [ 'email_attach' ])) { $ret [ 'email_attach' ] = $userInfo [ 'mail_attach' ]; }
2014-08-16 17:19:50 -07:00
elseif ( vartrue ( $EMAIL_TEMPLATE [ 'signup' ][ 'attachments' ])) { $ret [ 'email_attach' ] = $EMAIL_TEMPLATE [ 'signup' ][ 'attachments' ]; }
2012-07-31 07:32:00 +00:00
$style = vartrue ( $SIGNUPEMAIL_LINKSTYLE ) ? " style=' { $SIGNUPEMAIL_LINKSTYLE } ' " : " " ;
2015-05-21 16:53:10 -07:00
if ( empty ( $userInfo [ 'activation_url' ]) && ! empty ( $userInfo [ 'user_sess' ]) && ! empty ( $userInfo [ 'user_id' ]))
{
$userInfo [ 'activation_url' ] = SITEURL . " signup.php?activate. " . $userInfo [ 'user_id' ] . " . " . $userInfo [ 'user_sess' ];
}
2012-07-31 07:32:00 +00:00
2014-08-17 20:53:44 -07:00
$sc = array ();
2013-05-24 22:18:20 +03:00
2014-08-17 20:53:44 -07:00
$sc [ 'LOGINNAME' ] = intval ( $pref [ 'allowEmailLogin' ]) === 0 ? $userInfo [ 'user_loginname' ] : $userInfo [ 'user_email' ];
2016-04-04 18:00:07 -07:00
$sc [ 'PASSWORD' ] = ( $pass_show && ! empty ( $userInfo [ 'user_password' ])) ? '*************' : $userInfo [ 'user_password' ];
2014-08-17 20:53:44 -07:00
$sc [ 'ACTIVATION_LINK' ] = strpos ( $userInfo [ 'activation_url' ], 'http' ) === 0 ? '<a href="' . $userInfo [ 'activation_url' ] . '">' . $userInfo [ 'activation_url' ] . '</a>' : $userInfo [ 'activation_url' ];
// $sc['SITENAME'] = SITENAME;
$sc [ 'SITEURL' ] = " <a href=' " . SITEURL . " ' { $style } > " . SITEURL . " </a> " ;
$sc [ 'USERNAME' ] = $userInfo [ 'user_name' ];
$sc [ 'USERURL' ] = vartrue ( $userInfo [ 'user_website' ]) ? $userInfo [ 'user_website' ] : " " ;
$sc [ 'DISPLAYNAME' ] = $userInfo [ 'user_login' ] ? $userInfo [ 'user_login' ] : $userInfo [ 'user_name' ];
$sc [ 'EMAIL' ] = $userInfo [ 'user_email' ];
$sc [ 'ACTIVATION_URL' ] = $userInfo [ 'activation_url' ];
2014-08-16 17:19:50 -07:00
2016-06-10 17:52:08 -07:00
$ret [ 'subject' ] = $EMAIL_TEMPLATE [ 'signup' ][ 'subject' ]; // $subject;
2012-07-31 07:32:00 +00:00
$ret [ 'send_html' ] = TRUE ;
2014-08-17 20:53:44 -07:00
$ret [ 'shortcodes' ] = $sc ;
2012-07-31 07:32:00 +00:00
2014-08-16 17:19:50 -07:00
if ( ! varset ( $EMAIL_TEMPLATE [ 'signup' ][ 'header' ]))
{
$HEAD = " <!DOCTYPE html PUBLIC \" -//W3C//DTD XHTML 1.1//EN \" \" http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd \" > \n " ;
$HEAD .= " <html xmlns='http://www.w3.org/1999/xhtml' > \n " ;
$HEAD .= " <head><meta http-equiv='content-type' content='text/html; charset=utf-8' /> \n " ;
$HEAD .= ( $SIGNUPEMAIL_USETHEME == 1 ) ? " <link rel= \" stylesheet \" href= \" " . SITEURLBASE . THEME_ABS . " style.css \" type= \" text/css \" /> \n " : " " ;
$HEAD .= " <title> " . LAN_SIGNUP_58 . " </title> \n " ;
if ( $SIGNUPEMAIL_USETHEME == 2 ) // @deprecated in favor of {STYLESHEET}
{
$CSS = file_get_contents ( THEME . " style.css " );
$HEAD .= " <style> \n " . $CSS . " \n </style> " ;
}
$HEAD .= " </head> \n " ;
if ( vartrue ( $SIGNUPEMAIL_BACKGROUNDIMAGE )) // @deprecated.
{
$HEAD .= " <body background= \" " . $SIGNUPEMAIL_BACKGROUNDIMAGE . " \" > \n " ;
}
else
{
$HEAD .= " <body> \n " ;
}
2012-07-31 07:32:00 +00:00
2014-08-16 17:19:50 -07:00
}
else
2012-07-31 07:32:00 +00:00
{
2014-08-17 20:53:44 -07:00
$HEAD = " " ; // $tp->parseTemplate($EMAIL_TEMPLATE['signup']['header'], true);
2012-07-31 07:32:00 +00:00
}
2014-08-16 17:19:50 -07:00
if ( ! varset ( $EMAIL_TEMPLATE [ 'signup' ][ 'footer' ]))
2012-07-31 07:32:00 +00:00
{
2014-08-16 17:19:50 -07:00
$FOOT = " \n </body> \n </html> \n " ;
2012-07-31 07:32:00 +00:00
}
else
{
2014-08-17 20:53:44 -07:00
$FOOT = " " ; // $tp->parseTemplate($EMAIL_TEMPLATE['signup']['footer'], true);
2012-07-31 07:32:00 +00:00
}
2014-08-17 20:53:44 -07:00
$ret [ 'send_html' ] = TRUE ;
$ret [ 'email_body' ] = $HEAD . $template . $FOOT ; // e107::getParser()->parseTemplate(str_replace($search,$replace,$HEAD.$template.$FOOT), true);
$ret [ 'preview' ] = $tp -> parseTemplate ( $ret [ 'email_body' ], true , $sc ); // Non-standard field
$ret [ 'shortcodes' ] = $sc ;
2012-07-31 07:32:00 +00:00
return $ret ;
}
2014-10-03 20:59:05 -07:00
2014-08-17 20:53:44 -07:00
// all other email types
2014-10-03 20:59:05 -07:00
if ( ! $userInfo [ 'mail_subject' ])
2014-08-17 20:53:44 -07:00
{
2014-10-03 20:59:05 -07:00
$mes -> addDebug ( 'No Email subject provided to renderEmail() method.' ); // Debug only, do not translate.
return array ();
2014-08-17 20:53:44 -07:00
}
2012-07-31 07:32:00 +00:00
2016-04-04 18:00:07 -07:00
2014-10-03 20:59:05 -07:00
$templateName = $ret [ 'template' ];
2016-06-10 17:52:08 -07:00
// $ret['email_subject'] = varset($EMAIL_TEMPLATE[$templateName]['subject'], $EMAIL_TEMPLATE['default']['subject']) ; // $subject;
$ret [ 'subject' ] = $userInfo [ 'mail_subject' ];
2014-08-17 20:53:44 -07:00
$ret [ 'e107_header' ] = $userInfo [ 'user_id' ];
2012-07-31 07:32:00 +00:00
2014-08-17 20:53:44 -07:00
if ( vartrue ( $userInfo [ 'email_copy_to' ])) { $ret [ 'email_copy_to' ] = $userInfo [ 'email_copy_to' ]; }
if ( vartrue ( $userInfo [ 'email_bcopy_to' ])) { $ret [ 'email_bcopy_to' ] = $userInfo [ 'email_bcopy_to' ]; }
if ( vartrue ( $userInfo [ 'email_attach' ])) { $ret [ 'email_attach' ] = $userInfo [ 'email_attach' ]; }
2012-07-31 07:32:00 +00:00
2014-08-17 20:53:44 -07:00
$sc = array ();
$sc [ 'LOGINNAME' ] = intval ( $pref [ 'allowEmailLogin' ]) === 0 ? $userInfo [ 'user_loginname' ] : $userInfo [ 'user_email' ];
$sc [ 'DISPLAYNAME' ] = $userInfo [ 'user_login' ] ? $userInfo [ 'user_login' ] : $userInfo [ 'user_name' ];
$sc [ 'SITEURL' ] = " <a href=' " . SITEURL . " '> " . SITEURL . " </a> " ;
$sc [ 'USERNAME' ] = $userInfo [ 'user_name' ];
2016-04-04 18:00:07 -07:00
$sc [ 'USERURL' ] = vartrue ( $userInfo [ 'user_website' ], '' );
$sc [ 'PASSWORD' ] = vartrue ( $userInfo [ 'user_password' ], '***********' );
2014-10-03 20:59:05 -07:00
$sc [ 'SUBJECT' ] = $userInfo [ 'mail_subject' ];
2014-08-17 20:53:44 -07:00
2016-06-10 17:52:08 -07:00
2012-12-11 18:44:34 +02:00
if ( isset ( $userInfo [ 'activation_url' ]))
{
2014-08-17 20:53:44 -07:00
$sc [ 'ACTIVATION_URL' ] = $userInfo [ 'activation_url' ];
$sc [ 'ACTIVATION_LINK' ] = strpos ( $userInfo [ 'activation_url' ], 'http' ) === 0 ? '<a href="' . $userInfo [ 'activation_url' ] . '">' . $userInfo [ 'activation_url' ] . '</a>' : $userInfo [ 'activation_url' ];
2012-12-11 18:44:34 +02:00
}
2016-06-10 17:52:08 -07:00
$ret [ 'send_html' ] = true ;
2014-08-17 20:53:44 -07:00
$ret [ 'email_body' ] = $template ; // e107::getParser()->parseTemplate(str_replace($search, $replace, $template)); - performed in mail handler.
$ret [ 'preview' ] = $ret [ 'mail_body' ]; // Non-standard field
$ret [ 'shortcodes' ] = $sc ;
2012-07-31 07:32:00 +00:00
return $ret ;
}
2010-03-22 15:45:47 +00:00
}
2010-05-02 18:41:20 +00:00
/**
2010-05-13 15:47:31 +00:00
* Current system user
2010-05-02 18:41:20 +00:00
* @ author SecretR
*/
2010-03-22 15:45:47 +00:00
class e_user extends e_user_model
2010-05-05 15:05:32 +00:00
{
2010-05-13 15:47:31 +00:00
private $_session_data = null ;
private $_session_key = null ;
private $_session_type = null ;
private $_session_error = false ;
private $_parent_id = false ;
private $_parent_data = array ();
private $_parent_extmodel = null ;
private $_parent_extstruct = null ;
private $_parent_config = null ;
2012-07-31 07:32:00 +00:00
/**
* @ var Hybrid_Provider_Model
*/
protected $_provider ;
2010-05-13 15:47:31 +00:00
2010-05-02 18:41:20 +00:00
public function __construct ()
{
2010-05-13 15:47:31 +00:00
$this -> setSessionData () // retrieve data from current session
-> load () // load current user from DB
-> setEditor ( $this ); // reference to self
2010-05-02 18:41:20 +00:00
}
2012-07-31 07:32:00 +00:00
2010-05-02 18:41:20 +00:00
/**
* Yes , it ' s current user - return always true
* NOTE : it ' s not user check , use isUser () instead !
* @ return boolean
*/
2010-04-28 15:44:46 +00:00
final public function isCurrent ()
{
2010-05-02 18:41:20 +00:00
return true ;
}
2010-05-05 15:05:32 +00:00
2010-05-13 15:47:31 +00:00
/**
* Get parent user ID - present if main admin is browsing
* front - end logged in as another user account
*
* @ return integer or false if not present
*/
final public function getParentId ()
{
return $this -> _parent_id ;
}
2012-07-31 07:32:00 +00:00
/**
* Init external user login / signup provider
* @ return e_system_user
*/
public function initProvider ()
{
if ( null !== $this -> _provider ) return $this ;
if ( $this -> get ( 'user_xup' ))
{
$providerId = $this -> getProviderName ();
require_once ( e_HANDLER . 'user_handler.php' );
$this -> _provider = new e_user_provider ( $providerId );
$this -> _provider -> init ();
}
}
/**
* Get external user provider
* @ return Hybrid_Provider_Model
*/
public function getProvider ()
{
if ( null === $this -> _provider ) $this -> initProvider ();
return $this -> _provider ;
}
/**
* Set external user provider ( already initialized )
* @ return e_user
*/
public function setProvider ( $provider )
{
$this -> _provider = $provider ;
return $this ;
}
/**
* Check if this user has assigned login provider
* @ return boolean
*/
public function hasProvider ()
{
return ( $this -> getProvider () !== null );
}
2010-05-13 15:47:31 +00:00
/**
* User login
* @ param string $uname
* @ param string $upass_plain
* @ param boolean $uauto
* @ param string $uchallange
2010-05-14 18:45:51 +00:00
* @ param boolean $noredirect
2010-05-13 15:47:31 +00:00
* @ return boolean success
*/
2010-05-14 18:45:51 +00:00
final public function login ( $uname , $upass_plain , $uauto = false , $uchallange = false , $noredirect = true )
2010-05-02 18:41:20 +00:00
{
2010-05-13 15:47:31 +00:00
if ( $this -> isUser ()) return false ;
2011-09-14 11:30:58 +00:00
$userlogin = new userlogin ();
$userlogin -> login ( $uname , $upass_plain , $uauto , $uchallange , $noredirect );
2015-02-12 16:52:30 -08:00
$userdata = $userlogin -> getUserData ();
$this -> setSessionData ( true ) -> setData ( $userdata );
e107 :: getEvent () -> trigger ( 'user_login' , $userdata );
2010-05-13 15:47:31 +00:00
2010-05-02 18:41:20 +00:00
return $this -> isUser ();
}
2012-07-31 07:32:00 +00:00
/**
* User login via external user provider
* @ param string $xup external user provider identifier
* @ return boolean success
*/
final public function loginProvider ( $xup )
{
if ( ! e107 :: getPref ( 'social_login_active' , false )) return false ;
if ( $this -> isUser ()) return true ;
$userlogin = new userlogin ();
$userlogin -> login ( $xup , '' , 'provider' , false , true );
2016-03-16 12:05:10 -07:00
$userdata = $userlogin -> getUserData ();
2016-12-01 09:39:32 +01:00
if ( defset ( 'E107_DEBUG_LEVEL' , 0 ) > 0 )
{
e107 :: getLog () -> add ( 'XUP Debug' , ( __CLASS__ . ':' . __METHOD__ . '-' . __LINE__ ), E_LOG_INFORMATIVE , " XUP_DEBUG " );
}
2015-02-12 16:52:30 -08:00
$this -> setSessionData ( true ) -> setData ( $userdata );
2016-12-01 09:53:50 +01:00
e107 :: getEvent () -> trigger ( 'user_xup_login' , $userdata );
2012-07-31 07:32:00 +00:00
return $this -> isUser ();
}
2010-05-05 15:05:32 +00:00
2010-05-14 18:45:51 +00:00
/**
* Login as another user account
* @ param integer $user_id
* @ return boolean success
*/
2010-05-13 15:47:31 +00:00
final public function loginAs ( $user_id )
{
// TODO - set session data required for loadAs()
2010-05-14 18:45:51 +00:00
if ( $this -> getParentId ()
|| ! $this -> isMainAdmin ()
|| empty ( $user_id )
|| $this -> getSessionDataAs ()
|| $user_id == $this -> getId ()
) return false ;
$key = $this -> _session_key . '_as' ;
if ( 'session' == $this -> _session_type )
{
$_SESSION [ $key ] = $user_id ;
}
elseif ( 'cookie' == $this -> _session_type )
{
$_COOKIE [ $key ] = $user_id ;
cookie ( $key , $user_id );
}
2010-05-17 14:13:50 +00:00
// TODO - lan
e107 :: getAdminLog () -> log_event ( 'Head Admin used Login As feature' , 'Head Admin [#' . $this -> getId () . '] ' . $this -> getName () . ' logged in user account #' . $user_id );
2010-05-14 18:45:51 +00:00
//$this->loadAs(); - shouldn't be called here - loginAs should be called in Admin area only, loadAs - front-end
return true ;
2010-05-13 15:47:31 +00:00
}
2010-05-02 18:41:20 +00:00
/**
2010-05-05 15:05:32 +00:00
*
2010-05-13 15:47:31 +00:00
* @ return e_user
2010-05-02 18:41:20 +00:00
*/
2010-05-13 15:47:31 +00:00
protected function _initConstants ()
2010-05-02 18:41:20 +00:00
{
//FIXME - BC - constants from init_session() should be defined here
2010-05-13 15:47:31 +00:00
// [SecretR] Not sure we should do this here, it's too restricting - constants can be
// defined once, we need the freedom to do it multiple times - e.g. load() executed in constructor than login(), loginAs() etc.
// called by a controller
// We should switch to e.g. isAdmin() instead of ADMIN constant check
return $this ;
2010-05-02 18:41:20 +00:00
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
2010-05-13 15:47:31 +00:00
* Destroy cookie / session data , self destroy
* @ return e_user
2010-05-02 18:41:20 +00:00
*/
final public function logout ()
{
2012-07-31 07:32:00 +00:00
if ( $this -> hasProvider ())
{
$this -> getProvider () -> logout ();
}
2010-05-13 15:47:31 +00:00
$this -> logoutAs ()
-> _destroySession ();
parent :: destroy ();
2010-10-27 11:31:18 +00:00
//if(session_id()) session_destroy();
e107 :: getSession () -> destroy ();
2010-05-13 15:47:31 +00:00
e107 :: setRegistry ( 'core/e107/current_user' , null );
return $this ;
}
/**
* Destroy cookie / session / model data for current user , resurrect parent user
* @ return e_user
*/
final public function logoutAs ()
{
if ( $this -> getParentId ())
2010-05-02 18:41:20 +00:00
{
2010-05-13 15:47:31 +00:00
// load parent user data
$this -> _extended_model = $this -> _parent_extmodel ;
$this -> _extended_structure = $this -> _parent_extstruct ;
$this -> _user_config = $this -> _parent_config ;
2012-06-15 11:30:37 +00:00
if ( $this -> _parent_model )
$this -> setData ( $this -> _parent_model -> getData ());
2010-05-13 15:47:31 +00:00
// cleanup
$this -> _parent_id = false ;
$this -> _parent_model = $this -> _parent_extstruct = $this -> _parent_extmodel = $this -> _parent_config = null ;
2010-05-02 18:41:20 +00:00
}
2010-05-14 18:45:51 +00:00
$this -> _destroyAsSession ();
2010-05-13 15:47:31 +00:00
return $this ;
2010-05-02 18:41:20 +00:00
}
2012-07-31 07:32:00 +00:00
public function tryProviderSession ( $deniedAs )
{
// don't allow if main admin browse front-end or there is already user session
if (( ! $deniedAs && $this -> getSessionDataAs ()) || null !== $this -> _session_data || ! e107 :: getPref ( 'social_login_active' , false )) return $this ;
2012-12-20 10:45:22 +02:00
try
{
// detect all currently connected providers
$hybrid = e107 :: getHybridAuth (); // init the auth class
$connected = Hybrid_Auth :: getConnectedProviders ();
}
catch ( Exception $e )
{
e107 :: getMessage () -> addError ( '[' . $e -> getCode () . ']' . $e -> getMessage (), 'default' , true );
$session = e107 :: getSession ();
$session -> set ( 'HAuthError' , true );
$connected = false ;
}
2012-07-31 07:32:00 +00:00
// no active session found
if ( ! $connected ) return $this ;
// query DB
$sql = e107 :: getDb ();
$where = array ();
2016-03-16 12:05:10 -07:00
$userdata = array ();
2012-07-31 07:32:00 +00:00
foreach ( $connected as $providerId )
{
$adapter = Hybrid_Auth :: getAdapter ( $providerId );
if ( ! $adapter -> getUserProfile () -> identifier ) continue ;
2016-03-16 12:05:10 -07:00
$profile = $adapter -> getUserProfile ();
$userdata [ 'user_name' ] = $sql -> escape ( $profile -> displayName );
$userdata [ 'user_image' ] = $profile -> photoURL ; // avatar
2017-10-23 13:01:19 -07:00
$userdata [ 'user_email' ] = $profile -> email ;
2016-03-16 12:05:10 -07:00
$id = $providerId . '_' . $profile -> identifier ;
2015-02-14 23:34:15 -08:00
$where [] = " user_xup=' " . $sql -> escape ( $id ) . " ' " ;
2012-07-31 07:32:00 +00:00
}
2016-03-16 12:05:10 -07:00
2012-07-31 07:32:00 +00:00
$where = implode ( ' OR ' , $where );
2017-10-23 13:01:19 -07:00
if ( $sql -> select ( 'user' , 'user_id, user_name, user_email, user_image, user_password, user_xup' , $where ))
2012-07-31 07:32:00 +00:00
{
2016-03-16 12:05:10 -07:00
$user = $sql -> fetch ();
2012-07-31 07:32:00 +00:00
e107 :: getUserSession () -> makeUserCookie ( $user );
$this -> setSessionData ();
2016-03-16 12:05:10 -07:00
2016-06-01 12:33:15 -07:00
$spref = e107 :: pref ( 'social' );
2016-03-16 12:05:10 -07:00
// Update display name or avatar image if they have changed.
2017-10-23 13:01:19 -07:00
if (( empty ( $user [ 'user_email' ]) && ! empty ( $userdata [ 'user_email' ]) ) || ! empty ( $spref [ 'xup_login_update_username' ]) || ! empty ( $spref [ 'xup_login_update_avatar' ]) || ( $userdata [ 'user_name' ] != $user [ 'user_name' ]) || ( $userdata [ 'user_image' ] != $user [ 'user_image' ]))
2016-03-16 12:05:10 -07:00
{
2016-06-01 12:33:15 -07:00
$updateQry = array ();
if ( ! empty ( $spref [ 'xup_login_update_username' ]))
{
$updateQry [ 'user_name' ] = $userdata [ 'user_name' ];
}
if ( ! empty ( $spref [ 'xup_login_update_avatar' ]))
{
$updateQry [ 'user_image' ] = $userdata [ 'user_image' ];
}
2017-10-23 13:01:19 -07:00
if ( empty ( $user [ 'user_email' ]))
{
$updateQry [ 'user_email' ] = $userdata [ 'user_email' ];
}
2016-11-27 22:07:54 +01:00
$updateQry [ 'WHERE' ] = " user_id= " . $user [ 'user_id' ] . " LIMIT 1 " ;
2016-03-16 12:05:10 -07:00
2016-06-01 12:33:15 -07:00
if ( $sql -> update ( 'user' , $updateQry ) !== false )
2016-03-16 12:05:10 -07:00
{
2016-12-01 10:07:05 +01:00
$updatedProfile = array_replace ( $user , $userdata );
2016-12-01 22:57:00 +01:00
e107 :: getEvent () -> trigger ( 'user_xup_updated' , $updatedProfile );
2017-10-23 13:01:19 -07:00
e107 :: getLog () -> add ( 'User Profile Updated' , $userdata , E_LOG_INFORMATIVE , " XUP_LOGIN " , LOG_TO_ADMIN , array ( 'user_id' => $user [ 'user_id' ], 'user_name' => $user [ 'user_name' ], 'user_email' => $userdata [ 'user_email' ]));
2016-03-16 12:05:10 -07:00
}
else
{
2017-10-23 13:01:19 -07:00
e107 :: getLog () -> add ( 'User Profile Update Failed' , $userdata , E_LOG_WARNING , " XUP_LOGIN " , LOG_TO_ADMIN , array ( 'user_id' => $user [ 'user_id' ], 'user_name' => $user [ 'user_name' ], 'user_email' => $userdata [ 'user_email' ]));
2016-03-16 12:05:10 -07:00
}
}
2016-03-16 13:53:57 -07:00
unset ( $user [ 'user_password' ]);
e107 :: getLog () -> user_audit ( USER_AUDIT_LOGIN , '' , $user [ 'user_id' ], $user [ 'user_name' ]);
// e107::getLog()->add('XUP Login', $user, E_LOG_INFORMATIVE, "LOGIN", LOG_TO_ROLLING, array('user_id'=>$user['user_id'],'user_name'=>$user['user_name']));
2012-07-31 07:32:00 +00:00
}
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
* TODO load user data by cookie / session data
* @ return e_user
*/
2010-05-13 15:47:31 +00:00
final public function load ( $force = false , $denyAs = false )
2010-05-02 18:41:20 +00:00
{
2010-05-13 15:47:31 +00:00
if ( ! $force && $this -> getId ()) return $this ;
2010-05-14 18:45:51 +00:00
if ( deftrue ( 'e_ADMIN_AREA' )) $denyAs = true ;
2010-05-13 15:47:31 +00:00
// always run cli as main admin
if ( e107 :: isCli ())
{
$this -> _load ( 1 , $force );
$this -> _initConstants ();
return $this ;
}
2012-07-31 07:32:00 +00:00
// NEW - new external user login provider feature
$this -> tryProviderSession ( $denyAs );
2010-05-13 15:47:31 +00:00
// We have active session
if ( null !== $this -> _session_data )
{
list ( $uid , $upw ) = explode ( '.' , $this -> _session_data );
// Bad cookie - destroy session
if ( empty ( $uid ) || ! is_numeric ( $uid ) || empty ( $upw ))
{
$this -> _destroyBadSession ();
$this -> _initConstants ();
return $this ;
}
$udata = $this -> _load ( $uid , $force );
// Bad cookie - destroy session
if ( empty ( $udata ))
{
$this -> _destroyBadSession ();
$this -> _initConstants ();
return $this ;
}
// we have a match
if ( md5 ( $udata [ 'user_password' ]) == $upw )
{
// set current user data
$this -> setData ( $udata );
// NEW - try 'logged in as' feature
if ( ! $denyAs ) $this -> loadAs ();
2010-05-15 17:33:11 +00:00
// update lastvisit field
$this -> updateVisit ();
// currently does nothing
2010-05-13 15:47:31 +00:00
$this -> _initConstants ();
2012-07-31 07:32:00 +00:00
// init any available external user provider
if ( e107 :: getPref ( 'social_login_active' , false )) $this -> initProvider ();
2010-05-13 15:47:31 +00:00
return $this ;
}
$this -> _destroyBadSession ();
$this -> _initConstants ();
return $this ;
}
return $this ;
}
final public function loadAs ()
{
// FIXME - option to avoid it when browsing Admin area
2010-05-14 18:45:51 +00:00
$loginAs = $this -> getSessionDataAs ();
2010-05-13 15:47:31 +00:00
if ( ! $this -> getParentId () && false !== $loginAs && $loginAs !== $this -> getId () && $loginAs !== 1 && $this -> isMainAdmin ())
{
$uasdata = $this -> _load ( $loginAs );
if ( ! empty ( $uasdata ))
{
// backup parent user data to prevent further db queries
$this -> _parent_id = $this -> getId ();
2010-05-14 18:45:51 +00:00
$this -> _parent_model = new e_user_model ( $this -> getData ());
2010-05-13 15:47:31 +00:00
$this -> setData ( $uasdata );
// not allowed - revert back
if ( $this -> isMainAdmin ())
{
$this -> _parent_id = false ;
$this -> setData ( $this -> _parent_model -> getData ());
$this -> _parent_model = null ;
$this -> _destroyAsSession ();
}
else
{
$this -> _parent_extmodel = $this -> _extended_model ;
$this -> _parent_extstruct = $this -> _extended_structure ;
$this -> _user_config = $this -> _parent_config ;
$this -> _extended_model = $this -> _extended_structure = $this -> _user_config = null ;
}
}
}
else
{
$this -> _parent_id = false ;
$this -> _parent_model = null ;
$this -> _parent_extstruct = $this -> _parent_extmodel = null ;
}
2010-05-14 18:45:51 +00:00
return $this ;
2010-05-13 15:47:31 +00:00
}
2010-05-15 17:33:11 +00:00
/**
* Update user visit timestamp
* @ return void
*/
protected function updateVisit ()
{
// Don't update if main admin is logged in as current (non main admin) user
if ( ! $this -> getParentId ())
{
$sql = e107 :: getDb ();
$this -> set ( 'last_ip' , $this -> get ( 'user_ip' ));
2012-01-02 22:06:22 +00:00
$current_ip = e107 :: getIPHandler () -> getIP ( FALSE );
2010-05-15 17:33:11 +00:00
$update_ip = $this -> get ( 'user_ip' != $current_ip ? " , user_ip = ' " . $current_ip . " ' " : " " );
$this -> set ( 'user_ip' , $current_ip );
if ( $this -> get ( 'user_currentvisit' ) + 3600 < time () || ! $this -> get ( 'user_lastvisit' ))
{
$this -> set ( 'user_lastvisit' , ( integer ) $this -> get ( 'user_currentvisit' ));
$this -> set ( 'user_currentvisit' , time ());
$sql -> db_Update ( 'user' , " user_visits = user_visits + 1, user_lastvisit = " . $this -> get ( 'user_lastvisit' ) . " , user_currentvisit = " . $this -> get ( 'user_currentvisit' ) . " { $update_ip } WHERE user_id=' " . $this -> getId () . " ' " );
}
else
{
$this -> set ( 'user_currentvisit' , time ());
$sql -> db_Update ( 'user' , " user_currentvisit = " . $this -> get ( 'user_currentvisit' ) . " { $update_ip } WHERE user_id=' " . $this -> getId () . " ' " );
}
}
}
2010-05-13 15:47:31 +00:00
final protected function _destroySession ()
{
cookie ( $this -> _session_key , '' , ( time () - 2592000 ));
2010-10-27 11:31:18 +00:00
unset ( $_SESSION [ $this -> _session_key ]);
2010-05-13 15:47:31 +00:00
return $this ;
}
final protected function _destroyAsSession ()
{
$key = $this -> _session_key . '_as' ;
cookie ( $key , '' , ( time () - 2592000 ));
$_SESSION [ $key ] = '' ;
unset ( $_SESSION [ $key ]);
return $this ;
}
final protected function _destroyBadSession ()
{
$this -> _session_error = true ;
return $this -> _destroySession ();
}
2010-05-14 18:45:51 +00:00
final public function getSessionDataAs ()
2010-05-13 15:47:31 +00:00
{
$id = false ;
$key = $this -> _session_key . '_as' ;
if ( 'session' == $this -> _session_type && isset ( $_SESSION [ $key ]) && ! empty ( $_SESSION [ $key ]))
{
$id = $_SESSION [ $key ];
}
elseif ( 'cookie' == $this -> _session_type && isset ( $_COOKIE [ $key ]) && ! empty ( $_COOKIE [ $key ]))
{
$id = $_COOKIE [ $key ];
}
if ( ! empty ( $id ) && is_numeric ( $id )) return intval ( $id );
return false ;
}
final public function setSessionData ( $force = false )
{
if ( $force || null === $this -> _session_data )
{
2011-12-06 08:00:42 +00:00
$this -> _session_data = null ;
2010-05-13 15:47:31 +00:00
$this -> _session_key = e107 :: getPref ( 'cookie_name' , 'e107cookie' );
$this -> _session_type = e107 :: getPref ( 'user_tracking' , 'cookie' );
2010-10-26 07:41:20 +00:00
2010-05-13 15:47:31 +00:00
if ( 'session' == $this -> _session_type && isset ( $_SESSION [ $this -> _session_key ]) && ! empty ( $_SESSION [ $this -> _session_key ]))
{
$this -> _session_data = & $_SESSION [ $this -> _session_key ];
}
elseif ( 'cookie' == $this -> _session_type && isset ( $_COOKIE [ $this -> _session_key ]) && ! empty ( $_COOKIE [ $this -> _session_key ]))
{
$this -> _session_data = & $_COOKIE [ $this -> _session_key ];
}
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-05-13 15:47:31 +00:00
public function hasSessionError ()
{
return $this -> _session_error ;
}
final protected function _load ( $user_id )
{
2015-05-10 12:02:32 -07:00
$qry = 'SELECT u.*, ue.* FROM #user AS u LEFT JOIN #user_extended as ue ON u.user_id=ue.user_extended_id WHERE u.user_id=' . intval ( $user_id );
2015-02-12 16:52:30 -08:00
if ( e107 :: getDb () -> gen ( $qry ))
2010-05-13 15:47:31 +00:00
{
2015-02-12 16:52:30 -08:00
return e107 :: getDb () -> fetch ();
2010-05-13 15:47:31 +00:00
}
return array ();
}
2010-05-02 18:41:20 +00:00
/**
* Not allowed
2010-05-05 15:05:32 +00:00
*
2010-05-02 18:41:20 +00:00
* @ return e_user_model
*/
final protected function setAsTarget ()
{
return $this ;
}
/**
* Not allowed
2010-05-05 15:05:32 +00:00
*
2010-05-02 18:41:20 +00:00
* @ return e_user_model
*/
final protected function clearTarget ()
{
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
public function destroy ()
{
// not allowed - see logout()
2010-04-28 15:44:46 +00:00
}
}
2012-02-07 16:37:44 +00:00
class e_user_extended_model extends e_admin_model
2010-04-28 15:44:46 +00:00
{
2010-05-05 15:05:32 +00:00
/**
* Describes known model fields
* @ var array
*/
protected $_data_fields = array (
'user_extended_id' => 'integer' ,
'user_hidden_fields' => 'string' ,
);
2010-04-28 15:44:46 +00:00
/**
* @ see e_model
* @ var string
*/
protected $_db_table = 'user_extended' ;
/**
* @ see e_model
* @ var string
*/
protected $_field_id = 'user_extended_id' ;
/**
* @ see e_model
* @ var string
*/
protected $_message_stack = 'user' ;
2010-05-05 15:05:32 +00:00
2010-05-15 17:33:11 +00:00
/**
* User class as set in user Adminsitration
*
* @ var integer
*/
protected $_memberlist_access = null ;
2010-05-02 18:41:20 +00:00
/**
* @ var e_user_extended_structure_tree
*/
protected $_structure = null ;
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
* User model , the owner of extended fields model
* @ var e_user_model
*/
protected $_user = null ;
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
* Stores access classes and default value per custom field
* @ var array
*/
protected $_struct_index = array ();
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
* Constructor
* @ param e_user_model $user_model
* @ return void
*/
public function __construct ( e_user_model $user_model )
{
2010-05-15 17:33:11 +00:00
$this -> _memberlist_access = e107 :: getPref ( 'memberlist_access' );
2010-05-02 18:41:20 +00:00
$this -> setUser ( $user_model )
2010-05-15 17:33:11 +00:00
-> load ();
2010-05-02 18:41:20 +00:00
}
2010-05-05 15:05:32 +00:00
2010-05-13 15:47:31 +00:00
/**
* Always return integer
*/
public function getId ()
{
return ( integer ) parent :: getId ();
}
2010-05-02 18:41:20 +00:00
/**
* Get user model
* @ return e_user_model
*/
public function getUser ()
{
return $this -> _user ;
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
* Set User model
2012-04-03 14:39:19 +00:00
* @ param e_user_model $user_model
2010-05-02 18:41:20 +00:00
* @ return e_user_extended_model
*/
public function setUser ( $user_model )
{
$this -> _user = $user_model ;
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
* Get current user editor model
* @ return e_user_model
*/
public function getEditor ()
{
2010-05-15 17:33:11 +00:00
return $this -> getUser () -> getEditor ();
2010-05-02 18:41:20 +00:00
}
2010-05-05 15:05:32 +00:00
2010-05-17 15:51:42 +00:00
/**
* Bad but required ( BC ) method of retrieving all user data
* It ' s here to be used from get_user_data () core function .
* DON ' T USE IT unless you have VERY good reason to do it .
2010-05-28 13:24:25 +00:00
* TODO - revise this ! Merge it to getSystemData , getApplicableData
2010-05-17 15:51:42 +00:00
*
* @ return array
*/
public function getExtendedData ()
{
$ret = array ();
$fields = $this -> getExtendedStructure () -> getFieldTree ();
foreach ( $fields as $id => $field )
{
$value = $this -> getValue ( $field -> getValue ( 'name' ));
if ( null !== $value ) $ret [ $field -> getValue ( 'name' )] = $value ;
}
$ret [ 'user_extended_id' ] = $this -> getId ();
$ret [ 'user_hidden_fields' ] = $this -> get ( 'user_hidden_fields' );
return $ret ;
}
2010-04-28 15:44:46 +00:00
/**
2010-05-28 13:24:25 +00:00
* Get User extended field value . It performs all required read / applicable permission checks
* against current editor / user .
2010-05-02 18:41:20 +00:00
* Returns NULL when field / default value not found or not enough permissions
2010-05-13 15:47:31 +00:00
* @ param string $field
* @ param boolean $short if true , 'user_' prefix will be added to field name
2010-05-28 13:24:25 +00:00
* @ param boolean $raw doesn ' t retrieve db value when true ( no sql query )
2010-04-28 15:44:46 +00:00
* @ return mixed
*/
2010-05-14 18:45:51 +00:00
public function getValue ( $field , $short = true , $raw = false )
2010-04-28 15:44:46 +00:00
{
2010-05-13 15:47:31 +00:00
if ( $short ) $field = 'user_' . $field ;
2010-05-05 15:05:32 +00:00
if ( ! $this -> checkRead ( $field ))
return null ;
2010-05-14 18:45:51 +00:00
if ( ! $raw && vartrue ( $this -> _struct_index [ $field ][ 'db' ]))
{
return $this -> getDbValue ( $field );
}
2010-05-02 18:41:20 +00:00
return $this -> get ( $field , $this -> getDefault ( $field ));
2010-04-28 15:44:46 +00:00
}
/**
2010-05-28 13:24:25 +00:00
* Set User extended field value , only if current editor has write permissions and field
* is applicable for the current user .
2010-05-02 18:41:20 +00:00
* Note : Data is not sanitized !
2010-04-28 15:44:46 +00:00
* @ param string $field
* @ param mixed $value
2010-05-13 15:47:31 +00:00
* @ param boolean $short if true , 'user_' prefix will be added to field name
2010-05-02 18:41:20 +00:00
* @ return e_user_extended_model
2010-04-28 15:44:46 +00:00
*/
2010-05-13 15:47:31 +00:00
public function setValue ( $field , $value , $short = true )
2010-04-28 15:44:46 +00:00
{
2010-05-13 15:47:31 +00:00
if ( $short ) $field = 'user_' . $field ;
2010-05-05 15:05:32 +00:00
if ( ! $this -> checkWrite ( $field ))
return $this ;
2010-05-28 13:24:25 +00:00
2010-05-02 18:41:20 +00:00
$this -> set ( $field , $value , true );
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-05-28 13:24:25 +00:00
/**
* Retrieve value of a field of type 'db' . It does sql request only once .
*
* @ param string $field field name
* @ return mixed db value
*/
2010-05-14 18:45:51 +00:00
protected function getDbValue ( $field )
{
if ( null !== $this -> _struct_index [ $field ][ 'db_value' ])
{
return $this -> _struct_index [ $field ][ 'db_value' ];
}
// retrieve db data
$value = $this -> get ( $field );
list ( $table , $field_id , $field_name , $field_order ) = explode ( ',' , $this -> _struct_index [ $field ][ 'db' ], 4 );
$this -> _struct_index [ $field ][ 'db_value' ] = $value ;
if ( $value && $table && $field_id && $field_name && e107 :: getDb () -> db_Select ( $table , $field_name , " { $field_id } =' { $value } ' " ))
{
$res = e107 :: getDb () -> db_Fetch ();
$this -> _struct_index [ $field ][ 'db_value' ] = $res [ $field_name ];
}
return $this -> _struct_index [ $field ][ 'db_value' ];
}
2010-05-28 13:24:25 +00:00
/**
* System getter . It doesn ' t perform any read / applicable permission checks
* against current editor / user .
* It ' s here to serve in your application logic .
*
* @ param string $field
* @ param boolean $short if true , 'user_' prefix will be added to field name
* @ param boolean $raw don ' t retrieve db value
* @ return mixed
*/
public function getSystem ( $field , $short = true , $raw = true )
{
if ( $short ) $field = 'user_' . $field ;
if ( ! $raw && vartrue ( $this -> _struct_index [ $field ][ 'db' ]))
{
return $this -> getDbValue ( $field );
}
return $this -> get ( $field , $this -> getDefault ( $field ));
}
/**
* System setter . It doesn ' t perform any write / applicable permission checks
* against current editor / user .
* It ' s here to serve in your application logic .
* NOTE : untrusted data should be provided via setPosted () method !
*
* @ param string $field
* @ param mixed $value
* @ param boolean $short if true , 'user_' prefix will be added to field name
* @ param boolean $strict if false no Applicable check will be made
* @ return e_user_model
*/
public function setSystem ( $field , $value , $short = true , $strict = true )
{
if ( $short ) $field = 'user_' . $field ;
$this -> set ( $field , $value , $strict );
return $this ;
}
2010-05-13 15:47:31 +00:00
public function getReadData ()
{
2010-05-28 13:24:25 +00:00
// TODO array allowed user profile page data (read mode)
2010-05-13 15:47:31 +00:00
}
public function getWriteData ()
{
2010-05-28 13:24:25 +00:00
// TODO array allowed user settings page data (edit mode)
2010-05-13 15:47:31 +00:00
}
2010-05-02 18:41:20 +00:00
/**
* Get default field value , defined by extended field structure
* Returns NULL if field / default value not found
* @ param string $field
* @ return mixed
*/
public function getDefault ( $field )
{
return varset ( $this -> _struct_index [ $field ][ 'default' ], null );
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
* Check field read permissions against current editor
* @ param string $field
* @ return boolean
*/
public function checkRead ( $field )
{
2010-05-13 15:47:31 +00:00
$hidden = $this -> get ( 'user_hidden_fields' );
2010-05-28 13:24:25 +00:00
$editor = $this -> getEditor ();
2010-05-17 15:51:42 +00:00
if ( ! empty ( $hidden ) && $this -> getId () !== $editor -> getId () && strpos ( $hidden , '^' . $field . '^' ) !== false ) return false ;
2010-05-13 15:47:31 +00:00
2010-05-15 17:33:11 +00:00
return ( $this -> checkApplicable ( $field ) && $editor -> checkClass ( $this -> _memberlist_access ) && $editor -> checkClass ( varset ( $this -> _struct_index [ $field ][ 'read' ])));
2010-05-02 18:41:20 +00:00
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
2010-05-13 15:47:31 +00:00
* Check field write permissions against current editor
2010-05-02 18:41:20 +00:00
* @ param string $field
* @ return boolean
*/
public function checkWrite ( $field )
{
2010-05-13 15:47:31 +00:00
if ( ! $this -> checkApplicable ( $field )) return false ;
$editor = $this -> getEditor ();
// Main admin checked later in checkClass() method
if ( $editor -> checkAdminPerms ( '4' ) && varset ( $this -> _struct_index [ $field ][ 'write' ]) != e_UC_NOBODY )
return true ;
return $editor -> checkClass ( varset ( $this -> _struct_index [ $field ][ 'write' ]));
2010-05-02 18:41:20 +00:00
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
* Check field signup permissions
* @ param string $field
* @ return boolean
*/
public function checkSignup ( $field )
{
2010-05-13 15:47:31 +00:00
return $this -> getUser () -> checkClass ( varset ( $this -> _struct_index [ $field ][ 'signup' ]));
2010-05-02 18:41:20 +00:00
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
2010-05-13 15:47:31 +00:00
* Check field applicable permissions against current user
2010-05-02 18:41:20 +00:00
* @ param string $field
* @ return boolean
*/
public function checkApplicable ( $field )
{
2010-05-17 15:51:42 +00:00
return $this -> getUser () -> checkClass ( varset ( $this -> _struct_index [ $field ][ 'apply' ]));
2010-05-02 18:41:20 +00:00
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
* @ see e_model #load($id, $force)
* @ return e_user_extended_model
*/
2016-04-21 12:04:09 -07:00
public function load ( $id = null , $force = false )
2010-05-02 18:41:20 +00:00
{
2010-05-05 15:05:32 +00:00
if ( $this -> getId () && ! $force )
return $this ;
2010-05-28 13:24:25 +00:00
$this -> _loadDataAndAccess ();
2010-05-02 18:41:20 +00:00
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-05-28 13:24:25 +00:00
/**
* Check if given field name is present in extended user table structure
*
* @ param string $field
* @ param boolean $short
* @ return boolean
*/
public function isField ( $field , $short = true )
{
if ( $short ) $field = 'user_' . $field ;
return ( isset ( $this -> _struct_index [ $field ]) || in_array ( $field , array ( $this -> getFieldIdName (), 'user_hidden_fields' )));
}
2010-05-02 18:41:20 +00:00
/**
* Load extended fields permissions once ( performance )
* @ return e_user_extended_model
*/
2010-05-28 13:24:25 +00:00
protected function _loadDataAndAccess ()
2010-05-02 18:41:20 +00:00
{
$struct_tree = $this -> getExtendedStructure ();
2010-05-28 13:24:25 +00:00
$user = $this -> getUser ();
if ( $user && $struct_tree -> hasTree ())
2010-05-05 15:05:32 +00:00
{
2010-05-02 18:41:20 +00:00
// load structure dependencies
2010-05-28 13:24:25 +00:00
$ignore = array ( $this -> getFieldIdName (), 'user_hidden_fields' );
// set ignored values
foreach ( $ignore as $field_name )
{
$this -> set ( $field_name , $user -> get ( $field_name ));
}
2010-05-13 15:47:31 +00:00
2010-05-02 18:41:20 +00:00
$fields = $struct_tree -> getTree ();
2010-05-05 15:05:32 +00:00
foreach ( $fields as $id => $field )
2010-05-02 18:41:20 +00:00
{
2010-05-28 13:24:25 +00:00
$field_name = 'user_' . $field -> getValue ( 'name' );
$this -> set ( $field_name , $user -> get ( $field_name ));
2010-05-05 15:05:32 +00:00
if ( ! in_array ( $field -> getValue ( 'name' ), $ignore ))
2010-05-02 18:41:20 +00:00
{
2010-05-28 13:24:25 +00:00
$this -> _struct_index [ $field_name ] = array (
2010-05-14 18:45:51 +00:00
'db' => $field -> getValue ( 'type' ) == 4 ? $field -> getValue ( 'values' ) : '' ,
'db_value' => null , // used later for caching DB results
2010-05-05 15:05:32 +00:00
'read' => $field -> getValue ( 'read' ),
'write' => $field -> getValue ( 'write' ),
'signup' => $field -> getValue ( 'signup' ),
'apply' => $field -> getValue ( 'applicable' ),
'default' => $field -> getValue ( 'default' ),
2010-05-02 18:41:20 +00:00
);
}
}
}
2010-04-28 15:44:46 +00:00
return $this ;
}
2010-05-05 15:05:32 +00:00
/**
* Build manage rules for single field
* @ param $structure_model
* @ return e_user_extended_model
*/
protected function _buildManageField ( e_user_extended_structure_model $structure_model )
{
$ftype = $structure_model -> getValue ( 'type' ) == 6 ? 'integer' : 'string' ;
// 0- field control (html) attributes;1 - regex; 2 - validation error msg;
$parms = explode ( '^,^' , $structure_model -> getValue ( 'parms' ));
// validaton rules
$vtype = $parms [ 1 ] ? 'regex' : $ftype ;
2012-02-07 16:37:44 +00:00
$name = 'user_' . $structure_model -> getValue ( 'name' );
$this -> setValidationRule ( $name , array ( $vtype , $parms [ 1 ], $structure_model -> getValue ( 'text' ), $parms [ 2 ]), $structure_model -> getValue ( 'required' ));
2010-05-05 15:05:32 +00:00
// data type, required for sql query
2012-02-07 16:37:44 +00:00
$this -> _data_fields [ $name ] = $ftype ;
2010-05-05 15:05:32 +00:00
return $this ;
}
/**
* Build manage rules for single field
* @ param $structure_model
* @ return e_user_extended_model
*/
protected function _buildManageRules ()
{
$struct_tree = $this -> getExtendedStructure ();
if ( $this -> getId () && $struct_tree -> hasTree ())
{
// load structure dependencies TODO protected fields check as method
$ignore = array ( $this -> getFieldIdName (), 'user_hidden_fields' ); // TODO - user_hidden_fields? Old?
$fields = $struct_tree -> getTree ();
foreach ( $fields as $id => $field )
{
2012-02-07 16:37:44 +00:00
if ( ! in_array ( 'user_' . $field -> getValue ( 'name' ), $ignore ) && ! $field -> isCategory ())
2010-05-05 15:05:32 +00:00
{
// build _data_type and rules
$this -> _buildManageField ( $field );
}
}
}
return $this ;
}
2010-04-28 15:44:46 +00:00
/**
* Get extended structure tree
2010-05-02 18:41:20 +00:00
* @ return e_user_extended_structure_tree
2010-04-28 15:44:46 +00:00
*/
public function getExtendedStructure ()
{
2010-05-05 15:05:32 +00:00
if ( null === $this -> _structure )
$this -> _structure = e107 :: getUserStructure ();
2010-05-02 18:41:20 +00:00
return $this -> _structure ;
2010-04-28 15:44:46 +00:00
}
2010-05-05 15:05:32 +00:00
2010-05-13 15:47:31 +00:00
/**
* Additional security while applying posted
* data to user extended model
* @ return e_user_extended_model
*/
2016-02-14 19:00:12 -08:00
public function mergePostedData ( $strict = true , $sanitize = true , $validate = true )
2010-05-13 15:47:31 +00:00
{
$posted = $this -> getPostedData ();
foreach ( $posted as $key => $value )
{
if ( ! $this -> checkWrite ( $key ))
{
$this -> removePosted ( $key );
}
}
parent :: mergePostedData ( true , true , true );
return $this ;
}
2010-05-05 15:05:32 +00:00
/**
* Build data types and rules on the fly and save
2010-05-13 15:47:31 +00:00
* @ see e_front_model :: save ()
2010-05-05 15:05:32 +00:00
*/
2016-04-21 12:04:09 -07:00
public function save ( $from_post = true , $force = false , $session = false )
2010-05-05 15:05:32 +00:00
{
2012-02-07 16:37:44 +00:00
// when not loaded from db, see the construct check
if ( ! $this -> getId ())
{
$this -> setId ( $this -> getUser () -> getId ());
}
2010-05-05 15:05:32 +00:00
$this -> _buildManageRules ();
2012-02-07 16:37:44 +00:00
// insert new record
if ( ! e107 :: getDb () -> db_Count ( 'user_extended' , '(user_extended_id)' , " user_extended_id= " . $this -> getId ()))
{
return $this -> insert ( true , $session );
}
2010-05-05 15:05:32 +00:00
return parent :: save ( true , $force , $session );
}
2010-05-13 15:47:31 +00:00
/**
* Doesn ' t save anything actually ...
*/
2010-05-05 15:05:32 +00:00
public function saveDebug ( $retrun = false , $undo = true )
{
$this -> _buildManageRules ();
2010-05-13 15:47:31 +00:00
return parent :: saveDebug ( $return , $undo );
2010-05-05 15:05:32 +00:00
}
2010-04-28 15:44:46 +00:00
}
class e_user_extended_structure_model extends e_model
{
/**
* @ see e_model
* @ var string
*/
protected $_db_table = 'user_extended_struct' ;
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
/**
* @ see e_model
* @ var string
*/
protected $_field_id = 'user_extended_struct_id' ;
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
/**
* @ see e_model
* @ var string
*/
2010-05-02 18:41:20 +00:00
protected $_message_stack = 'user_struct' ;
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
/**
* Get User extended structure field value
*
* @ param string $field
* @ param string $default
* @ return mixed
*/
public function getValue ( $field , $default = '' )
{
2010-05-02 18:41:20 +00:00
$field = 'user_extended_struct_' . $field ;
2010-04-28 15:44:46 +00:00
return $this -> get ( $field , $default );
}
/**
* Set User extended structure field value
2010-05-05 15:05:32 +00:00
*
2010-04-28 15:44:46 +00:00
* @ param string $field
* @ param mixed $value
* @ return e_user_model
*/
public function setValue ( $field , $value )
{
2010-05-02 18:41:20 +00:00
$field = 'user_extended_struct_' . $field ;
2010-04-28 15:44:46 +00:00
$this -> set ( $field , $value , false );
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-05-14 18:45:51 +00:00
public function isCategory ()
{
return ( $this -> getValue ( 'type' ) ? false : true );
}
public function getCategoryId ()
{
return $this -> getValue ( 'parent' );
}
public function getLabel ()
{
$label = $this -> isCategory () ? $this -> getValue ( 'name' ) : $this -> getValue ( 'text' );
return defset ( $label , $label );
}
2010-04-28 15:44:46 +00:00
/**
* Loading of single structure row not allowed for front model
*/
2016-02-14 19:00:12 -08:00
public function load ( $id = null , $force = false )
2010-04-28 15:44:46 +00:00
{
return $this ;
}
}
2010-03-22 15:45:47 +00:00
2010-05-02 18:41:20 +00:00
class e_user_extended_structure_tree extends e_tree_model
2010-04-28 15:44:46 +00:00
{
/**
* @ see e_model
* @ var string
*/
protected $_db_table = 'user_extended_struct' ;
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
/**
* @ see e_model
* @ var string
*/
protected $_field_id = 'user_extended_struct_id' ;
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
/**
* @ see e_model
* @ var string
*/
protected $_message_stack = 'user' ;
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
/**
* @ var string
*/
2010-05-02 18:41:20 +00:00
protected $_cache_string = 'nomd5_user_extended_struct' ;
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
/**
2010-05-02 18:41:20 +00:00
* Force system cache ( cache used even if disabled by site admin )
2010-04-28 15:44:46 +00:00
* @ var boolen
*/
protected $_cache_force = true ;
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
2010-05-14 18:45:51 +00:00
* Index for speed up retrieving by name routine
* @ var array
*/
protected $_name_index = array ();
/**
* Category Index - numerical array of id ' s
* @ var array
*/
protected $_category_index = array ();
/**
* Items by category list
* @ var array
2010-05-02 18:41:20 +00:00
*/
2010-05-14 18:45:51 +00:00
protected $_parent_index = array ();
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
* Constructor - auto - load
* @ return void
*/
public function __construct ()
{
$this -> load ();
}
2010-05-05 15:05:32 +00:00
2010-05-14 18:45:51 +00:00
/**
* @ param string $name name field value
* @ return e_user_extended_structure_model
*/
2010-05-02 18:41:20 +00:00
public function getNodeByName ( $name )
{
2010-05-05 15:05:32 +00:00
if ( $this -> isNodeName ( $name ))
2010-05-02 18:41:20 +00:00
{
return $this -> getNode ( $this -> getNodeId ( $name ));
}
return null ;
}
2010-05-05 15:05:32 +00:00
2010-05-14 18:45:51 +00:00
/**
* Check if node exists by its name field value
* @ param string $name
* @ return boolean
*/
2010-05-02 18:41:20 +00:00
public function isNodeName ( $name )
{
return ( isset ( $this -> _name_index [ $name ]) && $this -> isNode ( $this -> _name_index [ $name ]));
}
2010-05-05 15:05:32 +00:00
2010-05-14 18:45:51 +00:00
/**
* Get node ID by node name field
* @ param string $name
* @ return integer
*/
2010-05-02 18:41:20 +00:00
public function getNodeId ( $name )
{
2010-05-14 18:45:51 +00:00
return ( isset ( $this -> _name_index [ $name ]) ? $this -> _name_index [ $name ] : null );
}
/**
* Get collection of nodes of type category
* @ return array
*/
public function getCategoryTree ()
{
return $this -> _array_intersect_key ( $this -> getTree (), array_combine ( $this -> _category_index , $this -> _category_index ));
}
2010-05-17 15:51:42 +00:00
/**
* Get collection of nodes of type field
* @ return array
*/
public function getFieldTree ()
{
return array_diff_key ( $this -> getTree (), array_combine ( $this -> _category_index , $this -> _category_index ));
}
2010-05-14 18:45:51 +00:00
/**
* Get collection of nodes assigned to a specific category
* @ param integer $category_id
* @ return array
*/
public function getTreeByCategory ( $category_id )
{
if ( ! isset ( $this -> _parent_index [ $category_id ]) || empty ( $this -> _parent_index [ $category_id ])) return array ();
return $this -> _array_intersect_key ( $this -> getTree (), array_combine ( $this -> _parent_index [ $category_id ], $this -> _parent_index [ $category_id ]));
2010-05-02 18:41:20 +00:00
}
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
/**
* Load tree data
2010-05-05 15:05:32 +00:00
*
2010-05-02 18:41:20 +00:00
* @ param boolean $force
2010-04-28 15:44:46 +00:00
*/
public function load ( $force = false )
{
$this -> setParam ( 'nocount' , true )
2010-05-14 18:45:51 +00:00
-> setParam ( 'model_class' , 'e_user_extended_structure_model' )
-> setParam ( 'db_order' , 'user_extended_struct_order ASC' );
2010-04-28 15:44:46 +00:00
parent :: load ( $force );
2010-05-17 15:51:42 +00:00
2010-04-28 15:44:46 +00:00
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-05-02 18:41:20 +00:00
/**
2010-05-14 18:45:51 +00:00
* Build all indexes on load
* ( New ) This method is auto - triggered by core load () method
2010-05-02 18:41:20 +00:00
* @ param e_user_extended_structure_model $model
*/
protected function _onLoad ( $model )
{
2010-05-14 18:45:51 +00:00
if ( $model -> isCategory ())
{
$this -> _category_index [] = $model -> getId ();
}
else
{
$this -> _name_index [ 'user_' . $model -> getValue ( 'name' )] = $model -> getId ();
$this -> _parent_index [ $model -> getCategoryId ()][] = $model -> getId ();
}
2010-05-02 18:41:20 +00:00
return $this ;
}
2010-05-14 18:45:51 +00:00
/**
* Compatibility - array_intersect_key () available since PHP 5.1
*
* @ see http :// php . net / manual / en / function . array - intersect - key . php
* @ param array $array1
* @ param array $array2
* @ return array
*/
protected function _array_intersect_key ( $array1 , $array2 )
{
if ( function_exists ( 'array_intersect_key' )) return array_intersect_key ( $array1 , $array2 );
$ret = array ();
foreach ( $array1 as $k => $v )
{
if ( isset ( $array2 [ $k ])) $ret [ $k ] = $v ;
}
return $ret ;
}
2010-05-05 15:05:32 +00:00
}
2010-05-13 15:47:31 +00:00
2010-05-14 18:45:51 +00:00
class e_user_pref extends e_front_model
2010-05-13 15:47:31 +00:00
{
/**
* @ var e_user_model
*/
protected $_user ;
/**
* Constructor
* @ param e_user_model $user_model
* @ return void
*/
public function __construct ( e_user_model $user_model )
{
$this -> _user = $user_model ;
$this -> load ();
}
/**
* Load data from user preferences string
* @ param boolean $force
* @ return e_user_pref
*/
2016-04-21 12:04:09 -07:00
public function load ( $id = null , $force = false )
2010-05-13 15:47:31 +00:00
{
if ( $force || ! $this -> hasData ())
{
$data = $this -> _user -> get ( 'user_prefs' , '' );
if ( ! empty ( $data ))
{
2010-05-14 18:45:51 +00:00
// BC
2015-02-14 23:34:15 -08:00
$data = substr ( $data , 0 , 5 ) == " array " ? e107 :: unserialize ( $data ) : unserialize ( $data );
2010-05-13 15:47:31 +00:00
if ( ! $data ) $data = array ();
}
else $data = array ();
$this -> setData ( $data );
}
return $this ;
}
/**
* Apply current data to user data
* @ return e_user_pref
*/
public function apply ()
{
2012-02-07 16:37:44 +00:00
$data = $this -> hasData () ? $this -> toString ( true ) : '' ;
$this -> _user -> set ( 'user_prefs' , $data );
2010-05-13 15:47:31 +00:00
return $this ;
}
/**
* Save and apply user preferences
2010-05-15 17:33:11 +00:00
* @ param boolean $from_post
* @ param boolean $force
2010-05-13 15:47:31 +00:00
* @ return boolean success
*/
2016-02-14 19:00:12 -08:00
public function save ( $from_post = false , $force = false , $session_messages = false )
2010-05-13 15:47:31 +00:00
{
if ( $this -> _user -> getId ())
{
2010-05-14 18:45:51 +00:00
if ( $from_post )
{
$this -> mergePostedData ( false , true , false );
}
2010-05-15 17:33:11 +00:00
if ( $force || $this -> dataHasChanged ())
{
$data = $this -> toString ( true );
$this -> apply ();
return ( e107 :: getDb ( 'user_prefs' ) -> db_Update ( 'user' , " user_prefs=' { $data } ' WHERE user_id= " . $this -> _user -> getId ()) ? true : false );
}
return 0 ;
2010-05-13 15:47:31 +00:00
}
return false ;
}
/**
2016-04-21 12:04:09 -07:00
* Remove & apply user preferences , optionally - save to DB
2010-05-13 15:47:31 +00:00
* @ return boolean success
*/
2016-04-21 12:04:09 -07:00
public function delete ( $ids , $destroy = true , $session_messages = false ) // replaced $save = false for PHP7 fix.
2010-05-13 15:47:31 +00:00
{
$this -> removeData () -> apply ();
2016-04-21 12:04:09 -07:00
// if($save) return $this->save(); //FIXME adjust within the context of the variables in the method.
2010-05-13 15:47:31 +00:00
return true ;
}
}