Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Addition of new WireHttp class. Add $options support for image sizing…

… functions. Fix minor bugs in ProcessPageList and SessionLoginThrottle.
  • Loading branch information...
commit 6ba6d64c74a1a208a58fc5e5a660460a3b71ae21 1 parent e5fdd83
@ryancramerdesign authored
View
10 site-default/config.php
@@ -146,6 +146,16 @@
$config->wireInputOrder = 'get post';
/**
+ * Default ImageSizer options, as used by $page->image->size(w, h), for example.
+ *
+ */
+$config->imageSizerOptions = array(
+ 'upscaling' => true,
+ 'cropping' => true,
+ 'quality' => 90,
+ );
+
+/**
* Optional 'set names utf8' for sites that need it (this option is deprecated)
*
* This may be used instead of the $config->dbCharset = 'utf8' option, and exists here only for
View
207 wire/core/Http.php
@@ -0,0 +1,207 @@
+<?php
+
+/**
+ * ProcessWire HTTP tools
+ *
+ * Provides capability for sending POST/GET requests to URLs
+ *
+ * ProcessWire 2.x
+ * Copyright (C) 2012 by Ryan Cramer
+ * Licensed under GNU/GPL v2, see LICENSE.TXT
+ *
+ * http://www.processwire.com
+ *
+ */
+
+class WireHttp {
+
+ /**
+ * Headers to include in the request
+ *
+ */
+ protected $headers = array(
+ 'charset' => 'utf-8',
+ );
+
+ /**
+ * Data to send in the request
+ *
+ */
+ protected $data = array();
+
+ /**
+ * Send to a URL using POST
+ *
+ * @param string $url URL to post to (including http:// or https://)
+ * @param array $data Array of data to send (if not already set before)
+ * @return bool|string False on failure or string of contents received on success.
+ *
+ */
+ public function post($url, array $data = array()) {
+ if(!isset($this->headers['content-type'])) $this->setHeader('content-type', 'application/x-www-form-urlencoded; charset=utf-8');
+ return $this->send($url, $data, 'POST');
+ }
+
+ /**
+ * Send to a URL using GET
+ *
+ * @param string $url URL to post to (including http:// or https://)
+ * @return bool|string False on failure or string of contents received on success.
+ *
+ */
+ public function get($url, array $data = array()) {
+ return $this->send($url, $data, 'GET');
+ }
+
+ /**
+ * Set an array of headers, removes any existing headers
+ *
+ */
+ public function setHeaders(array $headers) {
+ foreach($headers as $key => $value) {
+ $this->setHeader($key, $value);
+ }
+ return $this;
+ }
+
+ /**
+ * Send an individual header to send
+ *
+ */
+ public function setHeader($key, $value) {
+ $key = strtolower($key);
+ $this->headers[$key] = $value;
+ return $this;
+ }
+
+ /**
+ * Set an array of data, removes any existing data
+ *
+ */
+ public function setData(array $data) {
+ $this->data = $data;
+ return $this;
+ }
+
+ /**
+ * Set a variable to be included in the POST/GET request
+ *
+ * @param string $key
+ * @param string|int $value
+ * @return $this
+ *
+ */
+ public function set($key, $value) {
+ $this->$data[$key] = $value;
+ return $this;
+ }
+
+ /**
+ * Allows setting to $data via $http->key = $value
+ *
+ */
+ public function __set($key, $value) {
+ $this->set($key, $value);
+ }
+
+ /**
+ * Enables getting from $data via $http->key
+ *
+ */
+ public function __get($key) {
+ return array_key_exists($key, $this->data) ? $this->data[$key] : null;
+ }
+
+ /**
+ * Send the given $data array to a URL using either POST or GET
+ *
+ * @param string $url URL to post to (including http:// or https://)
+ * @param array $data Array of data to send (if not already set before)
+ * @param string $method Method to use (either POST or GET)
+ * @return bool|string False on failure or string of contents received on success.
+ *
+ */
+ protected function send($url, array $data = array(), $method = 'POST') {
+
+ if(count($data)) $this->setData($data);
+ if($method !== 'GET') $method = 'POST';
+
+ if(!ini_get('allow_url_fopen')) return $this->sendSocket($url, $method);
+
+ $content = http_build_query($this->data);
+ $this->setHeader('content-length', strlen($content));
+
+ $header = '';
+ foreach($this->headers as $key => $value) $header .= "$key: $value\r\n";
+
+ $options = array(
+ 'http' => array(
+ 'method' => $method,
+ 'content' => $content,
+ 'header' => $header
+ )
+ );
+
+ $context = @stream_context_create($options);
+ $fp = @fopen($url, 'rb', false, $context);
+ if(!$fp) return false;
+
+ $result = @stream_get_contents($fp);
+ return $result;
+ }
+
+ /**
+ * Alternate method of sending when allow_url_fopen isn't allowed
+ *
+ */
+ protected function sendSocket($url, $method = 'POST') {
+
+ $timeoutSeconds = 3;
+ if($method != 'GET') $method = 'POST';
+
+ $info = parse_url($url);
+ $host = $info['host'];
+ $path = empty($info['path']) ? '/' : $info['path'];
+
+ if($info['scheme'] == 'https') {
+ $port = 443;
+ $scheme = 'ssl://';
+ } else {
+ $port = empty($info['port']) ? 80 : $info['port'];
+ $scheme = '';
+ }
+
+ $content = http_build_query($this->data);
+
+ $this->setHeader('content-length', strlen($content));
+
+ $request = "$method $path HTTP/1.0\r\nHost: $host\r\n";
+
+ foreach($this->headers as $key => $value) {
+ $request .= "$key: $value\r\n";
+ }
+
+ $response = '';
+ $errno = '';
+ $errstr = '';
+
+ if(false !== ($fs = fsockopen($scheme . $host, $port, $errno, $errstr, $timeoutSeconds))) {
+ fwrite($fs, "$request\r\n$content");
+ while(!feof($fs)) {
+ // get 1 tcp-ip packet per iteration
+ $response .= fgets($fs, 1160);
+ }
+ fclose($fs);
+ }
+
+ // skip past the headers in the response, so that it is consistent with
+ // the results returned by the regular send() method
+ $pos = strpos($response, "\r\n\r\n");
+ $response = substr($response, $pos+4);
+
+ return $response;
+
+ }
+
+
+}
View
21 wire/core/ImageSizer.php
@@ -379,5 +379,26 @@ public function setQuality($n) {
return $this;
}
+ /**
+ * Alternative to the above set* functions where you specify all in an array
+ *
+ * @param array $options May contain the following (show with default values):
+ * 'quality' => 90,
+ * 'cropping' => true,
+ * 'upscaling' => true
+ * @return this
+ *
+ */
+ public function setOptions(array $options) {
+ foreach($options as $key => $value) {
+ switch($key) {
+ case 'quality': $this->setQuality($value); break;
+ case 'cropping': $this->setCropping($value); break;
+ case 'upscaling': $this->setUpscaling($value); break;
+ }
+ }
+ return $this;
+ }
+
}
View
11 wire/core/InputfieldWrapper.php
@@ -244,8 +244,13 @@ public function ___render() {
$collapsed = (int) $inputfield->getSetting('collapsed');
if($collapsed == Inputfield::collapsedHidden) continue;
- $ffOut = $renderValueMode ? $inputfield->renderValue() : $inputfield->render();
- if(!$ffOut) continue;
+ if($renderValueMode) {
+ $ffOut = $inputfield->renderValue();
+ if(!strlen($ffOut)) $ffOut = '&nbsp;';
+ } else {
+ $ffOut = $inputfield->render();
+ }
+ if(!strlen($ffOut)) continue;
if(!$inputfield instanceof InputfieldWrapper) {
$errors = $inputfield->getErrors(true);
@@ -258,7 +263,7 @@ public function ___render() {
$ffOut = preg_replace('/(\n\s*)</', "$1\t\t\t<", $ffOut); // indent lines beginning with markup
- if($inputfield->notes) $ffOut .= str_replace('{out}', nl2br($this->entityEncode($inputfield->notes, true)), $markup['item_notes']);
+ if($inputfield->getSetting('notes')) $ffOut .= str_replace('{out}', nl2br($this->entityEncode($inputfield->notes, true)), $markup['item_notes']);
// The inputfield's classname is always used in it's LI wrapper
$ffAttrs = array(
View
23 wire/core/Pageimage.php
@@ -135,10 +135,17 @@ public function getImageInfo($reset = false) {
*
* @param int $width
* @param int $height
+ * @param array $options Array of options to override default behavior (quality=90, upscaling=true, cropping=true)
* @return Pageimage
*
*/
- public function size($width, $height) {
+ public function size($width, $height, array $options = array()) {
+
+ $defaultOptions = array(
+ 'upscaling' => true,
+ 'cropping' => true,
+ 'quality' => 90
+ );
$width = (int) $width;
$height = (int) $height;
@@ -149,7 +156,11 @@ public function size($width, $height) {
if(!is_file($filename)) {
if(@copy($this->filename(), $filename)) {
+ $configOptions = wire('config')->imageSizerOptions;
+ if(!is_array($configOptions)) $configOptions = array();
+ $options = array_merge($defaultOptions, $configOptions, $options);
$sizer = new ImageSizer($filename);
+ $sizer->setOptions($options);
$sizer->resize($width, $height);
if($this->config->chmodFile) chmod($filename, octdec($this->config->chmodFile));
}
@@ -169,11 +180,12 @@ public function size($width, $height) {
* If not given a width, it'll return the width of this Pageimage
*
* @param int $n Optional width
+ * @param array $options Optional options (see size function)
* @return int|Pageimage
*
*/
- public function width($n = 0) {
- if($n) return $this->size($n, 0);
+ public function width($n = 0, array $options = array()) {
+ if($n) return $this->size($n, 0, $options);
$info = $this->getImageInfo();
return $info['width'];
}
@@ -185,11 +197,12 @@ public function width($n = 0) {
* If not given a height, it'll return the height of this Pageimage
*
* @param int $n Optional height
+ * @param array $options Optional options (see size function)
* @return int|Pageimage
*
*/
- public function height($n = 0) {
- if($n) return $this->size(0, $n);
+ public function height($n = 0, array $options = array()) {
+ if($n) return $this->size(0, $n, $options);
$info = $this->getImageInfo();
return $info['height'];
}
View
2  wire/core/ProcessWire.php
@@ -35,7 +35,7 @@ class ProcessWire extends Wire {
const versionMajor = 2;
const versionMinor = 2;
- const versionRevision = 6;
+ const versionRevision = 7;
/**
* Given a Config object, instantiates ProcessWire and it's API
View
4 wire/modules/Process/ProcessPageList/ProcessPageList.js
@@ -317,7 +317,7 @@ $(document).ready(function() {
var processChildren = function(data) {
- if(data.error) {
+ if(data && data.error) {
alert(data.message);
$loading.hide();
ignoreClicks = false;
@@ -652,7 +652,7 @@ $(document).ready(function() {
$root.removeClass('PageListSorting');
});
- if(data.error) {
+ if(data && data.error) {
alert(data.message);
}
View
4 wire/modules/SessionLoginThrottle.module
@@ -103,7 +103,9 @@ class SessionLoginThrottle extends WireData implements Module, ConfigurableModul
// delete saved login attempts that are no longer applicable
$expired = $time - $this->maxSeconds;
- $db->query("DELETE FROM session_login_throttle WHERE last_attempt < $expired");
+ $sql = "DELETE FROM session_login_throttle WHERE last_attempt < $expired ";
+ if($allowed) $sql .= "OR name='$name'";
+ $db->query($sql);
return $allowed;
}
Please sign in to comment.
Something went wrong with that request. Please try again.