script to update last month
[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_createHtml($site, $last = false)
14 {
15     if (!is_dir($GLOBALS['htmldir'])) {
16         die("HTML directory " . $GLOBALS['htmldir'] . " does not exist\n");
17     }
18
19     if ($last) {
20         $month = date('m', strtotime('last month'));
21         $year  = date('Y', strtotime('last month'));
22     } else {
23         $month = date('m');
24         $year  = date('Y');
25     }
26
27     $dir = $GLOBALS['htmldir'] . '/' . $site . '/' . $year . '-' . $month . '/';
28     if (!is_dir($dir)) {
29         mkdir($dir, 0777, true);
30     }
31     $cmd = $GLOBALS['awsbstp']
32         . ' -config=' . escapeshellarg($site)
33         . ' -awstatsprog=' . escapeshellarg($GLOBALS['awstats'])
34         . ' -update'
35         . ' -month=' . $month
36         . ' -year=' . $year
37         . ' -dir=' . escapeshellarg($dir);
38     passthru($cmd);
39
40     if (!$last) {
41         aws_updateLink($site, $dir);
42     }
43 }
44
45 function aws_updateLink($site, $targetdir)
46 {
47     $curdir = $GLOBALS['htmldir'] . '/' . $site . '/current';
48     unlink($curdir);
49     symlink($targetdir, $curdir);
50 }
51
52 function aws_createIndex($site)
53 {
54     $dirs = glob($GLOBALS['htmldir'] . '/' . $site . '/*', GLOB_ONLYDIR);
55     sort($dirs);
56     $links = '';
57     foreach ($dirs as $dir) {
58         $name = basename($dir);
59         $links .= '<li><a href="' . $name . '/awstats.' . $site . '.html">'
60             . $name . '</a></li>';
61     }
62     $html = <<<XML
63 <?xml version="1.0" encoding="utf-8"?>
64 <html>
65  <head>
66   <title>{$site}</title>
67  </head>
68  <body>
69   <ul>
70 {$links}
71   </ul>
72  </body>
73 </html>
74 XML;
75     file_put_contents(
76         $GLOBALS['htmldir'] . '/' . $site . '/index.htm',
77         $html
78     );
79 }
80
81 function aws_createGlobalIndex()
82 {
83     $dirs = glob($GLOBALS['htmldir'] . '/*', GLOB_ONLYDIR);
84     sort($dirs);
85     $links = '';
86     foreach ($dirs as $dir) {
87         $name = basename($dir);
88         $links .= '<li><a href="' . $name . '/">'
89             . $name . '</a></li>';
90     }
91     $html = <<<XML
92 <?xml version="1.0" encoding="utf-8"?>
93 <html>
94  <head>
95   <title>awstats</title>
96  </head>
97  <body>
98   <ul>
99 {$links}
100   </ul>
101  </body>
102 </html>
103 XML;
104     file_put_contents(
105         $GLOBALS['htmldir'] . '/index.htm',
106         $html
107     );
108 }
109 ?>