show awstats command
[awstats-helper.git] / functions.php
1 <?php
2 require 'config.php';
3
4 function aws_sites()
5 {
6     $sites = array();
7     foreach (glob($GLOBALS['confdir'] . '/awstats.*.conf') as $file) {
8         $sites[] = preg_replace('/^.*awstats.(.+).conf$/', '\\1', $file);
9     }
10     return $sites;
11 }
12
13 function aws_historic($site)
14 {
15     $dates = array();
16     foreach (glob($GLOBALS['awdata'] . '/awstats*.' . $site . '.txt') as $file) {
17         $date = explode(
18             '-',
19             preg_replace(
20                 '/^.*\/awstats([0-9]{2})([0-9]{4})\\.' . $site . '.txt$/',
21                 '\\2-\\1',
22                 $file
23             )
24         );
25         count($date) == 2 && $dates[] = $date;
26     }
27     return $dates;
28 }
29
30 function aws_createHtml($site, $last = false, $year = null, $month = null)
31 {
32     if (!is_dir($GLOBALS['htmldir'])) {
33         die("HTML directory " . $GLOBALS['htmldir'] . " does not exist\n");
34     }
35
36     if ($year !== null && $month !== null) {
37         //already set
38     } else if ($last) {
39         $month = date('m', strtotime('last month'));
40         $year  = date('Y', strtotime('last month'));
41     } else {
42         $month = date('m');
43         $year  = date('Y');
44     }
45
46     $dir = $GLOBALS['htmldir'] . '/' . $site . '/' . $year . '-' . $month . '/';
47     if (!is_dir($dir)) {
48         mkdir($dir, 0777, true);
49     }
50     $cmd = $GLOBALS['awsbstp']
51         . ' -config=' . escapeshellarg($site)
52         . ' -awstatsprog=' . escapeshellarg($GLOBALS['awstats'])
53         . ' -update'
54         . ' -month=' . $month
55         . ' -year=' . $year
56         . ' -dir=' . escapeshellarg($dir);
57     echo $cmd . "\n";
58     passthru($cmd);
59
60     if (!$last) {
61         aws_updateLink($site, $dir);
62     }
63 }
64
65 function aws_updateLink($site, $targetdir)
66 {
67     $curdir = $GLOBALS['htmldir'] . '/' . $site . '/current';
68     unlink($curdir);
69     symlink($targetdir, $curdir);
70 }
71
72 function aws_createIndex($site)
73 {
74     $dirs = glob($GLOBALS['htmldir'] . '/' . $site . '/*', GLOB_ONLYDIR);
75     sort($dirs);
76     $links = '';
77     foreach ($dirs as $dir) {
78         $name = basename($dir);
79         $links .= '<li><a href="' . $name . '/awstats.' . $site . '.html">'
80             . $name . '</a></li>';
81     }
82     $html = <<<XML
83 <?xml version="1.0" encoding="utf-8"?>
84 <html>
85  <head>
86   <title>{$site}</title>
87  </head>
88  <body>
89   <ul>
90 {$links}
91   </ul>
92  </body>
93 </html>
94 XML;
95     file_put_contents(
96         $GLOBALS['htmldir'] . '/' . $site . '/index.htm',
97         $html
98     );
99 }
100
101 function aws_createGlobalIndex()
102 {
103     $dirs = glob($GLOBALS['htmldir'] . '/*', GLOB_ONLYDIR);
104     sort($dirs);
105     $links = '';
106     foreach ($dirs as $dir) {
107         $name = basename($dir);
108         $links .= '<li><a href="' . $name . '/">'
109             . $name . '</a></li>';
110     }
111     $html = <<<XML
112 <?xml version="1.0" encoding="utf-8"?>
113 <html>
114  <head>
115   <title>awstats</title>
116  </head>
117  <body>
118   <ul>
119 {$links}
120   </ul>
121  </body>
122 </html>
123 XML;
124     file_put_contents(
125         $GLOBALS['htmldir'] . '/index.htm',
126         $html
127     );
128 }
129 ?>