<?php /******************************************************************************
*
* this is the template for the default ad, you can change it however you
* want, note how the local state variables are echoed!!!! And note how the
* string begins with ?>
******************************************************************************/ $textad_template = <<<EOL ?><table onmouseover="this.style.cursor='hand'; window.status='visit <?php echo $this->textad[url]?>';return true" onmouseout="this.style.cursor='auto';window.status='';return true" onclick="location.href='<?php echo $_SERVER[SCRIPT_NAME]?>?textad_op=click&textad_url=<?php echo $this->textad[url] ?>&textad_sid=<?php echo $this->session_id ?>'" cellspacing=0 border=0 width="140" style="border: 1pt solid green">
<tr>
<td style="text-align: center; background-color: #dde; color: navy; font: bold small geneva, arial, sans-serif">
<a href="<?php echo $this->textad[url] ?>" style="color:navy; text-decoration:underline;">
<?php echo $this->textad[title] ?>
</a>
</td>
</tr>
<tr>
<td>
<p style="text-indent:0pt; font: 8pt geneva, arial, sans-serif; margin: 0px 4px">
<?php echo $this->textad[text] ?></p>
<p style="text-indent:0pt; color: #536580; font:bold small geneva, arial, sans-serif; margin-top:8px">
<?php echo $this->textad[url] ?></p>
</td>
</tr>
</table> EOL;
/*
CREATE TABLE users (
email varchar(100) NOT NULL PRIMARY KEY,
name tinytext NOT NULL,
password tinytext NOT NULL
);
CREATE TABLE textads (
id int(11) NOT NULL PRIMARY KEY auto_increment,
owner int(100) NOT NULL,
approved enum('Y','N') DEFAULT 'N' NOT NULL,
url tinytext NOT NULL,
title tinytext NOT NULL,
text tinytext NOT NULL,
impressions int(10) NOT NULL,
views int(10) NOT NULL
);
CREATE TABLE views (
session_id varchar(32) PRIMARY KEY NOT NULL,
textad_id int(11) NOT NULL,
date date NOT NULL,
clicked enum('Y','N') DEFAULT 'N' NOT NULL,
ip varchar(15) NOT NULL
);
*/
class textad_db_abstractions {
/****************************************************************************
* void textad_db_abstractions()
*
* the imaginary constructor for this class, calls this->constructor();
* this class is mysql oriented, but since the rest of the class calls this
* class' database functions, you should be able to reabstract it nicely. yay!
****************************************************************************/
function textad_db_abstractions(){
$this->constructor();
}
/****************************************************************************
* void constructor(string user, string password, string database,
* string host)
*
* the actual constructor for this class, calls the function connector
****************************************************************************/
function constructor($USER = 'user', $PASSWORD = 'password',
$DATABASE = 'textads', $HOST = 'localhost'){
$this->connector($USER,$PASSWORD,$DATABASE,$HOST);
}
/****************************************************************************
* void connector(string user, string password, string database, string host)
*
* connects to a database, gets a link and sets it in state.
****************************************************************************/
var $link;
function connector($USER, $PASSWORD, $DATABASE, $HOST){
$dc = @mysql_connect($HOST,$USER,$PASSWORD)
or $this->error();
$db = @mysql_select_db($DATABASE, $dc)
or $this->error();
$this->link = $dc;
}
/****************************************************************************
* void error(string error)
*
* adds a consistent style to mysql error messages and then exits.
****************************************************************************/
function error($ERROR = false){
if(!$ERROR){
echo "<p style='color: red; font: 8pt sans-serif'>";
echo "<b>".mysql_errno().":</b> ";
echo mysql_error()."</p>";
//exit;
}
echo "<p style='color: red; font: 8pt sans-serif'>";
echo $ERROR."</p>";
//exit;
}
/****************************************************************************
* resource query(string query)
*
* takes a query and uses the local state link that should have been set
* by now to perform the query. returns a result just like the regular
* mysql_query.
****************************************************************************/
function query($QUERY){
$result = @mysql_query($QUERY, $this->link);
return $result;
}
/****************************************************************************
* array fetch_array(resource result)
*
* works just like mysql_fetch_array
****************************************************************************/
function fetch_array($RESULT){
$array = mysql_fetch_array($RESULT);
return $array;
}
/****************************************************************************
* int num_rows(resource result)
*
* an abstraction (hahaha) of mysql_num_rows
****************************************************************************/
function num_rows($RESULT){
$rows = mysql_num_rows($RESULT);
return $rows;
}
/****************************************************************************
* bool data_seek(resource result_id, int row)
*
* just like with mysql_data_seek, rows start at zero. the functions in
* the main classes will make this very same assumption...
****************************************************************************/
function data_seek($RESULT, $ROW){
$res = mysql_data_seek($RESULT, $ROW);
return $res;
}
}
class textad_abstractions extends textad_db_abstractions {
/****************************************************************************
* void textad_abstractions()
*
* the generic constructor for this class, calls the function constructor
****************************************************************************/
function textad_abstractions(){
$this->constructor();
}
/****************************************************************************
* void constructor(string user, string password, string database,
* string host)
*
* the actual constructor for this class, calls the function connector
****************************************************************************/
function constructor(){
parent::constructor();
}
/****************************************************************************
* void set_session_id()
*
* creates a unique session id for the textad and sets it as a local class
* variable for future use by get_session_id
****************************************************************************/
var $session_id;
function set_session_id(){
list($msec, $sec) = explode(" ",microtime());
$randstring .= $msec * $sec;
$randstring .= $_SERVER[REMOTE_ADDR];
$this->session_id = md5($randstring);
}
/****************************************************************************
* int set_textad_id()
*
* using the provided link, this function will set the textad_id for this
* pageview.
****************************************************************************/
var $textad_id;
function set_textad_id(){
$query = "SELECT * FROM textads WHERE impressions > views";
$result = $this->query($query)
or $this->error();
$rows = ($this->num_rows($result) - 1);
if($rows < 0){
return false;
}
$rownum = mt_rand(0,$rows);
$row = $this->data_seek($result,$rownum);
$therow = $this->fetch_array($result);
return $this->textad_id = $therow[id];
}
/****************************************************************************
* void set_owner()
*
* sets the owner of an ad being edited by editing the database entry
* associated with the current textad_id
****************************************************************************/
function set_owner(){
$query = "UPDATE textads SET owner = {$this->current_user} ";
$query .= "WHERE id = {$this->textad_id}";
$result = $this->query($query);
}
/****************************************************************************
* void set_approved()
*
* approves an ad (in future should this lock an ad?)
****************************************************************************/
function set_approved(){
$query = "UPDATE textads SET approved = 'y' WHERE id = {$this->textad_id}";
$result = $this->query($query);
}
/****************************************************************************
* void set_clicked()
*
* clicks an ad
****************************************************************************/
function set_clicked(){
$query = "UPDATE views SET clicked = 'Y' ";
$query .= "WHERE session_id = '{$this->session_id}'";
$result = $this->query($query);
}
/****************************************************************************
* resource set_field(string mode, string data)
*
* updates the database to set mode = data given some 'precautionary'
* conditions. mode can be any of 'url','title',or 'text'. returns a resource
* which can be treated as a boolean to see if the query worked, or used with
* mysql error message functions.
****************************************************************************/
function set_field($MODE,$DATA){
$query = "UPDATE textads SET {$MODE} = {$DATE} ";
$query .= "WHERE id = {$this->textad_id} AND owner = {$this->current_user}";
$result = $this->query($query);
return $result;
}
/****************************************************************************
* void init_view()
*
* creates a view that has not been clicked yet using a session id and a
* textad_id that it also makes up and sets BOTH in state.
****************************************************************************/
function init_view(){
$this->set_textad_id();
if(!isset($this->textad_id)){
return false;
}
$this->set_session_id();
$query = "INSERT INTO views (session_id,textad_id,date,ip)";
$query .= "VALUES ('{$this->session_id}',{$this->textad_id},current_date,";
$query .= "'$_SERVER[REMOTE_ADDR]')";
$result = $this->query($query);
$query2 = "UPDATE textads SET views = (views + 1) ";
$query2 .= "WHERE id = {$this->textad_id}";
$result2 = $this->query($query2)
or $this->error();
}
/****************************************************************************
* string get_textad()
*
* gets the textad as an array and sets it in state as $this->textad
****************************************************************************/
var $textad;
function get_textad(){
if(!isset($this->textad_id)){
return false;
}
$query = "SELECT * FROM textads WHERE id = {$this->textad_id}";
$result = $this->query($query);
$this->textad = $this->fetch_array($result);
}
}
class textad_disp extends textad_abstractions {
/****************************************************************************
* void textad_disp()
*
* generic constructor
****************************************************************************/
var $template;
function textad_disp(){
$this->constructor();
}
/****************************************************************************
* void constructor()
*
* this class' constructor, constructs its parent class like it's supposed
* to and runs basic initialization.
****************************************************************************/
function constructor(){
parent::constructor();
$this->init_view();
$this->get_textad();
}
/****************************************************************************
* void display()
*
* displays the textad
****************************************************************************/
var $display;
function display(){
if(!isset($this->textad_id)){
return false;
}
ob_start();
eval($this->template);
$this->display = ob_get_contents();
ob_clean();
}
}
class textad_admin extends textad_abstractions {
/****************************************************************************
* void textad_admin()
*
* constructor
****************************************************************************/
function textad_admin(){
$this->constructor();
}
/****************************************************************************
* void constructor()
*
* a constructor that calls the parent constructor and then does its thing
****************************************************************************/
function constructor(){
parent::constructor();
}
}
/* protection from prying eyes (it's kinda stupid, you can remove it)
if(ereg('templates',$_SERVER[REQUEST_URI])){
header('location: /');
}
*/
/* Things to do if we get a clickthru or a login*/ if(isset($_GET[textad_op])){
switch($_GET[textad_op]){
case 'click':
$textad = new textad_abstractions;
$textad->session_id = $_GET[textad_sid];
$textad->set_clicked();
header("Location: http://".$_GET[textad_url]);
break;
}
}
//we assume that if there is no recognizable querystring if(!isset($_GET[textad_op])){
$textad = new textad_disp;
$textad->template = $textad_template;
$textad->display();
}
|
|