File: 1.15.0a/server/base/rss.inc.php (View as HTML)

  1: <?php
  2: /* -------------------------------------------------------------
  3: This file is part of FreeNATS
  4: 
  5: FreeNATS is (C) Copyright 2008-2011 PurplePixie Systems
  6: 
  7: FreeNATS is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeNATS is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeNATS.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freenats
 21: -------------------------------------------------------------- */
 22: 
 23: class NATS_RSS
 24: {
 25: var $NATS=false;
 26: 
 27: function NATS_RSS()
 28: {
 29: 	global $NATS;
 30: 	$this->NATS = &$NATS;
 31: }
 32: 
 33: function Create($name)
 34: {
 35: 	mt_srand(microtime()*1000000);
 36: 	$allow="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 37: 	$alen=strlen($allow);
 38: 	$key="";
 39: 	$len=32;
 40: 	for ($a=0; $a<$len; $a++)
 41: 	{
 42: 		$key.=$allow[mt_rand(0,$alen-1)];
 43: 	}
 44: 	$q="INSERT INTO fnrssfeed(feedkey,feedname, feedtype, feedrange, rangeopt) "
 45: 		."VALUES(\"".$key."\",\"".ss($name)."\",\"all\""
 46: 		.",\"xdays\",30)";
 47: 	$this->NATS->DB->Query($q);
 48: 	
 49: 	return $this->NATS->DB->Insert_Id();
 50: }
 51: 
 52: function Delete($id)
 53: {
 54: 	$q="DELETE FROM fnrssfeed WHERE feedid=".ss($id);
 55: 	$this->NATS->DB->Query($q);
 56: }
 57: 
 58: function GetTypes()
 59: {
 60: 	return array(
 61: 		"all" => $this->NATS->Lang->Item("all.enabled.nodes"),
 62: 		"node" => $this->NATS->Lang->Item("one.node"),
 63: 		"group" => $this->NATS->Lang->Item("group") );
 64: }
 65: 
 66: function GetRanges()
 67: {
 68: 	return array(
 69: 		"xdays" => $this->NATS->Lang->Item("last.x.days"),
 70: 		"xalerts" => $this->NATS->Lang->Item("last.x.alerts"),
 71: 		"alerts" => $this->NATS->Lang->Item("current.alerts"),
 72: 		"alertnode" => $this->NATS->Lang->Item("last.alerts.node") );
 73: }
 74: 
 75: function GetFeeds($id=0)
 76: {
 77: 	$q="SELECT * FROM fnrssfeed";
 78: 	if ($id!=0) $q.=" WHERE feedid=".ss($id)." LIMIT 0,1";
 79: 	
 80: 	$out=array();
 81: 	
 82: 	$r=$this->NATS->DB->Query($q);
 83: 	
 84: 	$types=$this->GetTypes();
 85: 	$ranges=$this->GetRanges();
 86: 	
 87: 	while ($row=$this->NATS->DB->Fetch_Array($r))
 88: 	{
 89: 		$out[]=$row;
 90: 	}
 91: 	
 92: 	$this->NATS->DB->Free($r);
 93: 	
 94: 	return $out;
 95: }
 96: 
 97: function GetFeed($id)
 98: {
 99: 	$feeds = $this->GetFeeds($id);
100: 	if (count($feeds)>0) return $feeds[0];
101: 	else return $feeds;
102: }
103: 
104: function SaveFeed($id, $data)
105: {
106: 	$q="UPDATE fnrssfeed SET ";
107: 	$first=true;
108: 	
109: 	foreach($data as $field => $value)
110: 	{
111: 		if ($first) $first=false;
112: 		else $q.=",";
113: 		$q.=ss($field)."=\"".ss($value)."\"";
114: 	}
115: 	$q.=" WHERE feedid=".ss($id);
116: 	
117: 	$this->NATS->DB->Query($q);
118: 	
119: 	if ($this->NATS->DB->Affected_Rows()<=0) return false;
120: 	else return true;
121: }
122: 
123: function GetIdFromCompound($compound)
124: {
125: 	$parts=explode("-",$compound);
126: 	$key=$parts[0];
127: 	$id=$parts[1];
128: 	
129: 	$q="SELECT feedid FROM fnrssfeed WHERE feedid=".ss($id)." AND feedkey=\"".ss($key)."\" LIMIT 0,1";
130: 	$r=$this->NATS->DB->Query($q);
131: 	
132: 	if ($row=$this->NATS->DB->Fetch_Array($r))
133: 	{
134: 		$this->NATS->DB->Free($r);
135: 		return $row['feedid'];
136: 	}
137: 	else return false;
138: }
139: 
140: function GetCompound($id, $key="")
141: {
142: 	if ($key=="")
143: 	{
144: 		$feed=$this->GetFeed($id);
145: 		$key=$feed['feedkey'];
146: 	}
147: 	return $key."-".$id;
148: }
149: 
150: function GetURL($id, $key="")
151: {
152: 	$compound=$this->GetCompound($id, $key);
153: 	return GetAbsolute("rss.php?feed=".$compound);
154: }
155: 
156: function Item($title, $link, $published, $desc)
157: {
158: 	$out="<item>\n"
159: 		."<title><![CDATA[".$title."]]></title>\n"
160: 		."<link><![CDATA[".$link."]]></link>\n"
161: 		."<guid><![CDATA[".$link."]]></guid>\n"
162: 		."<pubDate><![CDATA[".$published."]]></pubDate>\n"
163: 		."<description><![CDATA[".$desc."]]></description>\n"
164: 		."</item>\n";
165: 	return $out;
166: }
167: 
168: function Render($id)
169: {
170: 	$feed=$this->GetFeed($id);
171: 	if (count($feed)<=0) return false;
172: 	$out="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
173: 	$out.="<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n";
174: 	$out.="<channel>\n";
175: 	$out.="<title><![CDATA[";
176: 	if ($feed['feedname']!="") $out.=$feed['feedname'];
177: 	else $out.="FreeNATS RSS Feed";
178: 	$out.="]]></title>\n";
179: 	$me=GetAbsolute("rss.php?feed=".$_REQUEST['feed']);
180: 	$out.="<link>".$me."</link>\n";
181: 	$out.="<atom:link href=\"".$me."\" rel=\"self\" type=\"application/rss+xml\" />\n";
182: 	$out.="<description>FreeNATS RSS Feed</description>\n";
183: 	$out.="<lastBuildDate>".date("D, d M Y H:i:s")." GMT</lastBuildDate>\n";
184: 	$out.="<language>en-us</language>\n";
185: 	
186: 	switch($feed['feedtype'])
187: 	{
188: 		case "all":
189: 		$nodes=$this->NATS->GetNodes();
190: 		break;
191: 		case "node":
192: 		$nodes=array();
193: 		$nodes[]['nodeid']=$feed['typeopt'];
194: 		break;
195: 		case "group":
196: 		$nodes=array();
197: 		$q="SELECT nodeid FROM fngrouplink WHERE groupid=".ss($feed['typeopt']);
198: 		$r=$this->NATS->DB->Query($q);
199: 		while ($row=$this->NATS->DB->Fetch_Array($r))
200: 		{
201: 			$nodes[]=$NATS->GetNode($row['nodeid']);
202: 		}
203: 		$this->NATS->DB->Free($r);
204: 		break;
205: 		default: $nodes=array();
206: 	}
207: 	
208: 	$inlist="";
209: 	$first=true;
210: 	foreach($nodes as $node)
211: 	{
212: 		if ($first) $first=false;
213: 		else $inlist.=",";
214: 		$inlist.="\"".ss($node['nodeid'])."\"";
215: 	}
216: 	
217: 	$alerts=array();
218: 	switch($feed['feedrange'])
219: 	{
220: 		case "xdays":
221: 		if (!is_numeric($feed['rangeopt'])) $days=30;
222: 		else $days=$feed['rangeopt'];
223: 		$secs = $days * 24 * 60 * 60;
224: 		$from = time() - $secs;
225: 		$q="SELECT * FROM fnalert WHERE (closedx=0 OR closedx>".$from.") AND nodeid IN (".$inlist.") ORDER BY LENGTH(closedx) ASC, closedx DESC";
226: 		break;
227: 		
228: 		case "xalerts":
229: 		if (!is_numeric($feed['rangeopt'])) $acount=30;
230: 		else $acount=$feed['rangeopt'];
231: 		$q="SELECT * FROM fnalert WHERE nodeid IN(".$inlist.") ORDER BY LENGTH(closedx) ASC, closedx DESC LIMIT 0,".$acount;
232: 		break;
233: 		
234: 		case "alerts":
235: 		$q="SELECT * FROM fnalert WHERE closedx=0 AND nodeid IN(".$inlist.")";
236: 		break;
237: 		
238: 		case "alertnode":
239: 		$q=array();
240: 		foreach($nodes as $node)
241: 		{
242: 			$q[]="SELECT * FROM fnalert WHERE nodeid=\"".ss($node['nodeid'])."\" ORDER BY LENGTH(closedx) ASC, closedx DESC LIMIT 0,1";
243: 		}
244: 		break;
245: 		
246: 	}
247: 	if (!is_array($q)) $q=array($q);
248: 	foreach($q as $query)
249: 	{
250: 		$r=$this->NATS->DB->Query($query);
251: 		while ($row=$this->NATS->DB->Fetch_Array($r))
252: 		{
253: 			$alerts[]=$row;
254: 		}
255: 		$this->NATS->DB->Free($r);
256: 	}
257: 	
258: 	foreach($alerts as $alert)
259: 	{
260: 		$title=$alert['nodeid']." ".$this->NATS->Lang->Item("alert");
261: 		if ($alert['closedx']!=0) $title.=" (".$this->NATS->Lang->Item("closed").")";
262: 		else $title.=" (".$this->NATS->Lang->Item("open").")";
263: 		$link=GetAbsolute("node.php?nodeid=".$alert['nodeid']);
264: 		if ($alert['closedx']==0) $pubtime=$alert['openedx'];
265: 		else $pubtime=$alert['closedx'];
266: 		$pub=date("D, d M Y H:i:s",$pubtime)." GMT";
267: 		
268: 		$q="SELECT logentry FROM fnalertlog WHERE alertid=".$alert['alertid']." ORDER BY alid ASC LIMIT 0,1";
269: 		$r=$this->NATS->DB->Query($q);
270: 		if ($row=$this->NATS->DB->Fetch_Array($r)) $desc=$row['logentry'];
271: 		else $desc="";
272: 		$this->NATS->DB->Free($r);
273: 		
274: 		$out.=$this->Item($title, $link, $pub, $desc);
275: 	}
276: 	
277: 	$out.="</channel>\n</rss>\n";
278: 	
279: 	return $out;
280: }
281: 		
282: 		
283: }
284: ?>