<?php
  ///////////////////////////////////////////////////
  //2007 (C)
  ///////////////////////////////////////////////////

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("../lang/".$lang."/".$lang.".php")) include "../lang/".$lang."/".$lang.".php";
     else include "../lang/ru/ru.php";

  $title=$titlepage = $TEXT['pho-title2'];
  $helppage = "";

  require_once "../config.php";
  require_once "../util/top_layer1.php";
  include "util.php";

  if($result2['master'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['manager'] == 1) puterror2($TEXT['error-loginform4']);

  if(empty($_POST['name'])) puterror2($TEXT['error-pho8']);
  if(strlen($_POST['name'])>300) puterror2($TEXT['error-pho9']);
  if(empty($_POST['position'])) puterror2($TEXT['error-pho10']);
  if(strlen($_POST['position'])>11) puterror2($TEXT['error-pho11']);
  if(strlen($_POST['descrip'])>800) puterror2($TEXT['error-pho12']);
  if(strlen($_POST['mer_id'])>11) puterror2($TEXT['error-pho13']);
  if(strlen($_POST['photo_obr'])>1) puterror2($TEXT['error-pho27']);
  $_POST['name'] = rep($_POST['name']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['descrip'] = rep($_POST['descrip']);
  $_POST['mer_id'] = rep($_POST['mer_id']);
  $_POST['photo_obr'] = rep($_POST['photo_obr']);

  $mer_id = $_POST['mer_id'];

  if($_FILES['image']['size'] > 1024*20*1024) puterror2($TEXT['error-pic3']);
  if($_POST['visible'] == "on") $visible = "show";
  else $visible = "hide";

  $sm = "small";
  $bg = "big";

  if (!empty($_FILES['image']['tmp_name']))
  {
    $ext = strrchr($_FILES['image']['name'], ".");
    $image = "../photo/$bg".date("YmdHis",time())."$ext";
    $smallimage = "../photo/$sm".date("YmdHis",time())."_s$ext";
    if (copy($_FILES['image']['tmp_name'], "../".$image) && copy($_FILES['image']['tmp_name'], "../".$smallimage))
    {
      unlink($_FILES['image']['tmp_name']);
      chmod("../".$image, 0644);
    }
  } else puterror2($TEXT['error-pho27']);

            if($_POST['photo_obr'] == '1') {
		$image = $image;
		if(!empty($_FILES['image2']['tmp_name']))
                  {
                   $ext = strrchr($_FILES['image2']['name'], ".");
                   $smallimage2 = "../photo/$sm".date("YmdHis",time())."_s$ext";
                  if(copy($_FILES['image2']['tmp_name'], "../".$smallimage2))
                 {
                 unlink("../".$smallimage);
                 unlink($_FILES['image2']['tmp_name']);
                 chmod("../".$smallimage2, 0644);
                 }
                 } else puterror2($TEXT['error-pho27']);
		$smallimage = $smallimage2;
            }
            if($_POST['photo_obr'] == '2') {
                 if(!resizeimg_simple($image, $smallimage, 130, 185))
                {
                  puterror2($TEXT['error-gdlib']);
                }
            }
            if($_POST['photo_obr'] == '3') {
                 if(!resizeimg_simple($image, $smallimage, 185, 130))
                {
                  puterror2($TEXT['error-gdlib']);
                }
            }
             if($_POST['photo_obr'] == '4') {
                 if(!resizeimg($image, $smallimage, 130, 185))
                {
                  puterror2($TEXT['error-gdlib']);
                }
            }
             if($_POST['photo_obr'] == '5') {
                 if(!resizeimg($image, $smallimage, 185, 130))
                {
                  puterror2($TEXT['error-gdlib']);
                }
            }
             if($_POST['photo_obr'] == '6') {
                 $maskfile = "../img/vert.png";
                 if(!resizeimg_round($maskfile, $image, $smallimage, 130, 185))
                {
                  puterror2($TEXT['error-gdlib']);
                }
            }
             if($_POST['photo_obr'] == '7') {
                 $maskfile = "../img/horiz.png";
                 if(!resizeimg_round($maskfile, $image, $smallimage, 185, 130))
                {
                  puterror2($TEXT['error-gdlib']);
                }
            }
             if($_POST['photo_obr'] == '8') {
                 if(!resizeimg($image, $smallimage, 185, 130))
                {
                  puterror2($TEXT['error-gdlib']);
                }
            }
             if($_POST['photo_obr'] == '9') {
                 if(!resizeimg($image, $smallimage, 185, 130))
                {
                  puterror2($TEXT['error-gdlib']);
                }
	    }

  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-pho2'];
  $what = $image.$TEXT['log-pho3'].$smallimage;
  $who = $result2['fullname'];
  //*************************************
  $query = "INSERT INTO `tbl_photo` ( `photo_id` , `name` , `small` , `big` , `description` , `position` , `visible` , `mer_id` ) VALUES ( NULL, '".$_POST['name']."', '".$smallimage."', '".$image."', '".$_POST['descrip']."', '".$_POST['position']."', '".$visible."', '".$_POST['mer_id']."')";
  if(mysql_query($query))
  {
    $query2 = "INSERT INTO `tbl_logs` ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=photoindex.php?mer_id=$mer_id' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
  } else puterror2($TEXT['error-tbl14']);
  require_once "../util/bottom.php";
?>