1 | <?php |
---|
2 | # ***** BEGIN LICENSE BLOCK ***** |
---|
3 | # Copyright (c) 2009 Olivier Azeau and contributors. All rights reserved. |
---|
4 | # |
---|
5 | # This 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 2 of the License, or |
---|
8 | # (at your option) any later version. |
---|
9 | # |
---|
10 | # This 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 DotClear; if not, write to the Free Software |
---|
17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
18 | # |
---|
19 | # ***** END LICENSE BLOCK ***** |
---|
20 | |
---|
21 | require_once("Field.php"); |
---|
22 | require_once("FieldSet.php"); |
---|
23 | require_once("fields/TextField.php"); |
---|
24 | require_once("fields/SubmitField.php"); |
---|
25 | require_once("fields/ComboField.php"); |
---|
26 | require_once("fields/CheckBoxField.php"); |
---|
27 | require_once("fields/RadioButtonField.php"); |
---|
28 | |
---|
29 | require_once("TplCore.php"); |
---|
30 | require_once("Captcha.php"); |
---|
31 | require_once("TplFields.php"); |
---|
32 | |
---|
33 | require_once("TplEmail.php"); |
---|
34 | require_once("TplDatabase.php"); |
---|
35 | |
---|
36 | $core->url->register('formGet','form','^form/(.+)$',array('MyForms','formGet')); |
---|
37 | $core->url->register('formPost','form','^form$',array('MyForms','formPost')); |
---|
38 | |
---|
39 | class PostSimu |
---|
40 | { |
---|
41 | public function getURL() |
---|
42 | { |
---|
43 | return $_SERVER['REQUEST_URI']; |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | class MyForms extends dcUrlHandlers |
---|
48 | { |
---|
49 | public static $formID; |
---|
50 | private static $nextFormID; |
---|
51 | public static $events; |
---|
52 | private static $errors; |
---|
53 | private static $fields; |
---|
54 | private static $htmlOut; |
---|
55 | private static $formHTML; |
---|
56 | private static $formIsValid; |
---|
57 | private static $captchaIsValidated; |
---|
58 | private static $passwordProtected; |
---|
59 | |
---|
60 | public static function formGet($args) |
---|
61 | { |
---|
62 | self::$formID = $args; |
---|
63 | self::form(); |
---|
64 | } |
---|
65 | |
---|
66 | public static function formPost($args) |
---|
67 | { |
---|
68 | global $_REQUEST; |
---|
69 | self::$formID = @$_REQUEST["myforms"]["formID"]; |
---|
70 | self::form(); |
---|
71 | } |
---|
72 | |
---|
73 | public static function form() |
---|
74 | { |
---|
75 | global $core, $_REQUEST, $_ctx; |
---|
76 | |
---|
77 | // add all 'default-templates' folders to form search path |
---|
78 | $tplPath = $core->tpl->getPath(); |
---|
79 | $plugins = $core->plugins->getModules(); |
---|
80 | foreach($plugins as $plugin) |
---|
81 | array_push($tplPath, $plugin['root'].'/default-templates'); |
---|
82 | $core->tpl->setPath($tplPath); |
---|
83 | |
---|
84 | self::$passwordProtected = false; |
---|
85 | |
---|
86 | self::loadForm(); |
---|
87 | |
---|
88 | if(self::$passwordProtected) { |
---|
89 | $_ctx->posts = new PostSimu(); |
---|
90 | self::serveDocument('password-form.html','text/html',false); |
---|
91 | return; |
---|
92 | } |
---|
93 | |
---|
94 | // process form post |
---|
95 | if( isset($_REQUEST["myforms"]) && self::$captchaIsValidated && self::$formIsValid ) { |
---|
96 | self::$nextFormID = false; |
---|
97 | self::$errors = array(); |
---|
98 | $currentEventCallback = MyFormsTplCore::GetFunction('OnSubmit_'.self::getCurrentEvent()); |
---|
99 | ob_start(); |
---|
100 | $currentEventCallback(); |
---|
101 | self::$htmlOut = ob_get_clean(); |
---|
102 | if( self::$nextFormID ) { |
---|
103 | self::$formID = self::$nextFormID; |
---|
104 | self::loadForm(); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | // display current form page |
---|
109 | self::serveDocument('myforms.html'); |
---|
110 | } |
---|
111 | |
---|
112 | private static function loadForm() |
---|
113 | { |
---|
114 | global $core; |
---|
115 | $formTpl = self::$formID.'.myforms.html'; |
---|
116 | |
---|
117 | if (!$core->tpl->getFilePath($formTpl)) { |
---|
118 | header('Content-Type: text/plain; charset=UTF-8'); |
---|
119 | echo 'Unable to find template for form <'.self::$formID.'>.'; |
---|
120 | exit; |
---|
121 | } |
---|
122 | |
---|
123 | // include form template template which, in turn, defines 'myforms' functions |
---|
124 | self::$events = array(); |
---|
125 | //include $core->tpl->getFile($formTpl);exit; |
---|
126 | //print $core->tpl->getData($formTpl); exit; |
---|
127 | $core->tpl->getData($formTpl); |
---|
128 | |
---|
129 | // form is password protected |
---|
130 | if(self::$passwordProtected) { |
---|
131 | // Get passwords cookie |
---|
132 | if (isset($_COOKIE['dc_form_passwd'])) { |
---|
133 | $pwd_cookie = unserialize($_COOKIE['dc_form_passwd']); |
---|
134 | } else { |
---|
135 | $pwd_cookie = array(); |
---|
136 | } |
---|
137 | |
---|
138 | // Check for match |
---|
139 | if ((!empty($_POST['password']) && $_POST['password'] == self::$passwordProtected) |
---|
140 | || (isset($pwd_cookie[self::$formID]) && $pwd_cookie[self::$formID] == self::$passwordProtected)) { |
---|
141 | // store password in cookie and clear password protection |
---|
142 | $pwd_cookie[self::$formID] = self::$passwordProtected; |
---|
143 | setcookie('dc_form_passwd',serialize($pwd_cookie),0,'/'); |
---|
144 | self::$passwordProtected = false; |
---|
145 | } else { |
---|
146 | // incorrect password : no need to go further |
---|
147 | return; |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | // field declaration |
---|
152 | ob_start(); |
---|
153 | $declareFunction = MyFormsTplCore::GetFunction('Declare'); |
---|
154 | self::$fields = $declareFunction(); |
---|
155 | //print ob_get_clean();print_r(self::$fields);exit; |
---|
156 | ob_get_clean(); |
---|
157 | |
---|
158 | // field display and validation |
---|
159 | self::$formIsValid = true; |
---|
160 | self::$captchaIsValidated = true; |
---|
161 | ob_start(); |
---|
162 | $displayFunction = MyFormsTplCore::GetFunction('Display'); |
---|
163 | $displayFunction(); |
---|
164 | self::$htmlOut = ob_get_clean(); |
---|
165 | } |
---|
166 | |
---|
167 | public static function InvalidateForm() { |
---|
168 | self::$formIsValid = false; |
---|
169 | } |
---|
170 | |
---|
171 | public static function validateCaptcha() { |
---|
172 | global $_REQUEST; |
---|
173 | $captchaIsValid = !isset($_REQUEST["myforms"]) || MyFormsCaptcha::isValid($_REQUEST["myforms"]["captcha"],$_REQUEST["myforms"]["captcharef"]); |
---|
174 | self::$captchaIsValidated = self::$captchaIsValidated && $captchaIsValid; |
---|
175 | return $captchaIsValid; |
---|
176 | } |
---|
177 | |
---|
178 | public static function validateField($fieldName,$condition) { |
---|
179 | global $_REQUEST; |
---|
180 | $fieldIsValid = !isset($_REQUEST["myforms"]) || preg_match('#'.$condition.'#', @$_REQUEST["myforms"][$fieldName]); |
---|
181 | self::$allFieldsAreValidated = self::$allFieldsAreValidated && $fieldIsValid; |
---|
182 | return $fieldIsValid; |
---|
183 | } |
---|
184 | |
---|
185 | public static function checkQueryMatches($queryFilter) |
---|
186 | { |
---|
187 | if( !preg_match('#'.$queryFilter.'#', $_SERVER['REQUEST_METHOD']) ) |
---|
188 | self::p404(); |
---|
189 | } |
---|
190 | |
---|
191 | public static function checkBlogMatches($blogFilter) |
---|
192 | { |
---|
193 | global $core; |
---|
194 | if( !preg_match('#'.$blogFilter.'#', $core->blog->id) ) |
---|
195 | self::p404(); |
---|
196 | } |
---|
197 | |
---|
198 | public static function display() |
---|
199 | { |
---|
200 | print self::$htmlOut; |
---|
201 | } |
---|
202 | |
---|
203 | public static function password($pw) |
---|
204 | { |
---|
205 | self::$passwordProtected = $pw; |
---|
206 | } |
---|
207 | |
---|
208 | public static function info($name) |
---|
209 | { |
---|
210 | $infoFunction = MyFormsTplCore::GetFunction('Info_'.$name); |
---|
211 | $infoFunction(); |
---|
212 | } |
---|
213 | |
---|
214 | public static function registerEvent($name) |
---|
215 | { |
---|
216 | self::$events[] = $name; |
---|
217 | } |
---|
218 | |
---|
219 | private static function getCurrentEvent() |
---|
220 | { |
---|
221 | foreach( self::$events as $event ) |
---|
222 | if( isset($_REQUEST["myforms"][$event]) ) |
---|
223 | return $event; |
---|
224 | } |
---|
225 | |
---|
226 | public static function execute($returnedError) |
---|
227 | { |
---|
228 | if($returnedError) |
---|
229 | self::$errors[] = $returnedError; |
---|
230 | } |
---|
231 | |
---|
232 | public static function hasErrors() |
---|
233 | { |
---|
234 | return count(self::$errors) > 0; |
---|
235 | } |
---|
236 | |
---|
237 | public static function hasError($class,$message) |
---|
238 | { |
---|
239 | foreach( self::$errors as $error ) { |
---|
240 | $hasClass = false; |
---|
241 | $hasMessage = false; |
---|
242 | if( preg_match('#'.$class.'#', $error[0]) ) |
---|
243 | $hasClass = true; |
---|
244 | if( preg_match('#'.$message.'#', $error[1]) ) |
---|
245 | $hasMessage = true; |
---|
246 | if($hasClass && $hasMessage) |
---|
247 | return true; |
---|
248 | } |
---|
249 | return false; |
---|
250 | } |
---|
251 | |
---|
252 | public static function allErrors() |
---|
253 | { |
---|
254 | return self::$errors; |
---|
255 | } |
---|
256 | |
---|
257 | public static function goto($formID) |
---|
258 | { |
---|
259 | self::$nextFormID = $formID; |
---|
260 | } |
---|
261 | |
---|
262 | public static function getField($name) |
---|
263 | { |
---|
264 | return self::$fields->get($name); |
---|
265 | } |
---|
266 | |
---|
267 | /* |
---|
268 | public static function getFieldValue($name,$defaultValue) |
---|
269 | { |
---|
270 | global $_REQUEST; |
---|
271 | if(isset($_REQUEST["myforms"][$name])) { |
---|
272 | return $_REQUEST["myforms"][$name]; |
---|
273 | } else { |
---|
274 | return $defaultValue; |
---|
275 | } |
---|
276 | } |
---|
277 | */ |
---|
278 | |
---|
279 | public static function getFileFieldValue($name,$data) |
---|
280 | { |
---|
281 | global $_FILES; |
---|
282 | if(isset($_FILES["myforms"][$data][$name])) |
---|
283 | return $_FILES["myforms"][$data][$name]; |
---|
284 | else |
---|
285 | return ''; |
---|
286 | } |
---|
287 | |
---|
288 | } |
---|
289 | |
---|
290 | ?> |
---|