<?php

##########################################
#   phpMyLinks v2.1.0 - 19 Apr 2005  
##########################################
#
# Etilem - 20 Sep 2000 - http://etilem.net/phpmylinks/
#                                    
# quickly add, delete or modify your http links and follow any link 
# in the alphabetical list to reach your destination.               
#                                                                   
# see readme.txt for more information (install, copying, todo, etc) 
#                                                                   
##########################################



##########################################
#   Don't forget to edit config.php !!!  
##########################################

require "secret/config.php";

class PhpMyLinks {

	var $SQL_id;
	var $Hostname;
	var $Username;
	var $Password;
	var $Database;
	var $Table;
	var $Database_Schema;
	var $Table_Schema;
	var $Init_Schema;
	var $Debug;
	var $Messages = array();
	var $Link_id;
	var $Url;
	var $Text;

	## Constructor
	function PhpMyLinks($host, $user, $pass, $data, $table, $debug, $messages, $mode = "", $id_link = 0, $url = "", $link = "") {
		
		$this->Hostname		= $host;
		$this->Username		= $user;
		$this->Password		= $pass;
		$this->Database		= $data;
		$this->Database_Schema	= sprintf("CREATE DATABASE %s", $this->Database);
		$this->Table		= $table;
		$this->Table_Schema = sprintf("CREATE TABLE %s ( id bigint(20) NOT NULL auto_increment, url text NOT NULL, link text NOT NULL, time timestamp(14), UNIQUE id (id) )", $this->Table);
		$this->Init_Schema	= sprintf("INSERT INTO %s VALUES ( '0', '', '', NULL)", $this->Table);
		$this->Debug		= $debug;
		$this->Messages		= $messages;
		$this->Link_id		= $id_link;
		$this->Url			= $url;
		$this->Text			= $link;

		if( $this->SQL_id = mysql_pconnect($this->Hostname, $this->Username, $this->Password) ) {
			if ($this->Debug) print("<CODE>debug : connected<br></CODE>");
		    $this->_select_database();
    		$this->_check_table();
		}
		else { 
			die(mysql_errno()." : ".mysql_error()."<BR>");
		}

		switch($mode) {
			case "add":
				$this->_display_add();
				break;
			case "mod":
				$this->_display_mod();
				break;
			case "del":
				$this->_display_del();
				break;
			case "sto":
				$this->_display_sto();
				break;
			default:
				$this->_display_links();
		}

	}

	## select database (create if needed) ##
	function _select_database() {
	
		if( mysql_select_db($this->Database, $this->SQL_id) ) {
			if($this->Debug) printf("<CODE>debug : database \"%s\" selected</CODE><br>", $this->Database);
		}
		elseif( mysql_query($this->Database_Schema, $this->SQL_id) ) {
			if($this->Debug) printf("<CODE>debug : database \"%s\" created<br></CODE>", $this->Database);
			$this->_select_database();
		}
		else {
			die(mysql_errno()." : ".mysql_error()."<BR>");
		}

	}

	## check if table exists (create if needed) ##
	function _check_table() {
	
		$result = mysql_list_tables($this->Database, $this->SQL_id);
		$need_create = true;
		for ($i = 0; $i < mysql_num_rows($result); $i++) {
			$table = strtolower( mysql_tablename($result, $i) );
			if ( $table == strtolower($this->Table) ) { # table exists
				if ($this->Debug) printf("<CODE>debug : table %s exists</CODE><BR>", $this->Table);
				$need_create = false;
			}
		}
		if ($need_create) { # no table found, attempt to create
			$query = $this->Table_Schema;
			$this->mysql_ask($query);
		}
		
		$query = sprintf("select * from %s", $this->Table);
		$result = $this->mysql_ask($query);
		if (mysql_num_rows($result) == 0) {
			$query = $this->Init_Schema;
			$this->mysql_ask($query);
		}

	}

	## query host ##
	function mysql_ask($query) {

        if ($this->Debug) printf("<CODE>debug : %s </CODE><BR>", $query);
		$result = mysql_query($query, $this->SQL_id) or die (mysql_errno()." : ".mysql_error()."<BR>");
		return $result;

	}

	function _add_proto($url) {

		if (substr($url, 0, 6) == "ftp://") return $url;
		elseif (substr($url, 0, 7) == "http://") return $url;
		elseif (substr($url, 0, 8) == "https://") return $url;
		elseif (substr($url, 0, 9) == "gopher://") return $url;
		elseif (substr($url, 0, 7) == "news://") return $url;
		elseif (substr($url, 0, 4) == "ftp.") return "ftp://" . $url;
		else return "http://" . $url;

	}

	function _display_header() {

		$html  = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>\n";
		$html .= "<html>\n";
		$html .= "<head>\n";
		$html .= sprintf("<title>%s</title>\n", $this->Messages['message']);
		$html .= "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>\n";
		$html .= "<style type='text/css'>\n";
		$html .= "<!--\n";
		$html .= "body {\n";
		$html .= "	background-color: #FFFFFF;\n";
		$html .= "	color: #000000;\n";
		$html .= "	font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;\n";
		$html .= "	font-size: 10px;\n";
		$html .= "}\n";
		$html .= "a:link, a:visited, a:active {\n";
		$html .= "	background-color: inherit;\n";
		$html .= "	color: #6666CC;\n";
		$html .= "	text-decoration: none;\n";
		$html .= "}\n";
		$html .= "a:hover {\n";
		$html .= "	background-color: inherit;\n";
		$html .= "	color: #FF0000;\n";
		$html .= "	text-decoration: underline;\n";
		$html .= "}\n";
		$html .= "//-->\n";
		$html .= "</style>\n";
		$html .= "</head>\n";
		$html .= "\n";
		$html .= "<body>\n";
		print $html;

	}

	function _display_footer() {

		$html  = "</body>\n";
		$html .= "\n";
		$html .= "</html>\n";
		print $html;

	}

	function _display_links() {

		$this->_display_header();
		$html = sprintf("<div align='%s'>\n", $this->Messages['table_align']);
		$html .= sprintf("<h3>%s</h3>\n", $this->Messages['message']);
		$html .= "</div>\n";
		$html .= sprintf("<table border='0' cellspacing='0' cellpadding='0' align='%s'>\n", $this->Messages['table_align']);
		$html .= "<tr>\n";
		$html .= sprintf("	<td align='%s'>\n", $this->Messages['cells_align']);
		$html .= "		&nbsp;\n";
		$html .= "	</td>\n";
		$html .= "</tr>\n";
		$query = sprintf("select id, url, link from %s order by link", $this->Table);
		$result = $this->mysql_ask($query);
		while ( $row =  mysql_fetch_object($result) ) {
			$seen[strtoupper(substr($row->link, 0, 1))][] = $row->id;
			$link[$row->id] = $row->link;
			$url[$row->id] = $row->url;
		}
		$html .= "<tr>\n";
		$html .= sprintf("	<td align='%s'>\n", $this->Messages['cells_align']);
		foreach (array_keys($seen) as $letter) {
			$html .= sprintf("		<a href='#%s'>%s</a>&nbsp;\n", $letter, $letter);
		}
		$html .= "	</td>\n";
		$html .= "</tr>\n";
		foreach (array_keys($seen) as $letter) {
			$html .= "<tr>\n";
			$html .= sprintf("	<td align='%s'>\n", $this->Messages['cells_align']);
			$html .= sprintf("		<a name='%s'>&nbsp;</a>\n", $letter);
			$html .= "	</td>\n";
			$html .= "</tr>\n";
			foreach (array_keys($seen[$letter]) as $id) {
				$uri = $this->_add_proto($url[$seen[$letter][$id]]);
				$html .= "<tr>\n";
				$html .= sprintf("	<td align='%s'>\n", $this->Messages['cells_align']);
				$html .= sprintf("		<a href='%s?act=add'>%s</a>\n", basename(__FILE__), $this->Messages['add_str']);
				$html .= sprintf("		<a href='%s?act=mod&id=%d'>%s</a>\n", basename(__FILE__), $seen[$letter][$id], $this->Messages['mod_str']);
				$html .= sprintf("		<a href='%s?act=del&id=%d'>%s</a>\n", basename(__FILE__), $seen[$letter][$id], $this->Messages['del_str']);
				$html .= "&nbsp;&nbsp;";
				$html .= sprintf("<a href='%s'>%s</a>", $uri, stripslashes($link[$seen[$letter][$id]]));
				$html .= "	</td>\n";
				$html .= "</tr>\n";
			}
		}
		$html .= "</table>\n";
		print $html;
		$this->_display_footer();
	}

	function _display_add() {

		$this->_display_header();
		$html  = sprintf("<form method='get' action='%s'>\n", basename(__FILE__));
		$html .= "<input type='hidden' name='act' value='sto'>\n";
		$html .= sprintf("<input type='hidden' name='id' value='%d'>\n", $this->Link_id);
		$html .= "<table>\n";
		$html .= "<tr>\n";
		$html .= "<td align='right'><b>Url : </b></td>\n";
		$html .= "	<td><input type='text' name='url' size='100' value=''></td>\n";
		$html .= "</tr>\n";
		$html .= "<tr>\n";
		$html .= "	<td align='right'><b>Text : </b></td>\n";
		$html .= "	<td><input type='text' name='link' size='100' value=''></td>\n";
		$html .= "</tr>\n";
		$html .= "<tr>\n";
		$html .= "	<td>&nbsp;</td>\n";
		$html .= sprintf("	<td><input type='submit' value='%s'></td>", $this->Messages['button_add']);
		$html .= "</tr>\n";
		$html .= "</table>\n";
		$html .= "</form>\n";
		print $html;
		$this->_display_footer();

	}


	function _display_mod() {

		$query = sprintf("select url, link from %s where id=%d", $this->Table, $this->Link_id);
		$result = $this->mysql_ask($query);
		$row =  mysql_fetch_object($result);
		$url = $row->url;
		$link = stripslashes($row->link);
		
		$this->_display_header();
		$html  = sprintf("<form method='get' action='%s'>\n", basename(__FILE__));
		$html .= "<input type='hidden' name='act' value='sto'>\n";
		$html .= sprintf("<input type='hidden' name='id' value='%d'>\n", $this->Link_id);
		$html .= "<table>\n";
		$html .= "<tr>\n";
		$html .= "<td align='right'><b>Url : </b></td>\n";
		$html .= sprintf("	<td><input type='text' name='url' size='100' value='%s'></td>\n", $url);
		$html .= "</tr>\n";
		$html .= "<tr>\n";
		$html .= "	<td align='right'><b>Text : </b></td>\n";
		$html .= sprintf("	<td><input type='text' name='link' size='100' value='%s'></td>\n", $link);
		$html .= "</tr>\n";
		$html .= "<tr>\n";
		$html .= "	<td>&nbsp;</td>\n";
		$html .= sprintf("	<td><input type='submit' value='%s'></td>", $this->Messages['button_mod']);
		$html .= "</tr>\n";
		$html .= "</table>\n";
		$html .= "</form>\n";
		print $html;
		$this->_display_footer();

	}

	function _display_sto() {

		if ($this->Link_id) {
			$query = sprintf("update %s set url='%s', link='%s', time=NULL where id=%d", $this->Table, $this->Url, addslashes($this->Text), $this->Link_id);
		}
		else {
			$query = sprintf("insert into %s values (0, '%s', '%s', NULL)", $this->Table, $this->Url, addslashes($this->Text));
		}
		$this->mysql_ask($query);
		header (sprintf("Location: %s", basename(__FILE__)));

	}

	function _display_del() {

	    $query = sprintf("delete from %s where id=%d", $this->Table, $this->Link_id);
	    $this->mysql_ask($query);
            header (sprintf("Location: %s", basename(__FILE__)));

	}

}

new PhpMyLinks($host, $user, $pass, $data, $table, $debug, $messages, $_GET['act'], $_GET['id'], $_GET['url'], $_GET['link']);

?>