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