Alternc  latest
Alternc logiel libre pour l'hébergement
m_log.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  ----------------------------------------------------------------------
5  LICENSE
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License (GPL)
9  as published by the Free Software Foundation; either version 2
10  of the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  To read the license please visit http://www.gnu.org/copyleft/gpl.html
18  ----------------------------------------------------------------------
19 */
20 
21 /**
22  * This class shows error or access logs of web server to the web panel
23  *
24  * @copyright AlternC-Team 2000-2017 https://alternc.com/
25  */
26 class m_log {
27 
28  /**
29  * List all logs files in a directory
30  */
31  function list_logs_directory($dir) {
32  global $cuid, $msg;
33  $msg->debug("log", "list_logs_directory");
34 
35  $c = array();
36  foreach (glob("${dir}/*log*") as $absfile) {
37  $c[] = array("name" => basename($absfile),
38  "creation_date" => date("F d Y H:i:s", filectime($absfile)),
39  "mtime" => filemtime($absfile),
40  "filesize" => filesize($absfile),
41  "downlink" => urlencode(basename($absfile)),
42  );
43  }
44  usort($c, "m_log::compare_logtime");
45  return $c;
46  }
47 
48 
49  /**
50  * Used by list_logs_directory to sort
51  */
52  private function compare_logname($a, $b) {
53  return strcmp($a['name'], $b['name']);
54  }
55 
56 
57  /**
58  * Used by list_logs_directory to sort
59  */
60  private function compare_logtime($a, $b) {
61  return $b['mtime'] - $a['mtime'];
62  }
63 
64 
65  /**
66  * hook called by the menu class
67  * to add menu to the left panel
68  */
69  function hook_menu() {
70  $obj = array(
71  'title' => _("Logs"),
72  'link' => 'logs_list.php',
73  'pos' => 130,
74  );
75 
76  return $obj;
77  }
78 
79  /**
80  * list all log files in all log directories
81  */
82  function list_logs_directory_all($dirs) {
83  global $msg;
84  $msg->debug("log", "get_logs_directory_all");
85  $c = array();
86  foreach ($dirs as $dir => $val) {
87  $c[$dir] = $this->list_logs_directory($val);
88  }
89  return $c;
90  }
91 
92 
93  function get_logs_directory() {
94  global $cuid, $mem, $msg;
95  $msg->debug("log", "get_logs_directory");
96  // Return an array to allow multiple directory in the future
97  if (defined('ALTERNC_LOGS_ARCHIVE')) {
98  $c = array("dir" => ALTERNC_LOGS_ARCHIVE . "/" . $cuid . "-" . $mem->user["login"]);
99  } else {
100  $c = array("dir" => ALTERNC_LOGS . "/" . $cuid . "-" . $mem->user["login"]);
101  }
102  return $c;
103  }
104 
105 
106  /**
107  * download a log file
108  */
109  function download_link($file) {
110  global $msg;
111  $msg->log("log", "download_link");
112  header("Content-Disposition: attachment; filename=" . $file . "");
113  header("Content-Type: application/force-download");
114  header("Content-Transfer-Encoding: binary");
115  $f = $this->get_logs_directory();
116  $ff = $f['dir'] . "/" . basename($file);
117  set_time_limit(0);
118  readfile($ff);
119  }
120 
121  /**
122  * show the last lines of a file
123  */
124  function tail($file, $lines = 20) {
125  global $msg;
126  $msg->debug("log", "tail");
127  $lines = intval($lines);
128  if ($lines <= 0) {
129  $lines = 20;
130  }
131  $f = $this->get_logs_directory();
132  $ff = $f['dir'] . "/" . basename($file);
133  $out=array();
134  exec("tail -" . $lines . " " . escapeshellarg($ff), $out);
135  return implode("\n", $out);
136  }
137 
138 } /* class m_log */
const ALTERNC_LOGS
Definition: bootstrap.php:13
$mem
Definition: bootstrap.php:71
$msg
Definition: bootstrap.php:75
$c
Definition: bootstrap.php:47
$cuid
Definition: bootstrap.php:43
This class shows error or access logs of web server to the web panel.
Definition: m_log.php:26
hook_menu()
hook called by the menu class to add menu to the left panel
Definition: m_log.php:69
download_link($file)
download a log file
Definition: m_log.php:109
list_logs_directory_all($dirs)
list all log files in all log directories
Definition: m_log.php:82
list_logs_directory($dir)
List all logs files in a directory.
Definition: m_log.php:31
get_logs_directory()
Definition: m_log.php:93
tail($file, $lines=20)
show the last lines of a file
Definition: m_log.php:124
compare_logname($a, $b)
Used by list_logs_directory to sort.
Definition: m_log.php:52
compare_logtime($a, $b)
Used by list_logs_directory to sort.
Definition: m_log.php:60
$val
Definition: tempovars.php:15