JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
upload.php cleanup: really don't make dot files
[wfpl.git] / metaform.php
1 <?php
2
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/
7
8
9 # This file writes the code for you (sql, php, html, email) to handle a form.
10
11 require_once(__DIR__.'/'.'template.php');
12 require_once(__DIR__.'/'.'http.php');
13 require_once(__DIR__.'/'.'tar.php');
14 require_once(__DIR__.'/'.'format.php');
15
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')
49 );
50
51 function list_available_types() {
52         ksort($GLOBALS['types']);
53         foreach($GLOBALS['types'] as $key => $value) {
54                 tem_set('type', $key);
55                 tem_show('types');
56                 tem_show('types_sep');
57         }
58 }
59
60
61 function tem_set_globals(&$tem) {
62         $vars = array(
63                 'file_name',
64                 'public_file_name',
65                 'table_name',
66                 'plural',
67                 'singular');
68         foreach($vars as $var) {
69                 $tem->set($var, $GLOBALS[$var]);
70         }
71
72         $bools = array(
73                 'opt_email',
74                 'opt_db',
75                 'opt_listing',
76                 'opt_display',
77                 'opt_pass',
78                 'opt_public_form',
79                 'opt_public_display',
80                 'opt_public_something');
81         foreach($bools as $bool) {
82                 if($GLOBALS[$bool]) {
83                         $tem->set($bool);
84                 }
85         }
86 }
87
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']);
96                 }
97
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']);
106
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;
111                 }
112
113                 tem_init();
114                 tem_set_globals($GLOBALS['wfpl_template']);
115         }
116
117         if(isset($_REQUEST['fields'])) {
118                 if(isset($_REQUEST['view_sql'])) {
119                         view_sql();
120                         exit();
121                 } elseif(isset($_REQUEST['view_php'])) {
122                         view_php();
123                         exit();
124                 } elseif(isset($_REQUEST['view_html'])) {
125                         view_html();
126                         exit();
127                 } elseif(isset($_REQUEST['view_email'])) {
128                         view_email();
129                         exit();
130                 } elseif(isset($_REQUEST['download_tar'])) {
131                         download_tar();
132                         exit();
133                 } elseif(isset($_REQUEST['preview'])) {
134                         preview();
135                         exit();
136                 } elseif(isset($_REQUEST['edit'])) {
137                         tem_set('fields', $_REQUEST['fields']);
138                         # fall through
139                 } else {
140                         die("Sorry... couldn't tell which button you pressed");
141                 }
142         }
143
144
145         set_form_action();
146         tem_load(__DIR__.'/'.'metaform/main.html');
147         list_available_types();
148         tem_output();
149         exit(); # in case we're being called by wfpl_main or something
150 }
151
152
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]; }
156
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'];
161         }
162
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']);
172                         if($i > 0) {
173                                 $i -= 1;
174                                 if (!$GLOBALS['gotten_fields'][$i]['options']) {
175                                         $GLOBALS['gotten_fields'][$i]['options'] = array();
176                                 }
177                                 $GLOBALS['gotten_fields'][$i]['options'][] = $field;
178                                 continue;
179                         }
180                 }
181                 if(substr($field, -1) == '{') {
182                         $caption = trim(substr($field, 0, -1));
183                         $name = format_varname($caption);
184                         $type = '{';
185                         $options = null;
186                 } else {
187                         $options = null;
188                         $type = null;
189                         $div = strpos($field, ' ');
190                         if ($div === false) {
191                                 $div = strpos($field, "\t");
192                         }
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
196                                         $type = $field;
197                                 }
198                         } else {
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
202                                         $type = $first_word;
203                                         $field = trim(substr($field, $div + 1));
204                                 }
205                         }
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])) {
212                                         $type = $name;
213                                 } elseif (isset($GLOBALS['types'][strtolower($caption)])) {
214                                         $type = strtolower($caption);
215                                 }
216                         } else {
217                                 $name = format_varname($field);
218                                 $caption = format_caption($field);
219                         }
220
221                         if ($type === null) {
222                                 $type = 'textbox';
223                         }
224                 }
225                 $input = field_input($type);
226                 $format = field_format($type);
227                 $sql = field_sql($type);
228                 $GLOBALS['gotten_fields'][] = array(
229                         'caption' => $caption,
230                         'name' => $name,
231                         'type' => $type,
232                         'input' => $input,
233                         'format' => $format,
234                         'sql' => $sql,
235                         'options' => $options
236                 );
237         }
238
239         return $GLOBALS['gotten_fields'];
240 }
241
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);
247 }
248
249 # perfect HTTP headers for viewing created files
250 function view_headers() {
251         header('Content-type: text/plain');
252 }
253         
254
255
256
257 function make_sql() {
258         $tem = new tem();
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"');
270                         } else {
271                                 $tem->set('default', '""');
272                         }
273                         $tem->show('column');
274                 }
275         }
276         view_headers();
277         return $tem->run();
278 }
279
280 function view_sql() {
281         view_headers();
282         echo make_sql();
283 }
284
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'];
290                 }
291         }
292
293         return 'FIXME';
294 }
295         
296         
297
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;
301         $tem = new tem();
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']);
314                 } else {
315                         $visible_fields[] = array(
316                                 $field['input'] => array(
317                                         'name' => $field['name'],
318                                         'caption' => $field['caption']
319                                 )
320                         );
321                         if($field['type'] == 'radio') {
322                                 $i = 0;
323                                 $opts = array();
324                                 foreach ($field['options'] as $row) {
325                                         if (is_array($row)) {
326                                                 $cap = $row[1];
327                                         } else {
328                                                 $cap = $row;
329                                         }
330                                         $opts[] = array('i' => $i, 'option_caption' => $cap);
331                                         $i += 1;
332                                 }
333                                 $visible_fields[count($visible_fields) - 1]['options'] = $opts;
334                         }
335                 }
336
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');
344                 }
345
346                 switch($field['input']) {
347                         case 'image':
348                         case 'checkbox':
349                         case 'date':
350                         case 'textarea':
351                         case 'html':
352                                 $display_type = $field['input'];
353                         break;
354                         default:
355                                 $display_type = 'short';
356                 }
357                 if($field['format'] != 'n/a') {
358                         $display_fields[] = array($display_type => array(
359                                 'name' => $field['name'], 'caption' => $field['caption']));
360                 }
361
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']
366                         );
367                         $listing_field = array(
368                                 'name' => $field['name']
369                         );
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;
376                         } else {
377                                 $listing_field['enc'] = 'html';
378                         }
379                         $listing_fields[] = $listing_field;
380                 }
381         }
382
383         # Submit/Send button
384         if($GLOBALS['opt_email'] == 'Yes' && !$GLOBALS['opt_db']) {
385                 $visible_fields[] = array('submit' => array(
386                         'name' => 'send',
387                         'caption' => 'Send'));
388         } else {
389                 $visible_fields[] = array('submit' => array(
390                         'name' => 'save',
391                         'caption' => 'Save'));
392         }
393
394         $form_fields = array();
395         $form_fields['visible_fields'] = $visible_fields;
396         if($hidden_fields) {
397                 $form_fields['hidden_fields'] = $hidden_fields;
398         }
399         $tem->set('form', $form_fields);
400
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);
405
406
407         if($has_html_editors) {
408                 $tem->set('html_editor_headers');
409         }
410
411         if($whole_file) {
412                 return $tem->run();
413         } else {
414                 $tem2 = new tem();
415                 $tem2->load_str('<!--~form~-->');
416                 $tem2->merge($tem);
417                 return $tem2->run();
418         }
419 }
420
421 function view_html() {
422         view_headers();
423         echo make_html();
424 }
425
426 function show_in_listing($type, $input, $format, $sql) {
427         switch($input) {
428                 case 'submit':
429                 case 'hidden':
430                 case 'password':
431                 case 'textarea':
432                 case 'html':
433                 case 'fieldset':
434                 case 'end_fieldset':
435                         return false;
436         }
437         if($type == 'image') {
438                 return false;
439         }
440
441         return true;
442 }
443
444 function pulldown_options_array($options) {
445         if($options) {
446                 $pulldown_options = array();
447                 foreach($options as $option) {
448                         $option = preg_replace("/['\\\\]/", '\\\\$0', $option);
449                         $pulldown_options[] = "'$option'";
450                 }
451                 $pulldown_options = 'array(' . join(', ', $pulldown_options) . ')';
452         } else {
453                 $pulldown_options = "array(array('op1', 'Option One'), array('op2', 'Option Two'), 'n/a')";
454         }
455
456         return $pulldown_options;
457 }
458
459 function make_php() {
460         $has_html_editors = false;
461         $tem = new tem();
462         $tem->load(__DIR__.'/'.'metaform/template.php');
463         tem_set_globals($tem);
464         $fields = get_fields();
465         $db_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'];
478                         }
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');
484                                 }
485                                 $tem->show('image_settings');
486                                 $tem->show('image_upload');
487                                 $has_uploads = true;
488                         } else if($field['input'] == 'file') {
489                                 $tem->show('file_settings');
490                                 $tem->show('file_upload');
491                                 $has_uploads = true;
492                         } else {
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');
501                                 }
502                                 if($field['format'] != 'n/a') {
503                                         $tem->show('formats');
504                                 }
505                         }
506                 }
507
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');
512                         }
513                 }
514         }
515
516         $tem->set('name_to_caption', $name_to_caption);
517
518         if($has_uploads) {
519                 $tem->show('uploads_include');
520                 $tem->show('upload_max');
521                 $tem->show('upload_settings');
522                 $image_included_yet = true;
523         }
524
525         if($has_html_editors) {
526                 $tem->show('show_extra_headers');
527         }
528
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';
536                 }
537                 $tem->set('this_domain', $this_domain);
538         }
539         return $tem->run();
540 }
541
542 # make a URL for the edit page with all the fields filled in
543 function edit_url() {
544         $url = this_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);
548         return $url;
549 }
550
551 function view_php() {
552         view_headers();
553         echo make_php();
554 }
555
556 function make_email() {
557         $tem = new tem();
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');
568                 } else {
569                         $tem->show('normal');
570                 }
571                 $tem->show('fields');
572         }
573         return $tem->run();
574 }
575
576 function make_htaccess() {
577         $tem = new tem();
578         $tem->set('form', $GLOBALS['file_name']);
579         return $tem->run(__DIR__.'/'.'metaform/htaccess');
580 }
581
582 function view_email() {
583         view_headers();
584         echo make_email();
585 }
586
587 function preview() {
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');
595         }
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']) . ';'));
600                 }
601         }
602         $preview = $preview_tem->run();
603         unset($preview_tem);
604         $preview = preg_replace('|type="submit"|', 'type="submit" disabled="disabled"', $preview);
605         tem_set('preview', $preview);
606         tem_show('hiddens');
607         set_form_action();
608         tem_output();
609 }
610
611 function download_tar() {
612         $admin_name = $GLOBALS['file_name'];
613         $nice_name = $GLOBALS['public_file_name'];
614         $files = array(
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";
626         }
627         if($GLOBALS['opt_db']) {
628                 $files["$nice_name.sql"] = make_sql();
629         }
630         if($GLOBALS['opt_email']) {
631                 $files["$nice_name.email.txt"] = make_email();
632         }
633         make_tar($nice_name, $files);
634 }