1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: |
|
15: |
|
16: | namespace LucidFrame\Core;
|
17: |
|
18: | |
19: | |
20: |
|
21: | class Form
|
22: | {
|
23: |
|
24: | private static $id;
|
25: |
|
26: | private static $error = array();
|
27: |
|
28: | private static $success = false;
|
29: |
|
30: | private static $message = '';
|
31: |
|
32: | private static $redirect = '';
|
33: |
|
34: | private static $callback = '';
|
35: |
|
36: | |
37: | |
38: |
|
39: | public static function init()
|
40: | {
|
41: | self::$id = '';
|
42: | self::$error = array();
|
43: | self::$success = false;
|
44: | self::$message = '';
|
45: | self::$redirect = '';
|
46: | self::$callback = '';
|
47: | }
|
48: |
|
49: | |
50: | |
51: | |
52: | |
53: | |
54: |
|
55: | public static function set($key, $value = '')
|
56: | {
|
57: | self::$$key = $value;
|
58: | }
|
59: |
|
60: | |
61: | |
62: | |
63: | |
64: | |
65: |
|
66: | public static function get($key, $value = null)
|
67: | {
|
68: | if (isset(self::$$key)) {
|
69: | return self::$$key;
|
70: | }
|
71: |
|
72: | return $value;
|
73: | }
|
74: |
|
75: | |
76: | |
77: | |
78: |
|
79: | public static function token()
|
80: | {
|
81: | $token = _encrypt(time());
|
82: | session_set(_cfg('formTokenName'), $token);
|
83: | echo '<input type="hidden" name="lc_formToken_' . _cfg('formTokenName') . '" value="' . $token . '" />';
|
84: | }
|
85: |
|
86: | |
87: | |
88: | |
89: | |
90: | |
91: |
|
92: | public static function validate($validations = null, $data = [])
|
93: | {
|
94: | if (!isset($_POST['lc_formToken_' . _cfg('formTokenName')])) {
|
95: | Validation::addError('', _t('Invalid form token.'));
|
96: | return false;
|
97: | }
|
98: |
|
99: | $token = _decrypt(session_get(_cfg('formTokenName')));
|
100: | $postedToken = _decrypt(_post('lc_formToken_'._cfg('formTokenName')));
|
101: | $result = false;
|
102: |
|
103: | if ($token == $postedToken) {
|
104: |
|
105: | if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] && _cfg('siteDomain')) {
|
106: | $siteDomain = _cfg('siteDomain');
|
107: | $siteDomain = preg_replace('/^www\./', '', $siteDomain);
|
108: | $parsedURL = parse_url($_SERVER['HTTP_REFERER']);
|
109: | $parsedURL['host'] = preg_replace('/^www\./', '', $parsedURL['host']);
|
110: | if (strcasecmp($siteDomain, $parsedURL['host']) == 0) {
|
111: | $result = true;
|
112: | }
|
113: | }
|
114: | }
|
115: |
|
116: | if (!$result) {
|
117: | Validation::addError('', _t('Error occurred during form submission. Please refresh the page to try again.'));
|
118: | return false;
|
119: | }
|
120: |
|
121: | if ($validations && Validation::check($validations, $data) === false) {
|
122: | return false;
|
123: | }
|
124: |
|
125: | return true;
|
126: | }
|
127: |
|
128: | |
129: | |
130: | |
131: | |
132: | |
133: | |
134: |
|
135: | public static function respond($formId, $errors = null, $forceJson = false)
|
136: | {
|
137: | self::$id = $formId;
|
138: | self::$error = validation_get('errors');
|
139: | $ajaxResponse = $errors === null;
|
140: |
|
141: | if (is_array($errors) && count($errors)) {
|
142: | self::$error = $errors;
|
143: | $ajaxResponse = false;
|
144: |
|
145: | if (count(self::$error) == 0 && empty(self::$message)) {
|
146: | return;
|
147: | }
|
148: | }
|
149: |
|
150: | $response = array(
|
151: | 'formId' => self::$id,
|
152: | 'success' => self::$success ? true : false,
|
153: | 'error' => self::$error,
|
154: | 'msg' => self::$message,
|
155: | 'redirect' => self::$redirect,
|
156: | 'callback' => self::$callback
|
157: | );
|
158: |
|
159: | if ($ajaxResponse) {
|
160: | if ($forceJson) {
|
161: | _json($response);
|
162: | } else {
|
163: | echo json_encode($response);
|
164: | }
|
165: | } else {
|
166: | echo '<script type="text/javascript">';
|
167: | echo 'LC.Form.submitHandler(' . json_encode($response) . ')';
|
168: | echo '</script>';
|
169: | }
|
170: | }
|
171: |
|
172: | |
173: | |
174: | |
175: | |
176: | |
177: | |
178: | |
179: | |
180: |
|
181: | public static function value($name, $defaultValue = null)
|
182: | {
|
183: | $value = _post($name);
|
184: |
|
185: | return $value ? _h($value) : _h($defaultValue);
|
186: | }
|
187: |
|
188: | |
189: | |
190: | |
191: | |
192: | |
193: | |
194: | |
195: | |
196: |
|
197: | public static function htmlValue($name, $defaultValue = null)
|
198: | {
|
199: | if (count($_POST)) {
|
200: | if (!isset($_POST[$name])) {
|
201: | return '';
|
202: | }
|
203: | $value = _xss($_POST[$name]);
|
204: |
|
205: | return _h($value);
|
206: | }
|
207: |
|
208: | return _h($defaultValue);
|
209: | }
|
210: |
|
211: | |
212: | |
213: | |
214: | |
215: | |
216: | |
217: | |
218: | |
219: |
|
220: | public static function selected($name, $value, $defaultValue = null)
|
221: | {
|
222: | return self::inputSelection($name, $value, $defaultValue) ? 'selected="selected"' : '';
|
223: | }
|
224: |
|
225: | |
226: | |
227: | |
228: | |
229: | |
230: | |
231: | |
232: | |
233: |
|
234: | public static function checked($name, $value, $defaultValue = null)
|
235: | {
|
236: | return self::inputSelection($name, $value, $defaultValue) ? 'checked="checked"' : '';
|
237: | }
|
238: |
|
239: | |
240: | |
241: | |
242: | |
243: | |
244: | |
245: | |
246: | |
247: | |
248: | |
249: | |
250: |
|
251: | public static function inputSelection($name, $value, $defaultValue = null)
|
252: | {
|
253: | if (count($_POST)) {
|
254: | $name = preg_replace('/(\[\])$/', '', $name);
|
255: | if (!isset($_POST[$name])) {
|
256: | return '';
|
257: | }
|
258: | $postedValue = _post($name);
|
259: | if (is_array($postedValue) && in_array($value, $postedValue)) {
|
260: | return true;
|
261: | } elseif ($value == $postedValue) {
|
262: | return true;
|
263: | } else {
|
264: | return false;
|
265: | }
|
266: | } else {
|
267: | if (is_array($defaultValue) && in_array($value, $defaultValue)) {
|
268: | return true;
|
269: | } elseif ($value == $defaultValue) {
|
270: | return true;
|
271: | } else {
|
272: | return false;
|
273: | }
|
274: | }
|
275: | }
|
276: | }
|
277: | |