//from email address
if (!empty($esl_from_email_address)) {
    if (is_numeric($esl_from_email_address)) {
        $admin_email_param['from_email'] = '{element_' . $esl_from_email_address . '}';
    } else {
        $admin_email_param['from_email'] = $esl_from_email_address;
    }
} else {
    if (!empty($mf_settings['default_from_email'])) {
        $admin_email_param['from_email'] = $mf_settings['default_from_email'];
    } else {
        $domain = str_replace('www.', '', $_SERVER['SERVER_NAME']);
        $admin_email_param['from_email'] = "no-reply@{$domain}";
    }
}
//subject
if (!empty($esl_subject)) {
    $admin_email_param['subject'] = $esl_subject;
} else {
    $admin_email_param['subject'] = '{form_name} [#{entry_no}]';
}
//content
if (!empty($esl_content)) {
    $admin_email_param['content'] = $esl_content;
} else {
    $admin_email_param['content'] = '{entry_data}';
}
$admin_email_param['as_plain_text'] = $esl_plain_text;
$admin_email_param['target_is_admin'] = true;
mf_send_notification($dbh, $form_id, $entry_id, $target_email, $admin_email_param);
echo '{"status" : "ok"}';
function mf_commit_form_review($dbh, $form_id, $record_id, $options = array())
{
    $mf_settings = mf_get_settings($dbh);
    //by default, this function will send notification email
    if ($options['send_notification'] === false) {
        $send_notification = false;
    } else {
        $send_notification = true;
    }
    //move data from ap_form_x_review table to ap_form_x table
    //get all column name except session_id and id
    $query = "SELECT * FROM `" . MF_TABLE_PREFIX . "form_{$form_id}_review` WHERE id=?";
    $params = array($record_id);
    $sth = mf_do_query($query, $params, $dbh);
    $row = mf_do_fetch_result($sth);
    $columns = array();
    foreach ($row as $column_name => $column_data) {
        if ($column_name != 'id' && $column_name != 'session_id' && $column_name != 'status' && $column_name != 'resume_key') {
            $columns[] = $column_name;
        }
    }
    $columns_joined = implode("`,`", $columns);
    $columns_joined = '`' . $columns_joined . '`';
    //copy data from review table
    $query = "INSERT INTO `" . MF_TABLE_PREFIX . "form_{$form_id}`({$columns_joined}) SELECT {$columns_joined} from `" . MF_TABLE_PREFIX . "form_{$form_id}_review` WHERE id=?";
    $params = array($record_id);
    mf_do_query($query, $params, $dbh);
    $new_record_id = (int) $dbh->lastInsertId();
    //check for resume_key from the review table
    //if there is resume_key, we need to delete the incomplete record within ap_form_x table which contain that resume_key
    $query = "SELECT `resume_key` FROM `" . MF_TABLE_PREFIX . "form_{$form_id}_review` WHERE id=?";
    $params = array($record_id);
    $sth = mf_do_query($query, $params, $dbh);
    $row = mf_do_fetch_result($sth);
    if (!empty($row['resume_key'])) {
        $query = "DELETE from `" . MF_TABLE_PREFIX . "form_{$form_id}` where resume_key=? and `status`=2";
        $params = array($row['resume_key']);
        mf_do_query($query, $params, $dbh);
    }
    //rename file uploads, if any
    //get all file uploads elements first
    $query = "SELECT \r\n\t\t\t\t\t\telement_id \r\n\t\t\t\t\tFROM \r\n\t\t\t\t\t\t" . MF_TABLE_PREFIX . "form_elements \r\n\t\t\t\t   WHERE \r\n\t\t\t\t   \t\tform_id=? AND \r\n\t\t\t\t   \t\telement_type='file' AND \r\n\t\t\t\t   \t\telement_status=1 AND\r\n\t\t\t\t   \t\telement_is_private=0";
    $params = array($form_id);
    $file_uploads_array = array();
    $sth = mf_do_query($query, $params, $dbh);
    while ($row = mf_do_fetch_result($sth)) {
        $file_uploads_array[] = 'element_' . $row['element_id'];
    }
    if (!empty($file_uploads_array)) {
        $file_uploads_column = implode('`,`', $file_uploads_array);
        $file_uploads_column = '`' . $file_uploads_column . '`';
        $query = "SELECT {$file_uploads_column} FROM `" . MF_TABLE_PREFIX . "form_{$form_id}_review` where id=?";
        $params = array($record_id);
        $sth = mf_do_query($query, $params, $dbh);
        $row = mf_do_fetch_result($sth);
        $file_update_query = '';
        foreach ($file_uploads_array as $element_name) {
            $filename_record = $row[$element_name];
            if (empty($filename_record)) {
                continue;
            }
            //if the file upload field is using advance uploader, $filename would contain multiple file names, separated by pipe character '|'
            $filename_array = array();
            $filename_array = explode('|', $filename_record);
            $file_joined_value = '';
            foreach ($filename_array as $filename) {
                $target_filename = $options['machform_data_path'] . $mf_settings['upload_dir'] . "/form_{$form_id}/files/{$filename}.tmp";
                $regex = '/^element_([0-9]*)_([0-9a-zA-Z]*)-([0-9]*)-(.*)$/';
                $matches = array();
                preg_match($regex, $filename, $matches);
                $filename_noelement = $matches[4];
                $file_token = md5(uniqid(rand(), true));
                //add random token to uploaded filename, to increase security
                $destination_filename = $options['machform_data_path'] . $mf_settings['upload_dir'] . "/form_{$form_id}/files/{$element_name}_{$file_token}-{$new_record_id}-{$filename_noelement}";
                if (file_exists($target_filename)) {
                    rename($target_filename, $destination_filename);
                }
                $filename_noelement = addslashes(stripslashes($filename_noelement));
                $file_joined_value .= "{$element_name}_{$file_token}-{$new_record_id}-{$filename_noelement}|";
            }
            //build update query
            $file_joined_value = rtrim($file_joined_value, '|');
            $file_update_query .= "`{$element_name}`='{$file_joined_value}',";
        }
        $file_update_query = rtrim($file_update_query, ',');
        if (!empty($file_update_query)) {
            $query = "UPDATE `" . MF_TABLE_PREFIX . "form_{$form_id}` SET {$file_update_query} WHERE id=?";
            $params = array($new_record_id);
            mf_do_query($query, $params, $dbh);
        }
    }
    $_SESSION['mf_form_completed'][$form_id] = true;
    //send notification emails
    //get form properties data
    $query = "select \r\n\t\t\t\t\t\t form_redirect,\r\n\t\t\t\t\t\t form_redirect_enable,\r\n\t\t\t\t\t\t form_email,\r\n\t\t\t\t\t\t esl_enable,\r\n\t\t\t\t\t\t esl_from_name,\r\n\t\t\t\t\t\t esl_from_email_address,\r\n\t\t\t\t\t\t esl_subject,\r\n\t\t\t\t\t\t esl_content,\r\n\t\t\t\t\t\t esl_plain_text,\r\n\t\t\t\t\t\t esr_enable,\r\n\t\t\t\t\t\t esr_email_address,\r\n\t\t\t\t\t\t esr_from_name,\r\n\t\t\t\t\t\t esr_from_email_address,\r\n\t\t\t\t\t\t esr_subject,\r\n\t\t\t\t\t\t esr_content,\r\n\t\t\t\t\t\t esr_plain_text\r\n\t\t\t\t     from \r\n\t\t\t\t     \t `" . MF_TABLE_PREFIX . "forms` \r\n\t\t\t\t    where \r\n\t\t\t\t    \t form_id=?";
    $params = array($form_id);
    $sth = mf_do_query($query, $params, $dbh);
    $row = mf_do_fetch_result($sth);
    if (!empty($row['form_redirect_enable'])) {
        $form_redirect = $row['form_redirect'];
    }
    $form_email = $row['form_email'];
    $esl_from_name = $row['esl_from_name'];
    $esl_from_email_address = $row['esl_from_email_address'];
    $esl_subject = $row['esl_subject'];
    $esl_content = $row['esl_content'];
    $esl_plain_text = $row['esl_plain_text'];
    $esl_enable = $row['esl_enable'];
    $esr_email_address = $row['esr_email_address'];
    $esr_from_name = $row['esr_from_name'];
    $esr_from_email_address = $row['esr_from_email_address'];
    $esr_subject = $row['esr_subject'];
    $esr_content = $row['esr_content'];
    $esr_plain_text = $row['esr_plain_text'];
    $esr_enable = $row['esr_enable'];
    //start sending notification email to admin ------------------------------------------
    if (!empty($esl_enable) && !empty($form_email) && $send_notification === true) {
        //get parameters for the email
        //from name
        if (!empty($esl_from_name)) {
            if (is_numeric($esl_from_name)) {
                $admin_email_param['from_name'] = '{element_' . $esl_from_name . '}';
            } else {
                $admin_email_param['from_name'] = $esl_from_name;
            }
        } else {
            $admin_email_param['from_name'] = 'MachForm';
        }
        //from email address
        if (!empty($esl_from_email_address)) {
            if (is_numeric($esl_from_email_address)) {
                $admin_email_param['from_email'] = '{element_' . $esl_from_email_address . '}';
            } else {
                $admin_email_param['from_email'] = $esl_from_email_address;
            }
        } else {
            $domain = str_replace('www.', '', $_SERVER['SERVER_NAME']);
            $admin_email_param['from_email'] = "no-reply@{$domain}";
        }
        //subject
        if (!empty($esl_subject)) {
            $admin_email_param['subject'] = $esl_subject;
        } else {
            $admin_email_param['subject'] = '{form_name} [#{entry_no}]';
        }
        //content
        if (!empty($esl_content)) {
            $admin_email_param['content'] = $esl_content;
        } else {
            $admin_email_param['content'] = '{entry_data}';
        }
        $admin_email_param['as_plain_text'] = $esl_plain_text;
        $admin_email_param['target_is_admin'] = true;
        $admin_email_param['machform_base_path'] = $options['machform_path'];
        $admin_email_param['check_hook_file'] = true;
        mf_send_notification($dbh, $form_id, $new_record_id, $form_email, $admin_email_param);
    }
    //end emailing notifications to admin ----------------------------------------------
    //start sending notification email to user ------------------------------------------
    if (!empty($esr_enable) && !empty($esr_email_address) && $send_notification === true) {
        //get parameters for the email
        //to email
        if (is_numeric($esr_email_address)) {
            $esr_email_address = '{element_' . $esr_email_address . '}';
        }
        //from name
        if (!empty($esr_from_name)) {
            if (is_numeric($esr_from_name)) {
                $user_email_param['from_name'] = '{element_' . $esr_from_name . '}';
            } else {
                $user_email_param['from_name'] = $esr_from_name;
            }
        } else {
            $user_email_param['from_name'] = 'MachForm';
        }
        //from email address
        if (!empty($esr_from_email_address)) {
            if (is_numeric($esr_from_email_address)) {
                $user_email_param['from_email'] = '{element_' . $esr_from_email_address . '}';
            } else {
                $user_email_param['from_email'] = $esr_from_email_address;
            }
        } else {
            $domain = str_replace('www.', '', $_SERVER['SERVER_NAME']);
            $user_email_param['from_email'] = "no-reply@{$domain}";
        }
        //subject
        if (!empty($esr_subject)) {
            $user_email_param['subject'] = $esr_subject;
        } else {
            $user_email_param['subject'] = '{form_name} - Receipt';
        }
        //content
        if (!empty($esr_content)) {
            $user_email_param['content'] = $esr_content;
        } else {
            $user_email_param['content'] = '{entry_data}';
        }
        $user_email_param['as_plain_text'] = $esr_plain_text;
        $user_email_param['target_is_admin'] = false;
        $user_email_param['machform_base_path'] = $options['machform_path'];
        mf_send_notification($dbh, $form_id, $new_record_id, $esr_email_address, $user_email_param);
    }
    //end emailing notifications to user ----------------------------------------------
    //delete all entry from this user in review table
    $session_id = session_id();
    $query = "DELETE FROM `" . MF_TABLE_PREFIX . "form_{$form_id}_review` where id=? or session_id=?";
    $params = array($record_id, $session_id);
    mf_do_query($query, $params, $dbh);
    //remove form history from session
    $_SESSION['mf_form_loaded'][$form_id] = array();
    unset($_SESSION['mf_form_loaded'][$form_id]);
    //remove form access session
    $_SESSION['mf_form_access'][$form_id] = array();
    unset($_SESSION['mf_form_access'][$form_id]);
    $_SESSION['mf_form_resume_url'][$form_id] = array();
    unset($_SESSION['mf_form_resume_url'][$form_id]);
    //remove pages history
    $_SESSION['mf_pages_history'][$form_id] = array();
    unset($_SESSION['mf_pages_history'][$form_id]);
    //unset the form resume session, if any
    $_SESSION['mf_form_resume_loaded'][$form_id] = false;
    unset($_SESSION['mf_form_resume_loaded'][$form_id]);
    //get merchant redirect url, if enabled for this form
    $merchant_redirect_url = mf_get_merchant_redirect_url($dbh, $form_id, $new_record_id);
    if (!empty($merchant_redirect_url)) {
        $form_redirect = $merchant_redirect_url;
    }
    $commit_result['form_redirect'] = $form_redirect;
    $commit_result['record_insert_id'] = $new_record_id;
    return $commit_result;
}
function mf_process_delayed_notifications($dbh, $form_id, $entry_id, $options = array())
{
    //get form properties data
    $query = "select \r\n\t\t\t\t\t\t form_redirect,\r\n\t\t\t\t\t\t form_redirect_enable,\r\n\t\t\t\t\t\t form_email,\r\n\t\t\t\t\t\t esl_enable,\r\n\t\t\t\t\t\t esl_from_name,\r\n\t\t\t\t\t\t esl_from_email_address,\r\n\t\t\t\t\t\t esl_subject,\r\n\t\t\t\t\t\t esl_content,\r\n\t\t\t\t\t\t esl_plain_text,\r\n\t\t\t\t\t\t esr_enable,\r\n\t\t\t\t\t\t esr_email_address,\r\n\t\t\t\t\t\t esr_from_name,\r\n\t\t\t\t\t\t esr_from_email_address,\r\n\t\t\t\t\t\t esr_subject,\r\n\t\t\t\t\t\t esr_content,\r\n\t\t\t\t\t\t esr_plain_text\r\n\t\t\t\t     from \r\n\t\t\t\t     \t `" . MF_TABLE_PREFIX . "forms` \r\n\t\t\t\t    where \r\n\t\t\t\t    \t form_id=?";
    $params = array($form_id);
    $sth = mf_do_query($query, $params, $dbh);
    $row = mf_do_fetch_result($sth);
    if (!empty($row['form_redirect_enable'])) {
        $form_redirect = $row['form_redirect'];
    }
    $form_email = $row['form_email'];
    $esl_from_name = $row['esl_from_name'];
    $esl_from_email_address = $row['esl_from_email_address'];
    $esl_subject = $row['esl_subject'];
    $esl_content = $row['esl_content'];
    $esl_plain_text = $row['esl_plain_text'];
    $esl_enable = $row['esl_enable'];
    $esr_email_address = $row['esr_email_address'];
    $esr_from_name = $row['esr_from_name'];
    $esr_from_email_address = $row['esr_from_email_address'];
    $esr_subject = $row['esr_subject'];
    $esr_content = $row['esr_content'];
    $esr_plain_text = $row['esr_plain_text'];
    $esr_enable = $row['esr_enable'];
    //start sending notification email to admin ------------------------------------------
    if (!empty($esl_enable) && !empty($form_email)) {
        //get parameters for the email
        //from name
        if (!empty($esl_from_name)) {
            if (is_numeric($esl_from_name)) {
                $admin_email_param['from_name'] = '{element_' . $esl_from_name . '}';
            } else {
                $admin_email_param['from_name'] = $esl_from_name;
            }
        } else {
            $admin_email_param['from_name'] = 'MachForm';
        }
        //from email address
        if (!empty($esl_from_email_address)) {
            if (is_numeric($esl_from_email_address)) {
                $admin_email_param['from_email'] = '{element_' . $esl_from_email_address . '}';
            } else {
                $admin_email_param['from_email'] = $esl_from_email_address;
            }
        } else {
            $domain = str_replace('www.', '', $_SERVER['SERVER_NAME']);
            $admin_email_param['from_email'] = "no-reply@{$domain}";
        }
        //subject
        if (!empty($esl_subject)) {
            $admin_email_param['subject'] = $esl_subject;
        } else {
            $admin_email_param['subject'] = '{form_name} [#{entry_no}]';
        }
        //content
        if (!empty($esl_content)) {
            $admin_email_param['content'] = $esl_content;
        } else {
            $admin_email_param['content'] = '{entry_data}';
        }
        $admin_email_param['as_plain_text'] = $esl_plain_text;
        $admin_email_param['target_is_admin'] = true;
        $admin_email_param['machform_base_path'] = $options['machform_path'];
        $admin_email_param['check_hook_file'] = true;
        mf_send_notification($dbh, $form_id, $entry_id, $form_email, $admin_email_param);
    }
    //end emailing notifications to admin ----------------------------------------------
    //start sending notification email to user ------------------------------------------
    if (!empty($esr_enable) && !empty($esr_email_address)) {
        //get parameters for the email
        //to email
        if (is_numeric($esr_email_address)) {
            $esr_email_address = '{element_' . $esr_email_address . '}';
        }
        //from name
        if (!empty($esr_from_name)) {
            if (is_numeric($esr_from_name)) {
                $user_email_param['from_name'] = '{element_' . $esr_from_name . '}';
            } else {
                $user_email_param['from_name'] = $esr_from_name;
            }
        } else {
            $user_email_param['from_name'] = 'MachForm';
        }
        //from email address
        if (!empty($esr_from_email_address)) {
            if (is_numeric($esr_from_email_address)) {
                $user_email_param['from_email'] = '{element_' . $esr_from_email_address . '}';
            } else {
                $user_email_param['from_email'] = $esr_from_email_address;
            }
        } else {
            $domain = str_replace('www.', '', $_SERVER['SERVER_NAME']);
            $user_email_param['from_email'] = "no-reply@{$domain}";
        }
        //subject
        if (!empty($esr_subject)) {
            $user_email_param['subject'] = $esr_subject;
        } else {
            $user_email_param['subject'] = '{form_name} - Receipt';
        }
        //content
        if (!empty($esr_content)) {
            $user_email_param['content'] = $esr_content;
        } else {
            $user_email_param['content'] = '{entry_data}';
        }
        $user_email_param['as_plain_text'] = $esr_plain_text;
        $user_email_param['target_is_admin'] = false;
        $user_email_param['machform_base_path'] = $options['machform_path'];
        mf_send_notification($dbh, $form_id, $entry_id, $esr_email_address, $user_email_param);
    }
    //end emailing notifications to user ----------------------------------------------
}