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.

196 lines
6.4 KiB

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
  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 http://postfixadmin.sf.net
  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. $username = authentication_get_username(); # enforce login
  26. $table = safepost('table', safeget('table'));
  27. $handlerclass = ucfirst($table) . 'Handler';
  28. if ( !preg_match('/^[a-z]+$/', $table) || !file_exists("model/$handlerclass.php")) { # validate $table
  29. die ("Invalid table name given!");
  30. }
  31. $error = 0;
  32. $edit = safepost('edit', safeget('edit'));
  33. $new = 0;
  34. if ($edit == "") $new = 1;
  35. $handler = new $handlerclass($new, $username);
  36. $formconf = $handler->webformConfig();
  37. authentication_require_role($formconf['required_role']);
  38. if ($edit != '' || $formconf['early_init']) {
  39. if (!$handler->init($edit)) {
  40. flash_error($handler->errormsg);
  41. header ("Location: " . $formconf['listview']);
  42. exit;
  43. }
  44. }
  45. $form_fields = $handler->getStruct();
  46. $id_field = $handler->getId_field();
  47. if ($_SERVER['REQUEST_METHOD'] == "GET") {
  48. if ($edit == '') { # new - prefill fields from URL parameters if allowed in $formconf['prefill']
  49. if ( isset($formconf['prefill']) ) {
  50. foreach ($formconf['prefill'] as $field) {
  51. if (isset ($_GET[$field])) {
  52. $form_fields[$field]['default'] = safeget($field);
  53. $handler->prefill($field, safeget($field));
  54. }
  55. }
  56. }
  57. $form_fields = $handler->getStruct(); # refresh $form_fields - a prefill field might have changed something
  58. } else { # edit mode - read values from database
  59. if (!$handler->view()) {
  60. flash_error($handler->errormsg);
  61. header ("Location: " . $formconf['listview']);
  62. exit;
  63. } else {
  64. $values = $handler->result;
  65. $values[$id_field] = $edit;
  66. }
  67. }
  68. }
  69. if ($_SERVER['REQUEST_METHOD'] == "POST") {
  70. foreach($form_fields as $key => $field) {
  71. if ($field['editable'] && $field['display_in_form']) {
  72. if($field['type'] == 'bool') {
  73. $values[$key] = safepost($key, 0); # isset() for unchecked checkboxes is always false
  74. } elseif($field['type'] == 'txtl') {
  75. $values[$key] = safepost($key);
  76. $values[$key] = preg_replace ('/\\\r\\\n/', ',', $values[$key]);
  77. $values[$key] = preg_replace ('/\r\n/', ',', $values[$key]);
  78. $values[$key] = preg_replace ('/,[\s]+/i', ',', $values[$key]);
  79. $values[$key] = preg_replace ('/[\s]+,/i', ',', $values[$key]);
  80. $values[$key] = preg_replace ('/,,*/', ',', $values[$key]);
  81. $values[$key] = preg_replace ('/,*$|^,*/', '', $values[$key]);
  82. if ($values[$key] == '') {
  83. $values[$key] = array();
  84. } else {
  85. $values[$key] = explode(",", $values[$key]);
  86. }
  87. } else {
  88. $values[$key] = safepost($key);
  89. }
  90. }
  91. }
  92. }
  93. if ($_SERVER['REQUEST_METHOD'] == "POST") {
  94. if (isset($formconf['hardcoded_edit']) && $formconf['hardcoded_edit']) {
  95. $values[$id_field] = $form_fields[$id_field]['default'];
  96. } elseif ($edit != "") {
  97. $values[$id_field] = $edit;
  98. }
  99. if ($new && ($form_fields[$id_field]['display_in_form'] == 0) && ($form_fields[$id_field]['editable'] == 1) ) { # address split to localpart and domain?
  100. $values[$id_field] = $handler->mergeId($values);
  101. }
  102. if (!$handler->init($values[$id_field])) {
  103. $error = 1;
  104. $errormsg = $handler->errormsg;
  105. }
  106. if (!$handler->set($values)) {
  107. $error = 1;
  108. $errormsg = $handler->errormsg;
  109. }
  110. $form_fields = $handler->getStruct(); # refresh $form_fields - set() might have changed something
  111. if ($error != 1) {
  112. if (!$handler->store()) {
  113. $errormsg = $handler->errormsg;
  114. } else {
  115. flash_info($handler->infomsg);
  116. if (count($handler->errormsg)) { # might happen if domain_postcreation fails
  117. flash_error($handler->errormsg);
  118. }
  119. if ($edit != "") {
  120. header ("Location: " . $formconf['listview']);
  121. exit;
  122. } else {
  123. header("Location: edit.php?table=$table"); # TODO: hand over last used domain etc. ($formconf['prefill'] ?)
  124. exit;
  125. }
  126. }
  127. }
  128. }
  129. if ($error != 1 && $new) { # no error and not in edit mode - reset fields to default for new item
  130. $values = array();
  131. foreach (array_keys($form_fields) as $key) {
  132. $values[$key] = $form_fields[$key]['default'];
  133. }
  134. }
  135. $errormsg = $handler->errormsg;
  136. $fielderror = array();
  137. foreach($form_fields as $key => $field) {
  138. if($form_fields[$key]['display_in_form']) {
  139. if (isset($errormsg[$key])) {
  140. $fielderror[$key] = $errormsg[$key];
  141. unset ($errormsg[$key]);
  142. } else {
  143. $fielderror[$key] = '';
  144. }
  145. $smarty->assign ("value_$key", $values[$key]);
  146. }
  147. }
  148. if (count($errormsg)) flash_error($errormsg); # display the remaining error messages (not related to a field) with flash_error
  149. if ($new) {
  150. $smarty->assign ('mode', 'create');
  151. $smarty->assign('formtitle', Config::lang($formconf['formtitle_create']));
  152. $smarty->assign('submitbutton', Config::lang($formconf['create_button']));
  153. } else {
  154. $smarty->assign ('mode', 'edit');
  155. $smarty->assign('formtitle', Config::lang($formconf['formtitle_edit']));
  156. $smarty->assign('submitbutton', Config::lang('save'));
  157. }
  158. $smarty->assign ('struct', $form_fields);
  159. $smarty->assign ('fielderror', $fielderror);
  160. $smarty->assign ('table', $table);
  161. $smarty->assign ('smarty_template', 'editform');
  162. $smarty->display ('index.tpl');
  163. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
  164. ?>