一个用 GTK 与 MySQL 数据库管理新闻的程序

1 数据库为 gtk_php

create table code_news
(
id int(9) not null default 0 auto_increment,
title char(255) not null,
author char(30) not null,
author_contact char(75) not null,
news text,
PRIMARY KEY (id),
UNIQUE ID (id)
);

2 GTK 程序部分,用于图形化管理新闻 - admin.php
#!/usr/local/bin/php -q
<?

if(strtoupper(substr(PHP_OS03)) == 'WIN')
dl('php_gtk.dll');
else
dl('php_gtk.so');


function 
destroy()
{
Gtk::main_quit();
}

function 
handle_select($clist,$row,$column,$event)
{
global 
$ret$entry$SELECTED_ID;
$id $clist->get_text($row,0);
$SELECTED_ID $id;
}

function 
add_record()
{
global 
$entry$SELECTED_ID$fields$API_URL$USERNAME$PASSWORD;
while(list(
$f,$l) = each($fields))
{
$
$f $entry[$f]->get_text();
}
$news $entry['news']->get_chars(0,$entry['news']->get_length());

$file $API_URL.'?a=add&p='.$PASSWORD.'&u='.$USERNAME.
'&news[title]='.urlencode($title).'&news[author]='.urlencode($author).
'&news[author_contact]='.urlencode($author_contact).
'&news[news]='.urlencode($news);

$fp fopen($file,'r');
if(
$fp)
{
$contents trim(fread($fp,4096));
if(!
eregi("^0:[A-Z]*",$contents))
{
clear_entries();
update_clist();
}
else
{
echo 
"n$contentsn";
}
}
reset($fields);
}

function 
delete_record()
{
global 
$entry$SELECTED_ID$USERNAME$PASSWORD$API_URL;
$file $API_URL.'?u='.$USERNAME.'&p='.$PASSWORD.'&a=del&id='.$SELECTED_ID;
$fp fopen($file,'r');
if(
$fp)
{
$contents trim(fread($fp,4096));
if(!
eregi("^0:[A-Z]*",$contents))
{
update_clist();
}
else
{
echo 
"n$contentsn";
}
}
}

function 
update_record()
{
global 
$entry$SELECTED_ID;
echo 
"n************************* NOT IMPLEMENTED *************************n";
echo 
"nUPDATE RECORD : $SELECTED_IDn";
echo 
"n************************* NOT IMPLEMENTED *************************n";
}

function 
update_clist()
{
global 
$clist$API_URL$PASSWORD$USERNAME$ret;
// Grab everything from our API
$fp fopen($API_URL.'?a=get&p='.$PASSWORD.'&u='.$USERNAME,'r');
if(
$fp)
{
$contents fread($fp,4096);
if(!
ereg("^0:[A-Z]*",$contents))
{
$clist->clear();
$ret unserialize(trim($contents));
$clist->freeze();
while(list(
$id,$info) = each($ret))
{
// $info = $ret[$i];
$row = array($info['id'],$info['author'],$info['title']);
$clist->append($row);
}
$clist->thaw();
}
else
{
echo 
"nERROR: ".$contents."n";
}
}
}

function 
clear_entries()
{
global 
$entry;
$entry['title']->set_text('');
$entry['author']->set_text('');
$entry['author_contact']->set_text('');
// echo $entry['news']->get_text();
// $foo = $entry['news']->get_chars(0,$entry['news']->get_length());
// echo "n".$foo."n";
$entry['news']->backward_delete($entry['news']->get_length());
}

function 
delete_event()
{
return 
false;
}

$window = &new GtkWindow();
$window->connect('destroy','destroy');
$window->set_border_width(10);

$box = &new GtkVBox();
$window->add($box);
$box->show();

$fields = array(
title => 'Title: ',
author => 'Author: ',
author_contact => 'Author Contact: ');

// Set up our regular text fields
while(list($f,$l) = each($fields))
{
$vbox = &new GtkVBox();
$box->pack_start($vbox,false,false);
$vbox->show();
$vbox->set_border_width(3);

$hbox = &new GtkHBox();
$vbox->pack_start($hbox,false,false);
$hbox->show();

$label = &new GtkLabel("$l");
$label->set_usize(150,10);
$hbox->pack_start($label,false);
$label->show();

$entry[$f] = &new GtkEntry();

$hbox->pack_start($entry[$f],false);
$entry[$f]->show();
}

$vbox = &new GtkVBox();
$box->pack_start($vbox,false,false);
$vbox->show();
$vbox->set_border_width(3);

$label = &new GtkLabel('News:');
$vbox->pack_start($label);
$label->show();

$entry['news'] = &new GtkText();
$entry['news']->set_editable(TRUE);
$vbox->pack_start($entry['news']);
$entry['news']->show();

$buttons = array(
Add => 'add_record',
Delete => 'delete_record',
Update => 'update_record',
Clear => 'clear_entries',
Refresh => 'update_clist',
Quit => 'destroy');

$hbox = &new GtkHBox();
$hbox->set_border_width(3);
$vbox->pack_start($hbox,false,false);
$hbox->show();

while(list(
$l,$f) = each($buttons))
{
$button = &new GtkButton($l);
$button->connect('clicked',$f);
$hbox->pack_start($button);
$button->show();
}

$scrolled_win = &new GtkScrolledWindow();
$scrolled_win->set_border_width(5);
$scrolled_win->set_policy(GTK_POLICY_AUTOMATICGTK_POLICY_AUTOMATIC);

$titles = array('ID','Author','Title');
$clist = &new GtkClist(sizeof($titles),$titles);
$scrolled_win->add($clist);
$clist->show();

$USERNAME md5('joestump');
$PASSWORD md5('phpgtk');
$API_URL 'http://127.0.0.1/gtk/news_api/api.php';

// Grab everything from our API
update_clist();

$clist->set_usize(400,300);
$clist->connect('select-row','handle_select');

$auto_resize = array(0,1);
for(
$i $i sizeof($auto_resize) ; ++$i)
$clist->set_column_auto_resize($auto_resize[$i],true);

$vbox->pack_start($scrolled_win);
$scrolled_win->show();

$window->show_all();
Gtk::main();

?>

3 用于执行管理程序,向数据库加入数据的代码 api.php
<?

// Change username and password accordingly
$link mysql_connect('192.168.192.47','root','')
or die(
'0:DB ERROR');

mysql_select_db('gtk_php',$link) or die('0:DB ERROR');

// Create a username and password to protect the
// API from outside people. THIS IS VERY IMPORTANT!
$USERNAME md5('joestump');
$PASSWORD md5('phpgtk');

switch(
$a)
{
case 
'add':
// Make sure we are getting the 4 fields
// we want
if(sizeof($news) == 4)
{
// Check the given username and password against
// the ones we have - $p and $u are md5()'d in the
// GTK app and then sent along so no one can easily
// decrypt them.
if($p == $PASSWORD && $u == $USERNAME)
{
// extract($news);
while(list($key,$val) = each($news))
$
$key addslashes($val);

$sql "INSERT INTO code_news
(title,author,author_contact,news)
VALUES
('$title','$author','$author_contact','$news')"
;

$r mysql_query($sql);
if(
$r)
{
if(
mysql_affected_rows($r))
echo 
'1:SUCCESS';
else
echo 
'0:FAILURE';
}
}
else
{
echo 
'0:BAD PASSWORD';
}
}
else
{
echo 
'0:BAD INFO ARRAY';
}
break;
case 
'get':
if(
$p == $PASSWORD && $u == $USERNAME)
{
$sql "SELECT * FROM code_news";
$r mysql_query($sql);
if(@
mysql_num_rows($r))
{
while(
$row mysql_fetch_assoc($r))
$ret[$row['id']] = $row;
echo 
serialize($ret);
}
else
{
echo 
'0:NO RECORDS';
}
}
break;
case 
'del':
if(
$id)
{
if(
$p == $PASSWORD && $u == $USERNAME)
{
$sql "SELECT * FROM code_news WHERE id='$id'";
$r mysql_query($sql);
if(@
mysql_num_rows($r))
{
$sql "DELETE FROM code_news WHERE id='$id'";
$r mysql_query($sql);
include(
'http://'.$SERVER_NAME.$PHP_SELF.'?u='.$USERNAME.'&p='.$PASSWORD.'&a=get');
}
else
{
echo 
'0:ID NOT ON RECORD';
}
}
}
else
{
echo 
'0:NO ID GIVEN';
}
break;
default:
echo 
'0:UNRECOGNIZED ACCESS';
}

?>

4 显示新闻 不用多说了- news.php 
<?

// Change username and password accordingly
$link mysql_connect('192.168.192.47','root','')
or die(
'0:DB ERROR');

mysql_select_db('gtk_php',$link) or die('0:DB ERROR');

$sql "SELECT * FROM code_news";
$r mysql_query($sql);

if(@
mysql_num_rows($r))
{
while(
$row mysql_fetch_array($r))
{
extract($row);
echo 
'<b>'.$title.'</b>
'
;

if(
ereg("@",$author_contact))
$href 'mailto:'.$author_contact;
else
$href $author_contact;

echo 
'Posted By: <a href="'.$href.'">'.$author.'</a>
'
;
echo 
$news.'<hr>';
}
}
else
{
echo 
'NO NEWS!';
}

?>