3 # This program is in the public domain within the United States. Additionally,
4 # we waive copyright and related rights in the work worldwide through the CC0
5 # 1.0 Universal public domain dedication, which can be found at
6 # http://creativecommons.org/publicdomain/zero/1.0/
9 # This file writes the code for you (sql, php, html, email) to handle a form.
11 require_once(__DIR__.'/'.'template.php');
12 require_once(__DIR__.'/'.'http.php');
13 require_once(__DIR__.'/'.'tar.php');
14 require_once(__DIR__.'/'.'format.php');
16 # see wfpl/metaform/template.html for the html templates for these elements
17 $GLOBALS['types'] = array(
18 # type input format sql
19 'varname' => array('textbox', 'varname', 'varchar(50) binary'),
20 'name' => array('textbox', 'oneline', 'varchar(200) binary'),
21 'textbox' => array('textbox', 'oneline', 'varchar(200) binary'),
22 'int' => array('textbox', 'int', 'int'),
23 'decimal' => array('textbox', 'decimal', 'decimal(12,2)'),
24 'bigint' => array('textbox', 'int', 'varchar(100) binary'), # up to 100 digits, stored as a string
25 'zip' => array('textbox', 'zip', 'varchar(20) binary'),
26 'email' => array('email', 'email', 'varchar(100) binary'),
27 'search' => array('search', 'oneline', 'varchar(200) binary'),
28 'phone' => array('textbox', 'phone', 'varchar(32) binary'),
29 'state' => array('states', 'oneline', 'varchar(2) binary'),
30 'money' => array('textbox', 'money', 'varchar(32) binary'),
31 'date' => array('date', 'mdy_to_ymd', 'char(10) binary'),
32 'dollars' => array('textbox', 'dollars', 'varchar(32) binary'),
33 'url' => array('textbox', 'url', 'varchar(200) binary'),
34 'hidden' => array('hidden', 'unix', 'varchar(200) binary'),
35 'password' => array('password', 'oneline', 'varchar(200) binary'),
36 'textarea' => array('textarea', 'unix', 'text binary'),
37 'html' => array('html', 'unix', 'mediumtext binary'),
38 'pulldown' => array('pulldown', 'options', 'varchar(100) binary'),
39 'radio' => array('radio', 'options', 'varchar(100) binary'),
40 'checkbox' => array('checkbox', 'bool', 'int(1)'),
41 'yesno' => array('checkbox', 'yesno', 'varchar(3) binary'),
42 'delete' => array('checkbox', 'yesno', 'n/a'),
43 'image' => array('image', 'oneline', 'varchar(120) binary'),
44 'thumb' => array('image', 'oneline', 'varchar(240) binary'),
45 'file' => array('file', 'oneline', 'varchar(100) binary'),
46 'submit' => array('submit', 'n/a', 'n/a'),
47 '{' => array('fieldset', 'n/a', 'n/a'),
48 '}' => array('end_fieldset','n/a', 'n/a')
51 function list_available_types() {
52 ksort($GLOBALS['types']);
53 foreach($GLOBALS['types'] as $key => $value) {
54 tem_set('type', $key);
56 tem_show('types_sep');
61 function tem_set_globals(&$tem) {
68 foreach($vars as $var) {
69 $tem->set($var, $GLOBALS[$var]);
80 'opt_public_something');
81 foreach($bools as $bool) {
88 function metaform_main() {
89 if(isset($_REQUEST['singular'])) {
90 $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']);
91 $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']);
92 $GLOBALS['plural'] = format_oneline($_REQUEST['plural']);
93 # backwards compatibility:
94 if(isset($_REQUEST['form_name'])) {
95 $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']);
98 $GLOBALS['singular'] = format_oneline($_REQUEST['singular']);
99 $GLOBALS['opt_email'] = format_bool($_REQUEST['opt_email']);
100 $GLOBALS['opt_db'] = format_bool($_REQUEST['opt_db']);
101 $GLOBALS['opt_listing'] = format_bool($_REQUEST['opt_listing']);
102 $GLOBALS['opt_display'] = format_bool($_REQUEST['opt_display']);
103 $GLOBALS['opt_pass'] = format_bool($_REQUEST['opt_pass']);
104 $GLOBALS['opt_public_form'] = format_bool($_REQUEST['opt_public_form']);
105 $GLOBALS['opt_public_display'] = format_bool($_REQUEST['opt_public_display']);
107 $GLOBALS['public_file_name'] = $GLOBALS['file_name'];
108 if($GLOBALS['opt_public_form'] || $GLOBALS['opt_public_display']) {
109 $GLOBALS['opt_public_something'] = 1;
110 $GLOBALS['file_name'] = $GLOBALS['file_name'] . _admin;
114 tem_set_globals($GLOBALS['wfpl_template']);
117 if(isset($_REQUEST['fields'])) {
118 if(isset($_REQUEST['view_sql'])) {
121 } elseif(isset($_REQUEST['view_php'])) {
124 } elseif(isset($_REQUEST['view_html'])) {
127 } elseif(isset($_REQUEST['view_email'])) {
130 } elseif(isset($_REQUEST['download_tar'])) {
133 } elseif(isset($_REQUEST['preview'])) {
136 } elseif(isset($_REQUEST['edit'])) {
137 tem_set('fields', $_REQUEST['fields']);
140 die("Sorry... couldn't tell which button you pressed");
146 tem_load(__DIR__.'/'.'metaform/main.html');
147 list_available_types();
149 exit(); # in case we're being called by wfpl_main or something
153 function field_input($type) { return $GLOBALS['types'][$type][0]; }
154 function field_format($type) { return $GLOBALS['types'][$type][1]; }
155 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
157 function get_fields() {
158 # no sense in doing all this so many times
159 if(isset($GLOBALS['gotten_fields'])) {
160 return $GLOBALS['gotten_fields'];
163 $fields_str = unix_newlines($_REQUEST['fields']);
164 $GLOBALS['gotten_fields'] = array();
165 $fields_str = rtrim($fields_str);
166 $fields = split("\n", $fields_str);
167 foreach($fields as $field) {
168 $first_char = substr($field, 0, 1);
169 $field = trim($field);
170 if ($first_char === ' ' || $first_char === "\t") {
171 $i = count($GLOBALS['gotten_fields']);
174 if (!$GLOBALS['gotten_fields'][$i]['options']) {
175 $GLOBALS['gotten_fields'][$i]['options'] = array();
177 $GLOBALS['gotten_fields'][$i]['options'][] = $field;
181 if(substr($field, -1) == '{') {
182 $caption = trim(substr($field, 0, -1));
183 $name = format_varname($caption);
189 $div = strpos($field, ' ');
190 if ($div === false) {
191 $div = strpos($field, "\t");
193 if ($div === false) {
194 if (isset($GLOBALS['types'][$field])) {
195 # if just one word, and it's a type, use it as name/caption and type
199 $first_word = trim(substr($field, 0, $div));
200 if (isset($GLOBALS['types'][$first_word])) {
201 # if the first word (of multiple) is a type, remove it from the name/caption
203 $field = trim(substr($field, $div + 1));
206 # see if there's an explicit name/caption splitter
207 $div = strpos($field, '|');
208 if ($div !== false) {
209 $name = trim(substr($field, 0, $div));
210 $caption = trim(substr($field, $div + 1));
211 if (isset($GLOBALS['types'][$name])) {
213 } elseif (isset($GLOBALS['types'][strtolower($caption)])) {
214 $type = strtolower($caption);
217 $name = format_varname($field);
218 $caption = format_caption($field);
221 if ($type === null) {
225 $input = field_input($type);
226 $format = field_format($type);
227 $sql = field_sql($type);
228 $GLOBALS['gotten_fields'][] = array(
229 'caption' => $caption,
235 'options' => $options
239 return $GLOBALS['gotten_fields'];
242 # this one, that you're using to create forms
243 function set_form_action() {
244 $action = preg_replace('|.*/|', '', $_SERVER['REQUEST_URI']);
245 if($action == '') $action = './';
246 tem_set('form_action', $action);
249 # perfect HTTP headers for viewing created files
250 function view_headers() {
251 header('Content-type: text/plain');
257 function make_sql() {
259 $tem->load(__DIR__.'/'.'metaform/template.sql');
260 tem_set_globals($tem);
261 $fields = get_fields();
262 foreach($fields as $field) {
263 if($field['sql'] != 'n/a') {
264 $tem->set('name', $field['name']);
265 $tem->set('type', $field['sql']);
266 if(substr($field['sql'], 0, 3) == 'int' || substr($field['sql'], 0, 7) == 'decimal') {
267 $tem->set('default', '0');
268 } elseif($field['format'] == 'yesno') {
269 $tem->set('default', '"No"');
271 $tem->set('default', '""');
273 $tem->show('column');
280 function view_sql() {
285 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
286 function find_always_field(&$fields) {
287 foreach($fields as $field) {
288 if($field['input'] != 'submit' && $field['input'] != 'image' && $field['input'] != 'file' && $field['input'] != 'checkbox' && $field['input'] != 'radio' && $field['type'] != '{' && $field['type'] != '}') {
289 return $field['name'];
298 # pass false if you want to exclude the <head> and <body> tag etc.
299 function make_html($whole_file = true) {
300 $has_html_editors = false;
302 $tem->load(__DIR__.'/'.'metaform/template.html');
303 tem_set_globals($tem);
304 $fields = get_fields();
305 $tem->set('always_field', find_always_field($fields));
306 $hidden_fields = array();
307 $visible_fields = array();
308 $display_fields = array();
309 $listing_headers = array();
310 $listing_fields = array();
311 foreach($fields as $field) {
312 if($field['input'] == 'hidden') {
313 $hidden_fields[] = array('name' => $field['name']);
315 $visible_fields[] = array(
316 $field['input'] => array(
317 'name' => $field['name'],
318 'caption' => $field['caption']
321 if($field['type'] == 'radio') {
324 foreach ($field['options'] as $row) {
325 if (is_array($row)) {
330 $opts[] = array('i' => $i, 'option_caption' => $cap);
333 $visible_fields[count($visible_fields) - 1]['options'] = $opts;
337 if($field['input'] == 'image' || $field['input'] == 'file') {
338 $tem->set('uploads');
339 $tem->set('enctype_attr', '" enctype="multipart/form-data');
340 } elseif($field['input'] == 'html') {
341 $has_html_editors = true;
342 $tem->set('html_field_name', $field['name']);
343 $tem->set('replace_textarea');
346 switch($field['input']) {
352 $display_type = $field['input'];
355 $display_type = 'short';
357 if($field['format'] != 'n/a') {
358 $display_fields[] = array($display_type => array(
359 'name' => $field['name'], 'caption' => $field['caption']));
362 if(show_in_listing($field['type'], $field['input'], $field['format'], $field['sql'])) {
363 $listing_headers[] = array(
364 'caption' => $field['caption'],
365 'name' => $field['name']
367 $listing_field = array(
368 'name' => $field['name']
370 if($field['format'] == 'bool' || $field['format'] == 'yesno') {
371 $listing_field['enc'] = 'yesno';
372 } elseif($field['input'] == 'date') {
373 $listing_field['enc'] = 'mmddyyyy';
374 } elseif($field['type'] == 'thumb') {
375 $listing_field['thumb'] = true;
377 $listing_field['enc'] = 'html';
379 $listing_fields[] = $listing_field;
384 if($GLOBALS['opt_email'] == 'Yes' && !$GLOBALS['opt_db']) {
385 $visible_fields[] = array('submit' => array(
387 'caption' => 'Send'));
389 $visible_fields[] = array('submit' => array(
391 'caption' => 'Save'));
394 $form_fields = array();
395 $form_fields['visible_fields'] = $visible_fields;
397 $form_fields['hidden_fields'] = $hidden_fields;
399 $tem->set('form', $form_fields);
401 # opt_display and opt_listing control whether these are actually displayed
402 $tem->set('display_fields', $display_fields);
403 $tem->set('listing_headers', $listing_headers);
404 $tem->set('listing_fields', $listing_fields);
407 if($has_html_editors) {
408 $tem->set('html_editor_headers');
415 $tem2->load_str('<!--~form~-->');
421 function view_html() {
426 function show_in_listing($type, $input, $format, $sql) {
437 if($type == 'image') {
444 function pulldown_options_array($options) {
446 $pulldown_options = array();
447 foreach($options as $option) {
448 $option = preg_replace("/['\\\\]/", '\\\\$0', $option);
449 $pulldown_options[] = "'$option'";
451 $pulldown_options = 'array(' . join(', ', $pulldown_options) . ')';
453 $pulldown_options = "array(array('op1', 'Option One'), array('op2', 'Option Two'), 'n/a')";
456 return $pulldown_options;
459 function make_php() {
460 $has_html_editors = false;
462 $tem->load(__DIR__.'/'.'metaform/template.php');
463 tem_set_globals($tem);
464 $fields = get_fields();
466 $always_field = find_always_field($fields);
467 $image_included_yet = false;
468 $name_to_caption = array();
469 foreach($fields as $field) {
470 $name_to_caption[] = array('name' => $field['name'], 'caption' => $field['caption']);
471 if($field['input'] != 'submit') {
472 $tem->set('format', $field['format']);
473 $tem->set('name', $field['name']);
474 $tem->set('db_field', ''); # we don't want to use the value from last time
475 if($field['sql'] != 'n/a') {
476 if($db_fields != '') $db_fields .= ',';
477 $db_fields .= $field['name'];
479 if($field['input'] == 'image') {
480 if($field['type'] == 'thumb') {
481 $tem->show('thumb_settings');
482 $tem->show('thumb_upload_params');
483 $tem->show('thumb_w_h');
485 $tem->show('image_settings');
486 $tem->show('image_upload');
488 } else if($field['input'] == 'file') {
489 $tem->show('file_settings');
490 $tem->show('file_upload');
493 if($field['input'] == 'html') {
494 $has_html_editors = true;
495 } elseif($field['input'] == 'pulldown' || $field['input'] == 'radio') {
496 $pulldown_options = pulldown_options_array($field['options']);
497 $tem->set('pulldown_options', $pulldown_options);
498 $tem->set('has_pulldowns');
499 $tem->show('pulldowns');
500 $tem->show('pulldown_format_extra');
502 if($field['format'] != 'n/a') {
503 $tem->show('formats');
508 if($GLOBALS['opt_listing']) {
509 if(show_in_listing($field['type'], $field['input'], $field['format'], $field['sql'])) {
510 $tem->show('listing_fields_1');
511 $tem->show('listing_fields_2');
516 $tem->set('name_to_caption', $name_to_caption);
519 $tem->show('uploads_include');
520 $tem->show('upload_max');
521 $tem->show('upload_settings');
522 $image_included_yet = true;
525 if($has_html_editors) {
526 $tem->show('show_extra_headers');
529 $tem->set('always_field', $always_field);
530 $tem->set('db_fields', $db_fields);
531 $tem->set('metaform_url', edit_url());
532 if($GLOBALS['opt_email']) {
533 $this_domain = $_SERVER['HTTP_HOST'];
534 if(substr($this_domain, -2) == '.l') {
535 $this_domain = substr($this_domain, 0, -1) . 'com';
537 $tem->set('this_domain', $this_domain);
542 # make a URL for the edit page with all the fields filled in
543 function edit_url() {
545 $url = preg_replace('|view_php=[^&]*|', 'edit=yes', $url);
546 $url = preg_replace('|download_tar=[^&]*|', 'edit=yes', $url);
547 $url = preg_replace('|/[a-z0-9_.]*\?|', '/?', $url);
551 function view_php() {
556 function make_email() {
558 $tem->load(__DIR__.'/'.'metaform/template.email.txt');
559 tem_set_globals($tem);
560 $fields = get_fields();
561 foreach($fields as $field) {
562 $tem->set('name', $field['name']);
563 $tem->set('caption', $field['caption']);
564 if($field['type'] == 'textarea') {
565 $tem->show('multi_line');
566 } elseif($field['type'] == 'checkbox') {
567 $tem->show('checkbox');
569 $tem->show('normal');
571 $tem->show('fields');
576 function make_htaccess() {
578 $tem->set('form', $GLOBALS['file_name']);
579 return $tem->run(__DIR__.'/'.'metaform/htaccess');
582 function view_email() {
588 tem_load(__DIR__.'/'.'metaform/preview.html');
589 tem_set_globals($GLOBALS['wfpl_template']);
590 tem_set('fields', $_REQUEST['fields']);
591 $preview_tem = new tem();
592 $preview_tem->load_str(make_html(false));
593 if($GLOBALS['opt_db']) {
594 $preview_tem->show('new_msg');
596 $fields = get_fields();
597 foreach($fields as $field) {
598 if($field['type'] == 'pulldown' || $field['type'] == 'radio') {
599 pulldown($field['name'], eval('return ' . pulldown_options_array($field['options']) . ';'));
602 $preview = $preview_tem->run();
604 $preview = preg_replace('|type="submit"|', 'type="submit" disabled="disabled"', $preview);
605 tem_set('preview', $preview);
611 function download_tar() {
612 $admin_name = $GLOBALS['file_name'];
613 $nice_name = $GLOBALS['public_file_name'];
615 "README" => "These files are meant to work with wfpl.\n\nSee: http://sametwice.com/wfpl\n",
616 ".htaccess" => make_htaccess(),
617 "config.php" => "<?php\n\n# put your site-wide configuration here\n",
618 "wfpl_main.php" => read_whole_file(__DIR__.'/'.'metaform/wfpl_main.php'),
619 "styl.styl" => read_whole_file(__DIR__.'/'.'metaform/styl.styl'),
620 "template.html" => read_whole_file(__DIR__.'/'.'metaform/site-template.html'),
621 "$admin_name.html" => make_html(),
622 "$admin_name.php" => make_php());
623 if($GLOBALS['opt_public_something']) {
624 $files["$nice_name.html ->"] = "$admin_name.html";
625 $files["$nice_name.php ->"] = "$admin_name.php";
627 if($GLOBALS['opt_db']) {
628 $files["$nice_name.sql"] = make_sql();
630 if($GLOBALS['opt_email']) {
631 $files["$nice_name.email.txt"] = make_email();
633 make_tar($nice_name, $files);