Skip to content

Commit

Permalink
0000327: Add plugin system
Browse files Browse the repository at this point in the history
  • Loading branch information
logart committed Dec 28, 2010
1 parent 2c9244c commit db67496
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 20 deletions.
168 changes: 168 additions & 0 deletions Plugin_class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php
/**
* Plugin_class should be used as an abstract class to create your own plugins.
* See the README file in the HelloWorld plugin folder for details
*
Copyright (C) 2010-2011 Stephen Lawrence Jr.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @author Stephen J. Lawrence Jr.
*/
class Plugin
{

var $name = '';
var $version = '';
var $author = '';
var $homepage = '';
var $description = '';
var $pluginslist = '';

function Plugin()
{
$name = $this->name;
$version = $this->version;
$author = $this->author;
$homepage = $this->homepage;
$description = $this->description;
$pluginslist = $this->pluginslist;
$this->loadPlugins();
}

/*
* INCLUDE ALL PLUGINS
* @return array $pluginslist An array of plugin names currently in the plug-ins folder
*/
function getPluginsList()
{
$pluginslist = array();
$curdir = dirname(__FILE__);
if ($handle = opendir($curdir . '/plug-ins'))
{
while (false !== ($file = readdir($handle)))
{
if ($file != '.htaccess' && $file != "." && $file != ".." && $file != '.svn' && is_file('plug-ins/' . $file . '/' . $file . '_class.php'))
{
array_push($pluginslist, $file);
}
}
$this->setPluginsList($pluginslist);
return $pluginslist;
}
}

/*
* Set the value for the pluginslist variable
*/
function setPluginsList($var)
{
$this->pluginslist = $var;
}

/*
* Include all the plugin class files
* @return true
*/
function loadPlugins()
{
foreach($this->getPluginsList() as $file)
{
include_once('plug-ins/' . $file . '/' . $file . '_class.php');
}
return true;
}

/*
* This function allows for new admin menu items to display for your plugin
*/
function onAdminMenu() {}

/*
* This function is run on the Add File page
*/
function onBeforeAdd() {}

/*
* This function is run on while the file is being added to the database
*/
function onDuringAdd() {}

/*
* This function is run after a new file is added
*/
function onAfterAdd() {}

/*
* This function is run before the edit file form is finished being rendered
*/
function onBeforeEditFile() {}

/*
* This function is run after the user saves and change to a file
*/
function onAfterEditFile() {}

/*
* This function is run after the user deletes a file (aka archive)
*/
function onAfterArchiveFile() {}

/*
* This function is run after the admin permanently deletes a file
*/
function onAfterDeleteFile() {}

/*
* This function is run before a user is logged in
*/
function onBeforeLogin() {}

/*
* This function is run after a user is logged in
*/
function onAfterLogin() {}

/*
* This function is run after the user session is cleared
*/

function onAfterLogout(){}

/*
* This function is called after a failed login
*/
function onFailedLogin(){}

/*
* This function is called after the user views a file
*/
function onViewFile() {}

/*
* This function is performed after a search has been initiated
*/
function onSearch() {}

/*
* This function is run at the top of the add user form
*/
function onBeforeAddUser() {}

/*
* This function is run after the add user form is saved
*/
function onAfterAddUser() {}
}
12 changes: 12 additions & 0 deletions add.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ function issetFlag()

</TR>
</TABLE>
<?php
// Call the plugin API
callPluginMethod('onBeforeAdd');
?>
<table>
<tr>
<td colspan="3" align="center"><div class="buttons"><button class="positive" tabindex=7 type="Submit" name="submit" value="Add Document"><?php echo msg('submit')?></button></div></td>
Expand Down Expand Up @@ -437,6 +441,10 @@ function issetFlag()
}
}
// all checks completed, proceed!

// Run the onDuringAdd() plugin function
callPluginMethod('onDuringAdd');

// INSERT file info into data table
$query = "INSERT INTO {$GLOBALS['CONFIG']['db_prefix']}data (status, category, owner, realname, created, description, department, comment, default_rights, publishable) VALUES(0, '" . addslashes($_REQUEST['category']) . "', '" . addslashes($_SESSION['uid']) . "', '" . addslashes($_FILES['file']['name']) . "', NOW(), '" . addslashes($_REQUEST['description']) . "','" . addslashes($current_user_dept) . "', '" . addslashes($_REQUEST['comment']) . "','" . addslashes($_REQUEST['default_Setting']) . "', $lpublishable )";

Expand Down Expand Up @@ -516,6 +524,10 @@ function issetFlag()
}
// back to main page
$message = urlencode(msg('message_document_added'));

// Call the plugin API
callPluginMethod('onAfterAdd');

header('Location: out.php?last_message=' . $message);
}
}
Expand Down
20 changes: 19 additions & 1 deletion admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@
?>
</tr>
</table>
<?php
if(is_array($GLOBALS['plugin']->getPluginsList()))
{
?>
<table border="1" cellspacing="5" cellpadding="5" >
<font color="#FFFFFF"><th bgcolor ="#83a9f7"><font color="#FFFFFF"><?php echo msg('label_plugins')?></th>
<tr>
<td>
<?php
//Perform the admin loop section to add plugin menu items
callPluginMethod('onAdminMenu');
?>
</td>
</tr>
</table>
<?php
}
?>
</center>
<?php
<?php
draw_footer();
4 changes: 4 additions & 0 deletions commitchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@
$mail_to = $new_user_obj->getEmailAddress();
mail($mail_to, $mail_subject, ($mail_greeting.' '.$mail_body.$mail_salute), $mail_headers);
$_POST['last_message'] = urlencode(msg('message_user_successfully_added'));

// Call the plugin API call for this section
callPluginMethod('onAfterAddUser');

header('Location: ' . $secureurl->encode('admin.php?last_message=' . $_POST['last_message']));
}
}
Expand Down
7 changes: 7 additions & 0 deletions delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
// delete from directory
// clean up and back to main page
$last_message = urlencode(msg('message_document_has_been_archived'));

// Call the plugin API call for this section
callPluginMethod('onAfterArchiveFile');

header('Location: out.php?last_message=' . $last_message);
}
elseif( isset($_REQUEST['mode']) && $_REQUEST['mode'] == 'pmntdel' )
Expand Down Expand Up @@ -130,6 +134,9 @@
}
}
}
// Call the plugin API call for this section
callPluginMethod('onAfterDeleteFile');

}
}
// delete from directory
Expand Down
13 changes: 11 additions & 2 deletions edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,12 @@ function issetFlag()
}

?>
</select></td>
</select>
</td>
<?php
// Call Plugin API
callPluginMethod('onBeforeEditFile');
?>
</tr>
</table>
<table>
Expand Down Expand Up @@ -542,7 +547,11 @@ function issetFlag()
// clean up
mysql_freeresult($result);
$message = urlencode('Document successfully updated');
header('Location: out.php?last_message=' . $message);

// Call the plugin API
callPluginMethod('onAfterEditFile');

header('Location: out.php?last_message=' . $message);
}
?>
<script type="text/javascript">
Expand Down
43 changes: 29 additions & 14 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ functions.php - various utility functions

include_once('version.php');

include_once('Plugin_class.php');
$plugin= new Plugin();

require_once('includes/smarty/Smarty.class.php');
$GLOBALS['smarty'] = new Smarty();
$GLOBALS['smarty']->template_dir = 'templates/' . $GLOBALS['CONFIG']['theme'] .'/';

$GLOBALS['smarty']->template_dir = dirname(__FILE__) . '/templates/' . $GLOBALS['CONFIG']['theme'] .'/';
$GLOBALS['smarty']->compile_dir = dirname(__FILE__) . '/templates_c/';
/**** SET g_ vars from Global Config arr ***/
foreach($GLOBALS['CONFIG'] as $key => $value)
{
Expand All @@ -46,9 +49,6 @@ functions.php - various utility functions
{
$GLOBALS['smarty']->assign('g_lang_' . $key, msg($key));
}
if( !defined('function') )
{
define('function', 'true', false);
// BEGIN FUNCTIONS
// function to format mySQL DATETIME values
function fix_date($val)
Expand Down Expand Up @@ -201,7 +201,6 @@ function draw_header($page_title)
{
echo '<span style="color: red;">' . msg('install_folder') . '</span>';
}

$GLOBALS['smarty']->assign('page_title', $page_title);
$GLOBALS['smarty']->display('header.tpl');
/*
Expand Down Expand Up @@ -972,13 +971,12 @@ function display_filesize($file)
}
function valid_username($username)
{
$unrx = '/^[a-zA-Z0-9]/'; // allow only letters and numbers. Limit 5 - 25 characters.
if(preg_match($unrx, $username))
return true;
else
$unrx = '/^[a-z]+[\w.-]*$/i'; // allow only letters and numbers. Limit 5 - 25 characters.
if(!preg_match($unrx, $username))
return false;
else
return true;
}
}

function cleanInput($input) {

Expand Down Expand Up @@ -1024,16 +1022,33 @@ function sanitizeme($input) {
* @param string $s
* @return string
*/
function msg($s) {
function msg($s){
if (isset($GLOBALS['lang'][$s]))
{
return $GLOBALS['lang'][$s];
}
}
else
{
//error_log("l10n error:LANG:" .
// $GLOBALS['CONFIG']['language']. ",message:'$s'");

return $s;
}
}

/*
* callPluginMethod
* @param string $method The name of the plugin method being envoked.
* @return null
*/
function callPluginMethod($method)
{
foreach ($GLOBALS['plugin']->pluginslist as $value)
{
if (!valid_username($value))
{
echo 'Sorry, your plugin ' . $value . ' is not setup properly';
}
$plugin_obj = new $value;
$plugin_obj->$method();
}
}
Loading

0 comments on commit db67496

Please sign in to comment.