2016-04-19 03:38:38 +00:00
< ? php
namespace Zotlabs\Module ;
2019-09-18 04:25:26 +00:00
/*
* @ file Profile_photo . php
* @ brief Module - file with functions for uploading and scaling of profile - photos
*
*/
2019-04-17 23:54:06 +00:00
use App ;
use Zotlabs\Web\Controller ;
2018-06-05 01:40:11 +00:00
use Zotlabs\Lib\Libsync ;
2019-05-04 02:36:09 +00:00
use Zotlabs\Lib\Libprofile ;
2020-06-15 07:29:56 +00:00
use Zotlabs\Daemon\Run ;
2018-06-01 04:05:09 +00:00
2016-04-19 03:38:38 +00:00
2019-04-16 06:43:06 +00:00
require_once ( 'include/photo_factory.php' );
2016-04-19 03:38:38 +00:00
require_once ( 'include/photos.php' );
2019-04-17 23:54:06 +00:00
class Profile_photo extends Controller {
2016-04-19 03:38:38 +00:00
/* @ brief Initalize the profile - photo edit view
*
* @ return void
*
*/
2016-06-23 02:08:59 +00:00
function init () {
2016-04-19 03:38:38 +00:00
if ( ! local_channel ()) {
return ;
}
2019-08-08 06:02:30 +00:00
$channel = App :: get_channel ();
2019-04-17 23:54:06 +00:00
Libprofile :: load ( $channel [ 'channel_address' ]);
2016-04-19 03:38:38 +00:00
}
/* @ brief Evaluate posted values
*
* @ param $a Current application
* @ return void
*
*/
2016-06-23 02:08:59 +00:00
function post () {
2016-04-19 03:38:38 +00:00
2019-09-18 04:25:26 +00:00
if ( ! local_channel ()) {
2016-04-19 03:38:38 +00:00
return ;
}
check_form_security_token_redirectOnErr ( '/profile_photo' , 'profile_photo' );
2019-09-18 04:25:26 +00:00
if (( array_key_exists ( 'cropfinal' , $_POST )) && ( intval ( $_POST [ 'cropfinal' ]) == 1 )) {
2016-04-19 03:38:38 +00:00
2018-01-24 00:44:11 +00:00
// logger('crop: ' . print_r($_POST,true));
2018-01-24 00:36:56 +00:00
2016-06-23 08:17:44 +00:00
// phase 2 - we have finished cropping
2019-09-18 04:25:26 +00:00
if ( argc () != 2 ) {
2016-06-23 08:17:44 +00:00
notice ( t ( 'Image uploaded but image cropping failed.' ) . EOL );
return ;
}
$image_id = argv ( 1 );
2019-09-18 04:25:26 +00:00
if ( substr ( $image_id , - 2 , 1 ) == '-' ) {
2016-06-23 08:17:44 +00:00
$scale = substr ( $image_id , - 1 , 1 );
$image_id = substr ( $image_id , 0 , - 2 );
}
2016-04-19 03:38:38 +00:00
// unless proven otherwise
$is_default_profile = 1 ;
2019-09-18 04:25:26 +00:00
if ( $_REQUEST [ 'profile' ]) {
2016-04-19 03:38:38 +00:00
$r = q ( " select id, profile_guid, is_default, gender from profile where id = %d and uid = %d limit 1 " ,
intval ( $_REQUEST [ 'profile' ]),
intval ( local_channel ())
);
2019-09-18 04:25:26 +00:00
if ( $r ) {
$profile = array_shift ( $r );
if ( ! intval ( $profile [ 'is_default' ])) {
2016-04-19 03:38:38 +00:00
$is_default_profile = 0 ;
2019-09-18 04:25:26 +00:00
}
2016-04-19 03:38:38 +00:00
}
}
2016-06-23 03:46:44 +00:00
2018-01-24 00:36:56 +00:00
$srcX = intval ( $_POST [ 'xstart' ]);
$srcY = intval ( $_POST [ 'ystart' ]);
$srcW = intval ( $_POST [ 'xfinal' ]) - $srcX ;
$srcH = intval ( $_POST [ 'yfinal' ]) - $srcY ;
2016-06-28 05:25:37 +00:00
2016-06-02 04:48:54 +00:00
$r = q ( " SELECT * FROM photo WHERE resource_id = '%s' AND uid = %d AND imgscale = %d LIMIT 1 " ,
2016-04-19 03:38:38 +00:00
dbesc ( $image_id ),
dbesc ( local_channel ()),
intval ( $scale ));
2019-09-18 04:25:26 +00:00
if ( $r ) {
2016-04-19 03:38:38 +00:00
2019-09-18 04:25:26 +00:00
$base_image = array_shift ( $r );
$base_image [ 'content' ] = (( $base_image [ 'os_storage' ]) ? @ file_get_contents ( dbunescbin ( $base_image [ 'content' ])) : dbunescbin ( $base_image [ 'content' ]));
2016-04-19 03:38:38 +00:00
2016-06-02 04:48:54 +00:00
$im = photo_factory ( $base_image [ 'content' ], $base_image [ 'mimetype' ]);
2019-09-18 04:25:26 +00:00
if ( $im -> is_valid ()) {
2016-04-19 03:38:38 +00:00
$im -> cropImage ( 300 , $srcX , $srcY , $srcW , $srcH );
$aid = get_account_id ();
2016-06-23 08:17:44 +00:00
$p = [
2017-03-15 08:20:21 +00:00
'aid' => $aid ,
'uid' => local_channel (),
'resource_id' => $base_image [ 'resource_id' ],
'filename' => $base_image [ 'filename' ],
'album' => t ( 'Profile Photos' ),
'os_path' => $base_image [ 'os_path' ],
2019-02-14 05:07:27 +00:00
'display_path' => $base_image [ 'display_path' ],
'created' => $base_image [ 'created' ],
'edited' => $base_image [ 'edited' ]
2016-06-23 08:17:44 +00:00
];
2020-10-21 05:54:05 +00:00
$animated = get_config ( 'system' , 'animated_avatars' , true );
2016-06-23 08:17:44 +00:00
$p [ 'imgscale' ] = PHOTO_RES_PROFILE_300 ;
2016-04-19 03:38:38 +00:00
$p [ 'photo_usage' ] = (( $is_default_profile ) ? PHOTO_PROFILE : PHOTO_NORMAL );
2020-10-21 05:54:05 +00:00
$r1 = $im -> storeThumbnail ( $p , PHOTO_RES_PROFILE_300 , $animated );
2016-04-19 03:38:38 +00:00
$im -> scaleImage ( 80 );
2016-06-23 08:17:44 +00:00
$p [ 'imgscale' ] = PHOTO_RES_PROFILE_80 ;
2016-04-19 03:38:38 +00:00
2020-10-21 05:54:05 +00:00
$r2 = $im -> storeThumbnail ( $p , PHOTO_RES_PROFILE_80 , $animated );
2016-04-19 03:38:38 +00:00
$im -> scaleImage ( 48 );
2016-06-23 08:17:44 +00:00
$p [ 'imgscale' ] = PHOTO_RES_PROFILE_48 ;
2016-04-19 03:38:38 +00:00
2020-10-21 05:54:05 +00:00
$r3 = $im -> storeThumbnail ( $p , PHOTO_RES_PROFILE_48 , $animated );
2016-04-19 03:38:38 +00:00
2019-09-18 04:25:26 +00:00
if ( $r1 === false || $r2 === false || $r3 === false ) {
2016-04-19 03:38:38 +00:00
// if one failed, delete them all so we can start over.
notice ( t ( 'Image resize failed.' ) . EOL );
2016-06-23 08:17:44 +00:00
$x = q ( " delete from photo where resource_id = '%s' and uid = %d and imgscale in ( %d, %d, %d ) " ,
2016-04-19 03:38:38 +00:00
dbesc ( $base_image [ 'resource_id' ]),
2016-06-23 08:17:44 +00:00
local_channel (),
intval ( PHOTO_RES_PROFILE_300 ),
intval ( PHOTO_RES_PROFILE_80 ),
intval ( PHOTO_RES_PROFILE_48 )
2016-04-19 03:38:38 +00:00
);
return ;
}
2019-08-08 06:02:30 +00:00
$channel = App :: get_channel ();
2016-04-19 03:38:38 +00:00
// If setting for the default profile, unset the profile photo flag from any other photos I own
2019-09-18 04:25:26 +00:00
if ( $is_default_profile ) {
2017-11-01 09:15:40 +00:00
$r = q ( " update profile set photo = '%s', thumb = '%s' where is_default = 1 and uid = %d " ,
dbesc ( z_root () . '/photo/profile/l/' . local_channel ()),
dbesc ( z_root () . '/photo/profile/m/' . local_channel ()),
intval ( local_channel ())
);
2016-04-19 03:38:38 +00:00
$r = q ( " UPDATE photo SET photo_usage = %d WHERE photo_usage = %d
2016-10-09 23:31:04 +00:00
AND resource_id != '%s' AND uid = % d " ,
2016-04-19 03:38:38 +00:00
intval ( PHOTO_NORMAL ),
intval ( PHOTO_PROFILE ),
dbesc ( $base_image [ 'resource_id' ]),
intval ( local_channel ())
);
2017-10-02 01:22:55 +00:00
2016-04-19 03:38:38 +00:00
send_profile_photo_activity ( $channel , $base_image , $profile );
}
else {
$r = q ( " update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d " ,
dbesc ( z_root () . '/photo/' . $base_image [ 'resource_id' ] . '-4' ),
dbesc ( z_root () . '/photo/' . $base_image [ 'resource_id' ] . '-5' ),
intval ( $_REQUEST [ 'profile' ]),
intval ( local_channel ())
);
}
2017-12-31 22:45:42 +00:00
// set $send to false in profiles_build_sync() to return the data
// so that we only send one sync packet.
$sync_profiles = profiles_build_sync ( local_channel (), false );
2016-04-19 03:38:38 +00:00
// We'll set the updated profile-photo timestamp even if it isn't the default profile,
// so that browsers will do a cache update unconditionally
2017-10-02 01:22:55 +00:00
// Also set links back to site-specific profile photo url in case it was
// changed to a generic URL by a clone operation. Otherwise the new photo may
// not get pushed to other sites correctly.
2016-04-19 03:38:38 +00:00
2017-10-02 01:22:55 +00:00
$r = q ( " UPDATE xchan set xchan_photo_mimetype = '%s', xchan_photo_date = '%s', xchan_photo_l = '%s', xchan_photo_m = '%s', xchan_photo_s = '%s'
2016-04-19 03:38:38 +00:00
where xchan_hash = '%s' " ,
dbesc ( $im -> getType ()),
dbesc ( datetime_convert ()),
2017-10-02 01:22:55 +00:00
dbesc ( z_root () . '/photo/profile/l/' . $channel [ 'channel_id' ]),
dbesc ( z_root () . '/photo/profile/m/' . $channel [ 'channel_id' ]),
dbesc ( z_root () . '/photo/profile/s/' . $channel [ 'channel_id' ]),
2016-04-19 03:38:38 +00:00
dbesc ( $channel [ 'xchan_hash' ])
);
2017-02-12 23:56:33 +00:00
photo_profile_setperms ( local_channel (), $base_image [ 'resource_id' ], $_REQUEST [ 'profile' ]);
2017-02-25 23:04:17 +00:00
$sync = attach_export_data ( $channel , $base_image [ 'resource_id' ]);
2019-09-18 04:25:26 +00:00
if ( $sync ) {
2018-06-05 01:40:11 +00:00
Libsync :: build_sync_packet ( $channel [ 'channel_id' ], array ( 'file' => array ( $sync ), 'profile' => $sync_profiles ));
2019-09-18 04:25:26 +00:00
}
2017-02-12 23:56:33 +00:00
2017-05-12 00:39:26 +00:00
// Similarly, tell the nav bar to bypass the cache and update the avatar image.
2016-07-03 12:21:48 +00:00
$_SESSION [ 'reload_avatar' ] = true ;
2016-04-19 03:38:38 +00:00
info ( t ( 'Shift-reload the page or clear browser cache if the new photo does not display immediately.' ) . EOL );
// Update directory in background
2020-06-15 07:29:56 +00:00
Run :: Summon ( [ 'Directory' , $channel [ 'channel_id' ] ] );
2016-04-19 03:38:38 +00:00
}
2019-09-18 04:25:26 +00:00
else {
2016-04-19 03:38:38 +00:00
notice ( t ( 'Unable to process image' ) . EOL );
2019-09-18 04:25:26 +00:00
}
2016-04-19 03:38:38 +00:00
}
goaway ( z_root () . '/profiles' );
}
2016-06-23 08:17:44 +00:00
// A new photo was uploaded. Store it and save some important details
// in App::$data for use in the cropping function
2016-04-19 03:38:38 +00:00
$hash = photo_new_resource ();
2016-10-19 01:30:50 +00:00
$importing = false ;
2016-04-19 03:38:38 +00:00
$smallest = 0 ;
2016-10-19 01:30:50 +00:00
2019-09-18 04:25:26 +00:00
if ( $_REQUEST [ 'importfile' ]) {
2016-10-19 01:30:50 +00:00
$hash = $_REQUEST [ 'importfile' ];
$importing = true ;
}
else {
2019-03-06 02:31:38 +00:00
$matches = [];
$partial = false ;
2019-09-18 04:25:26 +00:00
if ( array_key_exists ( 'HTTP_CONTENT_RANGE' , $_SERVER )) {
2019-03-06 02:31:38 +00:00
$pm = preg_match ( '/bytes (\d*)\-(\d*)\/(\d*)/' , $_SERVER [ 'HTTP_CONTENT_RANGE' ], $matches );
2019-09-18 04:25:26 +00:00
if ( $pm ) {
logger ( 'Content-Range: ' . print_r ( $matches , true ), LOGGER_DEBUG );
2019-03-06 02:31:38 +00:00
$partial = true ;
}
}
2019-09-18 04:25:26 +00:00
if ( $partial ) {
2019-03-06 02:31:38 +00:00
$x = save_chunk ( $channel , $matches [ 1 ], $matches [ 2 ], $matches [ 3 ]);
2019-09-18 04:25:26 +00:00
if ( $x [ 'partial' ]) {
2019-03-06 02:31:38 +00:00
header ( 'Range: bytes=0-' . (( $x [ 'length' ]) ? $x [ 'length' ] - 1 : 0 ));
2021-01-20 02:21:48 +00:00
json_return_and_die ( $x );
2019-03-06 02:31:38 +00:00
}
else {
header ( 'Range: bytes=0-' . (( $x [ 'size' ]) ? $x [ 'size' ] - 1 : 0 ));
$_FILES [ 'userfile' ] = [
'name' => $x [ 'name' ],
'type' => $x [ 'type' ],
'tmp_name' => $x [ 'tmp_name' ],
'error' => $x [ 'error' ],
'size' => $x [ 'size' ]
];
}
}
else {
2019-09-18 04:25:26 +00:00
if ( ! array_key_exists ( 'userfile' , $_FILES )) {
2019-03-06 02:31:38 +00:00
$_FILES [ 'userfile' ] = [
'name' => $_FILES [ 'files' ][ 'name' ],
'type' => $_FILES [ 'files' ][ 'type' ],
'tmp_name' => $_FILES [ 'files' ][ 'tmp_name' ],
'error' => $_FILES [ 'files' ][ 'error' ],
'size' => $_FILES [ 'files' ][ 'size' ]
];
}
}
2019-08-08 06:02:30 +00:00
$res = attach_store ( App :: get_channel (), get_observer_hash (), '' , array ( 'album' => t ( 'Profile Photos' ), 'hash' => $hash ));
2016-04-19 03:38:38 +00:00
2019-09-18 04:25:26 +00:00
logger ( 'attach_store: ' . print_r ( $res , true ), LOGGER_DEBUG );
2019-03-06 03:51:41 +00:00
json_return_and_die ([ 'message' => $hash ]);
2016-10-19 01:30:50 +00:00
}
2016-04-19 03:38:38 +00:00
2019-09-18 04:25:26 +00:00
if (( $res && intval ( $res [ 'data' ][ 'is_photo' ])) || $importing ) {
2016-06-02 04:48:54 +00:00
$i = q ( " select * from photo where resource_id = '%s' and uid = %d order by imgscale " ,
2016-04-19 03:38:38 +00:00
dbesc ( $hash ),
intval ( local_channel ())
);
2019-09-18 04:25:26 +00:00
if ( ! $i ) {
2016-04-19 03:38:38 +00:00
notice ( t ( 'Image upload failed.' ) . EOL );
return ;
}
$os_storage = false ;
2019-09-18 04:25:26 +00:00
foreach ( $i as $ii ) {
if ( intval ( $ii [ 'imgscale' ]) < PHOTO_RES_640 ) {
2016-06-02 04:48:54 +00:00
$smallest = intval ( $ii [ 'imgscale' ]);
2016-04-19 03:38:38 +00:00
$os_storage = intval ( $ii [ 'os_storage' ]);
2016-06-02 04:48:54 +00:00
$imagedata = $ii [ 'content' ];
$filetype = $ii [ 'mimetype' ];
2016-04-19 03:38:38 +00:00
}
}
}
2016-10-24 03:01:39 +00:00
$imagedata = (( $os_storage ) ? @ file_get_contents ( dbunescbin ( $imagedata )) : dbunescbin ( $imagedata ));
2016-04-19 03:38:38 +00:00
$ph = photo_factory ( $imagedata , $filetype );
2019-09-18 04:25:26 +00:00
if ( ! $ph -> is_valid ()) {
2016-04-19 03:38:38 +00:00
notice ( t ( 'Unable to process image.' ) . EOL );
return ;
}
2019-09-18 04:25:26 +00:00
return $this -> profile_photo_crop_ui_head ( $ph , $hash , $smallest );
2016-06-23 08:17:44 +00:00
// This will "fall through" to the get() method, and since
// App::$data['imagecrop'] is set, it will proceed to cropping
// rather than present the upload form
2016-04-19 03:38:38 +00:00
}
/* @ brief Generate content of profile - photo view
*
* @ return void
*
*/
2016-06-02 04:48:54 +00:00
function get () {
2016-04-19 03:38:38 +00:00
2019-09-18 04:25:26 +00:00
if ( ! local_channel ()) {
2016-04-19 03:38:38 +00:00
notice ( t ( 'Permission denied.' ) . EOL );
return ;
}
2019-08-08 06:02:30 +00:00
$channel = App :: get_channel ();
2018-02-24 21:20:59 +00:00
$pf = 0 ;
2016-04-19 03:38:38 +00:00
$newuser = false ;
2019-09-18 04:25:26 +00:00
if ( argc () == 2 && argv ( 1 ) === 'new' ) {
2016-04-19 03:38:38 +00:00
$newuser = true ;
2019-09-18 04:25:26 +00:00
}
2016-04-19 03:38:38 +00:00
2019-09-18 04:25:26 +00:00
if ( argv ( 1 ) === 'use' ) {
2016-04-19 03:38:38 +00:00
if ( argc () < 3 ) {
notice ( t ( 'Permission denied.' ) . EOL );
return ;
};
2016-06-23 03:46:44 +00:00
2016-04-19 03:38:38 +00:00
$resource_id = argv ( 2 );
2018-02-24 21:20:59 +00:00
$pf = (( $_REQUEST [ 'pf' ]) ? intval ( $_REQUEST [ 'pf' ]) : 0 );
2016-06-23 03:46:44 +00:00
2016-10-19 01:30:50 +00:00
$c = q ( " select id, is_default from profile where uid = %d " ,
2016-06-23 03:46:44 +00:00
intval ( local_channel ())
);
2016-10-19 01:30:50 +00:00
$multi_profiles = true ;
2019-09-18 04:25:26 +00:00
if (( $c ) && ( count ( $c ) === 1 ) && ( intval ( $c [ 0 ][ 'is_default' ]))) {
2016-10-19 01:30:50 +00:00
$_REQUEST [ 'profile' ] = $c [ 0 ][ 'id' ];
$multi_profiles = false ;
2016-06-23 03:46:44 +00:00
}
2018-02-24 21:20:59 +00:00
else {
$_REQUEST [ 'profile' ] = $pf ;
}
2016-06-23 03:46:44 +00:00
2016-06-02 04:48:54 +00:00
$r = q ( " SELECT id, album, imgscale FROM photo WHERE uid = %d AND resource_id = '%s' ORDER BY imgscale ASC " ,
2016-04-19 03:38:38 +00:00
intval ( local_channel ()),
dbesc ( $resource_id )
);
2019-09-18 04:25:26 +00:00
if ( ! $r ) {
2016-04-19 03:38:38 +00:00
notice ( t ( 'Photo not available.' ) . EOL );
return ;
}
$havescale = false ;
2019-09-18 04:25:26 +00:00
foreach ( $r as $rr ) {
if ( $rr [ 'imgscale' ] == PHOTO_RES_PROFILE_80 ) {
2016-04-19 03:38:38 +00:00
$havescale = true ;
2019-09-18 04:25:26 +00:00
}
2016-04-19 03:38:38 +00:00
}
2016-06-23 08:17:44 +00:00
// set an already loaded and cropped photo as profile photo
2016-04-19 03:38:38 +00:00
2019-09-18 04:25:26 +00:00
if ( $havescale ) {
2016-04-19 03:38:38 +00:00
// unset any existing profile photos
$r = q ( " UPDATE photo SET photo_usage = %d WHERE photo_usage = %d AND uid = %d " ,
intval ( PHOTO_NORMAL ),
intval ( PHOTO_PROFILE ),
2019-09-18 04:25:26 +00:00
intval ( local_channel ())
);
2016-04-19 03:38:38 +00:00
$r = q ( " UPDATE photo SET photo_usage = %d WHERE uid = %d AND resource_id = '%s' " ,
intval ( PHOTO_PROFILE ),
intval ( local_channel ()),
dbesc ( $resource_id )
2019-09-18 04:25:26 +00:00
);
2016-04-19 03:38:38 +00:00
2019-09-18 04:25:26 +00:00
$r = q ( " UPDATE xchan set xchan_photo_date = '%s' where xchan_hash = '%s' " ,
2016-04-19 03:38:38 +00:00
dbesc ( datetime_convert ()),
dbesc ( $channel [ 'xchan_hash' ])
);
2017-02-12 23:56:33 +00:00
photo_profile_setperms ( local_channel (), $resource_id , $_REQUEST [ 'profile' ]);
2017-02-25 23:04:17 +00:00
$sync = attach_export_data ( $channel , $resource_id );
2019-09-18 04:25:26 +00:00
if ( $sync ) {
2018-06-05 01:40:11 +00:00
Libsync :: build_sync_packet ( $channel [ 'channel_id' ], array ( 'file' => array ( $sync )));
2019-09-18 04:25:26 +00:00
}
2017-02-25 23:04:17 +00:00
2020-06-15 07:29:56 +00:00
Run :: Summon ( [ 'Directory' , local_channel () ] );
2016-04-19 03:38:38 +00:00
goaway ( z_root () . '/profiles' );
}
2016-06-02 04:48:54 +00:00
$r = q ( " SELECT content, mimetype, resource_id, os_storage FROM photo WHERE id = %d and uid = %d limit 1 " ,
2016-04-19 03:38:38 +00:00
intval ( $r [ 0 ][ 'id' ]),
intval ( local_channel ())
);
2019-09-18 04:25:26 +00:00
if ( ! $r ) {
2016-04-19 03:38:38 +00:00
notice ( t ( 'Photo not available.' ) . EOL );
return ;
}
2019-09-18 04:25:26 +00:00
if ( intval ( $r [ 0 ][ 'os_storage' ])) {
2016-10-24 00:46:29 +00:00
$data = @ file_get_contents ( dbunescbin ( $r [ 0 ][ 'content' ]));
2019-09-18 04:25:26 +00:00
}
else {
2016-06-02 04:48:54 +00:00
$data = dbunescbin ( $r [ 0 ][ 'content' ]);
2019-09-18 04:25:26 +00:00
}
2016-06-02 04:48:54 +00:00
$ph = photo_factory ( $data , $r [ 0 ][ 'mimetype' ]);
2016-04-19 03:38:38 +00:00
$smallest = 0 ;
2019-09-18 04:25:26 +00:00
if ( $ph -> is_valid ()) {
2016-04-19 03:38:38 +00:00
// go ahead as if we have just uploaded a new photo to crop
2016-06-02 04:48:54 +00:00
$i = q ( " select resource_id, imgscale from photo where resource_id = '%s' and uid = %d order by imgscale " ,
2016-04-19 03:38:38 +00:00
dbesc ( $r [ 0 ][ 'resource_id' ]),
intval ( local_channel ())
);
2019-09-18 04:25:26 +00:00
if ( $i ) {
2016-04-19 03:38:38 +00:00
$hash = $i [ 0 ][ 'resource_id' ];
2019-09-18 04:25:26 +00:00
foreach ( $i as $ii ) {
if ( intval ( $ii [ 'imgscale' ]) < PHOTO_RES_640 ) {
2016-06-02 04:48:54 +00:00
$smallest = intval ( $ii [ 'imgscale' ]);
2016-04-19 03:38:38 +00:00
}
}
}
}
2019-09-18 04:25:26 +00:00
if ( $multi_profiles ) {
2019-08-08 06:02:30 +00:00
App :: $data [ 'importfile' ] = $resource_id ;
2016-10-19 01:30:50 +00:00
}
else {
2019-09-18 04:25:26 +00:00
$this -> profile_photo_crop_ui_head ( $ph , $hash , $smallest );
2016-10-19 01:30:50 +00:00
}
2016-06-23 08:17:44 +00:00
// falls through with App::$data['imagecrop'] set so we go straight to the cropping section
2016-10-19 01:30:50 +00:00
2016-04-19 03:38:38 +00:00
}
2016-06-23 08:17:44 +00:00
// present an upload form
$profiles = q ( " select id, profile_name as name, is_default from profile where uid = %d order by id asc " ,
2016-04-19 03:38:38 +00:00
intval ( local_channel ())
);
2016-10-19 01:30:50 +00:00
2019-09-18 04:25:26 +00:00
if ( $profiles ) {
for ( $x = 0 ; $x < count ( $profiles ); $x ++ ) {
2018-02-24 21:20:59 +00:00
$profiles [ $x ][ 'selected' ] = false ;
2019-09-18 04:25:26 +00:00
if ( $pf && $profiles [ $x ][ 'id' ] == $pf ) {
2018-02-24 21:20:59 +00:00
$profiles [ $x ][ 'selected' ] = true ;
2019-09-18 04:25:26 +00:00
}
if (( ! $pf ) && $profiles [ $x ][ 'is_default' ]) {
2018-02-24 21:20:59 +00:00
$profiles [ $x ][ 'selected' ] = true ;
2019-09-18 04:25:26 +00:00
}
2018-02-24 21:20:59 +00:00
}
}
2019-08-08 06:02:30 +00:00
$importing = (( array_key_exists ( 'importfile' , App :: $data )) ? true : false );
2016-04-19 03:38:38 +00:00
2019-09-18 04:25:26 +00:00
if ( ! array_key_exists ( 'imagecrop' , App :: $data )) {
2016-04-19 03:38:38 +00:00
$tpl = get_markup_template ( 'profile_photo.tpl' );
2019-09-18 04:25:26 +00:00
$o .= replace_macros ( $tpl , [
'$user' => App :: $channel [ 'channel_address' ],
'$info' => (( count ( $profiles ) > 1 ) ? t ( 'Your default profile photo is visible to anybody on the internet. Profile photos for alternate profiles will inherit the permissions of the profile' ) : t ( 'Your profile photo is visible to anybody on the internet and may be distributed to other websites.' )),
'$importfile' => (( $importing ) ? App :: $data [ 'importfile' ] : '' ),
'$lbl_upfile' => t ( 'Upload File:' ),
'$lbl_profiles' => t ( 'Select a profile:' ),
'$title' => (( $importing ) ? t ( 'Use Photo for Profile' ) : t ( 'Change Profile Photo' )),
'$submit' => (( $importing ) ? t ( 'Use' ) : t ( 'Upload' )),
'$profiles' => $profiles ,
'$single' => (( count ( $profiles ) == 1 ) ? true : false ),
'$profile0' => $profiles [ 0 ],
'$embedPhotos' => t ( 'Use a photo from your albums' ),
'$embedPhotosModalTitle' => t ( 'Use a photo from your albums' ),
2018-02-24 03:23:17 +00:00
'$embedPhotosModalCancel' => t ( 'Cancel' ),
2019-09-18 04:25:26 +00:00
'$embedPhotosModalOK' => t ( 'OK' ),
'$modalchooseimages' => t ( 'Choose images to embed' ),
'$modalchoosealbum' => t ( 'Choose an album' ),
'$modaldiffalbum' => t ( 'Choose a different album' ),
'$modalerrorlist' => t ( 'Error getting album list' ),
'$modalerrorlink' => t ( 'Error getting photo link' ),
'$modalerroralbum' => t ( 'Error getting album' ),
'$form_security_token' => get_form_security_token ( " profile_photo " ),
'$select' => t ( 'Select previously uploaded photo' ),
]);
2016-04-19 03:38:38 +00:00
call_hooks ( 'profile_photo_content_end' , $o );
return $o ;
}
else {
2016-06-23 08:17:44 +00:00
// present a cropping form
2019-09-18 04:25:26 +00:00
$filename = App :: $data [ 'imagecrop' ] . '-' . App :: $data [ 'imagecrop_resolution' ];
2019-08-08 06:02:30 +00:00
$resolution = App :: $data [ 'imagecrop_resolution' ];
2019-09-18 04:25:26 +00:00
$o .= replace_macros ( get_markup_template ( 'cropbody.tpl' ), [
'$filename' => $filename ,
'$profile' => intval ( $_REQUEST [ 'profile' ]),
'$resource' => App :: $data [ 'imagecrop' ] . '-' . App :: $data [ 'imagecrop_resolution' ],
'$image_url' => z_root () . '/photo/' . $filename ,
'$title' => t ( 'Crop Image' ),
'$desc' => t ( 'Please adjust the image cropping for optimum viewing.' ),
2016-04-19 03:38:38 +00:00
'$form_security_token' => get_form_security_token ( " profile_photo " ),
2019-09-18 04:25:26 +00:00
'$done' => t ( 'Done Editing' )
]);
2016-04-19 03:38:38 +00:00
return $o ;
}
}
/* @ brief Generate the UI for photo - cropping
*
* @ param $ph Photo - Factory
* @ return void
*
*/
2019-09-18 04:25:26 +00:00
function profile_photo_crop_ui_head ( $ph , $hash , $smallest ) {
2016-04-19 03:38:38 +00:00
$max_length = get_config ( 'system' , 'max_image_length' );
2019-09-18 04:25:26 +00:00
if ( ! $max_length ) {
2016-04-19 03:38:38 +00:00
$max_length = MAX_IMAGE_LENGTH ;
2019-09-18 04:25:26 +00:00
}
if ( $max_length > 0 ) {
2016-04-19 03:38:38 +00:00
$ph -> scaleImage ( $max_length );
2019-09-18 04:25:26 +00:00
}
2019-08-08 06:02:30 +00:00
App :: $data [ 'width' ] = $ph -> getWidth ();
App :: $data [ 'height' ] = $ph -> getHeight ();
2016-04-19 03:38:38 +00:00
2019-09-18 04:25:26 +00:00
if ( App :: $data [ 'width' ] < 500 || App :: $data [ 'height' ] < 500 ) {
2016-04-19 03:38:38 +00:00
$ph -> scaleImageUp ( 400 );
2019-08-08 06:02:30 +00:00
App :: $data [ 'width' ] = $ph -> getWidth ();
App :: $data [ 'height' ] = $ph -> getHeight ();
2016-04-19 03:38:38 +00:00
}
2019-08-08 06:02:30 +00:00
App :: $data [ 'imagecrop' ] = $hash ;
App :: $data [ 'imagecrop_resolution' ] = $smallest ;
2019-09-18 04:25:26 +00:00
App :: $page [ 'htmlhead' ] .= replace_macros ( get_markup_template ( 'crophead.tpl' ), []);
2016-04-19 03:38:38 +00:00
return ;
}
}