<?php

    
// nut.ricio.us ver 0.1 - personal web-based bookmark manager
    // Copyright (c) 2006 Toby A Inkster
    //
    // 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    //
    // http://www.gnu.org/copyleft/gpl.html

    
session_start();

    
$datafile 'bookmarks.dat';
    
$optsfile 'bookmarks.ini';
    
$stylfile 'bookmarks.css';
    
$me      'http://' $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
    
    
$pin      '12345';
    
$target   '_self';
    
    
header("Expires: Sun, 1 Jun 1980 12:00:00 GMT");
    
header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
    
header("Cache-Control: no-store, no-cache, must-revalidate");
    
header("Cache-Control: post-check=0, pre-check=0"false);
    
header("Pragma: no-cache");
    

    
/*******************************************
     **  Data parsing. Would normally do      **
     **  this in a function and call as       **
     **  required, but we're pretty much      **
     **  always going to need to the info,    **
     **  so just parse the file at start up.  **
     *******************************************/

    // Read in bookmark data file.
    
$flst  = array();
    
$ulst  = array();
    
$lines file($datafile);
    foreach (
$lines as $bookmark)
    {
        
$bookmark str_replace("\n"''$bookmark);
        list(
$title$url$folder)
            = 
explode("\t"$bookmark);
        
        if (isset(
$url))
        {
            
$node = array(
                
'title'  => $title,
                
'url'      => $url,
                
'folder' => $folder
            
);
            
            
$pieces explode('/'$folder);
            while (
$pieces)
            {
                
$x implode('/'$pieces);
                if (!isset(
$flst[$x]))
                    
$flst[$x] = array();
                
array_pop($pieces);
            }
                
            
array_push($flst[$folder], $node);
            
array_push($ulst$node);
        }
    }
    
    
// Sort it
    
foreach($flst as $f)
        
ksort($f);
    
ksort($flst);





    
/*******************************************
     **  Actually add a new bookmark.         **
     *******************************************/

    
if (isset($_REQUEST['add']))
    {
        if (
$_REQUEST['pin']==$pin)
        {
            
$title     $_REQUEST['title'];
            
$url     $_REQUEST['url'];
            
$folder    $_REQUEST['folder'];
            
            if (
strlen($_REQUEST['subfolder']))
                
$folder .= '/' $_REQUEST['subfolder'];
            
            
$l "{$title}\t{$url}\t{$folder}\n";
            
            if (!
$handle fopen($datafile'a'))
                die(
"<p><b>Cannot open file ($filename).</b></p>");
            if (
fwrite($handle$l) === FALSE
                die(
"<p><b>Cannot write to file ($filename).</b></p>");
            
fclose($handle);
            
            
$node = array(
                
'title'  => $title,
                
'url'      => $url,
                
'folder' => $folder
            
);
            
            
$pieces explode('/'$folder);
            while (
$pieces)
            {
                
$x implode('/'$pieces);
                if (!isset(
$flst[$x]))
                    
$flst[$x] = array();
                
array_pop($pieces);
            }
                
            
array_push($flst[$folder], $node);
            
array_push($ulst$node);
                        
            
showBookmarks($folder"Added bookmark <a href=\"$url\">$title</a>.");
        }
        
        else
            
showBookmarks($folder"Incorrect PIN.");
    }
    
    


    
/*******************************************
     **  "Add bookmark" form for bookmarklet. **
     *******************************************/
    
    
elseif (isset($_REQUEST['url']))
    {
        
$url     $_REQUEST['url'];
        
        if (
$page file($url))
        {
            
$page    implode("\n"$page);
            
$pieces preg_split('/<\/?title>/'$page);
            
$title    $pieces[1];
        }
        else
            
$title 'Bookmark';
        
        
showTop("Add Bookmark");
        
        print 
"<form action=\"{$me}\" method=\"POST\">\n";
        print 
"<table class=\"addbookmark\">\n";
        
printf("<tr><th scope=\"row\">Title:</th>"
            
."<td><input name=\"title\" value=\"%s\"></td></tr>\n",
            
htmlentities($title));
        
printf("<tr><th scope=\"row\">URL:</th>"
            
."<td><input name=\"url\" value=\"%s\"></td></tr>\n",
            
htmlentities($url));
        print 
"<tr><th scope=\"row\">Folder:</th>"
            
."<td>\n<select name=\"folder\">\n";
        foreach (
$flst as $folder=>$dummy)
            
printf("<option>%s\n"$folder);
        print 
"</select></td></tr>\n";
        print 
"<tr><th scope=\"row\">New Subfolder:</th>"
            
."<td><input name=\"subfolder\"></td></tr>\n";
        print 
"<tr><th>PIN:</th><td><input style=\"width:4em\" size=\"4\" name=\"pin\">"
            
."<input class=\"button\" name=\"add\" value=\"Add Bookmark\" type=\"submit\"></td></tr>\n";
        print 
"</table>\n";
        print 
"</form>\n";
        
        
showTail();
    }
    
    
    

    
/*******************************************
     **  Otherwise show bookmarks.            **
     *******************************************/

    
else
        
showBookmarks($_REQUEST['folder']);
    
    
    function 
showBookmarks ($folder$message='')
    {
        global 
$flst$target$me;
    
        
$pieces explode('/'$folder);
        
$title  array_pop($pieces);
        
$parent implode('/'$pieces);
        
        if (
$folder=='')
            
$title 'Bookmarks';
        
        
showTop($title);
        
        if (
$message!='')
            print 
"<div class=\"message\">{$message}</div>\n";
            
        print 
"<div class=\"path\">{$folder}/</div>\n";
        
        print 
"<ul>\n";
        if (
$folder)
            print 
"<li class=\"parent\"><a href=\"{$me}?folder="
                
urlencode($parent) . "\">[..]</a></li>\n";
        foreach (
$flst as $f=>$c)
        {
            
$remainder substr($fstrlen($folder)+1);
            
$begin1 substr($folder0strlen($folder));
            
$begin2 substr($f0strlen($folder));
            if (
$begin1==$begin2 && !strstr($remainder,'/') && $f!=$folder)
            {
                
$pieces explode('/'$f);
                
$t array_pop($pieces);
                print 
"<li class=\"folder\"><a href=\"{$me}?folder="
                    
urlencode($f) . "\">" htmlentities($t) . "</a></li>\n";
            }
        }
        if (isset(
$flst[$folder]))
            foreach (
$flst[$folder] as $bm)
                print 
"<li class=\"bookmark\"><a target=\"$target\" href=\"{$bm['url']}\">" 
                    
htmlentities($bm['title']) . "</a></li>\n";
        print 
"</ul>\n";

        print 
"<form action=\"{$me}\" method=\"POST\">\n";
        print 
"<table class=\"addbookmark\">\n";
        print 
"<tr><th scope=\"row\">Title:</th>"
            
."<td><input name=\"title\"></td></tr>\n";
        print 
"<tr><th scope=\"row\">URL:</th>"
            
."<td><input name=\"url\"></td></tr>\n";
        print 
"<tr><th scope=\"row\">New Subfolder:</th>"
            
."<td><input value=\"".htmlentities($folder)."\" type=\"hidden\" name=\"folder\">"
            
."<input name=\"subfolder\"></td></tr>\n";
        print 
"<tr><th>PIN:</th><td><input style=\"width:4em\" size=\"4\" name=\"pin\"> "
            
."<input class=\"button\" name=\"add\" value=\"Add Bookmark\" type=\"submit\"></td></tr>\n";
        print 
"</table>\n";
        print 
"</form>\n";

        
showTail();
    }






    
/*******************************************
     **  HTML generation functions.           **
     *******************************************/

    
function showTop ($title)
    {
        global 
$stylfile;
        print 
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
            
."\"http://www.w3.org/TR/html4/loose.dtd\">\n";
        print 
"<link href=\"{$stylfile}\" media=\"screen,projection\" rel=\"stylesheet\" type=\"text/css\">\n";
        print 
"<title>".htmlentities($title)." [[nut.ricio.us]]</title>\n";
        print 
"<h1>".htmlentities($title)."</h1>\n";
    }
    
    function 
showTail ()
    {
        global 
$me;
        print 
"<p class=\"bookmarklets\">\n<small>";
        print 
"<a href=\"javascript:void(location.href='{$me}?url='+escape(location.href));\">"
            
."\"Add Bookmark\" bookmarklet</a>.\n";
        print 
"</small></p>\n";
    }
?>