Skip to content

Instantly share code, notes, and snippets.

@bountin
Created November 25, 2011 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bountin/1393995 to your computer and use it in GitHub Desktop.
Save bountin/1393995 to your computer and use it in GitHub Desktop.
Patch for WP-Filebase to require a form submission before downloading
diff -Narub wp-filebase/ajax.php wp-filebase-updated/ajax.php
--- wp-filebase/ajax.php 1970-01-01 01:00:00.000000000 +0100
+++ wp-filebase-updated/ajax.php 2011-07-13 10:24:01.000000000 +0200
@@ -0,0 +1,35 @@
+<?php
+/**
+ * WordPress AJAX Process Execution.
+ *
+ * @package WordPress
+ * @subpackage Administration
+ */
+
+/**
+ * Executing AJAX process.
+ *
+ * @since 2.1.0
+ */
+define('DOING_AJAX', true);
+define('WP_ADMIN', false);
+
+if (!isset($_REQUEST['action']))
+ die('-1');
+
+require_once('../../../wp-load.php');
+
+@header('Content-Type: text/html; charset=' . get_option('blog_charset'));
+send_nosniff_header();
+
+if (empty($_REQUEST['action'])) {
+ die('-1');
+}
+
+if (is_user_logged_in()) {
+ do_action('wp_ajax_noadmin_' . $_REQUEST['action']);
+} else {
+ do_action('wp_ajax_noadmin_nopriv_' . $_REQUEST['action']);
+}
+
+die(0);
diff -Narub wp-filebase/classes/Admin.php wp-filebase-updated/classes/Admin.php
--- wp-filebase/classes/Admin.php 2011-07-13 10:23:44.000000000 +0200
+++ wp-filebase-updated/classes/Admin.php 2011-07-13 10:23:58.000000000 +0200
@@ -24,6 +24,17 @@
if($upload_path_base == '' || $upload_path_base == '/')
$upload_path_base = 'wp-content/uploads';
+ $forms = array(
+ 0 => sprintf(' -- %s --', __('Feature deactivated', WPFB))
+ );
+
+ // Check for Gravityforms
+ if (WPFB_GF_FOUND) {
+ foreach (RGFormsModel::get_forms() AS $form) {
+ $forms[$form->id] = $form->title;
+ }
+ }
+
return array (
// common
@@ -39,6 +50,7 @@
'filelist_sorting' => array('default' => 'file_display_name', 'title' => __('Default sorting', WPFB), 'type' => 'select', 'desc' => __('The file property lists are sorted by', WPFB), 'options' => self::FileSortFields()),
'filelist_sorting_dir' => array('default' => 0, 'title' => __('Sort Order:'/*def*/), 'type' => 'select', 'desc' => __('The sorting direction of file lists', WPFB), 'options' => array(0 => __('Ascending'), 1 => __('Descending'))),
'filelist_num' => array('default' => 0, 'title' => __('Number of files per page', WPFB), 'type' => 'number', 'desc' => __('Length of the file list per page. Set to 0 to disable the limit.', WPFB)),
+ 'email_required_form' => array('default' => 0, 'title' => __('Email required form', WPFB), 'type' => 'select', 'desc' => __('Gravityform which will presented before downloading', WPFB), 'options' => $forms),
// limits
'bitrate_unregistered' => array('default' => 0, 'title' => __('Bit rate limit for guests', WPFB), 'type' => 'number', 'unit' => 'KiB/Sec', 'desc' => &$bitrate_desc),
@@ -338,7 +350,7 @@
{
global $wpdb;
- $catarr = wp_parse_args($catarr, array('cat_id' => 0, 'cat_name' => '', 'cat_description' => '', 'cat_parent' => 0, 'cat_folder' => ''));
+ $catarr = wp_parse_args($catarr, array('cat_id' => 0, 'cat_name' => '', 'cat_description' => '', 'cat_parent' => 0, 'cat_folder' => '', 'cat_email_required' => 0));
extract($catarr, EXTR_SKIP);
$cat_id = intval($cat_id);
@@ -362,6 +374,7 @@
$cat->cat_description = trim($cat_description);
$cat->cat_exclude_browser = (int)!empty($cat_exclude_browser);
$cat->cat_required_level = empty($cat_members_only) ? 0 : (WPFB_Core::UserRole2Level($cat_required_role)+1);
+ $cat->cat_email_required = $cat_email_required;
if($update && !empty($cat_child_apply_perm))
{
@@ -561,7 +574,7 @@
$data->file_direct_linking = 1; // allow direct linking by default
$file->file_direct_linking = (int)!empty($data->file_direct_linking);
- $var_names = array('remote_uri', 'author', 'date', 'post_id', 'description', 'hits', 'license');
+ $var_names = array('remote_uri', 'author', 'date', 'post_id', 'description', 'hits', 'license', 'email_required');
for($i = 0; $i < count($var_names); $i++)
{
$vn = 'file_' . $var_names[$i];
diff -Narub wp-filebase/classes/AdminGuiSettings.php wp-filebase-updated/classes/AdminGuiSettings.php
--- wp-filebase/classes/AdminGuiSettings.php 2011-07-13 10:23:44.000000000 +0200
+++ wp-filebase-updated/classes/AdminGuiSettings.php 2011-07-13 10:23:58.000000000 +0200
@@ -168,7 +168,7 @@
$option_categories = array(
__('Common', WPFB) => array('upload_path', /*'cat_drop_down'*/),
- __('Display', WPFB) => array('thumbnail_size','auto_attach_files', 'filelist_sorting', 'filelist_sorting_dir', 'filelist_num', /* TODO: remove? 'parse_tags_rss',*/ 'decimal_size_format'),
+ __('Display', WPFB) => array('thumbnail_size','auto_attach_files', 'filelist_sorting', 'filelist_sorting_dir', 'filelist_num', /* TODO: remove? 'parse_tags_rss',*/ 'decimal_size_format', 'email_required_form'),
__('File Browser',WPFB) => array('file_browser_post_id','file_browser_cat_sort_by','file_browser_cat_sort_dir','file_browser_file_sort_by','file_browser_file_sort_dir'),
__('Download', WPFB) => array('disable_permalinks', 'download_base', 'force_download', 'range_download', 'http_nocache', 'ignore_admin_dls', 'accept_empty_referers','allowed_referers'),
__('Form Presets', WPFB) => array('languages', 'platforms', 'licenses', 'requirements'),
diff -Narub wp-filebase/classes/Category.php wp-filebase-updated/classes/Category.php
--- wp-filebase/classes/Category.php 2011-07-13 10:23:44.000000000 +0200
+++ wp-filebase-updated/classes/Category.php 2011-07-13 10:23:58.000000000 +0200
@@ -13,6 +13,7 @@
var $cat_num_files = 0;
var $cat_num_files_total = 0;
var $cat_required_level = 0;
+ var $cat_email_required = false;
var $cat_icon;
var $cat_exclude_browser = 0;
diff -Narub wp-filebase/classes/Core.php wp-filebase-updated/classes/Core.php
--- wp-filebase/classes/Core.php 2011-07-13 10:23:44.000000000 +0200
+++ wp-filebase-updated/classes/Core.php 2011-07-13 10:23:58.000000000 +0200
@@ -62,6 +62,14 @@
add_action('wp_dashboard_setup', array(__CLASS__, 'AdminDashboardSetup'));
+ // Init JsonService
+ wpfb_loadclass('JsonService');
+
+ // Check for GravityForms
+ if (!WPFB_GF_FOUND) {
+ add_action('admin_notices', 'gravityforms_missing_notice');
+ }
+
self::DownloadRedirect();
}
diff -Narub wp-filebase/classes/File.php wp-filebase-updated/classes/File.php
--- wp-filebase/classes/File.php 2011-07-13 10:23:44.000000000 +0200
+++ wp-filebase-updated/classes/File.php 2011-07-13 10:23:58.000000000 +0200
@@ -20,6 +20,7 @@
var $file_requirement;
var $file_license;
var $file_required_level = 0;
+ var $file_email_required = false;
var $file_offline = 0;
var $file_direct_linking = 0;
var $file_force_download = 0;
@@ -315,6 +316,12 @@
function get_tpl_var($name) {
static $no_esc = array('file_languages', 'file_platforms', 'file_requirements', 'file_license', 'file_small_icon');
+
+ $file_category = WPFB_Category::GetCat($this->file_category);
+ if (strcasecmp($name, 'file_url') == 0 && ($this->file_email_required || $file_category->cat_email_required)) {
+ return 'javascript:wpfb_open_email_form('.$this->file_id.')';
+ }
+
return in_array($name, $no_esc) ? $this->getTplVar($name) : htmlspecialchars($this->getTplVar($name));
}
diff -Narub wp-filebase/classes/JsonService.php wp-filebase-updated/classes/JsonService.php
--- wp-filebase/classes/JsonService.php 1970-01-01 01:00:00.000000000 +0100
+++ wp-filebase-updated/classes/JsonService.php 2011-07-13 10:23:58.000000000 +0200
@@ -0,0 +1,128 @@
+<?php
+
+/**
+ * Hanlding Json RPCs via AJAX
+ */
+class WPFB_JsonService
+{
+
+ const GET_REQUIRED_EMAIL_FORM = 'wpfb_get_required_email_form';
+
+ /**
+ * Initialize class (like registering actions, ...)
+ *
+ * @static
+ * @return void
+ */
+ public static function InitClass()
+ {
+ // Define JS variables
+ $jsSettings = array();
+ $jsSettings['ajaxurl'] = WP_PLUGIN_URL.'/wp-filebase/ajax.php';
+ $jsSettings['getRequiredEmailForm'] = self::GET_REQUIRED_EMAIL_FORM;
+ wp_localize_script('wpfb', 'WPFB', $jsSettings);
+
+ // Register getRequiredEmailForm action
+ $required_email_form_callback = array(__CLASS__, 'getRequiredEmailForm');
+ add_action('wp_ajax_noadmin_'.self::GET_REQUIRED_EMAIL_FORM, $required_email_form_callback);
+ add_action('wp_ajax_noadmin_nopriv_'.self::GET_REQUIRED_EMAIL_FORM, $required_email_form_callback);
+
+ // Enqueue GF scripts
+ if (WPFB_GF_FOUND)
+ gravity_form_enqueue_scripts(2, true);
+ }
+
+ /**
+ *
+ *
+ * @return void
+ */
+ public function getRequiredEmailForm()
+ {
+ $form_id = WPFB_Core::GetOpt('email_required_form');
+ $file_id = (int)$_REQUEST['file_id'];
+
+ // If form is not set or GF is not found, just redirect to file
+ if ($form_id == 0 || !WPFB_GF_FOUND) {
+ wpfb_loadclass('File');
+ $file = WPFB_File::GetFile($file_id);
+ echo 'window.location.href = "'.$file->getUrl().'"';
+ exit;
+ }
+
+ // Register action for successful validation
+ add_action("gform_post_submission_$form_id", array(__CLASS__, 'requiredEmailFormSuccess'), 10, 2);
+
+ // Validate form
+ RGForms::maybe_process_form();
+
+ // Get form meta & HTML (with ajax code)
+ $form_meta = RGFormsModel::get_form_meta($form_id);
+ $form = RGForms::get_form($form_id, false, true, false, false, true);
+
+ // Calculate offset of script tag
+ $script_offset = strpos($form, '<script');
+
+ // Validation result form doesn't have a script tag
+ if ($script_offset === false) {
+ echo $form;
+ exit;
+ }
+
+ // Divide form html from script code
+ $real_form = substr($form, 0, $script_offset);
+ $script = substr($form, $script_offset);
+
+ // Remove script tags
+ $script = substr($script, strpos($script, '>')+1, -strlen(strrchr($script, '<')));
+
+ echo "
+ // Close modal dialog if it exists
+ if (dialog) {
+ dialog.dialog('close');
+ }
+
+ // Open dialog with form content
+ var dialog = jQuery('<div title=\"{$form_meta['title']}\"></div>')
+ .append(jQuery(\"" . str_replace("\n", ' ', $real_form) . "\"))
+ .dialog({
+ modal: true
+ });
+ ";
+
+ // Send GF scripts
+ echo $script;
+
+ exit;
+ }
+
+ /**
+ * Called after validation success and redirects to real file location
+ * @return void
+ */
+ public function requiredEmailFormSuccess()
+ {
+ $file_id = (int)$_REQUEST['file_id'];
+
+ // Get file and its url
+ wpfb_loadclass('File');
+ $file = WPFB_File::GetFile($file_id);
+ $file_url = $file->getUrl();
+
+ echo <<< HTML
+ <html>
+ <head>
+
+ <script>
+ top.dialog.dialog('close');
+ top.location.href = '$file_url';
+ </script>
+ </head>
+ <body>
+ BAR
+ </body>
+ </html>
+HTML;
+ exit;
+ }
+}
diff -Narub wp-filebase/classes/Setup.php wp-filebase-updated/classes/Setup.php
--- wp-filebase/classes/Setup.php 2011-07-13 10:23:44.000000000 +0200
+++ wp-filebase-updated/classes/Setup.php 2011-07-13 10:23:58.000000000 +0200
@@ -268,6 +268,10 @@
// since 0.2.8
$queries[] = "@ALTER TABLE `$tbl_files` ADD `file_category_name` varchar(127) NOT NULL default '' AFTER `file_category`";
+ // by mp@25th-floor.com
+ $queries[] = "@ALTER TABLE `$tbl_files` ADD `file_email_required` BOOL NOT NULL AFTER `file_required_level`";
+ $queries[] = "@ALTER TABLE `$tbl_cats` ADD `cat_email_required` BOOL NOT NULL AFTER `cat_required_level` ";
+
$queries[] = "OPTIMIZE TABLE `$tbl_cats`";
$queries[] = "OPTIMIZE TABLE `$tbl_files`";
diff -Narub wp-filebase/js/common.js wp-filebase-updated/js/common.js
--- wp-filebase/js/common.js 2011-07-13 10:23:45.000000000 +0200
+++ wp-filebase-updated/js/common.js 2011-07-13 10:23:59.000000000 +0200
@@ -83,5 +83,10 @@
}
}
+function wpfb_open_email_form(file_id) {
+ // All further handling is done via GF's iframe
+ jQuery.getScript(WPFB.ajaxurl + '?action='+WPFB.getRequiredEmailForm + '&file_id=' + file_id);
+}
+
if(typeof(jQuery) != 'undefined')
jQuery(document).ready(wpfb_setupLinks);
\ No newline at end of file
diff -Narub wp-filebase/lib/wpfb_form_cat.php wp-filebase-updated/lib/wpfb_form_cat.php
--- wp-filebase/lib/wpfb_form_cat.php 2011-07-13 10:23:44.000000000 +0200
+++ wp-filebase-updated/lib/wpfb_form_cat.php 2011-07-13 10:23:59.000000000 +0200
@@ -67,6 +67,13 @@
</label>
</td>
</tr>
+ <tr>
+ <th scope="row" valign="top"><label for="cat_email_required"><?php _e('Email required', WPFB); ?></label></th>
+ <td>
+ <input type="checkbox" name="cat_email_required" id="cat_email_required" value="1" <?php checked($file_category->cat_email_required); ?> />
+
+ </td>
+ </tr>
<?php if($update) { ?>
<tr>
<th scope="row" valign="top"><label for="cat_child_apply_perm"><?php _e('Apply permission to all child files', WPFB) ?></label></th>
diff -Narub wp-filebase/lib/wpfb_form_file.php wp-filebase-updated/lib/wpfb_form_file.php
--- wp-filebase/lib/wpfb_form_file.php 2011-07-13 10:23:44.000000000 +0200
+++ wp-filebase-updated/lib/wpfb_form_file.php 2011-07-13 10:23:59.000000000 +0200
@@ -181,6 +181,14 @@
</label>
</td>
</tr>
+ <tr>
+ <th scope="row" valign="top"><label for="file_email_required"><?php _e('Email required', WPFB); ?></label></th>
+ <td>
+ <input type="checkbox" name="file_email_required" id="file_email_required"
+ value="1" <?php checked($file->file_email_required); ?> />
+
+ </td>
+ </tr>
<?php } ?>
</table>
<p class="submit"><input type="submit" class="button-primary" name="submit-btn" value="<?php echo $title ?>" <?php if(false && !$in_editor) { ?>onclick="this.form.submit(); return false;"<?php } ?>/></p>
diff -Narub wp-filebase/wp-filebase.css wp-filebase-updated/wp-filebase.css
--- wp-filebase/wp-filebase.css 2011-07-13 10:23:45.000000000 +0200
+++ wp-filebase-updated/wp-filebase.css 2011-07-13 10:24:01.000000000 +0200
@@ -82,3 +82,17 @@
.wpfilebase-catselect {
width: 100%;
}
+
+/* Settings for GF dialog */
+
+.ui-dialog[role="dialog"] .gform_ajax_spinner {
+ /*border: 10px solid green;*/
+ vertical-align: middle;
+}
+.ui-dialog[role="dialog"] .gform_button {
+ display: inline !important;
+}
+
+.ui-dialog[role="dialog"] .ui-widget-content {
+ background: #333 !important;
+}
diff -Narub wp-filebase/wp-filebase.php wp-filebase-updated/wp-filebase.php
--- wp-filebase/wp-filebase.php 2011-07-13 10:23:45.000000000 +0200
+++ wp-filebase-updated/wp-filebase.php 2011-07-13 10:24:01.000000000 +0200
@@ -71,7 +71,28 @@
function wpfilebase_init()
{
+ define('WPFB_GF_FOUND', defined('RG_CURRENT_PAGE'));
+
wpfb_loadclass('Core');
+
+ if (!is_admin()) {
+ wp_deregister_script('jqueryui');
+ wp_register_script('jqueryui',
+ plugins_url('jqueryui/js/jquery-ui-1.8.13.custom.min.js', __FILE__),
+ array('jquery'), '1.8.13');
+ wp_enqueue_script('jqueryui');
+
+ wp_deregister_style('jqueryui');
+ wp_register_style('jqueryui',
+ plugins_url('jqueryui/css/ui-darkness/jquery-ui-1.8.13.custom.css', __FILE__),
+ array(), '1.8.13');
+ wp_enqueue_style('jqueryui');
+ }
+ }
+
+ function gravityforms_missing_notice(){
+ // XXX Add i18n
+ echo '<div id="message" class="updated"><p>WPFB needs Gravityforms for some of it\'s features.</p></div>';
}
// called on activation AND version change/update!!
@axemm
Copy link

axemm commented Jul 16, 2015

excuse my question but how you should apply this patch

in the current version of wp-filebase I do not see ajax.php now I see a wpfb-ajax.php
It is this file should be changed in your code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment