Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

important bug fix with encoding

  • Loading branch information...
commit c7106020848133aa991956529a8fd308bad7bdeb 1 parent 7834d2b
Mark S. authored
2  !CHANGES!.txt
View
@@ -12,7 +12,7 @@ SearchableBehavior:
PRGComponent:
fix for not throwing warnings
- encode/decode fix with / and - (removal of the str_replace)
+ encode/decode fix with / and - str_replace was at the wrong side of the encoding process!)
10 Controller/Component/PrgComponent.php
View
@@ -113,7 +113,8 @@ public function presetForm($model) {
if ($this->encode == true || isset($field['encode']) && $field['encode'] == true) {
// Its important to set it also back to the controllers passed args!
if (isset($args[$field['field']])) {
- $fieldContent = $args[$field['field']]; //str_replace(array('-', '_'), array('+', '/'), $args[$field['field']]);
+ $fieldContent = $args[$field['field']];
+ $fieldContent = str_replace(array('-', '_'), array('/', '='), $fieldContent);
$this->controller->passedArgs[$field['field']] = $args[$field['field']] = base64_decode($fieldContent);
}
}
@@ -164,8 +165,11 @@ public function serializeParams(&$data) {
}
if ($this->encode == true || isset($field['encode']) && $field['encode'] == true) {
- $fieldContent = $data[$field['field']]; //str_replace(array('+', '/'), array('-', '_'), $data[$field['field']]);
- $data[$field['field']] = base64_encode($fieldContent);
+ $fieldContent = $data[$field['field']];
+ $tmp = base64_encode($fieldContent);
+ //replace chars base64 uses that would mess up the url
+ $tmp = str_replace(array('/', '='), array('-', '_'), $tmp);
+ $data[$field['field']] = $tmp;
}
}
return $data;
148 Model/Behavior/SearchableBehavior.php
View
@@ -35,25 +35,25 @@ class SearchableBehavior extends ModelBehavior {
*/
protected $_defaults = array(
'wildcards' => true,
- 'wildcardAny' => '*',
- 'wildcardOne' => '~',
+ 'wildcardAny' => '*', //on windows/unix/mac thats the default one
+ 'wildcardOne' => '?', //on windows/unix/mac thats the default one
'like' => array('before'=>true, 'after'=>true)
);
/**
* Configuration of model
*
- * @param AppModel $model
+ * @param AppModel $Model
* @param array $config
*/
- public function setup(Model $model, $config = array()) {
- $this->settings[$model->alias] = array_merge($this->_defaults, $config);
- if (!isset($model->filterArgs)) {
+ public function setup(Model $Model, $config = array()) {
+ $this->settings[$Model->alias] = array_merge($this->_defaults, $config);
+ if (!isset($Model->filterArgs)) {
return;
}
- foreach ($model->filterArgs as $key => $val) {
+ foreach ($Model->filterArgs as $key => $val) {
if (!isset($val['name'])) {
- $model->filterArgs[$key]['name'] = $key;
+ $Model->filterArgs[$key]['name'] = $key;
}
}
}
@@ -66,19 +66,19 @@ public function setup(Model $model, $config = array()) {
* @param array $data Criteria of key->value pairs from post/named parameters
* @return array Array of conditions that express the conditions needed for the search.
*/
- public function parseCriteria(Model $model, $data) {
+ public function parseCriteria(Model $Model, $data) {
$conditions = array();
- foreach ($model->filterArgs as $field) {
+ foreach ($Model->filterArgs as $field) {
if (in_array($field['type'], array('string', 'like'))) {
- $this->_addCondLike($model, $conditions, $data, $field);
+ $this->_addCondLike($Model, $conditions, $data, $field);
} elseif (in_array($field['type'], array('int', 'value'))) {
- $this->_addCondValue($model, $conditions, $data, $field);
+ $this->_addCondValue($Model, $conditions, $data, $field);
} elseif ($field['type'] == 'expression') {
- $this->_addCondExpression($model, $conditions, $data, $field);
+ $this->_addCondExpression($Model, $conditions, $data, $field);
} elseif ($field['type'] == 'query') {
- $this->_addCondQuery($model, $conditions, $data, $field);
+ $this->_addCondQuery($Model, $conditions, $data, $field);
} elseif ($field['type'] == 'subquery') {
- $this->_addCondSubquery($model, $conditions, $data, $field);
+ $this->_addCondSubquery($Model, $conditions, $data, $field);
}
}
return $conditions;
@@ -90,14 +90,14 @@ public function parseCriteria(Model $model, $data) {
* @param object Model
* @return boolean always true
*/
- public function validateSearch(Model $model, $data = null) {
+ public function validateSearch(Model $Model, $data = null) {
if (!empty($data)) {
- $model->set($data);
+ $Model->set($data);
}
- $keys = array_keys($model->data[$model->alias]);
+ $keys = array_keys($Model->data[$Model->alias]);
foreach ($keys as $key) {
- if (empty($model->data[$model->alias][$key])) {
- unset($model->data[$model->alias][$key]);
+ if (empty($Model->data[$Model->alias][$key])) {
+ unset($Model->data[$Model->alias][$key]);
}
}
return true;
@@ -110,10 +110,10 @@ public function validateSearch(Model $model, $data = null) {
* @param array $vars
* @return array, filtered args
*/
- public function passedArgs(Model $model, $vars) {
+ public function passedArgs(Model $Model, $vars) {
$result = array();
foreach ($vars as $var => $val) {
- if (in_array($var, Set::extract($model->filterArgs, '{n}.name'))) {
+ if (in_array($var, Set::extract($Model->filterArgs, '{n}.name'))) {
$result[$var] = $val;
}
}
@@ -129,27 +129,27 @@ public function passedArgs(Model $model, $vars) {
* @return array Array of records
* @link http://book.cakephp.org/view/1018/find
*/
- public function getQuery(Model $model, $type = 'first', $query = array()) {
- $model->findQueryType = $type;
- $model->id = $model->getID();
- $query = $model->buildQuery($type, $query);
+ public function getQuery(Model $Model, $type = 'first', $query = array()) {
+ $Model->findQueryType = $type;
+ $Model->id = $Model->getID();
+ $query = $Model->buildQuery($type, $query);
$this->findQueryType = null;
- return $this->__queryGet($model, $query);
+ return $this->__queryGet($Model, $query);
}
/**
* Clear all associations
*
- * @param AppModel $model
+ * @param AppModel $Model
* @param bool $reset
*/
- public function unbindAllModels(Model $model, $reset = false) {
+ public function unbindAllModels(Model $Model, $reset = false) {
$assocs = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
$unbind = array();
foreach ($assocs as $assoc) {
- $unbind[$assoc] = array_keys($model->{$assoc});
+ $unbind[$assoc] = array_keys($Model->{$assoc});
}
- $model->unbindModel($unbind, $reset);
+ $Model->unbindModel($unbind, $reset);
}
/**
@@ -157,24 +157,24 @@ public function unbindAllModels(Model $model, $reset = false) {
* example "makePhoneCondition": $cond = array('OR' => array_merge($this->condLike('cell_number', $filter), $this->condLike('landline_number', $filter, array('before'=>false))));
* 2011-07-06 ms
*/
- public function condLike(Model $model, $name, $data, $field = array()) {
+ public function condLike(Model $Model, $name, $data, $field = array()) {
$conditions = array();
$field['name'] = $name;
if (!is_array($data)) {
$data = array($name=>$data);
}
- return $this->_addCondLike($model, $conditions, $data, $field);
+ return $this->_addCondLike($Model, $conditions, $data, $field);
}
/**
* replace substitions with original wildcards
* but first, escape the original wildcards in the text to use them as normal search text
- * @param Model $model
+ * @param Model $Model
* @param string $queryLikeString
* @return string $queryLikeString
*/
- public function formatLike(Model $model, $data, $options = array()) {
- $options = am($this->settings[$model->alias], $options);
+ public function formatLike(Model $Model, $data, $options = array()) {
+ $options = am($this->settings[$Model->alias], $options);
if (!$options['wildcards']) {
return $data;
}
@@ -202,32 +202,32 @@ public function formatLike(Model $model, $data, $options = array()) {
/**
* return the current chars for querying LIKE statements on this model
- * @param Model $model Reference to the model
+ * @param Model $Model Reference to the model
* @return array, [one=>..., any=>...]
*/
- public function getLikeQueryChars(Model $model, $options) {
- $options = am($this->settings[$model->alias], $options);
+ public function getLikeQueryChars(Model $Model, $options) {
+ $options = am($this->settings[$Model->alias], $options);
return array('one' => $options['wildcardOne'], 'any' => $options['wildcardAny']);
}
/**
* Add Conditions based on fuzzy comparison
*
- * @param AppModel $model Reference to the model
+ * @param AppModel $Model Reference to the model
* @param array $conditions existing Conditions collected for the model
* @param array $data Array of data used in search query
* @param array $field Field definition information
* @return array of conditions.
*/
- protected function _addCondLike(Model $model, &$conditions, $data, $field) {
+ protected function _addCondLike(Model $Model, &$conditions, $data, $field) {
$fieldName = $field['name'];
if (isset($field['field'])) {
$fieldName = $field['field'];
}
if (strpos($fieldName, '.') === false) {
- $fieldName = $model->alias . '.' . $fieldName;
+ $fieldName = $Model->alias . '.' . $fieldName;
}
- $field = array_merge($this->settings[$model->alias]['like'], $field);
+ $field = array_merge($this->settings[$Model->alias]['like'], $field);
if (empty($data[$field['name']])) {
return $conditions;
}
@@ -238,7 +238,7 @@ protected function _addCondLike(Model $model, &$conditions, $data, $field) {
$field['after'] = '%';
}
//if both before and after are false, LIKE allows custom placeholders, % and _ are always treated as normal chars
- $options = $this->settings[$model->alias];
+ $options = $this->settings[$Model->alias];
$from = $to = $substFrom = $substTo = array();
if ($options['wildcardAny'] != '%' || ($field['before'] !== false || $field['after'] !== false)) {
$from[] = '%';
@@ -270,19 +270,19 @@ protected function _addCondLike(Model $model, &$conditions, $data, $field) {
/**
* Add Conditions based on exact comparison
*
- * @param AppModel $model Reference to the model
+ * @param AppModel $Model Reference to the model
* @param array $conditions existing Conditions collected for the model
* @param array $data Array of data used in search query
* @param array $field Field definition information
* @return array of conditions.
*/
- protected function _addCondValue(Model $model, &$conditions, $data, $field) {
+ protected function _addCondValue(Model $Model, &$conditions, $data, $field) {
$fieldName = $field['name'];
if (isset($field['field'])) {
$fieldName = $field['field'];
}
if (strpos($fieldName, '.') === false) {
- $fieldName = $model->alias . '.' . $fieldName;
+ $fieldName = $Model->alias . '.' . $fieldName;
}
if (!empty($data[$field['name']]) || (isset($data[$field['name']]) && ($data[$field['name']] === 0 || $data[$field['name']] === '0'))) {
$conditions[$fieldName] = $data[$field['name']];
@@ -293,15 +293,15 @@ protected function _addCondValue(Model $model, &$conditions, $data, $field) {
/**
* Add Conditions based query to search conditions.
*
- * @param Object $model Instance of AppModel
+ * @param Object $Model Instance of AppModel
* @param array $conditions Existing conditions.
* @param array $data Data for a field.
* @param array $field Info for field.
* @return array of conditions modified by this method.
*/
- protected function _addCondQuery(Model $model, &$conditions, $data, $field) {
- if ((method_exists($model, $field['method']) || $this->__checkBehaviorMethods($model, $field['method'])) && (!empty($data[$field['name']]) || (isset($data[$field['name']]) && ($data[$field['name']] === 0 || $data[$field['name']] === '0')))) {
- $conditionsAdd = $model->{$field['method']}($data, $field);
+ protected function _addCondQuery(Model $Model, &$conditions, $data, $field) {
+ if ((method_exists($Model, $field['method']) || $this->__checkBehaviorMethods($Model, $field['method'])) && (!empty($data[$field['name']]) || (isset($data[$field['name']]) && ($data[$field['name']] === 0 || $data[$field['name']] === '0')))) {
+ $conditionsAdd = $Model->{$field['method']}($data, $field);
$conditions = array_merge($conditions, (array)$conditionsAdd);
}
return $conditions;
@@ -310,16 +310,16 @@ protected function _addCondQuery(Model $model, &$conditions, $data, $field) {
/**
* Add Conditions based expressions to search conditions.
*
- * @param Object $model Instance of AppModel
+ * @param Object $Model Instance of AppModel
* @param array $conditions Existing conditions.
* @param array $data Data for a field.
* @param array $field Info for field.
* @return array of conditions modified by this method.
*/
- protected function _addCondExpression(Model $model, &$conditions, $data, $field) {
+ protected function _addCondExpression(Model $Model, &$conditions, $data, $field) {
$fieldName = $field['field'];
- if ((method_exists($model, $field['method']) || $this->__checkBehaviorMethods($model, $field['method'])) && (!empty($data[$field['name']]) || (isset($data[$field['name']]) && ($data[$field['name']] === 0 || $data[$field['name']] === '0')))) {
- $fieldValues = $model->{$field['method']}($data, $field);
+ if ((method_exists($Model, $field['method']) || $this->__checkBehaviorMethods($Model, $field['method'])) && (!empty($data[$field['name']]) || (isset($data[$field['name']]) && ($data[$field['name']] === 0 || $data[$field['name']] === '0')))) {
+ $fieldValues = $Model->{$field['method']}($data, $field);
if (!empty($conditions[$fieldName]) && is_array($conditions[$fieldName])) {
$conditions[$fieldName] = array_unique(array_merge(array($conditions[$fieldName]), array($fieldValues)));
} else {
@@ -332,16 +332,16 @@ protected function _addCondExpression(Model $model, &$conditions, $data, $field)
/**
* Add Conditions based subquery to search conditions.
*
- * @param Object $model Instance of AppModel
+ * @param Object $Model Instance of AppModel
* @param array $conditions Existing conditions.
* @param array $data Data for a field.
* @param array $field Info for field.
* @return array of conditions modified by this method.
*/
- protected function _addCondSubquery(Model $model, &$conditions, $data, $field) {
+ protected function _addCondSubquery(Model $Model, &$conditions, $data, $field) {
$fieldName = $field['field'];
- if ((method_exists($model, $field['method']) || $this->__checkBehaviorMethods($model, $field['method'])) && (!empty($data[$field['name']]) || (isset($data[$field['name']]) && ($data[$field['name']] === 0 || $data[$field['name']] === '0')))) {
- $subquery = $model->{$field['method']}($data, $field);
+ if ((method_exists($Model, $field['method']) || $this->__checkBehaviorMethods($Model, $field['method'])) && (!empty($data[$field['name']]) || (isset($data[$field['name']]) && ($data[$field['name']] === 0 || $data[$field['name']] === '0')))) {
+ $subquery = $Model->{$field['method']}($data, $field);
$conditions[] = array("$fieldName in ($subquery)");
}
return $conditions;
@@ -351,13 +351,13 @@ protected function _addCondSubquery(Model $model, &$conditions, $data, $field) {
* Helper method for getQuery.
* extension of dbosource method. Create association query.
*
- * @param AppModel $model
+ * @param AppModel $Model
* @param array $queryData
* @param integer $recursive
*/
- private function __queryGet(Model $model, $queryData = array()) {
+ private function __queryGet(Model $Model, $queryData = array()) {
/** @var DboSource $db */
- $db = $model->getDataSource();
+ $db = $Model->getDataSource();
$queryData = $this->_scrubQueryData($queryData);
$recursive = null;
$byPass = false;
@@ -370,43 +370,43 @@ private function __queryGet(Model $model, $queryData = array()) {
}
if (!is_null($recursive)) {
- $_recursive = $model->recursive;
- $model->recursive = $recursive;
+ $_recursive = $Model->recursive;
+ $Model->recursive = $recursive;
}
if (!empty($queryData['fields'])) {
$byPass = true;
- $queryData['fields'] = $db->fields($model, null, $queryData['fields']);
+ $queryData['fields'] = $db->fields($Model, null, $queryData['fields']);
} else {
- $queryData['fields'] = $db->fields($model);
+ $queryData['fields'] = $db->fields($Model);
}
- $_associations = $model->associations();
+ $_associations = $Model->associations();
- if ($model->recursive == -1) {
+ if ($Model->recursive == -1) {
$_associations = array();
- } elseif ($model->recursive == 0) {
+ } elseif ($Model->recursive == 0) {
unset($_associations[2], $_associations[3]);
}
foreach ($_associations as $type) {
- foreach ($model->{$type} as $assoc => $assocData) {
- $linkModel = $model->{$assoc};
+ foreach ($Model->{$type} as $assoc => $assocData) {
+ $linkModel = $Model->{$assoc};
$external = isset($assocData['external']);
$linkModel->getDataSource();
- if ($model->useDbConfig === $linkModel->useDbConfig) {
+ if ($Model->useDbConfig === $linkModel->useDbConfig) {
if ($byPass) {
$assocData['fields'] = false;
}
- if (true === $db->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null)) {
+ if (true === $db->generateAssociationQuery($Model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null)) {
$linkedModels[$type . '/' . $assoc] = true;
}
}
}
}
- return trim($db->generateAssociationQuery($model, null, null, null, null, $queryData, false, $null));
+ return trim($db->generateAssociationQuery($Model, null, null, null, null, $queryData, false, $null));
}
/**
4 Test/Case/Controller/Component/PrgComponentTest.php
View
@@ -388,7 +388,7 @@ public function testSerializeParamsWithEncoding() {
$this->Controller->Prg->encode = true;
$test = array('title' => 'Something new');
$result = $this->Controller->Prg->serializeParams($test);
- $this->assertEqual($result['title'], base64_encode('Something new'));
+ $this->assertEqual($result['title'], str_replace(array('/', '='), array('-', '_'), base64_encode('Something new')));
}
/**
@@ -407,7 +407,7 @@ public function testSerializeParamsWithEncodingAndSpace() {
$this->Controller->Prg->encode = true;
$testData = $test = array('title' => 'Something new');
$result = $this->Controller->Prg->serializeParams($test);
- $this->assertEqual($result['title'], base64_encode('Something new'));
+ $this->assertEqual($result['title'], str_replace(array('/', '='), array('-', '_'), base64_encode('Something new')));
$this->Controller->passedArgs = $result;
$this->Controller->Prg->presetForm('Post');
6 Test/Case/Model/Behavior/SearchableBehaviorTest.php
View
@@ -287,19 +287,19 @@ public function testLikeCondition() {
$this->assertEqual($result, $expected);
# now custom like should be possible
- $data = array('faketitle' => '*First~');
+ $data = array('faketitle' => '*First?');
$this->Article->Behaviors->Searchable->settings['Article']['like']['after'] = false;
$result = $this->Article->parseCriteria($data);
$expected = array('Article.title LIKE' => '%First_');
$this->assertEqual($result, $expected);
# now we try the default wildcards % and _
- $data = array('faketitle' => '*First~');
+ $data = array('faketitle' => '*First?');
$this->Article->Behaviors->Searchable->settings['Article']['like']['after'] = false;
$this->Article->Behaviors->Searchable->settings['Article']['wildcardAny'] = '%';
$this->Article->Behaviors->Searchable->settings['Article']['wildcardOne'] = '_';
$result = $this->Article->parseCriteria($data);
- $expected = array('Article.title LIKE' => '*First~');
+ $expected = array('Article.title LIKE' => '*First?');
$this->assertEqual($result, $expected);
$data = array('faketitle' => '%First_');
Please sign in to comment.
Something went wrong with that request. Please try again.