1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
|
package Ops::User;
use SmallPigVars qw($config);
use SmallPig;
use CGI qw(:standard :cgi-lib);
use CGI::Cookie ();
use strict;
my $lang;
my %CACHE;
sub new{
my ($class, $STATE, $spdb) = @_;
my $self = {};
if($spdb){ $self->{'spdb'} = $spdb; }
else{ require SPDB; $self->{'spdb'} = SPDB->new; }
$self->{'STATE'} = $STATE;
$lang = SmallPig::get_lang_ptr($STATE->{lang}, "LUser");
return bless $self, $class;
}
sub DESTROY{
my $self = shift;
my $spdb = $self->{'spdb'};
$spdb->db_disconnect($spdb->{'dbh'});
}
sub _salt{
my @saltset = (0..9, 'A'..'Z', 'a'..'z', '.', '/');
return join('', @saltset[rand @saltset, rand @saltset]);
}
sub _create_passwd{
my $ret = substr(join('', localtime()), 0, 10);
return $ret;
}
sub _update_passwd{
my ($self, $uid, $passwd)= @_;
my $spdb = $self->{'spdb'};
my $DBH = $spdb->{'dbh'};
my $crypt = crypt($passwd, _salt());
my $data = {('passwd'=>$DBH->quote("$crypt"))};
return $spdb->db_update("Users", $data, "uid=$uid");
}
sub register{
my ($self) = @_;
my ($STATE, $spdb) = map{ $self->{$_} } qw(STATE spdb);
my $DBH = $spdb->{'dbh'};
my ($tboards, $tusers, $op) =
map { $STATE->{$_} } qw(boards users op);
my $msg;
my ($username, $email, $realname, $passwd, $passwda) =
map{ $STATE->{$_} }
qw(myusername myemail myrealname mypassword mypassworda);
my $uri = script_name()."?op=register";
if($username =~ /anonymous/gi){
return (0, $lang->{'anonymous'}, "", $uri);
}
if($username =~ /\W/g){
return (0, $lang->{'non_word'}, "", $uri);
}
# --- if there is no username or email
if(!$username||!$email){
my $msg;
$msg = "<LI>$lang->{'loginusername'} $lang->{'is_missing'}."
if !$username;
$msg .= "<LI>$lang->{'loginemail'} $lang->{'is_missing'}."
if !$email;
return (0, $msg, "", $uri);
}
if($config->{'self_chose_passwd'} eq "yes"){
my $msg;
if($passwd && $passwda){
unless($passwda eq $passwd){
$msg .= "<LI>".$lang->{'passwds_not_match'};
}
if(length($passwd) < 5 || length($passwda) <5){
$msg .= "<LI>".$lang->{'passwd_too_short'};
}
}
if($passwd && !$passwda || !$passwd && $passwda){
$msg .= "<LI>".$lang->{'need_passwd'};
}
return (0, $msg, "", $uri) if $msg;
}
my $username_q = $DBH->quote($username);
my $email_q = $DBH->quote($email);
my $res = $spdb->db_count("Users", "username=$username_q");
if($res){
return (0, $lang->{'user_in_use'}, "", $uri);
}
# --- if allow replicated email
if($config->{'emaildup'} eq "yes"){
$res = $spdb->db_count("Users", "realemail=$email_q AND active=1");
if($res){
return (0, $lang->{'mult_acc'}, "", $uri);
}
}
# --- find the current time
my $time = $spdb->db_date(); my $active;
# --- lock the table Users
$spdb->db_lock(("Users", "BoardPrefs"));
my ($uid, $isadmin) = $spdb->db_select_cols("MAX(uid)", "Users");
if($uid){
$uid++; $isadmin = 0;
# --- if need confirm, active = 0;
$active = ($config->{'confirm_email'} eq "yes")?0:1;
}
else{
# --- first user register should be admin
$uid = $isadmin = 1; $active = 1;
# --- insert dummy node to *Users*
my $data = {('uid' =>0,
'username' =>'anon','nickname' =>'anon',
'realemail' =>'anon','fakeemail' =>'anon')};
$spdb->db_insert("Users", $data);
}
# --- insert new user in *Users*
my $data = {('uid'=>$uid,
'username' =>$username,
'nickname' =>$username,
'realemail' =>$email,
'fakeemail' =>$email,
'registered'=>$time,
'isadmin' =>$isadmin,
'active' =>$active,
'realname' =>$realname,
'passwd' =>$passwd)};
my ($succ, $errmsg) = $spdb->db_insert("Users", $data);
return (0, $errmsg, "", $uri) if !$succ;
$data = {(
'uid' =>$uid,
'sort' =>$config->{'sort'},
'view' =>$config->{'view'},
'display' =>$config->{'display'},
'postsper' =>$config->{'postsper'},
)};
($succ, $errmsg) = $spdb->db_insert("BoardPrefs", $data);
if(!$succ){
$spdb->db_delete("Users", "uid=$uid");
return (0, $errmsg, "", $uri);
}
# --- unlock the table Users
$spdb->db_unlock();
# --- create a new password for the new user
my $new_passwd = $passwd || _create_passwd();
# --- mail the password to the new user
my $mailsubject = "$config->{'title'} $lang->{'reg_conf_title'}";
my $conf_text = "";
$conf_text = qq!
To confirm your email address, visit the following
Web page:
$config->{'cgidir'}/user.cgi?op=confirm&uid=$uid!
if $config->{'confirm_email'} eq "yes";
my $mailbody = qq!
Welcome to $config->{'title'}
You have registered for $config->{'title'} using email address:
$email. If you did not register for $config->{'title'},
please ignore this message.
$conf_text
As a reminder, your username is $username and your password
is $new_passwd. You may keep this email to refer to case you
forget.
Thanks.
$config->{'adminaddr'}
!;
my ($succ, $errmsg) = SmallPig::send_mail($email, $config->{'adminaddr'},
$mailsubject, $mailbody);
unless ($succ){
$spdb->db_delete("Users", "uid=$uid");
return (0, "$lang->{'cant_mail'}: $errmsg", "", $uri);
}
my ($succ, $errmsg) = $self->_update_passwd($uid, $new_passwd);
return (0, $errmsg, "", $uri) if !$succ;
if($config->{'notifyadmins'} eq "yes"){
my $title = "$config->{'title'} new user registration";
my $date = localtime();
my $agent = user_agent();
my $remote = remote_host();
my $mail = qq!
At $date, a user at $remote registered an account with uid $uid using the $agent browser..
!;
# --- look up the admins
my ($admins) = $spdb->db_select_many_new(1,"realemail","Users","isadmin=1");
# --- send email to each admin
if($admins){
while (my ($realemail) = $admins->fetchrow_array){
SmallPig::send_mail($realemail, $config->{'adminaddr'},
$title, $mail);
}
$spdb->db_handler_done($admins);
}
}
if($config->{'spmail_cgi'} && -d $config->{'spmail_cgi'}){
my $mlang = SmallPig::get_spmail_lang();
# --- create default folders
$spdb->db_lock(("MailFolders"));
my $mmfid = $spdb->db_max("mfid", "MailFolders");
$mmfid = 0 unless $mmfid; $mmfid++;
my $mmforder = $spdb->db_max("mforder", "MailFolders");
$mmforder = 0 unless $mmforder; $mmforder++;
my @folders = ($mlang->{'inbox'}, $mlang->{'draft'},
$mlang->{'sent'}, $mlang->{'trash'});
push @folders, $mlang->{'Private_Msg'} ;
for(my $i=$mmfid, my $j=0; $i<$mmfid+4; $i++, $j++){
my $data = {('mfid'=>$i,
'mfname'=>$folders[$j],
'msid'=>0,
'uid'=>$uid,
'mforder'=>$mmforder)};
$spdb->db_insert("MailFolders", $data);
}
}
$msg = "$lang->{'user_register2'}: $username <BR><BR>
$lang->{'user_register3'}";
return (1, $msg, $lang->{'user_register1'}, "$config->{'cgidir'}/user.cgi?op=login");
}
sub forget_passwd{
my ($self, $STATE) = @_;
my ($STATE, $spdb) = map{ $self->{$_} } qw(STATE spdb);
my $DBH = $spdb->{'dbh'};
my $msg;
my $username_q = $DBH->quote($STATE->{'myusername'});
unless($STATE->{'myusername'}){
return (0, "$lang->{'loginusername'} $lang->{'is_missing'}.", "", "$config->{'cgidir'}/user.cgi?op=login");
}
my ($uid, $email, $fakeemail) =
$spdb->db_select_cols("uid, realemail, fakeemail", "Users",
"username=$username_q AND active=1");
unless($uid){
return (0, $lang->{'cant_find_user'}, "", "$config->{'cgidir'}/user.cgi?op=login");
}
my $new_passwd = _create_passwd();
my $mailsubject = "$config->{'title'} $lang->{'request_passwd'}";
my $mailbody = qq|
Here is your temporary password: $new_passwd
Please write this Password down! Once you have
logged in, you may change this password.
Thanks.
$config->{'adminaddr'}
|;
my ($succ, $errmsg) = SmallPig::send_mail($email, $config->{'adminaddr'},
$mailsubject, $mailbody);
unless($succ){
return (0, "$lang->{'cant_mail'}: $errmsg", "", "$config->{'cgidir'}/user.cgi?op=login");
}
my ($succ, $errmsg) = $self->_update_passwd($uid, $new_passwd);
return (0, $errmsg, "", "$config->{'cgidir'}/user.cgi?op=login") if !$succ;
return (1, $lang->{'forget_passwd'}, $lang->{'passwd_sent'},
$ENV{SCRIPT_NAME}."?op=login");
}
sub save_user1{
my ($self, $STATE) = @_;
my ($STATE, $spdb) = map{ $self->{$_} } qw(STATE spdb);
my $DBH = $spdb->{'dbh'};
my ($uri, $msg);
my ($vpasswd, $passwd, $nickname, $realname, $realemail,
$fakeemail, $homepage, $bio,$icq, $myuid, $username, $prev_op,
$photo, $location, $privlev, $postsper, $uid) =
map{ $STATE->{$_} }
qw(vpasswd passwd
mynickname realname realemail
myfakeemail homepage mybio myicq
myuid myusername prev_op
myphoto mylocation myprivlev postsper uid);
# --- check the uid --- thanks Tim W from solutionscripts.com
# --- if uid ne myuid
if($uid != $myuid){
# --- need admin right to change the profile
my ($isadmin) = $spdb->db_select_cols("isadmin", "Users", "uid=$myuid");
if(!$isadmin){
return (0, $lang->{'not_allow_edit'}, "", "$config->{'cgidir'}/user.cgi");
}
}
if($passwd && $passwd){
unless($vpasswd eq $passwd){
$msg = "<LI>".$lang->{'passwds_not_match'};
}
if(length($passwd) < 5 || length($vpasswd) <5){
$msg .= "<LI>".$lang->{'passwd_too_short'};
}
}
if(($passwd && !$passwd) || (!$passwd && $passwd)){
$msg = "<LI>".$lang->{'need_passwd'};
}
if($msg){
$uri = "$config->{'cgidir'}/user.cgi";
if($prev_op eq "modifyuser"){
$uri = "$config->{'cgidir'}/admin.cgi";
}
return (0, $msg, "", $uri);
}
#update all fields
else{
$self->_update_passwd($myuid, $passwd) if $passwd && $vpasswd;
my ($passwdq, $nicknameq, $realnameq, $realemailq,
$fakeemailq, $homepageq, $bioq, $icqq, $photoq,
$locationq, $privlevq) =
map{ $DBH->quote($STATE->{$_}) }
qw(passwd
mynickname realname realemail
myfakeemail homepage mybio myicq
myphoto mylocation myprivlev);
my $data = {('realname'=>$realnameq,
'realemail'=>$realemailq, 'fakeemail'=>$fakeemailq,
'homepage'=>$homepageq, 'bio'=>$bioq,
'icq'=>$icqq, 'photourl'=>$photoq,
'location'=>$locationq, 'privlev'=>$privlevq)};
$data->{'nickname'} = $nicknameq if $config->{'one_hdlr'} ne "yes";
my ($succ, $errmsg) = $spdb->db_update("Users", $data, "uid=$myuid");
return (0, $errmsg) if !$succ;
}
if($prev_op eq "modifyuser"){
$uri = "$config->{'cgidir'}/admin.cgi";
}
return (1, $lang->{'profile_modified2'}, $lang->{'profile_modified1'}, $uri);
}
sub save_preferences1{
my ($self, $STATE) = @_;
my ($STATE, $spdb) = map{ $self->{$_} } qw(STATE spdb);
my $DBH = $spdb->{'dbh'};
my ($postsper, $myuid, $uid, $prev_op) =
map{ $STATE->{$_} } qw(mypostsper myuid uid prev_op);
# --- check the uid --- thanks Tim W from solutionscripts.com
# --- if uid ne myuid
if($uid != $myuid){
# --- need admin right to change the profile
my ($isadmin) = $spdb->db_select_cols("isadmin", "Users", "uid=$uid");
if(!$isadmin){
return (0, $lang->{'not_allow_edit'}, "", "$config->{'cgidir'}/user.cgi");
}
}
my ($sigq, $sortq, $displayq, $viewq, $cookieexpireq, $themeq, $langq) =
map{ $DBH->quote($STATE->{$_}) }
qw(mysig mysort mydisplay myview cookieexpire mytheme mylang);
my $data = {('sig'=>$sigq,
'sort'=>$sortq, 'display'=>$displayq,
'view'=>$viewq, 'postsper'=>$postsper,
'theme'=>$themeq,
'lang'=>$langq)};
my ($succ, $errmsg) = $spdb->db_update("BoardPrefs", $data, "uid=$myuid");
return (0, $errmsg) if !$succ;
($succ, $errmsg) = $spdb->db_update("Users", {'cookieexpire'=>$cookieexpireq}, "uid=$myuid");
return (0, $errmsg) if !$succ;
my $uri = "$config->{'cgidir'}/user.cgi";
if($prev_op eq "modifyuser"){
$uri = "$config->{'cgidir'}/admin.cgi";
}
return (0, $errmsg) if !$succ;
return (1, $lang->{'profile_modified2'}, $lang->{'profile_modified1'}, $uri);
}
sub _authenticate{
my ($self, $username, $passwd) = @_;
my $spdb = $self->{'spdb'};
my $DBH = $spdb->{'dbh'};
my $username_q = $DBH->quote($username);
my ($saved_passwd, $ce, $active) =
$spdb->db_select_cols("passwd, cookieexpire, active",
"Users",
"STRCMP(username, $username_q)=0");
return (0, $lang->{'username_not_exist'}) unless($saved_passwd);
return (0, $lang->{'passwd_not_match'})
unless($saved_passwd eq crypt($passwd, $saved_passwd));
return (0, $lang->{'not_active'}) unless $active;
return ($ce, '');
}
sub verify_cookie{
my $self = shift;
my ($STATE, $spdb) = map{ $self->{$_} } qw(STATE spdb);
require MD5;
my %cookie = cookie('login_cookie');
return (0, "no cookie") unless %cookie;
my $secret = $config->{'cookiesecret'};
my $newhash = MD5->hexhash(
$secret.MD5->hexhash(join('',
$secret,
@cookie{qw(time username)})));
unless($newhash eq $cookie{'hash'}){
undef $CACHE{'cookiesecret'};
return (0, "");
}
return($cookie{'username'}, "");
}
sub login{
my ($self) = @_;
my ($STATE, $spdb) = map{ $self->{$_} } qw(STATE spdb);
my $DBH = $spdb->{'dbh'};
my ($uri, $cookie, $msg);
my ($username, $passwd, $autologout) =
map{ $STATE->{$_} } qw(loginusername loginpasswd autologout);
$uri = $ENV{'SCRIPT_NAME'}."?op=login";
if(length($username) > 0 && length($passwd) > 0){
my $res;
($res, $msg) = $self->_authenticate($username, $passwd);
if($res){
# --- make cookie
$cookie = $self->_create_cookie("login_cookie", $username, "", $res);
unless($cookie){
return (0, $lang->{'cant_cookie'}, "", $uri);
}
$uri = $ENV{'SCRIPT_NAME'};
# --- update the lastlogin
my $timeq = $DBH->quote($spdb->db_date());
my $usernameq = $DBH->quote($username);
my $data = {('lastlogon'=>$timeq)};
$spdb->db_update("Users", $data, "username=$usernameq AND active=1");
}
else{
return (0, $msg, "", $uri);
}
}
else{
my $msg;
$msg = "<LI>$lang->{'loginusername'} $lang->{'is_missing'}."
if !$username;
$msg .= "<LI>$lang->{'loginpasswd'} $lang->{'is_missing'}."
if !$passwd;
return (0, $msg, "", $uri);
}
return (1, $lang->{'login_succ2'}, $lang->{'login_succ1'}, $uri, $cookie);
}
sub _create_cookie{
my ($self, $cname, $username, $secret, $cexpire) = @_;
my $now = time();
$secret = $secret || $config->{'cookiesecret'};
my $hash = MD5->hexhash($secret.MD5->hexhash(join('',
$secret,
$now,
$username)));
my $cval = {'time' => $now,
'username' => $username,
'hash' => $hash};
my $path = "/";
require CGI::Cookie;
return CGI::Cookie->new(-name => $cname,
-path => $path,
-value => $cval,
-expires => $cexpire);
}
sub logout{
my ($self) = @_;
my ($STATE, $spdb) = map{ $self->{$_} } qw(STATE spdb);
my $DBH = $spdb->{'dbh'};
my $username = $STATE->{'username'};
my $uid = $STATE->{'uid'};
my $usernameq = $DBH->quote($username);
# --- make_cookie
my $cookie = "";
$cookie = $self->_create_cookie("login_cookie", $username, "logout");
return (0, $lang->{'cant_cookie'}, "")
unless $cookie;
if($config->{'spmail_cgi'} && -d $config->{'spmail_cgi'}){
# --- update the caution
my $cnt = $spdb->db_count("CautionTo","mto=$uid");
if($cnt){
$spdb->db_delete("CautionTo", "mto=$uid AND slogon<=1");
$spdb->db_update("CautionTo", {('slogon'=>'slogon-1')},
"mto=$uid AND slogon>1");
}
# --- update the new messages to unread
$spdb->db_update("MailMessages", {('new'=>2)},
"new=1 AND uid=$uid");
}
return (0, $lang->{'logout_succ2'}, $lang->{'logout_succ1'},
$ENV{'SCRIPT_NAME'}."?op=login", $cookie);
}
sub confirm{
my ($self) = @_;
my ($STATE, $spdb) = map{ $self->{$_} } qw(STATE spdb);
my $uid = $STATE->{'uid'};
if(!$uid || !$spdb->db_count("Users", "uid=$uid")){
return (0, $lang->{'cant_found_page'});
}
$spdb->db_update("Users", {('active'=>1)}, "uid=$uid");
return (1, $lang->{'confirm2'}, $lang->{'confirm1'});
}
sub _select_the_most_parent{
my ($template, $cid) = @_;
my $spdb = $template->{'spdb'};
# --- find out its parentid
my ($pid) = $spdb->db_select_cols("parentid", "ParentAndChild",
"childid=$cid");
if(!$pid){
return $cid;
}
else{
$template->_select_the_most_parent($pid);
}
}
sub _select_the_least_child{
my ($self, $cid, $cnt, $date) = @_;
my $spdb = $self->{'spdb'};
my ($childids) = $spdb->db_select_many_new(1, "childid", "ParentAndChild",
"parentid=$cid");
if(!$childids){
($date) = $spdb->db_select_cols("date", "Posts", "cid=$cid") if !$date;
$cnt = 0 if !$cnt;
return ($cid, $cnt, $date);
}
my ($scid, $scnt, $cnt, $where, $date);
my $scnt = 0;
while (my ($childid) = $childids->fetchrow_array){
$scnt++;
($scid, $cnt) = $self->_select_the_least_child($childid, 0, $date);
$scnt += $cnt;
$where .= "cid=$scid OR ";
}
$spdb->db_handler_done($childids);
$where =~ s/(.+) OR $/$1/;
($scid, $date) = $spdb->db_select_cols("cid, MAX(date)",
"Posts", $where,
"GROUP BY sid");
return ($scid, $scnt, $date);
}
sub _expire_threads{
my ($self, $selectedthreads, $sid, $op) = @_;
my $spdb = $self->{'spdb'};
my $DBH = $spdb->{'dbh'};
my ($where, $where1, $where2, $where3, $cnt1);
foreach (@$selectedthreads){
my $cid = $_;
$where .= "cid=$cid OR ";
$where1 .= "parentid=$cid OR ";
$where2 .= "childid=$cid OR ";
my $mpid = $self->_select_the_most_parent($cid);
$where3 .= "cid=$mpid OR ";
}
$where =~ s/(.+) OR $/$1/g;
$where1 =~ s/(.+) OR $/$1/g;
$where2 =~ s/(.+) OR $/$1/g;
$where3 =~ s/(.+) OR $/$1/g;
# --- lookup child
my ($childids) = $spdb->db_select_many_new(1, "childid","ParentAndChild", "$where1");
if($childids){
while ( my ($childid) = $childids->fetchrow_array){
# --- find out the least child
my ($lchildid, $replies, $lastmod) =
$self->_select_the_least_child($childid);
my $data = {('cid'=>$childid, 'sid'=>$sid,
'lastmod'=>$lastmod, 'replies'=>$replies)};
$spdb->db_insert("MostParents", $data);
}
$spdb->db_handler_done($childids);
}
$where1 = "$where1 OR $where2" if $where1;
# --- delete from ParentAndChild
$spdb->db_delete("ParentAndChild", $where1);
# --- update the replies field for the most parent
my ($mpids) = $spdb->db_select_many_new(1, "cid", "MostParents", $where3);
if($mpids){
while (my ($mpid) = $mpids->fetchrow_array){
my ($lcid, $cnt, $date) = $self->_select_the_least_child($mpid);
my $dateq = $DBH->quote($date);
my ($succ, $errmsg) =
$spdb->db_update("MostParents",
{('replies'=>$cnt, 'lastmod'=>$dateq)},
"cid=$mpid");
return (0, $errmsg) if !$succ;
}
$spdb->db_handler_done($mpids);
}
# --- update commentcount in Boards
my $sidq = $DBH->quote($sid);
$cnt1 = $spdb->db_count("Posts", "approved=1 AND sid=$sidq");
my ($succ, $errmsg) =
$spdb->db_update("Boards",
{('commentcount'=>$cnt1)},
"sid=$sidq");
return (0, $errmsg) if !$succ;
return (1);
}
sub _recover_thread{
my ($self, $cid, $sid, $cautionblockptr) = @_;
my $spdb = $self->{'spdb'};
my $DBH = $spdb->{'dbh'};
my $sidq = $DBH->quote($sid);
my ($childids) = $spdb->db_select_many_new(1, "cid", "Posts",
"sid=$sidq AND pid=$cid");
my $where;
if($childids){
while (my ($childid) = $childids->fetchrow_array){
$where .= "cid=$childid OR ";
my $data = {('parentid'=>$cid, 'childid'=>$childid)};
$spdb->db_insert("ParentAndChild", $data);
}
$spdb->db_handler_done($childids);
}
$where =~ s/(.+) OR $/$1/g;
$spdb->db_delete("MostParents", $where);
return (1);
}
sub move_threads{
my ($self, $STATE) = @_;
my ($STATE, $spdb) = map{ $self->{$_} } qw(STATE spdb);
my $DBH = $spdb->{'dbh'};
my $ret;
my ($selectedthreads, $catandboard, $sid, $op, $userstatus,
$tuserstatus) =
map{ $STATE->{$_} }
qw(selectedthreads catandboard sid op userstatus tuserstatus);
my ($level) = $spdb->db_select_cols("level", "Boards",
"sid=".$DBH->quote($sid));
my $bstatus = SmallPig::get_board_status($level);
# --- sid from mod userstatus --> ptr to arr
my $sfmus = $tuserstatus->{mod};
my $sfgmus = $tuserstatus->{gmem};
if($userstatus ne "admin" &&
!($bstatus =~ /group/ && $sfmus && grep { $_ eq $sid } @$sfmus) &&
!($bstatus eq "group private" && $sfgmus && grep { $_ eq $sid } @$sfgmus) &&
$bstatus !~ /public/ && $bstatus !~ /user private/){
return (0, $lang->{'unable_to_move'});
}
# --- make sure at least one item is selected
unless($selectedthreads){
return (0, $lang->{'at_least_one'});
}
my $cabq = $DBH->quote($catandboard);
if($spdb->db_count("Boards", "sid=$cabq") <= 0){
return (0, $lang->{'choose_board'});
}
my @selectedthreads = split("#", $selectedthreads);
my ($ctype, $tmpmsg) = $self->_expire_threads(\@selectedthreads, $sid, $op);
return (0, $tmpmsg) if $ctype == 0;
my ($where, $where1);
foreach (@selectedthreads){
my $cid = $_;
$where .= "cid=$cid OR ";
}
$where =~ s/(.*) OR $/$1/g;
my $data = {('sid'=>$cabq)};
$spdb->db_update("Posts", $data, $where);
$spdb->db_update("FileAttach", $data, $where);
$spdb->db_update("MostParents", $data, $where);
my ($pids) = $spdb->db_select_many_new(1, "cid, pid", "Posts", $where);
if($pids){
while(my ($cid, $pid) = $pids->fetchrow_array){
if($spdb->db_count("Posts", "sid=$cabq AND cid=$pid") <= 0){
my $data = {('sid'=>$catandboard, 'cid'=>$cid)};
$spdb->db_insert("MostParents", $data);
}
else{
$spdb->db_delete("MostParents", "sid=$cabq AND cid=$cid");
$spdb->db_insert("ParentAndChild", {('parentid'=>$pid,
'childid'=>$cid)});
}
}
$spdb->db_handler_done($pids);
}
# --- update the ParentAndChild
foreach (@selectedthreads){
my $cid = $_;
if($spdb->db_count("Posts", "sid=$cabq AND pid=$cid AND pid<>0") > 0){
# --- need to recover the branch
my ($ctype, $tmpmsg) =
$self->_recover_thread($cid, $catandboard);
return (0, $tmpmsg) if $ctype==0;
}
my $mpid = $self->_select_the_most_parent($cid);
$where1 .= "cid=$mpid OR ";
}
$where1 =~ s/(.*) OR $/$1/g;
# --- update the replies field for the most parent
my ($mpids) = $spdb->db_select_many_new(1, "cid", "MostParents", $where1);
if($mpids){
while ( my ($mpid) = $mpids->fetchrow_array){
my ($lcid, $cnt, $date) = $self->_select_the_least_child($mpid);
my $dateq = $DBH->quote($date);
my ($succ, $errmsg) =
$spdb->db_update("MostParents",
{('replies'=>$cnt, 'lastmod'=>$dateq)},
"cid=$mpid");
return (0, $errmsg) if !$succ;
}
$spdb->db_handler_done($mpids);
}
# --- update commentcount in Boards
# -------------------------------------------------------------- #
my $sidq = $DBH->quote($catandboard);
my $cnt1 = $spdb->db_count("Posts", "approved=1 AND sid=$sidq");
my $tlastmod = $spdb->db_max("date", "Posts", "approved=1 AND sid=$sidq");
my ($succ, $errmsg) =
$spdb->db_update("Boards",
{('commentcount'=>$cnt1,
'lastmod'=>$DBH->quote($tlastmod))},
"sid=$sidq");
# -------------------------------------------------------------- #
$sidq = $DBH->quote($sid);
$cnt1 = $spdb->db_count("Posts", "approved=1 AND sid=$sidq");
$tlastmod = $spdb->db_max("date", "Posts", "approved=1 AND sid=$sidq");
($succ, $errmsg) =
$spdb->db_update("Boards",
{('commentcount'=>$cnt1,
'lastmod'=>$DBH->quote($tlastmod))},
"sid=$sidq");
# -------------------------------------------------------------- #
return (0, $errmsg) if !$succ;
return (1, $lang->{'move_threads4'}, $lang->{'move_threads3'});
}
return 1;
|