View file photos/index.php

File size: 7.8Kb
<?php
// by Mike O. (mides), coolcms.org

$title = 'Фотогалерея';
require_once '../includes/sys.php';
require_once '../includes/header.php';

$config['onpage'] = 4;
$type = 'photos';

switch ($act) {
	default:
        echo '<div class="title">Фотогалерея';
		if ($u['id']) {
			echo ' (<a href="index.php?act=photo_add">+фото</a>)';
		}
		echo '</div><div class="body">';
			
		if ($u['id']) {
			echo 'Мои: <a href="index.php?act=my">фото</a>, <a href="index.php?act=comm_by&amp;id='.$u['id'].'">комментарии</a><br />';
		}
		echo '<a href="index.php?act=comm_new">Новые комментарии</a></div>'; //<a href="index.php?act=">Все альбомы</a>, 

		$total = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `photos`"), 0);
		if ($total > 0) {
			$pages = ceil($total / $config['onpage']);
			if ($page > $pages or $page == 0) {
				$page = 1;
			}
			$begin = ($page - 1) * $config['onpage'];
			
			$gallery_r = mysql_query("SELECT * FROM `photos` ORDER BY `time` DESC LIMIT $begin, $config[onpage]");
			while ($gallery = mysql_fetch_assoc($gallery_r)) {
				$comm = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `photos_comm` WHERE `id_photos` = '$gallery[id]'"), 0);
				echo '<div class="row"><a href="index.php?act=view&amp;id='.$gallery['id'].'"><img src="../images/gallery.gif"> <b>'.$gallery['name'].'</b> ('.rating($gallery['rating']).')<br /><img src="resize.php?dir=photos/files&amp;name='.$gallery['file'].'" alt="'.$gallery['name'].'" /><br />Добавлено: '.username($gallery['id_user'], 2).' ('.ccdate($gallery['time'], 0).')<br />Комментарии ('.$comm.')</a></div>';
			}
		} else {
			echo '<div class="body"><hr>Не загружено ни одного фото<hr></div>';
		}
		echo '<div class="body">';
		navig($page, '?', $pages);
		echo '<a href="index.php?act=top">Топ фото</a> (всего '.$total.')</div>';
	break;

	case 'photo_add':
		if (!$u['id']) redirect('../other/login.php');
		if ($ok) {
			if ($_FILES['file']['name'] and $_POST['name']) {
				if (ext($_FILES['file']['name']) == 'jpg' or ext($_FILES['file']['name']) == 'gif') {
					$file = check(basename($_FILES['file']['name']));
					$file_loaded = $u['id'].'_'.$file;
					if (!mysql_num_rows(mysql_query("SELECT `id` FROM `photos` WHERE `file` = '$file_loaded'"))) {
						$name = check($_POST['name']);
						$desc = check($_POST['desc']);
						copy($_FILES['file']['tmp_name'], 'files/'.$file_loaded);
						mysql_query("INSERT INTO `photos` SET `id_user` = '$u[id]', `name` = '$name', `desc` = '$desc', `time` = '".TIME."', `file` = '$file_loaded'");
						$last_id = mysql_insert_id();
						redirect('?act=view&id='.$last_id);
					} else {
						error('Файл уже существует.');
						nav('?act=photo_add');
					}
				} else {
					error('Неверный формат файла. Можно только .jpg и .gif');
					nav('?act=photo_add');
				}
			} else {
				error('Вы не указали название или не прикрепили фото.');
				nav('?act=photo_add');
			}
		} else {
			tp('<a href="?">Фотогалерея</a>&raquo; Добавить фото');
			echo '<div class="body">';
			echo '<form name="form" action="?act=photo_add&amp;ok=1" method="post" enctype="multipart/form-data">
			Прикрепить фото*:<br /><input name="file" type="file" size="file" /><br />
			Название(max50)*:<br /><input name="name" type="text" maxlength="50" /><br />
			Описание:<br /> '.bbpanel('form', 'desc').'<textarea name="desc" cols="" rows="4"></textarea><br />
			<input name="submit" type="submit" value="Ok" />
			</form>
			<br />
			<a href="?">К фотогалереи</a>
			</div>
			';
		}
	break;						
	
	case 'view':
		$gallery = mysql_fetch_assoc(mysql_query("SELECT * FROM `photos` WHERE `id` = '$id'"));
		if ($gallery['id']) {
			tp('<a href="?">Фотогалерея</a>&raquo; '.$gallery['name']);
			echo '<div class="body">
			<a href="files/'.$gallery['file'].'"><img src="files/'.$gallery['file'].'" width="150" alt="'.$gallery['name'].'" /></a><br />';

			if ($u['id']) {
				if ($u['avatar'] != $gallery['id']) {
					echo '<a href="?act=avatar&amp;id='.$gallery['id'].'">Использовать как аватар</a><br />';
				}
			}
			
			if ($gallery['desc']) echo bb($gallery['desc']).'<br />';
			
			echo 'Рейтинг: <b>'.rating($gallery['rating']).'</b> <a href="index.php?act=rate&amp;id='.$gallery['id'].'"><image src="../images/arrow_down.png" alt="arrow_down" /></a><a href="index.php?act=rate&amp;id='.$gallery['id'].'&amp;ok=1""><image src="../images/arrow_up.png" alt="arrow_up" /></a><br /><br />';
			echo 'Добавлено: <a href="../other/profile.php?id='.$gallery['id_user'].'">'.username($gallery['id_user'], 2).'</a> ('.date('d.m.y, H:i', $gallery['time']).')<br />';
			
			$comm = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `photos_comm` WHERE `id_photos` = '$gallery[id]'"), 0);
			echo '<a href="?act=comm&amp;id='.$gallery['id'].'">Комментарии</a> ('.$comm.')<br />';
			
			echo '<br />Скопировать адрес:<br /><input name="file" type="text" value="'.HTTPHOME.'/photos/?act=view&amp;id='.$gallery['id'].'" /><br />';
			echo 'BB-код для форума:<br /><input name="bb_code" type="text" value="[url='.HTTPHOME.'/photos/?act=view&amp;id='.$gallery['id'].']'.$gallery['name'].'[/url]" /><br />';
			
			//if ($u['access'] > 1) echo '<br />- <a href="?act=photo_edit&amp;id='.$gallery['id'].'">Редактировать</a><br />';
			if (access(3)) echo '- <a href="?act=photo_del&amp;id='.$gallery['id'].'">Удалить</a><br />';
			echo '<br /><a href="?">В фотогалерею</a></div>';
		} else {
			redirect('?');
		}
	break;
	
	case 'avatar':
		if ($u['id']) {
			$photo_r = mysql_query("SELECT * FROM `photos` WHERE `id` = '$id'");
			$photo = mysql_fetch_assoc($photo_r);
			if ($photo['id']) {
				mysql_query("UPDATE `users` SET `avatar` = '$photo[id]', `avatar_photo` = 1 WHERE `id` = '$u[id]'");
				redirect('../other/profile.php?id='.$u['id']);
			} else {
				redirect('?');
			}
		} else {
			redirect('?');
		}
	break;
			
	case 'photo_del':
		if (access(3)) {
			$photo = mysql_fetch_assoc(mysql_query("SELECT * FROM `photos` WHERE `id` = '$id'"));
			if ($photo['id']) {
				if ($ok) {
					unlink('files/'.$photo['file']);
					mysql_query("DELETE FROM `photos` WHERE `id` = '$photo[id]'");
					redirect('?');
				} else {
					tp('Подтвердите, пожалуйста');
					echo '<div class="body">';
					echo 'Вы действительно хотите удалить фото "<b>'.$photo['name'].'</b>"?<br />
					<form name="form" action="?act=photo_del&amp;id='.$id.'&amp;ok=1" method="post">
					<input name="submit" type="submit" value="Да" />
					</form>
					<br />
					<a href="?act=view&amp;id='.$photo['id'].'">К фото</a>
					</div>';
				}
			} else {
				redirect('?');
			}
		} else {
			redirect('?');
		}
	break;

	case 'rate':
		require_once '../includes/rate.php';
	break;

	case 'comm':
		require_once '../includes/comm.php';
	break;
	
	case 'comm_add':
		require_once '../includes/comm_add.php';
	break;
	
	case 'comm_reply':
		require_once '../includes/comm_reply.php';
	break;
	
	case 'comm_edit':
		require_once '../includes/comm_edit.php';
	break;
	
	case 'comm_del':
		require_once '../includes/comm_del.php';
	break;	

	case 'comm_by':
		require_once '../includes/comm_by.php';
	break;
	
	case 'comm_new':
		require_once '../includes/comm_new.php';
	break;
	
	case 'top':
		require_once '../includes/top.php';
	break;

	case 'my':
		require_once '../includes/my.php';
	break;

}

require_once '../includes/tail.php';
?>