0
0
mirror of https://github.com/postfixadmin/postfixadmin.git synced 2024-09-20 03:36:20 +02:00
postfixadmin/public/edit.php

249 lines
8.0 KiB
PHP
Raw Normal View History

<?php
2018-01-26 15:45:57 +01:00
/**
* Postfix Admin
*
* LICENSE
* This source file is subject to the GPL license that is bundled with
* this package in the file LICENSE.TXT.
*
* Further details on the project are available at http://postfixadmin.sf.net
*
* @version $Id$
* @license GNU GPL v2 or later.
*
* File: edit.php
* This file implements the handling of edit forms.
* The form layout is retrieved from the *Handler classes, which also do
* the actual work of verifying and storing the values.
*
* GET parameters:
* table what to edit (*Handler)
* edit item to edit (if net given: a new item will be created)
* additional parameters will be accepted if specified in *Handler->webformConfig()[prefill] when creating a new item
*/
require_once('common.php');
$smarty = PFASmarty::getInstance();
$username = authentication_get_username(); # enforce login
$table = safepost('table', safeget('table'));
if (empty($table)) {
2019-10-17 21:03:47 +02:00
die("Invalid table name given!");
}
$handlerclass = ucfirst($table) . 'Handler';
2018-01-26 15:45:57 +01:00
if (!preg_match('/^[a-z]+$/', $table) || !file_exists(dirname(__FILE__) . "/../model/$handlerclass.php")) { # validate $table
die("Invalid table name given!");
}
$error = 0;
2020-03-12 21:45:51 +01:00
$values = [];
$edit = safepost('edit', safeget('edit'));
2019-06-08 21:05:59 +02:00
$new = 0;
2018-01-26 15:45:57 +01:00
if ($edit == "") {
$new = 1;
}
$is_admin = authentication_has_role('admin');
2019-06-08 21:05:59 +02:00
$handler = new $handlerclass($new, $username, $is_admin);
$formconf = $handler->webformConfig();
if ($is_admin) {
authentication_require_role($formconf['required_role']);
} else {
if (empty($formconf['user_hardcoded_field'])) {
die($handlerclass . ' is not available for users');
}
}
if ($new == 0 || $formconf['early_init']) {
if (!$handler->init($edit)) {
if (count($handler->errormsg) == 0) {
# should never happen and indicates a bug in $handler->init()
flash_error($handlerclass . "->init() failed, but didn't set any error message");
}
flash_error($handler->errormsg);
2018-01-26 15:45:57 +01:00
header("Location: " . $formconf['listview']);
exit;
}
}
$form_fields = $handler->getStruct();
2019-06-08 21:05:59 +02:00
$id_field = $handler->getId_field();
if ($_SERVER['REQUEST_METHOD'] == "GET") {
if ($new) { # new - prefill fields from URL parameters if allowed in $formconf['prefill']
2018-01-26 15:45:57 +01:00
if (isset($formconf['prefill'])) {
foreach ($formconf['prefill'] as $field) {
$prefillvalue = safeget($field, safesession("prefill:$table:$field"));
if ($prefillvalue != '') {
$form_fields[$field]['default'] = $prefillvalue;
$handler->prefill($field, $prefillvalue);
}
}
}
$form_fields = $handler->getStruct(); # refresh $form_fields - a prefill field might have changed something
} else { # edit mode - read values from database
if (!$handler->view()) {
flash_error($handler->errormsg);
2018-01-26 15:45:57 +01:00
header("Location: " . $formconf['listview']);
exit;
} else {
$values = $handler->result;
$values[$id_field] = $edit;
}
}
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
2018-01-26 15:45:57 +01:00
if (safepost('token') != $_SESSION['PFA_token']) {
die('Invalid token!');
}
$inp_values = [];
if (isset($_POST['value']) && is_array($_POST['value'])) {
$inp_values = $_POST['value'];
}
2018-01-26 15:45:57 +01:00
foreach ($form_fields as $key => $field) {
if ($field['editable'] && $field['display_in_form']) {
if (!isset($inp_values[$key])) {
2019-06-08 21:05:59 +02:00
$inp_values[$key] = '';
}
2018-01-26 15:45:57 +01:00
if ($field['type'] == 'bool' && $inp_values[$key] == '') {
$values[$key] = 0; # isset() for unchecked checkboxes is always false
2018-01-26 15:45:57 +01:00
} elseif ($field['type'] == 'txtl') {
$values[$key] = $inp_values[$key];
2018-01-26 15:45:57 +01:00
$values[$key] = preg_replace('/\\\r\\\n/', ',', $values[$key]);
2019-06-08 21:05:59 +02:00
$values[$key] = preg_replace('/\r\n/', ',', $values[$key]);
$values[$key] = preg_replace('/,[\s]+/i', ',', $values[$key]);
$values[$key] = preg_replace('/[\s]+,/i', ',', $values[$key]);
$values[$key] = preg_replace('/,,*/', ',', $values[$key]);
$values[$key] = preg_replace('/,*$|^,*/', '', $values[$key]);
AliasHandler now works with edit.php in many cases (TODO: catchall handling, mailbox and vacation aliases) AliasHandler.php - drop unused $username - set $domain_field - initStruct(): - use correct labels - set 'domain' field options to allowed domains - add (virtual) 'localpart' field - add comments for more virtual fields - add webformConfig() (note: modifies $struct on $new - otherwise we couldn't use the domain dropdown in the web interface) - add mergeId to merge localpart and domain to address (called by edit.php _before_ ->init) - add validate_new_id() (doesn't work for catchall yet) - add setmore() to - fill 'domain' based on 'address' - convert $values[goto] from array to comma-separated string - add read_from_db_postprocess to split goto to an array (TODO: handling of mailbox and vacation aliases) - add _field_goto() validator - add empty, commented dummy delete() that will replace the "old" delete function one day - make hasAliasRecord() private (only used internally) - mark all "old" functions as obsolete edit.php: - add handling of txtl field (convert textarea to array) - call $handler->mergeId if $id_field is editable, but not displayed in form (usecase: merge localpart + domain to address) editform.tpl: - add handling of txtl fields (textarea, filled by array) PFAHandler.php: - add setmore() hook function - runs at the end of set() AdminHandler.php: - add a comment for 'txtl' (array of one line texts, like alias goto) git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1311 a1433add-5e2c-0410-b055-b7f2511e0802
2011-12-27 19:13:32 +01:00
if ($values[$key] == '') {
$values[$key] = array();
} else {
$values[$key] = explode(",", $values[$key]);
}
} else {
$values[$key] = $inp_values[$key];
}
}
}
if (isset($formconf['hardcoded_edit']) && $formconf['hardcoded_edit']) {
$values[$id_field] = $form_fields[$id_field]['default'];
} elseif ($new == 0) {
$values[$id_field] = $edit;
}
if ($new && ($form_fields[$id_field]['display_in_form'] == 0)) {
if ($form_fields[$id_field]['editable'] == 1) { # address split to localpart and domain?
$values[$id_field] = $handler->mergeId($values);
} else { # probably auto_increment
$values[$id_field] = '';
}
AliasHandler now works with edit.php in many cases (TODO: catchall handling, mailbox and vacation aliases) AliasHandler.php - drop unused $username - set $domain_field - initStruct(): - use correct labels - set 'domain' field options to allowed domains - add (virtual) 'localpart' field - add comments for more virtual fields - add webformConfig() (note: modifies $struct on $new - otherwise we couldn't use the domain dropdown in the web interface) - add mergeId to merge localpart and domain to address (called by edit.php _before_ ->init) - add validate_new_id() (doesn't work for catchall yet) - add setmore() to - fill 'domain' based on 'address' - convert $values[goto] from array to comma-separated string - add read_from_db_postprocess to split goto to an array (TODO: handling of mailbox and vacation aliases) - add _field_goto() validator - add empty, commented dummy delete() that will replace the "old" delete function one day - make hasAliasRecord() private (only used internally) - mark all "old" functions as obsolete edit.php: - add handling of txtl field (convert textarea to array) - call $handler->mergeId if $id_field is editable, but not displayed in form (usecase: merge localpart + domain to address) editform.tpl: - add handling of txtl fields (textarea, filled by array) PFAHandler.php: - add setmore() hook function - runs at the end of set() AdminHandler.php: - add a comment for 'txtl' (array of one line texts, like alias goto) git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1311 a1433add-5e2c-0410-b055-b7f2511e0802
2011-12-27 19:13:32 +01:00
}
if (!$handler->init($values[$id_field])) {
$error = 1;
$errormsg = $handler->errormsg;
}
if (!$handler->set($values)) {
$error = 1;
$errormsg = $handler->errormsg;
}
$form_fields = $handler->getStruct(); # refresh $form_fields - set() might have changed something
if ($error != 1) {
2020-09-25 22:43:34 +02:00
if (!$handler->save()) {
$errormsg = $handler->errormsg;
} else {
flash_info($handler->infomsg);
if (count($handler->errormsg)) { # might happen if domain_postcreation fails
flash_error($handler->errormsg);
}
# remember prefill values for next usage of the form
2018-01-26 15:45:57 +01:00
if (isset($formconf['prefill'])) {
foreach ($formconf['prefill'] as $field) {
if (isset($values[$field])) {
$_SESSION["prefill:$table:$field"] = $values[$field];
}
}
}
if ($formconf['listview'] == 'list-virtual.php') {
$bits = [];
$bits['domain'] = $_SESSION['list-virtual:domain'] ?? null;
$bits['limit'] = $_SESSION['list-virtual:limit'] ?? null;
header("Location: " . $formconf['listview'] . '?' . http_build_query(array_filter($bits)));
exit(0);
}
2021-07-05 20:39:51 +02:00
header("Location: " . $formconf['listview']);
exit;
}
}
}
if ($error != 1 && $new) { # no error and not in edit mode - reset fields to default for new item
$values = array();
foreach (array_keys($form_fields) as $key) {
$values[$key] = $form_fields[$key]['default'];
}
}
$errormsg = $handler->errormsg;
$fielderror = array();
2018-01-26 15:45:57 +01:00
foreach ($form_fields as $key => $field) {
if ($form_fields[$key]['display_in_form']) {
if (isset($errormsg[$key])) {
$fielderror[$key] = $errormsg[$key];
unset($errormsg[$key]);
} else {
$fielderror[$key] = '';
}
2018-01-26 15:45:57 +01:00
if (isset($values[$key])) {
$smarty->assign("value_$key", $values[$key]);
} else {
$smarty->assign("value_$key", $form_fields[$key]['default']);
}
}
}
2018-01-26 15:45:57 +01:00
if (count($errormsg)) {
flash_error($errormsg);
} # display the remaining error messages (not related to a field) with flash_error
if ($new) {
2018-01-26 15:45:57 +01:00
$smarty->assign('mode', 'create');
$smarty->assign('formtitle', Config::lang($formconf['formtitle_create']));
$smarty->assign('submitbutton', Config::lang($formconf['create_button']));
} else {
2018-01-26 15:45:57 +01:00
$smarty->assign('mode', 'edit');
$smarty->assign('formtitle', Config::lang($formconf['formtitle_edit']));
$smarty->assign('submitbutton', Config::lang('save'));
}
2018-01-26 15:45:57 +01:00
$smarty->assign('struct', $form_fields);
$smarty->assign('fielderror', $fielderror);
$smarty->assign('table', $table);
$smarty->assign('smarty_template', 'editform');
$smarty->display('index.tpl');
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */