浏览代码

Correct merges and connection close for HTTP/1.1 and file_get_contents with remote files.

euromark 11 年之前
父节点
当前提交
b502edcf67

+ 1 - 8
Console/Command/IndexShell.php

@@ -60,14 +60,7 @@ class IndexShell extends AppShell {
 	 */
 	public function initialize() {
 		parent::initialize();
-		/*
-		if (file_exists(APP . 'Config' . DS . 'index.php')) {
-			include(APP . 'Config' . DS . 'index.php');
-			if (!empty($config)) {
-				$this->settings = array_merge($this->settings, $config);
-			}
-		}
-		*/
+
 		$this->_loadModels();
 	}
 

+ 2 - 1
Controller/Component/AjaxComponent.php

@@ -35,7 +35,8 @@ class AjaxComponent extends Component {
 	 * @param array $config
 	 */
 	public function __construct(ComponentCollection $collection, $config = array()) {
-		$config = array_merge($this->_defaultConfig, (array)Configure::read('Ajax'), $config);
+		$defaults = (array)Configure::read('Ajax') + $this->_defaultConfig;
+		$config += $defaults;
 		parent::__construct($collection, $config);
 	}
 

+ 1 - 1
Controller/Component/Auth/TinyAuthorize.php

@@ -56,7 +56,7 @@ class TinyAuthorize extends BaseAuthorize {
 	 * @param array $config
 	 */
 	public function __construct(ComponentCollection $Collection, $config = array()) {
-		$config = array_merge($this->_defaultConfig, $config);
+		$config += $this->_defaultConfig;
 		parent::__construct($Collection, $config);
 
 		if (Cache::config($config['cache']) === false) {

+ 2 - 1
Controller/Component/AuthExtComponent.php

@@ -49,7 +49,8 @@ class AuthExtComponent extends AuthComponent {
 	 * @param array $config
 	 */
 	public function __construct(ComponentCollection $Collection, $config = array()) {
-		$config = array_merge($this->_defaultConfig, (array)Configure::read('Auth'), $config);
+		$defaults = (array)Configure::read('Auth') + $this->_defaultConfig;
+		$config += $defaults;
 
 		parent::__construct($Collection, $config);
 	}

+ 2 - 1
Controller/Component/MobileComponent.php

@@ -65,7 +65,8 @@ class MobileComponent extends Component {
 	 * @param array $config
 	 */
 	public function __construct(ComponentCollection $collection, $config = array()) {
-		$config = array_merge($this->_defaultConfig, (array)Configure::read('Mobile'), $config);
+		$defaults = (array)Configure::read('Mobile') + $this->_defaultConfig;
+		$config += $defaults;
 		parent::__construct($collection, $config);
 	}
 

+ 3 - 3
Lib/CurrencyBitcoinLib.php

@@ -18,7 +18,7 @@ class CurrencyBitcoinLib {
 	 * @see https://bitmarket.eu/api
 	 */
 	public function bitmarket($options = array()) {
-		$options = array_merge($this->settings, $options);
+		$options += $this->settings;
 		$url = 'https://bitmarket.eu/api/ticker';
 		$res = $this->_getBitmarket($url);
 
@@ -40,7 +40,7 @@ class CurrencyBitcoinLib {
 	 * @see http://bitcoincharts.com/about/markets-api/
 	 */
 	public function bitcoincharts($options = array()) {
-		$options = array_merge($this->settings, $options);
+		$options += $this->settings;
 		$url = 'http://bitcoincharts.com/t/markets.json';
 		$res = $this->_getBitcoincharts($url);
 		if (!$res) {
@@ -67,7 +67,7 @@ class CurrencyBitcoinLib {
 	 * - api
 	 */
 	public function rate($options = array()) {
-		$options = array_merge($this->settings, $options);
+		$options += $this->settings;
 		$res = $this->{$options['api']}($options);
 
 		if ($res && isset($res['sell'])) {

+ 2 - 0
Lib/EmailLib.php

@@ -253,6 +253,8 @@ class EmailLib extends CakeEmail {
 	 * Read the file contents and return a base64 version of the file contents.
 	 * Overwrite parent to avoid File class and file_exists to false negative existent
 	 * remove images.
+	 * Also fixes file_get_contents (used via File class) to close the connection again
+	 * after getting remote files. So far it would have kept the connection open in HTTP/1.1.
 	 *
 	 * @param string $path The absolute path to the file to read.
 	 * @return string File contents in base64 encoding

+ 4 - 2
Lib/GeocodeLib.php

@@ -239,7 +239,8 @@ class GeocodeLib {
 	public function geocode($address, $params = array()) {
 		$this->reset(false);
 		$this->_setDebug('geocode', compact('address', 'params'));
-		$this->setParams(array_merge($params, array('address' => $address)));
+		$params = array('address' => $address) + $params;
+		$this->setParams($params);
 
 		$count = 0;
 		$requestUrl = $this->_url();
@@ -323,7 +324,8 @@ class GeocodeLib {
 		$this->reset(false);
 		$this->_setDebug('reverseGeocode', compact('lat', 'lng', 'params'));
 		$latlng = $lat . ',' . $lng;
-		$this->setParams(array_merge($params, array('latlng' => $latlng)));
+		$params = array('latlng' => $latlng) + $params;
+		$this->setParams($params);
 
 		$count = 0;
 		$requestUrl = $this->_url();

+ 3 - 1
Lib/IcalLib.php

@@ -115,7 +115,9 @@ class IcalLib {
 /** PARSING **/
 
 	public function parse($url) {
-		if (!file_exists($url) || !($res = file_get_contents($url))) {
+		$context = stream_context_create(
+			array('http' => array('header' => 'Connection: close')));
+		if (!file_exists($url) || !($res = file_get_contents($url, 0, $context))) {
 			return false;
 		}
 		$this->Ical = new ical($url);

+ 6 - 2
Lib/InlineCssLib.php

@@ -260,7 +260,9 @@ class InlineCssLib {
 
 		$css = null;
 		if (!empty($bestCssFilename) && is_file($bestCssFilename)) {
-			$css = file_get_contents($bestCssFilename);
+			$context = stream_context_create(
+				array('http' => array('header' => 'Connection: close')));
+			$css = file_get_contents($bestCssFilename, 0, $context);
 		}
 
 		return $css;
@@ -298,10 +300,12 @@ class InlineCssLib {
 			// First remove the @imports
 			$css = preg_replace("/\@import.*?url\(.*?\).*?;/i", '', $css);
 
+			$context = stream_context_create(
+				array('http' => array('header' => 'Connection: close')));
 			foreach ($matches[1] as $url) {
 				if (preg_match("/^http/i", $url)) {
 					if ($this->importExternalCss) {
-						$css .= file_get_contents($url);
+						$css .= file_get_contents($url, 0, $context);
 					}
 				} else {
 					$css .= $this->_findAndLoadCssFile($url);

+ 2 - 2
Lib/Utility/NumberLib.php

@@ -97,7 +97,7 @@ class NumberLib extends CakeNumber {
 			$formatOptions = array('places' => $formatOptions);
 		}
 		$options = array('before' => '', 'after' => '', 'places' => 2, 'thousands' => self::$_thousands, 'decimals' => self::$_decimals, 'escape' => false);
-		$options = array_merge($options, $formatOptions);
+		$options = $formatOptions + $options;
 
 		if (!empty($options['currency'])) {
 			if (!empty(self::$_symbolRight)) {
@@ -158,7 +158,7 @@ class NumberLib extends CakeNumber {
 			'decimals' => ',', 'thousands' => '.',
 			'spacer' => $currency === 'EUR' ? true : false
 		);
-		$options = array_merge($defaults, $formatOptions);
+		$options = $formatOptions + $defaults;
 
 		if (!empty($options['spacer'])) {
 			$spacer = is_string($options['spacer']) ? $options['spacer'] : ' ';

+ 2 - 2
Lib/Utility/NumberTextLib.php

@@ -73,8 +73,8 @@ class NumberTextLib {
 	protected static function load($lang) {
 		$lang = self::setLang($lang);
 
-		$url = self::$_dir . "$lang.sor";
-		$st = file_get_contents($url);
+		$file = self::$_dir . "$lang.sor";
+		$st = file_get_contents($file);
 		if ($st === false) {
 			return null;
 		}

+ 3 - 3
Lib/Utility/TextLib.php

@@ -265,13 +265,13 @@ class TextLib extends String {
 	 * @return string
 	 */
 	public static function maxWords($value, $words = 100, $options = array()) {
-		$default = array(
+		$defaults = array(
 			'ellipsis' => '...'
 		);
 		if (!empty($options['html']) && Configure::read('App.encoding') === 'UTF-8') {
-			$default['ellipsis'] = "\xe2\x80\xa6";
+			$defaults['ellipsis'] = "\xe2\x80\xa6";
 		}
-		$options = array_merge($default, $options);
+		$options += $defaults;
 
 		if (trim($value) === '') {
 			return '';

+ 1 - 1
Lib/WeatherLib.php

@@ -40,7 +40,7 @@ class WeatherLib {
 	);
 
 	public function __construct() {
-		$this->settings = array_merge($this->settings, (array)Configure::read('Weather'));
+		$this->settings = (array)Configure::read('Weather') + $this->settings;
 	}
 
 	/**

+ 3 - 6
View/Helper/FormExtHelper.php

@@ -199,11 +199,8 @@ class FormExtHelper extends FormHelper {
 	 * @link http://book.cakephp.org/view/1390/Automagic-Form-Elements
 	 */
 	public function inputExt($fieldName, $options = array()) {
-		$options = array_merge(
-			array('before' => null, 'between' => null, 'after' => null, 'format' => null),
-			$this->_inputDefaults,
-			$options
-		);
+		$defaults = $this->_inputDefaults + array('before' => null, 'between' => null, 'after' => null, 'format' => null);
+		$options += $defaults;
 
 		$modelKey = $this->model();
 		$fieldKey = $this->field();
@@ -706,7 +703,7 @@ class FormExtHelper extends FormHelper {
 			'minYear' => date('Y') - 10,
 			'maxYear' => date('Y') + 10
 		);
-		$defaultOptions = array_merge($defaultOptions, (array)Configure::read('Form.date'));
+		$defaultOptions = (array)Configure::read('Form.date') + $defaultOptions;
 
 		$fieldName = Inflector::camelize($fieldName);