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