streams/mod/wall_upload.php

182 lines
3.8 KiB
PHP
Raw Normal View History

2010-07-23 03:22:03 +00:00
<?php
2010-07-23 05:41:45 +00:00
require_once('Photo.php');
2010-07-23 03:22:03 +00:00
2010-07-23 03:22:03 +00:00
function wall_upload_post(&$a) {
$using_api = ((x($_FILES,'media')) ? true : false);
if($using_api) {
require_once('include/api.php');
$user_info = api_get_user($a);
$nick = $user_info['screen_name'];
}
else {
if(argc() > 1)
2012-10-24 10:11:52 +00:00
$nick = argv(1);
}
$channel = null;
if($nick) {
2012-10-24 10:11:52 +00:00
$r = q("SELECT channel.* from channel where channel_address = '%s' limit 1",
dbesc($nick)
);
if($r)
$channel = $r[0];
}
if(! $channel) {
if($using_api)
return;
else {
notice( t('Channel not found.') . EOL);
killme();
}
}
$can_post = false;
$visitor = 0;
2012-10-24 10:11:52 +00:00
$page_owner_uid = $r[0]['channel_id'];
$observer = $a->get_observer();
if(! perm_is_allowed($page_owner_uid,$observer['xchan_hash'],'post_photos')) {
if($using_api)
return;
else {
notice( t('Permission denied.') . EOL);
killme();
}
}
2010-08-03 02:06:36 +00:00
2012-06-07 15:42:13 +00:00
if(x($_FILES,'userfile')) {
$src = $_FILES['userfile']['tmp_name'];
$filename = basename($_FILES['userfile']['name']);
$filesize = intval($_FILES['userfile']['size']);
$filetype = $_FILES['userfile']['type'];
}
elseif(x($_FILES,'media')) {
$src = $_FILES['media']['tmp_name'];
$filename = basename($_FILES['media']['name']);
$filesize = intval($_FILES['media']['size']);
$filetype = $_FILES['media']['type'];
}
else {
if($using_api)
return;
else {
notice( t('Empty upload.') . EOL);
killme();
}
}
if($filetype == "")
$filetype=guess_image_type($filename);
$maximagesize = get_config('system','maximagesize');
if(($maximagesize) && ($filesize > $maximagesize)) {
@unlink($src);
if($using_api)
return;
else {
echo sprintf( t('Image exceeds size limit of %d'), $maximagesize) . EOL;
killme();
}
}
$limit = service_class_fetch($page_owner_uid,'photo_upload_limit');
if($limit !== false) {
$r = q("select sum(size) as total from photo where uid = %d and scale = 0 ",
intval($page_owner_uid)
);
if(($r) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
@unlink($src);
if($using_api)
return;
else {
echo upgrade_message(true) . EOL ;
killme();
}
}
}
2010-07-23 05:41:45 +00:00
$imagedata = @file_get_contents($src);
2012-06-07 15:42:13 +00:00
$ph = new Photo($imagedata, $filetype);
2010-07-23 03:22:03 +00:00
if(! $ph->is_valid()) {
2010-07-23 05:41:45 +00:00
@unlink($src);
if($using_api)
return;
else {
echo ( t('Unable to process image.') . EOL);
killme();
}
2010-07-23 05:41:45 +00:00
}
2010-07-23 03:22:03 +00:00
$ph->orient($src);
2010-07-23 05:41:45 +00:00
@unlink($src);
2010-07-23 03:22:03 +00:00
$max_length = get_config('system','max_image_length');
if(! $max_length)
$max_length = MAX_IMAGE_LENGTH;
if($max_length > 0)
$ph->scaleImage($max_length);
2010-07-23 05:41:45 +00:00
$width = $ph->getWidth();
$height = $ph->getHeight();
$hash = photo_new_resource();
2010-07-23 05:41:45 +00:00
$smallest = 0;
2012-10-24 10:15:32 +00:00
$defperm = '<' . $channel['channel_hash'] . '>';
$aid = $channel['channel_account_id'];
$visitor = ((remote_user()) ? remote_user() : '');
$r = $ph->store($aid, $page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 0, 0, $defperm);
2010-08-05 03:03:38 +00:00
2010-07-23 06:17:41 +00:00
if(! $r) {
if($using_api)
return;
else {
echo ( t('Image upload failed.') . EOL);
killme();
}
2010-07-23 06:17:41 +00:00
}
2010-07-23 05:41:45 +00:00
if($width > 640 || $height > 640) {
$ph->scaleImage(640);
$r = $ph->store($aid, $page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 1, 0, $defperm);
2010-07-23 06:17:41 +00:00
if($r)
2010-07-23 05:41:45 +00:00
$smallest = 1;
}
if($width > 320 || $height > 320) {
$ph->scaleImage(320);
$r = $ph->store($aid, $page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 2, 0, $defperm);
2010-07-23 06:17:41 +00:00
if($r)
2010-07-23 05:41:45 +00:00
$smallest = 2;
}
$basename = basename($filename);
2012-02-07 08:13:16 +00:00
if($_REQUEST['silent']) {
2012-06-07 15:42:13 +00:00
$m = '[url=' . $a->get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . $a->get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
return($m);
2012-06-07 15:42:13 +00:00
}
else {
echo "\n\n" . '[url=' . $a->get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . $a->get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]\n\n";
}
2010-07-23 05:41:45 +00:00
2010-07-23 03:22:03 +00:00
killme();
// NOTREACHED
}