JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added optional http-auth to metaform
[wfpl.git] / metaform.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22 # This file writes the code for you (sql, php, html, email) to handle a form.
23
24 require_once('code/wfpl/template.php');
25 require_once('code/wfpl/http.php');
26 require_once('code/wfpl/tar.php');
27 require_once('code/wfpl/format.php');
28
29 # see code/wfpl/metaform/template.html for the html templates for these elements
30 $GLOBALS['types'] = array(
31 #    type                  input          format        sql     
32         'name' =>       array('textbox',     'oneline',    'varchar(200)'),
33         'textbox' =>    array('textbox',     'oneline',    'varchar(200)'),
34         'int' =>        array('textbox',     'int',        'int'),
35         'bigint' =>     array('textbox',     'int',        'varchar(100)'), # up to 100 digits, stored as a string
36         'email' =>      array('textbox',     'email',      'varchar(100)'),
37         'phone' =>      array('textbox',     'phone',      'varchar(32)'),
38         'state' =>      array('states',      'oneline',    'varchar(2)'),
39         'money' =>      array('textbox',     'money',      'varchar(32)'),
40         'dollars' =>    array('textbox',     'dollars',    'varchar(32)'),
41         'url' =>        array('textbox',     'url',        'varchar(200)'),
42         'hidden' =>     array('hidden',      'unix',       'varchar(200)'),
43         'password' =>   array('password',    'oneline',    'varchar(200)'),
44         'textarea' =>   array('textarea',    'unix',       'text'),
45         'html' =>       array('html',        'unix',       'text'),
46         'pulldown' =>   array('pulldown',    'options',    'int'),
47         'radio' =>      array('radio',       'oneline',    'varchar(200)'),
48         'checkbox' =>   array('checkbox',    'yesno',      'varchar(3)'),
49         'yesno' =>      array('checkbox',    'yesno',      'varchar(3)'),
50         'delete' =>     array('checkbox',    'yesno',      'n/a'),
51         'image' =>      array('image',       'oneline',    'varchar(200)'),
52         'submit' =>     array('submit',      'oneline',    'n/a')
53 );
54
55 function list_available_types() {
56         $types = '';
57         foreach($GLOBALS['types'] as $key => $value) {
58                 if($types) {
59                         $types .= ', ';
60                 }
61                 $types .= $key;
62         }
63         tem_set('available_types', $types);
64 }
65
66
67 function metaform() {
68         if(isset($_REQUEST['form_name'])) {
69                 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
70                 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
71                 tem_set('opt_email', $GLOBALS['opt_email']);
72                 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
73                 tem_set('opt_db', $GLOBALS['opt_db']);
74                 $GLOBALS['opt_http_pass'] = format_yesno($_REQUEST['opt_http_pass']);
75                 tem_set('opt_http_pass', $GLOBALS['opt_http_pass']);
76         } else {
77                 $GLOBALS['form_name'] = 'some_form';
78         }
79
80         if(isset($_REQUEST['fields'])) {
81                 if(isset($_REQUEST['view_sql'])) {
82                         view_sql();
83                         exit();
84                 } elseif(isset($_REQUEST['view_php'])) {
85                         view_php();
86                         exit();
87                 } elseif(isset($_REQUEST['view_html'])) {
88                         view_html();
89                         exit();
90                 } elseif(isset($_REQUEST['view_email'])) {
91                         view_email();
92                         exit();
93                 } elseif(isset($_REQUEST['download_tar'])) {
94                         download_tar();
95                         exit();
96                 } elseif(isset($_REQUEST['preview'])) {
97                         preview();
98                         exit();
99                 } elseif(isset($_REQUEST['edit'])) {
100                         tem_set('fields', $_REQUEST['fields']);
101                         tem_set('form_name', $GLOBALS['form_name']);
102                         # fall through
103                 } else {
104                         die("Sorry... couldn't tell which button you pressed");
105                 }
106         }
107
108
109         set_form_action();
110         list_available_types();
111         tem_output('code/wfpl/metaform/main.html');
112 }
113
114
115 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
116 function field_format($type) { return $GLOBALS['types'][$type][1]; }
117 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
118
119 function get_fields() {
120         $fields_str = unix_newlines($_REQUEST['fields']);
121         $ret = array();
122         $fields_str = rtrim($fields_str);
123         $fields = split("\n", $fields_str);
124         foreach($fields as $field) {
125                 list($name, $type, $options) = split('  *', $field);
126                 if($options) $options = split(',', $options);
127                 if(!$type) $type = $name;
128                 $input = field_input($type);
129                 $format = field_format($type);
130                 $sql = field_sql($type);
131                 $ret[] = array($name, $type, $input, $format, $sql, $options);
132         }
133         return $ret;
134 }
135
136 # this one, that you're using to create forms
137 function set_form_action() {
138         $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
139         if($action == '') $action = './';
140         tem_set('form_action', $action);
141 }
142
143 # perfect HTTP headers for viewing created files
144 function view_headers() {
145         header('Content-type: text/plain');
146 }
147         
148
149
150
151 function make_sql() {
152         $tem = new tem();
153         $tem->load('code/wfpl/metaform/template.sql');
154         $tem->set('form_name', $GLOBALS['form_name']);
155         $fields = get_fields();
156         foreach($fields as $field) {
157                 list($name, $type, $input, $format, $sql) = $field;
158                 if($sql != 'n/a') {
159                         $tem->set('name', $name);
160                         $tem->set('type', $sql);
161                         if($sql == 'int') {
162                                 $tem->set('default', '0');
163                         } else {
164                                 $tem->set('default', '""');
165                         }
166                         $tem->sub('column');
167                 }
168         }
169         view_headers();
170         return $tem->run();
171 }
172
173 function view_sql() {
174         view_headers();
175         echo make_sql();
176 }
177         
178
179 # pass false if you want to exclude the <head> and <body> tag etc.
180 function make_html($whole_file = true) {
181         $uploads_output_already = false;
182         $has_html_editors = false;
183         $tem = new tem();
184         $tem->load('code/wfpl/metaform/template.html');
185         $tem->set('form_name', $GLOBALS['form_name']);
186         $fields = get_fields();
187         foreach($fields as $field) {
188                 list($name, $type, $input, $format, $sql) = $field;
189                 $tem->set('name', $name);
190                 $tem->set('caption', $name); # fixme
191                 $tem->sub($input);
192                 if($input != 'hidden') {
193                         $tem->sub('row');
194                 }
195                 if($input == 'image' && !$uploads_output_already) {
196                         $tem->sub('uploads');
197                         $tem->set('enctype_attr', '" enctype="multipart/form-data');
198                         $uploads_output_already = true;
199                 } elseif($input == 'html') {
200                         $has_html_editors = true;
201                         $tem->set('html_field_name', $name);
202                         $tem->sub('replace_textarea');
203                 }
204         }
205
206         if($GLOBALS['opt_db'] == 'Yes') {
207                 $tem->sub('opt_db_1');
208         } else {
209                 $tem->sub('opt_db_1_else');
210         }
211         $tem->set('name', 'save');
212         $tem->set('caption', 'Save');
213         $tem->sub('submit');
214         $tem->sub('row');
215         $tem->sub('form');
216
217         if($has_html_editors) {
218                 $tem->sub('html_editor_headers');
219         }
220
221         if($whole_file) {
222                 return $tem->run();
223         } else {
224                 return $tem->get('form');
225         }
226 }
227
228 function view_html() {
229         view_headers();
230         echo make_html();
231 }
232
233
234 function make_php() {
235         $tem = new tem();
236         $tem->load('code/wfpl/metaform/template.php');
237         $tem->set('form_name', $GLOBALS['form_name']);
238         $fields = get_fields();
239         $db_fields = '';
240         $php_fields = '';
241         $always_field = false;
242         $image_included_yet = false;
243         foreach($fields as $field) {
244                 list($name, $type, $input, $format, $sql) = $field;
245                 if($input != 'submit') {
246                         $tem->set('format', $format);
247                         $tem->set('name', $name);
248                         $tem->set('db_field', ''); # we don't want to use the value from last time
249                         if($sql != 'n/a') {
250                                 if($db_fields != '') $db_fields .= ',';
251                                 $db_fields .= $name;
252                                 if($php_fields != '') $php_fields .= ', ';
253                                 $php_fields .= '$' . $name;
254                         }
255                         if($input == 'image') {
256                                 $tem->sub('image_upload');
257                                 $tem->sub('image_db');
258                                 if(!$image_included_yet) {
259                                         $tem->sub('image_include');
260                                         $tem->sub('upload_max');
261                                         $tem->sub('upload_settings');
262                                         $image_included_yet = true;
263                                 }
264                         } else {
265                                 $tem->sub('formats');
266                         }
267                         $tem->sub('tem_sets');
268                         if(!$always_field and $input != 'checkbox' and $input != 'radio') {
269                                 $always_field = $name;
270                         }
271                 }
272         }
273         # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
274         $tem->set('always_field', $always_field);
275         $tem->set('db_fields', $db_fields);
276         $tem->set('php_fields', $php_fields);
277         $tem->set('metaform_url', edit_url());
278         if($GLOBALS['opt_db'] == 'Yes') {
279                 $tem->sub('opt_db_1');
280                 $tem->sub('opt_db_2');
281                 $tem->sub('opt_db_3');
282                 $tem->sub('opt_db_4');
283                 $tem->sub('opt_db_5');
284         }
285         if($GLOBALS['opt_email'] == 'Yes') {
286                 $tem->sub('opt_email_1');
287                 $tem->sub('opt_email_2');
288         }
289         if($GLOBALS['opt_http_pass'] == 'Yes') {
290                 $tem->sub('opt_http_pass_1');
291                 $tem->sub('opt_http_pass_2');
292         }
293         return $tem->run();
294 }
295
296 # make a URL for the edit page with all the fields filled in
297 function edit_url() {
298         $url = this_url();
299         $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
300         $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
301         $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
302         $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
303         return $url;
304 }
305
306 function view_php() {
307         view_headers();
308         echo make_php();
309 }
310
311
312 function make_email() {
313         $tem = new tem();
314         $tem->load('code/wfpl/metaform/template.email.txt');
315         $tem->set('form_name', $GLOBALS['form_name']);
316         $fields = get_fields();
317         foreach($fields as $field) {
318                 list($name, $type, $input, $format, $sql) = $field;
319                 $tem->set('name', $name);
320                 $tem->set('caption', $name); # fixme
321                 if($type == 'textarea') {
322                         $tem->sub('multi_line');
323                 } else {
324                         $tem->sub('fields');
325                 }
326         }
327         return $tem->run();
328 }
329
330 function make_htaccess() {
331         $tem = new tem();
332         $tem->set('form', $GLOBALS['form_name']);
333         return $tem->run('code/wfpl/metaform/template.htaccess');
334 }
335
336 function view_email() {
337         view_headers();
338         echo make_email();
339 }
340
341
342 function preview() {
343         tem_load('code/wfpl/metaform/preview.html');
344         tem_set('form_name', $GLOBALS['form_name']);
345         tem_set('fields', $_REQUEST['fields']);
346         $preview_tem = new tem();
347         $preview = $preview_tem->run(make_html(false));
348         unset($preview_tem);
349         tem_set('preview', $preview);
350         set_form_action();
351         tem_output();
352 }
353
354 function download_tar() {
355         $name = $GLOBALS['form_name'];
356         $data = array(
357                 ".htaccess" => make_htaccess(),
358                 "run.php ->" => 'code/wfpl/run.php',
359                 "$name.html" => make_html(),
360                 "$name.sql" => make_sql(),
361                 "$name.php" => make_php());
362         if($GLOBALS['opt_email'] == 'Yes') {
363                 $data["$name.email.txt"] = make_email();
364         }
365         make_wfpl_tar($name, $data);
366 }
367
368
369 metaform();
370 exit();
371
372 ?>