JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
no dashes in filenames, improved image uploading, make_thumbnail()
[wfpl.git] / metaform / template.php
1 <?php
2
3 # This form requires wfpl. If you didn't recieve wfpl along with this file,
4 # see: http://jasonwoof.org/wfpl
5
6 # This form was auto-generated. If you would like to alter the parameters and
7 # generate a new one try this URL:
8 #
9 # ~metaform_url~
10
11 # This code can send form results by e-mail and/or save them to a database. See
12 # the next two comments to enable either or both.
13
14 # To send results by e-mail, all you have to do is set your e-mail address here:
15 $GLOBALS['~form_name~_form_recipient'] = "fixme@example.com";
16 <!--~upload_settings start~-->
17 # Set this to the path to your uploads directory. It can be relative to the
18 # location of this script. IT MUST END WITH A SLASH
19 $GLOBALS['upload_directory'] = 'uploads/';
20 <!--~end~-->
21 # To save results to a database, you'll need to create the ~form_name~ table
22 # (the file ~form_name~.sql should help with this), and create a file called
23 # 'db_connect.php' which calls db_connect() see:
24 # code/wfpl/examples/db_connect.php
25
26 if(!file_exists('code/wfpl/template.php')) { die('This form requires <a href="http://jasonwoof.org/wfpl">wfpl</a>.'); }
27 require_once('code/wfpl/template.php');
28 require_once('code/wfpl/format.php');
29 require_once('code/wfpl/messages.php');
30 require_once('code/wfpl/email.php');
31 require_once('code/wfpl/db.php');<!--~image_include start~-->
32 require_once('code/wfpl/upload.php');<!--~end~-->
33
34 function ~form_name~_get_fields() {
35         <!--~formats start~-->
36         $~name~ = format_~format~($_REQUEST['~name~']);<!--~end~--><!--~image_upload start~-->
37         if($_FILE['~name~'] && $_FILE['~name~']['error'] == 0) {
38                 $~name~ = substr(save_uploaded_image('~name~', $GLOBALS['upload_directory']), strlen($GLOBALS['upload_directory']));
39         } else {
40                 $~name~ = format_filename($_REQUEST['old_~name~']);
41         }<!--~end~-->
42         <!--~tem_sets start~-->
43         tem_set('~name~', $~name~);<!--~end~-->
44
45         return array(~php_fields~);
46 }
47
48 function ~form_name~() {
49         $edit_id = format_int($_REQUEST['~form_name~_edit_id']);
50         unset($_REQUEST['~form_name~_edit_id']);
51         if($edit_id) {
52                 # add hidden field for database id of row we're editing
53                 tem_set('~form_name~_edit_id', $edit_id);
54                 tem_sub('editing');
55                 tem_sub('edit_msg');
56         }
57
58         $delete_id = format_int($_REQUEST['~form_name~_delete_id']);
59         unset($_REQUEST['~form_name~_delete_id']);
60         if($delete_id) {
61                 db_delete('~form_name~', 'where id=%i', $delete_id);
62                 message('Entry deleted.');
63
64                 # FIXME: what to do after delete?
65                 return;
66         }
67
68         if(!$edit_id && !$delet_id) {
69                 tem_sub('new_msg');
70         }
71
72         if(isset($_REQUEST['~always_field~'])) {
73                 list(~php_fields~) = ~form_name~_get_fields();
74
75                 if("you're happy with the POSTed values") {
76                         # to enable saving to a database, create a file called 'db_connect.php'
77                         # see: code/wfpl/examples/db_connect.php
78                         if(file_exists('db_connect.php') || file_exists('code/db_connect.php')) {
79                                 if(file_exists('db_connect.php') {
80                                         require_once('db_connect.php');
81                                 } else {
82                                         require_once('code/db_connect.php');
83                                 }
84                                 if($edit_id) {<!--~image_db start~-->
85                                         # uploading nothing means leaving it as is.
86                                         if(!$~name~ && $delete_~name~ != 'Yes') {
87                                                 $~name~ = db_get_value('~form_name~', '~name~', 'where id=%i', $edit_id);
88                                         }
89                                         <!--~end~-->
90                                         db_update('~form_name~', '~db_fields~', ~php_fields~, 'where id=%i', $edit_id);
91                                         message('Entry updated.');
92                                 } else {
93                                         db_insert('~form_name~', '~db_fields~', ~php_fields~);
94                                         message('Entry saved.');
95                                 }
96                         }
97                         if($GLOBALS['~form_name~_form_recipient'] != "fixme@example.com") {
98                                 $to = $GLOBALS['~form_name~_form_recipient'];
99                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
100                                         $from = $_REQUEST['email'];
101                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
102                                                 $from = "$_REQUEST[name] <$from>";
103                                         }
104                                 } else {
105                                         $from = $to;
106                                 }
107                                 $subject = '~form_name~ form submitted';
108                                 $message = tem_run('~form_name~.email.txt');
109                                 $cc = '';
110                                 $bcc = '';
111                                 if(email($from, $to, $subject, $message, $cc, $bcc)) {
112                                         tem_set('error_message', 'Due to an internal error, your message could not be sent. Please try again later.');
113                                         tem_sub('error');
114                                         $error = true;
115                                 }
116                         }
117                         if($error !== true) {
118                                 tem_load('~form_name~.html');
119                                 tem_sub('thankyou');
120                                 tem_output();
121                                 exit();
122                         }
123                 }
124                 # otherwise, we display the form again. ~form_name~_get_fields() has
125                 # already put the posted values back into the template engine, so they will
126                 # show up in the form fields. You should add some message asking people to
127                 # fix their entry in whatever way you require.
128         } elseif($edit_id) {
129                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
130                 list(~php_fields~) = db_get_row('~form_name~', '~db_fields~', 'where id=%i', $edit_id);
131                 ~tem_sets.tab~
132         } else {
133                 # form not submitted, you can set default values like so:
134                 #tem_set('~always_field~', 'Yes');
135         }<!--~upload_max start~-->
136
137         tem_set('upload_max_filesize', upload_max_filesize());<!--~end~-->
138
139         tem_sub('form');
140 }
141
142 ?>