Author: Nick Sagona, III (nick@popphp.org)
Inheritance: extends Pop\Dom\Child
Exemplo n.º 1
0
 /**
  * Constructor
  *
  * Instantiate the CSRF form element object.
  *
  * @param  string $name
  * @param  string $value
  * @param  int    $expire
  * @param  string $indent
  * @return \Pop\Form\Element\Csrf
  */
 public function __construct($name, $value = null, $expire = 300, $indent = null)
 {
     // Start a session.
     if (session_id() == '') {
         session_start();
     }
     // If token does not exist, create one
     if (!isset($_SESSION['pop_csrf'])) {
         $this->token = array('value' => sha1(rand(10000, getrandmax()) . $value), 'expire' => (int) $expire, 'start' => time());
         $_SESSION['pop_csrf'] = serialize($this->token);
         // Else, retrieve existing token
     } else {
         $this->token = unserialize($_SESSION['pop_csrf']);
         // Check to see if the token has expired
         if ($this->token['expire'] > 0) {
             if ($this->token['expire'] + $this->token['start'] < time()) {
                 $this->token = array('value' => sha1(rand(10000, getrandmax()) . $value), 'expire' => (int) $expire, 'start' => time());
                 $_SESSION['pop_csrf'] = serialize($this->token);
             }
         }
     }
     parent::__construct('hidden', $name, $this->token['value'], null, $indent);
     $this->setRequired(true);
     $this->setValidator();
 }
Exemplo n.º 2
0
 /**
  * Set an attribute or attributes for the child element object.
  *
  * @param  array|string $a
  * @param  string $v
  * @return \Pop\Dom\Child
  */
 public function setAttributes($a, $v = null)
 {
     parent::setAttributes($a, $v);
     if (array_key_exists('multiple', $this->attributes)) {
         if (strpos($this->name, '[]') === false) {
             $this->name .= '[]';
         }
         if (array_key_exists('name', $this->attributes)) {
             if (strpos($this->attributes['name'], '[]') === false) {
                 $this->attributes['name'] .= '[]';
             }
         }
     }
     return $this;
 }
Exemplo n.º 3
0
 /**
  * Set the field values. Optionally, you can apply filters
  * to the passed values via callbacks and their parameters
  *
  * @param  array $values
  * @param  array $filters
  * @return \Pop\Form\Form
  */
 public function setFieldValues(array $values = null, $filters = null)
 {
     // Filter values if passed
     if (null !== $values && null !== $filters) {
         $values = $this->filterValues($values, $filters);
     }
     // Loop through the initial fields values and build the fields
     // based on the _initFieldsValues property.
     if (count($this->initFieldsValues) > 0) {
         // If the fields are a group of fields
         $keys = array_keys($this->initFieldsValues);
         if (is_numeric($keys[0])) {
             $fields = array();
             foreach ($this->initFieldsValues as $ary) {
                 $k = array_keys($ary);
                 if (isset($k[0])) {
                     $this->groups[] = $k[0];
                 }
                 $fields = array_merge($fields, $ary);
             }
             $this->initFieldsValues = $fields;
         }
         foreach ($this->initFieldsValues as $name => $field) {
             if (is_array($field) && isset($field['type'])) {
                 $type = $field['type'];
                 $label = isset($field['label']) ? $field['label'] : null;
                 $required = isset($field['required']) ? $field['required'] : null;
                 $attributes = isset($field['attributes']) ? $field['attributes'] : null;
                 $validators = isset($field['validators']) ? $field['validators'] : null;
                 $expire = isset($field['expire']) ? $field['expire'] : 300;
                 $captcha = isset($field['captcha']) ? $field['captcha'] : null;
                 $data = isset($field['data']) ? $field['data'] : null;
                 if ($type == 'file') {
                     $this->hasFile = true;
                 }
                 if (isset($field['error'])) {
                     $error = array('container' => 'div', 'attributes' => array('class' => 'error'), 'pre' => false);
                     foreach ($field['error'] as $key => $value) {
                         if ($key != 'pre') {
                             $error['container'] = $key;
                             $error['attributes'] = $value;
                         } else {
                             if ($key == 'pre') {
                                 $error['pre'] = $value;
                             }
                         }
                     }
                 } else {
                     $error = null;
                 }
                 if (null !== $values && array_key_exists($name, $values)) {
                     if ($type == 'checkbox' || $type == 'radio' || $type == 'select') {
                         $value = isset($field['value']) ? $field['value'] : null;
                         $marked = $values[$name];
                     } else {
                         $value = $values[$name];
                         $marked = isset($field['marked']) ? $field['marked'] : null;
                     }
                 } else {
                     $value = isset($field['value']) ? $field['value'] : null;
                     $marked = isset($field['marked']) ? $field['marked'] : null;
                 }
                 // Initialize the form element.
                 switch (strtolower($type)) {
                     case 'checkbox':
                         $elem = new Element\Checkbox($name, $value, $marked);
                         break;
                     case 'radio':
                         $elem = new Element\Radio($name, $value, $marked);
                         break;
                     case 'select':
                         $elem = new Element\Select($name, $value, $marked, null, $data);
                         break;
                     case 'textarea':
                         $elem = new Element\Textarea($name, $value, $marked);
                         break;
                     case 'csrf':
                         $elem = new Element\Csrf($name, $value, $expire);
                         break;
                     case 'captcha':
                         $elem = new Element\Captcha($name, $value, $expire, $captcha);
                         break;
                     default:
                         $elem = new Element($type, $name, $value, $marked);
                 }
                 // Set the label.
                 if (null !== $label) {
                     $elem->setLabel($label);
                 }
                 // Set if required.
                 if (null !== $required) {
                     $elem->setRequired($required);
                 }
                 // Set if error display.
                 if (null !== $error) {
                     $elem->setErrorDisplay($error['container'], $error['attributes'], $error['pre']);
                 }
                 // Set any attributes.
                 if (null !== $attributes) {
                     foreach ($attributes as $a => $v) {
                         $elem->setAttributes($a, $v);
                     }
                 }
                 // Set any validators.
                 if (null !== $validators) {
                     if (is_array($validators)) {
                         foreach ($validators as $val) {
                             $elem->addValidator($val);
                         }
                     } else {
                         $elem->addValidator($validators);
                     }
                 }
                 $this->addElements($elem);
             }
         }
         // Else, set the passed values to the elements that
         // are already added to the form object
     } else {
         $fields = $this->getElements();
         if (null !== $values && count($fields) > 0) {
             foreach ($fields as $field) {
                 $fieldName = str_replace('[]', '', $field->getName());
                 if (isset($values[$fieldName])) {
                     // If a multi-value form element
                     if ($field->hasChildren()) {
                         $field->setMarked($values[$fieldName]);
                         $this->fields[$fieldName] = $values[$fieldName];
                         // Loop through the field's children
                         if ($field->hasChildren()) {
                             $children = $field->getChildren();
                             foreach ($children as $key => $child) {
                                 // If checkbox or radio
                                 if ($child->getAttribute('type') == 'checkbox' || $child->getAttribute('type') == 'radio') {
                                     if (is_array($field->getMarked()) && in_array($child->getAttribute('value'), $field->getMarked())) {
                                         $field->getChild($key)->setAttributes('checked', 'checked');
                                     } else {
                                         if ($child->getAttribute('value') == $field->getMarked()) {
                                             $field->getChild($key)->setAttributes('checked', 'checked');
                                         }
                                     }
                                     // If select option
                                 } else {
                                     if ($child->getNodeName() == 'option') {
                                         if (is_array($field->getMarked()) && in_array($child->getAttribute('value'), $field->getMarked())) {
                                             $field->getChild($key)->setAttributes('selected', 'selected');
                                         } else {
                                             if ($child->getAttribute('value') == $field->getMarked()) {
                                                 $field->getChild($key)->setAttributes('selected', 'selected');
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                         // Else, if a single-value form element
                     } else {
                         $field->setValue($values[$fieldName]);
                         $this->fields[$fieldName] = $values[$fieldName];
                         if ($field->getNodeName() == 'textarea') {
                             $field->setNodeValue($values[$fieldName]);
                         } else {
                             $field->setAttributes('value', $values[$fieldName]);
                         }
                     }
                 }
             }
         }
     }
     if (null !== $this->errorDisplay) {
         $this->setErrorDisplay($this->errorDisplay['container'], $this->errorDisplay['attributes'], $this->errorDisplay['pre']);
     }
     return $this;
 }
Exemplo n.º 4
0
 /**
  * Get configuration values
  *
  * @return void
  */
 public function getAll()
 {
     $cfg = Table\Config::getConfig();
     $config = array();
     $formattedConfig = array();
     foreach ($cfg->rows as $c) {
         $config[$c->setting] = $c->setting == 'media_allowed_types' || $c->setting == 'media_actions' ? $value = unserialize($c->value) : $c->value;
     }
     $sysVersion = $config['system_version'];
     $latest = '';
     $handle = fopen('http://update.phirecms.org/system/version', 'r');
     if ($handle !== false) {
         $latest = trim(stream_get_contents($handle));
         fclose($handle);
     }
     if (version_compare(\Phire\Project::VERSION, $latest) < 0 && $this->data['acl']->isAuth('Phire\\Controller\\Phire\\Config\\IndexController', 'update')) {
         $sysVersion .= ' (<a href="' . BASE_PATH . APP_URI . '/config/update">' . $this->i18n->__('Update to') . ' ' . $latest . '</a>?)';
     }
     // Set server config settings
     $formattedConfig['server'] = array('system_version' => $sysVersion, 'system_domain' => $config['system_domain'], 'system_document_root' => $config['system_document_root'], 'system_base_path' => BASE_PATH, 'system_application_path' => APP_PATH, 'system_content_path' => CONTENT_PATH, 'server_operating_system' => $config['server_operating_system'], 'server_software' => $config['server_software'], 'database_version' => $config['database_version'], 'php_version' => $config['php_version'], 'installed_on' => date($this->config->datetime_format, strtotime($config['installed_on'])), 'updated_on' => $config['updated_on'] != '0000-00-00 00:00:00' ? date($this->config->datetime_format, strtotime($config['updated_on'])) : '(' . $this->i18n->__('Never') . ')');
     // Set site title form element
     $siteTitle = new Element('text', 'site_title', $config['site_title']);
     $siteTitle->setAttributes('size', 85)->setAttributes('style', 'padding: 5px;');
     // Set system title form element
     $systemTitle = new Element('text', 'system_title', $config['system_title']);
     $systemTitle->setAttributes('size', 85)->setAttributes('style', 'padding: 5px;');
     // Set system email form element
     $systemEmail = new Element('text', 'system_email', $config['system_email']);
     $systemEmail->setAttributes('size', 85)->setAttributes('style', 'padding: 5px;');
     // Set system email form element
     $replyEmail = new Element('text', 'reply_email', $config['reply_email']);
     $replyEmail->setAttributes('size', 85)->setAttributes('style', 'padding: 5px;');
     // Set separator form element
     $separator = new Element('text', 'separator', $config['separator']);
     $separator->setAttributes('size', 3)->setAttributes('style', 'padding: 5px;');
     // Set default language form element
     $langs = I18n::getLanguages();
     foreach ($langs as $key => $value) {
         $langs[$key] = substr($value, 0, strpos($value, ' ('));
     }
     $lang = new Element\Select('default_language', $langs, $config['default_language'], '                    ');
     // Set date and time format form element
     $datetime = $this->getDateTimeFormat($config['datetime_format']);
     // Set max media size form element
     $maxSize = new Element('text', 'media_max_filesize', $this->getMaxSize($config['media_max_filesize']));
     $maxSize->setAttributes('size', 10)->setAttributes('style', 'padding: 3px;');
     // Set page limit form element
     $pageLimit = new Element('text', 'pagination_limit', $config['pagination_limit']);
     $pageLimit->setAttributes('size', 10)->setAttributes('style', 'padding: 3px;');
     // Set page range form element
     $pageRange = new Element('text', 'pagination_range', $config['pagination_range']);
     $pageRange->setAttributes('size', 10)->setAttributes('style', 'padding: 3px;');
     // Set media actions and media types form elements
     $mediaConfig = $this->getMediaConfig($config['media_actions']);
     $mediaTypes = $this->getMediaAllowedTypes($config['media_allowed_types']);
     $imageAdapters = array('Gd' => 'Gd');
     if (\Pop\Image\Imagick::isInstalled()) {
         $imageAdapters['Imagick'] = 'Imagick';
     }
     $phpLimits = array('post_max_size' => str_replace(array('M', 'K'), array(' MB', ' KB'), strtoupper(ini_get('post_max_size'))), 'upload_max_filesize' => str_replace(array('M', 'K'), array(' MB', ' KB'), strtoupper(ini_get('upload_max_filesize'))), 'max_file_uploads' => str_replace(array('M', 'K'), array(' MB', ' KB'), strtoupper(ini_get('max_file_uploads'))));
     $phpLimitsString = '';
     foreach ($phpLimits as $limit => $limitValue) {
         $phpLimitsString .= '<span style="padding: 0 5px 0 5px;">' . $this->i18n->__(ucwords(str_replace('_', ' ', $limit))) . ': ' . '<strong>' . $limitValue . '</strong></span>';
     }
     $formattedConfig['settings'] = array('site_title' => $siteTitle, 'system_title' => $systemTitle, 'system_email' => $systemEmail, 'reply_email' => $replyEmail, 'separator' => $separator, 'default_language' => $lang, 'datetime_format' => $datetime, 'media_allowed_types' => $mediaTypes, 'media_max_filesize' => '                    ' . $maxSize . ' &nbsp;&nbsp;&nbsp; [<strong style="color: #f00; padding: 0 0 0 5px;">PHP ' . $this->i18n->__('Limits') . ':</strong> ' . $phpLimitsString . ']', 'media_actions' => $mediaConfig, 'media_image_adapter' => new Element\Select('media_image_adapter', $imageAdapters, $config['media_image_adapter'], '                    '), 'pagination_limit' => '                    ' . $pageLimit, 'pagination_range' => '                    ' . $pageRange, 'force_ssl' => new Element\Radio('force_ssl', array('1' => $this->i18n->__('Yes'), '0' => $this->i18n->__('No')), $config['force_ssl'], '                    '), 'live' => new Element\Radio('live', array('1' => $this->i18n->__('Yes'), '0' => $this->i18n->__('No')), $config['live'], '                    '));
     $this->data['config'] = new \ArrayObject($formattedConfig, \ArrayObject::ARRAY_AS_PROPS);
 }
Exemplo n.º 5
0
 public function testRenderWithTemplate()
 {
     $e = new Element('text', 'username', 'Username');
     $e->setLabel('Username');
     $s = new Element('submit', 'submit', 'Submit');
     $f = new Form('/submit', 'post');
     $f->addElements(array($e, $s));
     $f->setTemplate("[{username}] [{submit}]");
     $f->username = '******';
     $form = $f->render(true);
     $this->assertContains('<form ', $form);
     $this->assertEquals('My Username', $f->username);
 }
Exemplo n.º 6
0
 /**
  * Constructor
  *
  * Instantiate the radio form element object.
  *
  * @param  string $name
  * @param  string|array $value
  * @param  string|array $marked
  * @param  string $indent
  * @return \Pop\Form\Element\Radio
  */
 public function __construct($name, $value = null, $marked = null, $indent = null)
 {
     $this->value = $value;
     $this->setMarked($marked);
     parent::__construct('radio', $name, $value, $marked, $indent);
 }
Exemplo n.º 7
0
 public function testRender()
 {
     $e = new Element('text', 'email');
     $element = $e->render(true);
     $e->setErrorPre(true);
     $element = $e->render(true);
     ob_start();
     $e->output();
     $output = ob_get_clean();
     $this->assertContains('<input', $element);
     $this->assertContains('<input', $output);
 }
Exemplo n.º 8
0
 /**
  * Constructor
  *
  * Instantiate the textarea form element object.
  *
  * @param  string $name
  * @param  string $value
  * @param  string|array $marked
  * @param  string $indent
  * @return \Pop\Form\Element\Textarea
  */
 public function __construct($name, $value = null, $marked = null, $indent = null)
 {
     parent::__construct('textarea', $name, $value, $marked, $indent);
 }
Exemplo n.º 9
0
            return 'The username value must be greater than or equal to 6.';
        }
    });
    $email = new Element('text', 'email');
    $email->setLabel('Email:')->setRequired(true)->setAttributes('size', 40)->addValidator(new Validator\Email());
    $password = new Element('password', 'password');
    $password->setLabel('Password:'******'size', 40)->addValidator(array(new MyValidator(), 'validate'));
    $checkbox = new Checkbox('colors', array('Red' => 'Red', 'Green' => 'Green', 'Blue' => 'Blue'));
    $checkbox->setLabel('Colors:')->setRequired(true);
    $radio = new Radio('answer', array('Yes' => 'Yes', 'No' => 'No', 'Maybe' => 'Maybe'));
    $radio->setLabel('Answer:')->setRequired(true);
    $select = new Select('days', Select::DAYS_OF_WEEK);
    $select->setLabel('Day:');
    $textarea = new Textarea('comments', 'Please type a comment...');
    $textarea->setAttributes('rows', '5')->setAttributes('cols', '40')->setLabel('Comments:');
    $submit = new Element('submit', 'submit', 'SUBMIT');
    $submit->setAttributes('style', 'padding: 5px; border: solid 2px #000; background-color: #00f; color: #fff; font-weight: bold;');
    $form->addElements(array($username, $email, $password, $checkbox, $radio, $select, $textarea, $submit));
    if ($_POST) {
        $form->setFieldValues($_POST, array('strip_tags' => null, 'htmlentities' => array(ENT_QUOTES, 'UTF-8')));
        if (!$form->isValid()) {
            $form->render();
        } else {
            echo 'Form is valid.<br />' . PHP_EOL;
            print_r($form->getFields());
        }
    } else {
        $form->render();
    }
    echo PHP_EOL . PHP_EOL;
} catch (\Exception $e) {
Exemplo n.º 10
0
 /**
  * Set the label of the form element object.
  *
  * @param  string $label
  * @return \Pop\Form\Element
  */
 public function setLabel($label)
 {
     parent::setLabel($label);
     if (isset($this->token['captcha'])) {
         if (strpos($this->token['captcha'], '<img') === false && (strpos($this->token['captcha'], ' + ') !== false || strpos($this->token['captcha'], ' - ') !== false || strpos($this->token['captcha'], ' * ') !== false || strpos($this->token['captcha'], ' / ') !== false)) {
             $this->label = $this->label . '(' . str_replace(array(' * ', ' / '), array(' &#215; ', ' &#247; '), $this->token['captcha'] . ')');
         } else {
             $this->label = $this->label . $this->token['captcha'];
         }
     }
     return $this;
 }
Exemplo n.º 11
0
<?php

require_once '../../bootstrap.php';
use Pop\Form\Element;
use Pop\Form\Element\Select;
use Pop\Form\Element\Textarea;
try {
    $input = new Element('text', 'email', 'Enter your email here...');
    $input->setAttributes('size', 30);
    $input->output();
    echo '<br />' . PHP_EOL;
    $values = array('Red' => 'Red', 'Green' => 'Green', 'Blue' => 'Blue');
    $checkbox = new Select('hours', Select::HOURS_24);
    $checkbox->output();
    echo '<br />' . PHP_EOL;
    $textarea = new Textarea('comments', 'Please type a comment...');
    $textarea->setAttributes('rows', '10')->setAttributes('cols', '50');
    $textarea->output();
    echo '<br />' . PHP_EOL . PHP_EOL;
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL . PHP_EOL;
}