JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: make run.php include (instead of being a symlink)
[wfpl.git] / metaform.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #  
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #  
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 # This file writes the code for you (sql, php, html, email) to handle a form.
20
21 require_once(__DIR__ . '/template.php');
22 require_once(__DIR__ . '/http.php');
23 require_once(__DIR__ . '/tar.php');
24 require_once(__DIR__ . '/format.php');
25
26 # see code/wfpl/metaform/template.html for the html templates for these elements
27 $GLOBALS['types'] = array(
28 #    type                  input          format        sql     
29         'varname' =>    array('textbox',     'varname',    'varchar(50) binary'),
30         'name' =>       array('textbox',     'oneline',    'varchar(200) binary'),
31         'textbox' =>    array('textbox',     'oneline',    'varchar(200) binary'),
32         'int' =>        array('textbox',     'int',        'int'),
33         'decimal' =>    array('textbox',     'decimal',    'decimal(12,2)'),
34         'bigint' =>     array('textbox',     'int',        'varchar(100) binary'), # up to 100 digits, stored as a string
35         'zip' =>        array('textbox',     'zip',        'varchar(20) binary'),
36         'email' =>      array('email',       'email',      'varchar(100) binary'),
37         'search' =>     array('search',      'oneline',    'varchar(200) binary'),
38         'phone' =>      array('textbox',     'phone',      'varchar(32) binary'),
39         'state' =>      array('states',      'oneline',    'varchar(2) binary'),
40         'money' =>      array('textbox',     'money',      'varchar(32) binary'),
41         'date' =>       array('date',        'mdy_to_ymd', 'char(10) binary'),
42         'dollars' =>    array('textbox',     'dollars',    'varchar(32) binary'),
43         'url' =>        array('textbox',     'url',        'varchar(200) binary'),
44         'hidden' =>     array('hidden',      'unix',       'varchar(200) binary'),
45         'password' =>   array('password',    'oneline',    'varchar(200) binary'),
46         'textarea' =>   array('textarea',    'unix',       'text binary'),
47         'html' =>       array('html',        'unix',       'mediumtext binary'),
48         'pulldown' =>   array('pulldown',    'options',    'varchar(100) binary'),
49         'radio' =>      array('radio',       'options',    'varchar(100) binary'),
50         'checkbox' =>   array('checkbox',    'bool',       'int(1)'),
51         'yesno' =>      array('checkbox',    'yesno',      'varchar(3) binary'),
52         'delete' =>     array('checkbox',    'yesno',      'n/a'),
53         'image' =>      array('image',       'oneline',    'varchar(120) binary'),
54         'thumb' =>      array('image',       'oneline',    'varchar(240) binary'),
55         'file' =>       array('file',        'oneline',    'varchar(100) binary'),
56         'submit' =>     array('submit',      'n/a',        'n/a'),
57         '{' =>          array('fieldset',    'n/a',        'n/a'),
58         '}' =>          array('end_fieldset','n/a',        'n/a')
59 );
60
61 function list_available_types() {
62         ksort($GLOBALS['types']);
63         foreach($GLOBALS['types'] as $key => $value) {
64                 tem_set('type', $key);
65                 tem_show('types');
66                 tem_show('types_sep');
67         }
68 }
69
70
71 function tem_set_globals(&$tem) {
72         $vars = array(
73                 'file_name',
74                 'public_file_name',
75                 'table_name',
76                 'plural',
77                 'singular');
78         foreach($vars as $var) {
79                 $tem->set($var, $GLOBALS[$var]);
80         }
81
82         $bools = array(
83                 'opt_email',
84                 'opt_db',
85                 'opt_listing',
86                 'opt_display',
87                 'opt_pass',
88                 'opt_public_form',
89                 'opt_public_display',
90                 'opt_public_something');
91         foreach($bools as $bool) {
92                 if($GLOBALS[$bool]) {
93                         $tem->set($bool);
94                 }
95         }
96 }
97
98 function metaform() {
99         if(isset($_REQUEST['singular'])) {
100                 $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']);
101                 $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']);
102                 $GLOBALS['plural'] = format_oneline($_REQUEST['plural']);
103                 # backwards compatibility:
104                 if(isset($_REQUEST['form_name'])) {
105                         $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']);
106                 }
107
108                 $GLOBALS['singular'] = format_oneline($_REQUEST['singular']);
109                 $GLOBALS['opt_email'] = format_bool($_REQUEST['opt_email']);
110                 $GLOBALS['opt_db'] = format_bool($_REQUEST['opt_db']);
111                 $GLOBALS['opt_listing'] = format_bool($_REQUEST['opt_listing']);
112                 $GLOBALS['opt_display'] = format_bool($_REQUEST['opt_display']);
113                 $GLOBALS['opt_pass'] = format_bool($_REQUEST['opt_pass']);
114                 $GLOBALS['opt_public_form'] = format_bool($_REQUEST['opt_public_form']);
115                 $GLOBALS['opt_public_display'] = format_bool($_REQUEST['opt_public_display']);
116
117                 $GLOBALS['public_file_name'] = $GLOBALS['file_name'];
118                 if($GLOBALS['opt_public_form'] || $GLOBALS['opt_public_display']) {
119                         $GLOBALS['opt_public_something'] = 1;
120                         $GLOBALS['file_name'] = $GLOBALS['file_name'] . _admin;
121                 }
122
123                 tem_init();
124                 tem_set_globals($GLOBALS['wfpl_template']);
125         }
126
127         if(isset($_REQUEST['fields'])) {
128                 if(isset($_REQUEST['view_sql'])) {
129                         view_sql();
130                         exit();
131                 } elseif(isset($_REQUEST['view_php'])) {
132                         view_php();
133                         exit();
134                 } elseif(isset($_REQUEST['view_html'])) {
135                         view_html();
136                         exit();
137                 } elseif(isset($_REQUEST['view_email'])) {
138                         view_email();
139                         exit();
140                 } elseif(isset($_REQUEST['download_tar'])) {
141                         download_tar();
142                         exit();
143                 } elseif(isset($_REQUEST['preview'])) {
144                         preview();
145                         exit();
146                 } elseif(isset($_REQUEST['edit'])) {
147                         tem_set('fields', $_REQUEST['fields']);
148                         # fall through
149                 } else {
150                         die("Sorry... couldn't tell which button you pressed");
151                 }
152         }
153
154
155         set_form_action();
156         tem_load('code/wfpl/metaform/main.html');
157         list_available_types();
158         tem_output();
159 }
160
161
162 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
163 function field_format($type) { return $GLOBALS['types'][$type][1]; }
164 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
165
166 function get_fields() {
167         # no sense in doing all this so many times
168         if(isset($GLOBALS['gotten_fields'])) {
169                 return $GLOBALS['gotten_fields'];
170         }
171
172         $fields_str = unix_newlines($_REQUEST['fields']);
173         $GLOBALS['gotten_fields'] = array();
174         $fields_str = rtrim($fields_str);
175         $fields = split("\n", $fields_str);
176         foreach($fields as $field) {
177                 $field = trim($field);
178                 if(substr($field, -1) == '{') {
179                         $caption = trim(substr($field, 0, -1));
180                         $name = format_varname($caption);
181                         $type = '{';
182                         $options = null;
183                 # FIXME restore parsing of option lists for pulldowns
184                 } else {
185                         $options = null;
186                         $type = null;
187                         $div = strpos($field, ' ');
188                         if ($div === false) {
189                                 $div = strpos($field, "\t");
190                         }
191                         if ($div === false) {
192                                 if (isset($GLOBALS['types'][$field])) {
193                                         # if just one word, and it's a type, use it as name/caption and type
194                                         $type = $field;
195                                 }
196                         } else {
197                                 $first_word = trim(substr($field, 0, $div));
198                                 if (isset($GLOBALS['types'][$first_word])) {
199                                         # if the first word (of multiple) is a type, remove it from the name/caption
200                                         $type = $first_word;
201                                         $field = trim(substr($field, $div + 1));
202                                 }
203                         }
204                         # see if there's an explicit name/caption splitter
205                         $div = strpos($field, '|');
206                         if ($div !== false) {
207                                 $name = trim(substr($field, 0, $div));
208                                 $caption = trim(substr($field, $div + 1));
209                                 if (isset($GLOBALS['types'][$name])) {
210                                         $type = $name;
211                                 } elseif (isset($GLOBALS['types'][strtolower($caption)])) {
212                                         $type = strtolower($caption);
213                                 }
214                         } else {
215                                 $name = format_varname($field);
216                                 $caption = format_caption($field);
217                         }
218
219                         if ($type === null) {
220                                 $type = 'textbox';
221                         }
222                 }
223                 $input = field_input($type);
224                 $format = field_format($type);
225                 $sql = field_sql($type);
226                 $GLOBALS['gotten_fields'][] = array(
227                         'caption' => $caption,
228                         'name' => $name,
229                         'type' => $type,
230                         'input' => $input,
231                         'format' => $format,
232                         'sql' => $sql,
233                         'options' => $options
234                 );
235         }
236
237         return $GLOBALS['gotten_fields'];
238 }
239
240 # this one, that you're using to create forms
241 function set_form_action() {
242         $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
243         if($action == '') $action = './';
244         tem_set('form_action', $action);
245 }
246
247 # perfect HTTP headers for viewing created files
248 function view_headers() {
249         header('Content-type: text/plain');
250 }
251         
252
253
254
255 function make_sql() {
256         $tem = new tem();
257         $tem->load('code/wfpl/metaform/template.sql');
258         tem_set_globals($tem);
259         $fields = get_fields();
260         foreach($fields as $field) {
261                 if($field['sql'] != 'n/a') {
262                         $tem->set('name', $field['name']);
263                         $tem->set('type', $field['sql']);
264                         if(substr($field['sql'], 0, 3) == 'int' || substr($field['sql'], 0, 7) == 'decimal') {
265                                 $tem->set('default', '0');
266                         } elseif($field['format'] == 'yesno') {
267                                 $tem->set('default', '"No"');
268                         } else {
269                                 $tem->set('default', '""');
270                         }
271                         $tem->show('column');
272                 }
273         }
274         view_headers();
275         return $tem->run();
276 }
277
278 function view_sql() {
279         view_headers();
280         echo make_sql();
281 }
282
283 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
284 function find_always_field(&$fields) {
285         foreach($fields as $field) {
286                 if($field['input'] != 'submit' && $field['input'] != 'image' && $field['input'] != 'file' && $field['input'] != 'checkbox' && $field['input'] != 'radio' && $field['type'] != '{' && $field['type'] != '}') {
287                         return $field['name'];
288                 }
289         }
290
291         return 'FIXME';
292 }
293         
294         
295
296 # pass false if you want to exclude the <head> and <body> tag etc.
297 function make_html($whole_file = true) {
298         $has_html_editors = false;
299         $tem = new tem();
300         $tem->load('code/wfpl/metaform/template.html');
301         tem_set_globals($tem);
302         $fields = get_fields();
303         $tem->set('always_field', find_always_field($fields));
304         $hidden_fields = array();
305         $visible_fields = array();
306         $display_fields = array();
307         $listing_headers = array();
308         $listing_fields = array();
309         foreach($fields as $field) {
310                 if($field['input'] == 'hidden') {
311                         $hidden_fields[] = array('name' => $field['name']);
312                 } else {
313                         $visible_fields[] = array(
314                                 $field['input'] => array(
315                                         'name' => $field['name'],
316                                         'caption' => $field['caption']
317                                 )
318                         );
319                 }
320
321                 if($field['input'] == 'image' || $field['input'] == 'file') {
322                         $tem->set('uploads');
323                         $tem->set('enctype_attr', '" enctype="multipart/form-data');
324                 } elseif($field['input'] == 'html') {
325                         $has_html_editors = true;
326                         $tem->set('html_field_name', $field['name']);
327                         $tem->set('replace_textarea');
328                 }
329
330                 switch($field['input']) {
331                         case 'image':
332                         case 'checkbox':
333                         case 'date':
334                         case 'textarea':
335                         case 'html':
336                                 $display_type = $field['input'];
337                         break;
338                         default:
339                                 $display_type = 'short';
340                 }
341                 if($field['format'] != 'n/a') {
342                         $display_fields[] = array($display_type => array(
343                                 'name' => $field['name'], 'caption' => $field['caption']));
344                 }
345
346                 if(show_in_listing($field['type'], $field['input'], $field['format'], $field['sql'])) {
347                         $listing_headers[] = array(
348                                 'caption' => $field['caption'],
349                                 'name' => $field['name']
350                         );
351                         $listing_field = array(
352                                 'name' => $field['name']
353                         );
354                         if($field['format'] == 'bool' || $field['format'] == 'yesno') {
355                                 $listing_field['enc'] = 'yesno';
356                         } elseif($field['input'] == 'date') {
357                                 $listing_field['enc'] = 'mmddyyyy';
358                         } elseif($field['type'] == 'thumb') {
359                                 $listing_field['thumb'] = true;
360                         } else {
361                                 $listing_field['enc'] = 'html';
362                         }
363                         $listing_fields[] = $listing_field;
364                 }
365         }
366
367         # Submit/Send button
368         if($GLOBALS['opt_email'] == 'Yes' && !$GLOBALS['opt_db']) {
369                 $visible_fields[] = array('submit' => array(
370                         'name' => 'send',
371                         'caption' => 'Send'));
372         } else {
373                 $visible_fields[] = array('submit' => array(
374                         'name' => 'save',
375                         'caption' => 'Save'));
376         }
377
378         $form_fields = array();
379         $form_fields['visible_fields'] = $visible_fields;
380         if($hidden_fields) {
381                 $form_fields['hidden_fields'] = $hidden_fields;
382         }
383         $tem->set('form', $form_fields);
384
385         # opt_display and opt_listing control whether these are actually displayed
386         $tem->set('display_fields', $display_fields);
387         $tem->set('listing_headers', $listing_headers);
388         $tem->set('listing_fields', $listing_fields);
389
390
391         if($has_html_editors) {
392                 $tem->set('html_editor_headers');
393         }
394
395         if($whole_file) {
396                 return $tem->run();
397         } else {
398                 $tem2 = new tem();
399                 $tem2->load_str('<!--~form~-->');
400                 $tem2->merge($tem);
401                 return $tem2->run();
402         }
403 }
404
405 function view_html() {
406         view_headers();
407         echo make_html();
408 }
409
410 function show_in_listing($type, $input, $format, $sql) {
411         switch($input) {
412                 case 'submit':
413                 case 'hidden':
414                 case 'password':
415                 case 'textarea':
416                 case 'html':
417                 case 'fieldset':
418                 case 'end_fieldset':
419                         return false;
420         }
421         if($type == 'image') {
422                 return false;
423         }
424
425         return true;
426 }
427
428 function pulldown_options_array($options) {
429         if($options) {
430                 $pulldown_options = array();
431                 foreach($options as $option) {
432                         $option = preg_replace("/['\\\\]/", '\\\\$0', $option);
433                         $pulldown_options[] = "'$option'";
434                 }
435                 $pulldown_options = 'array(' . join(', ', $pulldown_options) . ')';
436         } else {
437                 $pulldown_options = "array(array('op1', 'Option One'), array('op2', 'Option Two'), 'n/a')";
438         }
439
440         return $pulldown_options;
441 }
442
443 function make_php() {
444         $has_html_editors = false;
445         $tem = new tem();
446         $tem->load('code/wfpl/metaform/template.php');
447         tem_set_globals($tem);
448         $fields = get_fields();
449         $db_fields = '';
450         $always_field = find_always_field($fields);
451         $image_included_yet = false;
452         $name_to_caption = array();
453         foreach($fields as $field) {
454                 $name_to_caption[] = array('name' => $field['name'], 'caption' => $field['caption']);
455                 if($field['input'] != 'submit') {
456                         $tem->set('format', $field['format']);
457                         $tem->set('name', $field['name']);
458                         $tem->set('db_field', ''); # we don't want to use the value from last time
459                         if($field['sql'] != 'n/a') {
460                                 if($db_fields != '') $db_fields .= ',';
461                                 $db_fields .= $field['name'];
462                         }
463                         if($field['input'] == 'image') {
464                                 if($field['type'] == 'thumb') {
465                                         $tem->show('thumb_settings');
466                                         $tem->show('thumb_upload_params');
467                                         $tem->show('thumb_w_h');
468                                 }
469                                 $tem->show('image_settings');
470                                 $tem->show('image_upload');
471                                 $has_uploads = true;
472                         } else if($field['input'] == 'file') {
473                                 $tem->show('file_settings');
474                                 $tem->show('file_upload');
475                                 $has_uploads = true;
476                         } else {
477                                 if($field['input'] == 'html') {
478                                         $has_html_editors = true;
479                                 } elseif($field['input'] == 'pulldown' || $field['input'] == 'radio') {
480                                         $pulldown_options = pulldown_options_array($field['options']);
481                                         $tem->set('pulldown_options', $pulldown_options);
482                                         $tem->set('has_pulldowns');
483                                         $tem->show('pulldowns');
484                                         $tem->show('pulldown_format_extra');
485                                 }
486                                 if($field['format'] != 'n/a') {
487                                         $tem->show('formats');
488                                 }
489                         }
490                 }
491
492                 if($GLOBALS['opt_listing']) {
493                         if(show_in_listing($field['type'], $field['input'], $field['format'], $field['sql'])) {
494                                 $tem->show('listing_fields_1');
495                                 $tem->show('listing_fields_2');
496                         }
497                 }
498         }
499
500         $tem->set('name_to_caption', $name_to_caption);
501
502         if($has_uploads) {
503                 $tem->show('uploads_include');
504                 $tem->show('upload_max');
505                 $tem->show('upload_settings');
506                 $image_included_yet = true;
507         }
508
509         if($has_html_editors) {
510                 $tem->show('show_extra_headers');
511         }
512
513         $tem->set('always_field', $always_field);
514         $tem->set('db_fields', $db_fields);
515         $tem->set('metaform_url', edit_url());
516         if($GLOBALS['opt_email']) {
517                 $this_domain = $_SERVER['HTTP_HOST'];
518                 if(substr($this_domain, -2) == '.l') {
519                         $this_domain = substr($this_domain, 0, -1) . 'com';
520                 }
521                 $tem->set('this_domain', $this_domain);
522         }
523         return $tem->run();
524 }
525
526 # make a URL for the edit page with all the fields filled in
527 function edit_url() {
528         $url = this_url();
529         $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
530         $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
531         $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
532         $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
533         return $url;
534 }
535
536 function view_php() {
537         view_headers();
538         echo make_php();
539 }
540
541 function make_email() {
542         $tem = new tem();
543         $tem->load('code/wfpl/metaform/template.email.txt');
544         tem_set_globals($tem);
545         $fields = get_fields();
546         foreach($fields as $field) {
547                 $tem->set('name', $field['name']);
548                 $tem->set('caption', $field['caption']);
549                 if($field['type'] == 'textarea') {
550                         $tem->show('multi_line');
551                 } elseif($field['type'] == 'checkbox') {
552                         $tem->show('checkbox');
553                 } else {
554                         $tem->show('normal');
555                 }
556                 $tem->show('fields');
557         }
558         return $tem->run();
559 }
560
561 function make_htaccess() {
562         $tem = new tem();
563         $tem->set('form', $GLOBALS['file_name']);
564         return $tem->run('code/wfpl/metaform/htaccess');
565 }
566
567 function view_email() {
568         view_headers();
569         echo make_email();
570 }
571
572 function preview() {
573         tem_load('code/wfpl/metaform/preview.html');
574         tem_set_globals($GLOBALS['wfpl_template']);
575         tem_set('fields', $_REQUEST['fields']);
576         $preview_tem = new tem();
577         $preview_tem->load_str(make_html(false));
578         if($GLOBALS['opt_db']) {
579                 $preview_tem->show('new_msg');
580         }
581         $fields = get_fields();
582         foreach($fields as $field) {
583                 if($field['type'] == 'pulldown' || $field['type'] == 'radio') {
584                         pulldown($field['name'], eval('return ' . pulldown_options_array($field['options']) . ';'));
585                 }
586         }
587         $preview = $preview_tem->run();
588         unset($preview_tem);
589         $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
590         tem_set('preview', $preview);
591         tem_show('hiddens');
592         set_form_action();
593         tem_output();
594 }
595
596 function download_tar() {
597         $admin_name = $GLOBALS['file_name'];
598         $nice_name = $GLOBALS['public_file_name'];
599         $files = array(
600                 "INSTALL" => read_whole_file('code/wfpl/metaform/INSTALL'),
601                 ".htaccess" => make_htaccess(),
602                 "run.php" => "<?php\n\nrequire_once(__DIR__ . '/code/wfpl/run.php');",
603                 "style.less" => read_whole_file('code/wfpl/metaform/style.less'),
604                 "template.html" => read_whole_file('code/wfpl/metaform/site-template.html'),
605                 "$admin_name.html" => make_html(),
606                 "$admin_name.php" => make_php());
607         if($GLOBALS['opt_public_something']) {
608                 $files["$nice_name.html ->"] = "$admin_name.html";
609                 $files["$nice_name.php ->"] = "$admin_name.php";
610         }
611         if($GLOBALS['opt_db']) {
612                 $files["$nice_name.sql"] = make_sql();
613         }
614         if($GLOBALS['opt_email']) {
615                 $files["$nice_name.email.txt"] = make_email();
616         }
617         make_tar($nice_name, $files);
618 }
619
620
621 metaform();
622 exit();