Esempio n. 1
0
function tdomf_copy_form($form_id)
{
    global $wp_roles, $wpdb;
    $table_name = $wpdb->prefix . TDOMF_DB_TABLE_WIDGETS;
    // Copy form options
    //
    $form_name = sprintf(__("Copy of %s", "tdomf"), tdomf_get_option_form(TDOMF_OPTION_NAME, $form_id));
    $form_to_copy_options = tdomf_get_options_form($form_id);
    if (empty($form_to_copy_options)) {
        return 0;
    }
    $options = wp_parse_args($options, $form_to_copy_options);
    $copied_form_id = tdomf_create_form($form_name, $options);
    // Reset the "created pages" option
    //
    tdomf_set_option_form(TDOMF_OPTION_CREATEDPAGES, false, $copied_form_id);
    //Copy widget options
    //
    $query = "SELECT * \n            FROM {$table_name} \n            WHERE form_id = '" . $wpdb->escape($form_id) . "'";
    $widgets = $wpdb->get_results($query);
    foreach ($widgets as $widget) {
        tdomf_set_option_widget($widget->widget_key, maybe_unserialize($widget->widget_value), $copied_form_id);
    }
    // Copy capablities
    //
    if ($copied_form_id != 0) {
        if (!isset($wp_roles)) {
            $wp_roles = new WP_Roles();
        }
        $roles = $wp_roles->role_objects;
        foreach ($roles as $role) {
            if (isset($role->capabilities[TDOMF_CAPABILITY_CAN_SEE_FORM . '_' . $form_id])) {
                $role->add_cap(TDOMF_CAPABILITY_CAN_SEE_FORM . '_' . $copied_form_id);
            }
        }
    }
    return $copied_form_id;
}
function tdomf_handle_form_options_actions()
{
    global $wpdb, $wp_roles;
    $message = "";
    $retValue = false;
    if (!isset($wp_roles)) {
        $wp_roles = new WP_Roles();
    }
    $roles = $wp_roles->role_objects;
    $caps = tdomf_get_all_caps();
    $remove_throttle_rule = false;
    $rule_id = 0;
    if (isset($_REQUEST['tdomf_form_id'])) {
        $form_id = intval($_REQUEST['tdomf_form_id']);
        $rules = tdomf_get_option_form(TDOMF_OPTION_THROTTLE_RULES, $form_id);
        if (is_array($rules)) {
            foreach ($rules as $id => $r) {
                if (isset($_REQUEST["tdomf_remove_throttle_rule_{$id}"])) {
                    $remove_throttle_rule = true;
                    $rule_id = $id;
                    break;
                }
            }
        }
    }
    if ($remove_throttle_rule) {
        check_admin_referer('tdomf-options-save');
        unset($rules[$rule_id]);
        tdomf_set_option_form(TDOMF_OPTION_THROTTLE_RULES, $rules, $form_id);
        $message .= "Throttle rule removed!<br/>";
        tdomf_log_message("Removed throttle rule");
    } else {
        if (isset($_REQUEST['tdomf_add_throttle_rule'])) {
            check_admin_referer('tdomf-options-save');
            $form_id = intval($_REQUEST['tdomf_form_id']);
            $rule = array();
            $rule['sub_type'] = $_REQUEST['tdomf_throttle_rule_sub_type'];
            $rule['count'] = $_REQUEST['tdomf_throttle_rule_count'];
            $rule['type'] = $_REQUEST['tdomf_throttle_rule_user_type'];
            $rule['opt1'] = isset($_REQUEST['tdomf_throttle_rule_opt1']);
            $rule['time'] = intval($_REQUEST['tdomf_throttle_rule_time']);
            $rules = tdomf_get_option_form(TDOMF_OPTION_THROTTLE_RULES, $form_id);
            if (!is_array($rules)) {
                $rules = array();
            }
            $rules[] = $rule;
            tdomf_set_option_form(TDOMF_OPTION_THROTTLE_RULES, $rules, $form_id);
            $message .= "Throttle rule added!<br/>";
            tdomf_log_message("Added a new throttle rule: " . var_export($rule, true));
        } else {
            if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'create_form_page') {
                check_admin_referer('tdomf-create-form-page');
                $form_id = intval($_REQUEST['form']);
                $page_id = tdomf_create_form_page($form_id);
                $message = sprintf(__("A page with the form has been created. <a href='%s'>View page &raquo;</a><br/>", "tdomf"), get_permalink($page_id));
            } else {
                if (isset($_REQUEST['save_settings']) && isset($_REQUEST['tdomf_form_id'])) {
                    check_admin_referer('tdomf-options-save');
                    $form_id = intval($_REQUEST['tdomf_form_id']);
                    // Edit or Submit
                    $edit_form = false;
                    if (isset($_REQUEST['tdomf_mode']) && $_REQUEST['tdomf_mode'] == "edit") {
                        $edit_form = true;
                    }
                    tdomf_set_option_form(TDOMF_OPTION_FORM_EDIT, $edit_form, $form_id);
                    // Allow pages with forms to be editted
                    $edit_page_form = isset($_REQUEST['tdomf_edit_page_form']);
                    tdomf_set_option_form(TDOMF_OPTION_EDIT_PAGE_FORM, $edit_page_form, $form_id);
                    // Allow authors to edit
                    $author_edit = false;
                    if (isset($_REQUEST['tdomf_author_edit'])) {
                        $author_edit = true;
                    }
                    tdomf_set_option_form(TDOMF_OPTION_ALLOW_AUTHOR, $author_edit, $form_id);
                    // Edit post within X seconds of being published
                    $time_edit = false;
                    if (isset($_REQUEST['tdomf_time_edit'])) {
                        $time_edit = intval($_REQUEST['tdomf_time_edit']);
                        if ($time_edit <= 0) {
                            $time_edit = false;
                        }
                    }
                    tdomf_set_option_form(TDOMF_OPTION_ALLOW_TIME, $time_edit, $form_id);
                    // Who can access the form?
                    if (isset($_REQUEST['tdomf_special_access_anyone']) && tdomf_get_option_form(TDOMF_OPTION_ALLOW_EVERYONE, $form_id) == false) {
                        tdomf_set_option_form(TDOMF_OPTION_ALLOW_EVERYONE, true, $form_id);
                        foreach ($roles as $role) {
                            // remove cap as it's not needed
                            if (isset($role->capabilities[TDOMF_CAPABILITY_CAN_SEE_FORM . '_' . $form_id])) {
                                $role->remove_cap(TDOMF_CAPABILITY_CAN_SEE_FORM . '_' . $form_id);
                            }
                        }
                        tdomf_set_option_form(TDOMF_OPTION_ALLOW_CAPS, array(), $form_id);
                    } else {
                        if (!isset($_REQUEST['tdomf_special_access_anyone'])) {
                            tdomf_set_option_form(TDOMF_OPTION_ALLOW_EVERYONE, false, $form_id);
                            // add cap to right roles
                            foreach ($roles as $role) {
                                if (isset($_REQUEST["tdomf_access_" . $role->name])) {
                                    $role->add_cap(TDOMF_CAPABILITY_CAN_SEE_FORM . '_' . $form_id);
                                } else {
                                    if (isset($role->capabilities[TDOMF_CAPABILITY_CAN_SEE_FORM . '_' . $form_id])) {
                                        $role->remove_cap(TDOMF_CAPABILITY_CAN_SEE_FORM . '_' . $form_id);
                                    }
                                }
                            }
                            // list caps that can access form
                            $allow_caps = array();
                            foreach ($caps as $cap) {
                                if (isset($_REQUEST['tdomf_access_caps_' . $cap])) {
                                    $allow_caps[] = $cap;
                                }
                            }
                            tdomf_set_option_form(TDOMF_OPTION_ALLOW_CAPS, $allow_caps, $form_id);
                            // convert user names to ids
                            $allow_users = array();
                            if (isset($_REQUEST['tdomf_access_users_list'])) {
                                $user_names = trim($_REQUEST['tdomf_access_users_list']);
                                if (!empty($user_names)) {
                                    $user_names = explode(' ', $user_names);
                                    foreach ($user_names as $user_name) {
                                        if (!empty($user_name)) {
                                            if (($userdata = get_userdatabylogin($user_name)) != false) {
                                                $allow_users[] = $userdata->ID;
                                            } else {
                                                $message .= "<font color='red'>" . sprintf(__("{$user_name} is not a valid user name. Ignoring.<br/>", "tdomf"), $form_id) . "</font>";
                                                tdomf_log_message("User login {$user_name} is not recognised by wordpress. Ignoring.", TDOMF_LOG_BAD);
                                            }
                                        }
                                    }
                                }
                            }
                            tdomf_set_option_form(TDOMF_OPTION_ALLOW_USERS, $allow_users, $form_id);
                        }
                    }
                    tdomf_set_option_form(TDOMF_OPTION_ALLOW_PUBLISH, isset($_REQUEST['tdomf_user_publish_override']), $form_id);
                    // Who gets notified?
                    $notify_roles = "";
                    foreach ($roles as $role) {
                        if (isset($_REQUEST["tdomf_notify_" . $role->name])) {
                            $notify_roles .= $role->name . ";";
                        }
                    }
                    if (!empty($notify_roles)) {
                        tdomf_set_option_form(TDOMF_NOTIFY_ROLES, $notify_roles, $form_id);
                    } else {
                        tdomf_set_option_form(TDOMF_NOTIFY_ROLES, false, $form_id);
                    }
                    $save = true;
                    $tdomf_admin_emails = $_POST['tdomf_admin_emails'];
                    $emails = explode(',', $tdomf_admin_emails);
                    foreach ($emails as $email) {
                        if (!empty($email)) {
                            if (!tdomf_check_email_address($email)) {
                                $message .= "<font color='red'>" . sprintf(__("The email %s is not valid! Please update 'Who Gets Notified' with valid email addresses.", "tdomf"), $email) . "</font><br/>";
                                $save = false;
                                break;
                            }
                        }
                    }
                    if ($save) {
                        tdomf_set_option_form(TDOMF_OPTION_ADMIN_EMAILS, $tdomf_admin_emails, $form_id);
                    }
                    // Default Category
                    $def_cat = $_POST['tdomf_def_cat'];
                    tdomf_set_option_form(TDOMF_DEFAULT_CATEGORY, $def_cat, $form_id);
                    // Restrict editing to posts submitted by tdomf
                    $edit_restrict_tdomf = isset($_REQUEST['tdomf_edit_tdomf_only']);
                    tdomf_set_option_form(TDOMF_OPTION_EDIT_RESTRICT_TDOMF, $edit_restrict_tdomf, $form_id);
                    $edit_restrict_cats = explode(',', trim($_REQUEST['tdomf_edit_cats']));
                    if (!empty($edit_restrict_cats)) {
                        $cats = array();
                        foreach ($edit_restrict_cats as $cat) {
                            $cat = intval(trim($cat));
                            if ($cat > 0) {
                                $cats[] = $cat;
                            }
                        }
                        $edit_restrict_cats = $cats;
                    } else {
                        $edit_restrict_cats = array();
                    }
                    tdomf_set_option_form(TDOMF_OPTION_EDIT_RESTRICT_CATS, $edit_restrict_cats, $form_id);
                    // add edit link
                    $add_edit_link = $_REQUEST['tdomf_add_edit_link'];
                    if ($add_edit_link == 'custom') {
                        $add_edit_link = $_REQUEST['tdomf_add_edit_link_custom_url'];
                    }
                    tdomf_set_option_form(TDOMF_OPTION_ADD_EDIT_LINK, $add_edit_link, $form_id);
                    $ajax_edit = isset($_REQUEST['tdomf_ajax_edit']);
                    tdomf_set_option_form(TDOMF_OPTION_AJAX_EDIT, $ajax_edit, $form_id);
                    // auto modify edit link
                    $auto_edit_link = $_REQUEST['tdomf_auto_edit_link'];
                    if ($auto_edit_link == 'custom') {
                        $auto_edit_link = $_REQUEST['tdomf_auto_edit_link_custom_url'];
                    }
                    tdomf_set_option_form(TDOMF_OPTION_AUTO_EDIT_LINK, $auto_edit_link, $form_id);
                    //Turn On/Off Moderation
                    $mod = false;
                    if (isset($_POST['tdomf_moderation'])) {
                        $mod = true;
                    }
                    tdomf_set_option_form(TDOMF_OPTION_MODERATION, $mod, $form_id);
                    $tdomf_redirect = isset($_POST['tdomf_redirect']);
                    tdomf_set_option_form(TDOMF_OPTION_REDIRECT, $tdomf_redirect, $form_id);
                    //Preview
                    $preview = false;
                    if (isset($_POST['tdomf_preview'])) {
                        $preview = true;
                    }
                    tdomf_set_option_form(TDOMF_OPTION_PREVIEW, $preview, $form_id);
                    //From email
                    if (trim($_POST['tdomf_from_email']) == "") {
                        tdomf_set_option_form(TDOMF_OPTION_FROM_EMAIL, false, $form_id);
                    } else {
                        tdomf_set_option_form(TDOMF_OPTION_FROM_EMAIL, $_POST['tdomf_from_email'], $form_id);
                    }
                    // Form name
                    if (trim($_POST['tdomf_form_name']) == "") {
                        tdomf_set_option_form(TDOMF_OPTION_NAME, "", $form_id);
                    } else {
                        tdomf_set_option_form(TDOMF_OPTION_NAME, strip_tags($_POST['tdomf_form_name']), $form_id);
                    }
                    // Form description
                    if (trim($_POST['tdomf_form_descp']) == "") {
                        tdomf_set_option_form(TDOMF_OPTION_DESCRIPTION, false, $form_id);
                    } else {
                        tdomf_set_option_form(TDOMF_OPTION_DESCRIPTION, $_POST['tdomf_form_descp'], $form_id);
                    }
                    // Include on "your submissions" page
                    //
                    $include = false;
                    if (isset($_POST['tdomf_include_sub'])) {
                        $include = true;
                    }
                    tdomf_set_option_form(TDOMF_OPTION_INCLUDED_YOUR_SUBMISSIONS, $include, $form_id);
                    if (get_option(TDOMF_OPTION_YOUR_SUBMISSIONS) && $include) {
                        $message .= sprintf(__("Saved Options for Form %d. <a href='%s'>See your form &raquo</a>", "tdomf"), $form_id, "users.php?page=tdomf_your_submissions#tdomf_form%d") . "<br/>";
                    } else {
                        $message .= sprintf(__("Saved Options for Form %d.", "tdomf"), $form_id) . "<br/>";
                    }
                    // widget count
                    //
                    $widget_count = 10;
                    if (isset($_POST['tdomf_widget_count'])) {
                        $widget_count = intval($_POST['tdomf_widget_count']);
                    }
                    if ($widget_count < 1) {
                        $widget_count = 1;
                    }
                    tdomf_set_option_form(TDOMF_OPTION_WIDGET_INSTANCES, $widget_count, $form_id);
                    //Submit page instead of post
                    //
                    $use_page = false;
                    if (isset($_POST['tdomf_use_type']) && $_POST['tdomf_use_type'] == 'page') {
                        $use_page = true;
                    }
                    tdomf_set_option_form(TDOMF_OPTION_SUBMIT_PAGE, $use_page, $form_id);
                    // Queue period
                    //
                    $tdomf_queue_period = intval($_POST['tdomf_queue_period']);
                    tdomf_set_option_form(TDOMF_OPTION_QUEUE_PERIOD, $tdomf_queue_period, $form_id);
                    // Queue on all
                    //
                    $tdomf_queue_on_all = isset($_POST['tdomf_queue_on_all']);
                    tdomf_set_option_form(TDOMF_OPTION_QUEUE_ON_ALL, $tdomf_queue_on_all, $form_id);
                    // ajax
                    //
                    $tdomf_ajax = isset($_POST['tdomf_ajax']);
                    tdomf_set_option_form(TDOMF_OPTION_AJAX, $tdomf_ajax, $form_id);
                    // Send moderation email even for published posts
                    //
                    $tdomf_mod_email_on_pub = isset($_POST['tdomf_mod_email_on_pub']);
                    tdomf_set_option_form(TDOMF_OPTION_MOD_EMAIL_ON_PUB, $tdomf_mod_email_on_pub, $form_id);
                    // Admin users auto-publish?
                    //
                    $tdomf_publish_no_mod = isset($_POST['tdomf_user_publish_auto']);
                    tdomf_set_option_form(TDOMF_OPTION_PUBLISH_NO_MOD, $tdomf_publish_no_mod, $form_id);
                    // Spam
                    //
                    $message .= tdomf_handle_spam_options_actions($form_id);
                    tdomf_log_message("Options Saved for Form ID {$form_id}");
                } else {
                    if (isset($_REQUEST['delete'])) {
                        $form_id = intval($_REQUEST['delete']);
                        check_admin_referer('tdomf-delete-form-' . $form_id);
                        if (tdomf_form_exists($form_id)) {
                            $count_forms = count(tdomf_get_form_ids());
                            if ($count_forms > 1) {
                                if (tdomf_delete_form($form_id)) {
                                    $message .= sprintf(__("Form %d deleted.<br/>", "tdomf"), $form_id);
                                } else {
                                    $message .= sprintf(__("Could not delete Form %d!<br/>", "tdomf"), $form_id);
                                }
                            } else {
                                $message .= sprintf(__("You cannot delete the last form! There must be at least one form in the system.<br/>", "tdomf"), $form_id);
                            }
                        } else {
                            $message .= sprintf(__("Form %d is not valid!<br/>", "tdomf"), $form_id);
                        }
                    } else {
                        if (isset($_REQUEST['copy'])) {
                            $form_id = intval($_REQUEST['copy']);
                            check_admin_referer('tdomf-copy-form-' . $form_id);
                            $copy_form_id = tdomf_copy_form($form_id);
                            if ($copy_form_id != 0) {
                                $message .= sprintf(__("Form %d copied with id %d.<br/>", "tdomf"), $form_id, $copy_form_id);
                                $retValue = $copy_form_id;
                            } else {
                                $message .= sprintf(__("Failed to copy Form %d!<br/>", "tdomf"), $form_id);
                            }
                        } else {
                            if (isset($_REQUEST['new'])) {
                                check_admin_referer('tdomf-new-form');
                                $form_id = tdomf_create_form(__('New Form', 'tdomf'), array());
                                if ($form_id != 0) {
                                    $message .= sprintf(__("New form created with %d.<br/>", "tdomf"), $form_id);
                                    $retValue = $form_id;
                                } else {
                                    $message .= __("Failed to create new Form!<br/>", "tdomf");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    // Warnings
    $message .= tdomf_get_error_messages(false);
    if (!empty($message)) {
        ?>
   <div id="message" class="updated fade"><p><?php 
        echo $message;
        ?>
</p></div>
   <?php 
    }
    return $retValue;
}