made a copy
[atutor.git] / admin / error_logging_bundle.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto                  */
7 /* http://atutor.ca                                                                                                             */
8 /*                                                                                                                                              */
9 /* This program is free software. You can redistribute it and/or                */
10 /* modify it under the terms of the GNU General Public License                  */
11 /* as published by the Free Software Foundation.                                                */
12 /************************************************************************/
13 // $Id$
14
15 define('AT_INCLUDE_PATH', '../include/');
16 require(AT_INCLUDE_PATH.'vitals.inc.php');
17 admin_authenticate(AT_ADMIN_PRIV_ADMIN);
18
19 if (isset($_POST['back'])) {
20         header('Location: error_logging.php');
21         exit;
22 }
23
24 if (isset($_POST['step2'])) { // e-mail bundle
25
26         if ($_POST['email_add'] == '') {
27                 $msg->addError(array('EMPTY_FIELDS', _AT('recipient_address')));
28
29                 header('Location: ' . $_SERVER['PHP_SELF']);
30                 exit;
31         }
32         /* First lets check if they selected any profiles to bundle, run through $POST['file(\d)'] */
33         foreach($_POST as $elem => $val) {
34                 if (strpos($elem, 'file') !== false) {
35                         $found = true;
36                         
37                         $work = $val;
38                         
39                         $date = substr($work, 0, strpos($work, ':'));
40                         $id = substr($work, strpos($work, ':') + 1);
41                         /* Parse the variable */
42                         $profiles{$id} = $date;
43                 }
44         }
45         
46         if ($found === true) {
47                 require(AT_INCLUDE_PATH . 'classes/phpmailer/atutormailer.class.php');
48                 require(AT_INCLUDE_PATH.'classes/zipfile.class.php');
49         
50                 $mail = new ATutorMailer;
51         
52                 $zipfile = new zipfile();
53
54                 $dir_ = AT_CONTENT_DIR . 'logs';
55
56                 foreach($profiles as $elem => $val) {
57                         $store_some;
58
59                         // read the dir where this profile and its associated log files are located
60                         if (!($dir = opendir($dir_ . '/' . $val))) {
61                                         $msg->printNoLookupFeedback('Could not access /content/logs/' . $val . '. Check that the permission for the <strong>Server</string> user are r+w to it');
62                                         require(AT_INCLUDE_PATH.'footer.inc.php'); 
63                         
64                                         exit;
65                                 }
66                                 // Open a read pointer to run through each log date directory getting all the profiles
67                                 while (($file = readdir($dir)) !== false) {
68                 
69                                         if (($file == '.') || ($file == '..') || is_dir($file)) {
70                                                 continue;
71                                         }
72                 
73                                         // any files mathcing the $elem key correspond to this profile
74                                         if (strpos($file, $elem)        !== false) { 
75                                                 $store_some{$dir_ . '/'.  $val . '/' . $file} = $file;
76                                         }
77                                         
78                                 }
79                                 closedir($dir); // clean it up
80                                 
81                                 // The dir pointer is closed lets add to the zip
82                                 foreach($store_some as $val_ => $e)
83                                         $zipfile->add_file(file_get_contents($val_), $e);
84                 }
85                 
86                 $zipfile->close();
87
88                 if ($file_handle = fopen($dir_ . '/bundle.log', "w")) {
89                                 if (!fwrite($file_handle, $zipfile->get_file())) { }
90                 } else { }
91                 fclose($file_handle);
92
93                 $mail->From = $_config['contact_email'];
94                 $mail->addAddress($_POST['email_add']);
95                 $mail->Subject = _AT('log_file_bundle');
96                 $mail->Body    = _AT('see_attached');
97                 $mail->AddAttachment($dir_ . '/bundle.log');
98         
99                 // clean up the file at the redirection point
100                 if(!$mail->Send()) {
101                    $msg->addError('SENDING_ERROR');
102                    /* Make sure the tmp bundle file never exists past the lifetime of the bundle manager page */
103                    unlink($dir_ . '/bundle.log');
104                    header('Location: ' . $_SERVER['PHP_SELF']);
105                    exit;
106                 }
107                 unset($mail);
108
109                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
110                 /* Make sure the tmp bundle file never exists past the lifetime of the bundle manager page */
111                 unlink($dir_ . '/bundle.log');
112                 header('Location: error_logging.php');
113                 exit;
114         } else {
115                 $msg->addError('NO_LOGS_SELECTED');
116                 header('Location: ' . $_SERVER['PHP_SELF']);
117                 exit;
118         }
119 } // else step 1
120
121 require(AT_INCLUDE_PATH.'header.inc.php');
122
123 $msg->printAll();
124
125
126 ?>
127 <h3><?php echo _AT('profile_bundle_select'); ?></h3>
128
129 <p><?php echo _AT('admin_bundle_instructions'); ?></p>
130
131 <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
132 <table class="data" summary="" rules="cols">
133 <thead>
134 <tr>
135         <th span="col"><?php echo _AT('profile');   ?></th>
136         <th span="col"><?php echo _AT('date');      ?></th>
137         <th span="col"><?php echo _AT('bug_count'); ?></th>
138 </tr>
139 </thead>
140 <tfoot>
141 <tr>
142         <td colspan="3">
143                 <label for="email" ><?php echo _AT('recipient_address'); ?></label><br />
144                 <input type="text" id="email" name="email_add" value="" />
145                 <input type="submit" name="step2" value="<?php echo _AT('send_bundle'); ?>" />
146                 <input type="submit" name="back" value="<?php echo _AT('back_to_main'); ?>" />          
147         </td>
148 </tr>
149 </tfoot>
150 <tbody>
151 <?php
152         
153         $dir_ = AT_CONTENT_DIR . 'logs';
154                 
155                 if (!($dir = opendir($dir_))) {
156                         $msg->printNoLookupFeedback('Could not access /content/logs. Check that the permission for the <strong>Server</string> user are r+w to it');
157                         require(AT_INCLUDE_PATH.'footer.inc.php'); 
158                         
159                         exit;
160                 }
161                 
162                 /**
163                  * Run through the logs directory and lets get all the profiles of all the logs of all the dates, sort
164                  * by primary key as date, secondary key is profile name
165                  */ 
166                 $logdirs;
167                  
168                 // loop through folder to get files and directory listing
169                 while (($file = readdir($dir)) !== false) {
170
171                         /* if the name is not a directory */
172                         if( ($file == '.') || ($file == '..')) {
173                                 continue;
174                         }
175
176                         if (is_dir($dir_ . '/' . $file)) {
177                                 $logdirs{$file} = $file; // store the day log dir
178                         }
179                 }
180                 closedir($dir); // clean it up
181
182                 if (empty($logdirs)) { ?>
183                         <tr>
184                                 <td colspan="3"><?php echo _AT('none_found'); ?></td>
185                         </tr>
186                 <?php
187                 } else {
188                 
189                         $count_ = 1;
190                         foreach ($logdirs as $row => $val) {
191                                 $log_profiles; // store all the profiles under the dir /content/logs/$val
192                                 $log_profiles_bug_count; // store the amount of bugs per profile
193                                 
194                                 if (!($dir = opendir($dir_ . '/' . $val))) {
195                                         $msg->printNoLookupFeedback('Could not access /content/logs/' . $val . '. Check that the permission for the <strong>Server</string> user are r+w to it');
196                                         require(AT_INCLUDE_PATH.'footer.inc.php'); 
197                         
198                                         exit;
199                                 }
200                                 // Open a read pointer to run through each log date directory getting all the profiles
201                                 while (($file = readdir($dir)) !== false) {
202                 
203                                         if (($file == '.') || ($file == '..') || is_dir($file)) {
204                                                 continue;
205                                         }
206                 
207                                         if (strpos($file, 'profile')    !== false) { // found a profile, store its md5 key identifier
208                                                 $tmp_ = substr($file, strpos($file, '_') + 1);
209                                                 $tmp_ = substr($tmp_, 0, strpos($tmp_, '.log.php'));
210                                                 $log_profiles{$file} = $tmp_;
211                                         }
212                                         
213                                 }
214                                 closedir($dir); // clean it up
215                                 
216                                 /**
217                                  * Open a read pointer to run through each log date directory getting all the bugs associated
218                                  * all the profiles in $log_profiles
219                                  */
220                                 if (empty($log_profiles)) { 
221                                         $msg->printNoLookupFeedback('Warning. No profile found in ' . $dir_ . '/' . $val);
222                                         require(AT_INCLUDE_PATH.'footer.inc.php'); 
223                         
224                                         exit;
225                                 }
226                         
227                                 foreach ($log_profiles as $elem => $val_) {
228                                         $count = 0;
229                                         
230                                         /* for each profile get the number of bugs associated with it */
231                                         if (!($dir = opendir($dir_ . '/' . $val))) {
232                                                 $msg->printNoLookupFeedback('Could not access /content/logs' . $val . '. Check that the permission for the <strong>Server</string> user are r+w to it');
233                                                 require(AT_INCLUDE_PATH.'footer.inc.php'); 
234                         
235                                                 exit;
236                                         }
237                                         
238                                         while (($file = readdir($dir)) !== false) {
239                         
240                                                 // make sure we ignore profiles too!, just look at bug files
241                                                 if( ($file == '.') || ($file == '..') || is_dir($file) || (strpos($file, 'profile') !== false)) {
242                                                         continue;
243                                                 }
244
245                                                 // found a bug that maps to $val_ md5 profile identifer
246                                                 if (strpos($file, $val_)        !== false) { 
247                                                         $count++;
248                                                 }
249                                         }
250                                         closedir($dir);
251
252                                         // store the amount of bugs associated with profile
253                                         $log_profiles_bug_count{$val}[$val_] = $count;
254                                 }
255                                 $log_profiles = array();
256                         }
257                         /**
258                          * At this point ($log_profiles => key) = ($log_profiles_bug_count => key).
259                          *
260                          * Lets print out <td> rows corresponding to all profiles found in the following format:
261                          *
262                          * Profile name, profile date, profile bug count. 
263                          */             
264                         foreach ($log_profiles_bug_count as $day => $profile) :
265                                  foreach ($profile as $stamp => $total) :
266                         ?>
267                                         <tr onmousedown="document.form1['<?php echo $stamp.$day; ?>'].checked = !document.form1['<?php echo $stamp.$day; ?>'].checked;">
268                                                 <td><input type="checkbox" id="<?php echo $stamp.$day; ?>" value="<?php echo $day.':'.$stamp; ?>" name="file<?php echo $count_; ?>" onmouseup="this.checked=!this.checked" /><?php echo $count_; ?></td>                                        
269                                                 <td><?php echo $day; ?></td>
270                                                 <td><?php echo $total; ?></td>
271                                         </tr>
272                         <?php
273                                         $count_++;
274                                 endforeach;
275                         endforeach;
276                 }
277         
278 ?>
279 </tbody>
280 </table>
281 </form>
282
283 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>