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