nieuws/feed: atom content type and link attributes
[minimedit.git] / nieuws.inc.php
1 <?php
2 global $monthname;
3 $monthname = ['?',
4         'januari', 'februari', 'maart', 'april', 'mei', 'juni',
5         'juli', 'augustus', 'september', 'oktober', 'november', 'december',
6 ];
7
8 function showdate($parts)
9 {
10         global $monthname;
11         return implode(' ', array_filter([
12                 intval($parts[3]), $parts[2] > 0 ? $monthname[intval($parts[2])] : '', $parts[1],
13                 count($parts) > 6 ? "$parts[4]:$parts[5]" : '',
14         ]));
15 }
16
17 class ArchiveArticle
18 {
19         function __construct($path)
20         {
21                 $this->page = $path;
22                 $this->link = preg_replace('/\.html$/', '', $path);
23         }
24
25         function __get($col)
26         {
27                 return $this->$col = $this->$col();  # run method and cache
28         }
29
30         function file()
31         {
32                 return fopen($this->page, 'r');
33         }
34
35         function title()
36         {
37                 return preg_replace('{<h2>(.*)</h2>\s*}', '\1', fgets($this->file));
38         }
39
40         function safetitle()
41         {
42                 return strip_tags($this->title);
43         }
44
45         function last()
46         {
47                 return filemtime($this->page);
48         }
49
50         function lastiso()
51         {
52                 return date(DATE_ATOM, $this->last);
53         }
54
55         function dateparts()
56         {
57                 preg_match('</(\d{4})/(\d{2})-(\d{2})->', $this->page, $ymd);
58                 return $ymd;
59         }
60
61         function dateiso()
62         {
63                 return implode('-', $this->dateparts());
64         }
65
66         function date()
67         {
68                 return showdate($this->dateparts);
69         }
70
71         function body()
72         {
73                 $this->title;
74                 $rest = fread($this->file, filesize($this->page));
75                 if ( preg_match('{\n<p>(<img [^>]*>)</p>\s*\z}', $rest, $img, PREG_OFFSET_CAPTURE) ) {
76                         $this->img = $img[1][0];
77                         return substr($rest, 0, $img[0][1]);
78                 }
79                 $this->img = NULL;
80                 return $rest;
81         }
82
83         function img()
84         {
85                 $this->body;
86                 return $this->img;
87         }
88
89         function image()
90         {
91                 if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
92                         return $src[1];
93                 }
94         }
95
96         function thumb($size = '300x')
97         {
98                 if (!$this->image or $this->image[0] !== '/') return;
99                 return preg_replace('{^(?:/thumb/[^/]*)?}', "thumb/$size", $this->image);
100         }
101 }
102
103 function shownews($input, $limit = 1000)
104 {
105         if (!is_array($input)) $input = glob("$input/*.html");
106         print '<ul class="left">'."\n\n";
107         foreach (array_reverse($input) as $filename) {
108                 $article = new ArchiveArticle($filename);
109                 print '<li>';
110                 if ($article->thumb) {
111                         printf('<img src="/%s" class="left" />', $article->thumb);
112                 }
113                 print '<article>';
114                 printf(
115                         '<h3><a href="/%s">%s <small class="date">%s</small></a></h3>',
116                         $article->link, $article->title, $article->date
117                 );
118                 print $article->body;
119                 print "</article></li>\n\n";
120
121                 if (--$limit <= 0) break;
122         }
123         print "</ul>\n\n";
124 }
125
126 function printtoc($input)
127 {
128         if (!is_array($input)) $input = glob("$input/*.html");
129         print '<ul>';
130         foreach (array_reverse($input) as $page) {
131                 $article = new ArchiveArticle($page);
132                 printf('<li><a href="/%s">%s <small class="date">%s</small></a></li>',
133                         $article->link, $article->safetitle, $article->date);
134         }
135         print "</ul>\n";
136 }