<?php
/*
NetworX - open-source social networks platform
Copyright (C) 2009 SocialABC, Inc. http://www.socialabc.com
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program in a file called LICENSE; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
$section = 'files';
require_once('includes/application_top.php');
if (!Session::isCurrentSessionLoggedIn())
{
cmn_unauthorized_action();
}
$entity = isset($_GET['entity']) ? $_GET['entity'] : ( isset($_POST['Entity']) ? $_POST['Entity'] : 'User');
$id = isset($_GET['id']) ? $_GET['id'] : ( isset($_POST['ID']) ? $_POST['ID'] : $user->UserID );
$object = Entity::getObject($entity, $id);
$is_allowed = false;
switch (strtolower($entity))
{
case 'user':
$is_allowed = $object->hasBuddy($user->UserID) || $user->UserID == $id;
break;
case 'group':
case 'organization':
case 'community':
$is_allowed = true;//$object->hasMember($user->UserID);
break;
}
if ( (!$is_allowed) || ($object->{db_get_entity_key($object)} == -1) )
{
cmn_unauthorized_action();
}
if ($_POST['action'] == 'save')
{
$path = './tmp/files/' . strtolower($entity) . '/' . $id . '/';
$uploaddir = PATH_DOWNLOAD_FILES . strtolower($entity) . '/' . $id . '/';
cmn_create_path($uploaddir, false);
//cmn_create_path($path, false);
if (file_exists($path))
{
$d = dir($path);
//$photos = array();
while (false !== ($file = $d->read()))
{
if ($file != '.' && $file != '..')
{
$pathinfo = pathinfo($file);
$shared_file = new SharedFile(-1);
$shared_file->{ucfirst($entity) . 'ID'} = $id;
$shared_file->FileName = $pathinfo['basename'] . '.' . $path_parts['extension'];
$shared_file->FilePath = $uploaddir;
$shared_file->FileMIMEType = $pathinfo['extension'];
$shared_file->saveToDB();
rename($path . $file, $uploaddir . $shared_file->SharedFileID);
}
}
cmn_remove_directory($path);
cmn_redirect('file_sharing.php?entity=' . $entity . '&id=' . $id);
}
}
elseif ($_POST['action'] == 'cancel')
{
$path = './tmp/files/' . strtolower($entity) . '/' . $id . '/';
cmn_remove_directory($path);
cmn_redirect('file_sharing.php?entity=' . $entity . '&id=' . $id);
}
if ($_POST['action'] == 'delete' && !empty($_POST['SharedFileID']))
{
$shared_file = new SharedFile($_POST['SharedFileID']);
if (strtolower($entity) == 'user')
{
$file_size_mb = $shared_file->getFileSize() / 1024 / 1024;
ServerSpace::subUsedSpaceByUser($id, $file_size_mb);
}
$shared_file->delete();
}
$shared_files = $object->getSharedFiles();
if (count($shared_files) > 0)
{
foreach ($shared_files as $file)
{
$template->setVariable('FileMIMEType', $file->FileMIMEType);
$template->setVariable('SharedFileID', $file->SharedFileID);
$template->setVariable('FileName', $file->FileName);
$template->setVariable('TAG_TYPE_SHARED_FILE', TAG_TYPE_SHARED_FILE);
$template->setVariable('FileDescription', $file->FileDescription);
$template->setVariable('Updated', $file->Updated);
if ($object->userIsAdmin($user->UserID) || $user->UserID == $file->UserID)
{
$template->touchBlock('settings');
}
else
{
$template->hideBlock('settings');
}
$template->parse('shared_file');
}
}
else
{
$template->hideBlock('shared_file');
}
$template->setGlobalVariable('EntityID', $id);
if (strtolower($entity) == 'user')
{
$template->setGlobalVariable('Entity', 'user');
}
else
{
$template->setGlobalVariable('Entity', $entity);
}
$template->parse('back_link');
$template->setVariable('Entity', $entity);
$template->setVariable('ID', $id);
$confirm_msg = Message::getMessageTemplate('shared_file_delete_message.html');
$template->setGlobalVariable('SHARED_FILE_DELETE_MESSAGE', $confirm_msg->get());
$template->setVariable('session_id', session_id());
$template->setVariable('UPLOAD_SCRIPT_PATH', dirname($_SERVER["REQUEST_URI"])."/");
if ($object->hasMember($user->UserID) && $object->GroupStatus == GROUP_STATUS_PUBLIC || $object->userIsAdmin($user->UserID))
{
$template->parse('upload_button');
$template->touchBlock('save');
}
else
{
$template->hideBlock('upload_button');
}
require_once('includes/application_bottom.php');