Fixed bug: call method in object instead of global function
[vanilla-miry.git] / feed.php
blobf7103fd2c7417c75068baa2c5b4c733117f605da
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 $sFile;
97 var $sFileLock;
98 var $iCacheTime;
99 var $oCacheObject;
101 function SimpleCache($sKey, $iCacheTime = 300) {
102 $this->sFile = CACHE_PATH.md5($sKey).".cache";
103 $this->sFileLock = "$this->sFile.lock";
104 $iCacheTime >= 10 ? $this->iCacheTime = $iCacheTime : $this->iCacheTime = 10;
107 function Check() {
108 if (file_exists($this->sFileLock)) return true;
109 return (file_exists($this->sFile) && ($this->iCacheTime == -1 || time() - filemtime($this->sFile) <= $this->iCacheTime));
112 function Exists() {
113 return (file_exists($this->sFile) || file_exists($this->sFileLock));
116 function Set($vContents) {
117 if (!file_exists($this->sFileLock)) {
118 if (file_exists($this->sFile)) {
119 copy($this->sFile, $this->sFileLock);
121 $oFile = fopen($this->sFile, 'w');
122 fwrite($oFile, serialize($vContents));
123 fclose($oFile);
124 if (file_exists($this->sFileLock)) {
125 unlink($this->sFileLock);
127 return true;
129 return false;
132 function Get() {
133 if (file_exists($this->sFileLock)) {
134 return unserialize(file_get_contents($this->sFileLock));
135 } else {
136 return unserialize(file_get_contents($this->sFile));
140 function ReValidate() {
141 touch($this->sFile);
145 /* ------------------------------------------------------------------------ */
147 class FauxContext { // Create a faux-context, 'cos we don't need all the extra overhead
148 var $Dictionary;
149 function FauxContext() {
150 $this->Dictionary = array();
152 function GetDefinition($Code) {
153 if (array_key_exists($Code, $this->Dictionary)) {
154 return $this->Dictionary[$Code];
155 } else {
156 return $Code;
160 $Context = new FauxContext();
162 header ('Content-type: text/xml; charset='.$Configuration['CHARSET']);
164 // LANGUAGE DICTIONARY
165 include($Configuration['LANGUAGES_PATH'].$Configuration['LANGUAGE'].'/definitions.php');
166 //include($Configuration['APPLICATION_PATH'].'conf/language.php');
168 $What=strtolower(ForceIncomingString('What', null)); // 'Topics', 'Blog', 'Category', 'Discussion'
169 $Page=(int)1;
170 $Feed=strtoupper(ForceIncomingString('Feed', null));
171 if ($Feed != 'ATOM') $Feed = 'RSS2';
172 $FeedTitle=urldecode(ForceIncomingString('FeedTitle', null));
174 if (isset($_GET['Blog']) || isset($_GET['BlogSearch'])) { // Messages in the blog
175 $params = 'PostBackAction=Search&Type=Comments&Page='.$Page.'&Feed='.$Feed.'&BlogSearch=1';
176 if ($FeedTitle==NULL) $FeedTitle = 'Blog';
177 } else if (isset($_GET['CategoryID'])) { // Discussions in a category
178 $params = 'PostBackAction=Search&Type=Topics&Page='.$Page.'&Feed='.$Feed.'&CategoryID='.ForceIncomingInt('CategoryID', null);
179 if ($FeedTitle==NULL) $FeedTitle = 'Category';
180 } else if (isset($_GET['DiscussionID'])) { // Messages in a discussion
181 $params = 'PostBackAction=Search&Type=Comments&Page='.$Page.'&Feed='.$Feed.'&DiscussionID='.ForceIncomingInt('DiscussionID', null);
182 if ($FeedTitle==NULL) $FeedTitle = 'Discussion';
183 } else { // All the topics
184 $params = 'PostBackAction=Search&Type=Topics&Page='.$Page.'&Feed='.$Feed;
185 if ($FeedTitle==NULL) $FeedTitle = 'All topics';
188 $url = $Configuration['BASE_URL'].'/search.php?'.$params.'&FeedTitle='.urlencode($FeedTitle);
190 $cache = new SimpleCache($params);
191 if ($cache->Check()) {
192 echo($cache->Get());
193 } else {
194 $http = new SimpleHttp();
195 $data = $http->Get($url);
196 $cache->Set($data);
197 echo $data;