summaryrefslogtreecommitdiff
path: root/php/leftbar.php
diff options
context:
space:
mode:
Diffstat (limited to 'php/leftbar.php')
-rw-r--r--php/leftbar.php186
1 files changed, 186 insertions, 0 deletions
diff --git a/php/leftbar.php b/php/leftbar.php
new file mode 100644
index 0000000..05e3213
--- /dev/null
+++ b/php/leftbar.php
@@ -0,0 +1,186 @@
+<?php
+/*
+ leftbar - Simple link manipulation.
+
+ Copyright (C) 2006 Ian Cowburn (ianc@noddybox.co.uk)
+
+ 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, or
+ (at your option) any later version.
+
+ This program 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.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+ -------------------------------------------------------------------------
+
+ $Id$
+
+*/
+
+class LB_LinkDef
+{
+ var $url;
+ var $text;
+
+ function LB_LinkDef($u,$t)
+ {
+ $this->url=$u;
+ $this->text=$t;
+ }
+}
+
+function LB_LeftHeader($hdr)
+{
+ printf('<h2 class="lb_header">%s</h2>',$hdr);
+}
+
+function LB_TopLink($url,$txt)
+{
+ printf('<li class="lb_linklist"><a class="lb_toplink" href="%s">%s</a></li>',$url,$txt);
+}
+
+function LB_SubLink($url,$txt)
+{
+ printf('<li class="lb_sublist"><a class="lb_sublink" href="%s">%s</a></li>',$url,$txt);
+}
+
+function LB_OpenLink(&$is_open)
+{
+ if (!$is_open)
+ {
+ $is_open = TRUE;
+ echo '<ul class="lb_toplink">';
+ }
+}
+
+function LB_OpenSub(&$is_open)
+{
+ if (!$is_open)
+ {
+ $is_open = TRUE;
+ echo '<li class="lb_linklist">';
+ echo '<ul class="lb_sublist">';
+ }
+}
+
+function LB_CloseLink(&$is_open)
+{
+ if ($is_open)
+ {
+ $is_open = FALSE;
+ echo '</ul>';
+ }
+}
+
+function LB_CloseSub(&$is_open)
+{
+ if ($is_open)
+ {
+ $is_open = FALSE;
+ echo '</ul>';
+ echo '</li>';
+ }
+}
+
+function LeftBar($file, $context = '')
+{
+ $fp = fopen($file,'r');
+
+ if (!$fp)
+ {
+ printf('<p>Missing leftbar config %s!</p>',$file);
+ return;
+ }
+
+ $links = array();
+ $curcontext = '';
+ $in_link = FALSE;
+ $in_sub = FALSE;
+
+ while (!feof($fp))
+ {
+ $line = fgets($fp,1024);
+
+ $line = rtrim($line);
+
+ if (strlen($line)>0 && substr($line,0,1)!='#')
+ {
+ $tok = explode("\t",$line);
+
+ switch($tok[0])
+ {
+ case 'def':
+ if (count($tok)==4)
+ {
+ $links[$tok[1]] = new LB_LinkDef($tok[3],$tok[2]);
+ }
+ else
+ {
+ printf('<p>WARNING: Bad "def"</p>');
+ }
+ break;
+
+ case 'set':
+ if (count($tok)==2)
+ {
+ LB_CloseSub($in_sub);
+ LB_CloseLink($in_link);
+ LB_LeftHeader($tok[1]);
+ }
+ else
+ {
+ printf('<p>WARNING: Bad "set"</p>');
+ }
+ break;
+
+ case 'link':
+ if (count($tok)==3)
+ {
+ LB_CloseSub($in_sub);
+ LB_OpenLink($in_link);
+ $ldef = $links[$tok[1]];
+ $curcontext = $tok[2];
+ LB_TopLink($ldef->url,$ldef->text);
+ }
+ else
+ {
+ printf('<p>WARNING: Bad "link"</p>');
+ }
+ break;
+
+ case 'sub':
+ if (count($tok)==2)
+ {
+ if ($curcontext == $context)
+ {
+ LB_OpenSub($in_sub);
+ $ldef = $links[$tok[1]];
+ LB_SubLink($ldef->url,$ldef->text);
+ }
+ }
+ else
+ {
+ printf('<p>WARNING: Bad "sub"</p>');
+ }
+ break;
+
+ default:
+ printf('<p>WARNING: Bad line "%s"</p>',$line);
+ break;
+ }
+ }
+ }
+
+ LB_CloseSub($in_sub);
+ LB_CloseLink($in_link);
+
+ fclose($fp);
+}
+?>