929ac8a59ad894ce5d6a13faa01c5514738722e5
[atutor.git] / docs / mods / _standard / rss_feeds / module.php
1 <?php\r
2 if (!defined('AT_INCLUDE_PATH')) { exit; }\r
3 \r
4 if (!isset($this) || (isset($this) && (strtolower(get_class($this)) != 'module'))) { exit(__FILE__ . ' is not a Module'); }\r
5 \r
6 define('AT_ADMIN_PRIV_RSS', $this->getAdminPrivilege());\r
7 \r
8 define('AT_FEED_TIMEOUT', 21600);  //feed is cached for this long.  21600 = 6 hours\r
9 define('AT_FEED_NUM_RESULTS', 5);\r
10 define('AT_FEED_SHOW_DESCRIPTION', FALSE);\r
11 \r
12 //admin pages\r
13 if (admin_authenticate(AT_ADMIN_PRIV_RSS, TRUE) || admin_authenticate(AT_ADMIN_PRIV_ADMIN, TRUE)) {\r
14 \r
15         if (admin_authenticate(AT_ADMIN_PRIV_ADMIN, TRUE)) {\r
16                 $this->_pages['admin/config_edit.php']['children']      = array('mods/_standard/rss_feeds/index.php');\r
17                 $this->_pages['mods/_standard/rss_feeds/index.php']['parent'] = 'admin/config_edit.php';\r
18         } else {\r
19                 $this->_pages[AT_NAV_ADMIN] = array('mods/_standard/rss_feeds/index.php');\r
20                 $this->_pages['mods/_standard/rss_feeds/index.php']['parent'] = AT_NAV_ADMIN;\r
21         }\r
22 \r
23         $this->_pages['mods/_standard/rss_feeds/index.php']['title_var'] = 'rss_feeds';\r
24         $this->_pages['mods/_standard/rss_feeds/index.php']['children']  = array('mods/_standard/rss_feeds/add_feed.php');\r
25         $this->_pages['mods/_standard/rss_feeds/index.php']['guide']     = 'admin/?p=feeds.php';\r
26 \r
27                 $this->_pages['mods/_standard/rss_feeds/add_feed.php']['title_var'] = 'add';\r
28                 $this->_pages['mods/_standard/rss_feeds/add_feed.php']['parent'] = 'mods/_standard/rss_feeds/index.php';\r
29 \r
30                 $this->_pages['mods/_standard/rss_feeds/edit_feed.php']['title_var'] = 'edit';\r
31                 $this->_pages['mods/_standard/rss_feeds/edit_feed.php']['parent'] = 'mods/_standard/rss_feeds/index.php';\r
32 \r
33                 $this->_pages['mods/_standard/rss_feeds/delete_feed.php']['title_var'] = 'delete';\r
34                 $this->_pages['mods/_standard/rss_feeds/delete_feed.php']['parent'] = 'mods/_standard/rss_feeds/index.php';\r
35 \r
36                 $this->_pages['mods/_standard/rss_feeds/preview.php']['title_var'] = 'preview';\r
37                 $this->_pages['mods/_standard/rss_feeds/preview.php']['parent'] = 'mods/_standard/rss_feeds/index.php';\r
38 }\r
39 \r
40 //make the rss files side menu stacks\r
41 $rss_files = array();\r
42 $dh  = opendir(AT_CONTENT_DIR.'/feeds');\r
43 $count = 0;\r
44 while (false !== ($file = readdir($dh))) {\r
45         if (strpos($file, '_rss.inc.php')) {\r
46                 $feed_id = intval($file);\r
47                 if (file_exists(AT_CONTENT_DIR.'feeds/'.$feed_id.'_rss_title.cache')) {\r
48                         $title = @file_get_contents(AT_CONTENT_DIR.'feeds/'.$feed_id.'_rss_title.cache');\r
49                         $this->_stacks[$feed_id.'_rss_title'] = array('title'=>$title, 'file'=>AT_CONTENT_DIR.'feeds/'.$file);\r
50                 }\r
51         }\r
52 }\r
53 \r
54 //creates or updates the cache file\r
55 function make_cache_file($feed_id) {\r
56         global $db;\r
57         static $rss;\r
58 \r
59         if (!isset($rss)) {  \r
60                 require_once(AT_INCLUDE_PATH.'../mods/_standard/rss_feeds/classes/lastRSS.php');\r
61                 $rss = new lastRSS; \r
62                 $rss->cache_dir = AT_CONTENT_DIR.'feeds/'; \r
63                 $rss->num_results = AT_FEED_NUM_RESULTS;\r
64                 $rss->description = AT_FEED_SHOW_DESCRIPTION;\r
65         } \r
66 \r
67         $sql    = "SELECT url, feed_id FROM ".TABLE_PREFIX."feeds WHERE feed_id=".intval($feed_id);\r
68         $result = mysql_query($sql, $db);\r
69 \r
70         if ($row = mysql_fetch_assoc($result)) {\r
71                 $output = $rss->get($row['url'], $row['feed_id']);\r
72 \r
73                 $cache_file = AT_CONTENT_DIR.'feeds/'.$feed_id.'_rss.cache';\r
74                 if ($f = @fopen($cache_file, 'w')) {\r
75                         fwrite ($f, $output, strlen($output));\r
76                         fclose($f);\r
77                 }\r
78                 return 0;\r
79         } else {\r
80                 $output = $rss->get($_POST['url'], 0);\r
81                 return $output;\r
82         }\r
83 }\r
84 \r
85 function print_rss_feed($file) {\r
86         global $savant;\r
87 \r
88         $feed_id = intval(basename($file));\r
89         $cache_file = AT_CONTENT_DIR.'feeds/'.$feed_id.'_rss.cache';\r
90         $title_file = AT_CONTENT_DIR.'feeds/'.$feed_id.'_rss_title.cache';\r
91 \r
92         ob_start(); \r
93 \r
94         //if file doesn't exist or is more than AT_FEED_TIMEOUT old\r
95         if (!file_exists($cache_file) || ((time() - filemtime($cache_file)) > AT_FEED_TIMEOUT) ) {\r
96                 make_cache_file($feed_id);\r
97         }\r
98         if (file_exists($cache_file)) {\r
99                 readfile($cache_file);\r
100                 echo '<br /><small>'._AT('new_window').'</small>';\r
101         } else {\r
102                 echo _AT('no_content_avail');\r
103         }\r
104 \r
105         $savant->assign('dropdown_contents', ob_get_contents());\r
106         ob_end_clean();\r
107 \r
108         $savant->assign('title', @file_get_contents($title_file));\r
109         $savant->display('include/box.tmpl.php');\r
110 }\r
111 \r
112 ?>