/**
 * Sends the mail and returns whether that was successful.
 *
 * @param string $id           A form ID.
 * @param bool   $confirmation Whether to send the confirmation mail.
 *
 * @return bool
 *
 * @global array  The paths of system files and folders.
 * @global string The current language.
 * @global array  The configuration of the plugins.
 * @global array  The localization of the plugins.
 * @global string The (X)HTML fragment that contains error messages.
 */
function Advancedform_mail($id, $confirmation)
{
    global $pth, $sl, $plugin_cf, $plugin_tx, $e;
    include_once $pth['folder']['plugins'] . 'advancedform/phpmailer/class.phpmailer.php';
    $pcf = $plugin_cf['advancedform'];
    $ptx = $plugin_tx['advancedform'];
    $forms = Advancedform_db();
    $form = $forms[$id];
    $type = strtolower($pcf['mail_type']);
    $from = '';
    $from_name = '';
    foreach ($form['fields'] as $field) {
        if ($field['type'] == 'from_name') {
            $from_name = stsl($_POST['advfrm-' . $field['field']]);
        } elseif ($field['type'] == 'from') {
            $from = stsl($_POST['advfrm-' . $field['field']]);
        }
    }
    if ($confirmation && empty($from)) {
        $e .= '<li>' . $ptx['error_missing_sender'] . '</li>' . PHP_EOL;
        return false;
    }
    $mail = new PHPMailer();
    $mail->LE = $pcf['mail_line_ending_*nix'] ? "\n" : "\r\n";
    $mail->set('CharSet', 'UTF-8');
    $mail->SetLanguage($sl, $pth['folder']['plugins'] . 'advancedform/phpmailer/language/');
    $mail->set('WordWrap', 72);
    if ($confirmation) {
        $mail->set('From', $form['to']);
        $mail->set('FromName', $form['to_name']);
        $mail->AddAddress($from, $from_name);
    } else {
        $mail->set('From', $from);
        $mail->set('FromName', $from_name);
        $mail->AddAddress($form['to'], $form['to_name']);
        foreach (explode(';', $form['cc']) as $cc) {
            if (trim($cc) != '') {
                $mail->AddCC($cc);
            }
        }
        foreach (explode(';', $form['bcc']) as $bcc) {
            if (trim($bcc) != '') {
                $mail->AddBCC($bcc);
            }
        }
    }
    if ($confirmation) {
        $mail->set('Subject', sprintf($ptx['mail_subject_confirmation'], $form['title'], $_SERVER['SERVER_NAME']));
    } else {
        $mail->set('Subject', sprintf($ptx['mail_subject'], $form['title'], $_SERVER['SERVER_NAME'], $_SERVER['REMOTE_ADDR']));
    }
    $mail->IsHtml($type != 'text');
    if ($type == 'text') {
        $mail->set('Body', Advancedform_mailBody($id, !$confirmation, false));
    } else {
        $body = Advancedform_mailBody($id, !$confirmation, true);
        $mail->MsgHTML($body);
        $mail->set('AltBody', Advancedform_mailBody($id, !$confirmation, false));
    }
    if (!$confirmation) {
        foreach ($form['fields'] as $field) {
            if ($field['type'] == 'file') {
                $name = 'advfrm-' . $field['field'];
                $mail->AddAttachment($_FILES[$name]['tmp_name'], stsl($_FILES[$name]['name']));
            }
        }
    }
    if (function_exists('advfrm_custom_mail')) {
        if (advfrm_custom_mail($id, $mail, $confirmation) === false) {
            return true;
        }
    }
    $ok = $mail->Send();
    if (!$confirmation) {
        if (!$ok) {
            $message = !empty($mail->ErrorInfo) ? Advancedform_hsc($mail->ErrorInfo) : $ptx['error_mail'];
            $e .= '<li>' . $message . '</li>' . PHP_EOL;
        }
        if (function_exists('XH_logMessage')) {
            $type = $ok ? 'info' : 'error';
            $message = $ok ? $ptx['log_success'] : $ptx['log_error'];
            $message = sprintf($message, $from);
            XH_logMessage($type, 'Advancedform', $id, $message);
        }
    }
    return $ok;
}
/**
 * Returns the form editor.
 *
 * @param string $id A form ID.
 *
 * @return string (X)HTML.
 *
 * @global array  The paths of system files and folders.
 * @global string The script name.
 * @global array  The configuration of the plugins.
 * @global array  The localization of the core.
 * @global array  The localization of the plugins.
 * @global string The (X)HTML fragment containing error messages.
 * @global object The CSRF protector.
 */
function Advancedform_editForm($id)
{
    global $pth, $sn, $plugin_cf, $tx, $plugin_tx, $e, $_XH_csrfProtection;
    $pcf = $plugin_cf['advancedform'];
    $ptx = $plugin_tx['advancedform'];
    $forms = Advancedform_db();
    $form = $forms[$id];
    if (!isset($form)) {
        $e .= '<li><b>' . sprintf($plugin_tx['advancedform']['error_form_missing'], $id) . '</b></li>';
        return Advancedform_formsAdministration();
    }
    /*
     * general settings
     */
    $o = '<div id="advfrm-editor">' . PHP_EOL . '<h1>' . $id . '</h1>' . PHP_EOL;
    $action = $sn . '?advancedform&amp;admin=plugin_main&amp;action=save&amp;form=' . $id;
    $o .= '<form action="' . $action . '" method="post" accept-charset="UTF-8"' . ' onsubmit="return advfrm_checkForm()">' . PHP_EOL;
    $o .= '<table id="advfrm-form">' . PHP_EOL;
    $fields = array('name', 'title', 'to_name', 'to', 'cc', 'bcc', 'captcha', 'store', 'form_class', 'thanks_page');
    foreach ($fields as $det) {
        $name = 'advfrm-' . $det;
        $o .= '<tr>' . '<td><label for="' . $name . '">' . $ptx['label_' . $det] . '</label></td>';
        switch ($det) {
            case 'captcha':
            case 'store':
                $checked = $form[$det] ? ' checked="checked"' : '';
                $o .= '<td>' . tag('input type="checkbox" id="' . $name . '" name="' . $name . '"' . $checked) . '</td>';
                break;
            case 'thanks_page':
                $o .= '<td>' . Advancedform_pageSelect($name, $form[$det]) . '</td>';
                break;
            default:
                $o .= '<td>' . tag('input type="text" id="' . $name . '" name="' . $name . '"' . ' value="' . Advancedform_hsc($form[$det]) . '" size="40"') . '</td>';
        }
        $o .= '</tr>' . PHP_EOL;
    }
    $o .= '</table>' . PHP_EOL;
    /*
     * field settings
     */
    $o .= '<div class="toolbar">';
    foreach (array('add', 'delete', 'up', 'down') as $tool) {
        $o .= '<a onclick="advfrm_' . $tool . '(\'advfrm-fields\')">' . Advancedform_toolIcon($tool) . '</a>' . PHP_EOL;
    }
    $o .= '</div>' . PHP_EOL;
    $o .= '<table id="advfrm-fields">' . PHP_EOL;
    $o .= '<thead><tr>' . '<th>' . $ptx['label_field'] . '</th>' . '<th>' . $ptx['label_label'] . '</th>' . '<th colspan="3">' . $ptx['label_type'] . '</th>' . '<th>' . $ptx['label_class'] . '</th>' . '<th>' . $ptx['label_required'] . '</th>' . '</tr></thead>' . PHP_EOL;
    foreach ($form['fields'] as $num => $field) {
        $o .= '<tr>' . '<td>' . tag('input type="text" size="10" name="advfrm-field[]"' . ' value="' . $field['field'] . '" class="highlightable"') . '</td>' . '<td>' . tag('input type="text" size="10" name="advfrm-label[]" value="' . Advancedform_hsc($field['label']) . '" class="highlightable"') . '</td>' . '<td><select name="advfrm-type[]" onfocus="this.oldvalue = this.value"' . ' class="highlightable">';
        $types = array('text', 'from_name', 'from', 'mail', 'date', 'number', 'textarea', 'radio', 'checkbox', 'select', 'multi_select', 'password', 'file', 'hidden', 'output', 'custom');
        foreach ($types as $type) {
            $sel = $field['type'] == $type ? ' selected="selected"' : '';
            $o .= '<option value="' . $type . '"' . $sel . '>' . $ptx['field_' . $type] . '</option>';
        }
        $o .= '</select></td>' . '<td>' . tag('input type="hidden" class="hidden" name="advfrm-props[]"' . ' value="' . Advancedform_hsc($field['props']) . '"') . '<td><a>' . Advancedform_toolIcon('props') . '</a>' . PHP_EOL;
        $checked = $field['required'] ? ' checked="checked"' : '';
        // Add Class
        $o .= '<td>' . tag('input type="text"  size="10" name="advfrm-class[]"' . ' value="' . Advancedform_hsc($field['class']) . '"') . '</td>' . PHP_EOL;
        $o .= '<td>' . tag('input type="checkbox"' . $checked . ' onchange="this.' . 'nextSibling.value = this.checked ? 1 : 0"') . tag('input type="hidden" name="advfrm-required[]" value="' . $field['required'] . '"') . '</td>' . '</tr>' . PHP_EOL;
    }
    $o .= '</table>' . PHP_EOL;
    $o .= tag('input type="submit" class="submit" value="' . ucfirst($tx['action']['save']) . '" style="display:none"');
    if (isset($_XH_csrfProtection)) {
        $o .= $_XH_csrfProtection->tokenInput();
    }
    $o .= '</form>' . PHP_EOL . '</div>' . PHP_EOL;
    /*
     * property dialogs
     */
    $o .= '<div id="advfrm-text-props" style="display:none">' . PHP_EOL . '<table>' . PHP_EOL;
    $properties = array('size', 'maxlength', 'default', 'constraint', 'error_msg');
    foreach ($properties as $prop) {
        $o .= '<tr id="advfrm-text-props-' . $prop . '"><td>' . $prop . '</td>' . '<td>' . tag('input type="text" size="30"') . '</td></tr>' . PHP_EOL;
    }
    $o .= '</table>' . PHP_EOL . '</div>' . PHP_EOL;
    $o .= '<div id="advfrm-select-props" style="display:none">' . PHP_EOL;
    $o .= '<p id="advfrm-select-props-size">' . $ptx['label_size'] . ' ' . tag('input type="text"') . '</p>' . PHP_EOL;
    $o .= '<p id="advfrm-select-props-orient">' . tag('input type="radio" id="advrm-select-props-orient-horz"' . ' name="advrm-select-props-orient"') . '<label for="advrm-select-props-orient-horz">&nbsp;' . $ptx['label_horizontal'] . '</label>&nbsp;&nbsp;&nbsp;' . tag('input type="radio" id="advrm-select-props-orient-vert"' . ' name="advrm-select-props-orient"') . '<label for="advrm-select-props-orient-vert">&nbsp;' . $ptx['label_vertical'] . '</label>' . '</p>' . PHP_EOL;
    $o .= '<div class="toolbar">';
    foreach (array('add', 'delete', 'up', 'down', 'clear_defaults') as $tool) {
        $o .= '<a onclick="advfrm_' . $tool . '(\'advfrm-prop-fields\')">' . Advancedform_toolIcon($tool) . '</a>' . PHP_EOL;
    }
    $o .= '</div>' . PHP_EOL;
    $o .= '<table id="advfrm-prop-fields">' . PHP_EOL . '<tr>' . '<td>' . tag('input type="radio" name="advfrm-select-props-default"') . '</td>' . '<td>' . tag('input type="text" name="advfrm-select-props-opt" size="25"' . ' class="highlightable"') . '</td>' . '</tr>' . PHP_EOL . '</table>' . PHP_EOL . '</div>' . PHP_EOL;
    return $o;
}