From 7982208113854afdfde6f1d90a719fe52b7784f1 Mon Sep 17 00:00:00 2001 From: phpnut Date: Tue, 1 May 2007 15:17:27 +0000 Subject: [PATCH] Reverting changes in last commit git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4975 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/controller/components/security.php | 21 +-- cake/libs/view/helper.php | 50 +++--- cake/libs/view/helpers/form.php | 160 +++++------------- .../cases/libs/view/helpers/form.test.php | 23 +-- 4 files changed, 82 insertions(+), 172 deletions(-) diff --git a/cake/libs/controller/components/security.php b/cake/libs/controller/components/security.php index 1fbde35f1..ae06a64b1 100644 --- a/cake/libs/controller/components/security.php +++ b/cake/libs/controller/components/security.php @@ -73,15 +73,6 @@ class SecurityComponent extends Object { * @see SecurityComponent::requireAuth() */ var $requireAuth = array(); -/** - * Sets the window of time after which authentication keys expire. If true, authentication keys - * expire on every request. If a string, specifies a relative time frame, i.e. "+1 hour" - * - * @var mixed - * @access public - * @see SecurityComponent::requireAuth() - */ - var $expires = true; /** * List of actions that require an HTTP-authenticated login (basic or digest) * @@ -430,12 +421,12 @@ class SecurityComponent extends Object { */ function __validatePost(&$controller) { if(!empty($controller->data)) { - if (!isset($controller->data['_Token'])) { + if (!isset($controller->data['__Token'])) { if(!$this->blackHole($controller, 'auth')) { return null; } } - $token = $controller->data['_Token']['key']; + $token = $controller->data['__Token']['key']; if($this->Session->check('_Token')) { $tData = unserialize($this->Session->read('_Token')); @@ -447,17 +438,17 @@ class SecurityComponent extends Object { } } - if(!isset($controller->data['_Token']['fields'])) { + if(!isset($controller->data['__Token']['fields'])) { if(!$this->blackHole($controller, 'auth')) { return null; } } - $fields = $controller->data['_Token']['fields']; + $fields = $controller->data['__Token']['fields']; $check = $controller->data; - unset($check['_Token']['fields']); + unset($check['__Token']['fields']); foreach($check as $key => $value) { - if($key === '_Token') { + if($key === '__Token') { $field[$key] = $value; continue; } diff --git a/cake/libs/view/helper.php b/cake/libs/view/helper.php index ce06fe7f8..d38377722 100644 --- a/cake/libs/view/helper.php +++ b/cake/libs/view/helper.php @@ -328,11 +328,11 @@ class Helper extends Overloadable { /** * Sets this helper's model and field properties to the slash-separated value-pair in $tagValue. * - * @param string $field A field name, like "Modelname.fieldname", "Modelname/fieldname" is deprecated + * @param string $tagValue A field name, like "Modelname.fieldname", "Modelname/fieldname" is deprecated */ - function setFormTag($field) { + function setFormTag($tagValue) { $view =& ClassRegistry::getObject('view'); - $parts = preg_split('/\/|\./', $field); + $parts = preg_split('/\/|\./', $tagValue); if (count($parts) == 1) { $view->field = $parts[0]; @@ -347,15 +347,6 @@ class Helper extends Overloadable { $view->modelId = $parts[1]; $view->field = $parts[2]; } - - } -/** - * Returns the fully-qualified field name of the current field in the rendering context. - * - * @return string - */ - function getFormTag() { - return 'blah'; } /** * Gets the currently-used model of the rendering context. @@ -384,15 +375,6 @@ class Helper extends Overloadable { $view =& ClassRegistry::getObject('view'); return $view->field; } -/** - * Gets the currently-used model field of the rendering context. - * - * @return string - */ - function fieldSuffix() { - $view =& ClassRegistry::getObject('view'); - return $view->fieldSuffix; - } /** * Returns false if given FORM field has no errors. Otherwise it returns the constant set in the array Model->validationErrors. * @@ -430,7 +412,7 @@ class Helper extends Overloadable { * * @param array $options * @param string $key - * @return mixed + * @return array */ function __name($options = array(), $field = null, $key = 'name') { if ($options === null) { @@ -449,13 +431,14 @@ class Helper extends Overloadable { } switch($field) { + case 'method': case '_method': $name = $field; break; default: - $name = Set::filter(array($this->model(), $this->field())); //, $this->modelID())); + $name = array_filter(array($this->model(), $this->field(), $this->modelID())); if ($this->modelID() === 0) { - //$name[] = $this->modelID(); + $name[] = $this->modelID(); } $name = 'data[' . join('][', $name) . ']'; break; @@ -515,6 +498,25 @@ class Helper extends Overloadable { return $result; } } +/** + * Sets the defaults for an input tag + * + * @param array $options + * @param string $key + * @return array + */ + function __initInputField($field, $options = array()) { + $this->setFormTag($field); + $options = (array)$options; + $options = $this->__name($options); + $options = $this->__value($options); + $options = $this->domId($options); + if ($this->tagIsInvalid()) { + $options = $this->addClass($options, 'form-error'); + } + unset($options['name']); // Temporary + return $options; + } /** * Adds the given class to the element options * diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index e6563c16a..d132b83b0 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -58,38 +58,19 @@ class FormHelper extends AppHelper { var $helpers = array('Html'); -/** - * Holds the fields array('field_name'=>'type'), sizes array('field_name'=>'size'), - * primaryKey and validates array('field_name') - * - * @var array - * @access public - */ - var $fieldset = array('fields' => array(), 'sizes' => array(), 'key' => 'id', 'validates' => array()); -/** - * Local cache of default generated options for date/time input fields - * - * @var array - * @access private - */ + /** + * holds the fields array('field_name'=>'type'), sizes array('field_name'=>'size'), + * primaryKey and validates array('field_name') + * + * @access public + */ + var $fieldset = array('fields'=>array(), 'sizes'=>array(), 'key'=>'id', 'validates'=>array()); + var $__options = array('day' => array(), 'minute' => array(), 'hour' => array(), 'month' => array(), 'year' => array(), 'meridian' => array()); + var $fields = array(); -/** - * List of input fields generated in the current form (between create() and end()). - * - * @var array - * @access public - */ - var $inputFields = array(); -/** - * Maintains the attributes of the current form (between create() and end()). - * - * @var array - * @access public - */ - var $current = array(); /** * Returns an HTML FORM element. * @@ -100,8 +81,6 @@ class FormHelper extends AppHelper { */ function create($model = null, $options = array()) { $defaultModel = null; - $this->inputFields = array(); - $data = array('fields' => '','key' => '', 'validates' => ''); $view =& ClassRegistry::getObject('view'); @@ -164,7 +143,7 @@ class FormHelper extends AppHelper { if (empty($options['url']) || is_array($options['url'])) { $options = (array)$options; - if (!empty($this->plugin)) { + if(!empty($this->plugin)) { $controller = $this->plugin; } elseif (!empty($model) && $model != $defaultModel) { $controller = Inflector::underscore(Inflector::pluralize($model)); @@ -187,7 +166,6 @@ class FormHelper extends AppHelper { } elseif (is_string($options['url'])) { $options['action'] = $options['url']; } - $this->current = $options; unset($options['url']); switch (low($options['type'])) { @@ -205,7 +183,6 @@ class FormHelper extends AppHelper { $htmlAttributes['method'] = 'post'; break; } - $append .= $this->authToken(); $htmlAttributes['action'] = $this->url($options['action']); unset($options['type'], $options['action']); @@ -220,6 +197,12 @@ class FormHelper extends AppHelper { unset($options['default']); $htmlAttributes = am($options, $htmlAttributes); + if (isset($this->params['_Token']) && !empty($this->params['_Token'])) { + $append .= '

'; + $append .= $this->hidden('_Token/key', array('value' => $this->params['_Token']['key'], 'id' => $options['id'] . 'Token' . mt_rand())); + $append .= '

'; + } + $this->setFormTag($model . '.'); return $this->output(sprintf($this->Html->tags['form'], $this->Html->parseHtmlOptions($htmlAttributes, null, ''))) . $append; } @@ -229,17 +212,17 @@ class FormHelper extends AppHelper { * @access public * @return string A closing FORM tag. */ - function end($options = array()) { - $submitOptions = $submit = false; + function end($options = null) { + if (!empty($this->params['models'])) { + $models = $this->params['models'][0]; + } - if (!is_array($options)) { + $submitOptions = true; + if(!is_array($options)) { $submitOptions = $options; - } elseif (isset($options['submit'])) { + } else if(isset($options['submit'])) { $submitOptions = $options['submit']; unset($options['submit']); - if (!is_array($submitOptions)) { - $submitOptions = array('label' => $submitOptions); - } if(isset($submitOptions['label'])) { $submit = $submitOptions['label']; @@ -247,9 +230,9 @@ class FormHelper extends AppHelper { } } - if ($submitOptions === true) { + if($submitOptions === true) { $submit = 'Submit'; - } elseif (is_string($submitOptions)) { + } else if(is_string($submitOptions)) { $submit = $submitOptions; } @@ -258,47 +241,18 @@ class FormHelper extends AppHelper { } $out = null; - if($submit !== false) { + if(isset($submit)) { $out .= $this->submit($submit, $submitOptions); } - $out .= $this->output($this->Html->tags['formend']); - - $this->inputFields = $this->current = array(); - return $out; + $out .= $this->Html->tags['formend']; + return $this->output($out); } -/** - * Creates a serialized hash of the list of fields used in this form - * - * @param array $options - * @param string $key - * @return array - */ function secure($fields) { $append = '

'; $append .= $this->hidden('_Token/fields', array('value' => urlencode(Security::hash(serialize($fields) . CAKE_SESSION_STRING)), 'id' => 'TokenFields' . mt_rand())); $append .= '

'; return $append; } -/** - * Sets the defaults for an input tag - * - * @param array $options - * @param string $key - * @return array - */ - function __initInputField($field, $options = array()) { - $this->setFormTag($field); - $options = (array)$options; - $options = $this->__name($options); - $options = $this->__value($options); - $options = $this->domId($options); - if ($this->tagIsInvalid()) { - $options = $this->addClass($options, 'form-error'); - } - $this->inputFields[$this->getFormTag()] = $options; - unset($options['name']); // Temporary - return $options; - } /** * Returns true if there is an error for the given field, otherwise false * @@ -371,19 +325,6 @@ class FormHelper extends AppHelper { } return $this->output(sprintf($this->Html->tags['label'], $this->domId($tagName), $this->_parseAttributes($attributes), $text)); } -/** - * Captures field names to be stored in the list of current fields - * - * @access public - * @param array $options - * @param string $key - * @return mixed - */ - function __name($options = array(), $field = null, $key = 'name') { - $out = parent::__name($options, $field, $key); - $this->inputFields[] = implode('.', Set::filter(array($this->model(), $this->field(), $this->modelID()))); - return $out; - } /** * Will display all the fields passed in an array expects tagName as an array key * replaces generateFields @@ -391,7 +332,7 @@ class FormHelper extends AppHelper { * @access public * @param array $fields works well with Controller::generateFields() or on its own; * @param array $blacklist a simple array of fields to skip - * @return string + * @return output */ function inputs($fields = null, $blacklist = null) { if(!is_array($fields)) { @@ -662,6 +603,9 @@ class FormHelper extends AppHelper { $options = $this->__initInputField($fieldName, $options); $model = $this->model(); unset($options['class']); + if(isset($this->params['_Token']) && !empty($this->params['_Token'])) { + $model = '_' . $model; + } $this->fields[$model][$this->field()] = $options['value']; if (in_array($fieldName, array('_method', '_fields'))) { @@ -669,22 +613,6 @@ class FormHelper extends AppHelper { } return $this->output(sprintf($this->Html->tags['hidden'], $model, $this->field(), $this->_parseAttributes($options, null, ' ', ' '))); } -/** - * Creates a token field used for authentication with SecurityComponent::requireAuth() - * - * @return string - * @access public - * @see SecurityComponent::requireAuth - */ - function authToken($id = null) { - if (!isset($this->params['_Token']) || empty($this->params['_Token']) || empty($this->current) || in_array('_Token.key', $this->inputFields)) { - return false; - } - if (empty($id) && $id != false) { - $id = $this->current['id'] . 'Token' . mt_rand(); - } - return $this->hidden('_Token.key', array('value' => $this->params['_Token']['key'], 'id' => $id)); - } /** * Creates file input widget. * @@ -1183,7 +1111,7 @@ class FormHelper extends AppHelper { * @see FormHelper::input() */ function generateInputDiv($tagName, $prompt, $required = false, $errorMsg = null, $size = 20, $htmlOptions = null) { - trigger_error(__('Deprecated: Use FormHelper::input() or FormHelper::text() instead', true), E_USER_WARNING); + trigger_error(__('Deprecated: Use FormHelper::input() or FormHelper::text() instead'), E_USER_WARNING); $htmlOptions['id'] = strtolower(str_replace('/', '_', $tagName)); $htmlAttributes = $htmlOptions; $htmlAttributes['size'] = $size; @@ -1207,7 +1135,7 @@ class FormHelper extends AppHelper { * @see FormHelper::input() */ function generateCheckboxDiv($tagName, $prompt, $required = false, $errorMsg = null, $htmlOptions = null) { - trigger_error(__('Deprecated: Use FormHelper::input() or FormHelper::checkbox() instead', true), E_USER_WARNING); + trigger_error(__('Deprecated: Use FormHelper::input() or FormHelper::checkbox() instead'), E_USER_WARNING); $htmlOptions['class'] = "inputCheckbox"; $htmlOptions['id'] = strtolower(str_replace('/', '_', $tagName)); $str = $this->Html->checkbox($tagName, null, $htmlOptions); @@ -1230,7 +1158,7 @@ class FormHelper extends AppHelper { * @see FormHelper::input() */ function generateDate($tagName, $prompt, $required = false, $errorMsg = null, $size = 20, $htmlOptions = null, $selected = null) { - trigger_error(__('Deprecated: Use FormHelper::input() instead', true), E_USER_WARNING); + trigger_error(__('Deprecated: Use FormHelper::input() instead'), E_USER_WARNING); $htmlOptions['id']=strtolower(str_replace('/', '_', $tagName)); $str = $this->dateTime($tagName, 'MDY', 'NONE', $selected, $htmlOptions); $strLabel = $this->label($tagName, $prompt); @@ -1253,7 +1181,7 @@ class FormHelper extends AppHelper { * @see FormHelper::input() */ function generateTime($tagName, $prompt, $required = false, $errorMsg = null, $size = 20, $htmlOptions = null, $selected = null) { - trigger_error(__('Deprecated: Use FormHelper::input() instead', true), E_USER_WARNING); + trigger_error(__('Deprecated: Use FormHelper::input() instead'), E_USER_WARNING); $str = $this->dateTime($tagName, 'NONE', '24', $selected, $htmlOptions); $strLabel = $this->label($tagName, $prompt); $divClass = "optional"; @@ -1275,7 +1203,7 @@ class FormHelper extends AppHelper { * @see FormHelper::input() */ function generateDateTime($tagName, $prompt, $required = false, $errorMsg = null, $size = 20, $htmlOptions = null, $selected = null) { - trigger_error(__('Deprecated: Use FormHelper::input() instead', true), E_USER_WARNING); + trigger_error(__('Deprecated: Use FormHelper::input() instead'), E_USER_WARNING); $htmlOptions['id']=strtolower(str_replace('/', '_', $tagName)); $str = $this->dateTime($tagName, 'MDY', '12', $selected, $htmlOptions, null, false); $strLabel = $this->label($tagName, $prompt); @@ -1298,7 +1226,7 @@ class FormHelper extends AppHelper { * @see FormHelper::input() */ function generateAreaDiv($tagName, $prompt, $required = false, $errorMsg = null, $cols = 60, $rows = 10, $htmlOptions = null) { - trigger_error(__('Deprecated: Use FormHelper::input() instead', true), E_USER_WARNING); + trigger_error(__('Deprecated: Use FormHelper::input() instead'), E_USER_WARNING); $htmlOptions['id'] = strtolower(str_replace('/', '_', $tagName)); $htmlAttributes = $htmlOptions; $htmlAttributes['cols'] = $cols; @@ -1324,7 +1252,7 @@ class FormHelper extends AppHelper { * @see FormHelper::input() */ function generateSelectDiv($tagName, $prompt, $options, $selected = null, $selectAttr = null, $optionAttr = null, $required = false, $errorMsg = null) { - trigger_error(__('Deprecated: Use FormHelper::input() or FormHelper::select() instead', true), E_USER_WARNING); + trigger_error(__('Deprecated: Use FormHelper::input() or FormHelper::select() instead'), E_USER_WARNING); $selectAttr['id'] = strtolower(str_replace('/', '_', $tagName)); $str = $this->Html->selectTag($tagName, $options, $selected, $selectAttr, $optionAttr); $strLabel = $this->label($tagName, $prompt); @@ -1347,7 +1275,7 @@ class FormHelper extends AppHelper { * @see FormHelper::input() */ function generateSubmitDiv($displayText, $htmlOptions = null) { - trigger_error(__('Deprecated: Use FormHelper::submit() instead', true), E_USER_WARNING); + trigger_error(__('Deprecated: Use FormHelper::submit() instead'), E_USER_WARNING); return $this->divTag('submit', $this->Html->submit($displayText, $htmlOptions)); } /** @@ -1355,7 +1283,7 @@ class FormHelper extends AppHelper { * @see FormHelper::inputs() */ function generateFields($fields, $readOnly = false) { - trigger_error(__('Deprecated: Use FormHelper::input() instead', true), E_USER_WARNING); + trigger_error(__('Deprecated: Use FormHelper::input() instead'), E_USER_WARNING); $strFormFields = ''; foreach($fields as $field) { @@ -1472,7 +1400,7 @@ class FormHelper extends AppHelper { * @see FormHelper::label() */ function labelTag($tagName, $text) { - trigger_error(__('Deprecated: Use FormHelper::label() instead', true), E_USER_WARNING); + trigger_error(__('Deprecated: Use FormHelper::label() instead'), E_USER_WARNING); return sprintf($this->Html->tags['label'], Inflector::camelize(r('/', '_', $tagName)), $text); } /** @@ -1480,7 +1408,7 @@ class FormHelper extends AppHelper { * @see HtmlHelper::div */ function divTag($class, $text) { - trigger_error(__('(FormHelper::divTag) Deprecated: Use HtmlHelper::div instead', true), E_USER_WARNING); + //trigger_error(__('(FormHelper::divTag) Deprecated: Use HtmlHelper::div instead'), E_USER_WARNING); return sprintf(TAG_DIV, $class, $text); } /** @@ -1488,7 +1416,7 @@ class FormHelper extends AppHelper { * @see HtmlHelper::para */ function pTag($class, $text) { - trigger_error(__('(FormHelper::pTag) Deprecated: Use HtmlHelper::para instead', true), E_USER_WARNING); + //trigger_error(__('(FormHelper::pTag) Deprecated: Use HtmlHelper::para instead'), E_USER_WARNING); return sprintf(TAG_P_CLASS, $class, $text); } } diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index cc3440f58..5757f748a 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -93,12 +93,12 @@ } } -/** - * Short description for class. - * - * @package cake.tests - * @subpackage cake.tests.cases.libs.view.helpers - */ + /** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.view.helpers + */ class OpenidUrl extends Model { var $useTable = false; var $primaryKey = 'id'; @@ -635,23 +635,12 @@ class FormHelperTest extends CakeTestCase { $this->assertPattern('/^]+action="\/contacts\/edit\/1"[^<>]*>/', $result); $this->assertNoPattern('/^]+[^id|method|action]=[^<>]*>/', $result); - $this->Form->params['_Token']['key'] = 'blah'; - $result = $this->Form->create('Contact'); - $this->assertPattern('/]+type="hidden"[^<>]+>/', $result); - $this->assertPattern('/]+name="data\[_Token\]\[key\]"[^<>]*>/', $result); - $result = $this->Form->create('Contact', array('id' => 'TestId')); $this->assertPattern('/id="TestId"/', $result); } function testFormEnd() { $this->assertEqual($this->Form->end(), ''); - $result = $this->Form->end(true); - $this->assertPattern('/^<\/div><\/form>$/', $result); - $result = $this->Form->end('Send'); - $this->assertPattern('/^<\/div><\/form>$/', $result); - $result = $this->Form->end(array('submit' => 'Super')); - $this->assertPattern('/^<\/div><\/form>$/', $result); } function tearDown() {