websiteWebsite
codingteam CodingTeam
A free forge, lightweight and extensible.

 

Browse the code

Revision log Information on the revision
Revision: 346 (differences)
Author: xbright
Log message: * Fixed #1114
Change revision:
<?php
#    This file is a part of CodingTeam. See <http://www.codingteam.net>.
#    Copyright (C) 2007-2010 CodingTeam (See AUTHORS and THANKS for details)
#
#    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 $session, $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;
 
        if (empty($this->page[2]))
        {
            Header ('Status: 301 Moved Permanently', false, 301);
            Header ('Location: '.CT_BASEURL.'projects');
            exit(); 
        }
 
        if (!mb_ereg("^[0-9a-z_-]+$", $this->page[2]))
            exit('Error.');
 
        // Load project informations
        $this->project = getClass('projects.projects', $this->ct_db);
        $projects = $this->project->load($this->page[2], 'dbname');
        if (!$projects)
            $this->error->displayError(i18n('Project not found.'), 0);
 
        $this->id = $this->project->getId();
        $this->name = htmlspecialchars($this->project->getName());
        $this->dbname = htmlspecialchars($this->project->getDbname());
        $this->downloads = $this->project->getDownloads();
        $this->userid = $this->project->getUserid();
 
        $this->isvalid = $this->project->getValidity();
        $this->isprivate = $this->project->getPrivacy();
        check_can_see_project($this->isvalid, $this->isprivate, $this->id,
                              $this->userid, $this->ct_session, $this->ct_db,
                              $this->error);
 
        $this->project_admins = getClass('project.admins', $this->ct_db);
        $this->project_versions = getClass('project.versions', $this->ct_db);
        $this->project_screenshots = getClass('project.screenshots', $this->ct_db);
 
        // Popularity contest
        $previous_popularity = $this->project->getPopularity();
        $new_popularity = $previous_popularity + 1;
        $this->project->setPopularity($new_popularity);
 
        $this->config = getClass('config', $this->ct_db);
        $previous_overall = $this->config->get('projects', 'overall-popularity');
        $new_overall = $previous_overall + 1;
        $this->config->update('projects', 'overall-popularity', $new_overall);
 
        // Meta tags
        $this->metatags = array('title' => i18n('Screenshots - %(name)s', array('name' => $this->name)));
    }
 
    function showFeed()
    {
    }
 
    function treatForms()
    {
        // Start error handler
        $this->form_error = 0;
        $this->form_info = 0;
 
        if ($this->ct_session->isLogged())
            if (is_level($_SESSION['id'], 'administrator', $this->ct_db) ||
                is_affiliation($_SESSION['id'], $this->id, $this->ct_db, 'screenshot'))
            {
                // Clean POST values
                foreach ($_POST as $key => $value)
                    if (!is_scalar($value))
                        exit('Error.');
 
                if (isset($_POST['delete-screenshot']))
                {
                    $screenshot = $this->ct_db->cleanentry($_POST['delete-screenshot'], TRUE);
 
                    $this->project_screenshots->deleteScreenshot($this->id, $screenshot, $this->dbname);
                    deleteCacheVersion('/project/'.$this->dbname.'/screenshots');
                }
                elseif (count($_POST) >= 3)
                {
                    $this->form_name = $this->ct_db->cleanentry($_POST['name'], TRUE);
                    $this->form_version = $this->ct_db->cleanentry($_POST['version'], TRUE);
 
                    if (empty($this->form_name))
                        $this->form_error = i18n('You should write a name!');
 
                    if (!$this->project_versions->versionExist($this->id, $this->form_version))
                        exit('Error.');
 
                    $this->form_file = $this->ct_db->cleanentry($_POST['file'], TRUE);
                    switch ($this->form_file)
                    {
                    case 'url':
                        $this->form_file_ = $this->ct_db->cleanentry($_POST['file_url'], TRUE);
                        if (empty($this->form_file_))
                            $this->form_error = i18n('You should choose the URL of the file.');
                        break;
            
                    case 'upload':
                        $content_dir = CT_BASEDIR.'/public/upload/projects/'.$this->dbname.'/screenshots/';
                        $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 (!is_dir($content_dir))
                            mkdir($content_dir, 0755, TRUE);
 
                        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.');
                            
                            $this->form_file_ = $filename;
                        }
                        break;
 
                    default:
                        exit('Error.');
                    }
 
                    if (!$this->form_error)
                    {
                        $this->project_screenshots->addScreenshot($this->id,
                                    array('name' => $this->form_name,
                                          'version' => $this->form_version,
                                          'file' => $this->form_file_));
 
                        $this->form_info = i18n('Your screenshot have been added!');
                        deleteCacheVersion('/project/'.$this->dbname.'/screenshots');
                    }
                }
            }
    }
 
    function constructView()
    {
        $construct = array();
        $construct['__tpl__'] = 'screenshots.tpl';
 
        $versions = $this->project_versions->getVersions($this->id);
        $available_status = array('planned'     => i18n('Planned'),
                                  'development' => i18n('Development'),
                                  'testing'     => i18n('Testing'),
                                  'alpha'       => i18n('Alpha'),
                                  'beta'        => i18n('Beta'),
                                  'stable'      => i18n('Stable'),
                                  'abandonned'  => i18n('Abandonned'));
 
        $construct['_is_projectadmin'] = FALSE;
        if ($this->ct_session->isLogged())
            if (is_level($_SESSION['id'], 'administrator', $this->ct_db) ||
                is_affiliation($_SESSION['id'], $this->id, $this->ct_db))
                $construct['_is_projectadmin'] = TRUE;
 
        $construct['screenshots'] = array();
 
        if (count($versions) >= 1)
        {
            $construct['has_versions'] = TRUE;
 
            $classed = $versions;
            usort($classed, 'invertedvsort');
 
            foreach ($classed as $version)
            {
                $screenshots = $this->project_screenshots->getScreenshotsByVersion($this->id, $version['version']);
 
                if (count($screenshots) == 0)
                    continue;
 
                if (!array_key_exists($version['version'], $construct['screenshots']))
                    $construct['screenshots'][$version['version']] =
                        array(htmlspecialchars($version['version']), array());
 
                foreach ($screenshots as $screenshot)
                {
                    $image = htmlspecialchars($screenshot['image']);
 
                    $file = 'public/upload/projects/'.$this->dbname.'/screenshots/'.$image;
       
                    if (file_exists(CT_BASEDIR.'/'.$file))
                        $url = 'project/'.$this->dbname.'/upload/screenshots/'.$image;
                    else
                        $url = $image;
 
                    array_push($construct['screenshots'][$version['version']][1],
                          array('name'  => htmlspecialchars($screenshot['name']),
                                'image' => $url,
                                'id'    => $screenshot['id']));
                }
            }
        }
        else
            $construct['has_versions'] = FALSE;
 
        // Administration
        if ($this->ct_session->isLogged())
            if (is_level($_SESSION['id'], 'administrator', $this->ct_db) || is_affiliation($_SESSION['id'], $this->id, $this->ct_db))
            {
                if (count($versions) >= 1)
                {
                    $construct['form_error'] = $this->form_error;
                    $construct['form_info'] = $this->form_info;
 
                    $construct['versions'] = array();
                    foreach ($versions as $version)
                        array_push($construct['versions'],
                                   array('key'   => htmlspecialchars($version['version']),
                                         'value' => $available_status[$version['status']]));
                }
            }
 
        return $construct;
    }
}
?>