<?php
/**
 * @package linea21.core
 * @subpackage library
 * @author Simon Georget <simon@linea21.com>
 * @version $id SVN
 * @access public
 * @license http://opensource.org/licenses/gpl-3.0.html
 */

//header('Content-type: application/json');
//header('Content-type: text/plain');
session_start();
@set_time_limit(0);
include_once('../config/define.php');

$foldername = $_REQUEST['foldername'];
$id = $_REQUEST['id'];
$file = $_FILES['file'];

if(!$l21auth->isWorkgroupUser($id, $sql_object)) die('no way!');

if (!empty($_FILES)) {
  $tempFile = $_FILES['file']['tmp_name'];
  $file = $_FILES['file'];
  $filename_parts = pathinfo($_FILES['file']['name']);
  $filename = cleanString($filename_parts['filename']).'.'.$filename_parts['extension'];

  $targetPath = dirname(__FILE__). DIRECTORY_SEPARATOR . 'userfiles'. DIRECTORY_SEPARATOR . 'workgroups' . DIRECTORY_SEPARATOR . $foldername . DIRECTORY_SEPARATOR;

  $targetFile = str_replace('//','/',$targetPath) . $filename;

  if (in_array(strtolower($filename_parts['extension']),explode(',', MEDIA_ALLOWED_EXT))) {
    if(!file_exists(str_replace('//','/',$targetPath)))
    mkdir(str_replace('//','/',$targetPath), 0755, true);

    move_uploaded_file($tempFile,$targetFile);
    echo '{"name":"'.$filename.'","type":"'.$file['type'].'","size":"'.$file['size'].'"}';
  } else {
    echo '{"error" : "File transfer error"}';
  }
}
?>