home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / lib / dav / dav.pl next >
Encoding:
Perl Script  |  2004-03-08  |  5.6 KB  |  165 lines

  1. #!/usr/bin/perl
  2. #
  3. # Script for updating the fileshares of GO
  4. #
  5. ## TODO REWRITE THIS SCRIPT - USE PHP !!!
  6.  
  7. use POSIX;
  8.  
  9. # Fork once and let the parent exit.
  10. my $pid = fork;
  11. exit if $pid;
  12.     die "Couldn't fork: $!" unless defined($pid);
  13.  
  14. # Dissociate from the controlling terminal that started us and stop being
  15. # part of whatever process group we had been a member of.
  16. POSIX::setsid()
  17.     or die "Can't start a new session: $!";
  18.  
  19.  
  20. # Connecting to the database...
  21. use DBI;
  22. my $database = "groupoffice";
  23. $user = "root";
  24. $pw = "letmein";
  25. my $dbh = DBI -> connect("DBI:mysql:$database", $user, $pw);
  26.     die "Can't connect: $DBI::errstr\n" unless (defined $dbh);
  27.  
  28. # Open the pipe...
  29. #$fifo = '/var/log/dav/fifo.log';
  30. #sysopen(FIFO, $fifo, O_RDONLY) or die "Can't open $fifo: $!";
  31.  
  32.  
  33. # Logfile
  34. $logfile = '/var/www/groupoffice/log/davpl.log';
  35.  
  36.  
  37. # Reading the pipe...
  38. #while ($line = <FIFO>) {
  39. while ($line = <STDIN>) {
  40.         
  41.     # Splitting the input...
  42.     @felder = split(/ /,$line);
  43.     ##print "User: @felder[0] Action: @felder[1] Directory: @felder[2]\n";
  44.     $user = @felder[0];
  45.     $action = @felder[1];
  46.     $directory = @felder[2];
  47.     $freigaben = "/dav/$user/freigaben"; $equal = 0; if ( $freigaben eq $directory ) { $equal = 1; }
  48.     ##print "dir: #$directory# freigaben: #$freigaben# Equal? $equal\n";
  49.     $necessary = 1;
  50.  
  51.     # The NOTMOVEABLE Sharedirectory was renamed...
  52.     if ( $action =~ /MOVE/ && $equal ) {
  53.         
  54.         ##print "  The Sharedirectory must not be renamed...\n"; $count = 1;
  55.         # Next Entry; There should be the new Directory-Name
  56.         do {
  57.             ##sysopen(FIFO_MOVED, $fifo, O_RDONLY) or die "Can't open $fifo: $!";
  58.             @new_fields = split(/ /,<STDIN>);
  59.             ##close FIFO_MOVED;
  60.             ##print "    User: @new_fields[0] Action: @new_fields[1] Directory: @new_fields[2]\n";
  61.             
  62.             $newfields_user = @new_fields[0];
  63.             ##print "    The $count.Line after is being checked...\n"; $count++;
  64.         } while ( $user != $newfields_user );
  65.         
  66.         ##print "  Move /var@new_fields[2] back to /var@felder[2]... \n";
  67.         system "test -d /var@new_fields[2] && mv /var@new_fields[2] /var@felder[2]";
  68.         
  69.         open(LOG, ">>$logfile") or die "Can't write to Logfile..."; $date = `date`;
  70.         print LOG "Moved ShareDir /var@new_fields[2] back to /var@felder[2] - $newfields_user - $date";
  71.         close LOG;
  72.  
  73.         $necessary = 0;
  74.     }
  75.     
  76.     
  77.     # Something was renamed/moved, maybe a share!?
  78.     if ( $line =~ /MOVE/ && $necessary ) {
  79.  
  80.         ##print "  Something was renamed/moved, maybe a share!?\n";
  81.         # Getting the Shares from the database...
  82.         my $sth = $dbh->prepare(q{SELECT path FROM fsShares}) or die "Can't prepare the query: " . $dbh->errstr . "\n";
  83.         $sth->execute or die "Can't execute the query: " . $dbh->errstr . "\n";
  84.         $i = 0;
  85.         while (my $ref = $sth->fetchrow_arrayref) {
  86.             #...and put it into an array!
  87.             @shares[$i] = $ref->[0];
  88.             $i++;
  89.         }
  90.         $sth->finish;
  91.  
  92.         # If the User has permission to delete/rename the Folder...
  93.         ##if (@felder != "-") { ($old_go_sharedir = @felder[2])  =~ s#dav/@felder[0]/freigaben#var/www/groupoffice/writeable#; }
  94.         if (@felder != "-") { ($old_go_sharedir = @felder[2])  =~ s#dav#var/www/groupoffice/writeable#; }
  95.         $old_share = substr($old_go_sharedir, rindex($old_go_sharedir, "writeable/") + 10, length($old_go_sharedir));
  96.  
  97.         # Look if the directory is a share...
  98.         $i = 0; $if_share = 0;
  99.         ##print "  Share to check: $old_go_sharedir\n";
  100.         while ( @shares[$i] ) {
  101.             if ($old_go_sharedir =~ /@shares[$i]/) {$if_share = 1};
  102.             $i++;
  103.         }
  104.         ##print("  Is it a share? $if_share\n");
  105.         
  106.         if ( $if_share ) {
  107.  
  108.             ##print "    What is the new name of the directory?\n"; $count = 1;
  109.             do {
  110.             ## TODO : FLAG SETZEN !!! ABCHECKEN ...
  111.                 ##sysopen(FIFO_MOVED, $fifo, O_RDONLY) or die "Can't open $fifo: $!";
  112.                 @new_fields = split(/ /,<STDIN>);
  113.                 ##close FIFO_MOVED;
  114.                 ##print "      User: @new_fields[0] Action: @new_fields[1] Directory: @new_fields[2]\n";
  115.             
  116.                 ##print "      The $count.Line after is being checked...\n"; $count++;
  117.                 $newfields_user = @new_fields[0];
  118.             } while ( $user != $newfields_user );
  119.  
  120.             ##($new_go_sharedir = @new_fields[2])  =~ s#dav/@new_fields[0]/freigaben#var/www/groupoffice/writeable#;
  121.             ($new_go_sharedir = @new_fields[2])  =~ s#dav#var/www/groupoffice/writeable#;
  122.             $new_share = substr($new_go_sharedir, rindex($new_go_sharedir, "writeable/") + 10, length($new_go_sharedir));
  123.     
  124.             # Update the Sharename in the database
  125.             my $mvd = $dbh->prepare(q{UPDATE fsShares SET path = ? WHERE path = ? })
  126.                 or die "Can't prepare the query: " . $dbh->errstr . "\n";
  127.             $mvd->execute($new_go_sharedir, $old_go_sharedir) or die "Can't execute the query: " . $dbh->errstr . "\n";
  128.             $mvd->finish;
  129.  
  130.             #Rename the softlinks in the sharing-folders of each linker...
  131.             system "grep Linkers $new_go_sharedir/.htaccess > linkers";
  132.             open (ACCESS, "< linkers") or die "Can't open accessfile: $!";
  133.             $linkerliste = <ACCESS>; close ACCESS; system "rm linkers"; $j = 1;
  134.             @linkers = split(/ /,$linkerliste);
  135.  
  136.             ##print "  Move $old_go_sharedir to $new_go_sharedir... \n";
  137.             system "test -d $old_go_sharedir && mv $old_go_sharedir $new_go_sharedir";
  138.             
  139.             open(LOG, ">>$logfile") or die "Can't write to Logfile..."; $date = `date`;
  140.             print LOG "Moved $old_go_sharedir to $new_go_sharedir - $newfields_user - $date";
  141.             close LOG;
  142.  
  143.             foreach (@linkers) { 
  144.                 $nLinker = $_;
  145.                 if ( $nLinker =~ /#/ ) {
  146.                 } else {
  147.             
  148.                     $old_dav = "/var/dav/$nLinker/freigaben/$old_share";
  149.                     $new_dav = "/var/dav/$nLinker/freigaben/$new_share";
  150.  
  151.                     system "test -h $new_dav && rm $new_dav && ln -s $new_go_sharedir $new_dav";
  152.                     system "test -h $old_dav && rm $old_dav && ln -s $new_go_sharedir $new_dav";
  153.             
  154.                     $j++;
  155.                 }
  156.             }
  157.         }
  158.     }
  159.     ##print "Checked the Line... \n";
  160. }
  161.  
  162. # Closing all connections...
  163. #close FIFO;
  164. $dbh->disconnect;
  165.