1 (edited by akinnon 2007-12-05 16:26)

Topic: uploader plugin for adminsitrators and moderators

My first attempt at a pluging for punbb. This allows adminsitrators and moderators to upload files into an uploads directory.
I made this because I needed to provide a way to upload sample mp3 files for audio streaming playback in a site. Rather than building a seporate login for the management of other site features, it made sense just to add the option as a plugin through punbb.


<?php

// file upload plugin based on example plugin provided with punbb
// written by Allan Mackinnon 20071205

/***********************************************************************

  Copyright (C) 2002-2005  Rickard Andersson (rickard@punbb.org)

  This file is part of PunBB.

  PunBB 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.

  PunBB 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

************************************************************************/

##
##
##  A few notes of interest for aspiring plugin authors:
##
##  1. If you want to display a message via the message() function, you
##     must do so before calling generate_admin_menu($plugin).
##
##  2. Plugins are loaded by admin_loader.php and must not be
##     terminated (e.g. by calling exit()). After the plugin script has
##     finished, the loader script displays the footer, so don't worry
##     about that. Please note that terminating a plugin by calling
##     message() or redirect() is fine though.
##
##  3. The action attribute of any and all <form> tags and the target
##     URL for the redirect() function must be set to the value of
##     $_SERVER['REQUEST_URI']. This URL can however be extended to
##     include extra variables (like the addition of &foo=bar in
##     the form of this example plugin).
##
##  4. If your plugin is for administrators only, the filename must
##     have the prefix "AP_". If it is for both administrators and
##     moderators, use the prefix "AMP_". This example plugin has the
##     prefix "AMP_" and is therefore available for both admins and
##     moderators in the navigation menu.
##
##  5. Use _ instead of spaces in the file name.
##
##  6. Since plugin scripts are included from the PunBB script
##     admin_loader.php, you have access to all PunBB functions and
##     global variables (e.g. $db, $pun_config, $pun_user etc).
##
##  7. Do your best to keep the look and feel of your plugins' user
##     interface similar to the rest of the admin scripts. Feel free to
##     borrow markup and code from the admin scripts to use in your
##     plugins. If you create your own styles they need to be added to
##     the "base_admin" style sheet.
##
##  8. Plugins must be released under the GNU General Public License or
##     a GPL compatible license. Copy the GPL preamble at the top of
##     this file into your plugin script and alter the copyright notice
##     to refrect the author of the plugin (i.e. you).
##
##


// Make sure no one attempts to run this script "directly"
if (!defined('PUN'))
    exit;

// Tell admin_loader.php that this is indeed a plugin and that it is loaded
define('PUN_PLUGIN_LOADED', 1);

//
// The rest is up to you!
//
// If the upload button was clicked

    // Display the admin navigation menu
    generate_admin_menu($plugin);
if (isset($_FILES['uploadedfile'])){

    $message="No file was selected.";// set the default message
    if($_FILES['uploadedfile']['name']!=''){
        // Where the file is going to be placed. 
        $target_path = "../uploads/";
        /* Add the original filename to our target path.  
        Result is "uploads/filename.extension" */
        $fname=$_FILES['uploadedfile']['name'];
        $fname=preg_replace('~[^\w\d\.]~','_',$fname);// replace non numerics and digits with _ but keep the .
        $fname=str_replace('__','_',$fname);// remove double underscores
        $target_path = $target_path . $fname; 
        if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
            $message= "The file ".  pun_htmlspecialchars( $fname). 
            " has been uploaded";
        } else{
            $message= "There was an error uploading the file, please try again!";
        }        
    }    

?>
    <div class="block">
        <h2><span>File upload message</span></h2>
        <div class="box">
            <div class="inbox">
                <p><?php echo $message; ?></p>
            </div>
        </div>
    </div>
<?php

}

?>
    <div id="exampleplugin" class="blockform">
        <h2><span>Upload plugin</span></h2>
        <div class="box">
            <div class="inbox">
                <p>This plugin allows you to upload files into the upload directory</p>
                <p>Maximum file size is 10mb. </p>
            </div>
        </div>

        <h2 class="block2"><span>Upload Form</span></h2>
        <div class="box">
            <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
                <div class="inform">
                    <fieldset>
                        <legend>Use the browse button to select a file for upload</legend>
                        <div class="infldset">
                            
                            <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
                            Choose a file to upload: <input name="uploadedfile" type="file" accept="audio/x-mpeg" /><br />
                            <input type="submit" value="Upload File" />
                            
                        </div>
                    </fieldset>
                </div>
            </form>
        </div>
    </div>
<?php


// Note that the script just ends here. The footer will be included by admin_loader.php.

I hope this is usefull to someone.

All the best,
Akinnon

2 (edited by MattF 2007-12-05 16:29)

Re: uploader plugin for adminsitrators and moderators

Nice. smile Two things I would suggest, however.

1) Strip any non alphanumeric characters out of the filename and replace them with underscores or hyphens. Non Windoze systems use a lot of characters, the ampersand and quote are two, for special purposes.

2) Enclose the filename, where it is echoed/printed, in pun_htmlspecialchars, i.e:

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
            $message= 'The file '.pun_htmlspecialchars($_FILES['uploadedfile']['name']).' has been uploaded';

I don't think you need basename on that. PHP should only supply the filename itself. Although, you'll probably have a different var name there anyhow if you strip the non-alpha characters. big_smile

Re: uploader plugin for adminsitrators and moderators

Cool, thanks for the feedback. I'll make the changes suggested.

Re: uploader plugin for adminsitrators and moderators

I was going to write something similar to this later this month. Now, I need to do a little less work. Thanks!

Re: uploader plugin for adminsitrators and moderators

ok, I've added some extra lines to fix file names with _ using preg_replace and str_replace in case theres more than one _ beside each other. Also, your totally right MattF, basename makes no difference, so stripped it out.

all the best,
akinnon.