Location: PHPKode > scripts > AvantGo Addon > avantgo-addon/avantgo.php
<?php
/**
  * avantgo.php - PDA/AvantGo addon v1.2 for phpWebSite v0.8.2			
  *
  * This addon will display the latest articles, poll results, links
  * and downloads available on your site on a page viewable to
  * those using AvantGo <www.avantgo.com>See README for info on 
  * setting this addon up.			
  *									
  * This addon was developed based on work by these developers:
  * Tim Litwiller - http://linux.made-to-order.net/	
  * Christopher Bradford hide@address.com  www.wpsf.com (pda.php)
  *	Fabian Rodriguez - http://sf.net/users/MagicFab/
  *
  * This program 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.
  *
  * @author Edward Ritter <hide@address.com
  * @version $Id: avantgo.php,v 1.3 2002/08/23 18:12:00 esritter Exp $
  * @module addons
  * @package phpWebSite
  */

// Set these variables
$maxage = 7200;	//max age in seconds for caching pages
$private = 0; 	//change to 1 to not cache this page at all

// set & create these files
$bwlogo = "/images/bwlogo.gif"; 	// 2 bit color depth (greyscale)
$clrlogo = "/images/colorlogo.gif"; // >2 bit color depth (color)

$colordepth = base64_decode(getenv("HTTP_X_AVANTGO_COLORDEPTH"));
$deviceos = base64_decode(getenv("HTTP_X_AVANTGO_DEVICEOS"));

// set article and links limits (how many to display)
$articles_limit = 10;
$links_limit = 10;

if (file_exists ('./mod/poll/poll_functions.php')) {
	$poll = 1;
}

if (file_exists ('./mod/downloads/downloads.php')) {
	$downloads = 1;
	$downloads_limit = 10; // # of downloads to display
}
  
if ($deviceos == "PALM_OS") {
        $os = "PalmOS";
} elseif ($deviceos == "WINCE_OS") {  
        $os = "WindowsCE";   
} else {      
        $os = "Generic OS";
}

if (!isset ($mainfile)) { 
    include ('mainfile.php'); 
    include ('config.php');
}

/**
  * head - displays header 
  *
  * @author - Edward Ritter
  */
function head() {
    global $sitename, $private, $maxage, $colordepth, $phpws_url, $titlebar, $titletag;
    header ("Content-Type: text/html");
    $priv = ($private == 1)?"private":"";
    header ("Cache-Control: max-age=$maxage, $priv");        
    echo "<html><head><title>$sitename</title>\n
		  <meta name=\"HandheldFriendly\" content=\"True\"></head><body>";
    $logo = ($colordepth > 2)?$clrlogo:$bwlogo;
	if (file_exists ($logo)) {
		$logo_size = getimagesize ($logo);
    	echo "<p align=\"center\"><a href=\"$phpws_url\">
	 		<img src=\"$logo\" $logo_size[3] alt=\"$titlebar\" border=\"0\">
			</a></p>";
	} 
	echo "<h3 align=\"center\">$sitename</h3>
		  <h4 align=\"center\">$titlebar<br>$titletag</h4><hr>\n"; 
}

/**
  * footer - displays footer
  * 
  * @author Edward Ritter
  */
function foot() {
    global $sitename, $phpws_url, $os;
    echo "</p><hr><p align=\"center\">($os)<br>For more content, visit 
	<a href=\"$phpws_url\">$sitename</a><br>Reference contact and 
	copyright information on the site.</p></body></html>";
}

/**
  * pda_box - displays contents in a simple table
  *
  * @author Edward Ritter
  * @param string title: title of box
  * @param string anchor: anchor of box
  * @param string content: content for box
  */
function pda_box($title, $anchor, $content) {
?>
<table align="center">
	<tr><td><a name="<?=$anchor?>"><b><?=$title?></b></td></tr>
	<tr><td><?=$content?></td></tr>
</table>
<?
}

/**
  * index - displays main content including head and foot.
  * 
  * @author Edward Ritter
  */
function index() {
    global $sitename, $phpws_url, $titlebar, $poll, $downloads, 
    	$articles_limit, $links_limit, $downloads_limit;
    head();
	new_articles($articles_limit);

	if ($poll) {
	    pda_poll();
	}
	new_links($links_limit);
	if ($download) {
		new_downloads($downloads_limit);
	}
	echo "<p><center>[ <a href=\"#articles\">Articles</a> ]"; 
	if ($poll) {
		echo "[ <a href=\"#poll\">Poll</a> ]";
	}
	echo "[ <a href=\"#links\">Links</a> ]";
	if ($downloads) {
		echo "[ <a href=\"#downloads\">Downloads</a> ]";
	}
	echo "</center></p>";
    foot();
}

/**
  * show - displays article
  *
  * @author Christoper Bradford <hide@address.com>
  * @param int sid: story id
  * @param int offset: offset
  */
function show($sid, $offset) {
	global $table_prefix;
	$result = mysql_query ("SELECT hometext, bodytext, title, time, 
		informant, topic FROM ".$table_prefix."stories WHERE sid=$sid") 
		or die ("Couldn't retrieve article");
	extract (mysql_fetch_array ($result));
	mysql_free_result ($result);
	$result = mysql_query ("SELECT topictext FROM ".$table_prefix."topics 
		WHERE topicid=$topic");
	if ($result) {
		extract (mysql_fetch_array ($result));
	}
	head();
	echo "<p><b>$title</b><br>";
	echo "::Posted by <b>$informant</b> on $time ($topictext)</p>";
	echo "<p>$hometext</p><p>$bodytext</p>";
	$story_link = "avantgo.php?op=show&sid=$sid&offset=";
	if ($offset >= 1) {
		mysql_free_result($result);
		$offset--;
		$result = mysql_query ("SELECT sid, title 
			FROM ".$table_prefix."stories ORDER BY sid DESC LIMIT $offset,1");
		if ($result) {
			extract (mysql_fetch_array ($result));
			echo "<p>Previous: <a href=\"$story_link$offset\">$title</a><br>";
		}
		$offset++;
	}
	$offset++;
	if ($offset <= 9) {
		mysql_free_result ($result);
		$result = mysql_query ("SELECT sid, title FROM ".$table_prefix."stories 
			ORDER BY sid DESC LIMIT $offset,1");
		if ($result) {
			if (mysql_num_rows($result)) {
				extract (mysql_fetch_array ($result));
				echo "Next: <a href=\"$story_link$offset\">$title</a></p>";
			}
		}
	}

	echo "<p><center>[ <a href=\"avantgo.php\">home</a> ]</center></p>";
	foot();
}

/**
  * new_articles - displays list of new articles
  * 
  * @author Edward Ritter
  * @param int limit: number of articles to show
  */
function new_articles($limit) {
	global $table_prefix;
	$boxtitle = "Latest Articles";
	$anchor = "articles";

	$result = mysql_query ("SELECT sid, title, time 
		FROM ".$table_prefix."stories ORDER BY sid DESC limit $limit") 
		or die ("Can't retrieve articles");

	while (@extract (mysql_fetch_array ($result))) {
		$content .= "&middot;&nbsp;<a href=\"$phpws_url/avantgo.php?op=show&sid=$sid&offset=$m\">$title</a>&nbsp;[$time]<br>\n";
	}
	pda_box($boxtitle, $anchor, $content);
}

/**
  * pda_poll - displays poll results
  * 
  * @author Edward Ritter
  */
function pda_poll() {
	global $table_prefix;
	include_once ('./mod/poll/poll.php');
	include_once ('./mod/poll/poll_config.php');
	
	$result = mysql_query ("SELECT pid FROM " . $table_prefix."mod_poll_flag");
	if ($result) {
		extract (mysql_fetch_array ($result));
	}

	$result = mysql_query ("SELECT data FROM " . $table_prefix."mod_poll_data
	 	WHERE pid='$pid'");
	if ($result) {
		extract (mysql_fetch_array ($result));
	}
	stripslashes ($data);
	$current_poll = new poll;
	$current_poll = unserialize ($data);

	$title = "Latest Poll Results";
	$anchor = "poll";

	$content .= "<table align=\"center\">\n<tr><td colspan=\"3\" align=\"center\">
		<b>". stripslashes ($current_poll->title)."</b></td></tr>\n";
	for ($i = 1; $i < $current_poll->option_count+1; $i++) {
		$option_text = $current_poll->options[$i-1];
		$option_votes = $current_poll->option_votes[$i-1];
		$total_votes = $current_poll->total_votes;
		if ($total_votes != 0) {
			$percent = ($option_votes / $total_votes) * 100;
		} else {
			$percent = 0;
		}
		$content .= "<tr><td>" . stripslashes ($option_text) . "</td><td>";
		$content .= number_format ($percent,2);
		$content .= " %</td><td align=\"right\">($option_votes)</td></tr>\n";
	}
	$content .= "<tr><td>Total Votes:</td><td colspan=\"2\" align=\"right\"><b>$total_votes</b></td></tr></table>";
	pda_box($title, $anchor, $content);
}

/**
  * new_links - displays new links added to the site
  *
  * @author Edward Ritter
  * @param int limit: how many links to display
  */
function new_links($limit) {
	global $table_prefix;
	$boxtitle = "Latest Links";
	$anchor = "links";
	$result = mysql_query ("SELECT title, url, description, category 
		FROM ".$table_prefix."mod_weblink_data ORDER BY date 
		DESC LIMIT $limit") or die ("Can't retrieve new links");
	while (@extract (mysql_fetch_array ($result))) {
		$content .= "&middot;&nbsp;<a href=\"$url\">$title</a> [$category] - $description<br>";
	}
	pda_box($boxtitle, $anchor, $content);
}
/**
  * new_downloads - displays new downloads on the site
  * 
  * @author Edward Ritter
  * @param int limit: how many downloads to show
  */
function new_downloads($limit) {
	global $table_prefix;
	$boxtitle = "Latest Downloads";
	$anchor = "downloads";
	$result = mysql_query ("SELECT title, description, version
		FROM ".$table_prefix ."mod_downloads_downloads ORDER BY 
		date DESC LIMIT $limit") or die ("Can't retrieve latest downloads");
	while (@extract (mysql_fetch_array ($result))) {
		$content .= "&middot;&nbsp;<b>$title ($version)</b> - $description<br>";
	}
	pda_box($boxtitle, $anchor, $content);
}

include_once ('counter.php');

switch($op) {
     
  case "show":
    show($sid, $offset);
    break;

  default:
    index();
    break;
} 

?>
Return current item: AvantGo Addon