54853158a6a18ea14514757469d470d9bb4c8f34
[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                 if (!file_exists($this->page)) return;
33                 return fopen($this->page, 'r');
34         }
35
36         function title()
37         {
38                 return preg_replace('{<h2>(.*)</h2>\s*}', '\1', fgets($this->file));
39         }
40
41         function safetitle()
42         {
43                 return strip_tags($this->title);
44         }
45
46         function last()
47         {
48                 return filemtime($this->page);
49         }
50
51         function lastiso()
52         {
53                 return date(DATE_ATOM, $this->last);
54         }
55
56         function dateparts()
57         {
58                 preg_match('</(\d{4})[/-](\d{2})-(\d{2})->', $this->page, $ymd);
59                 return $ymd;
60         }
61
62         function dateiso()
63         {
64                 return implode('-', $this->dateparts());
65         }
66
67         function date()
68         {
69                 return showdate($this->dateparts);
70         }
71
72         function body()
73         {
74                 $this->title;
75                 $rest = fread($this->file, filesize($this->page));
76                 if ( preg_match('{\n<p>(<img [^>]*>)</p>\s*\z}', $rest, $img, PREG_OFFSET_CAPTURE) ) {
77                         $this->img = $img[1][0];
78                         return substr($rest, 0, $img[0][1]);
79                 }
80                 $this->img = NULL;
81                 return $rest;
82         }
83
84         function img()
85         {
86                 $this->body;
87                 return $this->img;
88         }
89
90         function image()
91         {
92                 if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
93                         return $src[1];
94                 }
95         }
96
97         function thumb($size = '300x')
98         {
99                 if (!$this->image or $this->image[0] !== '/') return;
100                 return preg_replace('{^(?:/thumb/[^/]*)?}', "thumb/$size", $this->image);
101         }
102 }
103
104 function shownews($input, $limit = 1000)
105 {
106         if (!is_array($input)) $input = glob("$input/*.html");
107         print '<ul class="left">'."\n\n";
108         foreach (array_reverse($input) as $filename) {
109                 $article = new ArchiveArticle($filename);
110                 print '<li>';
111                 if ($article->thumb) {
112                         printf('<img src="/%s" class="left" />', $article->thumb);
113                 }
114                 print '<article>';
115                 printf(
116                         '<h3><a href="/%s">%s <small class="date">%s</small></a></h3>',
117                         $article->link, $article->title, $article->date
118                 );
119                 print $article->body;
120                 print "</article></li>\n\n";
121
122                 if (--$limit <= 0) break;
123         }
124         print "</ul>\n\n";
125 }
126
127 function printtoc($input)
128 {
129         if (!is_array($input)) $input = glob("$input/*.html");
130         print '<ul>';
131         foreach (array_reverse($input) as $page) {
132                 $article = new ArchiveArticle($page);
133                 printf('<li><a href="/%s">%s <small class="date">%s</small></a></li>',
134                         $article->link, $article->safetitle, $article->date);
135         }
136         print "</ul>\n";
137 }