streams/include/photo/photo_driver.php

719 lines
17 KiB
PHP
Raw Normal View History

2013-02-26 01:09:40 +00:00
<?php /** @file */
2013-04-26 03:01:24 +00:00
function photo_factory($data, $type = null) {
$ph = null;
$ignore_imagick = get_config('system', 'ignore_imagick');
if(class_exists('Imagick') && !$ignore_imagick) {
$v = Imagick::getVersion();
preg_match('/ImageMagick ([0-9]+\.[0-9]+\.[0-9]+)/', $v['versionString'], $m);
if(version_compare($m[1],'6.6.7') >= 0) {
require_once('include/photo/photo_imagick.php');
$ph = new photo_imagick($data,$type);
}
else {
// earlier imagick versions have issues with scaling png's
// don't log this because it will just fill the logfile.
// leave this note here so those who are looking for why
// we aren't using imagick can find it
}
2013-04-26 03:01:24 +00:00
}
if(! $ph) {
2013-04-26 03:01:24 +00:00
require_once('include/photo/photo_gd.php');
$ph = new photo_gd($data,$type);
}
2013-04-26 03:01:24 +00:00
return $ph;
}
2013-04-26 03:01:24 +00:00
abstract class photo_driver {
2013-04-26 03:01:24 +00:00
protected $image;
protected $width;
protected $height;
protected $valid;
protected $type;
protected $types;
2013-04-26 03:01:24 +00:00
abstract function supportedTypes();
2013-04-26 03:01:24 +00:00
abstract function load($data,$type);
2013-04-26 03:01:24 +00:00
abstract function destroy();
2013-04-26 03:01:24 +00:00
abstract function setDimensions();
2013-04-26 03:01:24 +00:00
abstract function getImage();
2013-04-26 03:01:24 +00:00
abstract function doScaleImage($new_width,$new_height);
2013-04-26 03:01:24 +00:00
abstract function rotate($degrees);
2010-07-01 23:48:07 +00:00
2013-04-26 03:01:24 +00:00
abstract function flip($horiz = true, $vert = false);
2013-02-13 00:21:12 +00:00
2013-04-26 03:01:24 +00:00
abstract function cropImage($max,$x,$y,$w,$h);
2013-02-13 00:21:12 +00:00
2013-04-26 03:01:24 +00:00
abstract function imageString();
2013-02-13 00:21:12 +00:00
2013-04-26 03:01:24 +00:00
public function __construct($data, $type='') {
2013-02-13 00:21:12 +00:00
$this->types = $this->supportedTypes();
2013-04-26 03:01:24 +00:00
if (! array_key_exists($type,$this->types)){
2013-02-13 00:21:12 +00:00
$type='image/jpeg';
}
$this->type = $type;
2013-04-26 03:01:24 +00:00
$this->valid = false;
$this->load($data,$type);
2013-02-13 00:21:12 +00:00
}
public function __destruct() {
2013-04-26 03:01:24 +00:00
if($this->is_valid())
$this->destroy();
2013-02-13 00:21:12 +00:00
}
public function is_valid() {
return $this->valid;
}
public function getWidth() {
if(!$this->is_valid())
return FALSE;
return $this->width;
}
public function getHeight() {
if(!$this->is_valid())
return FALSE;
return $this->height;
}
2013-04-26 03:01:24 +00:00
public function saveImage($path) {
2013-02-13 00:21:12 +00:00
if(!$this->is_valid())
return FALSE;
2013-04-26 03:01:24 +00:00
file_put_contents($path, $this->imageString());
2013-02-13 00:21:12 +00:00
}
2013-04-26 03:01:24 +00:00
2013-02-13 00:21:12 +00:00
public function getType() {
if(!$this->is_valid())
return FALSE;
return $this->type;
}
public function getExt() {
if(!$this->is_valid())
return FALSE;
return $this->types[$this->getType()];
}
public function scaleImage($max) {
if(!$this->is_valid())
return FALSE;
$width = $this->width;
$height = $this->height;
$dest_width = $dest_height = 0;
if((! $width)|| (! $height))
return FALSE;
if($width > $max && $height > $max) {
// very tall image (greater than 16:9)
// constrain the width - let the height float.
if((($height * 9) / 16) > $width) {
$dest_width = $max;
2013-02-13 00:21:12 +00:00
$dest_height = intval(( $height * $max ) / $width);
}
// else constrain both dimensions
elseif($width > $height) {
2013-02-13 00:21:12 +00:00
$dest_width = $max;
$dest_height = intval(( $height * $max ) / $width);
}
else {
$dest_width = intval(( $width * $max ) / $height);
$dest_height = $max;
}
}
else {
if( $width > $max ) {
$dest_width = $max;
$dest_height = intval(( $height * $max ) / $width);
}
else {
if( $height > $max ) {
// very tall image (greater than 16:9)
// but width is OK - don't do anything
if((($height * 9) / 16) > $width) {
$dest_width = $width;
2013-02-13 00:21:12 +00:00
$dest_height = $height;
}
else {
2013-02-13 00:21:12 +00:00
$dest_width = intval(( $width * $max ) / $height);
$dest_height = $max;
}
2013-02-13 00:21:12 +00:00
}
else {
$dest_width = $width;
$dest_height = $height;
}
}
}
2013-04-26 03:01:24 +00:00
$this->doScaleImage($dest_width,$dest_height);
}
2013-02-13 00:21:12 +00:00
2013-04-26 03:01:24 +00:00
public function scaleImageUp($min) {
if(!$this->is_valid())
return FALSE;
2013-02-13 00:21:12 +00:00
2013-04-26 03:01:24 +00:00
$width = $this->width;
$height = $this->height;
2013-04-26 03:01:24 +00:00
$dest_width = $dest_height = 0;
2013-04-26 03:01:24 +00:00
if((! $width)|| (! $height))
return FALSE;
2013-04-26 03:01:24 +00:00
if($width < $min && $height < $min) {
if($width > $height) {
$dest_width = $min;
$dest_height = intval(( $height * $min ) / $width);
}
else {
$dest_width = intval(( $width * $min ) / $height);
$dest_height = $min;
}
2013-02-13 00:21:12 +00:00
}
2013-04-26 03:01:24 +00:00
else {
if( $width < $min ) {
$dest_width = $min;
$dest_height = intval(( $height * $min ) / $width);
}
else {
if( $height < $min ) {
$dest_width = intval(( $width * $min ) / $height);
$dest_height = $min;
}
else {
$dest_width = $width;
$dest_height = $height;
}
}
}
$this->doScaleImage($dest_width,$dest_height);
2013-02-13 00:21:12 +00:00
}
2013-04-26 03:01:24 +00:00
public function scaleImageSquare($dim) {
2013-02-13 00:21:12 +00:00
if(!$this->is_valid())
return FALSE;
2013-04-26 03:01:24 +00:00
$this->doScaleImage($dim,$dim);
2013-02-13 00:21:12 +00:00
}
public function orient($filename) {
2013-04-26 03:01:24 +00:00
/**
* This function is a bit unusual, because it is operating on a file, but you must
* first create an image from that file to initialise the type and check validity
* of the image.
*/
if(! $this->is_valid())
2013-02-13 00:21:12 +00:00
return FALSE;
2015-08-20 02:17:17 +00:00
2013-04-26 03:01:24 +00:00
if((! function_exists('exif_read_data')) || ($this->getType() !== 'image/jpeg'))
2013-02-13 00:21:12 +00:00
return;
$exif = @exif_read_data($filename,null,true);
2015-08-20 02:17:17 +00:00
2013-02-08 03:55:40 +00:00
if($exif) {
$ort = $exif['IFD0']['Orientation'];
2013-02-13 00:21:12 +00:00
switch($ort)
{
case 1: // nothing
break;
case 2: // horizontal flip
$this->flip();
break;
case 3: // 180 rotate left
$this->rotate(180);
break;
case 4: // vertical flip
$this->flip(false, true);
break;
case 5: // vertical flip + 90 rotate right
$this->flip(false, true);
$this->rotate(-90);
break;
case 6: // 90 rotate right
$this->rotate(-90);
break;
case 7: // horizontal flip + 90 rotate right
$this->flip();
$this->rotate(-90);
break;
case 8: // 90 rotate left
$this->rotate(90);
break;
}
return $exif;
2013-02-13 00:21:12 +00:00
}
return false;
2013-02-08 03:55:40 +00:00
}
2012-07-22 12:13:29 +00:00
public function save($arr) {
$p = array();
$p['aid'] = ((intval($arr['aid'])) ? intval($arr['aid']) : 0);
$p['uid'] = ((intval($arr['uid'])) ? intval($arr['uid']) : 0);
$p['xchan'] = (($arr['xchan']) ? $arr['xchan'] : '');
$p['resource_id'] = (($arr['resource_id']) ? $arr['resource_id'] : '');
$p['filename'] = (($arr['filename']) ? $arr['filename'] : '');
$p['album'] = (($arr['album']) ? $arr['album'] : '');
$p['scale'] = ((intval($arr['scale'])) ? intval($arr['scale']) : 0);
$p['allow_cid'] = (($arr['allow_cid']) ? $arr['allow_cid'] : '');
$p['allow_gid'] = (($arr['allow_gid']) ? $arr['allow_gid'] : '');
$p['deny_cid'] = (($arr['deny_cid']) ? $arr['deny_cid'] : '');
$p['deny_gid'] = (($arr['deny_gid']) ? $arr['deny_gid'] : '');
$p['created'] = (($arr['created']) ? $arr['created'] : datetime_convert());
$p['edited'] = (($arr['edited']) ? $arr['edited'] : $p['created']);
$p['title'] = (($arr['title']) ? $arr['title'] : '');
$p['description'] = (($arr['description']) ? $arr['description'] : '');
2015-05-19 02:43:53 +00:00
$p['photo_usage'] = intval($arr['photo_usage']);
$p['os_storage'] = intval($arr['os_storage']);
$p['os_path'] = $arr['os_path'];
if(! intval($p['scale']))
logger('save: ' . print_r($arr,true));
$x = q("select id from photo where resource_id = '%s' and uid = %d and xchan = '%s' and `scale` = %d limit 1",
dbesc($p['resource_id']),
intval($p['uid']),
dbesc($p['xchan']),
intval($p['scale'])
);
if($x) {
$r = q("UPDATE `photo` set
`aid` = %d,
`uid` = %d,
`xchan` = '%s',
`resource_id` = '%s',
`created` = '%s',
`edited` = '%s',
`filename` = '%s',
`type` = '%s',
`album` = '%s',
`height` = %d,
`width` = %d,
`data` = '%s',
`os_storage` = %d,
`size` = %d,
`scale` = %d,
2015-05-19 02:43:53 +00:00
`photo_usage` = %d,
`title` = '%s',
`description` = '%s',
`allow_cid` = '%s',
`allow_gid` = '%s',
`deny_cid` = '%s',
`deny_gid` = '%s'
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
where id = %d",
intval($p['aid']),
intval($p['uid']),
dbesc($p['xchan']),
dbesc($p['resource_id']),
dbesc($p['created']),
dbesc($p['edited']),
dbesc(basename($p['filename'])),
dbesc($this->getType()),
dbesc($p['album']),
intval($this->getHeight()),
intval($this->getWidth()),
(intval($p['os_storage']) ? dbesc($p['os_path']) : dbescbin($this->imageString())),
intval($p['os_storage']),
intval(strlen($this->imageString())),
intval($p['scale']),
2015-05-19 02:43:53 +00:00
intval($p['photo_usage']),
dbesc($p['title']),
dbesc($p['description']),
dbesc($p['allow_cid']),
dbesc($p['allow_gid']),
dbesc($p['deny_cid']),
dbesc($p['deny_gid']),
intval($x[0]['id'])
);
}
else {
$r = q("INSERT INTO `photo`
( `aid`, `uid`, `xchan`, `resource_id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `data`, `os_storage`, `size`, `scale`, `photo_usage`, `title`, `description`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s', '%s' )",
intval($p['aid']),
intval($p['uid']),
dbesc($p['xchan']),
dbesc($p['resource_id']),
dbesc($p['created']),
dbesc($p['edited']),
dbesc(basename($p['filename'])),
dbesc($this->getType()),
dbesc($p['album']),
intval($this->getHeight()),
intval($this->getWidth()),
(intval($p['os_storage']) ? dbesc($p['os_path']) : dbescbin($this->imageString())),
intval($p['os_storage']),
intval(strlen($this->imageString())),
intval($p['scale']),
2015-05-19 02:43:53 +00:00
intval($p['photo_usage']),
dbesc($p['title']),
dbesc($p['description']),
dbesc($p['allow_cid']),
dbesc($p['allow_gid']),
dbesc($p['deny_cid']),
dbesc($p['deny_gid'])
);
}
return $r;
}
2015-05-19 02:43:53 +00:00
public function store($aid, $uid, $xchan, $rid, $filename, $album, $scale, $usage = PHOTO_NORMAL, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
2013-02-13 00:21:12 +00:00
$x = q("select id from photo where `resource_id` = '%s' and uid = %d and `xchan` = '%s' and `scale` = %d limit 1",
dbesc($rid),
intval($uid),
dbesc($xchan),
intval($scale)
);
if(count($x)) {
$r = q("UPDATE `photo`
set `aid` = %d,
2013-02-13 00:21:12 +00:00
`uid` = %d,
`xchan` = '%s',
`resource_id` = '%s',
`created` = '%s',
`edited` = '%s',
`filename` = '%s',
`type` = '%s',
`album` = '%s',
`height` = %d,
`width` = %d,
`data` = '%s',
`size` = %d,
2013-02-13 00:21:12 +00:00
`scale` = %d,
2015-05-19 02:43:53 +00:00
`photo_usage` = %d,
2013-02-13 00:21:12 +00:00
`allow_cid` = '%s',
`allow_gid` = '%s',
`deny_cid` = '%s',
`deny_gid` = '%s'
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
where id = %d",
2012-07-22 12:13:29 +00:00
intval($aid),
2013-02-13 00:21:12 +00:00
intval($uid),
dbesc($xchan),
dbesc($rid),
dbesc(datetime_convert()),
dbesc(datetime_convert()),
dbesc(basename($filename)),
dbesc($this->getType()),
dbesc($album),
intval($this->getHeight()),
intval($this->getWidth()),
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
dbescbin($this->imageString()),
intval(strlen($this->imageString())),
2013-02-13 00:21:12 +00:00
intval($scale),
2015-05-19 02:43:53 +00:00
intval($photo_usage),
2013-02-13 00:21:12 +00:00
dbesc($allow_cid),
dbesc($allow_gid),
dbesc($deny_cid),
dbesc($deny_gid),
intval($x[0]['id'])
);
}
else {
$r = q("INSERT INTO `photo`
2015-05-19 02:43:53 +00:00
( `aid`, `uid`, `xchan`, `resource_id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `data`, `size`, `scale`, `photo_usage`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
2013-02-13 00:21:12 +00:00
VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s' )",
intval($aid),
2013-02-13 00:21:12 +00:00
intval($uid),
dbesc($xchan),
dbesc($rid),
dbesc(datetime_convert()),
dbesc(datetime_convert()),
dbesc(basename($filename)),
dbesc($this->getType()),
dbesc($album),
intval($this->getHeight()),
intval($this->getWidth()),
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
dbescbin($this->imageString()),
intval(strlen($this->imageString())),
2013-02-13 00:21:12 +00:00
intval($scale),
2015-05-19 02:43:53 +00:00
intval($photo_usage),
2013-02-13 00:21:12 +00:00
dbesc($allow_cid),
dbesc($allow_gid),
dbesc($deny_cid),
dbesc($deny_gid)
);
}
return $r;
}
2013-04-26 03:01:24 +00:00
2013-02-26 01:09:40 +00:00
}
2010-07-01 23:48:07 +00:00
2010-08-05 03:03:38 +00:00
2013-04-26 03:01:24 +00:00
2012-06-07 15:42:13 +00:00
/**
* Guess image mimetype from filename or from Content-Type header
2012-07-22 12:13:29 +00:00
*
2012-06-07 15:42:13 +00:00
* @arg $filename string Image filename
* @arg $headers string Headers to check for Content-Type (from curl request)
2012-06-07 15:42:13 +00:00
*/
2013-04-26 03:01:24 +00:00
function guess_image_type($filename, $headers = '') {
logger('Photo: guess_image_type: '.$filename . ($headers?' from curl headers':''), LOGGER_DEBUG);
2013-02-13 00:21:12 +00:00
$type = null;
if ($headers) {
2013-02-13 00:21:12 +00:00
$a = get_app();
$hdrs=array();
$h = explode("\n",$headers);
2013-02-13 00:21:12 +00:00
foreach ($h as $l) {
list($k,$v) = array_map("trim", explode(":", trim($l), 2));
$hdrs[$k] = $v;
2013-02-13 00:21:12 +00:00
}
logger('Curl headers: '.var_export($hdrs, true), LOGGER_DEBUG);
if (array_key_exists('Content-Type', $hdrs))
$type = $hdrs['Content-Type'];
2013-02-13 00:21:12 +00:00
}
if (is_null($type)){
$ignore_imagick = get_config('system', 'ignore_imagick');
2013-02-13 00:21:12 +00:00
// Guessing from extension? Isn't that... dangerous?
if(class_exists('Imagick') && file_exists($filename) && is_readable($filename) && !$ignore_imagick) {
$v = Imagick::getVersion();
preg_match('/ImageMagick ([0-9]+\.[0-9]+\.[0-9]+)/', $v['versionString'], $m);
if(version_compare($m[1],'6.6.7') >= 0) {
/**
* Well, this not much better,
* but at least it comes from the data inside the image,
* we won't be tricked by a manipulated extension
*/
$image = new Imagick($filename);
$type = $image->getImageMimeType();
}
else {
// earlier imagick versions have issues with scaling png's
// don't log this because it will just fill the logfile.
// leave this note here so those who are looking for why
// we aren't using imagick can find it
}
}
if(is_null($type)) {
2013-02-13 00:21:12 +00:00
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$ph = photo_factory('');
$types = $ph->supportedTypes();
2013-02-13 00:21:12 +00:00
$type = "image/jpeg";
foreach ($types as $m=>$e){
if ($ext==$e) $type = $m;
}
}
}
logger('Photo: guess_image_type: type='.$type, LOGGER_DEBUG);
return $type;
2012-07-22 12:13:29 +00:00
2012-06-07 15:42:13 +00:00
}
function import_xchan_photo($photo,$xchan,$thing = false) {
2010-10-26 21:50:38 +00:00
2013-02-13 00:21:12 +00:00
$a = get_app();
2012-07-22 12:13:29 +00:00
$flags = (($thing) ? PHOTO_THING : PHOTO_XCHAN);
$album = (($thing) ? 'Things' : 'Contact Photos');
logger('import_xchan_photo: updating channel photo from ' . $photo . ' for ' . $xchan, LOGGER_DEBUG);
2013-02-11 04:09:32 +00:00
if($thing)
2013-02-13 00:21:12 +00:00
$hash = photo_new_resource();
else {
2015-05-19 02:43:53 +00:00
$r = q("select resource_id from photo where xchan = '%s' and photo_usage = %d and scale = 4 limit 1",
dbesc($xchan),
intval(PHOTO_XCHAN)
);
if($r) {
$hash = $r[0]['resource_id'];
}
else {
$hash = photo_new_resource();
}
2013-02-13 00:21:12 +00:00
}
2012-07-22 12:13:29 +00:00
2013-02-13 00:21:12 +00:00
$photo_failure = false;
2014-02-17 22:30:02 +00:00
$img_str = '';
2010-10-26 21:50:38 +00:00
2014-02-17 22:30:02 +00:00
if($photo) {
$filename = basename($photo);
$type = guess_image_type($photo);
2013-02-11 04:09:32 +00:00
2014-02-17 22:30:02 +00:00
if(! $type)
$type = 'image/jpeg';
2014-02-17 22:30:02 +00:00
$result = z_fetch_url($photo,true);
if($result['success'])
$img_str = $result['body'];
}
$img = photo_factory($img_str, $type);
2013-02-13 00:21:12 +00:00
if($img->is_valid()) {
$width = $img->getWidth();
$height = $img->getHeight();
if($width && $height) {
if(($width / $height) > 1.2) {
// crop out the sides
$margin = $width - $height;
$img->cropImage(300,($margin / 2),0,$height,$height);
}
elseif(($height / $width) > 1.2) {
// crop out the bottom
$margin = $height - $width;
$img->cropImage(300,0,0,$width,$width);
2012-06-07 15:42:13 +00:00
}
else {
$img->scaleImageSquare(300);
}
}
else
$photo_failure = true;
2010-10-26 21:50:38 +00:00
2015-05-19 02:43:53 +00:00
$p = array('xchan' => $xchan,'resource_id' => $hash, 'filename' => basename($photo), 'album' => $album, 'photo_usage' => $flags, 'scale' => 4);
$r = $img->save($p);
2010-10-26 21:50:38 +00:00
2013-02-13 00:21:12 +00:00
if($r === false)
$photo_failure = true;
2010-10-26 21:50:38 +00:00
2013-02-13 00:21:12 +00:00
$img->scaleImage(80);
$p['scale'] = 5;
2010-10-26 21:50:38 +00:00
$r = $img->save($p);
2010-10-26 21:50:38 +00:00
2013-02-13 00:21:12 +00:00
if($r === false)
$photo_failure = true;
2010-10-26 21:50:38 +00:00
2013-02-13 00:21:12 +00:00
$img->scaleImage(48);
$p['scale'] = 6;
2010-11-05 06:50:32 +00:00
$r = $img->save($p);
2010-11-05 06:50:32 +00:00
2013-02-13 00:21:12 +00:00
if($r === false)
$photo_failure = true;
2010-11-05 06:50:32 +00:00
2013-02-13 00:21:12 +00:00
$photo = $a->get_baseurl() . '/photo/' . $hash . '-4';
$thumb = $a->get_baseurl() . '/photo/' . $hash . '-5';
$micro = $a->get_baseurl() . '/photo/' . $hash . '-6';
}
else {
logger('import_xchan_photo: invalid image from ' . $photo);
2013-02-13 00:21:12 +00:00
$photo_failure = true;
2013-02-11 04:09:32 +00:00
}
2013-02-13 00:21:12 +00:00
if($photo_failure) {
$photo = $a->get_baseurl() . '/' . get_default_profile_photo();
$thumb = $a->get_baseurl() . '/' . get_default_profile_photo(80);
$micro = $a->get_baseurl() . '/' . get_default_profile_photo(48);
2014-10-05 21:38:47 +00:00
$type = 'image/png';
2013-02-13 00:21:12 +00:00
}
2010-10-26 21:50:38 +00:00
return(array($photo,$thumb,$micro,$type,$photo_failure));
2010-10-26 21:50:38 +00:00
}
function import_channel_photo($photo,$type,$aid,$uid) {
$a = get_app();
logger('import_channel_photo: importing channel photo for ' . $uid, LOGGER_DEBUG);
$hash = photo_new_resource();
$photo_failure = false;
$filename = $hash;
$img = photo_factory($photo, $type);
if($img->is_valid()) {
$img->scaleImageSquare(300);
2015-05-19 02:43:53 +00:00
$p = array('aid' => $aid, 'uid' => $uid, 'resource_id' => $hash, 'filename' => $filename, 'album' => t('Profile Photos'), 'photo_usage' => PHOTO_PROFILE, 'scale' => 4);
$r = $img->save($p);
if($r === false)
$photo_failure = true;
$img->scaleImage(80);
$p['scale'] = 5;
$r = $img->save($p);
if($r === false)
$photo_failure = true;
$img->scaleImage(48);
$p['scale'] = 6;
$r = $img->save($p);
if($r === false)
$photo_failure = true;
}
else {
logger('import_channel_photo: invalid image.');
$photo_failure = true;
}
return(($photo_failure)? false : true);
}