An external Gallery random photo side-bar plugin

Creating and modifying plugins.
Post Reply
andy
Regular
Posts: 13
Joined: Mon Apr 25, 2005 6:21 am
Location: FL
Contact:

An external Gallery random photo side-bar plugin

Post by andy »

Since it's so short... here's the code:

Code: Select all

<?php # $Id: serendipity_plugin_gallery_menalto_random_block.php,v 1.0 2005/05/02 02:00:00 andybrown Exp $

// Probe for a language include with constants. Still include defines later on, if some constants were missing
$probelang = dirname(__FILE__) . '/lang_' . $serendipity['lang'] . '.inc.php';
if (file_exists($probelang)) {
    include $probelang;
}

switch ($serendipity['lang']) {
    case 'en':
    default:
        @define('PLUGIN_GALLERYRANDOMBLOCK_NAME',     'Gallery Random Photo Block');
        @define('PLUGIN_GALLERYRANDOMBLOCK_DESC',     'Adds a reference to a Gallery Random Block script (see http://gallery.menalto.com for details on this script)');
        @define('PLUGIN_GALLERYRANDOMBLOCK_URL_NAME',   'URL of Gallery install');
        @define('PLUGIN_GALLERYRANDOMBLOCK_URL_DESC',   'Enter the URL to a Gallery Installation');
        @define('PLUGIN_GALLERYRANDOMBLOCK_URL_DEFAULT',   'http://www.susanandandy.com/gallery/');
		@define('PLUGIN_GALLERYRANDOMBLOCK_NUMREPEAT_NAME', 'Number of Random Photos');
		@define('PLUGIN_GALLERYRANDOMBLOCK_NUMREPEAT_DESC', 'Enter the number of random photos to show in this block.');
        break;
}

class serendipity_plugin_gallery_menalto_random_block extends serendipity_plugin {
    var $title = PLUGIN_GALLERYRANDOMBLOCK_NAME;

    function introspect(&$propbag)
    {
        $this->title = $this->get_config('title', $this->title);

        $propbag->add('name',          PLUGIN_GALLERYRANDOMBLOCK_NAME);
        $propbag->add('description',   PLUGIN_GALLERYRANDOMBLOCK_DESC);
        $propbag->add('stackable',     false);
        $propbag->add('author',        'Andrew Brown');
        $propbag->add('version',       '1.0');
        $propbag->add('requirements',  array(
            'serendipity' => '0.8',
            'smarty'      => '2.6.7',
            'php'         => '4.1.0'
        ));
        $propbag->add('configuration', array('title', 'path', 'repeat'));

        // Register (multiple) dependencies. KEY is the name of the depending plugin. VALUE is a mode of either 'remove' or 'keep'.
        // If the mode 'remove' is set, removing the plugin results in a removal of the depending plugin. 'Keep' meens to
        // not touch the depending plugin.
    }

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

            case 'path':
                $propbag->add('type',        'string');
                $propbag->add('name',        PLUGIN_GALLERYRANDOMBLOCK_URL_NAME);
                $propbag->add('description', PLUGIN_GALLERYRANDOMBLOCK_URL_DESC);
                $propbag->add('default',     PLUGIN_GALLERYRANDOMBLOCK_URL_DEFAULT);
                break;

            case 'repeat':
                $propbag->add('type',        'string');
                $propbag->add('name',        PLUGIN_GALLERYRANDOMBLOCK_NUMREPEAT_NAME);
                $propbag->add('description', PLUGIN_GALLERYRANDOMBLOCK_NUMREPEAT_DESC);
                $propbag->add('default',     '1');
                break;
				
            default:
                    return false;
        }
        return true;
    }

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

        $title = $this->get_config('title');
		
		$path = $this->get_config('path');

        $repeat = $this->get_config('repeat');
		if ($repeat != '1') {
			$repeat == '1';
		}

		for ($i=1; $i <= $repeat; $i++) {
			if (substr($path,-1) == '/') {
				include($path . 'block-random.php');
			}
			else {
				include($path . '/block-random.php');
			}
			if ($i < $repeat) {
				echo '<HR>';
			}
		}
	}
}
/* vim: set sts=4 ts=4 expandtab : */
?>
Settings include: Title; Path to your Gallery install (currentlyrequires a fully qualified URL); Number of random photos to display.

View a working version here: http://www.susanandandy.com
It uses Gallery's built-in random photo script (http://gallery.menalto.com)

If anyone knows how to write just the virtual path as the $path variable, I'd like the plugin to work that way, as currently, it can steal someone else's gallery...

Please let me know of any bugs or missing error checking.

I should also mention that I used the templatedropdown plugin as a template as it was the easiest to understand (I'm new to php...).


Enjoy!
-Andy
Last edited by andy on Mon May 02, 2005 2:45 pm, edited 2 times in total.
andy
Regular
Posts: 13
Joined: Mon Apr 25, 2005 6:21 am
Location: FL
Contact:

Post by andy »

Oh, and guess this could be expanded to be a Repeatable PHP Code Executor plugin as well... just change the include lines by taking out the

Code: Select all

. 'block-random.php'
Didn't want to post it as such, being that it could be a security issue?
the tech. is there though...

-Andy
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Thanks a lot! I just committed the plugin to CVS. I fixed the bad code indent, added german translation, made the filename configurable and added a security fallback that the plugin can only be configured as Administrator.

Hope that's fine with you? Thanks a lot for contributing your code!

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/
andy
Regular
Posts: 13
Joined: Mon Apr 25, 2005 6:21 am
Location: FL
Contact:

Post by andy »

sweet, glad to hear it wasn't too messed up.

I'll download that version tonight...

Thanks,
-Andy
andy
Regular
Posts: 13
Joined: Mon Apr 25, 2005 6:21 am
Location: FL
Contact:

Post by andy »

The serendipityuser < UserAdmin portion is causing those sections not to show, even though I'm the admin.

Also, I have an update... I guess I need a dev login after all.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Just send me a mail with your SourceForge.net username :)

Or provide a unified diff? Could you look why the Admin-stuff doesn't work? Try out which userlevel you got and against which one it's comparing?

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/
Guest

Post by Guest »

Well, I'm running s9y on WinXP, Apache2 for Win, MySQL 4+ for Win, and PHP5.
I tried to echo the userlevel using the following:

Code: Select all

echo $serendipity['serendipityUserlevel']
Which returned nothing.
No errors in the site's error log either.
I can login though with admin permissions, user with author rights can login as well with just author permissions as well.

I'll send my SF username...
andy
Regular
Posts: 13
Joined: Mon Apr 25, 2005 6:21 am
Location: FL
Contact:

Post by andy »

ok, I'm guessing I was added to the project in some sense?
Care to PM or E-mail me with the details of what I should do now?

Sorry for the trouble, but I'm new to developing with Sourceforge.net, and PHP.

Thanks again,
-Andy
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Andy - when you echo the $serendipity['serendipityUserlevel'] variable, did you make sure you have a "global $serendipity" inside the PHP method you are calling?

About the CVS development, I'd like you to read http://sourceforge.net/cvs/?group_id=75065 and maybe search for some general CVS information with Google. WinCVS is a good graphical client which should ease things up for you?

Best 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/
andy
Regular
Posts: 13
Joined: Mon Apr 25, 2005 6:21 am
Location: FL
Contact:

Post by andy »

I'm not sure...
Basically, for testing purposes, and since I'm not that fluent in PHP, I just tried 2 things...
1. Tried echoing it as something that would come up when the plug-in runs in the sidebar, as part of the plug-in code that you provided.
2. Tried just using it as the default string for one of the plug-in's configuration variables (I think it was path)...

Both times, I didn't get any output (not even an error).

Thanks for the suggestion on WinCVS, I'll try it tonight.

-Andy
prunille
Posts: 1
Joined: Sun Jul 30, 2006 1:00 pm

Gallery and s9y not using the same charset

Post by prunille »

Hi Andy,

I use Serendipity, Gallery 1.x (migration will arrive soon) and your plugin. I had a problem with your plugin not taking into account the charset of the gallery server. I made a quick hack :

Code: Select all

               $req = new HTTP_Request($path.$file,$options);
                $req_result = $req->sendRequest();
                if ( PEAR::isError( $req_result)) {
                    echo PLUGIN_GALLERYRANDOMBLOCK_ERROR_CONNECT . "<br />\n";
                } else {
                    $res_code = $req->getResponseCode();
                    if ($res_code != "200") {
                        printf( PLUGIN_GALLERYRANDOMBLOCK_ERROR_HTTP . "<br />\n", $res_code);
                    } else {
                        // here we have the answer but we don't know yet the charset...
                        if ( preg_match('@charset=(.*)@i',$req->getResponseHeader("content-type"), $matches)) { $codage = $matches[1]; }
                        if ( $codage ) {
                            echo iconv("$codage",$serendipity['charset'],$req->getResponseBody());
                        } else {
                            echo $req->getResponseBody();
                        }
                    }
                }
It should do for any charset different from the one I use but it is still prettry fragile as far as I can tell.

Cheers,

François
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Gallery and s9y not using the same charset

Post by garvinhicking »

Hi Francois!

Thanks for your patch, I'll try to include that for future versions of the plugin! (however, I'll need to use also mb_* functions if iconv() is not available). In most cases though, s9y and gallery run with same charsets, so maybe this is the reason why not many people have found the same problem...

Best 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/
Post Reply