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