Browse Source

minor fixes

euromark 12 years ago
parent
commit
d4c4c88f8e

+ 49 - 0
Controller/Component/CommonComponent.php

@@ -374,6 +374,30 @@ class CommonComponent extends Component {
 		return $url;
 	}
 
+	/**
+	 * Tries to allow super admin access for certain urls via Config.pwd
+	 * Only used in admin actions and only to prevent accidental data loss due to incorrect access.
+	 * Do not assume this to be a safe access control mechanism!
+	 *
+	 * Password can be passed as named param or query string param
+	 *
+	 * @return bool Success
+	 */
+	public function validAdminUrlAccess() {
+		$pwd = Configure::read('Config.pwd');
+		if (!$pwd) {
+			return false;
+		}
+		$urlPwd = $this->getNamedParam('pwd');
+		if (!$urlPwd) {
+			$urlPwd = $this->getQueryParam('pwd');
+		}
+		if (!$urlPwd) {
+			return false;
+		}
+		return $pwd === $urlPwd;
+	}
+
 
 	### Controller Stuff ###
 
@@ -473,6 +497,31 @@ class CommonComponent extends Component {
 	}
 
 	/**
+	 * Automatically add missing url parts of the current url including
+	 * - querystring (especially for 3.x then)
+	 * - named params (until 3.x when they will become deprecated)
+	 * - passed params
+	 *
+	 * @param mixed $url
+	 * @param intger $status
+	 * @param boolean $exit
+	 * @return void
+	 */
+	public function completeRedirect($url = null, $status = null, $exit = true) {
+		if ($url === null) {
+			$url = $this->Controller->request->params;
+			unset($url['named']);
+			unset($url['pass']);
+			unset($url['isAjax']);
+		}
+		if (is_array($url)) {
+			$url += $this->Controller->request->params['named'];
+			$url += $this->Controller->request->params['pass'];
+		}
+		return $this->Controller->redirect($url, $status, $exit);
+	}
+
+	/**
 	 * only redirect to itself if cookies are on
 	 * prevents problems with lost data
 	 * Note: Many pre-HTTP/1.1 user agents do not understand the 303 status. When interoperability with such clients is a concern, the 302 status code may be used instead, since most user agents react to a 302 response as described here for 303.

+ 14 - 7
Lib/HttpSocketLib.php

@@ -32,6 +32,8 @@ class HttpSocketLib {
 
 	public $error = array();
 
+	public $allowRedirects = array(301);
+
 	public function __construct($use = array()) {
 		if (is_array($use)) {
 			foreach ($use as $key => $value) {
@@ -119,6 +121,8 @@ class HttpSocketLib {
 	 * @return string Response or false on failure
 	 */
 	public function _fetch($url, $options) {
+		$allowedCodes = array_merge($this->allowRedirects, array(200, 201, 202, 203, 204, 205, 206));
+
 		if ($options['use']['curl'] && function_exists('curl_init')) {
 			$this->debug = 'curl';
 			$Ch = new CurlLib();
@@ -126,7 +130,7 @@ class HttpSocketLib {
 			$data = $Ch->get($url);
 			$response = $data[0];
 			$statusCode = $data[1]['http_code'];
-			if (!in_array($statusCode, array(200, 201, 202, 203, 204, 205, 206))) {
+			if (!in_array($statusCode, $allowedCodes)) {
 				$this->setError('Error '.$statusCode);
 				return false;
 			}
@@ -138,7 +142,7 @@ class HttpSocketLib {
 
 			$HttpSocket = new HttpSocket(array('timeout' => $options['timeout']));
 			$response = $HttpSocket->get($url);
-			if (!in_array($response->code, array(200, 201, 202, 203, 204, 205, 206))) {
+			if (!in_array($response->code, $allowedCodes)) {
 				return false;
 			}
 			$response = $this->_assertEncoding($response);
@@ -149,10 +153,10 @@ class HttpSocketLib {
 
 			$opts = array(
 				'http' => array(
-				'method' => 'GET',
-				'header' => array('Connection: close'),
-				'timeout' => $options['timeout']
-			)
+					'method' => 'GET',
+					'header' => array('Connection: close'),
+					'timeout' => $options['timeout']
+				)
 			);
 			if (isset($options['http'])) {
 				$opts['http'] = array_merge($opts['http'], $options['http']);
@@ -162,9 +166,12 @@ class HttpSocketLib {
 			}
 			$context = stream_context_create($opts);
 			$response = file_get_contents($url, false, $context);
+			if (!isset($http_response_header)) {
+				return false;
+			}
 			preg_match('/^HTTP.*\s([0-9]{3})/', $http_response_header[0], $matches);
 			$statusCode = (int)$matches[1];
-			if (!in_array($statusCode, array(200, 201, 202, 203, 204, 205, 206))) {
+			if (!in_array($statusCode, $allowedCodes)) {
 				return false;
 			}
 			$response = $this->_assertEncoding($response);

+ 4 - 0
Model/Behavior/KeyValueBehavior.php

@@ -117,6 +117,10 @@ class KeyValueBehavior extends ModelBehavior {
 
 		extract($this->settings[$Model->alias]);
 		foreach ($data as $model => $details) {
+			if ($section && $section !== $model) {
+				continue;
+			}
+
 			foreach ($details as $field => $value) {
 				$newDetail = array();
 				$section = $section ? $section : $model;

+ 14 - 20
Model/MyModel.php

@@ -269,7 +269,6 @@ class MyModel extends Model {
 		return $db->value($content);
 	}
 
-
 	/**
 	 * TODO: move to behavior (Incremental)
 	 * @param mixed id (single string)
@@ -316,22 +315,6 @@ class MyModel extends Model {
 	}
 
 	/**
-	 * improve paginate count for "normal queries"
-	 * @deprecated?
-	 * 2011-04-11 ms
-	 */
-	public function _paginateCount($conditions = null, $recursive = -1, $extra = array()) {
-		$conditions = compact('conditions');
-		if ($recursive != $this->recursive) {
-			$conditions['recursive'] = $recursive;
-		}
-		if ($recursive == -1) {
-			$extra['contain'] = array();
-		}
-		return $this->find('count', array_merge($conditions, $extra));
-	}
-
-	/**
 	 * return the next auto increment id from the current table
 	 * UUIDs will return false
 	 *
@@ -417,7 +400,17 @@ class MyModel extends Model {
 	}
 
 	/**
-	 * Makes a subquery
+	 * Generates a SQL subquery snippet to be used in your actual query.
+	 * Your subquery snippet needs to return a single value or flat array of values.
+	 *
+	 * Example:
+	 *
+	 *   $this->Model->find('first', array(
+	 *     'conditions' => array('NOT' => array('some_id' => $this->Model->subquery(...)))
+	 *   ))
+	 *
+	 * Note: You might have to set `autoFields` to false in order to retrieve only the fields you request:
+	 * http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#containablebehavior-options
 	 *
 	 * @param string $type The type o the query ('count'/'all'/'first' - first only works with some mysql versions)
 	 * @param array $options The options array
@@ -762,9 +755,10 @@ class MyModel extends Model {
 	 * - field (sortField, if not id)
 	 * - reverse: sortDirection (0=normalAsc/1=reverseDesc)
 	 * - displayField: ($this->displayField, if empty)
-	 * @param array qryOptions
+	 * @param array $qryOptions
 	 * - recursive (defaults to -1)
 	 * TODO: try to use core function, TRY TO ALLOW MULTIPLE SORT FIELDS
+	 * @return array
 	 */
 	public function neighbors($id = null, $options = array(), $qryOptions = array()) {
 		$sortField = (!empty($options['field']) ? $options['field'] : 'created');
@@ -785,7 +779,7 @@ class MyModel extends Model {
 		}
 
 		if (empty($id) || empty($data) || empty($data[$this->alias][$sortField])) {
-			return false;
+			return array();
 		} else {
 			$field = $data[$this->alias][$sortField];
 		}

+ 1 - 1
Test/Case/Lib/Utility/TimeLibTest.php

@@ -78,7 +78,7 @@ class TimeLibTest extends MyCakeTestCase {
 
 	public function testNiceDate() {
 		$res = setlocale(LC_TIME, 'de_DE.UTF-8', 'deu_deu');
-		$this->assertTrue(!empty($res));
+		//$this->assertTrue(!empty($res));
 
 		$values = array(
 			array('2009-12-01 00:00:00', FORMAT_NICE_YMD, '01.12.2009'),

+ 2 - 2
View/Helper/CommonHelper.php

@@ -170,11 +170,11 @@ class CommonHelper extends AppHelper {
 	 * @return string $htmlMarkup
 	 * 2008-12-08 ms
 	 */
-	public function metaEquiv($type = null, $value = null, $escape = true) {
+	public function metaEquiv($type, $value, $escape = true) {
 		$tags = array(
 			'meta' => '<meta http-equiv="%s"%s />',
 		);
-		if (empty($value)) {
+		if ($value === null) {
 			return '';
 		}
 		if ($escape) {

+ 15 - 8
View/Helper/FormatHelper.php

@@ -20,6 +20,7 @@ class FormatHelper extends TextHelper {
 	 * @access public
 	 */
 	public $helpers = array('Html', 'Form', 'Tools.Common', 'Tools.Gravatar', 'Tools.PhpThumb');
+
 	/**
 	 * jqueryAccess: {id}Pro, {id}Contra
 	 * 2009-07-24 ms
@@ -40,8 +41,10 @@ class FormatHelper extends TextHelper {
 		return $ret;
 	}
 
-
 	/**
+	 * Display neighbor quicklinks
+	 *
+	 * @param array $neighbors (containing prev and next)
 	 * @param string $field: just field or Model.field syntax
 	 * @param array $options:
 	 * - name: title name: next{Record} (if none is provided, "record" is used - not translated!)
@@ -99,14 +102,23 @@ class FormatHelper extends TextHelper {
 
 		$ret = '<div class="nextPrevNavi">';
 		if (!empty($neighbors['prev'])) {
+			$url = array($neighbors['prev'][$alias]['id'], $prevSlug);
+			if (!empty($options['url'])) {
+				$url += $options['url'];
+			}
 
-			$ret.= $this->Html->link($this->cIcon(ICON_PREV, false).'&nbsp;'.__('prev'.$name), array($neighbors['prev'][$alias]['id'], $prevSlug), array('escape'=>false, 'title'=>$neighbors['prev'][$titleAlias][$titleField]));
+			$ret.= $this->Html->link($this->cIcon(ICON_PREV, false).'&nbsp;'.__('prev'.$name), $url, array('escape'=>false, 'title'=>$neighbors['prev'][$titleAlias][$titleField]));
 		} else {
 			$ret.= $this->cIcon(ICON_PREV_DISABLED, __('noPrev'.$name)).'&nbsp;'.__('prev'.$name);
 }
 		$ret.= '&nbsp;&nbsp;';
 		if (!empty($neighbors['next'])) {
-			$ret.= $this->Html->link($this->cIcon(ICON_NEXT, false).'&nbsp;'.__('next'.$name), array($neighbors['next'][$alias]['id'], $nextSlug), array('escape'=>false, 'title'=>$neighbors['next'][$titleAlias][$titleField]));
+			$url = array($neighbors['next'][$alias]['id'], $prevSlug);
+			if (!empty($options['url'])) {
+				$url += $options['url'];
+			}
+
+			$ret.= $this->Html->link($this->cIcon(ICON_NEXT, false).'&nbsp;'.__('next'.$name), $url, array('escape'=>false, 'title'=>$neighbors['next'][$titleAlias][$titleField]));
 		} else {
 			$ret.= $this->cIcon(ICON_NEXT_DISABLED, __('noNext'.$name)).'&nbsp;'.__('next'.$name);
 		}
@@ -114,8 +126,6 @@ class FormatHelper extends TextHelper {
 		return $ret;
 	}
 
-
-
 	/**
 	 * allows icons to be added on the fly
 	 * NOTE: overriding not allowed by default
@@ -150,9 +160,6 @@ class FormatHelper extends TextHelper {
 		return $icon;
 	}
 
-
-
-
 	/**
 	 * //TODO: move to Format?
 	 * returns img from customImgFolder