Browse code

Webalizer/Statistik-Funktionalität

git-svn-id: https://svn.schokokeks.org/repos/tools/webinterface/trunk@1615 87cf0b9e-d624-0410-a070-f6ee81989793

bernd authored on22/01/2010 07:48:35
Showing11 changed files
1 1
new file mode 100644
2 2
Binary files /dev/null and b/images/chart_bar.png differ
3 3
new file mode 100644
4 4
Binary files /dev/null and b/images/lock.png differ
5 5
new file mode 100644
6 6
Binary files /dev/null and b/images/world.png differ
... ...
@@ -68,3 +68,15 @@ function icon_delete($title = '')
68 68
 
69 69
 
70 70
 
71
+function other_icon($filename, $title = '')
72
+{
73
+  global $prefix;
74
+  return "<img src=\"{$prefix}images/{$filename}\" style=\"height: 16px; width: 16px;\" alt=\"{$title}\" title=\"{$title}\" />";
75
+}
76
+
77
+
78
+
79
+
80
+
81
+
82
+
... ...
@@ -199,9 +199,10 @@ $form .= "
199 199
         <option value=\"anonymous\" ".($vhost['logtype'] == 'anonymous' ? 'selected="selected"' : '')." >anonymisiert</option>
200 200
         <option value=\"default\" ".($vhost['logtype'] == 'default' ? 'selected="selected"' : '')." >vollständige Logfile</option>
201 201
       </select><br />
202
-      <input type=\"checkbox\" id=\"errorlog\" name=\"errorlog\" value=\"1\" ".($vhost['errorlog'] == 1 ? ' checked="checked" ' : '')." />&#160;<label for=\"errorlog\">Fehlerprotokoll (error_log) einschalten</label>
202
+      <input type=\"checkbox\" id=\"errorlog\" name=\"errorlog\" value=\"1\" ".($vhost['errorlog'] == 1 ? ' checked="checked" ' : '')." />&#160;<label for=\"errorlog\">Fehlerprotokoll (error_log) einschalten</label><br />
203
+      <input type=\"checkbox\" id=\"stats\" name=\"stats\" value=\"1\" ".($vhost['stats'] != NULL ? ' checked="checked" ' : '')." />&#160;<label for=\"stats\">Statistiken/Auswertungen erzeugen</label>
203 204
     </div>
204
-    <p>Logfiles werden unter <b>/var/log/apache2/".$_SESSION[userinfo][username]."</b> abgelegt.</p>
205
+    <p>Logfiles werden unter <b>/var/log/apache2/".$_SESSION['userinfo']['username']."</b> abgelegt.</p>
205 206
 </div>
206 207
     ";
207 208
 
... ...
@@ -12,7 +12,7 @@ require_once("certs.php");
12 12
 function list_vhosts()
13 13
 {
14 14
   $uid = (int) $_SESSION['userinfo']['uid'];
15
-  $result = db_query("SELECT vh.id,fqdn,domain,docroot,docroot_is_default,php,vh.certid AS cert, vh.ssl, vh.options,logtype,errorlog,IF(dav.id IS NULL OR dav.type='svn', 0, 1) AS is_dav,IF(dav.id IS NULL OR dav.type='dav', 0, 1) AS is_svn, IF(webapps.id IS NULL, 0, 1) AS is_webapp FROM vhosts.v_vhost AS vh LEFT JOIN vhosts.dav ON (dav.vhost=vh.id) LEFT JOIN vhosts.webapps ON (webapps.vhost = vh.id) WHERE uid={$uid} ORDER BY domain,hostname");
15
+  $result = db_query("SELECT vh.id,fqdn,domain,docroot,docroot_is_default,php,vh.certid AS cert, vh.ssl, vh.options,logtype,errorlog,IF(dav.id IS NULL OR dav.type='svn', 0, 1) AS is_dav,IF(dav.id IS NULL OR dav.type='dav', 0, 1) AS is_svn, IF(webapps.id IS NULL, 0, 1) AS is_webapp, stats FROM vhosts.v_vhost AS vh LEFT JOIN vhosts.dav ON (dav.vhost=vh.id) LEFT JOIN vhosts.webapps ON (webapps.vhost = vh.id) WHERE uid={$uid} ORDER BY domain,hostname");
16 16
   $ret = array();
17 17
   while ($item = mysql_fetch_assoc($result))
18 18
     array_push($ret, $item);
... ...
@@ -43,6 +43,7 @@ function empty_vhost()
43 43
   $vhost['ipv4'] = NULL;
44 44
 
45 45
   $vhost['options'] = '';
46
+  $vhost['stats'] = NULL;
46 47
   return $vhost;
47 48
 }
48 49
 
... ...
@@ -200,10 +201,10 @@ function save_vhost($vhost)
200 201
     system_failure('$vhost kein array!');
201 202
   $id = (int) $vhost['id'];
202 203
   $hostname = maybe_null($vhost['hostname']);
203
-  $domain = (int) $vhost['domainid'];
204
+  $domain = (int) $vhost['domain_id'];
204 205
   if ($domain == 0)
205 206
     system_failure('$domain == 0');
206
-  if ($vhost['domainid'] == -1)
207
+  if ($vhost['domain_id'] == -1)
207 208
     $domain = 'NULL';
208 209
   $docroot = maybe_null($vhost['docroot']);
209 210
   $php = maybe_null($vhost['php']);
... ...
@@ -230,14 +231,15 @@ function save_vhost($vhost)
230 231
   {
231 232
     $ipv4 = maybe_null($vhost['ipv4']);
232 233
   }
234
+  $stats = maybe_null($vhost['stats']);
233 235
 
234 236
   if ($id != 0) {
235 237
     logger(LOG_INFO, 'modules/vhosts/include/vhosts', 'vhosts', 'Updating vhost #'.$id.' ('.$vhost['hostname'].'.'.$vhost['domain'].')');
236
-    db_query("UPDATE vhosts.vhost SET hostname={$hostname}, domain={$domain}, docroot={$docroot}, php={$php}, `ssl`={$ssl}, logtype={$logtype}, errorlog={$errorlog}, certid={$cert}, ipv4={$ipv4}, options='{$options}' WHERE id={$id} LIMIT 1");
238
+    db_query("UPDATE vhosts.vhost SET hostname={$hostname}, domain={$domain}, docroot={$docroot}, php={$php}, `ssl`={$ssl}, logtype={$logtype}, errorlog={$errorlog}, certid={$cert}, ipv4={$ipv4}, options='{$options}', stats={$stats} WHERE id={$id} LIMIT 1");
237 239
   }
238 240
   else {
239 241
     logger(LOG_INFO, 'modules/vhosts/include/vhosts', 'vhosts', 'Creating vhost '.$vhost['hostname'].'.'.$vhost['domain'].'');
240
-    $result = db_query("INSERT INTO vhosts.vhost (user, hostname, domain, docroot, php, `ssl`, logtype, errorlog, certid, ipv4, options) VALUES ({$_SESSION['userinfo']['uid']}, {$hostname}, {$domain}, {$docroot}, {$php}, {$ssl}, {$logtype}, {$errorlog}, {$cert}, {$ipv4}, '{$options}')");
242
+    $result = db_query("INSERT INTO vhosts.vhost (user, hostname, domain, docroot, php, `ssl`, logtype, errorlog, certid, ipv4, options, stats) VALUES ({$_SESSION['userinfo']['uid']}, {$hostname}, {$domain}, {$docroot}, {$php}, {$ssl}, {$logtype}, {$errorlog}, {$cert}, {$ipv4}, '{$options}', {$stats})");
241 243
     $id = mysql_insert_id();
242 244
   }
243 245
   $oldvhost = get_vhost_details($id);
... ...
@@ -293,10 +295,10 @@ function save_alias($alias)
293 295
     system_failure('$alias kein array!');
294 296
   $id = (isset($alias['id']) ? (int) $alias['id'] : 0);
295 297
   $hostname = maybe_null($alias['hostname']);
296
-  $domain = (int) $alias['domainid'];
298
+  $domain = (int) $alias['domain_id'];
297 299
   if ($domain == 0)
298 300
     system_failure('$domain == 0');
299
-  if ($alias['domainid'] == -1)
301
+  if ($alias['domain_id'] == -1)
300 302
     $domain = 'NULL';
301 303
   $vhost = get_vhost_details( (int) $alias['vhost']);
302 304
   $options = mysql_real_escape_string( $alias['options'] );
... ...
@@ -6,6 +6,7 @@ if ($role & ROLE_SYSTEMUSER)
6 6
 {
7 7
     $menu["vhosts_vhosts"] = array("label" => "Webserver", "file" => "vhosts", "weight" => 1);
8 8
     $menu["vhosts_certs"] = array("label" => "SSL-Zertifikate", "file" => "certs", "weight" => 10, "submenu" => "vhosts_vhosts");
9
+    $menu["vhosts_stats"] = array("label" => "Zugriffs-Statistiken", "file" => "stats", "weight" => 12, "submenu" => "vhosts_vhosts");
9 10
 }
10 11
 
11 12
 ?>
... ...
@@ -24,11 +24,11 @@ if ($_GET['action'] == 'edit')
24 24
 
25 25
   $hostname = filter_input_hostname($_POST['hostname']);
26 26
 
27
-  $domainid = (int) $_POST['domain'];
28
-  if ($domainid != -1) {
27
+  $domain_id = (int) $_POST['domain'];
28
+  if ($domain_id != -1) {
29 29
     $domain = new Domain( (int) $_POST['domain'] );
30 30
     $domain->ensure_userdomain();
31
-    $domainid = $domain->id;
31
+    $domain_id = $domain->id;
32 32
   }
33 33
 
34 34
   if (! (isset($_POST['options']) && is_array($_POST['options'])))
... ...
@@ -111,6 +111,7 @@ if ($_GET['action'] == 'edit')
111 111
 
112 112
   $ipv4 = (isset($_POST['ipv4']) ? $_POST['ipv4'] : NULL);
113 113
 
114
+
114 115
   $logtype = '';
115 116
   switch ($_POST['logtype']) {
116 117
     case 'anonymous':
... ...
@@ -126,6 +127,18 @@ if ($_GET['action'] == 'edit')
126 127
   if (isset($_POST['errorlog']) and ($_POST['errorlog'] == 1))
127 128
     $errorlog = 1;
128 129
 
130
+
131
+  if (isset($_POST['stats']) && $_POST['stats'] == 1)
132
+  {
133
+    if ($vhost['stats'] == NULL)
134
+      $vhost['stats'] = 'private';
135
+  }
136
+  else
137
+    $vhost['stats'] = NULL;
138
+
139
+  if ($logtype == '')
140
+    $vhost['stats'] = NULL;
141
+  
129 142
   DEBUG("PHP: {$php} / Logging: {$logtype}");
130 143
 
131 144
   $old_options = explode(',', $vhost['options']);
... ...
@@ -144,7 +157,7 @@ if ($_GET['action'] == 'edit')
144 157
   DEBUG('New options: '.$options);
145 158
 
146 159
   $vhost['hostname'] = $hostname;
147
-  $vhost['domainid'] = $domainid;
160
+  $vhost['domain_id'] = $domain_id;
148 161
   $vhost['docroot'] = $docroot;
149 162
   $vhost['php'] = $php;
150 163
   $vhost['ssl'] = $ssl;
151 164
new file mode 100644
... ...
@@ -0,0 +1,59 @@
1
+<?php
2
+
3
+require_once("inc/base.php");
4
+require_once("vhosts.php");
5
+
6
+require_role(ROLE_SYSTEMUSER);
7
+
8
+$section = 'vhosts_stats';
9
+
10
+// Stellt sicher, dass der angegebene VHost dem User gehört
11
+$vhost = get_vhost_details($_REQUEST['vhost']);
12
+
13
+if (! isset($_REQUEST['file']))
14
+{
15
+  $_REQUEST['file'] = 'index.html';
16
+}
17
+
18
+if (!preg_match('/((daily_|hourly_|ctry_)?(usage|agent|search|ref|url|site)(_[0-9]+)?|index)\.(png|html)/', $_REQUEST['file']))
19
+{
20
+  system_failure("Ungültiger Dateiname: »".filter_input_general($_REQUEST['file'])."«");
21
+}
22
+
23
+$path = '/home/stats/webalizer/data/' . $vhost['fqdn'];
24
+$file = $path . '/' . $_REQUEST['file'];
25
+
26
+if ( is_file($file) )
27
+{
28
+  DEBUG("opening file ".$file);
29
+  if (preg_match('/\.png/', $file))
30
+  {
31
+    //Binärdateien
32
+    header("Content-Type: image/png");
33
+    header("Content-Length: " . filesize($file));
34
+    header("Content-Transfer-Encoding: binary\n");
35
+    
36
+    $fp = fopen($file, "r");
37
+    fpassthru($fp);
38
+    die();
39
+  }
40
+
41
+  $html = iconv('latin9', 'utf8', file_get_contents($file));
42
+  DEBUG($html);
43
+  // Nur content vom HTML
44
+  $html = preg_replace(':^.*?<BODY[^>]*>(.*)</BODY>.*$:si', '$1', $html);
45
+  DEBUG($html);
46
+
47
+  // Bilder rewriten
48
+  $html = preg_replace('/SRC="((daily_|hourly_|ctry_)?usage(_[0-9]+)?\.png)"/', 'SRC="showstats?vhost='.$vhost['id'].'&amp;file=$1"', $html);
49
+  // Interne Links rewriten
50
+  $html = preg_replace('!HREF="(./)?((usage|agent|search|ref|url|site|index)(_[0-9]+)?\.html)"!', 'HREF="showstats?vhost='.$vhost['id'].'&amp;file=$2"', $html);
51
+  output($html);
52
+}
53
+else
54
+{
55
+  system_failure("Die Statistiken konnten nicht gefunden werden. Beachten Sie bitte, dass die Erstellung regelmäßig nachts geschieht. Neu in Auftrag gegebene Statistiken können Sie erst am darauffolgenden Tag betrachten.");
56
+}
57
+
58
+
59
+
0 60
new file mode 100644
... ...
@@ -0,0 +1,108 @@
1
+<?php
2
+
3
+require_once("inc/icons.php");
4
+
5
+require_once("vhosts.php");
6
+
7
+require_role(ROLE_SYSTEMUSER);
8
+
9
+
10
+
11
+if (isset($_REQUEST['vhost']))
12
+{
13
+  $v = get_vhost_details($_REQUEST['vhost']);
14
+
15
+  if (isset($_REQUEST['public']))
16
+  {
17
+    $v['stats'] = ($_REQUEST['public'] == 1) ? 'public' : 'private';
18
+  }
19
+  if (isset($_REQUEST['action']))
20
+  {
21
+    if ($_REQUEST['action'] == 'delete')
22
+      $v['stats'] = NULL;
23
+    elseif ($_REQUEST['action'] == 'new')
24
+      check_form_token('stats_new');
25
+  }
26
+  save_vhost($v);
27
+  redirect('stats');
28
+}
29
+else
30
+{
31
+
32
+$all_vhosts = list_vhosts();
33
+$stats_vhosts = array();
34
+
35
+foreach ($all_vhosts AS $v)
36
+  if ($v['stats'])
37
+    $stats_vhosts[] = $v;
38
+
39
+
40
+$title = "Zugriffs-Statistiken";
41
+
42
+output('<h3>Zugriffs-Statistiken</h3>
43
+<p>Um die Reichweite und das Publikum Ihrer Internet-Seiten besser einschätzen zu können, besteht die Möglichkeit aus den ggf. vorhandenen Webserver-Logfiles grafisch aufbereitete Statistiken erstellen zu lassen.</p>
44
+
45
+<h3>Statistiken für Ihre Seiten</h3>
46
+');
47
+
48
+if (count($stats_vhosts) > 0)
49
+{
50
+  output('
51
+  <table><tr><th>Für Website</th><th>Öffentlich abrufbar?</th><th>Operationen</th></tr>
52
+  ');
53
+
54
+  foreach ($stats_vhosts AS $v)
55
+  {
56
+    output("<tr>");
57
+    output("<td>".internal_link('showstats', $v['fqdn'], "vhost={$v['id']}")."</td>");
58
+
59
+    if ($v['stats'] == 'public')
60
+      output("<td><a href=\"http://stats.schokokeks.org/{$v['fqdn']}\">".icon_enabled("Diese Statistiken können von jedermann aufgerufen werden. Klicken Sie hier um die öffentliche Version zu sehen.")."</a></td>");
61
+    else
62
+      output("<td>".icon_disabled("Diese Statistiken können nur hier im Webinterface betrachtet werden.")."</td>");
63
+    
64
+    output("<td>");
65
+    if ($v['stats'] == 'public')
66
+      output(internal_link("", other_icon("lock.png", "Statistiken nicht mehr öffentlich anzeigen"), "vhost={$v['id']}&public=0"));
67
+    else
68
+      output(internal_link("", other_icon("world.png", "Statistiken veröffentlichen"), "vhost={$v['id']}&public=1"));
69
+    output(" &#160; ".internal_link("", icon_delete("Diese Statistiken löschen"), "vhost={$v['id']}&action=delete")."</td>");
70
+    output("</tr>");
71
+  
72
+  }
73
+  output('</table');
74
+}
75
+else
76
+  output('<em>Für Ihre Seiten werden bisher keine Statistiken erzeugt</em>');
77
+
78
+
79
+output("<h3>Weitere Statistiken</h3>");
80
+
81
+$sel = array();
82
+foreach ($all_vhosts AS $v)
83
+{
84
+  if ($v['logtype'])
85
+  {
86
+    $found = False;
87
+    foreach ($stats_vhosts AS $s)
88
+      if ($s['id'] == $v['id'])
89
+        $found = True;
90
+    if (! $found)
91
+      $sel[$v['id']] = $v['fqdn'];
92
+  }
93
+}
94
+
95
+
96
+output(html_form('stats_new', '', 'action=new', "<p>".html_select("vhost", $sel).'<br/>
97
+<input type="radio" name="public" id="public_0" value="0" checked="checked" /><label for="public_0"> Statistiken hier im Webinterface anzeigen</label><br />
98
+<input type="radio" name="public" id="public_1" value="1" /><label for="public_1"> Statistiken unter stats.schokokeks.org veröffentlichen (Ohne Passwortschutz)</label><br />
99
+<input type="submit" value="Neue Statistiken erzeugen" /></p>
100
+'));
101
+
102
+
103
+output('
104
+
105
+<p><strong>Hinweis:</strong> Die Statistiken werden mindestens täglich erzeugt. Bis zum ersten Durchlauf nach der Aktivierung der Statistik wird der obige Link eine Fehlermeldung erzeugen. Bitte warten Sie mindestens einen Tag ab bevor Sie die Statistik zum ersten Mal aufrufen.</p>
106
+');
107
+
108
+}
... ...
@@ -58,7 +58,8 @@ if (count($vhosts) > 0)
58 58
       else
59 59
         $logfiles .= ' und Fehler';
60 60
     }
61
-    output("<td>{$logfiles}</td>");
61
+    $stats = $vhost['stats'] ? internal_link("showstats", other_icon("chart_bar.png", "Statistiken anzeigen"), "vhost={$vhost['id']}").' ' : '';
62
+    output("<td>{$stats}{$logfiles}</td>");
62 63
   
63 64
     if ($vhost['ssl'] == 'http')
64 65
     {