Source for file CMS3_PlainHTML.php
Documentation is available at CMS3_PlainHTML.php
// CMS3 - A Three Content Management System.
// Copyright (C) 2007 Jop... (Jonas F. Jensen).
// 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.
*This file defines CMS3_PlainHTML
* @author Jonas F. Jensen <jopsen@gmail.com>
* @copyright 2007 Jonas F. Jensen.
* @license http://www.gnu.org/licenses/gpl.txt
* The PlainHTML plugin provides ability to create plain HTML pages.
//ISystemCallback implementation
* Handles a callback from ajax application.
* @param string data Data attached to this callback
//Callbacks from page editor
if($_POST["TODO"] ==
"Create"){
$Query =
"INSERT INTO CMS3_PlainHTML (Identifier, Body) VALUES ('" .
$_POST["Identifier"] .
"','" .
$_POST["Content"] .
"')";
if($_POST["TODO"] ==
"Modify"){
$Query =
"UPDATE CMS3_PlainHTML SET Body = '" .
$_POST["Content"] .
"' WHERE Identifier = '" .
$_POST["Identifier"] .
"' limit 1";
if(mysql_query($Query, $this->Owner->GetDatabaseHandle())){
$Output =
"Could not save data, please try agian or check the identifier.";
if($_POST["IsAjaxCallback"] ==
"true"){
$Link =
$this->Owner->GetCMS3URL() .
"System/Edit/" .
$this->GetPluginID() .
"/" .
$_POST["Identifier"];
print
"<html><head><META http-equiv='refresh' content='5;URL=$Link'></head><body><h2>$Output</h2><a href='$Link'>Click here if you not automaticly redirected in 5 secounds...</a><br><i>It's recommended to activate javascript while using CMS3 backend.</i></body></html>";
//Some callbacks from page listings
}elseif($data ==
"GotoPage"){
if($_POST["Submitter"] ==
"Create/Edit"){
header("Location: " .
$this->Owner->GetCMS3URL() .
"System/Edit/" .
$this->GetPluginID() .
"/" .
$_POST["Identifier"]);
}elseif($_POST["Submitter"] ==
"Show"){
header("Location: " .
$this->Owner->GetCMS3URL() .
$Namespace .
"/" .
$_POST["Identifier"]);
$Output =
"No namespace registed for this plugin";
print
"<html><head><META http-equiv='refresh' content='5;URL=$Link'></head><body><h2>$Output</h2><a href='$Link'>Click here if you not automaticly redirected in 5 secounds...</a><br><i>It's recommended to activate javascript while using CMS3 backend.</i></body></html>";
//None ajax delete callback
}elseif($_POST["Submitter"] ==
"Delete"){
$Output =
"Page successfully deleted";
$Output =
"Page could not be deleted";
print
"<html><head><META http-equiv='refresh' content='5;URL=$Link'></head><body><h2>$Output</h2><a href='$Link'>Click here if you not automaticly redirected in 5 secounds...</a><br><i>It's recommended to activate javascript while using CMS3 backend.</i></body></html>";
}elseif($data ==
"Delete"){
print
"Page named: ".
$_POST["Identifier"] .
" has been successfully deleted...";
print
"Error: Page named: ".
$_POST["Identifier"] .
" could not be deleted...";
//Callback from config to save namespace (Ajax)
}elseif($data ==
"SaveNamespace"){
//Does old namespace exist?
//TODO: remove illegal chars...
//Try to register new namespace
print
"Registration failed, make sure you have chosen a unique namespace.";
//Unregister current namespace (should not be able to fail)
if($this->Owner->UnregisterNamespace($OldNS)) //NOTE: a plugin can't have two namespaces due to database layout.
//TODO: remove illegal chars...
//Try to register new namespace
//Try to reregister old namespace
print
"Registration failed, make sure you have chosen a unique namespace. You still have the old namespace.";
print
"Registration failed, make sure you have chosen a unique namespace. You don't have any namespace now.";
//Not owning any namespace
//Could not unregister current namespace
$msg =
"Unregistration of current namespace failed.";
//Callback from config to save default page (Ajax)
}elseif($data ==
"SaveDefaultPage"){
$Settings["DefaultPage"] =
$_POST["DefaultPage"];
$msg =
"Failed to save settings.";
//Log this since it should not be happing:
print
"Error: 404, data not found.";
*@param string Identifier Title of the page to delete
*@return bool True/false, depending on success
$Query =
"DELETE FROM CMS3_PlainHTML WHERE Identifier = '$Identifier' limit 1";
//TODO: Log error on fail
return mysql_query($Query, $this->Owner->GetDatabaseHandle());
//IProvidesContent implementation
* Print the page of a given identifier.
* @param string identifier Identifier of the requested page.
* @param IOutputHandler OutputHandler OutputHandler used the parse the output. You may disable this OutputHandler with
* OutputHandler->Disable() and print the content manually.
public function &GetPage( $identifier, &$OutputHandler ){
//Handle identifier = "", by use of settings
$identifier =
$Settings["DefaultPage"];
//Find the page requested
$Query =
"SELECT Body FROM CMS3_PlainHTML WHERE Identifier = '$identifier' limit 1";
$Result =
mysql_query($Query, $this->Owner->GetDatabaseHandle());
if(mysql_num_rows($Result) !=
0){
$Row =
mysql_fetch_array($Result, MYSQL_ASSOC);
$OutputHandler->SetBody($Row["Body"]);
$OutputHandler->SetTitle($identifier);
$CookieName =
"CMS3_" .
$this->GetPluginID() .
"_" .
$identifier;
if($_COOKIE[$CookieName] !=
"True"){
//Set cookie so we may remember where we are
setcookie($CookieName, "True"); //TODO Set expire, path and domain to avoid conficts
$Query =
"UPDATE CMS3_PlainHTML SET Visits=(Visits + 1) WHERE Identifier = '$identifier' limit 1";
if(!mysql_query($Query, $this->Owner->GetDatabaseHandle())){
$this->Owner->LogMySQL($this->GetPluginID(), "Unable to update counter at mysql_query", $this->Owner->GetDatabaseHandle());
//Parse content (Must be done after statistics since it requires header communication)
$OutputHandler->PrintError("Page not found in collection$identifier.", 404);
//IProvidesEditor implementation
* Gets an Ajax guide for modification of the content.
* @param IEmbedmentOutputHandler EmbedmentOutputHandler Outputhandler the content MUST be parse to.
* @return IEmbedmentOutputHandler
public function &ModifyContent($Identifier =
null, &$EmbedmentOutputHandler){
//Printing the editor for an entry
$Query =
"SELECT Body FROM CMS3_PlainHTML WHERE Identifier = '$Identifier' limit 1";
$Result =
mysql_query($Query, $this->Owner->GetDatabaseHandle());
if(mysql_num_rows($Result) !=
0){
$Row =
mysql_fetch_array($Result, MYSQL_ASSOC);
$PageBody =
$Row["Body"];
$PageBody =
"New page\n<br/>\n<br/>\n<br/>";
$Head =
"<script type='text/javascript' src='" .
$this->Owner->GetCMS3URL() .
"bin/System/js/dojo/src/lfx/html.js'></script>";
$Body .=
"<h2>$TODO: $Identifier</h2><form id='PlainHTML' action='" .
$this->Owner->GetCMS3URL() .
"System/Callback/" .
$this->GetPluginID() .
"/Save' method='post'><input type='hidden' name='Identifier' value='$Identifier' /><textarea name='Content' dojoType='Editor2' style='width: 99%;background-color: white;border: 1px solid black;' rows=30 >$PageBody</textarea><br /><input type='submit' value='Save' /><input type='hidden' name='IsAjaxCallback' id='IsAjax' value='false' /><input type='hidden' name='TODO' id='TODO' value='$TODO' /></form>";
$EmbedmentOutputHandler->SetBody($Body);
$EmbedmentOutputHandler->ExtentHeader($Head);
//List all pages, enable deletion, editing and creation of new pages
$Query =
"SELECT Identifier FROM CMS3_PlainHTML ORDER by Identifier";
$Result =
mysql_query($Query, $this->Owner->GetDatabaseHandle());
if(mysql_num_rows($Result) !=
0){
while($Row =
mysql_fetch_array($Result, MYSQL_ASSOC))
$Options .=
"<option value='" .
$Row["Identifier"] .
"'>" .
$Row["Identifier"] .
"</option>\n";
$Query =
"SELECT Identifier,Visits FROM CMS3_PlainHTML ORDER by Visits";
$Result =
mysql_query($Query, $this->Owner->GetDatabaseHandle());
if(mysql_num_rows($Result) !=
0){
while($Row =
mysql_fetch_array($Result, MYSQL_ASSOC))
$Stats .=
"<tr><td>" .
$Row["Identifier"] .
"</td><td>" .
$Row["Visits"] .
"</td></tr>";
//Load template and fill in dynamic content
//Parse it to the output handler
$EmbedmentOutputHandler->ExtentHeader($Head);
$EmbedmentOutputHandler->SetBody($Body);
return $EmbedmentOutputHandler;
* Gets an absolute address of an icon for this plugin
return $this->Owner->GetCMS3URL() .
"bin/" .
$this->GetPluginID() .
"/writer.png";
return "Plain HTML pages";
//Implementation of ILinkable
* Gets a list of links that the plugins exposes
* Each entry in the array is an array with 3 fields, one for the display name, one for the URL of the page, and one for an array of sub entries. Each sub entry consits of 3 fields, just like the toplevel entries.
$URL =
$this->Owner->GetCMS3URL() .
$Namespace .
"/";
$Links =
array($Namespace,$URL, array());
$Query =
"SELECT Identifier FROM CMS3_PlainHTML ORDER by Identifier";
$Result =
mysql_query($Query, $this->Owner->GetDatabaseHandle());
if(mysql_num_rows($Result) !=
0){
while($Row =
mysql_fetch_array($Result, MYSQL_ASSOC))
$Links[2][] =
array($Row["Identifier"], $URL .
$Row["Identifier"] , null);
//Add toplevel array and return it
* Gets the pluginID of the plugin.
//IConfigure implementation
* Gets an absolute address of an icon for this plugin
return $this->Owner->GetCMS3URL() .
"bin/" .
$this->GetPluginID() .
"/config.png";
* Gets a dojo based ajax guide for configuration of the plugin.
* @param IEmbedmentOutputHandler OutputHandler
* @return IEmbedmentOutputHandler
public function &Configure($Identifier =
null, &$EmbedmentOutputHandler ){
//Load templates from files
$Query =
"SELECT Identifier FROM CMS3_PlainHTML ORDER BY Identifier";
$Result =
mysql_query($Query, $this->Owner->GetDatabaseHandle());
if(mysql_num_rows($Result) !=
0){
while($Row =
mysql_fetch_array($Result, MYSQL_ASSOC))
if($Settings["DefaultPage"] ==
$Row["Identifier"]){
$Selected =
" selected=\"selected\"";
$Options .=
"<option value='" .
$Row["Identifier"] .
"'" .
$Selected .
">" .
$Row["Identifier"] .
"</option>\n";
$EmbedmentOutputHandler->ExtentHeader($Head);
$EmbedmentOutputHandler->SetBody($Body);
return $EmbedmentOutputHandler;
*Opens the settings file for writting
//We're return be referance for performance issues...
$SettingsFile =
$this->Owner->GetCMS3Path() .
"etc/" .
$this->GetPluginID() .
"/settings.bin"; //.bin since it a binary serialized string
//Deserialize loaded data
*Save settings to files...
$SettingsFile =
$this->Owner->GetCMS3Path() .
"etc/" .
$this->GetPluginID() .
"/settings.bin"; //.bin since it a binary serialized string
$SettingsPath =
$this->Owner->GetCMS3Path() .
"etc/" .
$this->GetPluginID() .
"/";
//Create dir if not there
if(!mkdir($SettingsPath)){
//Log error if couldn't be created
//Check if file is writeable
//Log error if file isn't writeable
//Open the file for writing
if(!$Handle =
fopen($SettingsFile, "w")){
//Close filestream/handle
//Return true since we've saved the settings, doesn't really matter if file wasn't closed correctly
//Implementation of CMS3_Help_IProvidesHelp
*Gets a list of the help available for this plugin
*This method returns a list of toplevel subjects, only one or two toplevel subjects, each toplevel subject is an array with 3 fields number zero a title, number one the body text of the help page (in HTML) and number 2 is an array of subsubjects. Each subsubject consists of the same as the toplevel subjects.
*Return array List of help subjects and subsubjects
//TODO Serialize this and store it in data, then deserialize whenever loading... (Should be done at build time)
//Or Use CSV as defined in RFC4180 this could improve performance and maintainability
$TLA[0][1] =
"<i>A free plugin by Jonas F. Jensen.</i>";
$TLA[0][1] .=
"<p>The PlainHTML plugin will let create and display plain html pages.</p><p>Curretly there's no help text provided, since it takes an awfull long time to write... But this should prove the concept of plugin specific help pages, and sub help pages... Meaning that this text is provided using an implementation of CMS3_Help_IProvidesHelp on the CMS3_plainHTML class of the PlainHTML plugin.<br />As a final thought notice that the help plugin showing this text, reads from URL, making it possible for plugins to link directly to their (sub) help pages.</p>";
//Create array for subjects
//Create array for subject
$TLA[0][2][0][0] =
"Managing pages";
$TLA[0][2][0][1] =
"TODO: Subpages Create, Edit (Using editor) , Display, Delete";
$TLA[0][2][0][2] =
array();
//Create array for subject
$TLA[0][2][1][0] =
"Create pages";
$TLA[0][2][1][1] =
"TODO";
$TLA[0][2][1][2] =
array();
//Create array for subject
$TLA[0][2][2][0] =
"Display pages";
$TLA[0][2][2][1] =
"TODO";
$TLA[0][2][2][2] =
array();
//Create array for subject
$TLA[0][2][3][0] =
"Configuration";
$TLA[0][2][3][1] =
"TODO";
$TLA[0][2][3][2] =
array();
//Create array for subject
$TLA[0][2][4][0] =
"About";
$TLA[0][2][4][1] =
"TODO";
$TLA[0][2][4][2] =
array();
//Implementation of IInstall
* Runs a script to complet installation
* This creates database tables, and default data.
* @return bool True if installations script went okay, otherwise return false.
//Create some default settings
$Settings["DefaultPage"] =
"Mainpage";
//This is not a importent error, user will have to edit settings anyway, therefor we don't return false
$dbh =
$this->Owner->GetDatabaseHandle();
if(!mysql_query("CREATE TABLE IF NOT EXISTS `CMS3_PlainHTML` ( `Identifier` varchar(255) character set latin1 NOT NULL, `Body` longtext character set latin1 NOT NULL, `Visits` int(11) NOT NULL default '0', PRIMARY KEY (`Identifier`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_danish_ci", $dbh ))
$this->Owner->LogMySQL($this->GetPluginID(), "Error during creating of tables ", $dbh);
if(mysql_query("INSERT INTO `CMS3_PlainHTML` (`Identifier`, `Body`, `Visits`) VALUES ('Mainpage', '<h1>Mainpage</h1>You have just installed the PlainHTML plugin for CMS3,<br>enter the backend to start editing pages.<br><br><br>', 2)", $dbh ))
$this->Owner->LogMySQL($this->GetPluginID(), "Error during creating of sample data " ,$dbh);
//Implementation of IRemove
* Removes files and databases created after fysical installation
* @return bool true/false depending on success
//Delete potential settingsfile
$SettingsFile =
$this->Owner->GetCMS3Path() .
"etc/" .
$this->GetPluginID() .
"/settings.bin"; //.bin since it a binary serialized string
$sql =
'DROP TABLE CMS3_PlainHTML';
$dbh =
$this->Owner->GetDatabaseHandle();
if(mysql_query($sql, $dbh))
$this->Owner->LogMySQL($this->GetPluginID(), "Error during deletion of database table " ,$dbh);
Documentation generated on Mon, 30 Apr 2007 01:59:08 +0200 by phpDocumentor 1.3.1