Akyra

Jomblo1

May 8th, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.33 KB | None | 0 0
  1. <?php
  2. ///wp-file-upload/wordpress_file_upload.php
  3. session_start();
  4. /*
  5. Plugin Name: Wordpress File Upload
  6. Plugin URI: http://www.iptanus.com/support/wordpress-file-upload
  7. Description: Simple interface to upload files from a page.
  8. Version: 2.4.1
  9. Author: Nickolas Bossinas
  10. Author URI: http://www.iptanus.com
  11. */
  12.  
  13. /*
  14. Wordpress File Upload (Wordpress Plugin)
  15. Copyright (C) 2010-2014 Nickolas Bossinas
  16. Contact me at http://www.iptanus.com
  17.  
  18. This program is free software: you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation, either version 3 of the License, or
  21. (at your option) any later version.
  22.  
  23. This program is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. GNU General Public License for more details.
  27.  
  28. You should have received a copy of the GNU General Public License
  29. along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. */
  31.  
  32. //set global db variables
  33. $wfu_tb_log_version = "1.0";
  34. $wfu_tb_userdata_version = "1.0";
  35.  
  36. /* do not load plugin if this is the login page */
  37. $uri = $_SERVER['REQUEST_URI'];
  38. if ( strpos($uri, 'wp-login.php') !== false ) return;
  39.  
  40. DEFINE("WPFILEUPLOAD_DIR", '/'.PLUGINDIR .'/'.dirname(plugin_basename (__FILE__)).'/');
  41. add_shortcode("wordpress_file_upload", "wordpress_file_upload_handler");
  42. load_plugin_textdomain('wordpress-file-upload', false, dirname(plugin_basename (__FILE__)).'/languages');
  43. /* do not load styles and scripts in admin pages */
  44. if ( is_admin() ) {
  45.     if ( strpos($uri, "options-general.php") !== false ) {
  46.         wp_enqueue_style('wordpress-file-upload-admin-style', WPFILEUPLOAD_DIR.'css/wordpress_file_upload_adminstyle.css',false,'1.0','all');
  47.         wp_enqueue_style( 'wp-color-picker' );
  48.         wp_enqueue_script('wordpress_file_upload_admin_script', WPFILEUPLOAD_DIR.'js/wordpress_file_upload_adminfunctions.js', array( 'wp-color-picker' ), false, true);
  49.         wp_enqueue_script('wordpress_file_upload_classname_script', WPFILEUPLOAD_DIR.'js/getElementsByClassName-1.0.1.js');
  50.         $AdminParams = array("wfu_ajax_url" => site_url()."/wp-admin/admin-ajax.php");
  51.         wp_localize_script( 'wordpress_file_upload_admin_script', 'AdminParams', $AdminParams );
  52.     }
  53. }
  54. else {
  55. //  wp_enqueue_style('wordpress-file-upload-reset', WPFILEUPLOAD_DIR.'css/wordpress_file_upload_reset.css',false,'1.0','all');
  56.     wp_enqueue_style('wordpress-file-upload-style', WPFILEUPLOAD_DIR.'css/wordpress_file_upload_style.css',false,'1.0','all');
  57.     wp_enqueue_style('wordpress-file-upload-style-safe', WPFILEUPLOAD_DIR.'css/wordpress_file_upload_style_safe.css',false,'1.0','all');
  58.     wp_enqueue_script('json_class', WPFILEUPLOAD_DIR.'js/json2.js');
  59.     wp_enqueue_script('wordpress_file_upload_script', WPFILEUPLOAD_DIR.'js/wordpress_file_upload_functions.js');
  60. }
  61. add_action('admin_menu', 'wordpress_file_upload_add_admin_pages');
  62. register_activation_hook(__FILE__,'wordpress_file_upload_install');
  63. add_action('plugins_loaded', 'wordpress_file_upload_update_db_check');
  64. //ajax actions
  65. add_action('wp_ajax_wfu_ajax_action', 'wfu_ajax_action_callback');
  66. add_action('wp_ajax_nopriv_wfu_ajax_action', 'wfu_ajax_action_callback');
  67. add_action('wp_ajax_wfu_ajax_action_send_email_notification', 'wfu_ajax_action_send_email_notification');
  68. add_action('wp_ajax_nopriv_wfu_ajax_action_send_email_notification', 'wfu_ajax_action_send_email_notification');
  69. add_action('wp_ajax_wfu_ajax_action_notify_wpfilebase', 'wfu_ajax_action_notify_wpfilebase');
  70. add_action('wp_ajax_nopriv_wfu_ajax_action_notify_wpfilebase', 'wfu_ajax_action_notify_wpfilebase');
  71. add_action('wp_ajax_wfu_ajax_action_save_shortcode', 'wfu_ajax_action_save_shortcode');
  72. add_action('wp_ajax_wfu_ajax_action_read_subfolders', 'wfu_ajax_action_read_subfolders');
  73. add_action('wp_ajax_wfu_ajax_action_download_file', 'wfu_ajax_action_download_file');
  74. wfu_include_lib();
  75.  
  76. function wfu_include_lib() {
  77.     if ( $handle = opendir(plugin_dir_path( __FILE__ )."lib/") ) {
  78.         $blacklist = array('.', '..');
  79.         while ( false !== ($file = readdir($handle)) )
  80.             if ( !in_array($file, $blacklist) )
  81.                 include_once plugin_dir_path( __FILE__ )."lib/".$file;
  82.         closedir($handle);
  83.     }
  84. }
  85.  
  86. /* exit if we are in admin pages (in case of ajax call) */
  87. if ( is_admin() ) return;
  88.  
  89. function wordpress_file_upload_handler($incomingfrompost) {
  90.     //process incoming attributes assigning defaults if required
  91.     $defs = wfu_attribute_definitions();
  92.     $defs_indexed = array();
  93.     foreach ( $defs as $def ) $defs_indexed[$def["attribute"]] = $def["value"];
  94.     $incomingfrompost = shortcode_atts($defs_indexed, $incomingfrompost);
  95.     //run function that actually does the work of the plugin
  96.     $wordpress_file_upload_output = wordpress_file_upload_function($incomingfrompost);
  97.     //send back text to replace shortcode in post
  98.     return $wordpress_file_upload_output;
  99. }
  100.  
  101. function wordpress_file_upload_function($incomingfromhandler) {
  102.     global $post;
  103.     global $blog_id;
  104.     $params = wfu_plugin_parse_array($incomingfromhandler);
  105.     $sid = $params["uploadid"];
  106.     // store current page id in params array
  107.     $params["pageid"] = $post->ID;
  108.  
  109.     $_SESSION['wfu_token_'.$sid] = uniqid(mt_rand(), TRUE);
  110.  
  111.     $user = wp_get_current_user();
  112.     $widths = wfu_decode_dimensions($params["widths"]);
  113.     $heights = wfu_decode_dimensions($params["heights"]);
  114.  
  115.     $uploadedfile = 'uploadedfile_'.$sid;
  116.     $hiddeninput = 'hiddeninput_'.$sid;
  117.     $adminerrorcodes = 'adminerrorcodes_'.$sid;
  118.     $upload_clickaction = 'wfu_redirect_to_classic('.$sid.', \''.$_SESSION['wfu_token_'.$sid].'\' , 0, 0);';
  119.  
  120.     //check if user is allowed to view plugin, otherwise do not generate it
  121.     $uploadroles = explode(",", $params["uploadrole"]);
  122.     foreach ( $uploadroles as &$uploadrole ) {
  123.         $uploadrole = strtolower(trim($uploadrole));
  124.     }
  125.     $plugin_upload_user_role = wfu_get_user_role($user, $uploadroles);     
  126.     if ( !in_array($plugin_upload_user_role, $uploadroles) && $plugin_upload_user_role != 'administrator' && $params["uploadrole"] != 'all' ) return;
  127.  
  128.     //activate debug mode only for admins
  129.     if ( $plugin_upload_user_role != 'administrator' ) $params["debugmode"] = "false";
  130.  
  131.     $params["adminmessages"] = ( $params["adminmessages"] == "true" && $plugin_upload_user_role == 'administrator' );
  132.     // define variable to hold any additional admin errors coming before processing of files (e.g. due to redirection)
  133.     $params["adminerrors"] = "";
  134.  
  135.     /* Define dynamic upload path from variables */
  136.     $search = array ('/%userid%/', '/%username%/', '/%blogid%/', '/%pageid%/', '/%pagetitle%/');   
  137.     if ( is_user_logged_in() ) $username = $user->user_login;
  138.     else $username = "guests";
  139.     $replace = array ($user->ID, $username, $blog_id, $post->ID, get_the_title($post->ID));
  140.     $params["uploadpath"] = preg_replace($search, $replace, $params["uploadpath"]);
  141.  
  142.     /* Determine if userdata fields have been defined */
  143.     $userdata_fields = array();
  144.     if ( $params["userdata"] == "true" && $params["userdatalabel"] != "" ) {
  145.         $userdata_rawfields = explode("/", $params["userdatalabel"]);
  146.         foreach ($userdata_rawfields as $userdata_rawitem) {
  147.             if ( $userdata_rawitem != "" ) {
  148.                 $is_required = ( $userdata_rawitem[0] == "*" ? "true" : "false" );
  149.                 if ( $is_required == "true" ) $userdata_rawitem = substr($userdata_rawitem, 1);
  150.                 if ( $userdata_rawitem != "" ) {
  151.                     array_push($userdata_fields, array( "label" => $userdata_rawitem, "required" => $is_required ));
  152.                 }
  153.             }
  154.         }
  155.        
  156.     }
  157.     $params["userdata_fields"] = $userdata_fields;
  158.  
  159.     /* Prepare information about directory or selection of target subdirectory */
  160.     $subfolders = wfu_prepare_subfolders_block($params, $widths, $heights);
  161.     $subfolders_item = $subfolders['item'];
  162.     $params['subfoldersarray'] = $subfolders['paths'];
  163.  
  164. //____________________________________________________________________________________________________________________________________________________________________________________
  165.  
  166.     if ( $params['forceclassic'] != "true" ) { 
  167. //**************section to put additional options inside params array**************
  168.         $params['subdir_selection_index'] = "-1";
  169. //**************end of section of additional options inside params array**************
  170.  
  171.  
  172. //  below this line no other changes to params array are allowed
  173.  
  174.  
  175. //**************section to save params as Wordpress options**************
  176. //      every params array is indexed (uniquely identified) by three fields:
  177. //          - the page that contains the shortcode
  178. //          - the id of the shortcode instance (because there may be more than one instances of the shortcode inside a page)
  179. //          - the user that views the plugin (because some items of the params array are affected by the user name)
  180. //      the wordpress option "wfu_params_index" holds an array of combinations of these three fields, together with a randomly generated string that corresponds to these fields.
  181. //      the wordpress option "wfu_params_xxx", where xxx is the randomly generated string, holds the params array (encoded to string) that corresponds to this string.
  182. //      the structure of the "wfu_params_index" option is as follows: "a1||b1||c1||d1&&a2||b2||c2||d2&&...", where
  183. //          - a is the randomly generated string (16 characters)
  184. //          - b is the page id
  185. //          - c is the shortcode id
  186. //          - d is the user name
  187.         $params_index = wfu_generate_current_params_index($sid, $user->user_login);
  188.         $params_str = wfu_encode_array_to_string($params);
  189.         update_option('wfu_params_'.$params_index, $params_str);
  190.         $ajax_params['shortcode_id'] = $sid;
  191.         $ajax_params['params_index'] = $params_index;
  192.         $ajax_params['debugmode'] = $params["debugmode"];
  193.         $ajax_params['is_admin'] = ( $plugin_upload_user_role == 'administrator' ? "true" : "false" );
  194.         $ajax_params["error_header"] = $params["errormessage"];
  195.         $ajax_params["fail_colors"] = $params["failmessagecolors"];
  196.  
  197.         $ajax_params_str = wfu_encode_array_to_string($ajax_params);
  198.         $upload_clickaction = 'wfu_HTML5UploadFile('.$sid.', \''.$ajax_params_str.'\', \''.$_SESSION['wfu_token_'.$sid].'\')';
  199.     }
  200.     $upload_onclick = ' onclick="'.$upload_clickaction.'"';
  201.  
  202.     /* Prepare the title */
  203.     $title_item = wfu_prepare_title_block($params, $widths, $heights);
  204.     /* Prepare the text box showing filename */
  205.     $textbox_item = wfu_prepare_textbox_block($params, $widths, $heights);
  206.     /* Prepare the upload form */
  207.     $additional_params = array( );
  208.     $uploadform_item = wfu_prepare_uploadform_block($params, $widths, $heights, $upload_clickaction, $additional_params);
  209.     /* Prepare the submit button */
  210.     $submit_item = wfu_prepare_submit_block($params, $widths, $heights, $upload_clickaction);
  211.     /* Prepare the progress bar */
  212.     $progressbar_item = wfu_prepare_progressbar_block($params, $widths, $heights);
  213.     /* Prepare the message */
  214.     $message_item = wfu_prepare_message_block($params, $widths, $heights);
  215.     /* Prepare user data */
  216.     $userdata_item = wfu_prepare_userdata_block($params, $widths, $heights);
  217.  
  218.     /* Compose the html code for the plugin */
  219.     $wordpress_file_upload_output = "";
  220.     $wordpress_file_upload_output .= '<div id="wordpress_file_upload_block_'.$sid.'" class="file_div_clean">';
  221.     $itemplaces = explode("/", $params["placements"]);
  222.     foreach ( $itemplaces as $section ) {
  223.         $items_in_section = explode("+", trim($section));
  224.         $section_array = array( );
  225.         foreach ( $items_in_section as $item_in_section ) {
  226.             $item_in_section = strtolower(trim($item_in_section));
  227.             if ( $item_in_section == "title" ) array_push($section_array, $title_item);
  228.             elseif ( $item_in_section == "filename" ) array_push($section_array, $textbox_item);
  229.             elseif ( $item_in_section == "selectbutton" ) array_push($section_array, $uploadform_item);
  230.             elseif ( $item_in_section == "uploadbutton" && $params["singlebutton"] != "true" ) array_push($section_array, $submit_item);
  231.             elseif ( $item_in_section == "subfolders" ) array_push($section_array, $subfolders_item);
  232.             elseif ( $item_in_section == "progressbar" ) array_push($section_array, $progressbar_item);
  233.             elseif ( $item_in_section == "message" ) array_push($section_array, $message_item);
  234.             elseif ( $item_in_section == "userdata" && $params["userdata"] == "true" ) array_push($section_array, $userdata_item);
  235.         }
  236.         $wordpress_file_upload_output .= call_user_func_array("wfu_add_div", $section_array);
  237.     }
  238.     /* Append mandatory blocks, if have not been included in placements attribute */
  239.     if ( $params["userdata"] == "true" && strpos($params["placements"], "userdata") === false ) {
  240.         $section_array = array( );
  241.         array_push($section_array, $userdata_item);
  242.         $wordpress_file_upload_output .= call_user_func_array("wfu_add_div", $section_array);
  243.     }
  244.     if ( strpos($params["placements"], "selectbutton") === false ) {
  245.         $section_array = array( );
  246.         array_push($section_array, $uploadform_item);
  247.         $wordpress_file_upload_output .= call_user_func_array("wfu_add_div", $section_array);
  248.     }
  249.  
  250.     /* Pass constants to javascript */
  251.     $consts = wfu_set_javascript_constants();
  252.     $handler = 'function() { wfu_Initialize_Consts("'.$consts.'") }';
  253.     $wordpress_file_upload_output .= "\n\t".'<script type="text/javascript">if(window.addEventListener) { window.addEventListener("load", '.$handler.', false); } else if(window.attachEvent) { window.attachEvent("onload", '.$handler.'); } else { window["onload"] = '.$handler.'; }</script>';
  254.     $wordpress_file_upload_output .= '</div>';
  255. //  $wordpress_file_upload_output .= '<div>';
  256. //  $wordpress_file_upload_output .= wfu_test_admin();
  257. //  $wordpress_file_upload_output .= '</div>';
  258.  
  259. //  The plugin uses sessions in order to detect if the page was loaded due to file upload or
  260. //  because the user pressed the Refresh button (or F5) of the page.
  261. //  In the second case we do not want to perform any file upload, so we abort the rest of the script.
  262.     if ( $_SESSION['wfu_check_refresh_'.$sid] != "form button pressed" ) {
  263.         $_SESSION['wfu_check_refresh_'.$sid] = 'do not process';
  264.         $wordpress_file_upload_output .= wfu_post_plugin_actions($params);
  265.         return $wordpress_file_upload_output."\n";
  266.     }
  267.     $_SESSION['wfu_check_refresh_'.$sid] = 'do not process';
  268.     $params["upload_start_time"] = $_SESSION['wfu_start_time_'.$sid];
  269.  
  270. //  The plugin uses two ways to upload the file:
  271. //      - The first one uses classic functionality of an HTML form (highest compatibility with browsers but few capabilities).
  272. //      - The second uses ajax (HTML5) functionality (medium compatibility with browsers but many capabilities, like no page refresh and progress bar).
  273. //  The plugin loads using ajax functionality by default, however if it detects that ajax functionality is not supported, it will automatically switch to classic functionality.
  274. //  The next line checks to see if the form was submitted using ajax or classic functionality.
  275. //  If the uploaded file variable stored in $_FILES ends with "_redirected", then it means that ajax functionality is not supported and the plugin must switch to classic functionality.
  276.     if ( isset($_FILES[$uploadedfile.'_redirected']) ) $params['forceclassic'] = "true";
  277.  
  278.     if ( $params['forceclassic'] != "true" ) {
  279.         $wordpress_file_upload_output .= wfu_post_plugin_actions($params);
  280.         return $wordpress_file_upload_output."\n";
  281.     }
  282.  
  283. //  The section below is executed when using classic upload methods
  284.     if ( isset( $_POST[$adminerrorcodes] ) ) {
  285.         $code = $_POST[$adminerrorcodes];
  286.         if ( $code == "" ) $params['adminerrors'] = "";
  287.         elseif ( $code == "1" || $code == "2" || $code == "3" ) $params['adminerrors'] = constant('WFU_ERROR_REDIRECTION_ERRORCODE'.$code);
  288.         else $params['adminerrors'] = WFU_ERROR_REDIRECTION_ERRORCODE0;
  289.     }
  290.  
  291.     $params['subdir_selection_index'] = -1;
  292.     if ( isset( $_POST[$hiddeninput] ) ) $params['subdir_selection_index'] = $_POST[$hiddeninput];
  293.  
  294.     $wfu_process_file_array = wfu_process_files($params, 'no_ajax');
  295.     $safe_output = $wfu_process_file_array["general"]['safe_output'];
  296.     unset($wfu_process_file_array["general"]['safe_output']);
  297.  
  298.     $wfu_process_file_array_str = wfu_encode_array_to_string($wfu_process_file_array);
  299.     $ProcessUploadComplete_functiondef = 'function(){wfu_ProcessUploadComplete('.$sid.', 1, "'.$wfu_process_file_array_str.'", "no-ajax", "", "", "'.$safe_output.'", ["false", "", "false"]);}';
  300.     $wordpress_file_upload_output .= '<script type="text/javascript">window.onload='.$ProcessUploadComplete_functiondef.'</script>';
  301.  
  302.     $wordpress_file_upload_output .= wfu_post_plugin_actions($params);
  303.     return $wordpress_file_upload_output."\n";
  304. }
  305.  
  306. function wfu_post_plugin_actions($params) {
  307.     $echo_str = '';
  308.  
  309.     return $echo_str;
  310. }
  311.  
  312. ?>
Advertisement
Add Comment
Please, Sign In to add comment