]> git.mxchange.org Git - friendica.git/blob - wip/atom.php
new todo list
[friendica.git] / wip / atom.php
1 <?php 
2
3 $month = 0;
4 $year = 0;
5 $sort='';
6 $category='';
7 $category_id = 0;
8 $author = '';
9 $dest = '';
10 $rums = false;
11 $blgs = false;
12
13 $q = '';
14 $glbs = false;
15
16 $pagenum = 1;
17 $articles_page = 20;
18 $startrecord = 0;
19
20   $query_saved = substr($_SERVER['QUERY_STRING'],2); // strip '?=', save
21   
22
23
24
25
26
27   $query_original = substr($query_saved,4);      // strip 'atom'
28
29   $query_original = trim($query_original,'/');
30
31   ereg("[/]?page=([0-9]*)",$query_original,$va);
32   
33   if(((count($va)) > 1) && ($va[1] > 0)) {
34     $pagenum = $va[1];
35     $query_original = ereg_replace("([/]?page=[0-9]*)","",$query_original);
36     $startrecord = ($pagenum * $articles_page) - $articles_page;
37   }
38
39
40   if(substr($query_original,-4,4) == 'text') {
41     $type = 'text';
42     $query_original = substr($query_original,0,-4);
43   }
44
45   $query_original = trim($query_original,'/');
46   
47   if(substr($query_original,0,5) == 'forum') {
48     $rums = true;
49     $query_original = substr($query_original,5);
50   }
51
52   $query_original = trim($query_original,'/');
53   
54   $cmd = explode('/',$query_original);
55
56   if($rums) {
57     if($cmd[0] == 'category') {
58       $category = $cmd[1];       
59     }
60     else
61       $dest = $cmd[0];
62   }
63   else {
64     switch (count($cmd)) {
65       case 2:
66         if($cmd[0] == 'share') {
67           $share = $cmd[1];
68           break;
69         }
70         if(strlen($cmd[1])) {
71           $blgs     = true;
72           $category = $cmd[1];
73         }
74
75         $author     = $cmd[0];
76         break;
77
78       case 1:
79         if(strlen($cmd[0])) {
80           $blgs       = true;
81           $author     = $cmd[0];
82         }
83         break;
84
85       default:
86         break;
87
88     }
89   }
90
91 // build the return link
92
93 $href = $SITE_URL;
94 if($blgs)
95   $href .= preg_replace("@^atom@","/weblog",$query_saved);
96 else
97   $href .= preg_replace("@^atom@","",$query_saved);
98 $href = preg_replace("@/text$@","",$href);
99 $href = preg_replace("@/share/@","/view/",$href);
100
101 session_start();
102 session_write_close();
103
104 if((xint($SITE_RESTRICTED)) && (! xint($_SESSION['authenticated'])))
105   exit;
106
107 if($type == "text")
108   header("Content-type: text/plain");
109 else
110   header("Content-type: application/atom+xml");
111 echo '<?xml version="1.0" encoding="utf-8" ?>'."\r\n"; 
112
113 include("globs.php");
114 include("sql.php");
115
116 $sitesubs = get_default_subs();
117
118 // system defaults
119 $blog_url    = $SITE_URL;
120 $blog_title  = xmlificator($SITE_TITLE);
121 $blog_desc   = xmlificator($BLOG_DESC);
122 $blog_author = xmlificator($BLOG_AUTHOR);
123 $blog_email  = xmlificator($BLOG_EMAIL);
124 $blog_logo   = $BLOG_LOGO;
125 if((strlen($blog_logo)) && (! strstr($blog_logo,"://")))
126   $blog_logo = $SITE_URL.'/'.$blog_logo;
127
128 $category = dbesc($category);
129 $author = dbesc($author);
130 $dest = dbesc($dest);
131
132 if(strlen($category) && (! ($category_id = category::get_cat_id($category,$author))))
133   $category = '';
134
135 if(strlen($author)) {
136   $author_info = get_author($author);
137   if(count($author_info)) {
138     $blog_url = xmlificator($SITE_URL.'/weblog/'.$author);
139     if(strlen($author_info[0]['blogname']))
140       $blog_title = xmlificator($author_info[0]['blogname']);
141     if(strlen($author_info[0]['blogdesc']))
142       $blog_desc = xmlificator($author_info[0]['blogdesc']);
143     if(strlen($author_info[0]['fullname']))
144       $blog_author = xmlificator($author_info[0]['fullname']);
145     if(strlen($author_info[0]['email']))
146       $blog_email = xmlificator($author_info[0]['email']);
147     if(strlen($author_info[0]['bloglogo'])) {
148       $blog_logo = xmlificator($author_info[0]['bloglogo']);
149     if((strlen($blog_logo)) && (! strstr($blog_logo,"://")))
150         $blog_logo = $SITE_URL.'/'.$blog_logo;
151     }
152   }
153 }
154
155 if(strlen($share)) {
156   $x = get_user($share);
157   if(count($x) 
158     && ($x[0]['share']) 
159     && (! $x[0]['lurker'])
160     && (! $x[0]['sharecensor'])
161     && (! $x[0]['sharemutual'])) {
162
163     if(xstr($x[0]['sitetitle']))
164       $blog_title = $x[0]['sitetitle'];
165     if(xstr($x[0]['sitedesc']))
166       $blog_desc = $x[0]['sitedesc'];
167     if(xstr($x[0]['sitelogo']))
168       $blog_logo = $x[0]['sitelogo'];
169
170     $newsubs = get_subs($x[0]['name']);
171     $groups = groupsinit();
172     unset($sitesubs);
173     $sitesubs = array();
174     foreach($newsubs as $sub) {
175       $p = fetch_permissions('forum',$sub);
176       if(check_access($_SESSION['username'], $groups, '',$p))
177         $sitesubs[] = $sub;
178     }
179
180   }
181
182 }
183
184
185 $media = getmediamap();
186 $build_date = gmdate('c');
187 $copyright = 'Copyright '.date('Y').' '.$blog_author;
188 $canonical = $SITE_URL;
189 if(substr($canonical,0,-1) != '/')
190   $canonical .= '/';
191
192 $self = $canonical;
193 if(substr($query_saved,0,1) == '/')
194  $self .= substr($query_saved,1);
195 else
196  $self .= $query_saved;
197
198 $subtitle = '';
199 if(strlen($blog_desc))
200   $subtitle = "<subtitle>$blog_desc</subtitle>";
201
202 echo <<< EOT
203 <feed xmlns="http://www.w3.org/2005/Atom"
204       xmlns:thr="http://purl.org/syndication/thread/1.0" >
205
206  <id>$canonical</id>
207  <title>$blog_title</title>
208  <link rel="alternate" href="$href" />
209  <updated>$build_date</updated>
210  $subtitle
211
212 EOT;
213
214 if(strlen($blog_logo)) {
215 echo <<< EOT
216  <logo>$blog_logo</logo>
217
218 EOT;
219 }
220
221   $article_id_list = array();
222
223     $ss = array();
224
225     $ss['tolerance'] = 0;
226
227     if($category_id)
228       $ss['category_id'] = $category_id;
229
230     if($blgs) {
231       $ss['type'] = 'weblog';
232       $ss['author'] = $author;
233     }
234
235     if($rums) {
236       $ss['type'] = 'forum';
237       $ss['forum'] = $dest;
238     }
239
240
241     $ss['countonly'] = true;
242
243     $rt = article_query($ss);
244     $totalarticles = $rt[0]['total'];
245
246     echo " <link rel=\"self\" href=\"$self\" />\r\n";
247
248     $self = ereg_replace("([/]?page=[0-9]*)","",$self);
249
250     if($totalarticles > $articles_page) {
251
252       if($pagenum != 1) {
253         $ppage = $pagenum - 1;
254         echo " <link rel=\"previous\" href=\"$self/page=$ppage\" />\r\n";
255       }
256       echo " <link rel=\"first\" href=\"$self/".'page=1'."\" />\r\n";
257
258       $numpages = $totalarticles / $articles_page;
259
260       $lastpage = (($numpages > intval($numpages)) 
261          ? intval($numpages)+1 
262          : $numpages);
263
264       echo " <link rel=\"last\" href=\"$self/page=$lastpage\" />\r\n";
265
266       if(($totalarticles - ($articles_page * $pagenum)) > 0) {
267         $npage = $pagenum + 1;
268         echo " <link rel=\"next\" href=\"$self/page=$npage\" />\r\n";
269       }
270     }
271
272
273     unset($ss['countonly']);
274
275     $ss['startat'] = $startrecord;
276     $ss['articles_page'] = $articles_page;
277
278     $r = article_query($ss);    
279
280   if(count($r)) {
281     foreach($r as $rr) {
282       $article_id_list[] = "'".$rr['id']."'";
283     }
284   }
285
286   if(count($article_id_list)) {
287     $rcc = fetch_comments_by_list($article_id_list,'article');
288     $ratt = fetch_attachments_by_list($article_id_list);
289   }
290
291   foreach($r as $rr) {
292
293     $link = "{$SITE_URL}/article/{$rr['guid']}";
294     $datestr = $rr['created'];    
295     $pubdate = gmdate('c',strtotime($datestr.' +0000'));
296     $datestr = $rr['edited'];    
297     $editdate = gmdate('c',strtotime($datestr.' +0000'));
298     if(strstr($rr['type'],'news')) {
299       if(strlen($rr['ext_author']))
300         $authorstr = "<author><name>{$rr['ext_author']}</name></author>\r\n";
301       else
302         $authorstr = "<author><name>External Author</name></author>\r\n";
303        
304     }
305     else {
306       if(strlen($rr['fullname']))
307         $authorstr = "<author><name>{$rr['fullname']}</name></author>\r\n";
308       else
309         $authorstr = "<author><name>{$rr['author']}</name></author>\r\n";
310     }
311     $guid = "urn:uuid:{$rr[guid]}";
312     $comments = "{$SITE_URL}/comments/{$rr['guid']}";
313
314     $contents = $rr['body'];
315
316     if((! $rr['approved']) && (! strstr($rr['type'], 'news')))
317       $contents = filter_images($contents);
318
319     $contents = macro_youtube($contents);
320     $contents = reltoabs($contents,$SITE_URL.'/');
321
322     $title = xmlificator($rr['title']);
323     if(! strlen($title)) {
324       $title = xmlificator(substr($datestr,0,11));
325
326       // Use the first 28 chars of text for the title-summary. Watch that 
327       // we don't end up with a  _partial_ escape sequence at the end.
328
329       $stripped = xmlificator(strip_tags($contents));
330
331       $len = 28;
332       $ok = 0;
333       while(! $ok) {
334         $summary=substr($stripped,0,$len);
335         if(($teststr = strrchr($summary,"&")) && (! strchr($teststr,";"))) {
336           $len --;
337         }
338         else {
339           $ok = 1;
340           break;
341         }
342       }
343
344       // Chopped off words don't look pretty in an RSS title
345  
346       if(strrpos($summary,' ') > 10)
347         $title .= ' '.substr($summary,0,(strrpos($summary,' ')));
348     }
349
350     $contents .= feed_attachments($ratt,$rr['id']);
351     $contents .= feed_comments($rcc,$rr['id'],$rr['guid'],$rr['rank']);
352
353     $contents = reltoabs($contents,$SITE_URL.'/');
354
355     $contents = xmlificator($contents);
356
357     // output the item
358
359 echo <<< EOT
360
361 <entry>
362  <id>$guid</id>
363  <title type="html">$title</title>
364  <published>$pubdate</published>
365  <updated>$editdate</updated>
366  <link rel="alternate" href="$link" />
367  <content type="html">$contents</content>
368  $authorstr
369
370 EOT;
371
372     $rc = category::categories_by_article($rr['id']);
373     if(count($rc))
374       for($x = 0; $x < count($rc); $x ++)
375         echo ' <category term="'.xmlificator($rc[$x]).'"/>'."\r\n";
376
377     $rat = attachments_fetch($rr['id']);
378     if(count($rat)) {
379       foreach($rat as $att) {
380         $filename = $att['filename'];
381         $ext = substr($filename,strrpos($filename,'.'));
382         $type = 'application/octet-stream';
383         foreach($media as $m) {
384           if($m[0] != $ext)
385             continue;
386           $type = $m[2];
387             break;
388         }
389         echo ' <link rel="enclosure" href="'.$SITE_URL.'/'.xmlificator($filename).'" length="'.$att['size'].'" type="'.$type.'" />'."\r\n";
390         
391       }
392     }
393     echo "</entry>\r\n\r\n";
394
395 //    if(($ALLOW_COMMENTS == "1") && (count($rcc))) {
396 //      foreach($rcc as $c) { 
397 //        if($c['tid'] != $rr['id'])
398 //          continue;
399 //        $cguid = "urn:uuid:{$c['guid']}";
400 //        $datestr = $c['created'];    
401 //        $cpubdate = date(c,strtotime($datestr));
402 //        
403 //        $ccontents = format_avatar($c['avatar'],$c['author'],$c['userid'],
404 //                                   $c['censored'],0,'',64).'<br />';
405 //        $ccontents .= $c['body'];
406 //
407 //        $ccontents = filter_images($ccontents);
408 //        $ccontents = reltoabs($ccontents,$SITE_URL.'/');
409 //        $ccontents = xmlificator($ccontents);
410 //        if(strlen($c['fullname']))
411 //          $cauthor = xmlificator($c['fullname']);
412 //        else
413 //          $cauthor = xmlificator($c['author']);        
414 //        $cauthorstr = '<author><name>'.$cauthor.'</name>';
415 //        if(strlen($c['url']))
416 //          $cauthorstr .= '<uri>'.xmlificator($c['url']).'</uri>';
417 //        $cauthorstr .= '</author>';
418 //
419 //echo <<< EOT
420 //<entry>
421 //<thr:in-reply-to ref="$guid" />
422 //<id>$cguid</id>
423 //<title type="html">Re: $title</title>
424 //<link rel="related" href="$link" />
425 //<published>$cpubdate</published>
426 //<updated>$cpubdate</updated>
427 //<content type="html">$ccontents</content>
428 //$cauthorstr
429 //</entry>
430 //
431 //EOT;
432 //}
433 //}
434
435
436   }
437
438   echo "</feed>\r\n";
439
440 ?>