<? /*

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'];
  $link=$GLOBALS['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/t__ratingcount AS trating, tv.tid AS visited, t__pcount AS pcount, p1.p__time AS lastpost ".
  "FROM ".$GLOBALS['DBprefix']."Topic t, ".
  $GLOBALS['DBprefix']."Photo ph, ".$GLOBALS['DBprefix']."Post p1, ". $GLOBALS['DBprefix']."Post p2 ".
  "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'];
  $link=$GLOBALS['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() {
  $link=$GLOBALS['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'];
  $link=$GLOBALS['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;

  $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=tempnam("temp/","ph");
  imagejpeg($thumb,$tmpname,$GLOBALS['opt_thumb_qlty']);
  $fh=fopen($tmpname,"r");
  $thumbdata=db_slashes(fread($fh,filesize($tmpname)));
  fclose($fh);
  unlink($tmpname);

  if ($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=tempnam("temp/","ph");
    imagejpeg($photo,$tmpname,$GLOBALS['opt_photo_qlty']);
    $fh=fopen($tmpname,"r");
    $photodata=db_slashes(fread($fh,filesize($tmpname)));
    fclose($fh);
    unlink($tmpname);
  }
  else $photodata=db_slashes($buffer);

  imagedestroy($fullimg);
  if ($GLOBALS['opt_photo_size_x']) imagedestroy($photo);
  imagedestroy($thumb);

  $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\", ph_thumb=\"$thumbdata\", ph_image=\"$photodata\"";
  $res = db_query($sql,$link);

  $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() {
  $link=$GLOBALS['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 ($inuserlevel<$inforum['f_lmoderate']) error(MSG_e_t_norights);
  $forum=$GLOBALS['forum'];
  $link=$GLOBALS['link'];

  $sqldata = build_sql("t_");
  $sql = "UPDATE ".$GLOBALS['DBprefix']."Topic SET $sqldata WHERE t_id=".$GLOBALS['topic'];
  $res = db_query($sql,$link);
  $tid = db_insert_id($res);

  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'])) {
    $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);
    $xsize=imagesx($fullimg);
    $ysize=imagesy($fullimg);
    $coeff=$xsize/$ysize;

    $thumb=imagecreate(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=tempnam("temp/","ph");
    imagejpeg($thumb,$tmpname,$GLOBALS['opt_thumb_qlty']);
    $fh=fopen($tmpname,"r");
    $thumbdata=db_slashes(fread($fh,filesize($tmpname)));
    fclose($fh);
    unlink($tmpname);

    if ($GLOBALS['opt_photo_size_x']) {
    $photo=imagecreate($GLOBALS['opt_photo_size_x'],floor($GLOBALS['opt_photo_size_x']/$coeff));
      if ($GLOBALS['opt_GD2']) imagecopyresampled($thumb,$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=tempnam("temp/","ph");
      imagejpeg($photo,$tmpname,$GLOBALS['opt_photo_qlty']);
      $fh=fopen($tmpname,"r");
      $photodata=db_slashes(fread($fh,filesize($tmpname)));
      fclose($fh);
      unlink($tmpname);
    }
    else $photodata=$buffer;

    imagedestroy($full);
    imagedestroy($photo);
    imagedestroy($thumb);

    $sql = "UPDATE ".$GLOBALS['DBprefix']."Photo SET ph_thumb=\"$thumbdata\", ph_image=\"$photodata\" WHERE ph_tid=".$GLOBALS['topic'];
    $res = db_query($sql,$link);
  }

  $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'];
  $link=$GLOBALS['link'];

  $sql = "DELETE FROM ".$GLOBALS['DBprefix']."Photo WHERE ph_tid=$tid";
  $res = db_query($sql,$link);

  delete_topic($tid);
  forum_resync($GLOBALS['forum']);
  $GLOBALS['refpage']="index.php?f=".$GLOBALS['forum'];
  message(MSG_t_deleted);
}

function do_print() {
  $link=$GLOBALS['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,"<a href=\"index.php?f=".$GLOBALS['forum']."\">".$GLOBALS['inforum']['f_title']."</a>");
    if ($GLOBALS['action']=='photos_view') array_push($locations,$GLOBALS['intopic']['t_title']);
    else array_push($locations,"<a href=\"index.php?t=".$GLOBALS['intopic']['t_title']."\">".$GLOBALS['intopic']['t_title']."</a>");
  }
  else {
    array_push($locations,$GLOBALS['inforum']['f_title']);
  }
  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;
}