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:
116: if (getSystemProperty('debug', 'debug_for_plugins') == 'true') {
117: $cfg = cRegistry::getConfig();
118:
119: $log = new cLog(cLogWriter::factory('file', array(
120: 'destination' => $cfg['path']['contenido_logs'] . 'errorlog.txt'
121: )), cLog::ERR);
122:
123: $log->err($e->getMessage());
124: $log->err($e->getTraceAsString());
125: }
126: }
127:
128: 129: 130: 131: 132: 133:
134: public static function displayException(Exception $e, $showTrace = false) {
135: header($_SERVER['SERVER_PROTOCOL'] . ' 500 ' . self::i18n('INTERNAL_SERVER_ERROR'), true, 500);
136:
137: if (true) {
138:
139: $class = "ui-state-error";
140: $icon = "ui-icon-alert";
141: } else {
142:
143: $class = "ui-state-highlight";
144: $icon = "ui-icon-info";
145: }
146:
147: echo '<div class="ui-widget">';
148: echo '<div class="' . $class . ' ui-corner-all">';
149: echo '<p>';
150: echo '<span class="ui-icon ' . $icon . '"></span>';
151: echo $e->getMessage();
152: if (true === $showTrace) {
153: echo '<pre style="overflow: auto">';
154: echo htmlentities($e->getTraceAsString(), ENT_COMPAT | ENT_HTML401, 'UTF-8');
155: echo '</pre>';
156: }
157: echo '</p>';
158: echo '</div>';
159: echo '</div>';
160: }
161:
162: 163: 164: 165: 166: 167: 168:
169: public static function notifyException(Exception $e) {
170: $cGuiNotification = new cGuiNotification();
171: $level = cGuiNotification::LEVEL_ERROR;
172: $message = $e->getMessage();
173:
174: return $cGuiNotification->returnNotification($level, $message);
175: }
176:
177: 178: 179: 180: 181: 182: 183:
184: public static function getExtensionClasses($parentClass) {
185:
186:
187: if (false === ($handle = cDirHandler::read(self::getPath() . 'extensions/'))) {
188: return array();
189: }
190:
191: $extensionClasses = array();
192: foreach ($handle as $file) {
193:
194: $matches = array();
195: $matchCount = preg_match('/^class\.pifa\.([^\.]+)\.php$/', $file, $matches);
196:
197:
198: if (false === $matchCount) {
199: $msg = self::i18n('EXTENSION_REGEX_ERROR');
200: throw new PifaException($msg);
201: }
202:
203:
204: if (0 === $matchCount) {
205: continue;
206: }
207:
208:
209: $optionClass = self::toCamelCase($matches[1], true);
210:
211: include_once(self::getPath() . 'extensions/' . $file);
212:
213: $reflection = new ReflectionClass($optionClass);
214: if (false === $reflection->isSubclassOf($parentClass)) {
215: continue;
216: }
217:
218: $extensionClasses[] = array(
219: 'value' => $optionClass,
220: 'label' => $optionClass
221: );
222: }
223:
224: return $extensionClasses;
225: }
226:
227: 228: 229: 230: 231: 232: 233: 234:
235: public static function getTemplates($re = '/cms_pifaform_[^\.]+\.tpl/') {
236: $clientConfig = cRegistry::getClientConfig(cRegistry::getClientId());
237:
238:
239: if (false === ($handle = cDirHandler::read($clientConfig['template']['path']))) {
240: return array();
241: }
242:
243: $templates = array();
244: foreach ($handle as $file) {
245:
246:
247: if (true === is_dir($file)) {
248: continue;
249: }
250:
251:
252: $matches = array();
253: $matchCount = preg_match($re, $file, $matches);
254:
255:
256: if (false === $matchCount) {
257: $msg = self::i18n('TEMPLATE_REGEX_ERROR');
258: throw new PifaException($msg);
259: }
260:
261:
262: if (0 === $matchCount) {
263: continue;
264: }
265:
266: $templates[] = array(
267: 'value' => $file,
268: 'label' => $file
269: );
270: }
271:
272: return $templates;
273: }
274:
275:
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: public static function fromCamelCase($str) {
307: $str[0] = strtolower($str[0]);
308: $func = create_function('$c', 'return "_" . strtolower($c[1]);');
309: return preg_replace_callback('/([A-Z])/', $func, $str);
310: }
311:
312: 313: 314: 315: 316: 317: 318: 319: 320: 321:
322: public static function toCamelCase($str, $capitalise_first_char = false) {
323: if ($capitalise_first_char) {
324: $str[0] = strtoupper($str[0]);
325: }
326: $func = create_function('$c', 'return strtoupper($c[1]);');
327: return preg_replace_callback('/_([a-z])/', $func, $str);
328: }
329:
330: 331: 332: 333: 334: 335: 336: 337: 338: 339:
340: public static function getTimestampSetting($force = false) {
341: if (is_null(self::$_timestampSetting) || $force) {
342: self::$_timestampSetting = getEffectiveSetting('pifa', 'timestamp', self::TIMESTAMP_ALWAYS);
343: if (!in_array(self::$_timestampSetting, array(
344: self::TIMESTAMP_NEVER,
345: self::TIMESTAMP_BYFORM,
346: self::TIMESTAMP_ALWAYS
347: ))) {
348: self::$_timestampSetting = self::TIMESTAMP_ALWAYS;
349: }
350: }
351: return self::$_timestampSetting;
352: }
353:
354: 355: 356: 357: 358:
359: public static function isHttps() {
360: $isHttps = false;
361: $isHttps |= 443 === $_SERVER['SERVER_PORT'];
362: $isHttps |= array_key_exists('HTTP_X_SSL_CIPHER', $_SERVER);
363: return $isHttps;
364: }
365: }
366:
367:
368: $cfg['plugins'][Pifa::getName()] = Pifa::getPath();
369:
370:
371:
372:
373: $cfg['templates']['pifa_right_bottom_form'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.right_bottom_form.tpl';
374: $cfg['templates']['pifa_right_bottom_fields'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.right_bottom_fields.tpl';
375: $cfg['templates']['pifa_right_bottom_data'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.right_bottom_data.tpl';
376: $cfg['templates']['pifa_right_bottom_export'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.right_bottom_export.tpl';
377: $cfg['templates']['pifa_right_bottom_import'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.right_bottom_import.tpl';
378: $cfg['templates']['pifa_ajax_field_form'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.ajax_field_form.tpl';
379: $cfg['templates']['pifa_ajax_field_row'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.ajax_field_row.tpl';
380: $cfg['templates']['pifa_ajax_option_row'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.ajax_option_row.tpl';
381:
382:
383: $cfg['tab']['pifa_form'] = $cfg['sql']['sqlprefix'] . '_pifa_form';
384: $cfg['tab']['pifa_field'] = $cfg['sql']['sqlprefix'] . '_pifa_field';
385:
386:
387: global $lngAct;
388: $lngAct['form']['pifa_show_form'] = Pifa::i18n('pifa_show_form');
389: $lngAct['form']['pifa_store_form'] = Pifa::i18n('pifa_store_form');
390: $lngAct['form']['pifa_delete_form'] = Pifa::i18n('pifa_delete_form');
391: $lngAct['form_fields']['pifa_show_fields'] = Pifa::i18n('pifa_show_fields');
392: $lngAct['form_data']['pifa_show_data'] = Pifa::i18n('pifa_show_data');
393: $lngAct['form_import']['pifa_import_form'] = Pifa::i18n('pifa_import_form');
394: $lngAct['form_ajax']['pifa_export_form'] = Pifa::i18n('pifa_export_form');
395: $lngAct['form_ajax']['pifa_get_field_form'] = Pifa::i18n('pifa_get_field_form');
396: $lngAct['form_ajax']['pifa_post_field_form'] = Pifa::i18n('pifa_post_field_form');
397: $lngAct['form_ajax']['pifa_reorder_fields'] = Pifa::i18n('pifa_reorder_fields');
398: $lngAct['form_ajax']['pifa_export_data'] = Pifa::i18n('pifa_export_data');
399: $lngAct['form_ajax']['pifa_get_file'] = Pifa::i18n('pifa_get_file');
400: $lngAct['form_ajax']['pifa_delete_field'] = Pifa::i18n('pifa_delete_field');
401: $lngAct['form_ajax']['pifa_get_option_row'] = Pifa::i18n('pifa_get_option_row');
402:
403:
404:
405: $pluginClassPath = 'contenido/plugins/' . Pifa::getName() . '/';
406: cAutoload::addClassmapConfig(array(
407: 'cContentTypePifaForm' => $pluginClassPath . 'classes/class.content.type.pifa_form.php',
408: 'PifaExternalOptionsDatasourceInterface' => $pluginClassPath . 'classes/class.pifa.external_options_datasource_interface.php',
409: 'PifaExporter' => $pluginClassPath . 'classes/class.pifa.exporter.php',
410: 'PifaImporter' => $pluginClassPath . 'classes/class.pifa.importer.php',
411: 'PifaLeftBottomPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
412: 'PifaRightBottomFormPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
413: 'PifaRightBottomFormFieldsPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
414: 'PifaRightBottomFormDataPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
415: 'PifaRightBottomFormExportPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
416: 'PifaRightBottomFormImportPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
417: 'PifaFormCollection' => $pluginClassPath . 'classes/class.pifa.form.php',
418: 'PifaForm' => $pluginClassPath . 'classes/class.pifa.form.php',
419: 'PifaFieldCollection' => $pluginClassPath . 'classes/class.pifa.field.php',
420: 'PifaField' => $pluginClassPath . 'classes/class.pifa.field.php',
421: 'PifaAbstractFormModule' => $pluginClassPath . 'classes/class.pifa.abstract_form_module.php',
422: 'PifaAbstractFormProcessor' => $pluginClassPath . 'classes/class.pifa.abstract_form_processor.php',
423: 'PifaAjaxHandler' => $pluginClassPath . 'classes/class.pifa.ajax_handler.php',
424: 'PifaException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
425: 'PifaDatabaseException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
426: 'PifaNotImplementedException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
427: 'PifaIllegalStateException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
428:
429: 'PifaNotYetStoredException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
430: 'PifaValidationException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
431: 'PifaMailException' => $pluginClassPath . 'classes/class.pifa.exceptions.php'
432: ));
433: unset($pluginClassPath);
434:
435:
436:
437: