Файл: adultscript-2.0.3-pro/files/mobile/components/photo_add.php
Строк: 75
<?php
defined('_VALID') or die('Restricted Access!');
class VComponent_mobile_photo_add extends VComponent_mobile_photo
{
    private $pcfg;
    public function __construct()
    {
        parent::__construct();
        
        $this->pcfg    = VF::cfg('module.photo');
    }
    
    public function render()
    {
        VAuth::check('Registered', MOBILE_URL.'/user/login/');
        
        VLanguage::load('frontend.photo');
        if (!$this->pcfg['upload_enabled']) {
              $this->notfound(__('upload-disabled'));
        }
        
        $album_id    = (VUri::request(2)) ? (int) VUri::request(2) : 0;
        $user_id    = (int) $_SESSION['user_id'];
        
        
        $errors     = array();
        $messages   = array();
        $warnings   = array();
        $unique     = time().'0'.mt_rand();
        $user_id    = (int) $_SESSION['user_id'];
        $album      = array();
        $album_id   = (int) VUri::request(2);
        $this->db->query("SELECT album_id, title, locked
                          FROM #__photo_albums
                          WHERE album_id = ".$album_id."
                          AND user_id = ".$user_id."
                          AND status = '1'
                          LIMIT 1");
        if ($this->db->affected_rows()) {
            $album  = $this->db->fetch_assoc();
        }
        if (isset($_POST['upload-submitted'])) {
              $filter        = VF::factory('filter');
              $upload_id    = $filter->get('upload_id');
            $tmp_dir    = TMP_DIR.'/uploads/'.$upload_id;
            if (!file_exists($tmp_dir) or !is_dir($tmp_dir)) {
                $errors[]   = 'Invalid tmp folder!?';
            }
            if (!$files = VFolder::files($tmp_dir, true)) {
                $errors[]   = 'Please selected at least one image for this album!';
            }
        
            if (!$errors) {
                $image      = VF::factory('image');
                $images     = 0;
                $ids        = array();
                foreach ($files as $entry) {
                    if ($entry) {
                        if (!VFile::exists($entry)) {
                            continue;
                        }
                        $size   = filesize($entry);
                        if ($size > $this->pcfg['photo_max_size']*1024*1024) {
                            continue;
                        }
                
                        $ext    = VFile::ext($entry);
                        if (!in_array($ext, $this->pcfg['photo_allowed_ext'])) {
                            continue;
                        }
 
                        $file   = array(
                            'path'  => $entry,
                            'name'  => VFile::name($entry),
                            'size'  => $size,
                            'ext'   => $ext
                        );
                        if (!$image->load($file['path'])) {
                            continue;
                        }
                        
                        $this->db->query("INSERT INTO #__photo
                                          SET album_id = ".$album_id.",
                                              ext = '".$this->db->escape($image->src['ext'])."',
                                              size = ".$file['size'].",
                                              add_date = '".date('Y-m-d h:i:s')."',
                                              status = '0'");
                        if (!$this->db->affected_rows()) {
                            continue;
                        }
                        
                        $photo_id   = $this->db->get_last_insert_id('#__photo');
                        $ext_orig   = $image->src['ext'];
                        $dst_orig   = MEDIA_DIR.'/photos/orig/'.$photo_id.'.'.$ext_orig;
                        if (!copy($file['path'], $dst_orig)) {
                            continue;
                        }
                        
                        $dst            = MEDIA_DIR.'/photos/'.$photo_id.'.'.$image->src['ext'];
                        $dst_thumb      = MEDIA_DIR.'/photos/thumbs/'.$photo_id.'.jpg';
                        $dst_thumb_tmp  = TMP_DIR.'/images/'.$photo_id.'.thumb.jpg';
                        if ($image->src['width'] < $this->pcfg['photo_width']) {
                            copy($file['path'], $dst);
                        } else {
                            if (!$image->resize($this->pcfg['photo_width'], $this->pcfg['photo_height'], 'MAX_WIDTH', $dst)) {
                                continue;
                            }
                        }
                        
                        if (!empty($this->pcfg['mobile'])) {
                            $dst_mobile = MEDIA_DIR.'/photos/mobile/'.$photo_id.'.'.$image->src['ext'];
                            if ($image->src['width'] < $this->pcfg['mobile_width']) {
                                copy($file['path'], $dst_mobile);
                            } else {
                                if (!$image->resize($this->pcfg['mobile_width'], $this->pcfg['mobile_height'], 'MAX_WIDTH', $dst_mobile)) {
                                    continue;
                                }
                            }
                        }
                        
                        $thumb_width    = $this->pcfg['thumb_width']+30;
                        $thumb_height   = $this->pcfg['thumb_height']+100;
                        $image->set_option('jpeg_quality', 100);
                        if (!$image->resize($thumb_width, $thumb_height, 'MAX_HEIGHT', $dst_thumb_tmp)) {
                            continue;
                        }
                        
                        $image->clear();
                        $image->load($dst_thumb_tmp);
                        if (!$image->crop_from_center($this->pcfg['thumb_width'], $this->pcfg['thumb_height'], $dst_thumb)) {
                            continue;
                        }
                        
                        ++$images;
                        $ids[]  = $photo_id;
                        $image->clear();
                        VFile::delete($dst_thumb_tmp);
                    }
                }
                
                if ($images === 0) {
                    $errors[]   = __('add-failed');
                } else {
                    if (isset($tmp_dir)) {
                        VFolder::delete($tmp_dir, true);
                    }
                
                    $this->db->query("UPDATE #__photo_albums
                                      SET total_photos = total_photos+".$images."
                                      WHERE album_id = ".$album_id."
                                      LIMIT 1");
                    $this->db->query("UPDATE #__photo
                                      SET status = '1'
                                      WHERE album_id = ".$album_id."
                                      AND photo_id IN (".implode(',', array_values($ids)).")");
                    $messages[] = __('add-success');
                }
            }
        }
    
        $this->tpl->menu        = 'photo';
        $this->tpl->meta_title    = __('photo-add-meta-title', array($this->mcfg['site_name']));
        $this->tpl->canonical    = BASE_URL.'/photo/add/'.$album_id.'/';
        $this->tpl->errors        = $errors;
        $this->tpl->messages    = $messages;
        $this->tpl->warnings    = $warnings;
        $this->tpl->pcfg        = $this->pcfg;
        $this->tpl->unique        = $unique;
        $this->tpl->album       = $album;
        $this->tpl->load(array('header', 'photo_add', 'footer'));
        $this->tpl->display();
    }    
}