home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / FAQ / discus_admin_1357211388 / source / adm-accs.pl < prev    next >
Text File  |  2009-11-06  |  11KB  |  358 lines

  1. # FILE: adm-accs.pl
  2. # DESCRIPTION: Access Manager
  3. #-------------------------------------------------------------------------------
  4. # DISCUS COPYRIGHT NOTICE
  5. #
  6. # Discus is copyright (c) 2002 by DiscusWare, LLC, all rights reserved.
  7. # The use of Discus is governed by the Discus License Agreement which is
  8. # available from the Discus WWW site at:
  9. #    http://www.discusware.com/discus/license
  10. #
  11. # Pursuant to the Discus License Agreement, this copyright notice may not be
  12. # removed or altered in any way.
  13. #-------------------------------------------------------------------------------
  14.  
  15. use strict;
  16. use vars qw($GLOBAL_OPTIONS $DCONF $PARAMS);
  17.  
  18. ###
  19. ### ACCESS_admin
  20. ###
  21. ### Runs the Access Manager
  22. ###
  23.  
  24. sub ACCESS_admin {
  25.     my ($FORMref) = @_;
  26.     my $result = check_password($FORMref->{username}, undef, { type_required => 'moderator' }, $FORMref->{'COOKIE'});
  27.     bad_login({ username => 1}) if ! scalar(@{ $result });
  28.     my $subst = {};
  29.     $subst->{'general'}->{'username'} = $result->[0]->{user};
  30.     if ($FORMref->{action} eq "access_mgr_1") {
  31.         dreq("adm-page");
  32.         return page_mgr_1($FORMref->{username}, 2);
  33.     }
  34.     if ($FORMref->{topic} =~ /^C(\d+)$/ || $FORMref->{save_cat}) {
  35.         bad_login( { superuser_required => 1 } ) if $result->[0]->{user} ne $DCONF->{superuser};
  36.         access_manager_cat($FORMref) if $FORMref->{action} eq "access_mgr_2";
  37.         access_manager_cat($FORMref, access_manager_save_cat($FORMref));
  38.     } else {
  39.         dreq("fcn-priv");
  40.         check_topic_authorization($FORMref->{username}, $FORMref->{topic}) or error_message("Permissions Error", "You are not permitted to set access privileges for the selected topic.", 0, 1);
  41.     }
  42.     access_manager($FORMref) if $FORMref->{action} eq "access_mgr_2";
  43.     if ($FORMref->{action} eq "access_mgr_3") {
  44.         access_manager($FORMref, access_manager_save($FORMref));
  45.     }
  46.     access_manager_popup($FORMref) if $FORMref->{action} eq "access_mgr_p";
  47. }
  48.  
  49. ###
  50. ### access_manager_cat
  51. ###
  52. ### Access Manager for categories
  53. ###
  54.  
  55. sub access_manager_cat {
  56.     my ($FORMref, $success) = @_;    
  57.     my $topic = $FORMref->{topic};
  58.     my $subst = {};
  59.     if ($topic =~ /^C(\d+)/) {
  60.         $subst->{general}->{topic} = $1;
  61.         $topic = $1;
  62.     } else {
  63.         $FORMref->{topic} = "C$topic";
  64.         $subst->{general}->{topic} = $topic;
  65.     }
  66.     $subst->{general}->{is_category} = 1;
  67.     $subst->{general}->{username} = $FORMref->{username};
  68.     $subst->{general}->{topic_name} = show_topic_name($FORMref->{topic});
  69.     $subst->{success} = $success;
  70.     my @t = grep { $_->{type} == 1 } @{ board_topics() };
  71.     $subst->{topic_list} = \@t;
  72.     $subst->{checked} = {};
  73.     dreq("fcn-usrp");
  74.     my $p = read_topic_privilege_file();
  75.     if ($p->{$topic}->{'cr'}->{'ip'}->{':list:'} eq "") {
  76.         $subst->{privilege}->{'cr'} = 1;
  77.     } elsif ($p->{$topic}->{'cr'}->{'ip'}->{':list:'} eq "x") {
  78.         $subst->{privilege}->{'cr'} = 0;
  79.     } else {
  80.         $subst->{privilege}->{'cr'} = 2;
  81.         my %ch = map { $_, 1 } split(/,/, $p->{$topic}->{'cr'}->{'ip'}->{':list:'});
  82.         $subst->{checked} = \%ch;
  83.     }    
  84.     screen_out("accmgr", $subst);
  85. }
  86.  
  87. ###
  88. ### access_manager_popup
  89. ###
  90. ### Popup screens in Access Manager
  91. ###
  92.  
  93. sub access_manager_popup {
  94.     my ($FORMref) = @_;
  95.     undef my $subst;
  96.     $subst->{general}->{letter} = $FORMref->{letter};
  97.     dreq("fcn-usrp");
  98.     my $privs = read_topic_privilege_file();
  99.     if ($FORMref->{subaction} eq "modselect") {
  100.         $subst->{general}->{popup} = 1;
  101.         dreq("fcn-priv");
  102.         my $mod = read_account_file("passwd", undef, { returnformat => 'single_array' });
  103.         my @mod = ();
  104.         foreach my $x (@{ $mod }) {
  105.             undef my $i;
  106.             $i->{user} = $x->{user};
  107.             $i->{fullname} = $x->{fullname};
  108.             $i->{checked} = ($privs->{$FORMref->{topic}}->{$FORMref->{letter}}->{moderator}->{$x->{user}} == 1 ? 1 : 0);
  109.             push (@mod, $i);
  110.         }
  111.         @mod = sort { $a->{user} cmp $b->{user} } @mod;
  112.         $subst->{moderators} = \@mod;
  113.     }
  114.     if ($FORMref->{subaction} eq "usrselect") {
  115.         $subst->{general}->{popup} = 2;
  116.         dreq("fcn-grp");
  117.         my $group_file = read_group_file();
  118.         my @grp = ();
  119.         foreach my $g (@{ $group_file->{group_list} }) {
  120.             undef my $i;
  121.             $i->{group} = $g;
  122.             $i->{checked} = ($privs->{$FORMref->{topic}}->{$FORMref->{letter}}->{user}->{$i->{group}} == 1 ? 1 : 0);
  123.             push (@grp, $i);
  124.         }
  125.         @grp = sort { $a->{group} cmp $b->{group} } @grp;
  126.         $subst->{groups} = \@grp;
  127.     }
  128.     if ($FORMref->{subaction} eq "ipselect") {
  129.         $subst->{general}->{popup} = 3;
  130.         my @ips = split(/,/, $privs->{$FORMref->{topic}}->{$FORMref->{letter}}->{ip}->{':list:'});
  131.         my @ip_pass = ();
  132.         my $c = -1;
  133.         foreach my $ip (@ips) {
  134.             $c += 1;
  135.             if ($c == $#ips && $ip =~ m|^[\!\*]+$|) {
  136.                 $subst->{general}->{ipseldef} = ($ip eq "!*" ? 2 : 1);
  137.             } else {
  138.                 undef my $i;
  139.                 $i->{ipval} = $ip;
  140.                 $i->{ipval} = $' if $ip =~ m|^!|;
  141.                 $i->{ipsel} = ($ip eq "" ? 0 : ( $ip =~ m|^!| ? 2 : 1 ));
  142.                 push (@ip_pass, $i);
  143.             }
  144.         }
  145.         for (my $i = 1; $i <= 7; $i++) {
  146.             undef my $f;
  147.             $f->{ipval} = "";
  148.             $f->{ipsel} = 0;
  149.             push (@ip_pass, $f);
  150.         }
  151.         $subst->{ips} = \@ip_pass;
  152.     }
  153.     if ($FORMref->{subaction} eq "specselect") {
  154.         $subst->{general}->{popup} = 4;
  155.         if ($privs->{$FORMref->{topic}}->{$FORMref->{letter}}->{special} ne "") {
  156.             $subst->{general}->{special_exists} = 1;
  157.         }
  158.     }
  159.     screen_out("accmgr", $subst);
  160. }
  161.  
  162. ###
  163. ### access_manager_save_cat
  164. ###
  165. ### Saving of access to category options
  166. ###
  167.  
  168. sub access_manager_save_cat {
  169.     my ($FORMref) = @_;
  170.     my @actions = ();
  171.     my $topic = $FORMref->{topic}; $topic =~ s/\D//g; return -1 if $topic == 0;
  172.     dreq("fcn-usrp");
  173.     my $i = {};
  174.     $i->{topic} = $topic;
  175.     $i->{action} = "set_equal";
  176.     $i->{type} = "cr";
  177.     my $letter = "cr";
  178.     if ($FORMref->{"main$letter"} == 0) {
  179.         $i->{ip} = "x";
  180.         $i->{user} = undef;
  181.         $i->{moderator} = undef;
  182.         $i->{special} = undef;
  183.     } elsif ($FORMref->{"main$letter"} == 1) {
  184.         $i->{ip} = "";
  185.         $i->{user} = undef;
  186.         $i->{moderator} = undef;
  187.         $i->{special} = undef;
  188.     } else {
  189.         $i->{ip} = join(",", grep { /^\d+$/ } split(/,/, $FORMref->{ta}));
  190.         $i->{user} = undef;
  191.         $i->{moderator} = undef;
  192.         $i->{special} = undef;
  193.     }
  194.     push (@actions, $i);
  195.     my $m1 = read_topic_privilege_file(1);
  196.     write_topic_privilege_file(\@actions);
  197.     my $m2 = read_topic_privilege_file(1);
  198.     return { saved => 1 };
  199. }
  200.  
  201. ###
  202. ### access_manager_save
  203. ###
  204. ### Save settings for a topic (return -1 failed, 1 succeeded)
  205. ###
  206.  
  207. sub access_manager_save {
  208.     my ($FORMref) = @_;
  209.     if ($FORMref->{save_cat} == 1) {
  210.         return access_manager_save_cat($FORMref);
  211.     }
  212.     dreq("fcn-usrp", "fcn-priv");
  213.     my $topic = $FORMref->{topic}; $topic =~ s/\D//g; return -1 if $topic == 0;
  214.     my @actions = ();
  215.     { # Post Options
  216.         my $u = $FORMref->{postoption}; $u =~ s/\W//g;
  217.         undef my $i;
  218.         $i->{topic} = $topic;
  219.         $i->{type} = "o";
  220.         $i->{define} = $u;
  221.         my $k1 = (($FORMref->{ip_on_post} + 0) % 3); $i->{define} .= "i$k1";
  222.         my $k2 = (($FORMref->{ip_on_post_anon} + 0) % 3); $i->{define} .= "j$k2";
  223.         push (@actions, $i);
  224.     }
  225.     { # Queued Messages
  226.         my $u = $FORMref->{postqueue}; $u =~ s/\W//g;
  227.         undef my $i;
  228.         $i->{topic} = $topic;
  229.         $i->{type} = "q";
  230.         $i->{define} = $u;
  231.         push (@actions, $i);
  232.     }
  233.     { # Posting & Reading
  234.         foreach my $letter ('p', 'r') {
  235.             next if $letter eq "r" && ! $DCONF->{pro};
  236.             undef my $i;
  237.             $i->{topic} = $topic;
  238.             $i->{action} = "set_equal";
  239.             $i->{type} = $letter;
  240.             if ($FORMref->{"main$letter"} == 0) {
  241.                 $i->{ip} = undef;
  242.                 $i->{user} = undef;
  243.                 $i->{moderator} = undef;
  244.                 $i->{special} = undef;
  245.             } elsif ($FORMref->{"main$letter"} == 1) {
  246.                 $i->{ip} = "*";
  247.                 $i->{user} = undef;
  248.                 $i->{moderator} = undef;
  249.                 $i->{special} = undef;
  250.             } else {
  251.                 $i->{ip} = ( $FORMref->{"ip$letter"} == 0 ? undef : $FORMref->{"ipselect$letter"} );
  252.                 $i->{special} = (
  253.                     $FORMref->{"spec$letter"} == 0 ? undef :
  254.                     ($FORMref->{"specselect$letter"} =~ m|^\*| ? crypt(prepare_userpass_p($'), "ab" ) :
  255.                     $FORMref->{"specselect$letter"}));
  256.                 $i->{moderator} = ( $FORMref->{"mod$letter"} == 0 ? undef :
  257.                     ( $FORMref->{"modc$letter"} == 0 ? "**" :
  258.                     ( $FORMref->{"modc$letter"} == 1 ? "*" : $FORMref->{"modselect$letter"} )));
  259.                 $i->{user} = ( $FORMref->{"usr$letter"} == 0 ? undef :
  260.                     ( $FORMref->{"usrc$letter"} == 0 ? "*" : $FORMref->{"usrselect$letter"} ));
  261.                 foreach my $x ('ip', 'special', 'moderator', 'user') {
  262.                     $i->{$x} =~ s/://g; $i->{$x} =~ s/,,//g; $i->{$x} =~ s/^,+//; $i->{$x} =~ s/,+$//;
  263.                 }
  264.             }
  265.             push (@actions, $i);
  266.         }
  267.     }
  268.     { # Save Settings
  269.         my $m1 = read_topic_privilege_file(1);
  270.         write_topic_privilege_file(\@actions);
  271.         my $m2 = read_topic_privilege_file(1);
  272.         if ($DCONF->{pro}) {
  273.             dreq("fcn-sec-PRO");
  274.             topic_security($topic, $m2);
  275.         }
  276.         my $r1p = determine_addmessage($topic, $m1, "p");
  277.         my $r2p = determine_addmessage($topic, $m2, "p");
  278.         my $r1c = ($r1p->{disabled} + 2*$r1p->{public} + 4*$r1p->{anonymous_posting});
  279.         my $r2c = ($r2p->{disabled} + 2*$r2p->{public} + 4*$r2p->{anonymous_posting});
  280.         if ($r1c != $r2c) {
  281.             dreq("fcn-regn"); regenerate_board({ hold => "topic=$topic", topic => $topic, username => $FORMref->{username}, action => "regen1" });
  282.         }
  283.     }
  284.     return { saved => 1 };
  285. }
  286.  
  287. ###
  288. ### access_manager
  289. ###
  290. ### Main interface of the Access Manager
  291. ###
  292.  
  293. sub access_manager {
  294.     my ($FORMref, $success) = @_;
  295.     my $topic = $FORMref->{topic};
  296.     my $subst = {};
  297.     if ($topic =~ /^C/ || $FORMref->{save_cat}) {
  298.         $subst->{general}->{topic} = $1;
  299.         $subst->{general}->{is_category} = 1;
  300.     } else {
  301.         $subst->{general}->{topic} = $topic;
  302.         $subst->{general}->{is_category} = 0;
  303.     }
  304.     $subst->{general}->{username} = $FORMref->{username};
  305.     $subst->{general}->{topic_name} = show_topic_name($FORMref->{topic});
  306.     $subst->{success} = $success;
  307.     dreq("fcn-usrp");
  308.     my @u = ('p'); push (@u, 'r') if $DCONF->{pro};
  309.     my $p = read_topic_privilege_file();
  310.     foreach my $i (@u) {
  311.         my $x = $p->{$FORMref->{topic}}->{$i};
  312.         $subst->{privilege}->{$i} = (
  313.             ($x->{ip}->{':list:'} eq "" && $x->{user}->{':list:'} eq "" && $x->{moderator}->{':list:'} eq "" && $x->{special} eq "") ? 0 :
  314.             ( $x->{ip}->{':list:'} eq "*" ? 1 : 2 ));
  315.         $subst->{modcheck}->{$i} = ( $x->{moderator}->{':list:'} eq "" ? 0 : 1 );
  316.         $subst->{modtype}->{$i} = ( $x->{moderator}->{':list:'} eq "**" ? 0 : ( $x->{moderator}->{':list:'} eq "*" ? 1 : 2 ));
  317.         $subst->{usrcheck}->{$i} = ( $x->{user}->{':list:'} eq "" ? 0 : 1 );
  318.         $subst->{usrtype}->{$i} = ( $x->{user}->{':list:'} eq "*" ? 0 : 1 );
  319.         $subst->{ipcheck}->{$i} = ( $x->{ip}->{':list:'} eq "" ? 0 : 1 );
  320.         $subst->{speccheck}->{$i} = ( $x->{special} eq "" ? 0 : 1 );
  321.         $subst->{general}->{"modselect$i"} = $x->{moderator}->{':list:'};
  322.         $subst->{general}->{"usrselect$i"} = $x->{user}->{':list:'};
  323.         $subst->{general}->{"ipselect$i"} = $x->{ip}->{':list:'};
  324.         $subst->{general}->{"specselect$i"} = $x->{special};
  325.     }
  326.     if ($DCONF->{pro}) {
  327.         $subst->{general}->{problem} = 1 if (
  328.             ($subst->{privilege}->{'r'} == 1 && -e "$DCONF->{secdir}/$topic") ||
  329.             ($subst->{privilege}->{'r'} != 1 && -e "$DCONF->{message_dir}/$topic"));
  330.     }
  331.     $subst->{queuecheck} = $p->{$topic}->{'q'};
  332.     $subst->{postcheck} = $p->{$topic}->{'o'};
  333.     screen_out("accmgr", $subst);
  334. }
  335.  
  336. ###
  337. ### show_topic_name
  338. ###
  339. ### Returns the topic name, given a topic number
  340. ###
  341.  
  342. sub show_topic_name {
  343.     my ($topic_num) = @_;
  344.     if ($topic_num =~ /^C(\d+)$/) {
  345.         my $l = $1;
  346.         my $x = board_topics();
  347.         foreach my $z (@{ $x }) {
  348.             return $z->{text} if $z->{number} == $l;
  349.         }
  350.         return "";
  351.     }    
  352.     return undef if $topic_num !~ m|^\d+$|;
  353.     my $tree = read_tree($topic_num);
  354.     return $tree->[0]->{name};
  355. }
  356.  
  357. 1;
  358.