Merge pull request #8 from radiocontrolled/0004872
[atutor.git] / get_rss.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12 // $Id: get_acheck.php 2291 2004-11-16 19:35:41Z joel $
13
14 /* call it:
15  * ATUTOR_PATH/get_rss.php?COURSE_ID-VERSION
16
17         COURSE_ID: integer value of the course (non-zero)
18         VERSION: [1|2] version of RSS
19 */
20
21 /* assumption: if the rss files exist, then they're supposed to exist and are public.
22  * if the rss file does not exist: check if this course has it enabled, and create it if needed.
23  * that way rss is only ever created if it's ever called. if it's enabled and never viewed, then there's no need
24  * to generate the files.
25  */
26
27 $_user_location = 'public';
28
29 define('AT_INCLUDE_PATH', 'include/');
30 require(AT_INCLUDE_PATH . '/vitals.inc.php');
31
32 if (isset($_SERVER['QUERY_STRING'])) {
33         $parts   = explode('-', $_SERVER['QUERY_STRING'], 2);
34         $course  = intval($parts[0]);
35         $version = intval($parts[1]);
36 } else {
37         header('HTTP/1.1 404 Not Found');
38         exit;
39 }
40
41 if (file_exists(AT_CONTENT_DIR . 'feeds/' . $course . '/RSS' . $version . '.0.xml')) {
42         header('Content-Type: text/xml');
43         header('Content-Length: ' . filesize(AT_CONTENT_DIR . 'feeds/' . $course . '/RSS'.$version.'.0.xml'));
44         echo file_get_contents(AT_CONTENT_DIR . 'feeds/' . $course . '/RSS'.$version.'.0.xml');
45         exit;
46 } // else: (rss does not exist)
47 if ($system_courses[$course]['rss'] && (($version == 1) || ($version == 2))) {
48         // only RSS1 and 2 for now.
49
50         require(AT_INCLUDE_PATH . 'classes/feedcreator.class.php');
51
52         if (!is_dir(AT_CONTENT_DIR.'feeds/')){
53                 @mkdir(AT_CONTENT_DIR. 'feeds/', 0700);
54         }
55         if (!is_dir(AT_CONTENT_DIR . 'feeds/' . $course)){
56                 @mkdir(AT_CONTENT_DIR . 'feeds/' . $course . '/', 0700);
57         }
58
59         $rss = new UniversalFeedCreator();
60         $rss->useCached();
61         $rss->title          = $system_courses[$course]['title'];
62         $rss->description    = $system_courses[$course]['description'];
63         $rss->link           = AT_BASE_HREF;
64         $rss->syndicationURL = AT_BASE_HREF;
65         
66         $image = new FeedImage();
67         $image->title = 'ATutor Logo';
68         $image->url   = AT_BASE_HREF . 'images/at-logo.v.3.gif';
69         $image->link  = AT_BASE_HREF;
70         $rss->image   = $image;
71
72         $sql = "SELECT A.*, M.login from ".TABLE_PREFIX."news A, ".TABLE_PREFIX."members M WHERE A.course_id = ".$course." AND A.member_id=M.member_id ORDER BY A.date DESC LIMIT 5";
73
74         $res = mysql_query($sql, $db);
75
76         while ($data = mysql_fetch_assoc($res)) {
77                 $item = new FeedItem();
78                 
79                 $item->title          = $data['title'];
80                 $item->link           = AT_BASE_HREF . 'index.php';
81                 $item->description    = $data['body'];
82                 $item->date           = strtotime($data['date']);
83                 $item->source         = AT_BASE_HREF;
84                 $item->author         = $data['login'];
85
86                 $rss->addItem($item);
87         }
88
89         header('Content-Type: text/xml');
90         $rss->saveFeed('RSS'.$version.'.0', AT_CONTENT_DIR . 'feeds/' . $course . '/RSS' . $version . '.0.xml', false);
91
92         echo file_get_contents(AT_CONTENT_DIR . 'feeds/' . $course . '/RSS'.$version.'.0.xml');
93
94         exit;
95 } // else: this course didn't enable rss
96
97 header('HTTP/1.1 404 Not Found');
98 exit;
99
100
101 ?>