<?php

include('includes/hlava.php');
title('Klub priateľov fotografie', '');

if ($link) {
  if ($db_selected) {
  
include('includes/vedla.php');
odkaz('');
include('includes/menu.php');
menu('','');
?>        
          
        <div class="obsah">   
          
<?php

		  $zobraz = false;
		  
  		if (isset($_POST['submit'])) {
  			if (isset($_FILES['foto']) && $_FILES['foto'] != '') {
        $nazov = 'xxx'; 
      	if (isset($_FILES['foto'])) :
      	  if ($_FILES['foto']['error'] == UPLOAD_ERR_OK) {
      			if (is_uploaded_file($_FILES['foto']['tmp_name'])) {
      				$nazov = $_FILES['foto']['name'];
      			}
      		} elseif ($_FILES['foto']['error'] != UPLOAD_ERR_NO_FILE) { 
               echo '<p style="color: #FF0000;">Foto NEbolo prenesené, nastal problém pri uploadovaní súboru. Chyba č. ' . $_FILES['obr']['error'] . '</p>';
      	   	  $zobraz = true;
          }
      	endif;

        if ($nazov !== 'xxx') {
              $sql = "INSERT INTO kpf_forum_photos (fid,meno,nazov,pridane) VALUES (NULL, '". $_SESSION["priezvisko"] . "', '".$nazov."', NOW());";
           		$result = mysql_query($sql, $link);
           		if ($result) {
                echo '<p><strong>Fotografia bola odoslaná.</strong></p>';
                      
          		} else {
        				echo '<p style="color: #FF0000;">Fotografia NEbola odoslaná, nastal problém pri zápise do databázy - ' . mysql_errno($link) . ': ' . mysql_error($link) . '</p>';                
              }
              $cislo = mysql_insert_id();
      			  $nazov = $_SESSION['aid'] . '-' . $cislo . '.jpg';    			  
      			  
      				move_uploaded_file($_FILES['foto']['tmp_name'], 'photos/forum/' . $nazov);
      				$sql = "UPDATE kpf_forum_photos SET nazov='".$nazov."' WHERE fid=$cislo;";
           		$result = mysql_query($sql, $link);	
           		if ($result) {
                header("Location: photo_do_fora.php?id=".$cislo);                                      
              } else {
                   echo '<p style="color: #FF0000;">Fotografia sa už vo fotogalérii nachádza.</p>';                
              }
        }
      }
      } else {
        if (($_SESSION['aid'] == 0) && ($poslane == true)){
        $zobraz = false;
        }
        else {
        $zobraz = true;
        }
      }
      
      if ($zobraz) : 
          $poslane = false;?>    
          <h2>Vložiť <em>fotku</em> "do <em>fóra"</em></h2>
                  
                  
              <p>Na obmedzeniach sa ešte dohodneme.</p>
              <form class="login" action="<?php echo $_SERVER['REQUEST_URI'] ?>" method="post" enctype="multipart/form-data">
                <p class="form">
                <em class="bold">* Súbor</em> <input type="file" name="foto" size="35"/><input type="hidden" name="MAX_FILE_SIZE" value="1" />
                  <em class="bold"></em></p>
                <input class="ok" name="submit" type="submit" id="submit" value="vložiť"/>
              </form>
              
              
      <?php
        else :
        if ($_SESSION['aid'] == 0){
      ?>
      <h2>Treba <em>sa</em> prihlásiť</h2>
      
      <?php
      }
      endif; // kontrola SESSION        
        		} // end vyber db
        		mysql_close($link);
        	} // end spojenie s db

include('includes/pata.php');
      ?>
