readme, license and authors file. also tpl and tpl-howto
[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     passthru($cmd);
58
59     if (!$last) {
60         aws_updateLink($site, $dir);
61     }
62 }
63
64 function aws_updateLink($site, $targetdir)
65 {
66     $curdir = $GLOBALS['htmldir'] . '/' . $site . '/current';
67     unlink($curdir);
68     symlink($targetdir, $curdir);
69 }
70
71 function aws_createIndex($site)
72 {
73     $dirs = glob($GLOBALS['htmldir'] . '/' . $site . '/*', GLOB_ONLYDIR);
74     sort($dirs);
75     $links = '';
76     foreach ($dirs as $dir) {
77         $name = basename($dir);
78         $links .= '<li><a href="' . $name . '/awstats.' . $site . '.html">'
79             . $name . '</a></li>';
80     }
81     $html = <<<XML
82 <?xml version="1.0" encoding="utf-8"?>
83 <html>
84  <head>
85   <title>{$site}</title>
86  </head>
87  <body>
88   <ul>
89 {$links}
90   </ul>
91  </body>
92 </html>
93 XML;
94     file_put_contents(
95         $GLOBALS['htmldir'] . '/' . $site . '/index.htm',
96         $html
97     );
98 }
99
100 function aws_createGlobalIndex()
101 {
102     $dirs = glob($GLOBALS['htmldir'] . '/*', GLOB_ONLYDIR);
103     sort($dirs);
104     $links = '';
105     foreach ($dirs as $dir) {
106         $name = basename($dir);
107         $links .= '<li><a href="' . $name . '/">'
108             . $name . '</a></li>';
109     }
110     $html = <<<XML
111 <?xml version="1.0" encoding="utf-8"?>
112 <html>
113  <head>
114   <title>awstats</title>
115  </head>
116  <body>
117   <ul>
118 {$links}
119   </ul>
120  </body>
121 </html>
122 XML;
123     file_put_contents(
124         $GLOBALS['htmldir'] . '/index.htm',
125         $html
126     );
127 }
128 ?>