Location: PHPKode > projects > ZompLog > plugins/mailnotify/install.php
<?php
/*
This Zomplog plugin notifies the administrator (or anyone else) when a new post is submitted, or a comment to a post
created by Frank Hoek

version 1.4, March 24th 2007
- I rewrote the coding concerning automatic upgrading

version 1.3, November 12th 2006
 - automatic upgrade
 - strip tags from the post for better presentation in the email

version 1.2, November 11th 2006
 - distinct messages for new post and new comment

www.frankma.nl
*/

// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// 0. THE USUAL INSTALL-STUFF
ob_start();

include_once("../../admin/functions.php");
include("../../admin/config.php");
include("../../admin/session.php");
include("../../admin/header.php");
?>

<div id="submenu"><a href="../../admin/plugins.php" class="sub">plugins</a></div>

<div id="side">
<div class="title">&nbsp;</div>
<div class="box"><?php
if(!empty($messages)){
	displayErrors($messages);
} elseif($_GET[message] && empty($messages)){
  	displayMessage($_GET[message]);
} else{
  	echo "Install plugin.";
}
?>
</div></div>

<div id="main">
<h1>Install plugin</h1><br />

<?php
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// 1. DEFINITION OF TEMPORARY VARIABLES
// define temporary variables with default values
$t_installed   = "no";
$t_address     = "hide@address.com";
$t_entrytitle  = "Post title:";
$t_name        = "Name:";
$t_day         = "Date:";
$t_post        = "Post:";
$t_comment     = "Comment:";
$t_entrysubj   = "A post has been added to your Zomplog";
$t_commentsubj = "A comment has been added to your Zomplog";
$t_version     = "1.4";

// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// 2. STORE OLD SETTINGS OF PLUGIN
// when no previous version of the plugin was installed, this part is skipped

// check for installed version of this plugin
$query = "SELECT * FROM $prefix" . "_mailnotify LIMIT 1";
$result = mysql_query($query, $link);

// when found do the following
if ($result) {
	echo "<div class='text'>previously installed version of this plugin found</div>";
	// set flag
	$t_installed = "yes";
	// replace temporary variables by existing variables in table _mailnotify
	$currentsetting = mysql_fetch_array($result);
	if ($currentsetting[address])     { $t_address     = $currentsetting[address]; };
	if ($currentsetting[entrytitle])  { $t_entrytitle  = $currentsetting[entrytitle]; };
	if ($currentsetting[name])        { $t_name        = $currentsetting[name]; };
	if ($currentsetting[day])         { $t_day         = $currentsetting[day]; };
	if ($currentsetting[post])        { $t_post        = $currentsetting[post]; };
	if ($currentsetting[comment])     { $t_comment     = $currentsetting[comment]; };
	if ($currentsetting[entrysubj])   { $t_entrysubj   = $currentsetting[entrysubj]; };
	if ($currentsetting[commentsubj]) { $t_commentsubj = $currentsetting[commentsubj]; };

	echo "<div class='text'><img src='../../install/images/check.gif'> settings from previous version saved</div>";
	
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// 3. REMOVE OLD VERSION OF PLUGIN COMPLETELY
	// remove existing table
	$query = "DROP TABLE $prefix" . "_mailnotify";
	$q = mysql_query($query) or die ("table $prefix"."_mailnotify could not be removed.<br /><br />"); 
	echo "<div class='text'><img src='../../install/images/check.gif'> previous table $prefix" . "_mailnotify removed</div>";

	// remove record for this plugin from table _plugins
	$query = "DELETE FROM $prefix"."_plugins WHERE name = 'mailnotify' LIMIT 1";
	$q = mysql_query($query) or die ("mailnotify-record in table $prefix"."_plugins could not be removed<br /><br />"); 
	echo "<div class='text'><img src='../../install/images/check.gif'> mailnotify-record in table $prefix"."_plugins removed<br />";
	echo "<br />ready to install new version of plugin <em>mailnotify</em></div><br />";
};

// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// 4. CREATE SETTINGS TABLE FOR THE PLUGIN
// create table which holds the emailadress that should be notified, and all other variables needed for this plugin
$query = "CREATE TABLE $prefix" . "_mailnotify (
id int (10 ) NOT NULL auto_increment ,
	address varchar (100 ) NOT NULL ,
	entrytitle varchar (25),
	name varchar (25),
	day varchar (25),
	post varchar (25),
	comment varchar (25),
	entrysubj varchar (100) NOT NULL,
	commentsubj varchar (100) NOT NULL,
	version varchar (10),
PRIMARY KEY (id )
)";

$q = mysql_query($query) or die ("Plugin could not be installed or has already been installed.<br /><br />"); 
echo "<div class='text'><img src='../../install/images/check.gif'> $prefix" . "_mailnotify created</div>";

// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// 5. PUT VALUES INTO THE SETTINGS TABLE
   $query = "INSERT INTO $prefix" . "_mailnotify (address,entrytitle,name,day,post,comment,entrysubj,commentsubj,version) VALUE ('$t_address','$t_entrytitle','$t_name','$t_day','$t_post','$t_comment','$t_entrysubj','$t_commentsubj','$t_version')";
   mysql_query($query) or die (mysql_error());
	// show applicable message
	if ($t_installed == "yes") {
		echo "<div class='text'><img src='../../install/images/check.gif'> your previous settings are added to table</div>";
	} else {
		echo "<div class='text'><img src='../../install/images/check.gif'> dummy settings are added to table</div>";
	};

// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// 6. HERE YOU PUT EVERYTHING ELSE FOR YOURPLUGIN
// when the installer has to do more than create and fill a settings table, then have it done here

// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// 7. ACTIVATE THE PLUGIN
   $query = "INSERT INTO $table_plugins (name,active,installed) VALUES ('mailnotify','1','1')";

   mysql_query($query) or die (mysql_error());

   echo "<div class='text'><img src='../../install/images/check.gif'> plugin set to active</div><br />";
	// and once again show applicable message
	if ($t_installed == "yes") {
		echo "<div class='text'><a href='admin.php'>you may have to update your settings</a></div><br />";
	} else {
		echo "<div class='text'><a href='admin.php'>personalize your settings</a></div><br />";
	};

echo "<div class='text'>go back to <a href='../../admin/plugins.php'>plugins-panel</a></div>";

// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// 8. PROPER ENDING OF THE INSTALLER
?>
</div>
<?php include ("../../admin/footer.php"); ?>
Return current item: ZompLog