/*
Photo gallery script for Intellect Board 2 Project
(C) 2004-2005, XXXX Pro, United Open Project
Visit us online: http://intboard.ru
*/
if (!$IBOARD) die("Hack attempt!");
function photos_view() {
if (!$GLOBALS['opt_photos_line']) $GLOBALS['opt_photos_line']=5;
if (!$GLOBALS['opt_photo_thumb_y']) $GLOBALS['opt_photo_thumb_y']=100;
// if (!$GLOBALS['opt_photo_size_x']) $GLOBALS['opt_photo_size_x']=720;
if ($GLOBALS['topic']) view_topic();
else view_list();
}
function view_list() {
if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lread']) error(MSG_e_f_norightsread);
$forum=$GLOBALS['forum'];
global $link;
$order = getvar("o");
$direct=getvar("desc");
$start=getvar("st");
if (!$start) $start="0";
if (!$perpage) $perpage=$GLOBALS['inuser']['u_aperpage'];
$tcount=$GLOBALS['inforum']['f__tcount'];
$pages = build_pages($tcount,$start,$perpage,"index.php?f=$forum&o=$order&desc=$desc");
if (!$order) $order="t__startpostid DESC";
if ($start!="all") $limit = " LIMIT $start,$perpage";
$sql = "SELECT t.*, ph.*, p2.p_uname AS u__name, p2.p_uid AS u_id, p2.p__time AS posttime, t__ratingsum/NULLIF(t__ratingcount,0) AS trating, tv.tid AS visited, t__pcount AS pcount, p1.p__time AS lastpost ".
"FROM ".$GLOBALS['DBprefix']."Photo ph, ".$GLOBALS['DBprefix']."Post p1, ". $GLOBALS['DBprefix']."Post p2, ".$GLOBALS['DBprefix']."Topic t ".
"LEFT JOIN ".$GLOBALS['DBprefix']."TopicView tv ON (tv.tid=t.t_id AND tv.uid=".$GLOBALS['inuserid'].") ".
"WHERE t.t_fid=$forum AND t.t_id=ph.ph_tid AND t.t__lastpostid=p1.p_id AND t.t__startpostid=p2.p_id ".
"ORDER BY t__sticky DESC, $order $direct $limit";
$res = db_query($sql,$link);
$counter=0;
photo_list_start($pages);
while ($phdata=db_fetch_array($res)) {
if ($counter % $GLOBALS['opt_photos_line'] == 0) photo_line_start();
photo_list_entry($phdata);
$counter++;
if ($counter % $GLOBALS['opt_photos_line'] == 0) photo_line_end();
}
if ($counter % $GLOBALS['opt_photos_line']!=0) {
for ($i=$GLOBALS['opt_photos_line']; $i>$counter % $GLOBALS['opt_photos_line']; $i--) photo_list_empty();
photo_line_end();
}
photo_list_end();
}
function view_topic() {
if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lread']) error(MSG_e_f_norightsread);
$forum=$GLOBALS['forum'];
$topic=$GLOBALS['topic'];
global $link;
$rated=common_topic_view($topic);
$sql = "SELECT MIN(t_id), MAX(t_id) FROM ".$GLOBALS['DBprefix']."Topic WHERE t_fid=".$GLOBALS['forum'];
$res = db_query($sql,$link);
list($min_tid,$max_tid)=db_fetch_row($res);
db_free_result($res);
$sql = "SELECT ph.* FROM ".$GLOBALS['DBprefix']."Photo ph WHERE ph_tid=$topic";
$res = db_query($sql,$link);
$phdata=db_fetch_array($res);
db_free_result($res);
$tdata=$GLOBALS['intopic'];
$sql = "SELECT p.*, u.u__name, u.u_id FROM ".$GLOBALS['DBprefix']."Post p, ".$GLOBALS['DBprefix']."User u ".
"WHERE p.p_tid=$topic AND u.u_id=p.p_uid AND p.p__premoderate=0 ORDER BY p_id";
$res = db_query($sql,$link);
$pdata=db_fetch_array($res);
photo_display($tdata,$phdata,$pdata,$rated,$min_tid,$max_tid);
$numrows=db_num_rows($res);
if ($numrows>1) {
photo_discuss_start();
while ($phentry=db_fetch_array($res)) {
photo_discuss_entry($phentry);
}
photo_discuss_end();
}
$trash['p__bcode']=$GLOBALS['inforum']['f_bcode'];
$trash['p__smiles']=$GLOBALS['inforum']['f_smiles'];
if ($GLOBALS['inuserlevel']>=$GLOBALS['inforum']['f_lpost']) photo_discuss_form($trash,MSG_ph_addcoment,"do_post");
}
function do_post() {
global $link;
$tid=$GLOBALS['topic'];
process_post($tid);
if (!$GLOBALS['inforum']['f_premoderate'] || $GLOBALS['inuserlevel']>=$GLOBALS['inforum']['f_lmoderate']) {
topic_message(MSG_ph_commentadded,1);
}
else message(MSG_p_premoderated,1);
}
function add_photo() {
$pdata['p__bcode']=$GLOBALS['inforum']['f_bcode'];
$pdata['p__smiles']=$GLOBALS['inforum']['f_smiles'];
photo_edit_form($tdata,$pdata,"do_topic",MSG_ph_adding);
}
function do_topic() {
if ($GLOBALS['inuserlevel']<$inforum['f_ltopic']) error(MSG_e_t_norights);
if ($GLOBALS['inforum']['f_status']!=0) error(MSG_e_f_closed);
$forum=$GLOBALS['forum'];
global $link;
if (!getvar("p_text")) $_POST['p_text']=str_repeat(" ",$GLOBALS['opt_minpost']+1);
if (!getvar("t_title")) {
$sql = "SELECT COUNT(*)+1 FROM ".$GLOBALS['DBprefix']."Topic WHERE t_fid=".$GLOBALS['forum'];
$res = db_query($sql,$link);
list($count)=db_fetch_row($res);
db_free_result($res);
$_POST['t_title']=MSG_photo." ".$count;
}
if (!$GLOBALS['opt_photos_line']) $GLOBALS['opt_photos_line']=5;
if (!$GLOBALS['opt_photo_thumb_y']) $GLOBALS['opt_photo_thumb_y']=100;
// if (!$GLOBALS['opt_photo_size_x']) $GLOBALS['opt_photo_size_x']=720;
if (!$GLOBALS['opt_thumb_qlty']) $GLOBALS['opt_thumb_qlty']=70;
if (!$GLOBALS['opt_photo_qlty']) $GLOBALS['opt_photo_qlty']=70;
if (!is_uploaded_file($_FILES['photo']['tmp_name'])) error(MSG_e_ph_nophoto);
$fh=fopen($_FILES['photo']['tmp_name'],"rb");
$buffer=fread($fh,$_FILES['photo']['size']);
fclose($fh);
$fullimg=imagecreatefromstring($buffer);
if (!$fullimg) error(MSG_e_ph_badfile);
$sizey=imagesy($fullimg);
$sizex=imagesx($fullimg);
$coeff=$sizex/$sizey;
$trash=rand();
$key=substr(md5($trash),0,8);
$sqldata = build_sql("t_");
$sqldata.= check_topic_params();
$sql = "INSERT INTO ".$GLOBALS['DBprefix']."Topic SET t_fid=$forum, $sqldata";
$res = db_query($sql,$link);
$tid = db_insert_id($res);
$sql = "INSERT INTO ".$GLOBALS['DBprefix']."Photo SET ph_tid=".$tid.", ph_key=\"$key\"";
$res = db_query($sql,$link);
$thumb=imagecreatetruecolor(floor($GLOBALS['opt_photo_thumb_y']*$coeff),$GLOBALS['opt_photo_thumb_y']);
if ($GLOBALS['opt_GD2']) imagecopyresampled($thumb,$fullimg,0,0,0,0,$GLOBALS['opt_photo_thumb_y']*$coeff,$GLOBALS['opt_photo_thumb_y'],$sizex,$sizey);
else imagecopyresized($thumb,$fullimg,0,0,0,0,floor($GLOBALS['opt_photo_thumb_y']*$coeff),$GLOBALS['opt_photo_thumb_y'],$sizex,$sizey);
$tmpname=$GLOBALS['opt_dir']."/photos/previews/$tid.jpg";
imagejpeg($thumb,$tmpname,$GLOBALS['opt_thumb_qlty']);
if ($GLOBALS['opt_photo_size_x'] && $sizex>$GLOBALS['opt_photo_size_x']) {
$photo=imagecreatetruecolor($GLOBALS['opt_photo_size_x'],floor($GLOBALS['opt_photo_size_x']/$coeff));
if ($GLOBALS['opt_GD2']) imagecopyresampled($photo,$fullimg,0,0,0,0,$GLOBALS['opt_photo_size_x'],floor($GLOBALS['opt_photo_size_x']/$coeff),$sizex,$sizey);
else imagecopyresized($photo,$fullimg,0,0,0,0,$GLOBALS['opt_photo_size_x'],$GLOBALS['opt_photo_size_x']/$coeff,$sizex,$sizey);
$tmpname=$GLOBALS['opt_dir']."/photos/$tid.jpg";
imagejpeg($photo,$tmpname,$GLOBALS['opt_photo_qlty']);
}
else move_uploaded_file($_FILES['photo']['tmp_name'],$GLOBALS['opt_dir']."/photos/$tid.jpg");
imagedestroy($fullimg);
if ($GLOBALS['opt_photo_size_x'] && $sizex>$GLOBALS['opt_photo_size_x']) imagedestroy($photo);
imagedestroy($thumb);
$pid=process_post($tid);
topic_increment($forum,$tid,$pid);
if (!$GLOBALS['inforum']['f_premoderate'] || $GLOBALS['inuserlevel']>=$GLOBALS['inforum']['f_lmoderate']) {
$GLOBALS['topic']=$tid;
topic_message(MSG_ph_added,1);
}
else {
$GLOBALS['refpage']="index.php?f=".$GLOBALS['forum'];
message(MSG_t_premoderated,1);
}
}
function edit_photo() {
if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate'] && $GLOBALS['intopic']['t_author']!=$GLOBALS['inuserid'] && $GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_ledit']) error(MSG_e_t_norights);
global $link;
$tid=$GLOBALS['topic'];
$sql = "SELECT * FROM ".$GLOBALS['DBprefix']."Topic WHERE t_id=$tid";
$res = db_query($sql,$link);
$tdata = db_fetch_array($res);
db_free_result($res);
$sql = "SELECT * FROM ".$GLOBALS['DBprefix']."Post WHERE p_tid=$tid";
$res = db_query($sql,$link);
$pdata=db_fetch_array($res);
db_free_result($res);
// $sql = "DELETE FROM ".$GLOBALS['DBprefix']."TopicView WHERE tid=$tid";
// $res = db_query($sql,$link);
photo_edit_form($tdata,$pdata,"do_edit",MSG_a_edit);
}
function do_edit() {
if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate'] && $GLOBALS['intopic']['t_author']!=$GLOBALS['inuserid'] && $GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_ledit']) error(MSG_e_t_norights);
$forum=$GLOBALS['forum'];
global $link;
$sqldata = build_sql("t_");
$sql = "UPDATE ".$GLOBALS['DBprefix']."Topic SET $sqldata WHERE t_id=".$GLOBALS['topic'];
$res = db_query($sql,$link);
if (!$GLOBALS['opt_photos_line']) $GLOBALS['opt_photos_line']=5;
if (!$GLOBALS['opt_photo_thumb_y']) $GLOBALS['opt_photo_thumb_y']=100;
if (!$GLOBALS['opt_thumb_qlty']) $GLOBALS['opt_thumb_qlty']=70;
if (!$GLOBALS['opt_photo_qlty']) $GLOBALS['opt_photo_qlty']=70;
$tid=$GLOBALS['topic'];
if (is_uploaded_file($_FILES['photo']['tmp_name'])) //фотку заменили
{
$fh=fopen($_FILES['photo']['tmp_name'],"rb");
$buffer=fread($fh,$_FILES['photo']['size']);
fclose($fh);
$fullimg=imagecreatefromstring($buffer);
if (!$fullimg) error(MSG_e_ph_badfile);
$sizey=imagesy($fullimg);
$sizex=imagesx($fullimg);
$coeff=$sizex/$sizey;
$thumb=imagecreatetruecolor(floor($GLOBALS['opt_photo_thumb_y']*$coeff),$GLOBALS['opt_photo_thumb_y']);
if ($GLOBALS['opt_GD2']) imagecopyresampled($thumb,$fullimg,0,0,0,0,$GLOBALS['opt_photo_thumb_y']*$coeff,$GLOBALS['opt_photo_thumb_y'],$sizex,$sizey);
else imagecopyresized($thumb,$fullimg,0,0,0,0,floor($GLOBALS['opt_photo_thumb_y']*$coeff),$GLOBALS['opt_photo_thumb_y'],$sizex,$sizey);
$tmpname=$GLOBALS['opt_dir']."/photos/previews/$tid.jpg";
if (file_exists($tmpname)) unlink($tmpname);
imagejpeg($thumb,$tmpname,$GLOBALS['opt_thumb_qlty']);
if ($GLOBALS['opt_photo_size_x'] && $sizex>$GLOBALS['opt_photo_size_x']) {
$photo=imagecreatetruecolor($GLOBALS['opt_photo_size_x'],floor($GLOBALS['opt_photo_size_x']/$coeff));
if ($GLOBALS['opt_GD2']) imagecopyresampled($photo,$fullimg,0,0,0,0,$GLOBALS['opt_photo_size_x'],floor($GLOBALS['opt_photo_size_x']/$coeff),$sizex,$sizey);
else imagecopyresized($photo,$fullimg,0,0,0,0,$GLOBALS['opt_photo_size_x'],$GLOBALS['opt_photo_size_x']/$coeff,$sizex,$sizey);
$tmpname=$GLOBALS['opt_dir']."/photos/$tid.jpg";
if (file_exists($tmpname)) unlink($tmpname);
imagejpeg($photo,$tmpname,$GLOBALS['opt_photo_qlty']);
}
else move_uploaded_file($_FILES['photo']['tmp_name'],$GLOBALS['opt_dir']."/photos/$tid.jpg");
imagedestroy($fullimg);
if ($GLOBALS['opt_photo_size_x'] && $sizex>$GLOBALS['opt_photo_size_x']) imagedestroy($photo);
imagedestroy($thumb);
}
$sql = "SELECT MIN(p_id) FROM ".$GLOBALS['DBprefix']."Post WHERE p_tid=".$GLOBALS['topic'];
$res = db_query($sql,$link);
list($pid)=db_fetch_row($res);
$sqldata = build_sql("p_");
$sqldata.= check_post_params();
$sql = "UPDATE ".$GLOBALS['DBprefix']."Post SET $sqldata WHERE p_id=\"$pid\" AND p_tid=".$GLOBALS['topic'];
$res = db_query($sql,$link);
topic_message(MSG_a_saved,1);
}
function do_delete_photo() {
if ($inuserlevel<$inforum['f_lmoderate']) error(MSG_e_t_norights);
$tid=$GLOBALS['topic'];
global $link;
$sql = "DELETE FROM ".$GLOBALS['DBprefix']."Photo WHERE ph_tid=$tid";
$res = db_query($sql,$link);
delete_topic($tid);
unlink($GLOBALS['opt_dir']."/photos/$tid.jpg");
unlink($GLOBALS['opt_dir']."/photos/previews/$tid.jpg");
forum_resync($GLOBALS['forum']);
$GLOBALS['refpage']="index.php?f=".$GLOBALS['forum'];
message(MSG_t_deleted);
}
function do_print() {
global $link;
$tid=$GLOBALS['topic'];
$sql = "SELECT * FROM ".$GLOBALS['DBprefix']."Topic WHERE t_id=$tid";
$res = db_query($sql,$link);
$tdata = db_fetch_array($res);
db_free_result($res);
$sql = "SELECT * FROM ".$GLOBALS['DBprefix']."Post WHERE p_tid=$tid LIMIT 1";
$res = db_query($sql,$link);
$pdata=db_fetch_array($res);
db_free_result($res);
$sql = "SELECT * FROM ".$GLOBALS['DBprefix']."Photo WHERE ph_tid=$tid";
$res = db_query($sql,$link);
$phdata = db_fetch_array($res);
photo_print_form($tdata,$phdata,$pdata);
}
function delete_confirm() {
$params['t']=$GLOBALS['topic'];
confirm("photos","do_delete_photo",$params,MSG_a_deleteconfirm." ".$GLOBALS['intopic']['t_title'],"index.php?t=".$GLOBALS['topic']);
}
function edit_comment() {
edit_post("photo_discuss_form",MSG_a_editcoment);
}
function photos_locations($locations) {
push_parents($locations,$GLOBALS['inforum']['f_parent']);
if ($GLOBALS['topic']) {
array_push($locations,"".$GLOBALS['inforum']['f_title']."");
if ($GLOBALS['action']=='photos_view') array_push($locations,$GLOBALS['intopic']['t_title']);
else array_push($locations,"".$GLOBALS['intopic']['t_title']."");
$GLOBALS['rss_link']="rss.php?t=".$GLOBALS['topic']."&count=".$GLOBALS['inuser']['u_mperpage'];
}
else {
array_push($locations,$GLOBALS['inforum']['f_title']);
$GLOBALS['rss_link']="rss.php?a=newtopic&f=".$GLOBALS['forum']."&count=".$GLOBALS['inuser']['u_aperpage'];
}
if ($GLOBALS['action']=="edit_photo") {
array_push($locations,MSG_ph_edit);
}
elseif ($GLOBALS['action']=="edit_comment") {
array_push($locations,MSG_ph_editcomment);
}
elseif ($GLOBALS['action']=="add_photo") {
array_push($locations,MSG_ph_adding);
}
return $locations;
}