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

  1. # FILE: fcn-priv.pl
  2. # DESCRIPTION: Privilege Manipulation Functions (privs.txt)
  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. ### check_topic_authorization
  20. ###
  21. ### Checks to see if $moderator is authorized to edit $topic
  22. ###
  23.  
  24. sub check_topic_authorization {
  25.     my ($moderator, $topic, $file_in) = @_;
  26.     $moderator = lc($moderator);
  27.     return 1 if $moderator eq $DCONF->{superuser};
  28.     if (! defined $file_in) {
  29.         $file_in = read_privilege_file();
  30.     }
  31.     return $file_in->{by_mod}->{$moderator}->{$topic};
  32. }
  33.  
  34. ###
  35. ### read_privilege_file
  36. ###
  37. ### Low-level reading of the privs.txt file
  38. ###
  39.  
  40. sub read_privilege_file {
  41.     my ($noerr, $nolock) = @_;
  42.     my $file = $nolock ? readfile("$DCONF->{admin_dir}/privs.txt", "read_privilege_file", { no_lock => 1, no_unlock => 1, create => $noerr, zero_ok => 1 }) : readfile("$DCONF->{admin_dir}/privs.txt", "read_privilege_file", { create => $noerr, zero_ok => 1 });
  43.     undef my $result;
  44.     foreach my $line (@{ $file }) {
  45.         chomp $line;
  46.         my ($topic, $mods) = split(/:/, $line);
  47.         next if $topic eq "#";
  48.         next if $topic !~ m|\S|;
  49.         my @mods = split(/,/, $mods);
  50.         push (@{ $result->{topic_list} }, $topic);
  51.         foreach my $mod (@mods) {
  52.             $result->{by_mod}->{$mod}->{$topic} = 1;
  53.             $result->{by_topic}->{$topic}->{$mod} = 1;
  54.         }
  55.     }
  56.     return $result;
  57. }
  58.  
  59. ###
  60. ### write_privilege_file
  61. ###
  62. ### Low-level writing of the privs.txt file based on a list of "actions"
  63. ###
  64.  
  65. sub write_privilege_file {
  66.     my ($actions, $noerr) = @_;
  67.     my @newfile = ();
  68.     undef my $classify;
  69.     foreach my $action (@{ $actions }) {
  70.         my ($gq, $a, $m) = ($action->{topic}, $action->{action}, $action->{moderator});
  71.         foreach my $g (split(/,/, $gq)) {
  72.             if ($a eq "add_topic") {
  73.                 push (@newfile, "$g:$m\n");
  74.             } elsif ($a eq "del_topic") {
  75.                 $classify->{$g}->{del} = 1;
  76.             } elsif ($a eq "set_equal") {
  77.                 $classify->{$g}->{modequals} = $m;
  78.             } else {
  79.                 foreach my $md (split(/,/, $m)) {
  80.                     if ($a eq "add_mod") {
  81.                         $classify->{$g}->{modadd}->{$md} = 1;
  82.                     } elsif ($a eq "del_mod") {
  83.                         $classify->{$g}->{moddel}->{$md} = 1;
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.     }
  89.     my $file = readfile("$DCONF->{admin_dir}/privs.txt", "write_privilege_file", { no_unlock => 1, zero_ok => 1, create => $noerr });
  90.     foreach my $line (@{ $file }) {
  91.         my $line_manip = $line; chomp $line_manip;
  92.         my ($topic, $mods) = split(/:/, $line_manip);
  93.         if ($topic eq "#") {
  94.             push (@newfile, $line); next;
  95.         }
  96.         if ($topic !~ m|\S|) {
  97.             next;
  98.         }
  99.         if ($classify->{$topic}->{del}) {
  100.             next;
  101.         }
  102.         if (defined $classify->{$topic}->{modequals}) {
  103.             push (@newfile, "$topic:$classify->{$topic}->{modequals}\n");
  104.             next;
  105.         }
  106.         my @mods = split(/,/, $mods);
  107.         undef my @modsnew;
  108.         undef my $seen;
  109.         foreach my $m (@mods) {
  110.             next if $classify->{$topic}->{moddel}->{$m};
  111.             next if $classify->{$topic}->{modadd}->{$m};
  112.             next if $classify->{"*"}->{moddel}->{$m};
  113.             push (@modsnew, $m);
  114.         }
  115.         foreach my $k (keys(%{ $classify->{$topic}->{modadd} })) {
  116.             push (@modsnew, $k);
  117.         }
  118.         $mods = join(",", sort @modsnew);
  119.         $line_manip = join(":", $topic, $mods);
  120.         $line_manip .= "\n";
  121.         push (@newfile, $line_manip);
  122.     }
  123.     writefile("$DCONF->{admin_dir}/privs.txt", \@newfile, "write_privilege_file", { no_lock => 1, zero_ok => 1 });
  124. }
  125.  
  126. 1;
  127.