diff options
author | plegall <plg@piwigo.org> | 2016-05-13 11:05:15 +0200 |
---|---|---|
committer | plegall <plg@piwigo.org> | 2016-05-13 11:05:15 +0200 |
commit | bd0d2ae6459c63ed7f7060589297bf63ae1cc619 (patch) | |
tree | 66fa333d54fa39d7ea2e559cb607f0b594805934 | |
parent | 2543002c8bb751517947010814aa8128e2f368aa (diff) | |
parent | bf81ba031575b8cd1ccc318f5d5a8f8ec7cb5049 (diff) |
Merge branch '2.8'
-rw-r--r-- | action.php | 8 | ||||
-rw-r--r-- | include/functions_session.inc.php | 12 | ||||
-rw-r--r-- | include/srand.php | 149 | ||||
-rw-r--r-- | picture.php | 3 |
4 files changed, 167 insertions, 5 deletions
diff --git a/action.php b/action.php index 33ae408ed..5a7ca423e 100644 --- a/action.php +++ b/action.php @@ -100,6 +100,8 @@ if ( empty($element_info) ) do_error(404, 'Requested id not found'); } +$src_image = new SrcImage($element_info); + // $filter['visible_categories'] and $filter['visible_images'] // are not used because it's not necessary (filter <> restriction) $query=' @@ -126,9 +128,9 @@ $file=''; switch ($_GET['part']) { case 'e': - if ( !$user['enabled_high'] ) - { - $deriv = new DerivativeImage(IMG_XXLARGE, new SrcImage($element_info)); + if ( $src_image->is_original() and !$user['enabled_high'] ) + {// we have a photo and the user has no access to HD + $deriv = new DerivativeImage(IMG_XXLARGE, $src_image); if ( !$deriv->same_as_source() ) { do_error(401, 'Access denied e'); diff --git a/include/functions_session.inc.php b/include/functions_session.inc.php index 0829bcfda..e75f22e12 100644 --- a/include/functions_session.inc.php +++ b/include/functions_session.inc.php @@ -64,11 +64,21 @@ function generate_key($size) { include_once(PHPWG_ROOT_PATH.'include/random_compat/random.php'); + try + { + $bytes = random_bytes($size+10); + } + catch (Exception $ex) + { + include_once(PHPWG_ROOT_PATH.'include/srand.php'); + $bytes = secure_random_bytes($size+10); + } + return substr( str_replace( array('+', '/'), '', - base64_encode(random_bytes($size+10)) + base64_encode($bytes) ), 0, $size diff --git a/include/srand.php b/include/srand.php new file mode 100644 index 000000000..b0d34ba6f --- /dev/null +++ b/include/srand.php @@ -0,0 +1,149 @@ +<?php + +/* + * Author: + * George Argyros <argyros.george@gmail.com> + * + * Copyright (c) 2012, George Argyros + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the <organization> nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL GEORGE ARGYROS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * + * The function is providing, at least at the systems tested :), + * $len bytes of entropy under any PHP installation or operating system. + * The execution time should be at most 10-20 ms in any system. + */ +function secure_random_bytes($len = 10) +{ + + /* + * Our primary choice for a cryptographic strong randomness function is + * openssl_random_pseudo_bytes. + */ + $SSLstr = '4'; // http://xkcd.com/221/ + if (function_exists('openssl_random_pseudo_bytes') && + (version_compare(PHP_VERSION, '5.3.4') >= 0 || + substr(PHP_OS, 0, 3) !== 'WIN')) + { + $SSLstr = openssl_random_pseudo_bytes($len, $strong); + if ($strong) { + return $SSLstr; + } + } + + /* + * If mcrypt extension is available then we use it to gather entropy from + * the operating system's PRNG. This is better than reading /dev/urandom + * directly since it avoids reading larger blocks of data than needed. + * Older versions of mcrypt_create_iv may be broken or take too much time + * to finish so we only use this function with PHP 5.3.7 and above. + * @see https://bugs.php.net/bug.php?id=55169 + */ + if (function_exists('mcrypt_create_iv') && + (version_compare(PHP_VERSION, '5.3.7') >= 0 || + substr(PHP_OS, 0, 3) !== 'WIN')) { + $str = mcrypt_create_iv($len, MCRYPT_DEV_URANDOM); + if ($str !== false) { + return $str; + } + } + + + /* + * No build-in crypto randomness function found. We collect any entropy + * available in the PHP core PRNGs along with some filesystem info and memory + * stats. To make this data cryptographically strong we add data either from + * /dev/urandom or if its unavailable, we gather entropy by measuring the + * time needed to compute a number of SHA-1 hashes. + */ + $str = ''; + $bits_per_round = 2; // bits of entropy collected in each clock drift round + $msec_per_round = 400; // expected running time of each round in microseconds + $hash_len = 20; // SHA-1 Hash length + $total = $len; // total bytes of entropy to collect + + $handle = @fopen('/dev/urandom', 'rb'); + if ($handle && function_exists('stream_set_read_buffer')) { + @stream_set_read_buffer($handle, 0); + } + + do + { + $bytes = ($total > $hash_len)? $hash_len : $total; + $total -= $bytes; + + //collect any entropy available from the PHP system and filesystem + $entropy = rand() . uniqid(mt_rand(), true) . $SSLstr; + $entropy .= implode('', @fstat(@fopen( __FILE__, 'r'))); + $entropy .= memory_get_usage() . getmypid(); + $entropy .= serialize($_ENV) . serialize($_SERVER); + if (function_exists('posix_times')) { + $entropy .= serialize(posix_times()); + } + if (function_exists('zend_thread_id')) { + $entropy .= zend_thread_id(); + } + if ($handle) { + $entropy .= @fread($handle, $bytes); + } else { + // Measure the time that the operations will take on average + for ($i = 0; $i < 3; $i++) + { + $c1 = microtime(true); + $var = sha1(mt_rand()); + for ($j = 0; $j < 50; $j++) { + $var = sha1($var); + } + $c2 = microtime(true); + $entropy .= $c1 . $c2; + } + + // Based on the above measurement determine the total rounds + // in order to bound the total running time. + $rounds = (int) ($msec_per_round * 50 / (int) (($c2 - $c1) * 1000000)); + + // Take the additional measurements. On average we can expect + // at least $bits_per_round bits of entropy from each measurement. + $iter = $bytes * (int) (ceil(8 / $bits_per_round)); + for ($i = 0; $i < $iter; $i++) { + $c1 = microtime(); + $var = sha1(mt_rand()); + for ($j = 0; $j < $rounds; $j++) { + $var = sha1($var); + } + $c2 = microtime(); + $entropy .= $c1 . $c2; + } + + } + // We assume sha1 is a deterministic extractor for the $entropy variable. + $str .= sha1($entropy, true); + } while ($len > strlen($str)); + + if ($handle) { + @fclose($handle); + } + return substr($str, 0, $len); +} diff --git a/picture.php b/picture.php index db58c12e7..37d65a1f9 100644 --- a/picture.php +++ b/picture.php @@ -532,7 +532,8 @@ while ($row = pwg_db_fetch_assoc($result)) } else { // not a pic - need download link - $row['download_url'] = $row['element_url'] = get_element_url($row);; + $row['element_url'] = get_element_url($row); + $row['download_url'] = get_action_url($row['id'], 'e', true); } } |