Dotclear

source: plugins/myForms/trunk/_public.php @ 2108

Revision 2108, 7.4 KB checked in by Oaz, 14 years ago (diff)

fixed multipart email bug + added DB content display

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

Sites map