Plugin: Phpbox.

Creating and modifying plugins.
wese
Regular
Posts: 8
Joined: Mon Nov 08, 2004 1:01 pm
Contact:

Plugin: Phpbox.

Post by wese » Mon Nov 08, 2004 1:09 pm

Good Day,

I just started to use S9y and i felt a bit low featured with an HTML Block alone..
So i made up a lil Plugin to add a PHP Block.

Features:
    Parses PHP Code entered no more no less.

Install:
    Create a plugin folder e.g.: serendipity_plugin_phpbox
    Put the code into the file: serendipity_plugin_phpbox.php
    Move to your adminpanel and add the "PhpBox" Plugin.


Code: Select all

<?php # $Id: serendipity_plugin_phpbox.php,v 1.0 2004/06/22 13:45:48 garvinhicking Exp $

switch ($serendipity['lang']) {
    case 'de': {
        @define('PLUGIN_SIDEBAR_PHPBOX_NAME', 'PhpBox');
        @define('PLUGIN_SIDEBAR_PHPBOX_DESC', 'PhpBox für PHP Code');
        @define('THE_PHPBOX', 'Auszuführende Code.');
        @define('TITLE_FOR_PHPBOX', 'Angezeigter box Titel.');
        }
    break;
    case 'en':
    default: {
        @define('PLUGIN_SIDEBAR_PHPBOX_NAME', 'PhpBox');
        @define('PLUGIN_SIDEBAR_PHPBOX_DESC', 'PhpBox for PHP Code.');
        @define('THE_PHPBOX', 'Code to be evaled.');
        @define('TITLE_FOR_PHPBOX', 'Box Title to be shown.');
    }
    break;
}

class serendipity_plugin_phpbox extends serendipity_plugin {
    function introspect(&$propbag)
    {
        $propbag->add('name',          PLUGIN_SIDEBAR_PHPBOX_NAME);
        $propbag->add('description',   PLUGIN_SIDEBAR_PHPBOX_DESC);
        $propbag->add('configuration', array(
                                        'title',
                                        'content',
                                        'markup',
                                        'show_where'
                                       )
        );

        $this->protected = TRUE; // If set to TRUE, only allows the owner of the plugin to modify its configuration
    }

    function introspect_config_item($name, &$propbag)
    {
        switch($name) {
            case 'title':
                $propbag->add('type',        'string');
                $propbag->add('name',        TITLE);
                $propbag->add('description', TITLE_FOR_PHPBOX);
                $propbag->add('default',     '');
                break;

            case 'content':
                $propbag->add('type',        'html');
                $propbag->add('name',        CONTENT);
                $propbag->add('description', THE_PHPBOX);
                $propbag->add('default',     '');
                break;

            case 'show_where':
                $select = array('extended' => PLUGIN_ITEM_DISPLAY_EXTENDED, 'overview' => PLUGIN_ITEM_DISPLAY_OVERVIEW, 'both' => PLUGIN_ITEM_DISPLAY_BOTH);
                $propbag->add('type',        'select');
                $propbag->add('select_values', $select);
                $propbag->add('name',        PLUGIN_ITEM_DISPLAY);
                $propbag->add('description', '');
                $propbag->add('default',     'both');
                break;

            default:
                return false;
        }
        return true;
    }

    function generate_content(&$title)
    {
        global $serendipity;

        $title = $this->get_config('title', $title);
        $show_where = $this->get_config('show_where', 'both');

        if ($show_where == 'extended' && (!isset($serendipity['GET']['id']) || !is_numeric($serendipity['GET']['id']))) {
            return false;
        } else if ($show_where == 'overview' && isset($serendipity['GET']['id']) && is_numeric($serendipity['GET']['id'])) {
            return false;
        }

        eval($this->get_config('content')); # Actualy this is the only BIG change to the HTML Block code.
    }
}
?>


Hope u enjoy, suggestions are welcome.
ImageDo your Part! Join now.

Xanthous

Right on!

Post by Xanthous » Mon Jan 03, 2005 9:15 pm

This was just what I needed to include a random image from my foto gallery! Thanks! ;-)

Dan

removal

Post by Dan » Wed Mar 23, 2005 8:30 am

hehe,

i feel a little stupid asking this, but i removed the plugin and still gives this error in the sidebar:

Code: Select all

serendipity error: could not include serendipity_plugin_phpbox:878b497a033d7e253d80d601e85d3945 - exiting


Any idea on how to remove it completely?
Any help would be appriciated

Tys
Regular
Posts: 36
Joined: Thu Feb 24, 2005 5:18 am

Post by Tys » Wed Mar 23, 2005 9:25 am

You could try deleting that row from the serendipity_plugins table.

Dan

Post by Dan » Wed Mar 23, 2005 11:08 am

tnx .. it worked

Hathor
Regular
Posts: 118
Joined: Tue Mar 22, 2005 11:48 pm

Post by Hathor » Thu Mar 24, 2005 12:54 am

Okay, I may be misunderstanding how this plug in works - believe me, I'm not that savvy - but I tried to use it to run an include for a stat php script, and it seems fine, but what displays on the blog is:

Parse error: parse error, unexpected '<' in /home/wwwtheh/public_html/plugins/serendipity_plugin_phpbox/serendipity_plugin_phpbox.php(82) : eval()'d code on line 1


Any clue what's causing that?

And is this the right plug-in for me to incorporate php script features, such as a site recommendation script, a stat script, etc.? Or is there a better way to implement such features?

User avatar
garvinhicking
Core Developer
Posts: 30020
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking » Thu Mar 24, 2005 1:00 pm

Hathor, I'd need your PHP snippet to see what's creating the parse error. Pay attention that the phpbox plugin needs, just like the PHP eval() code, to NOT begin with "<?php" strings.

And using the PHP Box plugin is bad in first instance. You should always create custom plugins for PHP applications. Look at the additional plugin (see wiki) called serendipity_event_externalphp - this is a skeleton to easily include PHP applications.

You can build a similar easy sidebar PHP plugin by taking the phpbox plugin and instead of using eval(...) you can put "include /path/to/tool/tool.php" into the file. That is much cleaner and has no security issues.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/

warbringer
Posts: 1
Joined: Fri May 13, 2005 9:24 pm
Location: Netherlands

Post by warbringer » Sat May 14, 2005 8:51 am

Hathor wrote:Okay, I may be misunderstanding how this plug in works - believe me, I'm not that savvy - but I tried to use it to run an include for a stat php script, and it seems fine, but what displays on the blog is:

Parse error: parse error, unexpected '<' in /home/wwwtheh/public_html/plugins/serendipity_plugin_phpbox/serendipity_plugin_phpbox.php(82) : eval()'d code on line 1


Any clue what's causing that?

And is this the right plug-in for me to incorporate php script features, such as a site recommendation script, a stat script, etc.? Or is there a better way to implement such features?


I have the same error.
The eval hangs on the < in "<?" in my code but when I paste the code in a test.php and run it, it works fine. (it calculated the days left to a certain day).

I don't think the piece of PHP is at error or does it need to say something special after "<?" ?
I plan on living forever, so far.. so good.

User avatar
garvinhicking
Core Developer
Posts: 30020
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking » Sat May 14, 2005 2:24 pm

I think because the plugin uses eval() you are not allowed to have a "<?php" at the beginning nor at the end?

regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/

JulianDE
Regular
Posts: 13
Joined: Sat May 28, 2005 9:44 pm
Location: Germany

Post by JulianDE » Sun Jun 12, 2005 6:58 pm

did anybody solve this problem until know?
would like to use the plugin but getting the same error ...

thanks,
Julian

User avatar
garvinhicking
Core Developer
Posts: 30020
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking » Mon Jun 13, 2005 1:54 pm

Julian: Did you try to remove the <? and ?> parts as I mentioned?

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/

JulianDE
Regular
Posts: 13
Joined: Sat May 28, 2005 9:44 pm
Location: Germany

Post by JulianDE » Mon Jun 13, 2005 2:03 pm

Hi Garvin,

yes, I've removed <? and ?>. Still the following:
Parse error: parse error, unexpected T_VARIABLE ...plugins/serendipity_plugin_phpbox/serendipity_plugin_phpbox.php(82) : eval()'d code on line 3

I've pasted the php code in editor's html mode, not wysiwyg mode. I hope this is correct and it doesn't matter that the saved code loses its 'layout' (character returns)?

thank you very much,

Julian

User avatar
garvinhicking
Core Developer
Posts: 30020
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking » Mon Jun 13, 2005 2:18 pm

Please copy+paste the actual PHP code that you paste in the textarea, there must be some other error.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/

JulianDE
Regular
Posts: 13
Joined: Sat May 28, 2005 9:44 pm
Location: Germany

Post by JulianDE » Mon Jun 13, 2005 2:30 pm

Hi Garvin,

thanks a lot.
The original code (without removement of <? and ?>):

Code: Select all

<?php
// ############         Edit below         ########################################
$topic_length = '30';   // length of topic title
$topic_limit = '5';   // limit of displayed topics
$special_forums = '1';   // specify forums ('0' = no; '1' = yes)
$forum_ids = '5,13';      // IDs of forums; separate them with a comma

$config_path = '/phpbb/';   // path to config.php
$root_path = '/phpbb/';      // link path
// ############         Edit above         ########################################

$path = dirname(__FILE__);
include_once($path.$config_path .'config.php');
mysql_connect($dbhost, $dbuser, $dbpasswd) OR die('Unable to select server.');
mysql_select_db($dbname) OR die('Unable to select database.');

// ############## output ##############
echo '<table width="100%" cellpadding="1" cellspacing="1" border="0" align="center">
          <tr>
                <th colspan="2">'. $topic_limit .' last topics</th>
          </tr>';
// ############## output ##############

$where_forums = ( $special_forums == '0' ) ? '' : 't.forum_id NOT IN ('. $forum_ids .') AND ';
$sql = "SELECT t.*, f.forum_id, f.forum_name, u.username AS first_poster, u.user_id AS first_poster_id, u2.username AS last_poster, u2.user_id AS last_poster_id, p.post_username AS first_poster_name, p2.post_username AS last_poster_name, p2.post_time
   FROM ". $table_prefix ."topics t, ". $table_prefix ."forums f, ". $table_prefix ."users u, ". $table_prefix ."posts p, ". $table_prefix ."posts p2, ". $table_prefix ."users u2
   WHERE $where_forums t.topic_poster = u.user_id AND f.forum_id = t.forum_id AND p.post_id = t.topic_first_post_id AND p2.post_id = t.topic_last_post_id AND u2.user_id = p2.poster_id
   ORDER BY t.topic_last_post_id DESC LIMIT $topic_limit";
$result = mysql_query($sql);
if( !$result )
{
   die('SQL Statement Error: '. mysql_error());
   exit();
}

$line = array();
while( $row = mysql_fetch_array($result) )
{
   $line[] = $row;
}

for( $i = 0; $i < count($line); $i++ )
{
   $forum_id = $line[$i]['forum_id'];
   $forum_url = $root_path .'viewforum.php?f='. $forum_id;
   $topic_id = $line[$i]['topic_id'];
   $topic_url = $root_path .'viewtopic.php?t='. $topic_id;

   $topic_title = ( strlen($line[$i]['topic_title']) < $topic_length ) ? $line[$i]['topic_title'] : substr(stripslashes($line[$i]['topic_title']), 0, $topic_length) .'...';

   $topic_type =  ( $line[$i]['topic_type'] == '2' ) ? 'Announcement ': '';
   $topic_type .= ( $line[$i]['topic_type'] == '3' ) ? 'Global Announcement ': '';
   $topic_type .= ( $line[$i]['topic_type'] == '1' ) ? 'Sticky ': '';
   $topic_type .= ( $line[$i]['topic_vote'] ) ? 'Poll ': '';

   $views = $line[$i]['topic_views'];
   $replies = $line[$i]['topic_replies'];

   $first_time = date('d.m.Y', $line[$i]['topic_time']);
   $first_author = ( $line[$i]['first_poster_id'] != '-1' ) ? '<a href="'. $root_path .'profile.php?mode=viewprofile&amp;u='. $line[$i]['first_poster_id'] .'">'. $line[$i]['first_poster'] .'</a>' : ( ($line[$i]['first_poster_name'] != '' ) ? $line[$i]['first_poster_name'] : 'guest' );
   $last_time = date('d.m.Y', $line[$i]['post_time']);
   $last_author = ( $line[$i]['last_poster_id'] != '-1' ) ? $line[$i]['last_poster'] : ( ($line[$i]['last_poster_name'] != '' ) ? $line[$i]['last_poster_name'] : 'guest' );
   $last_url = '<a href="'. $root_path .'viewtopic.php?p='. $line[$i]['topic_last_post_id'] .'#'. $line[$i]['topic_last_post_id'] .'">'. $last_author .'</a>';

   // ############## output ##############
   echo '<tr>
                        <td valign="top" nowrap="nowrap">'. $topic_type .'<a href="'. $topic_url .'">'. $topic_title .'</a></td>
                    </tr>
                    <tr>
                        <td><a href="'. $forum_url .'">'. $line[$i]['forum_name'] .'</a>: '. $last_url .' '. $last_time .'</td>
                    </tr>';
   // ############## output ##############
}

echo '</table>';
mysql_close();
?>


blog and recent.php (the code above) are in the root. phpbb is in subfolder phpbb. it works when I call the file directly in the browser.

thank you,
Julian

JulianDE
Regular
Posts: 13
Joined: Sat May 28, 2005 9:44 pm
Location: Germany

Post by JulianDE » Mon Jun 13, 2005 2:40 pm

Garvin,

could it be that some text formatting plugin has influence on the code when entering and saving with the editor? As the editor is basically designed for entry writing and not for php code pasting?

thanks,
Julian

Post Reply