streams/mod/photo.php

245 lines
6.3 KiB
PHP
Raw Normal View History

2010-07-01 23:48:07 +00:00
<?php
require_once('include/security.php');
2013-04-26 03:01:24 +00:00
require_once('include/photo/photo_driver.php');
2010-07-01 23:48:07 +00:00
function photo_init(&$a) {
$prvcachecontrol = false;
2012-08-27 06:05:00 +00:00
switch(argc()) {
case 4:
2012-08-27 06:05:00 +00:00
$person = argv(3);
2012-10-24 04:24:23 +00:00
$res = argv(2);
$type = argv(1);
break;
case 2:
2012-08-27 06:05:00 +00:00
$photo = argv(1);
break;
case 1:
default:
killme();
2010-11-07 23:46:49 +00:00
// NOTREACHED
}
$observer_xchan = get_observer_hash();
$default = get_default_profile_photo();
2010-11-05 06:50:32 +00:00
2010-10-31 23:38:22 +00:00
if(isset($type)) {
/**
* Profile photos - Access controls on default profile photos are not honoured since they need to be exchanged with remote sites.
*
*/
2012-10-24 04:24:23 +00:00
if($type === 'profile') {
switch($res) {
case 'm':
$resolution = 5;
$default = get_default_profile_photo(80);
2012-10-24 04:24:23 +00:00
break;
case 's':
$resolution = 6;
$default = get_default_profile_photo(48);
2012-10-24 04:24:23 +00:00
break;
case 'l':
default:
$resolution = 4;
break;
}
}
2012-08-27 06:05:00 +00:00
$uid = $person;
2015-06-16 06:37:51 +00:00
$r = q("SELECT * FROM photo WHERE scale = %d AND uid = %d AND photo_usage = %d LIMIT 1",
intval($resolution),
2015-06-16 06:37:51 +00:00
intval($uid),
intval(PHOTO_PROFILE)
);
if(count($r)) {
PostgreSQL support initial commit There were 11 main types of changes: - UPDATE's and DELETE's sometimes had LIMIT 1 at the end of them. This is not only non-compliant but it would certainly not do what whoever wrote it thought it would. It is likely this mistake was just copied from Friendica. All of these instances, the LIMIT 1 was simply removed. - Bitwise operations (and even some non-zero int checks) erroneously rely on MySQL implicit integer-boolean conversion in the WHERE clauses. This is non-compliant (and bad programming practice to boot). Proper explicit boolean conversions were added. New queries should use proper conventions. - MySQL has a different operator for bitwise XOR than postgres. Rather than add yet another dba_ func, I converted them to "& ~" ("AND NOT") when turning off, and "|" ("OR") when turning on. There were no true toggles (XOR). New queries should refrain from using XOR when not necessary. - There are several fields which the schema has marked as NOT NULL, but the inserts don't specify them. The reason this works is because mysql totally ignores the constraint and adds an empty text default automatically. Again, non-compliant, obviously. In these cases a default of empty text was added. - Several statements rely on a non-standard MySQL feature (http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html). These queries can all be rewritten to be standards compliant. Interestingly enough, the newly rewritten standards compliant queries run a zillion times faster, even on MySQL. - A couple of function/operator name translations were needed (RAND/RANDOM, GROUP_CONCAT/STRING_AGG, UTC_NOW, REGEXP/~, ^/#) -- assist functions added in the dba_ - INTERVALs: postgres requires quotes around the value, mysql requires that there are not quotes around the value -- assist functions added in the dba_ - NULL_DATE's -- Postgres does not allow the invalid date '0000-00-00 00:00:00' (there is no such thing as year 0 or month 0 or day 0). We use '0001-01-01 00:00:00' for postgres. Conversions are handled in Zot/item packets automagically by quoting all dates with dbescdate(). - char(##) specifications in the schema creates fields with blank spaces that aren't trimmed in the code. MySQL apparently treats char(##) as varchar(##), again, non-compliant. Since postgres works better with text fields anyway, this ball of bugs was simply side-stepped by using 'text' datatype for all text fields in the postgres schema. varchar was used in a couple of places where it actually seemed appropriate (size constraint), but without rigorously vetting that all of the PHP code actually validates data, new bugs might come out from under the rug. - postgres doesn't store nul bytes and a few other non-printables in text fields, even when quoted. bytea fields were used when storing binary data (photo.data, attach.data). A new dbescbin() function was added to handle this transparently. - postgres does not support LIMIT #,# syntax. All databases support LIMIT # OFFSET # syntax. Statements were updated to be standard. These changes require corresponding changes in the coding standards. Please review those before adding any code going forward. Still on my TODO list: - remove quotes from non-reserved identifiers and make reserved identifiers use dba func for quoting - Rewrite search queries for better results (both MySQL and Postgres)
2014-11-13 20:21:58 +00:00
$data = dbunescbin($r[0]['data']);
2012-06-07 15:42:13 +00:00
$mimetype = $r[0]['type'];
}
2015-06-14 22:59:11 +00:00
if(intval($r[0]['os_storage']))
$data = file_get_contents($data);
2010-10-31 23:38:22 +00:00
if(! isset($data)) {
2010-11-05 06:50:32 +00:00
$data = file_get_contents($default);
2014-10-05 21:38:47 +00:00
$mimetype = 'image/png';
}
2010-07-01 23:48:07 +00:00
}
else {
2010-08-05 03:03:38 +00:00
/**
* Other photos
*/
/* Check for a cookie to indicate display pixel density, in order to detect high-resolution
displays. This procedure was derived from the "Retina Images" by Jeremey Worboys,
used in accordance with the Creative Commons Attribution 3.0 Unported License.
Project link: https://github.com/Retina-Images/Retina-Images
License link: http://creativecommons.org/licenses/by/3.0/
*/
$cookie_value = false;
if (isset($_COOKIE['devicePixelRatio'])) {
$cookie_value = intval($_COOKIE['devicePixelRatio']);
}
else {
// Force revalidation of cache on next request
$cache_directive = 'no-cache';
$status = 'no cookie';
}
$resolution = 0;
2012-10-24 04:24:23 +00:00
if(strpos($photo,'.') !== false)
$photo = substr($photo,0,strpos($photo,'.'));
if(substr($photo,-2,1) == '-') {
$resolution = intval(substr($photo,-1,1));
$photo = substr($photo,0,-2);
2014-05-10 18:22:00 +00:00
// If viewing on a high-res screen, attempt to serve a higher resolution image:
if ($resolution == 2 && ($cookie_value > 1))
{
$resolution = 1;
}
}
// If using resolution 1, make sure it exists before proceeding:
if ($resolution == 1)
{
$r = q("SELECT uid FROM photo WHERE resource_id = '%s' AND scale = %d LIMIT 1",
dbesc($photo),
intval($resolution)
);
if (!($r))
$resolution = 2;
}
2012-10-24 04:24:23 +00:00
$r = q("SELECT uid FROM photo WHERE resource_id = '%s' AND scale = %d LIMIT 1",
dbesc($photo),
intval($resolution)
);
if($r) {
2010-08-05 03:03:38 +00:00
$allowed = (($r[0]['uid']) ? perm_is_allowed($r[0]['uid'],$observer_xchan,'view_storage') : true);
$sql_extra = permissions_sql($r[0]['uid']);
2010-08-05 03:03:38 +00:00
// Now we'll see if we can access the photo
2012-10-24 04:24:23 +00:00
$r = q("SELECT * FROM photo WHERE resource_id = '%s' AND scale = %d $sql_extra LIMIT 1",
2010-08-05 03:03:38 +00:00
dbesc($photo),
intval($resolution)
);
if($r && $allowed) {
PostgreSQL support initial commit There were 11 main types of changes: - UPDATE's and DELETE's sometimes had LIMIT 1 at the end of them. This is not only non-compliant but it would certainly not do what whoever wrote it thought it would. It is likely this mistake was just copied from Friendica. All of these instances, the LIMIT 1 was simply removed. - Bitwise operations (and even some non-zero int checks) erroneously rely on MySQL implicit integer-boolean conversion in the WHERE clauses. This is non-compliant (and bad programming practice to boot). Proper explicit boolean conversions were added. New queries should use proper conventions. - MySQL has a different operator for bitwise XOR than postgres. Rather than add yet another dba_ func, I converted them to "& ~" ("AND NOT") when turning off, and "|" ("OR") when turning on. There were no true toggles (XOR). New queries should refrain from using XOR when not necessary. - There are several fields which the schema has marked as NOT NULL, but the inserts don't specify them. The reason this works is because mysql totally ignores the constraint and adds an empty text default automatically. Again, non-compliant, obviously. In these cases a default of empty text was added. - Several statements rely on a non-standard MySQL feature (http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html). These queries can all be rewritten to be standards compliant. Interestingly enough, the newly rewritten standards compliant queries run a zillion times faster, even on MySQL. - A couple of function/operator name translations were needed (RAND/RANDOM, GROUP_CONCAT/STRING_AGG, UTC_NOW, REGEXP/~, ^/#) -- assist functions added in the dba_ - INTERVALs: postgres requires quotes around the value, mysql requires that there are not quotes around the value -- assist functions added in the dba_ - NULL_DATE's -- Postgres does not allow the invalid date '0000-00-00 00:00:00' (there is no such thing as year 0 or month 0 or day 0). We use '0001-01-01 00:00:00' for postgres. Conversions are handled in Zot/item packets automagically by quoting all dates with dbescdate(). - char(##) specifications in the schema creates fields with blank spaces that aren't trimmed in the code. MySQL apparently treats char(##) as varchar(##), again, non-compliant. Since postgres works better with text fields anyway, this ball of bugs was simply side-stepped by using 'text' datatype for all text fields in the postgres schema. varchar was used in a couple of places where it actually seemed appropriate (size constraint), but without rigorously vetting that all of the PHP code actually validates data, new bugs might come out from under the rug. - postgres doesn't store nul bytes and a few other non-printables in text fields, even when quoted. bytea fields were used when storing binary data (photo.data, attach.data). A new dbescbin() function was added to handle this transparently. - postgres does not support LIMIT #,# syntax. All databases support LIMIT # OFFSET # syntax. Statements were updated to be standard. These changes require corresponding changes in the coding standards. Please review those before adding any code going forward. Still on my TODO list: - remove quotes from non-reserved identifiers and make reserved identifiers use dba func for quoting - Rewrite search queries for better results (both MySQL and Postgres)
2014-11-13 20:21:58 +00:00
$data = dbunescbin($r[0]['data']);
2012-06-07 15:42:13 +00:00
$mimetype = $r[0]['type'];
2015-06-14 22:59:11 +00:00
if(intval($r[0]['os_storage']))
$data = file_get_contents($data);
2010-08-05 03:03:38 +00:00
}
else {
// Does the picture exist? It may be a remote person with no credentials,
// but who should otherwise be able to view it. Show a default image to let
// them know permissions was denied. It may be possible to view the image
// through an authenticated profile visit.
// There won't be many completely unauthorised people seeing this because
// they won't have the photo link, so there's a reasonable chance that the person
// might be able to obtain permission to view it.
$r = q("SELECT * FROM `photo` WHERE `resource_id` = '%s' AND `scale` = %d LIMIT 1",
dbesc($photo),
intval($resolution)
);
if($r) {
logger('mod_photo: forbidden. ' . $a->query_string);
$observer = $a->get_observer();
logger('mod_photo: observer = ' . (($observer) ? $observer['xchan_addr'] : '(not authenticated)'));
$data = file_get_contents('images/nosign.png');
$mimetype = 'image/png';
$prvcachecontrol = true;
}
}
}
2010-07-01 23:48:07 +00:00
}
2010-11-07 23:46:49 +00:00
if(! isset($data)) {
if(isset($resolution)) {
switch($resolution) {
case 4:
$data = file_get_contents(get_default_profile_photo());
2014-10-05 21:38:47 +00:00
$mimetype = 'image/png';
break;
case 5:
$data = file_get_contents(get_default_profile_photo(80));
2014-10-05 21:38:47 +00:00
$mimetype = 'image/png';
break;
case 6:
$data = file_get_contents(get_default_profile_photo(48));
2014-10-05 21:38:47 +00:00
$mimetype = 'image/png';
break;
default:
killme();
// NOTREACHED
break;
}
}
2010-07-01 23:48:07 +00:00
}
2012-10-24 04:24:23 +00:00
if(isset($res) && intval($res) && $res < 500) {
2013-04-26 03:01:24 +00:00
$ph = photo_factory($data, $mimetype);
if($ph->is_valid()) {
2012-10-24 04:24:23 +00:00
$ph->scaleImageSquare($res);
$data = $ph->imageString();
2012-06-07 15:42:13 +00:00
$mimetype = $ph->getType();
}
}
// Writing in cachefile
2012-04-17 11:33:50 +00:00
if (isset($cachefile) && $cachefile != '')
file_put_contents($cachefile, $data);
2011-10-01 09:22:48 +00:00
if(function_exists('header_remove')) {
header_remove('Pragma');
header_remove('pragma');
}
2012-10-24 04:24:23 +00:00
header("Content-type: " . $mimetype);
if($prvcachecontrol) {
// it is a private photo that they have no permission to view.
// tell the browser not to cache it, in case they authenticate
// and subsequently have permission to see it
header("Cache-Control: no-store, no-cache, must-revalidate");
}
else {
// The photo cache default is 1 day to provide a privacy trade-off,
// as somebody reducing photo permissions on a photo that is already
// "in the wild" won't be able to stop the photo from being viewed
// for this amount amount of time once it is in the browser cache.
// The privacy expectations of your site members and their perception
// of privacy where it affects the entire project may be affected.
// This has performance considerations but we highly recommend you
// leave it alone.
$cache = get_config('system','photo_cache_time');
if(! $cache)
$cache = (3600 * 24); // 1 day
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $cache) . " GMT");
header("Cache-Control: max-age=" . $cache);
}
2010-11-07 23:46:49 +00:00
echo $data;
killme();
2010-11-07 23:46:49 +00:00
// NOTREACHED
}