Update of /cvsroot/php-blog/serendipity/plugins/serendipity_event_spartacus
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14619/serendipity_event_spartacus
Added Files:
serendipity_event_spartacus.php
Log Message:
groundwork for SPARTACUS. See mailinglist.
--- NEW FILE: serendipity_event_spartacus.php ---
<?php # $Id: serendipity_event_spartacus.php,v 1.1 2004/12/29 14:06:05 garvinhicking Exp $
/************
TODO:
- If a plugin is already installed, try to detect if version numbers match. Show "update" icon in case of a newer version, show "already installed" in case of same version
- Perform Serendipity version checks to only install plugins available for version
- Allow fetching files from mirrors / different locations - don't use ViewCVS hack (revision 1.999 dumbness)
***********/
switch ($serendipity['lang']) {
case 'en':
default:
@define('PLUGIN_EVENT_SPARTACUS_NAME', 'Spartacus');
@define('PLUGIN_EVENT_SPARTACUS_DESC', '[S]erendipity [P]lugin [A]ccess [R]epository [T]ool [A]nd [C]ustomization/[U]nification [S]ystem - Allows you to download plugins from our online repository');
@define('PLUGIN_EVENT_SPARTACUS_FETCH', 'Click here to fetch a new %s from the Serendipity Online Repository');
@define('PLUGIN_EVENT_SPARTACUS_FETCHERROR', 'The URL %s could not be opened. Maybe the Serendipity or SourceForge.net Server is down - we are sorry, you need to try again later.');
@define('PLUGIN_EVENT_SPARTACUS_FETCHING', 'Trying to open URL %s...');
break;
}
class serendipity_event_spartacus extends serendipity_event
{
var $title = PLUGIN_EVENT_SPARTACUS_NAME;
function introspect(&$propbag)
{
global $serendipity;
$propbag->add('name', PLUGIN_EVENT_SPARTACUS_NAME);
$propbag->add('description', PLUGIN_EVENT_SPARTACUS_DESC);
$propbag->add('stackable', false);
$propbag->add('author', 'Garvin Hicking');
$propbag->add('version', '1.0');
$propbag->add('event_hooks', array(
'backend_plugins_sidebar_header' => true,
'backend_plugins_event_header' => true,
'backend_plugins_fetchlist' => true,
'backend_plugins_fetchplugin' => true
));
}
function generate_content(&$title) {
$title = $this->title;
}
function GetChildren(&$vals, &$i) {
$children = array();
$cnt = sizeof($vals);
while (++$i < $cnt) {
// compare type
switch ($vals[$i]['type']) {
case 'cdata':
$children[] = $vals[$i]['value'];
break;
case 'complete':
$children[] = array(
'tag' => $vals[$i]['tag'],
'attributes' => $vals[$i]['attributes'],
'value' => $vals[$i]['value']
);
break;
case 'open':
$children[] = array(
'tag' => $vals[$i]['tag'],
'attributes' => $vals[$i]['attributes'],
'value' => $vals[$i]['value'],
'children' => $this->GetChildren($vals, $i)
);
break;
case 'close':
return $children;
}
}
}
function &fetchfile($url, $target, $cacheTimeout = 0) {
printf(PLUGIN_EVENT_SPARTACUS_FETCHING, '<a href="' . $url . '">' . serendipity_truncateString($url, 68) . '</a>');
echo '<br />';
if (file_exists($target) && filesize($target) > 0 && filemtime($target) >= (time()-$cacheTimeout)) {
$data = file_get_contents($target);
} else {
require_once "HTTP/Request.php";
$req = &new HTTP_Request($url);
if (PEAR::isError($req->sendRequest()) || $req->getResponseCode() != '200') {
printf(PLUGIN_EVENT_SPARTACUS_FETCHERROR, $url);
if (file_exists($target) && filesize($target) > 0) {
$data = file_get_contents($target);
}
} else {
// Fetch file
$data = $req->getResponseBody();
$fp = @fopen($target, 'w');
if ($fp) {
fwrite($fp, $data);
fclose($fp);
} else {
printf(FILE_WRITE_ERROR, $target);
}
}
}
return $data;
}
function &fetchOnline($type) {
switch($type) {
// Sanitize to not fetch other URLs
default:
case 'event':
$url_type = 'event';
break;
case 'sidebar':
$url_type = 'sidebar';
break;
}
$url = 'http://netmirror.org/mirror/serendipity/package_' . $url_type . '.xml';
$cacheTimeout = 60*60*12; // XML file is cached for half a day
$target = dirname(__FILE__) . '/package_' . $url_type . '.xml';
$xml = $this->fetchfile($url, $target, $cacheTimeout);
echo '<br /><br />';
// XML functions
$p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($p, $xml, $vals, $index);
xml_parser_free($p);
$i = 0;
$tree = array();
$tree[] = array(
'tag' => $vals[$i]['tag'],
'attributes' => $vals[$i]['attributes'],
'value' => $vals[$i]['value'],
'children' => $this->GetChildren($vals, $i)
);
return $tree;
}
function &buildList(&$tree, $type) {
$plugins = serendipity_plugin_api::get_installed_plugins();
$pluginstack = array();
$i = 0;
foreach($tree[0]['children'] AS $idx => $subtree) {
if ($subtree['tag'] == 'package') {
$i++;
foreach($subtree['children'] AS $child => $childtree) {
switch($childtree['tag']) {
case 'name':
$pluginstack[$i]['plugin_class'] = $childtree['value'];
$pluginstack[$i]['class_name'] = $childtree['value'];
break;
case 'summary':
$pluginstack[$i]['name'] = $childtree['value'];
break;
case 'description':
$pluginstack[$i]['desc'] = $childtree['value'];
break;
case 'release':
$pluginstack[$i]['version'] = $childtree['children'][0]['value'];
break;
case 'maintainers':
$pluginstack[$i]['author'] = $childtree['children'][0]['children'][0]['value']; // I dig my PHP arrays ;-)
break;
}
}
$pluginstack[$i]['installable'] = !in_array($pluginstack[$i]['class_name'], $plugins);
$pluginstack[$i]['pluginPath'] = 'online_repository';
$pluginstack[$i]['customURI'] = '&serendipity[spartacus_fetch]=' . $type;
}
}
return $pluginstack;
}
function download(&$tree, $plugin_to_install) {
global $serendipity;
$pdir = $serendipity['serendipityPath'] . '/plugins/';
if (!is_writable($pdir)) {
printf(DIRECTORY_WRITE_ERROR, $pdir);
return false;
}
$files = array();
$found = false;
foreach($tree[0]['children'] AS $idx => $subtree) {
if ($subtree['tag'] != 'package') {
continue;
}
foreach($subtree['children'] AS $child => $childtree) {
if ($childtree['tag'] == 'name' && $childtree['value'] == $plugin_to_install) {
$found = true;
}
if (!$found || $childtree['tag'] != 'release') {
continue;
}
foreach($childtree['children'] AS $child2 => $childtree2) {
if ($childtree2['tag'] != 'serendipityFilelist') {
continue;
}
foreach($childtree2['children'] AS $idx => $_files) {
if ($_files['tag'] == 'file' && !empty($_files['value'])) {
$files[] = $_files['value'];
}
}
}
$found = false;
}
}
foreach($files AS $file) {
$url = 'http://cvs.sourceforge.net/viewcvs.py/*checkout*/php-blog/additional_plugins/' . $file . '?rev=1.9999';
$target = $pdir . $file;
@mkdir($pdir . $plugin_to_install);
$this->fetchfile($url, $target);
}
}
function event_hook($event, &$bag, &$eventData) {
global $serendipity;
$hooks = &$bag->get('event_hooks');
if (isset($hooks[$event])) {
switch($event) {
case 'backend_plugins_sidebar_header':
?>
<br /><a href="?serendipity[adminModule]=plugins&serendipity[adminAction]=addnew" class="serendipityIconLink"><img src="<?php echo serendipity_getTemplateFile('admin/img/install.png') ?>" style="border: 0px none ; vertical-align: middle; display: inline;" alt=""><?php echo sprintf(PLUGIN_EVENT_SPARTACUS_FETCH, SIDEBAR_PLUGIN) ?></a>
<?php
return true;
break;
case 'backend_plugins_event_header':
?>
<br /><a href="?serendipity[adminModule]=plugins&serendipity[adminAction]=addnew&serendipity[type]=event" class="serendipityIconLink"><img src="<?php echo serendipity_getTemplateFile('admin/img/install.png') ?>" style="border: 0px none ; vertical-align: middle; display: inline;" alt=""><?php echo sprintf(PLUGIN_EVENT_SPARTACUS_FETCH, EVENT_PLUGIN) ?></a>
<?php
return true;
break;
case 'backend_plugins_fetchlist':
$type = (isset($serendipity['GET']['type']) ? $serendipity['GET']['type'] : 'sidebar');
$eventData = array(
'pluginstack' => $this->buildList($this->fetchOnline($type), $type),
'errorstack' => array()
);
return false;
break;
case 'backend_plugins_fetchplugin':
if (!empty($eventData['spartacus_fetch'])) {
$this->download($this->fetchOnline($eventData['spartacus_fetch']), $eventData['install_plugin']);
$eventData['pluginPath'] = $eventData['install_plugin'];
}
return false;
break;
default:
return false;
break;
}
} else {
return false;
}
}
}
/* vim: set sts=4 ts=4 expandtab : */
?>
|