<?php
class background
{
    const DIRECTORY = 'web/skin/background/';
    
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    public static function get_list()
    {
        $list = glob(self::DIRECTORY . '*.jpg');
        
        foreach ($list as &$l)
        {
            $l = pathinfo($l, PATHINFO_FILENAME);
        }
        
        return $list;
    }
    
    public static function save($name, array $file)
    {
        if (!preg_match('~((jpe?g)|(gif)|(png))$~i', $file['name']))
        {
            throw new exception();
        }
        
        $temp = self::DIRECTORY . '__temp_' . pathinfo($file['name'], PATHINFO_BASENAME);
        $target = self::DIRECTORY . $name . '.jpg';
        folder::move($file['tmp_name'], $temp);
        $image = new image($temp);
        $image->save($target, 70);
        folder::mode($target, 0775);
        folder::delete($temp);
    }
    
    public static function delete($name)
    {
        if (is_file($file = self::DIRECTORY . $name . '.jpg'))
        {
            folder::delete($file);
        }
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}
