Dropbox Upload Form with added features
-
I’ve modified Henrik’s very handy plugin to add some extra features. On upload the plugin now
- Saves some metadata about the file uploaded. The data’s saved in a text file which has the name of the uploaded file + “.txt”.
- Leaves a copy of the uploaded file and metadata text file in the temp directory.
- Copies both files to Dropbox.
- Emails a notification about the upload.
The code below should replace the contents of Henrik’s main plugin page wp-dropbox.php. Hope someone finds this useful!
Andfinally
<?php /* ************************************************************************** Plugin Name: Dropbox Upload Form Plugin URI: Description: Use the shortcode [wp-dropbox] in any page to insert a Dropbox file upload form. Kudos to <a href="https://inuse.se">inUse</a> for letting me release this in-house developed plugin under GPL. Use with caution and DON'T blame me if something breaks. Version: 0.0.4 Author: Henrik ?stlund Author URI: https://?stlund.info/ ************************************************************************** Copyright (C) 2010 Henrik ?stlund henrik(at)myworld.se This program 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 3 of the License, or (at your option) any later version. This program 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, see <https://www.gnu.org/licenses/>. **************************************************************************/ function show_dropbox() { $db_user = get_option( 'db_username' ); $db_pass = get_option( 'db_password' ); $db_path = get_option( 'db_path' ); $db_tmp_path = get_option( 'db_temp_path' ); $db_allow_ext = trim( get_option( 'db_allow_ext' ) ); echo '<link type="text/css" rel="stylesheet" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/wp-dropbox/css/wp-db-style.css" />' . "\n"; echo '<div class="wp-dropbox">'; $showform = True; try { if (($db_user == '') or ($db_pass == '') ) throw new Exception(__('Need to configure plugin!')); if ($db_allow_ext == '') throw new Exception(__('Need to configure allowed file extensions!')); } catch(Exception $e) { echo '<span id="syntax_error">'.__('Error:'). ' ' . htmlspecialchars($e->getMessage()) . '</span>'; $showform = False; } if ($_POST) { require 'inc/DropboxUploader.php'; try { $allowedExtensions = split("[ ]+", $db_allow_ext); foreach ($_FILES as $file) { if ($file['tmp_name'] > '') { if (!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) { $ext = implode(", ", $allowedExtensions); die('<p>'.__('Allowed file extensions: ',wpdbUploadForm).''.$ext.'<br/>'. '<a href="javascript:history.go(-1);">'. __('<= Go back',wpdbUploadForm).'</a></p>'); } } } // Rename uploaded file to reflect original name if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) throw new Exception(__('File was not uploaded from your computer.',wpdbUploadForm)); if (!file_exists($db_tmp_path)) { if (!mkdir($db_tmp_path)) throw new Exception(__('Cannot create temporary directory!',wpdbUploadForm)); } if ($_FILES['file']['name'] === "") throw new Exception(__('File name not supplied by the browser.',wpdbUploadForm)); $new_file_name = explode(".",$file['name']); $tmpFile = $db_tmp_path . str_replace("/", '_', $new_file_name[0]) . "_" . date("Y-m-d") . "." . str_replace("/", '_', $new_file_name[1]); if (!move_uploaded_file($_FILES['file']['tmp_name'], $tmpFile)) throw new Exception(__('Cannot rename uploaded file!',wpdbUploadForm)); save_metadata($db_tmp_path, $tmpFile, $_POST['client'], $_POST['medium'], $_POST['country'], $_POST['email']); // Upload $uploader = new DropboxUploader($db_user, $db_pass); $uploader->upload($tmpFile, $db_path); $uploader->upload($tmpFile . '.txt', $db_path); send_mail($db_tmp_path, $tmpFile, $_POST['client'], $_POST['medium'], $_POST['country'], $_POST['email']); echo '<div class="instructions" style="width:450px;margin:20px 0;padding:20px;border:1px solid #333;font-size:16px;"><p>'.__('Thanks, your file has been uploaded.',wpdbUploadForm).'</p><p style="margin-bottom:0;"><a href="" style="color:#fff;">'.__('Upload another',wpdbUploadForm).'</a></p></div>'; $showform = False; //$delete_file = True; $delete_file = False; } catch(Exception $e) { echo '<span id="syntax_error">'.__('Error:',wpdbUploadForm) . ' ' . htmlspecialchars($e->getMessage()) . '</span>'; $delete_file = False; } // Clean up if($delete_file == True) { if (isset($tmpFile) && file_exists($tmpFile)) unlink($tmpFile); } } if($showform == True) { ?> <style> form.dropbox { width:450px; padding:20px 20px 0; background-color:#444; } .dropbox input, .dropbox select { display: block; margin: 0 0 10px; } .dropbox label { margin: 0; color: #fff; font-weight: normal; } div.instructions { margin:8px; color:#ccc; font-size:13px; } </style> <form method="POST" class="dropbox" enctype="multipart/form-data"> <?php global $current_user; get_currentuserinfo(); ?> <div class="instructions"> <p>Choose the file you want to upload using the "Browse" or "Choose File" button and browsing to the location of the file on your computer.</p> <p>Enter the client name and choose the most appropriate medium and country of origin.</p> <p>Click the "Upload" button to start the upload.</p> </div> <input type="hidden" name="email" <?php if (! empty( $current_user->user_email) ) echo 'value="' . $current_user->user_email . '" '; ?>/> <table> <tr> <td colspan="2"> <input class="input_form button" type="file" name="file"/> </td> </tr> <tr> <td colspan="2"> <label for="client">Client</label> <input type="text" name="client" size="50" /> </td> </tr> <tr> <td> <label for="medium">Medium</label> <select type="text" name="medium" > <option></option> <option value="TV">TV</option> <option value="Press">Press</option> <option value="Radio">Radio</option> </select> </td> <td> <label for="country">Country</label> <select type="text" name="country" > <option></option> <option value="Austria">Austria</option> <option value="Belarus">Belarus</option> <option value="Belgium">Belgium</option> <option value="Botswana">Botswana</option> <option value="Bulgaria">Bulgaria</option> <option value="Kenya">Kenya</option> <option value="Kuwait">Kuwait</option> <option value="Serbia">Serbia</option> <option value="Slovenia">Slovenia</option> <option value="South Africa">South Africa</option> <option value="Spain">Spain</option> <option value="Sweden">Sweden</option> <option value="Turkey">Turkey</option> <option value="Ukraine">Ukraine</option> <option value="United Kingdom">United Kingdom</option> <option value="Uzbekistan">Uzbekistan</option> </select> </td> </tr> <tr> <td> </td> <td> <p align="right"> <input type="submit" value="<?php _e('Upload',wpdbUploadForm); ?>" /> </p> </td> </tr> </table> </form> <?php } echo "</div>"; } function wp_dropbox_settings_page() { ?> <div class="wrap"> <h2>Wordpress Drobox Upload Form</h2> <p>Make and use a special folder on your Dropbox if you allow public uploads. The date when the file got uploaded is appended at the end of the filename, example foo_2010-01-01.pdf just so we don't overwrite files.</p> <?php if( $_POST[ "wp_db_submit_hidden" ] == 'Y' ) { // Save the posted value in the database update_option( 'db_username', $_POST[ 'wp_db_username' ] ); update_option( 'db_password', $_POST[ 'wp_db_password' ] ); update_option( 'db_path', $_POST[ 'db_path' ] ); update_option( 'db_temp_path', $_POST[ 'db_temp_path' ] ); update_option( 'db_allow_ext', $_POST[ 'db_allow_ext' ] ); // Put an options updated message on the screen ?> <div class="updated"><p><strong><?php _e('Options saved.', 'mt_trans_domain' ); ?></strong></p></div> <?php } ?> <form name="wp_db_form" method="POST" action=""> <input type="hidden" name="wp_db_submit_hidden" value="Y"> <table class="form-table"> <tr> <th scope="row"><p>Dropbox username/login.</p></td> <td><input id="inputid" type="text" size="30" name="wp_db_username" value="<?php echo get_option( 'db_username' ); ?>" /> <label for="inputid">[email protected]</label> </td> </tr> <tr> <th scope="row"><p>Dropbox password.</p></th> <td><input id="inputid" type="password" size="30" name="wp_db_password" value="<?php echo get_option( 'db_password' ); ?>" /> <label for="inputid">supersecretpassword</label> </td> </tr> <tr> <th scope="row"><p>Path in dropbox folder (please, don't save to root folder).</p></th> <td><input type="text" size="60" name="db_path" value="<?php echo get_option( 'db_path' ); ?>" /> <label for="inputid">/public_upload/</label> </td> </tr> <tr> <th scope="row"><p>Temporary path on server. Files get saved here if Dropbox server is down.</p></th> <td><input type="text" size="60" name="db_temp_path" value="<?php echo get_option( 'db_temp_path' ); ?>" /> <label for="inputid">/path/outside/your/public_html/</label> </td> </tr> <tr> <th scope="row"><p>Allowed file extensions, separated by space.</p></th> <td><input type="text" size="60" name="db_allow_ext" value="<?php echo get_option( 'db_allow_ext' ); ?>" /> <label for="inputid">doc docx pdf</label> </td> </tr> <tr> <th scope="row"><input type="submit" value="<?php _e('Save options',wpdbUploadForm); ?>" /></th> <td></td> </tr> </table> </form> </div> <?php } // shortcode function shortcode_wp_dropbox( $atts, $content = NULL ) { // Hackis way to show my shortcode at the right place ob_start(); show_dropbox(); $output_string=ob_get_contents(); ob_end_clean(); return $output_string; } function wp_db_create_menu() { //create new top-level menu add_options_page('WP-Dropbox', 'WP-Dropbox', 'administrator', __FILE__, 'wp_dropbox_settings_page'); //call register settings function add_action( 'admin_init', 'register_wp_dropbox_settings' ); } function wp_dropbox_deactivate() { remove_shortcode( 'wp-dropbox' ); delete_option( 'db_username' ); delete_option( 'db_password' ); delete_option( 'db_path' ); delete_option( 'db_temp_path' ); delete_option( 'db_allow_ext' ); } function register_wp_dropbox_settings() { //register our settings register_setting( 'wp_db-settings-group', 'db_username' ); register_setting( 'wp_db-settings-group', 'db_password' ); register_setting( 'wp_db-settings-group', 'db_path' ); register_setting( 'wp_db-settings-group', 'db_temp_path' ); register_setting( 'wp_db-settings-group', 'db_allow_ext' ); } function WP_DB_PluginInit() { load_plugin_textdomain( 'wpdbUploadForm', PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)), dirname(plugin_basename(__FILE__))); } // Start this plugin once all other plugins are fully loaded // add_action( 'plugins_loaded', 'WPDropbox'); add_shortcode( 'wp-dropbox', 'shortcode_wp_dropbox' ); add_action('admin_menu', 'wp_db_create_menu'); add_action( 'init', 'WP_DB_PluginInit' ); register_deactivation_hook( __FILE__, 'wp_dropbox_deactivate' ); // Logs metadata submitted with file upload to text file function save_metadata($db_temp_path, $filepath, $client = '', $medium = '', $country = '', $email_address = '') { // Trim temp path off filename $filename = str_replace($db_temp_path, '', $filepath); // Create text file $fd = fopen($filepath . '.txt', "w"); $metadata = 'File: ' . $filename . "\n"; $metadata .= 'Client: ' . $client . "\n"; $metadata .= 'Medium: ' . $medium . "\n"; $metadata .= 'Country: ' . $country . "\n"; $metadata .= 'Uploaded By: ' . $email_address . "\n"; // write string fwrite($fd, $metadata); // close file fclose($fd); } function send_mail($db_temp_path, $filepath, $client = '', $medium = '', $country = '', $email_address = '') { require_once "Mail.php"; // PEAR Mail class // Trim temp path off filename $filename = str_replace($db_temp_path, '', $filepath); $params['host'] = 'SMTP_HOST'; $params['auth'] = true; $params['username'] = 'SMTP_USERNAME'; $params['password'] = 'SMTP_PASSWORD'; $to = '[email protected]'; $headers['From'] = "From Name <[email protected]>"; $headers['To'] = $to; $headers['Subject'] = 'New Upload'; $headers['MIME-Version'] = "1.0"; $headers['Content-type'] = "text/html; charset=utf-8"; $headers['X-Mailer'] = "php"; $body = <<<HEREDOC <body style="color:#333;font-family:Geneva,Verdana,sans-serif;font-size:12px;line-height:1.5em;"> <center> <div style="background-color:#f0f0f0;border:1px solid #ccc;margin:20px auto;padding:20px;width:600px;text-align:left;"> <p>Greetings. Someone has uploaded a new item on the <a href="https://url.here">Blog</a>.</p> <table align="center" cellpadding="5" style="color:#333;font-family:Geneva,Verdana,sans-serif;font-size:12px;"> <tr> <td><b>Uploaded by:</b></td> <td>$email_address</td> </tr> <tr> <td><b>File:</b></td> <td>$filename</td> </tr> <tr> <td><b>Client:</b></td> <td>$client</td> </tr> <tr> <td><b>Medium:</b></td> <td>$medium</td> </tr> <tr> <td><b>Country:</b></td> <td>$country</td> </tr> </table> </div> </center> </body> HEREDOC; $smtp = Mail::factory('smtp', $params); $mail = $smtp->send($to, $headers, $body); /* Error sending mail */ if (PEAR::isError($mail)) { // Error handling here return false; } /* Mail sent */ else { // Error handling here return true; } } ?>
https://www.ads-software.com/extend/plugins/dropbox-upload-form/
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Dropbox Upload Form with added features’ is closed to new replies.