1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: 17: 18: 19: 20:
21: class Pifa {
22:
23: 24: 25: 26: 27:
28: const TIMESTAMP_NEVER = 'never';
29:
30: const TIMESTAMP_BYFORM = 'byform';
31:
32: const TIMESTAMP_ALWAYS = 'always';
33:
34: 35: 36: 37: 38:
39: private static $_name = 'form_assistant';
40:
41: 42: 43: 44:
45: private static $_timestampSetting = NULL;
46:
47: 48:
49: public static function getName() {
50: return self::$_name;
51: }
52:
53: 54: 55: 56: 57:
58: public static function getPath() {
59: $cfg = cRegistry::getConfig();
60:
61: $path = cRegistry::getBackendPath() . $cfg['path']['plugins'];
62: $path .= self::$_name . '/';
63:
64: return $path;
65: }
66:
67: 68: 69: 70: 71:
72: public static function getUrl() {
73: $cfg = cRegistry::getConfig();
74:
75: $path = cRegistry::getBackendUrl() . $cfg['path']['plugins'];
76: $path .= self::$_name . '/';
77:
78: return $path;
79: }
80:
81: 82: 83: 84: 85:
86: public static function i18n($key) {
87: $trans = i18n($key, self::$_name);
88: return $trans;
89: }
90:
91: 92: 93: 94: 95:
96: public static function getNote($level, $note) {
97: $note = self::i18n($note);
98: $notification = new cGuiNotification();
99: return $notification->returnNotification($level, $note);
100: }
101:
102: 103: 104: 105:
106: public static function getError($note) {
107: return self::getNote(cGuiNotification::LEVEL_ERROR, $note);
108: }
109:
110: 111: 112: 113:
114: public static function logException(Exception $e) {
115: $cfg = cRegistry::getConfig();
116:
117: $log = new cLog(cLogWriter::factory('file', array(
118: 'destination' => $cfg['path']['contenido_logs'] . 'errorlog.txt'
119: )), cLog::ERR);
120:
121: $log->err($e->getMessage());
122: $log->err($e->getTraceAsString());
123: }
124:
125: 126: 127: 128: 129: 130:
131: public static function displayException(Exception $e, $showTrace = false) {
132: header($_SERVER['SERVER_PROTOCOL'] . ' 500 ' . self::i18n('INTERNAL_SERVER_ERROR'), true, 500);
133:
134: if (true) {
135:
136: $class = "ui-state-error";
137: $icon = "ui-icon-alert";
138: } else {
139:
140: $class = "ui-state-highlight";
141: $icon = "ui-icon-info";
142: }
143:
144: echo '<div class="ui-widget">';
145: echo '<div class="' . $class . ' ui-corner-all">';
146: echo '<p>';
147: echo '<span class="ui-icon ' . $icon . '"></span>';
148: echo $e->getMessage();
149: if (true === $showTrace) {
150: echo '<pre style="overflow: auto">';
151: echo htmlentities($e->getTraceAsString(), ENT_COMPAT | ENT_HTML401, 'UTF-8');
152: echo '</pre>';
153: }
154: echo '</p>';
155: echo '</div>';
156: echo '</div>';
157: }
158:
159: 160: 161: 162: 163: 164: 165:
166: public static function notifyException(Exception $e) {
167: $cGuiNotification = new cGuiNotification();
168: $level = cGuiNotification::LEVEL_ERROR;
169: $message = $e->getMessage();
170:
171: return $cGuiNotification->returnNotification($level, $message);
172: }
173:
174: 175: 176: 177: 178: 179: 180:
181: public static function getExtensionClasses($parentClass) {
182:
183:
184: if (false === $dh = opendir(self::getPath() . 'extensions/')) {
185: return array();
186: }
187:
188: $extensionClasses = array();
189: while (false !== $file = readdir($dh)) {
190:
191:
192: if (true === is_dir($file)) {
193: continue;
194: }
195:
196:
197: $matches = array();
198: $matchCount = preg_match('/class\.pifa\.([^\.]+)\.php/', $file, $matches);
199:
200:
201: if (false === $matchCount) {
202: $msg = self::i18n('EXTENSION_REGEX_ERROR');
203: throw new PifaException($msg);
204: }
205:
206:
207: if (0 === $matchCount) {
208: continue;
209: }
210:
211:
212: $optionClass = self::toCamelCase($matches[1], true);
213:
214: include_once self::getPath() . 'extensions/' . $file;
215:
216: $reflection = new ReflectionClass($optionClass);
217: if (false === $reflection->isSubclassOf($parentClass)) {
218: continue;
219: }
220:
221: $extensionClasses[] = array(
222: 'value' => $optionClass,
223: 'label' => $optionClass
224: );
225: }
226:
227: return $extensionClasses;
228: }
229:
230: 231: 232: 233: 234: 235: 236: 237:
238: public static function getTemplates($re = '/cms_pifaform_[^\.]+\.tpl/') {
239: $clientConfig = cRegistry::getClientConfig(cRegistry::getClientId());
240:
241:
242: if (false === $dh = opendir($clientConfig['template']['path'])) {
243: return array();
244: }
245:
246: $templates = array();
247: while (false !== $file = readdir($dh)) {
248:
249:
250: if (true === is_dir($file)) {
251: continue;
252: }
253:
254:
255: $matches = array();
256: $matchCount = preg_match($re, $file, $matches);
257:
258:
259: if (false === $matchCount) {
260: $msg = self::i18n('TEMPLATE_REGEX_ERROR');
261: throw new PifaException($msg);
262: }
263:
264:
265: if (0 === $matchCount) {
266: continue;
267: }
268:
269: $templates[] = array(
270: 'value' => $file,
271: 'label' => $file
272: );
273: }
274:
275: return $templates;
276: }
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300: 301: 302: 303: 304: 305: 306: 307: 308:
309: public static function fromCamelCase($str) {
310: $str[0] = strtolower($str[0]);
311: $func = create_function('$c', 'return "_" . strtolower($c[1]);');
312: return preg_replace_callback('/([A-Z])/', $func, $str);
313: }
314:
315: 316: 317: 318: 319: 320: 321: 322: 323: 324:
325: public static function toCamelCase($str, $capitalise_first_char = false) {
326: if ($capitalise_first_char) {
327: $str[0] = strtoupper($str[0]);
328: }
329: $func = create_function('$c', 'return strtoupper($c[1]);');
330: return preg_replace_callback('/_([a-z])/', $func, $str);
331: }
332:
333: 334: 335: 336: 337: 338: 339: 340: 341: 342:
343: public static function getTimestampSetting($force = false) {
344: if (is_null(self::$_timestampSetting) || $force) {
345: self::$_timestampSetting = getEffectiveSetting('pifa', 'timestamp', self::TIMESTAMP_ALWAYS);
346: if (!in_array(self::$_timestampSetting, array(
347: self::TIMESTAMP_NEVER,
348: self::TIMESTAMP_BYFORM,
349: self::TIMESTAMP_ALWAYS
350: ))) {
351: self::$_timestampSetting = self::TIMESTAMP_ALWAYS;
352: }
353: }
354: return self::$_timestampSetting;
355: }
356:
357: 358: 359: 360: 361:
362: public static function isHttps() {
363: $isHttps = false;
364: $isHttps |= 443 === $_SERVER['SERVER_PORT'];
365: $isHttps |= array_key_exists('HTTP_X_SSL_CIPHER', $_SERVER);
366: return $isHttps;
367: }
368: }
369:
370:
371: $cfg['plugins'][Pifa::getName()] = Pifa::getPath();
372:
373:
374:
375:
376: $cfg['templates']['pifa_right_bottom_form'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.right_bottom_form.tpl';
377: $cfg['templates']['pifa_right_bottom_fields'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.right_bottom_fields.tpl';
378: $cfg['templates']['pifa_right_bottom_data'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.right_bottom_data.tpl';
379: $cfg['templates']['pifa_ajax_field_form'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.ajax_field_form.tpl';
380: $cfg['templates']['pifa_ajax_field_row'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.ajax_field_row.tpl';
381: $cfg['templates']['pifa_ajax_option_row'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.ajax_option_row.tpl';
382:
383:
384: $cfg['tab']['pifa_form'] = $cfg['sql']['sqlprefix'] . '_pifa_form';
385: $cfg['tab']['pifa_field'] = $cfg['sql']['sqlprefix'] . '_pifa_field';
386:
387:
388: global $lngAct;
389: $lngAct['form']['pifa_show_form'] = Pifa::i18n('pifa_show_form');
390: $lngAct['form']['pifa_store_form'] = Pifa::i18n('pifa_store_form');
391: $lngAct['form']['pifa_delete_form'] = Pifa::i18n('pifa_delete_form');
392: $lngAct['form_fields']['pifa_show_fields'] = Pifa::i18n('pifa_show_fields');
393: $lngAct['form_data']['pifa_show_data'] = Pifa::i18n('pifa_show_data');
394: $lngAct['form_ajax']['pifa_get_field_form'] = Pifa::i18n('pifa_get_field_form');
395: $lngAct['form_ajax']['pifa_post_field_form'] = Pifa::i18n('pifa_post_field_form');
396: $lngAct['form_ajax']['pifa_reorder_fields'] = Pifa::i18n('pifa_reorder_fields');
397: $lngAct['form_ajax']['pifa_export_data'] = Pifa::i18n('pifa_export_data');
398: $lngAct['form_ajax']['pifa_get_file'] = Pifa::i18n('pifa_get_file');
399: $lngAct['form_ajax']['pifa_delete_field'] = Pifa::i18n('pifa_delete_field');
400: $lngAct['form_ajax']['pifa_get_option_row'] = Pifa::i18n('pifa_get_option_row');
401:
402:
403:
404: $pluginClassPath = 'contenido/plugins/' . Pifa::getName() . '/';
405: cAutoload::addClassmapConfig(array(
406: 'cContentTypePifaForm' => $pluginClassPath . 'classes/class.content.type.pifa_form.php',
407: 'PifaLeftBottomPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
408: 'PifaRightBottomFormPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
409: 'PifaRightBottomFormFieldsPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
410: 'PifaRightBottomFormDataPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
411: 'PifaFormCollection' => $pluginClassPath . 'classes/class.pifa.form.php',
412: 'PifaForm' => $pluginClassPath . 'classes/class.pifa.form.php',
413: 'PifaFieldCollection' => $pluginClassPath . 'classes/class.pifa.field.php',
414: 'PifaField' => $pluginClassPath . 'classes/class.pifa.field.php',
415: 'PifaAbstractFormModule' => $pluginClassPath . 'classes/class.pifa.abstract_form_module.php',
416: 'PifaAbstractFormProcessor' => $pluginClassPath . 'classes/class.pifa.abstract_form_processor.php',
417: 'PifaAjaxHandler' => $pluginClassPath . 'classes/class.pifa.ajax_handler.php',
418: 'PifaException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
419: 'PifaDatabaseException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
420: 'PifaExternalOptionsDatasourceInterface' => $pluginClassPath . 'classes/class.pifa.external_options_datasource_interface.php',
421: 'PifaNotImplementedException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
422: 'PifaIllegalStateException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
423:
424: 'PifaNotYetStoredException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
425: 'PifaValidationException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
426: 'PifaMailException' => $pluginClassPath . 'classes/class.pifa.exceptions.php'
427: ));
428: unset($pluginClassPath);
429:
430:
431:
432: