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 === ($handle = cDirHandler::read(self::getPath() . 'extensions/'))) {
185:             return array();
186:         }
187: 
188:         $extensionClasses = array();
189:         foreach ($handle as $file) {
190:             
191:             $matches = array();
192:             $matchCount = preg_match('/^class\.pifa\.([^\.]+)\.php$/', $file, $matches);
193: 
194:             
195:             if (false === $matchCount) {
196:                 $msg = self::i18n('EXTENSION_REGEX_ERROR');
197:                 throw new PifaException($msg);
198:             }
199: 
200:             
201:             if (0 === $matchCount) {
202:                 continue;
203:             }
204: 
205:             
206:             $optionClass = self::toCamelCase($matches[1], true);
207: 
208:             include_once(self::getPath() . 'extensions/' . $file);
209: 
210:             $reflection = new ReflectionClass($optionClass);
211:             if (false === $reflection->isSubclassOf($parentClass)) {
212:                 continue;
213:             }
214: 
215:             $extensionClasses[] = array(
216:                     'value' => $optionClass,
217:                     'label' => $optionClass
218:             );
219:         }
220: 
221:         return $extensionClasses;
222:     }
223: 
224:     225: 226: 227: 228: 229: 230: 231: 
232:     public static function getTemplates($re = '/cms_pifaform_[^\.]+\.tpl/') {
233:         $clientConfig = cRegistry::getClientConfig(cRegistry::getClientId());
234: 
235:         
236:         if (false === ($handle = cDirHandler::read($clientConfig['template']['path']))) {
237:             return array();
238:         }
239: 
240:         $templates = array();
241:         foreach ($handle as $file) {
242: 
243:             
244:             if (true === is_dir($file)) {
245:                 continue;
246:             }
247: 
248:             
249:             $matches = array();
250:             $matchCount = preg_match($re, $file, $matches);
251: 
252:             
253:             if (false === $matchCount) {
254:                 $msg = self::i18n('TEMPLATE_REGEX_ERROR');
255:                 throw new PifaException($msg);
256:             }
257: 
258:             
259:             if (0 === $matchCount) {
260:                 continue;
261:             }
262: 
263:             $templates[] = array(
264:                     'value' => $file,
265:                     'label' => $file
266:             );
267:         }
268: 
269:         return $templates;
270:     }
271: 
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:     public static function fromCamelCase($str) {
304:         $str[0] = strtolower($str[0]);
305:         $func = create_function('$c', 'return "_" . strtolower($c[1]);');
306:         return preg_replace_callback('/([A-Z])/', $func, $str);
307:     }
308: 
309:     310: 311: 312: 313: 314: 315: 316: 317: 318: 
319:     public static function toCamelCase($str, $capitalise_first_char = false) {
320:         if ($capitalise_first_char) {
321:             $str[0] = strtoupper($str[0]);
322:         }
323:         $func = create_function('$c', 'return strtoupper($c[1]);');
324:         return preg_replace_callback('/_([a-z])/', $func, $str);
325:     }
326: 
327:     328: 329: 330: 331: 332: 333: 334: 335: 336: 
337:     public static function getTimestampSetting($force = false) {
338:         if (is_null(self::$_timestampSetting) || $force) {
339:             self::$_timestampSetting = getEffectiveSetting('pifa', 'timestamp', self::TIMESTAMP_ALWAYS);
340:             if (!in_array(self::$_timestampSetting, array(
341:                 self::TIMESTAMP_NEVER,
342:                 self::TIMESTAMP_BYFORM,
343:                 self::TIMESTAMP_ALWAYS
344:             ))) {
345:                 self::$_timestampSetting = self::TIMESTAMP_ALWAYS;
346:             }
347:         }
348:         return self::$_timestampSetting;
349:     }
350: 
351:     352: 353: 354: 355: 
356:     public static function isHttps() {
357:         $isHttps = false;
358:         $isHttps |= 443 === $_SERVER['SERVER_PORT'];
359:         $isHttps |= array_key_exists('HTTP_X_SSL_CIPHER', $_SERVER);
360:         return $isHttps;
361:     }
362: }
363: 
364: 
365: $cfg['plugins'][Pifa::getName()] = Pifa::getPath();
366: 
367: 
368: 
369: 
370: $cfg['templates']['pifa_right_bottom_form'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.right_bottom_form.tpl';
371: $cfg['templates']['pifa_right_bottom_fields'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.right_bottom_fields.tpl';
372: $cfg['templates']['pifa_right_bottom_data'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.right_bottom_data.tpl';
373: $cfg['templates']['pifa_right_bottom_export'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.right_bottom_export.tpl';
374: $cfg['templates']['pifa_right_bottom_import'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.right_bottom_import.tpl';
375: $cfg['templates']['pifa_ajax_field_form'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.ajax_field_form.tpl';
376: $cfg['templates']['pifa_ajax_field_row'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.ajax_field_row.tpl';
377: $cfg['templates']['pifa_ajax_option_row'] = $cfg['plugins'][Pifa::getName()] . 'templates/template.ajax_option_row.tpl';
378: 
379: 
380: $cfg['tab']['pifa_form'] = $cfg['sql']['sqlprefix'] . '_pifa_form';
381: $cfg['tab']['pifa_field'] = $cfg['sql']['sqlprefix'] . '_pifa_field';
382: 
383: 
384: global $lngAct;
385: $lngAct['form']['pifa_show_form'] = Pifa::i18n('pifa_show_form');
386: $lngAct['form']['pifa_store_form'] = Pifa::i18n('pifa_store_form');
387: $lngAct['form']['pifa_delete_form'] = Pifa::i18n('pifa_delete_form');
388: $lngAct['form_fields']['pifa_show_fields'] = Pifa::i18n('pifa_show_fields');
389: $lngAct['form_data']['pifa_show_data'] = Pifa::i18n('pifa_show_data');
390: $lngAct['form_import']['pifa_import_form'] = Pifa::i18n('pifa_import_form');
391: $lngAct['form_ajax']['pifa_export_form'] = Pifa::i18n('pifa_export_form');
392: $lngAct['form_ajax']['pifa_get_field_form'] = Pifa::i18n('pifa_get_field_form');
393: $lngAct['form_ajax']['pifa_post_field_form'] = Pifa::i18n('pifa_post_field_form');
394: $lngAct['form_ajax']['pifa_reorder_fields'] = Pifa::i18n('pifa_reorder_fields');
395: $lngAct['form_ajax']['pifa_export_data'] = Pifa::i18n('pifa_export_data');
396: $lngAct['form_ajax']['pifa_get_file'] = Pifa::i18n('pifa_get_file');
397: $lngAct['form_ajax']['pifa_delete_field'] = Pifa::i18n('pifa_delete_field');
398: $lngAct['form_ajax']['pifa_get_option_row'] = Pifa::i18n('pifa_get_option_row');
399: 
400: 
401: 
402: $pluginClassPath = 'contenido/plugins/' . Pifa::getName() . '/';
403: cAutoload::addClassmapConfig(array(
404:     'cContentTypePifaForm' => $pluginClassPath . 'classes/class.content.type.pifa_form.php',
405:     'PifaExternalOptionsDatasourceInterface' => $pluginClassPath . 'classes/class.pifa.external_options_datasource_interface.php',
406:     'PifaExporter' => $pluginClassPath . 'classes/class.pifa.exporter.php',
407:     'PifaImporter' => $pluginClassPath . 'classes/class.pifa.importer.php',
408:     'PifaLeftBottomPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
409:     'PifaRightBottomFormPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
410:     'PifaRightBottomFormFieldsPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
411:     'PifaRightBottomFormDataPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
412:     'PifaRightBottomFormExportPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
413:     'PifaRightBottomFormImportPage' => $pluginClassPath . 'classes/class.pifa.gui.php',
414:     'PifaFormCollection' => $pluginClassPath . 'classes/class.pifa.form.php',
415:     'PifaForm' => $pluginClassPath . 'classes/class.pifa.form.php',
416:     'PifaFieldCollection' => $pluginClassPath . 'classes/class.pifa.field.php',
417:     'PifaField' => $pluginClassPath . 'classes/class.pifa.field.php',
418:     'PifaAbstractFormModule' => $pluginClassPath . 'classes/class.pifa.abstract_form_module.php',
419:     'PifaAbstractFormProcessor' => $pluginClassPath . 'classes/class.pifa.abstract_form_processor.php',
420:     'PifaAjaxHandler' => $pluginClassPath . 'classes/class.pifa.ajax_handler.php',
421:     'PifaException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
422:     'PifaDatabaseException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
423:     'PifaNotImplementedException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
424:     'PifaIllegalStateException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
425:     
426:     'PifaNotYetStoredException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
427:     'PifaValidationException' => $pluginClassPath . 'classes/class.pifa.exceptions.php',
428:     'PifaMailException' => $pluginClassPath . 'classes/class.pifa.exceptions.php'
429: ));
430: unset($pluginClassPath);
431: 
432: 
433: 
434: