Made quite a few changes so that the code works in the server
[vanilla-miry.git] / feed.php
bloba2b8f00d3033f25e5bafefbbfad6442ae8452f85
1 <?php
2 /*
3 * Copyright 2010 Miriam Ruiz
4 * Copyright 2003 Mark O'Sullivan
5 * This file is part of Vanilla.
6 * Vanilla is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
7 * Vanilla is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
8 * You should have received a copy of the GNU General Public License along with Vanilla; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
9 * The latest source code for Vanilla is available at www.lussumo.com
10 * Contact Mark O'Sullivan at mark [at] lussumo [dot] com
12 * Description: Terms of use for the vanilla forum - should be customized by Vanilla user
14 include('appg/settings.php');
15 include('conf/settings.php');
16 include('library/Framework/Framework.Functions.php');
18 /* ------------------------------------------------------------------------ */
20 // PhpCache - a class for caching arbitrary data
21 // Copyright (C) 2005-2007, Edward Eliot
22 // http://www.ejeliot.com/blog/77
25 Software License Agreement (BSD License)
27 Redistribution and use in source and binary forms, with or without
28 modification, are permitted provided that the following conditions are met:
30 * Redistributions of source code must retain the above copyright
31 notice, this list of conditions and the following disclaimer.
32 * Redistributions in binary form must reproduce the above copyright
33 notice, this list of conditions and the following disclaimer in the
34 documentation and/or other materials provided with the distribution.
35 * Neither the name of Edward Eliot nor the names of its contributors
36 may be used to endorse or promote products derived from this software
37 without specific prior written permission of Edward Eliot.
39 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND ANY
40 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
41 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
43 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
44 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
48 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 define('HTTP_TIMEOUT', 3); // how long to wait for a connection before aborting, CURL only
52 define('MAX_HTTP_REQUEST_TIME', 5); // maximum time allowed for completing URL request before aborting, CURL only
53 define('HTTP_USERAGENT', 'SimpleRss');
55 class SimpleHttp {
56 var $iConnectTimeout;
57 var $iRequestTimeout;
58 var $sUserAgent;
60 function SimpleHttp($iConnectTimeout = HTTP_TIMEOUT, $iRequestTimeout = MAX_HTTP_REQUEST_TIME, $sUserAgent = HTTP_USERAGENT)
62 $this->iConnectTimeout = $iConnectTimeout;
63 $this->iRequestTimeout = $iRequestTimeout;
66 function Get($sUrl) { // check for curl lib, use in preference to file_get_contents if available
67 if (function_exists('curl_init')) {
68 // initiate session
69 $oCurl = curl_init($sUrl);
70 // set options
71 curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->iConnectTimeout);
72 curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->iRequestTimeout);
73 curl_setopt($oCurl, CURLOPT_USERAGENT, $this->sUserAgent);
74 curl_setopt($oCurl, CURLOPT_HEADER, false);
75 curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
76 // request URL
77 $sResult = curl_exec($oCurl);
78 // close session
79 curl_close($oCurl);
80 return $sResult;
81 } else {
82 ini_set('user_agent', HTTP_USERAGENT);
83 // fopen_wrappers need to be enabled for this to work
84 // See http://www.php.net/manual/en/function.file-get-contents.php
85 if ($sResult = @file_get_contents($sUrl)) {
86 return $sResult;
89 return false;
93 define('CACHE_PATH', $Configuration['APPLICATION_PATH'] . '/cache/');
95 class SimpleCache {
96 var $sCacheSubDir;
97 var $sFile;
98 var $sFileLock;
99 var $iCacheTime; // In seconds
100 var $oCacheObject;
102 function SimpleCache($sType,$sKey, $iCacheTime = 300) {
103 $this->sCacheSubDir = '/'.$sType.'/';
104 $this->sFile = CACHE_PATH.$this->sCacheSubDir.md5($sKey).".cache";
105 $this->sFileLock = "$this->sFile.lock";
106 $iCacheTime >= 10 ? $this->iCacheTime = $iCacheTime : $this->iCacheTime = 10;
109 function Check() {
110 if (file_exists($this->sFileLock)) return true;
111 return (file_exists($this->sFile) && ($this->iCacheTime == -1 || time() - filemtime($this->sFile) <= $this->iCacheTime));
114 function Exists() {
115 return (file_exists($this->sFile) || file_exists($this->sFileLock));
118 function Set($vContents) {
119 if (!file_exists($this->sFileLock)) {
120 if (file_exists($this->sFile)) {
121 copy($this->sFile, $this->sFileLock);
123 $oFile = fopen($this->sFile, 'w');
124 fwrite($oFile, serialize($vContents));
125 fclose($oFile);
126 if (file_exists($this->sFileLock)) {
127 unlink($this->sFileLock);
129 return true;
131 return false;
134 function Get() {
135 if (file_exists($this->sFileLock)) {
136 return unserialize(file_get_contents($this->sFileLock));
137 } else {
138 return unserialize(file_get_contents($this->sFile));
142 function ReValidate() {
143 touch($this->sFile);
147 /* ------------------------------------------------------------------------ */
149 class FauxContext { // Create a faux-context, 'cos we don't need all the extra overhead
150 var $Dictionary;
151 function FauxContext() {
152 $this->Dictionary = array();
154 function GetDefinition($Code) {
155 if (array_key_exists($Code, $this->Dictionary)) {
156 return $this->Dictionary[$Code];
157 } else {
158 return $Code;
162 $Context = new FauxContext();
164 header ('Content-type: text/xml; charset='.$Configuration['CHARSET']);
166 // LANGUAGE DICTIONARY
167 include($Configuration['LANGUAGES_PATH'].$Configuration['LANGUAGE'].'/definitions.php');
168 //include($Configuration['APPLICATION_PATH'].'conf/language.php');
170 $What=strtolower(ForceIncomingString('What', null)); // 'Topics', 'Blog', 'Category', 'Discussion'
171 $Page=(int)1;
172 $Feed=strtoupper(ForceIncomingString('Feed', null));
173 if ($Feed != 'ATOM') $Feed = 'RSS2';
174 $FeedTitle=urldecode(ForceIncomingString('FeedTitle', null));
176 if (isset($_GET['Blog']) || isset($_GET['BlogSearch'])) { // Messages in the blog
177 $params = 'PostBackAction=Search&Type=Comments&Page='.$Page.'&Feed='.$Feed.'&BlogSearch=1';
178 if ($FeedTitle==NULL) $FeedTitle = 'Blog';
179 } else if (isset($_GET['CategoryID'])) { // Discussions in a category
180 $params = 'PostBackAction=Search&Type=Topics&Page='.$Page.'&Feed='.$Feed.'&CategoryID='.ForceIncomingInt('CategoryID', null);
181 if ($FeedTitle==NULL) $FeedTitle = 'Category';
182 } else if (isset($_GET['DiscussionID'])) { // Messages in a discussion
183 $params = 'PostBackAction=Search&Type=Comments&Page='.$Page.'&Feed='.$Feed.'&DiscussionID='.ForceIncomingInt('DiscussionID', null);
184 if ($FeedTitle==NULL) $FeedTitle = 'Discussion';
185 } else { // All the topics
186 $params = 'PostBackAction=Search&Type=Topics&Page='.$Page.'&Feed='.$Feed;
187 if ($FeedTitle==NULL) $FeedTitle = 'All topics';
190 $url = $Configuration['BASE_URL'].'/search.php?'.$params.'&FeedTitle='.urlencode($FeedTitle);
192 $cache = new SimpleCache('feed', $params, 600);
193 if ($cache->Check()) {
194 echo($cache->Get());
195 } else {
196 $http = new SimpleHttp();
197 $data = $http->Get($url);
198 $cache->Set($data);
199 echo $data;