function system_handle_form_submit

7.x system.module system_handle_form_submit()
6.x system.module system_handle_form_submit()
4.x system.module system_handle_form_submit()
5.x system.module system_handle_form_submit()

Intercepts form submissions from System Settings Forms.

File

modules/system/system.module, line 1190

Code

function system_handle_form_submit() {

  $callback = $_REQUEST ["callback"];

  $form_type = $_REQUEST ["form_type"];
  $form_include = $_REQUEST ["form_include"];

  $form_token = $_REQUEST ["form_token"];
  // Make sure the form_token is valid!
  if ($form_token != md5($callback . fp_token())) {
    die(t("Sorry, but you have encountered an error.  A form submission was flagged
          as possibly being an invalid or forged submission.  This may constitute a bug
          in the system.  Please report this error to your Systems Administrator."));
  }

  if ($form_include != "") {
    // This is a file we need to include in order to complete the submission process.
    include_once ($form_include);
  }


  // We need to make sure the user has permission to submit this form!
  $form_path = $_REQUEST ["form_path"];
  // Check the menu router table for whatever the permissions were for this
  // path, if any.
  if ($form_path != "") {
    $router_item = menu_get_item($form_path);
    if (!menu_check_user_access($router_item)) {
      // The user does NOT have access to submit this form!  The fact that
      // it has made it this far means this may be some sort of hacking attempt.
      die(t("Sorry, but you have encountered an error.  A form submission was flagged
          as possibly being an invalid or having insufficient permissions to submit.  
          This may constitute a bug in the system.  
          Please report this error to your Systems Administrator."));

    }
  }

  // Let's get our set of allowed values, by looking at the original form,
  // and grab what's in the POST which matches the name.

  $values = array();
  if (function_exists($callback)) {
    $form = fp_get_form($callback);
    foreach ($form as $name => $element) {

      $values [$name] = $_POST [$name];
      // Do we need to alter the value from the POST?

      // If this is a checkbox, and we have any value in the POST, it should
      // be saved as boolean TRUE
      if ($element ["type"] == "checkbox") {
        if (isset($_POST [$name]) && $_POST [$name] === "1") {
          $values [$name] = TRUE;
        }
      }

    }
  }

  // Does the form have any defined submit_handler's?  If not, let's assign it the
  // default of callback_submit().    
  $submit_handlers = $form ["#submit_handlers"];
  if (!is_array($submit_handlers)) {
    $submit_handlers = array();
  }

  // If the submit_handlers is empty, then add our default submit handler.  We don't
  // want to do this if the user went out of their way to enter a different handler.
  if (count($submit_handlers) == 0) {
    array_push($submit_handlers, $callback . "_submit");
  }

  // Does the form have any defined validate_handler's?  This works exactly like the submit handler.
  $validate_handlers = $form ["#validate_handlers"];
  if (!is_array($validate_handlers)) {
    $validate_handlers = array();
  }

  if (count($validate_handlers) == 0) {
    array_push($validate_handlers, $callback . "_validate");
  }

  // Let's store our values in the SESSION in case we need them later on.
  // But only if this is NOT a system_settings form!
  if ($form_type != "system_settings") {
    $_SESSION ["fp_form_submissions"][$callback]["values"] = $values;
  }

  $form_state = array("values" => $values);

  // Let's pass this through our default form validator (mainly to check for required fields
  // which do not have values entered)
  form_basic_validate($form, $form_state);

  if (!form_has_errors()) {
    // Let's now pass it through all of our custom validators, if there are any.
    foreach ($validate_handlers as $validate_callback) {
      if (function_exists($validate_callback)) {

        call_user_func_array($validate_callback, array(&$form, &$form_state));
      }
    }
  }

  if (!form_has_errors()) {
    // No errors from the validate, so let's continue.

    // Is this a "system settings" form, or a normal form?
    if ($form_type == "system_settings") {
      // This is system settings, so let's save all of our values to the variables table.
      // Write our values array to our variable table.
      foreach ($form_state ["values"] as $name => $val) {

        variable_set($name, $val);
      }

      fp_add_message("Settings saved successfully.");

    }

    // Let's go through the form's submit handlers now.
    foreach ($submit_handlers as $submit_callback) {
      if (function_exists($submit_callback)) {
        //call_user_func($submit_callback, $form, &$form_state);        
        call_user_func_array($submit_callback, array(&$form, &$form_state));
      }
    }



  }

  // Figure out where we are supposed to redirect the user.
  $redirect_path = $redirect_query = "";

  if (is_array($form ["#redirect"])) {
    $redirect_path = $form ["#redirect"]["path"];
    $redirect_query = $form ["#redirect"]["query"];
  }
  else {
    $redirect_path = $_REQUEST ["default_redirect_path"];
    $redirect_query = $_REQUEST ["default_redirect_query"];
  }

  // Okay, go back to where we were!  
  fp_goto($redirect_path, $redirect_query);

}