/FormCreator.php
PHP | 777 lines | 479 code | 65 blank | 233 comment | 74 complexity | 78d532f9231c928dfc90a86cf7e176a1 MD5 | raw file
- <?php
- /**
- * AfroSoft Form Manager
- *
- * @author AfroSoft
- * @version @build.version@ (Build @build.number@)
- * @license http://www.mozilla.org/MPL/MPL-1.1.txt MPL 1.1
- * @copyright Copyright (c) AfroSoft
- *
- /* * **** BEGIN LICENSE BLOCK *****
- * Version: MPL 1.1
- *
- * The contents of this file are subject to the Mozilla Public License Version
- * 1.1 (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS" basis,
- * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- * for the specific language governing rights and limitations under the
- * License.
- *
- * The Original Code is the AfroSoft Form Manager.
- *
- * The Initial Developer of the Original Code is AfroSoft.
- * Portions created by the Initial Developer are Copyright (C) 2011 the Initial
- * Developer. All Rights Reserved.
- *
- * Contributors are provided in the CONTRIB file, located at the root of the package.
- * ***** END LICENSE BLOCK ***** */
- /**
- * Constants defining the different fields available.
- */
- class FormField {
- // regular input
- const TEXT = 0;
- const PASSWORD = 1;
- const FILE = 2;
- const RADIO = 3;
- const CHECKBOX = 4;
- const HIDDEN = 5;
- const EMAIL = 14;
- const TEL = 15;
- const URL = 16;
- const SEARCH = 20;
- const NUMBER = 19;
- const DATE = 17;
- const TIME = 18;
- const DATETIME = 21;
- const WEEK = 22;
- const MONTH = 23;
- const RANGE = 24;
- const COLOR = 25;
-
- // other
- const TEXTAREA = 7;
- const LABEL = 8;
- const SELECT = 13;
-
- // buttons
- const IMAGE = 6;
- const SUBMIT = 9;
- const RESET = 10;
- const BUTTON = 11;
- const BUTTON_TAG = 12;
-
- // special
- const RECAPTCHA = 26;
- }
- /**
- * Constants defining the different field templates available.
- */
- class FormTemplate {
- const FIELD = 1;
- const FIELD_LABEL = 5;
- const HIDDEN = 2;
- const BUTTON = 3;
- const RADIO = 4;
- }
- /**
- * The form creator class.
- */
- class FormCreator {
- private $fields = array();
- private $form = array();
- private $template = array(
- FormTemplate::FIELD => '<p class="input">%s</p>',
- FormTemplate::FIELD_LABEL => '<p class="input">%s: %s</p>',
- FormTemplate::HIDDEN => '%s',
- FormTemplate::BUTTON => '<p class="button">%s</p>',
- FormTemplate::RADIO => '<p class="input radio">%s %s</p>',
- );
- private $secret = null;
-
- public function __construct($secret) {
- $this->secret = $secret;
- }
-
- public function __toString() {
- return $this->process();
- }
-
- public function display() {
- echo $this->process();
- }
-
- public function process() {
- $timestamp = time();
- $spinner = $this->_calculate_spinner($timestamp);
-
- $form = $this->_form_start() . "\n";
- $form .= $this->_field_hidden(array('name' => 'spinner', 'value' => $spinner)) . "\n";
- $form .= $this->_field_hidden(array('name' => ($this->_hash_name('timestamp', $spinner)), 'value' => $timestamp)) . "\n";
-
- foreach ($this->fields as $index => $field) {
- if (!isset($field['name'])) {
- $field['name'] = $field['id'];
- }
- $field['name'] = ($this->_hash_name($field['name'], $spinner));
- switch ($field['field_type']) {
- case FormField::TEXT:
- $form .= $this->_field_text($field);
- break;
- case FormField::PASSWORD:
- $form .= $this->_field_password($field);
- break;
- case FormField::FILE:
- $form .= $this->_field_file($field);
- break;
- case FormField::RADIO:
- $form .= $this->_field_radio($field);
- break;
- case FormField::CHECKBOX:
- $form .= $this->_field_checkbox($field);
- break;
- case FormField::HIDDEN:
- $form .= $this->_field_hidden($field);
- break;
- case FormField::IMAGE:
- $form .= $this->_field_image($field);
- break;
- case FormField::TEXTAREA:
- $form .= $this->_field_textarea($field);
- break;
- case FormField::LABEL:
- $form .= $this->_field_label($field);
- break;
- case FormField::SELECT:
- //$form .= $this->_field_file($field);
- break;
- case FormField::SUBMIT:
- $form .= $this->_field_submit($field);
- break;
- case FormField::RESET:
- $form .= $this->_field_reset($field);
- break;
- case FormField::BUTTON:
- $form .= $this->_field_button($field);
- break;
- case FormField::BUTTON_TAG:
- $form .= $this->_field_button_tag($field);
- break;
- case FormField::EMAIL:
- $form .= $this->_field_email($field);
- break;
- case FormField::TEL:
- $form .= $this->_field_tel($field);
- break;
- case FormField::URL:
- $form .= $this->_field_url($field);
- break;
- case FormField::SEARCH:
- $form .= $this->_field_search($field);
- break;
- case FormField::NUMBER:
- $form .= $this->_field_number($field);
- break;
- case FormField::DATE:
- $form .= $this->_field_date($field);
- break;
- case FormField::DATETIME:
- $form .= $this->_field_datetime($field);
- break;
- case FormField::TIME:
- $form .= $this->_field_time($field);
- break;
- case FormField::WEEK:
- $form .= $this->_field_week($field);
- break;
- case FormField::MONTH:
- $form .= $this->_field_month($field);
- break;
- case FormField::RANGE:
- $form .= $this->_field_range($field);
- break;
- case FormField::COLOR:
- $form .= $this->_field_color($field);
- break;
- case FormField::RECAPTCHA:
- $form .= $this->_field_recaptacha($field);
- break;
- default:
- throw new Exception('Unknown field type: ' . $field['field_type']);
- }
- $form .= "\n";
- }
- $form .= $this->_form_end();
- return $form;
- }
-
- public function setTemplate($template, $string) {
- $this->template[$template] = $string;
- }
-
- public function form($options) {
- $default = array(
- 'id' => null,
- 'class' => null,
- 'style' => null,
- 'name' => null,
- 'action' => '#',
- 'method' => 'get',
- 'enctype' => null,
- );
- $this->form = array_merge($default, $options);
- return $this;
- }
-
- public function add($field, $id, $options = array()) {
- $default = array(
- 'value' => null,
- 'name' => null,
- );
- $this->fields[] = array_merge($default, $options, array('id' => $id, 'field_type' => $field));
- return $this;
- }
-
- public function remove($field, $id) {
- $delete = array();
- foreach($this->fields as $index => $data) {
- if ($data['id'] == $id && $data['field_type'] == $field) {
- $delete[] = $index;
- }
- }
- foreach($delete as $dIndex) {
- unset($this->fields[$dIndex]);
- }
- return $this;
- }
-
- private function _calculate_spinner($timestamp) {
- return hash('sha256', $timestamp . $this->secret);
- }
-
- private function _hash_name($name, $spinner) {
- require_once 'Encryption\TW_Encryption.php';
-
- $array = false;
-
- if (strstr($name, '[]')) {
- $array = true;
- }
-
- $crypt = new TW_Encryption();
- $encoded = $this->_encode_name($crypt->encrypt($spinner, $name . $this->secret));
- return $encoded;
- }
-
- private function _encode_name($name) {
- return strtr(base64_encode($name), array('+' => '-', '/' => '_', '=' => ''));
- }
-
- private function _form_start() {
- $html = '<form';
- $html .= (isset($this->form['id'])) ? $this->_add_attrib('id', $this->form['id']): '';
- $html .= (isset($this->form['class'])) ? $this->_add_attrib('class', $this->form['class']): '';
- $html .= (isset($this->form['style'])) ? $this->_add_attrib('style', $this->form['style']): '';
- $html .= (isset($this->form['name'])) ? $this->_add_attrib('name', $this->form['name']): '';
- $html .= (isset($this->form['enctype'])) ? $this->_add_attrib('enctype', $this->form['enctype']): '';
- $html .= $this->_add_attrib('action', $this->form['action']);
- $html .= $this->_add_attrib('method', $this->form['method']);
- $html .= '>';
- return $html;
- }
-
- private function _form_end() {
- return '</form>';
- }
-
- /**
- * Creates a text input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_text($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('text', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('text', $data));
- }
- }
-
- /**
- * Creates a password input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_password($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('password', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('password', $data));
- }
- }
-
- /**
- * Creates a file input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_file($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('file', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('file', $data));
- }
- }
-
- /**
- * Creates a radio button input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_radio($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::RADIO], $this->_do_field_input('radio', $data), $this->_do_field_label($data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('radio', $data));
- }
- }
-
- /**
- * Creates a checkbox input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_checkbox($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('checkbox', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('checkbox', $data));
- }
- }
-
- /**
- * Creates a hidden input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_hidden($data) {
- return sprintf($this->template[FormTemplate::HIDDEN], $this->_do_field_input('hidden', $data));
- }
-
- /**
- * Creates an image input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_image($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('image', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('image', $data));
- }
- }
-
- /**
- * Creates an textarea field.
- *
- * @param array $data
- * @return string
- */
- private function _field_textarea($data) {
- $input = '<textarea';
- foreach ($data as $key => $value) {
- if ($key == 'value') {
- continue;
- }
-
- if ($value === true) {
- $input .= $this->_add_param($key);
- continue;
- }
- if ($key == 'id') {
- $input .= $this->_add_attrib('id', 'field_' . $value);
- continue;
- }
- $input .= $this->_add_attrib($key, $value);
- }
- $input .= '>';
- $input .= (isset($data['value'])) ? $data['value']: '';
- $input .= '</textarea>';
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $input);
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $input);
- }
- }
-
- /**
- * Creates a label field.
- *
- * @param array $data
- * @return string
- */
- private function _field_label($data) {
- unset($data['field_type']);
-
- $input = '<label';
- foreach ($data as $key => $value) {
- if ($key == 'value' || $key == 'label') {
- continue;
- }
-
- if ($value === true) {
- $input .= $this->_add_param($key);
- continue;
- }
- if ($key == 'id') {
- $input .= $this->_add_attrib('id', 'field_' . $value);
- continue;
- }
- $input .= $this->_add_attrib($key, $value);
- }
- $input .= '>';
- $input .= $data['value'];
- $input .= '</label>';
- return sprintf($this->template[FormTemplate::FIELD], $input);
- }
-
- /**
- * Creates a select field.
- *
- * @param array $data
- * @return string
- */
- private function _field_select($data) { }
-
- /**
- * Creates a submit button.
- *
- * @param array $data
- * @return string
- */
- private function _field_submit($data) {
- return sprintf($this->template[FormTemplate::BUTTON], $this->_do_field_input('submit', $data));
- }
-
- /**
- * Creates a reset button.
- *
- * @param array $data
- * @return string
- */
- private function _field_reset($data) {
- return sprintf($this->template[FormTemplate::BUTTON], $this->_do_field_input('reset', $data));
- }
-
- /**
- * Creates a standard button.
- *
- * @param array $data
- * @return string
- */
- private function _field_button($data) {
- return sprintf($this->template[FormTemplate::BUTTON], $this->_do_field_input('button', $data));
- }
-
- /**
- * Creates a button tag.
- *
- * @param array $data
- * @return string
- */
- private function _field_button_tag($data) {
- unset($data['field_type']);
- unset($data['label']);
-
- $input = '<button';
- foreach ($data as $key => $value) {
- if ($key == 'content') {
- continue;
- }
-
- if ($value === true) {
- $input .= $this->_add_param($key);
- continue;
- }
- if ($key == 'id') {
- $input .= $this->_add_attrib('id', 'field_' . $value);
- continue;
- }
- $input .= $this->_add_attrib($key, $value);
- }
- $input .= '>';
- $input .= (isset($data['content'])) ? $data['content'] : '';
- $input .= '</button>';
- return sprintf($this->template[FormTemplate::FIELD], $input);
- }
-
- /**
- * Create email input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_email($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('email', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('email', $data));
- }
- }
-
- /**
- * Create telephone input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_tel($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('tel', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('tel', $data));
- }
- }
-
- /**
- * Create URL input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_url($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('url', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('url', $data));
- }
- }
-
- /**
- * Create search input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_search($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('search', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('search', $data));
- }
- }
-
- /**
- * Create number field.
- *
- * @param array $data
- * @return string
- */
- private function _field_number($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('number', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('number', $data));
- }
- }
-
- /**
- * Create date input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_date($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('date', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('date', $data));
- }
- }
-
- /**
- * Create time field.
- *
- * @param array $data
- * @return string
- */
- private function _field_time($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('time', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('time', $data));
- }
- }
-
- /**
- * Create datetime input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_datetime($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('datetime', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('datetime', $data));
- }
- }
-
- /**
- * Create week input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_week($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('week', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('week', $data));
- }
- }
-
- /**
- * Create month input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_month($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('month', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('month', $data));
- }
- }
-
- /**
- * Create color input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_color($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('color', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('color', $data));
- }
- }
-
- /**
- * Create range input field.
- *
- * @param array $data
- * @return string
- */
- private function _field_range($data) {
- if (isset($data['label'])) {
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($data), $this->_do_field_input('range', $data));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], $this->_do_field_input('range', $data));
- }
- }
-
- /**
- * Create a recaptcha field.
- *
- * @param array $data
- * @return string
- */
- private function _field_recaptacha($data) {
- require_once 'ReCAPTCHA\recaptchalib.php';
-
- if (isset($data['label'])) {
- $clean = $data;
- unset($clean['key']);
- return sprintf($this->template[FormTemplate::FIELD_LABEL], $this->_do_field_label($clean), recaptcha_get_html($data['key']));
- } else {
- return sprintf($this->template[FormTemplate::FIELD], recaptcha_get_html($data['key']));
- }
- }
-
- /**
- * Builds a standard input field from an array.
- *
- * @param string $type
- * @param array $data
- * @return string
- */
- private function _do_field_input($type, $data) {
- $input = '<input type="' . $type . '"';
- unset($data['field_type']);
- unset($data['label']);
- foreach ($data as $key => $value) {
- //no empty value param for buttons
- if (($type == 'reset' || $type == 'submit') && $key == 'value' && empty($value)) {
- continue;
- }
-
- if ($value === true) {
- $input .= $this->_add_param($key);
- continue;
- }
- if ($key == 'id') {
- $input .= $this->_add_attrib('id', 'field_' . $value);
- continue;
- }
- $input .= $this->_add_attrib($key, $value);
- }
- $input .= '>';
-
- return $input;
- }
-
- /**
- * Builds a standard label from an array.
- *
- * @param array $data
- * @return string
- */
- private function _do_field_label($data) {
- $label = '<label';
- $label .= $this->_add_attrib('for', $data['name']);
- $label .= $this->_add_attrib('id', 'label_' . $data['id']);
- $label .= (isset($data['class'])) ? $this->_add_attrib('class', $data['class']): '';
- $label .= '>' . $data['label'] . '</label>';
- return $label;
- }
-
- /**
- * Creates an attribute pair (key=value).
- *
- * @param string $name
- * @param mixed $value
- * @return string
- */
- private function _add_attrib($name, $value) {
- return " {$name}=\"{$value}\"";
- }
-
- /**
- * Creates an parameter entry (key).
- *
- * @param string $name
- * @return string
- */
- private function _add_param($name) {
- return " {$name}";
- }
- }