Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
log_manager_edit_fns.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset_edit/asset_edit_fns.inc';
18 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
19 
20 
34 {
35 
36 
42  {
43  $this->Asset_Edit_Fns();
44 
45  // disable all static screens
46  $this->static_screens = Array();
47 
48  // enable just the details
49  $this->static_screens['details']['name'] =translate('details');
50  $this->static_screens['details']['force_unlock'] = FALSE;
51  $this->static_screens['details']['lock_type'] = 'none';
52 
53  }//end constructor
54 
55 
66  function paintLogList(&$lm, &$o, $prefix)
67  {
68  $download_file = array_get_index($_POST, $prefix.'_download_file', '');
69  if ($download_file !== '') {
70  $this->processDownloadLogFile($lm, $o, $prefix);
71  }
72 
73  $write_access = $lm->writeAccess();
74 
75  $o->openRaw();
76  $logs = $lm->getLogs();
77  if (empty($logs)) {
78  echo '<h2>'.translate('no_logs_found').'</h2>';
79  return $write_access;
80  }
81 
82  ?>
83  <script type="text/javascript">
84  //<![CDATA[
85  var prefix = "<?php echo $prefix; ?>";
86  var communicator_url = "<?php echo $lm->getCommunicatorUrl(); ?>";
87  var action_prefix = prefix + "_action";
88  var action_value = prefix + "_value";
89  var logname_index = prefix + "_log";
90  var offset_index = prefix + "_offset";
91  var num_lines_index = prefix + "_num_lines";
92 
93  var refresh_interval = 30000; //seconds
94  var last_offset = null;
95  var num_lines = 50;
96 
97  var log_refresh = true;
98  var refresher = null;
99  var current_log = null;
100 
104  function monitorLog(logname)
105  {
106  if (logname == null) {
107  logname = current_log;
108  } else {
109  current_log = logname;
110  }
111 
112  if (logname == null) {
113  return false;
114  }
115 
116  document.getElementById('sq-log-monitor-initial-message').style.display = 'none';
117  document.getElementById('sq-log-monitor-containter').style.display = 'block';
118  setIdValue('sq-log-name', logname);
119  document.getElementById('sq-log-num-lines').defaultValue = num_lines;
120 
121  // initial setting
122  if (log_refresh && null === refresher) {
123  enableRefresh();
124  }
125 
126  last_offset = null;
127  loadLog();
128 
129  return false;
130 
131  }//end monitorLog()
132 
133 
134 
140  function forceRefresh()
141  {
142  loadLog();
143  enableRefresh();
144 
145  }//end forceRefresh()
146 
147 
151  function loadLog()
152  {
153  request_url = communicator_url+'&'+action_prefix+'=monitor&'+logname_index+'='+current_log+'&rand='+ Math.random()+'&'+num_lines_index+'='+num_lines;
154  if (null !== last_offset) {
155  request_url = request_url+'&'+offset_index+'='+last_offset;
156  }
157 
158  document.getElementById('sq-log-monitor').src = request_url;
159  return true;
160 
161  }//end loadLog()
162 
163 
165 
168  function setRefreshInterval(interval)
169  {
170  var value = parseInt(interval);
171  if (!isNaN(value)) {
172  refresh_interval = value * 1000;
173  enableRefresh();
174  }
175 
176  }//end setRefreshInterval()
177 
178 
182  function setNumLines(new_num_lines)
183  {
184  var value = parseInt(new_num_lines);
185  if (!isNaN(value)) {
186  num_lines = value;
187  }
188  forceRefresh();
189  }//end setRefreshInterval()
190 
201  function setLastRefreshInfo(date_string, num_rows, offset)
202  {
203  setIdValue('sq-log-last-refresh', date_string);
204  setIdValue('sq-log-total-rows', num_rows);
205  setLastOffset(offset);
206 
207  }//end setLastRefreshInfo()
208 
209 
210  function setIdValue(id, value)
211  {
212  document.getElementById(id).innerHTML = value;
213 
214  }//end setIdValue()
215 
216 
226  function setLastOffset(value)
227  {
228  var value = parseInt(value);
229  if (!isNaN(value)) {
230  last_offset = value;
231  }
232 
233  }//end setLastOffset()
234 
235 
243  function enableRefresh()
244  {
245  if (null !== refresher) {
246  clearTimeout(refresher);
247  }
248  refresher = setInterval(loadLog, refresh_interval);
249 
250  setIdValue('sq-log-refresh-status', 'ON');
251  document.getElementById('sq-log-refresh-enabler').value=js_translate('disable');
252 
253  log_refresh = true;
254 
255  }//end enableRefresh()
256 
257 
263  function disableRefresh()
264  {
265  if (null !== refresher) {
266  clearInterval(refresher);
267  refresher = null;
268  }
269 
270  setIdValue('sq-log-refresh-status', 'OFF');
271  document.getElementById('sq-log-refresh-enabler').value=js_translate('enable');
272 
273  log_refresh = false;
274 
275  }//end disableRefresh()
276 
277 
281  function toggleLogRefreshStatus()
282  {
283 
284  if (!log_refresh) {
285  enableRefresh();
286  } else {
287  disableRefresh();
288  }
289 
290  return true;
291 
292  }//end toggleLogRefreshStatus()
293 
294 
295  //]]>
296  </script>
297  <table class="sq-backend-table">
298  <tr>
299  <th style="width: 10%"><?php echo translate('log_name'); ?></th>
300  <th style="width: 50%"><?php echo translate('info'); ?></th>
301  <th>Action</th>
302  </tr>
303  <?php
304  foreach ($logs as $logname => $rotation) {
305  $filepath = SQ_LOG_PATH.'/'.$logname.SQ_CONF_LOG_EXTENSION;
306  if (!file_exists($filepath)) continue;
307 
308  $last_modified = date('G:i:s - M d', filemtime($filepath));
309 
310  $last_rotated = array_get_index(array_get_index($rotation,'rotation'), 'last_timestamp');
311  if (empty($last_rotated)) {
312  $last_rotated = translate('never');
313  } else {
314  $last_rotated = date('G:i:s - M d', $last_rotated);
315  }
316 
317  ?>
318  <tr>
319  <td><strong><?php echo $logname; ?></strong></td>
320  <td>
321  <?php echo translate('modified'); ?>: <strong><?php echo $last_modified; ?></strong>
322  <br />
323  <?php echo translate('core_log_manager_current_logfile_size'); ?>: <strong><?php echo easy_filesize(filesize($filepath)); ?></strong>
324  <br />
325  <?php echo translate('rotated'); ?>: <strong><?php echo $last_rotated; ?></strong>
326  </td>
327  <td>
328  <input type="button" onclick="monitorLog('<?php echo $logname; ?>');" value="<?php echo translate('monitor'); ?>" />
329  <?php normal_button($prefix.'_export_to_csv', translate('download_file'), 'this.form.'.$prefix.'_download_file.value = \''.$logname.'\'; this.form.submit(); SQ_FORM_SUBMITTED = false; this.form.'.$prefix.'_download_file.value = 0; return true;');
330  ?>
331  </td>
332  </tr>
333  <?php
334  }
335  echo '</table>';
336  hidden_field($prefix.'_download_file', 0);
337 
338  $o->closeRaw();
339  $o->closeSection();
340 
341  $o->openSection(translate('log_monitor'));
342  $o->openRaw('');
343  ?>
344  <div id="sq-log-monitor-initial-message">
345  <?php echo translate('moniter_logs_instructions'); ?>
346  </div>
347  <div id="sq-log-monitor-containter" style="display: none;">
348  <table class="sq-backend-table">
349  <tr>
350  <td>
351  <?php echo translate('log'); ?>: <strong id="sq-log-name"></strong>
352  &nbsp;&nbsp;&nbsp;
353  <?php echo translate('loaded'); ?>: <strong id="sq-log-last-refresh"></strong>
354  &nbsp;&nbsp;&nbsp;
355  <?php echo translate('rows_displayed'); ?>: <strong id="sq-log-total-rows"></strong>
356 
357  </td>
358  <td style="text-align: right">
359  <?php echo translate('max_rows'); ?>:
360  <select id="sq-log-num-lines" onchange="setNumLines(this.value)">
361  <option value="10">10</option>
362  <option value="50" selected="selected">50</option>
363  <option value="100">100</option>
364  <option value="500">500</option>
365  <option value="1000">1000</option>
366 
367  </select>
368  <?php echo translate('auto-refresh'); ?>: <strong id="sq-log-refresh-status">&nbsp;</strong>
369  <input type="button" onclick="toggleLogRefreshStatus();" id="sq-log-refresh-enabler" value="" />
370  &nbsp;&nbsp;&nbsp;
371  <?php echo translate('refresh_interval'); ?>:
372  <select name="sq-log-refresh-interval" id="sq-log-refresh-interval" onchange="setRefreshInterval(this.value)">
373  <option value="15">15 <?php echo translate('sec'); ?></option>
374  <option value="30" selected="selected">30 <?php echo translate('sec'); ?></option>
375  <option value="60">1 <?php echo translate('min'); ?></option>
376  <option value="300">5 <?php echo translate('min'); ?></option>
377  </select>
378  &nbsp;&nbsp;&nbsp;
379  <input type="button" onclick="forceRefresh();" id="sq-log-refresh-force" value="<?php echo translate('refresh_now'); ?>" />
380  </td>
381  </tr>
382  </table>
383  <br />
384  <iframe id="sq-log-monitor" src="" style="width: 100%; height: 400px; border: 1px solid #C3C3C3;"></iframe>
385  </div>
386  <?php
387  $o->closeRaw();
388  $o->closeSection();
389 
390  return FALSE;
391 
392  }//end paintLogList()
393 
394 
404  public function processDownloadLogFile(Log_Manager $asset, Backend_Outputter $o, $prefix)
405  {
406  if ($asset->writeAccess() === FALSE) {
407  return FALSE;
408  }
409 
410  $download_file = array_get_index($_POST, $prefix.'_download_file', '');
411 
412  if ($download_file !== '') {
413  // Get all the valid log files in that directory, and see whether our
414  // file is one of them
415  $log_file = SQ_DATA_PATH.'/private/logs/'.$download_file.SQ_CONF_LOG_EXTENSION;
416  $valid_log_files = glob(SQ_DATA_PATH.'/private/logs/*'.SQ_CONF_LOG_EXTENSION);
417 
418  if (array_search($log_file, $valid_log_files) !== FALSE) {
419  $fp = fopen($log_file, 'r');
420  if ($fp === FALSE) {
421  trigger_localised_error('CORE0288', E_USER_WARNING, $download_file);
422  return FALSE;
423  }
424 
425  // Only do the headers if we were able to read the file
426  header('Content-Type: text/plain');
427  header('Content-Disposition: attachment; filename="'.$download_file.SQ_CONF_LOG_EXTENSION.'"');
428  header('Content-Length: '.filesize($log_file));
429  header('Last-Modified: '.gmdate('r', filemtime($log_file)));
430 
431  while (feof($fp) === FALSE) {
432  $chunk = fread($fp, 4096);
433  if ($chunk === FALSE) {
434  // Uh oh...
435  exit(1);
436  } else {
437  echo $chunk;
438  }
439  }
440  fclose($fp);
441 
442  exit(0);
443  }
444 
445  }//end if download file exists
446 
447  return FALSE;
448 
449  }//end processDownloadLogFile()
450 
451 
452 }//end class
453 
454 ?>