2007-04-16 11:58:25 +00:00
< ? php
2010-01-05 10:11:53 +00:00
/*
* Name : User Management
* Author : Shish
* Description : Allows people to sign up to the website
*/
2007-05-08 21:01:51 +00:00
class UserBlockBuildingEvent extends Event {
var $parts = array ();
public function add_link ( $name , $link , $position = 50 ) {
while ( isset ( $this -> parts [ $position ])) $position ++ ;
2009-08-03 09:46:40 +00:00
$this -> parts [ $position ] = array ( " name " => $name , " link " => $link );
2007-05-08 21:01:51 +00:00
}
}
2007-07-16 19:38:12 +00:00
class UserPageBuildingEvent extends Event {
2009-01-04 18:31:54 +00:00
var $display_user ;
2009-08-18 21:30:52 +00:00
var $stats = array ();
2007-07-16 19:38:12 +00:00
2009-05-11 14:04:33 +00:00
public function __construct ( User $display_user ) {
2009-01-04 18:31:54 +00:00
$this -> display_user = $display_user ;
2007-07-16 19:38:12 +00:00
}
2009-08-18 21:30:52 +00:00
public function add_stats ( $html , $position = 50 ) {
while ( isset ( $this -> stats [ $position ])) $position ++ ;
$this -> stats [ $position ] = $html ;
}
2007-07-16 19:38:12 +00:00
}
2007-11-04 03:51:41 +00:00
class UserCreationEvent extends Event {
var $username ;
var $password ;
var $email ;
2009-05-11 14:04:33 +00:00
public function __construct ( $name , $pass , $email ) {
2007-11-04 03:51:41 +00:00
$this -> username = $name ;
$this -> password = $pass ;
$this -> email = $email ;
}
}
2009-01-04 14:01:59 +00:00
class UserCreationException extends SCoreException {}
2012-02-08 12:07:01 +00:00
class UserPage extends Extension {
2012-02-02 08:07:57 +00:00
public function onInitExt ( InitExtEvent $event ) {
2009-05-30 13:47:57 +00:00
global $config ;
$config -> set_default_bool ( " login_signup_enabled " , true );
$config -> set_default_int ( " login_memory " , 365 );
2010-04-21 16:56:01 +00:00
$config -> set_default_string ( " avatar_host " , " none " );
$config -> set_default_int ( " avatar_gravatar_size " , 80 );
$config -> set_default_string ( " avatar_gravatar_default " , " " );
$config -> set_default_string ( " avatar_gravatar_rating " , " g " );
2010-01-05 19:06:04 +00:00
$config -> set_default_bool ( " login_tac_bbcode " , true );
2009-05-30 13:47:57 +00:00
}
2009-05-11 14:04:33 +00:00
2012-02-02 08:07:57 +00:00
public function onPageRequest ( PageRequestEvent $event ) {
2009-05-30 13:47:57 +00:00
global $config , $database , $page , $user ;
2009-01-04 19:18:37 +00:00
2009-05-30 13:47:57 +00:00
// user info is shown on all pages
if ( $user -> is_anonymous ()) {
$this -> theme -> display_login_block ( $page );
}
else {
$ubbe = new UserBlockBuildingEvent ();
send_event ( $ubbe );
ksort ( $ubbe -> parts );
$this -> theme -> display_user_block ( $page , $user , $ubbe -> parts );
2007-07-16 13:15:56 +00:00
}
2009-05-30 13:47:57 +00:00
if ( $event -> page_matches ( " user_admin " )) {
2007-04-16 11:58:25 +00:00
if ( $event -> get_arg ( 0 ) == " login " ) {
if ( isset ( $_POST [ 'user' ]) && isset ( $_POST [ 'pass' ])) {
2009-01-04 19:18:37 +00:00
$this -> login ( $page );
2007-04-16 11:58:25 +00:00
}
else {
2009-01-04 19:18:37 +00:00
$this -> theme -> display_login_page ( $page );
2007-04-16 11:58:25 +00:00
}
}
2009-07-11 12:38:48 +00:00
else if ( $event -> get_arg ( 0 ) == " recover " ) {
$user = User :: by_name ( $_POST [ 'username' ]);
if ( is_null ( $user )) {
2012-02-16 15:37:05 +00:00
$this -> theme -> display_error ( 404 , " Error " , " There's no user with that name " );
2009-07-11 12:38:48 +00:00
}
if ( is_null ( $user -> email )) {
//
}
}
2007-04-16 11:58:25 +00:00
else if ( $event -> get_arg ( 0 ) == " create " ) {
2007-11-04 03:51:41 +00:00
if ( ! $config -> get_bool ( " login_signup_enabled " )) {
$this -> theme -> display_signups_disabled ( $page );
}
else if ( ! isset ( $_POST [ 'name' ])) {
2009-01-04 19:18:37 +00:00
$this -> theme -> display_signup_page ( $page );
2007-11-04 03:51:41 +00:00
}
else if ( $_POST [ 'pass1' ] != $_POST [ 'pass2' ]) {
2012-02-16 15:37:05 +00:00
$this -> theme -> display_error ( 400 , " Password Mismatch " , " Passwords don't match " );
2007-11-04 03:51:41 +00:00
}
else {
2009-01-04 14:01:59 +00:00
try {
2010-01-05 17:57:10 +00:00
if ( ! captcha_check ()) {
throw new UserCreationException ( " Error in captcha " );
2009-11-10 03:21:02 +00:00
}
2009-05-11 14:04:33 +00:00
$uce = new UserCreationEvent ( $_POST [ 'name' ], $_POST [ 'pass1' ], $_POST [ 'email' ]);
2009-01-04 14:01:59 +00:00
send_event ( $uce );
2007-11-04 03:51:41 +00:00
$this -> set_login_cookie ( $uce -> username , $uce -> password );
2009-01-04 19:18:37 +00:00
$page -> set_mode ( " redirect " );
$page -> set_redirect ( make_link ( " user " ));
2007-11-04 03:51:41 +00:00
}
2009-01-04 14:01:59 +00:00
catch ( UserCreationException $ex ) {
2012-02-16 15:37:05 +00:00
$this -> theme -> display_error ( 400 , " User Creation Error " , $ex -> getMessage ());
2009-01-04 14:01:59 +00:00
}
2007-11-04 03:51:41 +00:00
}
2007-04-16 11:58:25 +00:00
}
2009-05-30 13:47:57 +00:00
else if ( $event -> get_arg ( 0 ) == " list " ) {
// select users.id,name,joindate,admin,
// (select count(*) from images where images.owner_id=users.id) as images,
// (select count(*) from comments where comments.owner_id=users.id) as comments from users;
// select users.id,name,joindate,admin,image_count,comment_count
// from users
// join (select owner_id,count(*) as image_count from images group by owner_id) as _images on _images.owner_id=users.id
// join (select owner_id,count(*) as comment_count from comments group by owner_id) as _comments on _comments.owner_id=users.id;
$this -> theme -> display_user_list ( $page , User :: by_list ( 0 ), $user );
}
2012-03-12 18:04:06 +00:00
else if ( $event -> get_arg ( 0 ) == " logout " ) {
2012-03-12 17:44:07 +00:00
set_prefixed_cookie ( " session " , " " , time () + 60 * 60 * 24 * $config -> get_int ( 'login_memory' ), " / " );
if ( CACHE_HTTP || SPEED_HAX ) {
# to keep as few versions of content as possible,
# make cookies all-or-nothing
set_prefixed_cookie ( " user " , " " , time () + 60 * 60 * 24 * $config -> get_int ( 'login_memory' ), " / " );
}
log_info ( " user " , " Logged out " );
$page -> set_mode ( " redirect " );
$page -> set_redirect ( make_link ());
}
2012-03-12 18:04:06 +00:00
if ( ! $user -> check_auth_token ()) {
return ;
}
2012-03-12 17:44:07 +00:00
else if ( $event -> get_arg ( 0 ) == " change_pass " ) {
if ( isset ( $_POST [ 'id' ]) && isset ( $_POST [ 'pass1' ]) && isset ( $_POST [ 'pass2' ])) {
$duser = User :: by_id ( $_POST [ 'id' ]);
$pass1 = $_POST [ 'pass1' ];
$pass2 = $_POST [ 'pass2' ];
$this -> change_password_wrapper ( $duser , $pass1 , $pass2 );
}
}
else if ( $event -> get_arg ( 0 ) == " change_email " ) {
if ( isset ( $_POST [ 'id' ]) && isset ( $_POST [ 'address' ])) {
$duser = User :: by_id ( $_POST [ 'id' ]);
$address = $_POST [ 'address' ];
$this -> change_email_wrapper ( $duser , $address );
}
}
else if ( $event -> get_arg ( 0 ) == " change_class " ) {
global $_user_classes ;
if ( isset ( $_POST [ 'id' ]) && isset ( $_POST [ 'class' ])) {
$duser = User :: by_id ( $_POST [ 'id' ]);
$class = $_POST [ 'class' ];
if ( ! array_key_exists ( $class , $_user_classes )) {
throw Exception ( " Invalid user class: " . html_escape ( $class ));
}
$this -> change_class_wrapper ( $duser , $class );
}
2012-01-19 17:23:43 +00:00
}
2012-03-12 17:44:07 +00:00
else if ( $event -> get_arg ( 0 ) == " delete_user " ) {
$this -> delete_user ( $page , isset ( $_POST [ " with_images " ]), isset ( $_POST [ " with_comments " ]));
2012-01-22 17:55:52 +00:00
}
2007-07-27 13:05:48 +00:00
}
2009-05-30 13:47:57 +00:00
2012-02-02 08:07:57 +00:00
if ( $event -> page_matches ( " user " )) {
2009-05-11 14:04:33 +00:00
$display_user = ( $event -> count_args () == 0 ) ? $user : User :: by_name ( $event -> get_arg ( 0 ));
2009-07-20 05:42:09 +00:00
if ( $event -> count_args () == 0 && $user -> is_anonymous ()) {
2012-02-16 15:37:05 +00:00
$this -> theme -> display_error ( 401 , " Not Logged In " ,
2009-07-20 05:42:09 +00:00
" You aren't logged in. First do that, then you can see your stats. " );
}
2012-02-05 04:25:50 +00:00
else if ( ! is_null ( $display_user ) && ( $display_user -> id != $config -> get_int ( " anon_id " ))) {
2009-05-11 14:04:33 +00:00
send_event ( new UserPageBuildingEvent ( $display_user ));
2007-07-27 13:05:48 +00:00
}
else {
2012-02-16 15:37:05 +00:00
$this -> theme -> display_error ( 404 , " No Such User " ,
2007-07-27 13:05:48 +00:00
" If you typed the ID by hand, try again; if you came from a link on this " .
" site, it might be bug report time... " );
2007-04-16 11:58:25 +00:00
}
}
2009-05-30 13:47:57 +00:00
}
2009-01-04 19:18:37 +00:00
2012-02-02 08:07:57 +00:00
public function onUserPageBuilding ( UserPageBuildingEvent $event ) {
2009-06-06 19:04:33 +00:00
global $page , $user , $config ;
2009-08-18 21:30:52 +00:00
2011-12-31 14:12:05 +00:00
$h_join_date = autodate ( $event -> display_user -> join_date );
$event -> add_stats ( " Joined: $h_join_date " , 10 );
2009-08-18 21:30:52 +00:00
2009-10-08 11:32:01 +00:00
$av = $event -> display_user -> get_avatar_html ();
2012-03-31 11:38:39 +00:00
if ( $av ) {
$event -> add_stats ( $av , 0 );
}
else if ((
$config -> get_string ( " avatar_host " ) == " gravatar " ) &&
( $user -> id == $event -> display_user -> id )
) {
$event -> add_stats ( " No avatar? This gallery uses <a href='http://gravatar.com'>Gravatar</a> for avatar hosting, use the same email address here and there to have your avatar synced " , 0 );
}
2009-08-18 21:30:52 +00:00
ksort ( $event -> stats );
$this -> theme -> display_user_page ( $event -> display_user , $event -> stats );
2009-05-30 13:47:57 +00:00
if ( $user -> id == $event -> display_user -> id ) {
$ubbe = new UserBlockBuildingEvent ();
send_event ( $ubbe );
ksort ( $ubbe -> parts );
$this -> theme -> display_user_links ( $page , $user , $ubbe -> parts );
2007-07-16 19:38:12 +00:00
}
2009-08-18 21:30:52 +00:00
if (
2012-02-07 15:15:18 +00:00
( $user -> can ( " view_ip " ) || ( $user -> is_logged_in () && $user -> id == $event -> display_user -> id )) && # admin or self-user
2012-01-27 10:17:44 +00:00
( $event -> display_user -> id != $config -> get_int ( 'anon_id' )) # don't show anon's IP list, it is le huge
2009-08-18 21:30:52 +00:00
) {
$this -> theme -> display_ip_list (
$page ,
$this -> count_upload_ips ( $event -> display_user ),
$this -> count_comment_ips ( $event -> display_user ));
2007-04-16 11:58:25 +00:00
}
2009-05-30 13:47:57 +00:00
}
2009-01-04 19:18:37 +00:00
2012-02-02 08:07:57 +00:00
public function onSetupBuilding ( SetupBuildingEvent $event ) {
2010-04-21 16:56:01 +00:00
global $config ;
2010-04-21 16:32:32 +00:00
$hosts = array (
" None " => " none " ,
" Gravatar " => " gravatar "
);
2009-05-30 13:47:57 +00:00
$sb = new SetupBlock ( " User Options " );
$sb -> add_bool_option ( " login_signup_enabled " , " Allow new signups: " );
$sb -> add_longtext_option ( " login_tac " , " <br>Terms & Conditions:<br> " );
2010-04-21 16:56:01 +00:00
$sb -> add_choice_option ( " avatar_host " , $hosts , " <br>Avatars: " );
if ( $config -> get_string ( " avatar_host " ) == " gravatar " ) {
$sb -> add_label ( " <br> <br><b>Gravatar Options</b> " );
$sb -> add_choice_option ( " avatar_gravatar_type " ,
array (
'Default' => 'default' ,
'Wavatar' => 'wavatar' ,
'Monster ID' => 'monsterid' ,
'Identicon' => 'identicon'
),
" <br>Type: " );
$sb -> add_choice_option ( " avatar_gravatar_rating " ,
array ( 'G' => 'g' , 'PG' => 'pg' , 'R' => 'r' , 'X' => 'x' ),
" <br>Rating: " );
}
2009-05-30 13:47:57 +00:00
$event -> panel -> add_block ( $sb );
}
2007-05-08 21:01:51 +00:00
2012-02-02 08:07:57 +00:00
public function onUserBlockBuilding ( UserBlockBuildingEvent $event ) {
2009-05-30 13:47:57 +00:00
$event -> add_link ( " My Profile " , make_link ( " user " ));
$event -> add_link ( " Log Out " , make_link ( " user_admin/logout " ), 99 );
}
2007-11-04 03:51:41 +00:00
2012-02-02 08:07:57 +00:00
public function onUserCreation ( UserCreationEvent $event ) {
2009-05-30 13:47:57 +00:00
$this -> check_user_creation ( $event );
$this -> create_user ( $event );
}
2008-02-16 03:14:38 +00:00
2012-02-02 08:07:57 +00:00
public function onSearchTermParse ( SearchTermParseEvent $event ) {
2011-12-25 12:31:28 +00:00
global $user ;
2009-05-30 13:47:57 +00:00
$matches = array ();
if ( preg_match ( " /^(poster|user)=(.*) $ /i " , $event -> term , $matches )) {
2012-03-05 09:55:11 +00:00
$duser = User :: by_name ( $matches [ 2 ]);
if ( ! is_null ( $duser )) {
$user_id = $duser -> id ;
2008-02-16 03:14:38 +00:00
}
2009-05-30 13:47:57 +00:00
else {
$user_id = - 1 ;
2008-02-16 03:14:38 +00:00
}
2009-05-30 13:47:57 +00:00
$event -> add_querylet ( new Querylet ( " images.owner_id = $user_id " ));
}
else if ( preg_match ( " /^(poster|user)_id=([0-9]+) $ /i " , $event -> term , $matches )) {
$user_id = int_escape ( $matches [ 2 ]);
$event -> add_querylet ( new Querylet ( " images.owner_id = $user_id " ));
2008-02-16 03:14:38 +00:00
}
2012-02-07 15:15:18 +00:00
else if ( $user -> can ( " view_ip " ) && preg_match ( " /^(poster|user)_ip=([0-9 \ .]+) $ /i " , $event -> term , $matches )) {
2011-12-25 12:38:07 +00:00
$user_ip = $matches [ 2 ]; // FIXME: ip_escape?
2011-12-25 12:31:28 +00:00
$event -> add_querylet ( new Querylet ( " images.owner_ip = ' $user_ip ' " ));
}
2007-04-16 11:58:25 +00:00
}
// }}}
// Things done *with* the user {{{
2012-02-02 14:14:33 +00:00
private function login ( Page $page ) {
2007-04-16 11:58:25 +00:00
global $user ;
$name = $_POST [ 'user' ];
$pass = $_POST [ 'pass' ];
2007-11-04 03:51:41 +00:00
$hash = md5 ( strtolower ( $name ) . $pass );
2007-04-16 11:58:25 +00:00
2012-03-08 04:35:52 +00:00
if ( empty ( $name ) || empty ( $pass )) {
$this -> theme -> display_error ( 400 , " Error " , " Username or password left blank " );
return ;
}
2009-05-11 14:04:33 +00:00
$duser = User :: by_name_and_hash ( $name , $hash );
2007-04-16 11:58:25 +00:00
if ( ! is_null ( $duser )) {
$user = $duser ;
2007-11-04 03:51:41 +00:00
$this -> set_login_cookie ( $name , $pass );
2010-01-05 10:52:23 +00:00
if ( $user -> is_admin ()) {
log_warning ( " user " , " Admin logged in " );
}
else {
log_info ( " user " , " User logged in " );
}
2007-04-16 11:58:25 +00:00
$page -> set_mode ( " redirect " );
$page -> set_redirect ( make_link ( " user " ));
}
else {
2011-01-22 16:10:07 +00:00
log_warning ( " user " , " Failed to log in as " . html_escape ( $name ) . " [ $hash ] " );
2012-02-16 15:37:05 +00:00
$this -> theme -> display_error ( 401 , " Error " , " No user with those details was found " );
2007-04-16 11:58:25 +00:00
}
}
2012-02-02 14:14:33 +00:00
private function check_user_creation ( $event ) { // FIXME type
2007-11-04 03:51:41 +00:00
$name = $event -> username ;
$pass = $event -> password ;
$email = $event -> email ;
2007-04-16 11:58:25 +00:00
global $database ;
2007-11-04 03:51:41 +00:00
if ( strlen ( $name ) < 1 ) {
2009-01-04 14:01:59 +00:00
throw new UserCreationException ( " Username must be at least 1 character " );
2007-11-04 03:51:41 +00:00
}
2008-11-19 22:55:04 +00:00
else if ( ! preg_match ( '/^[a-zA-Z0-9-_]+$/' , $name )) {
2009-01-04 14:01:59 +00:00
throw new UserCreationException (
" Username contains invalid characters. Allowed characters are " .
" letters, numbers, dash, and underscore " );
2007-11-04 03:51:41 +00:00
}
2011-02-21 11:57:50 +00:00
else if ( $database -> get_row ( " SELECT * FROM users WHERE name = :name " , array ( " name " => $name ))) {
2009-01-04 14:01:59 +00:00
throw new UserCreationException ( " That username is already taken " );
2007-04-16 11:58:25 +00:00
}
2007-11-04 03:51:41 +00:00
}
2007-04-16 11:58:25 +00:00
2012-02-02 14:14:33 +00:00
private function create_user ( $event ) { // FIXME type
2007-11-04 03:51:41 +00:00
global $database ;
2007-04-16 11:58:25 +00:00
2007-11-04 03:51:41 +00:00
$hash = md5 ( strtolower ( $event -> username ) . $event -> password );
$email = ( ! empty ( $event -> email )) ? $event -> email : null ;
2009-01-22 09:39:44 +00:00
// if there are currently no admins, the new user should be one
2012-02-21 18:37:36 +00:00
$need_admin = ( $database -> get_one ( " SELECT COUNT(*) FROM users WHERE class='admin' " ) == 0 );
2012-02-22 13:58:55 +00:00
$class = $need_admin ? 'admin' : 'user' ;
2009-01-22 09:39:44 +00:00
2007-11-04 03:51:41 +00:00
$database -> Execute (
2012-02-21 18:37:36 +00:00
" INSERT INTO users (name, pass, joindate, email, class) VALUES (:username, :hash, now(), :email, :class) " ,
array ( " username " => $event -> username , " hash " => $hash , " email " => $email , " class " => $class ));
2012-03-08 02:55:04 +00:00
$uid = $database -> get_last_insert_id ( 'users_id_seq' );
2009-05-08 10:52:29 +00:00
log_info ( " user " , " Created User # $uid ( { $event -> username } ) " );
2007-11-04 03:51:41 +00:00
}
2009-01-04 19:18:37 +00:00
2012-02-02 14:14:33 +00:00
private function set_login_cookie ( /*string*/ $name , /*string*/ $pass ) {
2007-11-04 03:51:41 +00:00
global $config ;
2009-01-04 16:17:41 +00:00
$addr = get_session_ip ( $config );
2007-11-04 03:51:41 +00:00
$hash = md5 ( strtolower ( $name ) . $pass );
2009-10-08 12:59:12 +00:00
set_prefixed_cookie ( " user " , $name ,
2007-11-04 03:51:41 +00:00
time () + 60 * 60 * 24 * 365 , '/' );
2009-10-08 12:59:12 +00:00
set_prefixed_cookie ( " session " , md5 ( $hash . $addr ),
2007-11-04 03:51:41 +00:00
time () + 60 * 60 * 24 * $config -> get_int ( 'login_memory' ), '/' );
2007-04-16 11:58:25 +00:00
}
2009-01-04 19:18:37 +00:00
//}}}
2007-06-30 01:19:11 +00:00
// Things done *to* the user {{{
2012-02-22 12:32:54 +00:00
private function user_can_edit_user ( User $a , User $b ) {
if ( $a -> is_anonymous ()) {
2012-02-16 15:37:05 +00:00
$this -> theme -> display_error ( 401 , " Error " , " You aren't logged in " );
2007-04-16 11:58:25 +00:00
}
2012-02-22 12:32:54 +00:00
if ( $a -> name == $b -> name ) {
return true ;
}
if ( $b -> can ( " protected " )) {
if ( $a -> class -> name == " admin " ) {
return true ;
2007-04-16 11:58:25 +00:00
}
2012-02-22 12:32:54 +00:00
else {
$this -> theme -> display_error ( 401 , " Error " , " You need to be an admin to change other people's details " );
}
}
else {
if ( $a -> can ( " edit_user_info " )) {
return true ;
2007-04-16 11:58:25 +00:00
}
else {
2012-02-22 12:32:54 +00:00
$this -> theme -> display_error ( 401 , " Error " , " You need to be an admin to change other people's details " );
2009-08-11 16:07:03 +00:00
}
}
2012-02-22 12:32:54 +00:00
return false ;
2009-08-11 16:07:03 +00:00
}
2012-02-22 12:32:54 +00:00
private function redirect_to_user ( User $duser ) {
global $page , $user ;
2009-08-11 16:07:03 +00:00
2012-02-22 12:32:54 +00:00
if ( $user -> id == $duser -> id ) {
$page -> set_mode ( " redirect " );
$page -> set_redirect ( make_link ( " user " ));
}
else {
$page -> set_mode ( " redirect " );
$page -> set_redirect ( make_link ( " user/ { $duser -> name } " ));
2009-08-11 16:07:03 +00:00
}
2012-02-22 12:32:54 +00:00
}
2009-08-11 16:07:03 +00:00
2012-02-22 12:32:54 +00:00
private function change_password_wrapper ( User $duser , $pass1 , $pass2 ) {
global $user ;
2009-08-11 16:07:03 +00:00
2012-02-22 12:32:54 +00:00
if ( $this -> user_can_edit_user ( $user , $duser )) {
if ( $pass1 != $pass2 ) {
$this -> theme -> display_error ( 400 , " Error " , " Passwords don't match " );
2009-08-11 16:07:03 +00:00
}
else {
2012-02-22 12:32:54 +00:00
// FIXME: send_event()
$duser -> set_password ( $pass1 );
2009-08-11 16:07:03 +00:00
if ( $id == $user -> id ) {
2012-02-22 12:32:54 +00:00
$this -> set_login_cookie ( $duser -> name , $pass1 );
2007-04-16 11:58:25 +00:00
}
2012-02-22 12:32:54 +00:00
$this -> redirect_to_user ( $duser );
2007-04-16 11:58:25 +00:00
}
}
}
2012-02-22 12:32:54 +00:00
private function change_email_wrapper ( User $duser , /*string(email)*/ $address ) {
global $user ;
2009-01-04 19:18:37 +00:00
2012-02-22 12:32:54 +00:00
if ( $this -> user_can_edit_user ( $user , $duser )) {
$duser -> set_email ( $address );
$this -> redirect_to_user ( $duser );
2007-04-16 11:58:25 +00:00
}
2012-02-22 12:32:54 +00:00
}
2009-01-04 19:18:37 +00:00
2012-02-22 12:32:54 +00:00
private function change_class_wrapper ( User $duser , /*string(class)*/ $class ) {
global $user ;
if ( $user -> class -> name == " admin " ) {
2009-05-11 14:04:33 +00:00
$duser = User :: by_id ( $_POST [ 'id' ]);
2012-02-14 20:38:19 +00:00
$duser -> set_class ( $class );
2012-02-22 12:32:54 +00:00
$this -> redirect_to_user ( $duser );
2007-04-16 11:58:25 +00:00
}
}
// }}}
2007-07-19 14:32:25 +00:00
// ips {{{
2012-02-02 14:14:33 +00:00
private function count_upload_ips ( User $duser ) {
2007-04-16 11:58:25 +00:00
global $database ;
2011-01-01 16:28:04 +00:00
$rows = $database -> get_pairs ( "
2007-07-27 02:31:15 +00:00
SELECT
owner_ip ,
COUNT ( images . id ) AS count ,
MAX ( posted ) AS most_recent
2007-06-03 10:40:00 +00:00
FROM images
2011-01-01 16:28:04 +00:00
WHERE owner_id =: id
2007-06-03 10:40:00 +00:00
GROUP BY owner_ip
2011-01-01 16:28:04 +00:00
ORDER BY most_recent DESC " , array( " id " => $duser->id ));
2007-07-19 14:32:25 +00:00
return $rows ;
}
2012-02-02 14:14:33 +00:00
private function count_comment_ips ( User $duser ) {
2007-07-19 14:32:25 +00:00
global $database ;
2011-01-01 16:28:04 +00:00
$rows = $database -> get_pairs ( "
2007-07-27 02:31:15 +00:00
SELECT
owner_ip ,
2007-07-27 03:41:09 +00:00
COUNT ( comments . id ) AS count ,
2007-07-27 02:31:15 +00:00
MAX ( posted ) AS most_recent
2007-06-03 10:40:00 +00:00
FROM comments
2011-01-01 16:28:04 +00:00
WHERE owner_id =: id
2007-06-03 10:40:00 +00:00
GROUP BY owner_ip
2011-01-01 16:28:04 +00:00
ORDER BY most_recent DESC " , array( " id " => $duser->id ));
2007-07-19 14:32:25 +00:00
return $rows ;
2007-04-16 11:58:25 +00:00
}
2012-01-19 17:23:43 +00:00
2012-03-12 17:44:07 +00:00
private function delete_user ( Page $page , /*boolean*/ $with_images = false , /*boolean*/ $with_comments = false ) {
2012-01-19 17:23:43 +00:00
global $user ;
global $config ;
global $database ;
$page -> set_title ( " Error " );
$page -> set_heading ( " Error " );
$page -> add_block ( new NavBlock ());
2012-02-07 15:15:18 +00:00
if ( ! $user -> can ( " delete_user " )) {
2012-01-19 17:23:43 +00:00
$page -> add_block ( new Block ( " Not Admin " , " Only admins can delete accounts " ));
}
else if ( ! isset ( $_POST [ 'id' ]) || ! is_numeric ( $_POST [ 'id' ])) {
$page -> add_block ( new Block ( " No ID Specified " ,
" You need to specify the account number to edit " ));
}
2012-03-12 17:44:07 +00:00
else {
if ( $with_images ) {
$rows = $database -> get_all ( " SELECT * FROM images WHERE owner_id = :owner_id " , array ( " owner_id " => $_POST [ 'id' ]));
foreach ( $rows as $key => $value ) {
$image = Image :: by_id ( $value [ 'id' ]);
if ( $image ) {
send_event ( new ImageDeletionEvent ( $image ));
}
}
}
else {
$database -> Execute (
" UPDATE images SET owner_id = :new_owner_id WHERE owner_id = :old_owner_id " ,
array ( " new_owner_id " => $config -> get_int ( 'anon_id' ), " old_owner_id " => $_POST [ 'id' ])
);
}
if ( $with_comments ) {
$database -> execute ( " DELETE FROM comments WHERE owner_id = :owner_id " , array ( " owner_id " => $_POST [ 'id' ]));
}
else {
$database -> Execute (
" UPDATE comments SET owner_id = :new_owner_id WHERE owner_id = :old_owner_id " ,
array ( " new_owner_id " => $config -> get_int ( 'anon_id' ), " old_owner_id " => $_POST [ 'id' ])
);
}
2012-01-24 16:40:36 +00:00
$database -> execute (
" DELETE FROM users WHERE id = :id " ,
array ( " id " => $_POST [ 'id' ])
);
2012-01-22 17:55:52 +00:00
$page -> set_mode ( " redirect " );
$page -> set_redirect ( make_link ( " post/list " ));
}
}
2007-04-16 11:58:25 +00:00
// }}}
}
?>