root / nephthys_watch.php

View | Annotate | Download (4.9 KB)

1
#!/usr/bin/php
2
<?php
3
4
/***************************************************************************
5
 *
6
 * Nephthys - file sharing management
7
 * Copyright (c) by Andreas Unterkircher, unki@netshadow.at
8
 *
9
 *  This file is part of Nephthys.
10
 *
11
 *  Nephthys is free software: you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation, either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  Nephthys is distributed in the hope that it will be useful,
17
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 *  GNU General Public License for more details.
20
 *
21
 *  You should have received a copy of the GNU General Public License
22
 *  along with Nephthys. If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 ***************************************************************************/
25
26
if(!isset($_SERVER['argv'])) {
27
   die("This script needs to be called from command line.");
28
}
29
30
class NEPHTHYS_WATCH {
31
32
   private $db;
33
   private $parent;
34
   private $verbose;
35
36
   /**
37
    * NEPHTHYS_WATCH constructor
38
    *
39
    * Initialize the NEPHTHYS_WATCH class
40
    */
41
   public function __construct()
42
   {
43
      require_once "nephthys.class.php";
44
45
      $nephthys = new NEPHTHYS;
46
      $this->parent =& $nephthys;
47
      $this->db =& $nephthys->db;
48
49
      $this->verbose = false;
50
51
      $short_options = "";
52
      $short_options.= "h"; /* help */
53
      $short_options.= "v"; /* overwrite */
54
55
      $long_options = array(
56
         "help",
57
         "verbose",
58
      );
59
60
      /* command line option specified? */
61
      if(isset($_SERVER['argc']) && $_SERVER['argc'] > 1) {
62
         /* validate */
63
         $con = new Console_Getopt;
64
         $args = $con->readPHPArgv();
65
         $options = $con->getopt($args, $short_options, $long_options);
66
67
         if(PEAR::isError($options)) {
68
            die ("Error in command line: " . $options->getMessage() . "\n");
69
         }
70
71
         foreach($options[0] as $opt) {
72
            switch($opt[0]) {
73
               case 'h':
74
               case '--help':
75
                  print "nephthys_watch.php - cleanup expired buckets\n"
76
                     ."http://oss.netshadow.at\n"
77
                     ."\n"
78
                     ."   ./nephthys_watch.php <options>\n"
79
                     ."\n"
80
                     ."   -h ... this help text\n"
81
                     ."   -v ... be verbose\n"
82
                     ."\n";
83
                  exit(0);
84
                  break;
85
               case 'v':
86
               case '--verbose':
87
                  $this->verbose = true;
88
                  break;
89
               default:
90
                  $this->parent->_error("invalid option(s) provided. use --help to see possibile options.");
91
                  exit(1);
92
                  break;
93
            }
94
         }
95
      }
96
97
      $this->watch();
98
99
   } // __construct()
100
101
   public function watch()
102
   {
103
      $nephthys_buckets = new NEPHTHYS_BUCKETS;
104
      $expired_buckets = $nephthys_buckets->get_expired_buckets();
105
106
      foreach($expired_buckets as $bucket) {
107
108
         $found_error = false;
109
110
         $bucket_details = $nephthys_buckets->get_bucket_details($bucket);
111
112
         $log_msg =
113
            "Bucket: ". $bucket_details->bucket_name .", ".
114
            "Owner: ". $this->parent->get_user_name($bucket_details->bucket_owner) .", ".
115
            "Expired on: ". date("%c", $bucket_details->bucket_expire) .", ";
116
117
         /* does the bucket-directory still exist? */
118
         if(file_exists($this->parent->cfg->data_path ."/". $bucket_details->bucket_hash)) {
119
            /* lets delete the bucket-directory recursivly */
120
            if(!$nephthys_buckets->del_data_directory($bucket_details->bucket_hash)) {
121
               $this->parent->_error("ERROR: Can't delete bucket directory ". $this->parent->cfg->data_path ."/". $bucket_details->bucket_hash .".");
122
               $found_error = true;
123
            }
124
         }
125
         else {
126
            $this->parent->_error("WARNING: Bucket directory ". $this->parent->cfg->data_path ."/". $bucket_details->bucket_hash ." no longer exists.");
127
         }
128
129
         /* if directory-deletion was successful, delete bucket from database */
130
         if(empty($found_error)) {
131
132
            /* check if deletion on this bucket needs to be notified */
133
            if($bucket_details->bucket_notify_on_expire == 'Y') {
134
               $nephthys_buckets->notify_expired_bucket($bucket);
135
            }
136
            /* delete bucket from database */
137
            $nephthys_buckets->delete_bucket($bucket);
138
139
            if(!empty($this->verbose)) {
140
               $log_msg.= "deleted";
141
            }
142
         }
143
144
         if(empty($found_error) && !empty($this->verbose)) {
145
            $this->parent->_error($log_msg);
146
         }
147
      }
148
149
   } // watch()
150
151
} // NEPHTHYS_WATCH
152
153
$class = new NEPHTHYS_WATCH;
154
155
// vim: set filetype=php expandtab softtabstop=3 tabstop=3 shiftwidth=3 autoindent smartindent:
156
?>
157