Page 1 of 1

An external Gallery random photo side-bar plugin

Posted: Mon May 02, 2005 2:20 pm
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

Posted: Mon May 02, 2005 2:28 pm
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

Posted: Mon May 02, 2005 2:47 pm
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

Posted: Mon May 02, 2005 2:51 pm
by andy
sweet, glad to hear it wasn't too messed up.

I'll download that version tonight...

Thanks,
-Andy

Posted: Fri May 06, 2005 4:40 am
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.

Posted: Fri May 06, 2005 10:13 am
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

Posted: Fri May 06, 2005 7:19 pm
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...

Posted: Sat May 07, 2005 6:39 am
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

Posted: Mon May 09, 2005 2:16 pm
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

Posted: Mon May 09, 2005 2:45 pm
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

Gallery and s9y not using the same charset

Posted: Sun Jul 30, 2006 1:17 pm
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

Re: Gallery and s9y not using the same charset

Posted: Mon Jul 31, 2006 1:50 pm
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