View file foto/img.php

File size: 4.19Kb
<?php
require('../sys/inc/core.php');
$p = (int)$_GET['id'];
if (isset($_GET['size'])) {
    $w = $z = (int)$_GET['size'];
}
$u = (isset($_SESSION['id_user']) ? $_SESSION['id_user'] : -1);
$pwd = (isset($_SESSION['pass']) ? $_SESSION['pass'] : 0);
$cache = new Cache(600);
$cached = array(
    50,
    128,
    640
);
$cacheLv = 'photo_access.' . $u . '.' . $p;
$access = $cache->read($cacheLv);
if ($access) {
    $p = $access;
}
$cacheId = false;
if (isset($w) && in_array($w, $cached) && $w != 0) {
    $cacheId = 'photo.' . $p . '.' . $w;
    if ($w == 50) {
        $w = (int)$set['photo_size_50'];
    } elseif ($w == 128) {
        $w = (int)$set['photo_size_128'];
    } elseif ($w == 640) {
        $w = (int)$set['photo_size_640'];
    }
}
$imageCache = $cache->read($cacheId, false, true);
if (!$access && $p) {

    $photo = db::fetch("
          SELECT ph.id, ph.effects, ph.name, ph.avatar, pg.privat, pg.pass, pg.id_user, pu.group_access, pu.level, ps.privat_str, IF (pf.user, 1, 0) AS frends
          FROM gallery_foto AS ph 
          JOIN gallery AS pg ON ph.id_gallery = pg.id
          JOIN user AS pu ON pu.id = pg.id_user
          LEFT JOIN user_set AS ps ON ps.id_user = pg.id_user
          LEFT JOIN frends AS pf ON (pf.user = '$u' AND pf.frend = pg.id_user) OR (pf.user = pg.id_user AND pf.frend = '$u')
          WHERE ph.id = '$p'
    ", ARRAY_A);

    if ($u != $photo['id_user'] && $photo['avatar'] == 0) {
        if ($photo['privat_str'] == 0 || (($photo['privat_str'] == 2 || $photo['privat'] == 1) && !$photo['frends']) || $photo['privat'] == 2) {
            $p = 0;
        }
        if ($photo['privat'] == 3) {
            if ($pwd != $photo['pass']) {
                $p = 0;
            }
        }
    }
    if ($u && $p == 0) {
        $user = db::fetch("
              SELECT * FROM user
              WHERE id = '$u'
        ", ARRAY_A);
        if ($user['group_access'] > $photo['group_access']) {
            $p = $photo['id'];
        }
    }
    if (!$access) {
        $access = $cache->write($cacheLv, $p);
    }
}
if ($imageCache) {
    $file = $imageCache;
} else {
    $file = PATH_UPLOADS . '/gallery/foto/' . $p . '.jpg';
}
$image = $foo = new upload($file);
header('Cache-control: public');
header('Expires: ' . gmdate("D, d M Y H:i:s", time() + 60 * 60 * 24 * 7) . ' GMT');
header('Content-type: ' . $image->file_src_mime);
if ($imageCache) {
    die($image->Process());
}
if (!isset($photo)) {
    $photo = db::fetch("
          SELECT ph.effects
          FROM gallery_foto AS ph 
          WHERE ph.id = '$p'
    ", ARRAY_A);
}
if (isset($photo) && $photo['effects']) {
    $exp = unserialize($photo['effects']);
    if ($exp['rotate'] != 0) {
        $image->image_rotate = $exp['rotate'];
    }
    if ($exp['filter'] != 'none') {
        if ($exp['filter'] == 'greyscale')
            $image->image_greyscale = true;
        if ($exp['filter'] == 'negative')
            $image->image_negative = true;
        if ($exp['filter'] == 'pixelate')
            $image->image_pixelate = 4;
    }
    if ($exp['tint'] != 'none') {
        $image->image_tint_color = $exp['tint'];
    }
}
if (isset($_GET['crop'])) {
    $image->image_resize = true;
    if (preg_match('/^([0-9]{1,3}x[0-9]{1,3})$/i', $_GET['crop'])) {
        $prop = explode('x', $_GET['crop']);
        if ($prop[0] > 0 && $image->image_src_x > $prop[0])
            $image->image_x = (int)$prop[0];
        if ($prop[1] > 0 && $image->image_src_y > $prop[1])
            $image->image_y = (int)$prop[1];
        $image->image_ratio_crop = true;
    } elseif (preg_match('/^([0-9]{1,3})$/i', $_GET['crop'])) {
        if ($image->image_src_x > $_GET['crop']) {
            $image->image_x = $_GET['crop'];
            $image->image_ratio_y = true;
        }
    }
} elseif (isset($w) && $w && $image->image_src_x > $w) {
    $image->image_resize = true;
    $image->image_x = $w;
    if ($z == 50) {
        $image->image_y = $w;
        $image->image_ratio_crop = true;
    } else {
        $image->image_ratio_y = true;
    }
}
$output = $image->Process();
if (!$imageCache) {
    $cache->write($cacheId, $output);
}
die($output);