websiteWebsite
codingteam CodingTeam
A free forge, lightweight and extensible.

 

Browse the code

Revision log Information on the revision
Revision: 498 (differences)
Author: xbright
Log message: * 2012 gigayears! 2012 gigayears. Great Scott!
Change revision:
<?php
//   This file is a part of CodingTeam. Take a look at <http://codingteam.org>.
//   Copyright © 2007-2012 Erwan Briand <erwan@codingteam.net>
//
//   This program is free software: you can redistribute it and/or modify it
//   under the terms of the GNU Affero General Public License as published by
//   the Free Software Foundation, version 3 only.
//
//   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 Affero General Public
//   License for more details.
//
//   You should have received a copy of the GNU Affero General Public License
//   along with this program. If not, see <http://www.gnu.org/licenses/>.
 
class View {
    private $ct_session, $ct_db, $page, $error, $langlist, $lang;
    public $metatags, $maincontent;
 
    function __construct($session, $db, $page, $error, $langlist, $lang)
    {
        // Variables
        $this->ct_session = $session;
        $this->ct_db = $db;
        $this->page = $page;
        $this->error = $error;
        $this->langlist = $langlist;
        $this->lang = $lang;
 
        // Security
        if (!$this->ct_session->isLogged())
            $this->error->displayError(i18n('Please log-in…'), 0);
 
        // Create user directory
        $user = getUser($_SESSION['id'], $this->ct_db);
        $nickname = htmlspecialchars($user->getNickname());
        $this->folder = CT_BASEDIR.'/public/upload/briefcase/'.$nickname;
 
        if (!file_exists($this->folder))
            mkdir($this->folder, 0755, TRUE);
 
        // Preferences
        $cfg = getClass('config', $this->ct_db);
        $this->quota = $cfg->get('global', 'briefcase-max');
 
        // Walk
        $this->content = array();
        $this->size = 0;
        $this->files = 0;
 
        $dir = opendir($this->folder);
        while ($file = readdir($dir))
            if (!in_array($file, array('.', '..')))
            {
                $f = $this->folder.'/'.$file;
                $s = filesize($f);
 
                $l = 'users/briefcase/'.$nickname.'/'.$file;
 
                $this->size += $s;
                $this->files ++;
                array_push($this->content, array(format_size($s), $file, $l));
            }
        closedir($dir);
 
        // Meta tags
        $this->metatags = array('title' => i18n('Briefcase'));
    }
 
    function showFeed()
    {
    }
 
    function treatForms()
    {
        $this->form_error = 0;
        $form_quota = $this->quota * 1000000;
 
        // Clean POST values
        foreach ($_POST as $key => $value)
            if (!is_scalar($value))
                exit('Error.');
 
        if (isset($_POST['delete-file']))
        {
            $file = $this->ct_db->cleanentry($_POST['delete-file'], TRUE);
            if (file_exists($this->folder.'/'.$file))
                unlink($this->folder.'/'.$file);
 
            Header('Location: '.CT_BASEURL.'users/briefcase');
        }
        elseif (isset($_FILES['file_upload']))
        {
            if (($this->size >= $form_quota))
                $this->form_error = i18n('Your quota is exceeded.');
 
            $content_dir = $this->folder.'/';
            $tmp_file = $_FILES['file_upload']['tmp_name'];
 
            if(!is_uploaded_file($tmp_file))
                $this->form_error = i18n('File not found.');
 
            if(preg_match('#[\x00-\x1F\x7F-\x9F/\\\\]#', $_FILES['file_upload']['name']))
                exit('Error: Unidentified error occurred.');
 
            $filename = strtolower($_FILES['file_upload']['name']);
 
            if (file_exists($content_dir.$filename))
                $this->form_error = i18n('This file already exists!');
 
            if (!$this->form_error)
            {
                if(!move_uploaded_file($tmp_file, $content_dir.$filename))
                    $this->form_error = i18n('Error when trying to upload file.');
 
                Header('Location: '.CT_BASEURL.'users/briefcase');
            }
       }
    }
 
    function constructView()
    {
        $construct = array();
        $construct['__tpl__'] = 'briefcase.tpl';
 
        $construct['form_error'] = $this->form_error;
        $construct['quota'] = $this->quota;
        $construct['size'] = round($this->size / 1000000, 1);
        $construct['nb'] = $this->files;
        $construct['files'] = $this->content;
 
        return $construct;
    }
}
?>