PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
8.0 KiB

6 years ago
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
14 years ago
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
14 years ago
5 years ago
  1. <?php
  2. /**
  3. * Postfix Admin
  4. *
  5. * LICENSE
  6. * This source file is subject to the GPL license that is bundled with
  7. * this package in the file LICENSE.TXT.
  8. *
  9. * Further details on the project are available at https://github.com/postfixadmin/postfixadmin
  10. *
  11. * @version $Id$
  12. * @license GNU GPL v2 or later.
  13. *
  14. * File: edit.php
  15. * This file implements the handling of edit forms.
  16. * The form layout is retrieved from the *Handler classes, which also do
  17. * the actual work of verifying and storing the values.
  18. *
  19. * GET parameters:
  20. * table what to edit (*Handler)
  21. * edit item to edit (if net given: a new item will be created)
  22. * additional parameters will be accepted if specified in *Handler->webformConfig()[prefill] when creating a new item
  23. */
  24. require_once('common.php');
  25. $smarty = PFASmarty::getInstance();
  26. $username = authentication_get_username(); # enforce login
  27. $table = safepost('table', safeget('table'));
  28. if (empty($table)) {
  29. die("Invalid table name given!");
  30. }
  31. $handlerclass = ucfirst($table) . 'Handler';
  32. if (!preg_match('/^[a-z]+$/', $table) || !file_exists(dirname(__FILE__) . "/../model/$handlerclass.php")) { # validate $table
  33. die("Invalid table name given!");
  34. }
  35. $error = 0;
  36. $values = [];
  37. $edit = safepost('edit', safeget('edit'));
  38. $new = 0;
  39. if ($edit == "") {
  40. $new = 1;
  41. }
  42. $is_admin = authentication_has_role('admin');
  43. $handler = new $handlerclass($new, $username, $is_admin);
  44. $formconf = $handler->webformConfig();
  45. if ($is_admin) {
  46. authentication_require_role($formconf['required_role']);
  47. } else {
  48. if (empty($formconf['user_hardcoded_field'])) {
  49. die($handlerclass . ' is not available for users');
  50. }
  51. }
  52. if ($new == 0 || $formconf['early_init']) {
  53. if (!$handler->init($edit)) {
  54. if (count($handler->errormsg) == 0) {
  55. # should never happen and indicates a bug in $handler->init()
  56. flash_error($handlerclass . "->init() failed, but didn't set any error message");
  57. }
  58. flash_error($handler->errormsg);
  59. header("Location: " . $formconf['listview']);
  60. exit;
  61. }
  62. }
  63. $form_fields = $handler->getStruct();
  64. $id_field = $handler->getId_field();
  65. if ($_SERVER['REQUEST_METHOD'] == "GET") {
  66. if ($new) { # new - prefill fields from URL parameters if allowed in $formconf['prefill']
  67. if (isset($formconf['prefill'])) {
  68. foreach ($formconf['prefill'] as $field) {
  69. $prefillvalue = safeget($field, safesession("prefill:$table:$field"));
  70. if ($prefillvalue != '') {
  71. $form_fields[$field]['default'] = $prefillvalue;
  72. $handler->prefill($field, $prefillvalue);
  73. }
  74. }
  75. }
  76. $form_fields = $handler->getStruct(); # refresh $form_fields - a prefill field might have changed something
  77. } else { # edit mode - read values from database
  78. if (!$handler->view()) {
  79. flash_error($handler->errormsg);
  80. header("Location: " . $formconf['listview']);
  81. exit;
  82. } else {
  83. $values = $handler->result;
  84. $values[$id_field] = $edit;
  85. }
  86. }
  87. }
  88. if ($_SERVER['REQUEST_METHOD'] == "POST") {
  89. if (safepost('token') != $_SESSION['PFA_token']) {
  90. die('Invalid token!');
  91. }
  92. $inp_values = [];
  93. if (isset($_POST['value']) && is_array($_POST['value'])) {
  94. $inp_values = $_POST['value'];
  95. }
  96. foreach ($form_fields as $key => $field) {
  97. if ($field['editable'] && $field['display_in_form']) {
  98. if (!isset($inp_values[$key])) {
  99. $inp_values[$key] = '';
  100. }
  101. if ($field['type'] == 'bool' && $inp_values[$key] == '') {
  102. $values[$key] = 0; # isset() for unchecked checkboxes is always false
  103. } elseif ($field['type'] == 'txtl') {
  104. $values[$key] = $inp_values[$key];
  105. $values[$key] = preg_replace('/\\\r\\\n/', ',', $values[$key]);
  106. $values[$key] = preg_replace('/\r\n/', ',', $values[$key]);
  107. $values[$key] = preg_replace('/,[\s]+/i', ',', $values[$key]);
  108. $values[$key] = preg_replace('/[\s]+,/i', ',', $values[$key]);
  109. $values[$key] = preg_replace('/,,*/', ',', $values[$key]);
  110. $values[$key] = preg_replace('/,*$|^,*/', '', $values[$key]);
  111. if ($values[$key] == '') {
  112. $values[$key] = array();
  113. } else {
  114. $values[$key] = explode(",", $values[$key]);
  115. }
  116. } else {
  117. $values[$key] = $inp_values[$key];
  118. }
  119. }
  120. }
  121. if (isset($formconf['hardcoded_edit']) && $formconf['hardcoded_edit']) {
  122. $values[$id_field] = $form_fields[$id_field]['default'];
  123. } elseif ($new == 0) {
  124. $values[$id_field] = $edit;
  125. }
  126. if ($new && ($form_fields[$id_field]['display_in_form'] == 0)) {
  127. if ($form_fields[$id_field]['editable'] == 1) { # address split to localpart and domain?
  128. $values[$id_field] = $handler->mergeId($values);
  129. } else { # probably auto_increment
  130. $values[$id_field] = '';
  131. }
  132. }
  133. if (!$handler->init($values[$id_field])) {
  134. $error = 1;
  135. $errormsg = $handler->errormsg;
  136. }
  137. if (!$handler->set($values)) {
  138. $error = 1;
  139. $errormsg = $handler->errormsg;
  140. }
  141. $form_fields = $handler->getStruct(); # refresh $form_fields - set() might have changed something
  142. if ($error != 1) {
  143. if (!$handler->save()) {
  144. $errormsg = $handler->errormsg;
  145. } else {
  146. flash_info($handler->infomsg);
  147. if (count($handler->errormsg)) { # might happen if domain_postcreation fails
  148. flash_error($handler->errormsg);
  149. }
  150. # remember prefill values for next usage of the form
  151. if (isset($formconf['prefill'])) {
  152. foreach ($formconf['prefill'] as $field) {
  153. if (isset($values[$field])) {
  154. $_SESSION["prefill:$table:$field"] = $values[$field];
  155. }
  156. }
  157. }
  158. if ($formconf['listview'] == 'list-virtual.php') {
  159. $bits = [];
  160. $bits['domain'] = $_SESSION['list-virtual:domain'] ?? null;
  161. $bits['limit'] = $_SESSION['list-virtual:limit'] ?? null;
  162. header("Location: " . $formconf['listview'] . '?' . http_build_query(array_filter($bits)));
  163. exit(0);
  164. }
  165. header("Location: " . $formconf['listview']);
  166. exit;
  167. }
  168. }
  169. }
  170. if ($error != 1 && $new) { # no error and not in edit mode - reset fields to default for new item
  171. $values = array();
  172. foreach (array_keys($form_fields) as $key) {
  173. $values[$key] = $form_fields[$key]['default'];
  174. }
  175. }
  176. $errormsg = $handler->errormsg;
  177. $fielderror = array();
  178. foreach ($form_fields as $key => $field) {
  179. if ($form_fields[$key]['display_in_form']) {
  180. if (isset($errormsg[$key])) {
  181. $fielderror[$key] = $errormsg[$key];
  182. unset($errormsg[$key]);
  183. } else {
  184. $fielderror[$key] = '';
  185. }
  186. if (isset($values[$key])) {
  187. $smarty->assign("value_$key", $values[$key]);
  188. } else {
  189. $smarty->assign("value_$key", $form_fields[$key]['default']);
  190. }
  191. }
  192. }
  193. if (count($errormsg)) {
  194. flash_error($errormsg);
  195. } # display the remaining error messages (not related to a field) with flash_error
  196. if ($new) {
  197. $smarty->assign('mode', 'create');
  198. $smarty->assign('formtitle', Config::lang($formconf['formtitle_create']));
  199. $smarty->assign('submitbutton', Config::lang($formconf['create_button']));
  200. } else {
  201. $smarty->assign('mode', 'edit');
  202. $smarty->assign('formtitle', Config::lang($formconf['formtitle_edit']));
  203. $smarty->assign('submitbutton', Config::lang('save'));
  204. }
  205. $smarty->assign('struct', $form_fields);
  206. $smarty->assign('fielderror', $fielderror);
  207. $smarty->assign('table', $table);
  208. $smarty->assign('smarty_template', 'editform');
  209. $smarty->display('index.tpl');
  210. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */