<?php
defined('_VALID') or die('Restricted Access!');

require $config['BASE_DIR']. '/classes/auth.class.php';
require $config['BASE_DIR']. '/classes/image.class.php';
require $config['BASE_DIR']. '/classes/filter.class.php';

$auth   = new Auth();
$auth->check();

if ( isset($_SESSION['uid']) && $uid != $_SESSION['uid'] ) {
    session_write_close();
    header('Location: ' .$config['BASE_URL']. '/error/album_permission');
    die();
}

if ( isset($_POST['add_photos_submit']) ) {
    $photos     = 0;
    $filter     = new VFilter();
    $image      = new VImageConv();
    foreach ( $_FILES as $key => $values ) {
        if ( $values['tmp_name'] != '' ) {
            if ( is_uploaded_file($values['tmp_name']) ) {
                $photo_expl     = explode('_', $key);
                $photo_nr       = $photo_expl['1'];
                $caption        = $filter->get('caption_' .$photo_nr);
                $sql_add        = NULL;
                if ( $caption != '' ) {
                    $sql_add    = ", caption = '" .mysql_real_escape_string($caption). "'";
                }
                $sql            = "INSERT INTO photos SET AID = " .$aid . $sql_add;
                $conn->execute($sql);
                $photo_id       = mysql_insert_id();
                $src            = $values['tmp_name'];
                $dst            = $config['BASE_DIR']. '/media/photos/tmb/' .$photo_id. '.jpg';
                $image->process($src, $dst, 'MAX_WIDTH', 150, 0);
                $image->resize(true, true);
                $dst            = $config['BASE_DIR']. '/media/photos/' .$photo_id. '.jpg';
                $image->process($src, $dst, 'MAX_WIDTH', 575, 0);
                $image->resize(true, true);
                ++$photos;
            }
        }
    }
    
    $sql        = "UPDATE albums SET total_photos = total_photos+" .intval($photos). " WHERE AID = " .$aid. " LIMIT 1";
    $conn->execute($sql);
    $_SESSION['message'] = 'Photos successfully added!';
    header('Location: ' .$config['BASE_URL']. '/album/' .$aid);
    die();
}            
?>
