<?

/************************************************************************/
/* XAlbum Version 0.9.8                                                 */
/* ==================================================================== */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

function listcategories($dir){
  global
          $gallerypath,
        $prefix
  ;

  $dossier = opendir($dir);
  $category = "";
  while ($file = readdir($dossier)) {
        if ($file != "." && $file != ".." && $file!="thumb"){
                if (is_dir($dir."/".$file)){
                        $galloc  = substr($dir."/".$file, strlen("$gallerypath/"));
                        $result = mysql_query("select gallid from $prefix"._4nalbum_categories." where galloc='$galloc'");
                        if ($result) {
                                list($gid) = mysql_fetch_array($result);
                                $category .= "$gid ";
                        }
                        $category .= listcategories("$dir/$file");
                  }
          }
  }
  return $category;
}

function upload_file() {
        global
                $name,
                $font,
                $galleryvar,
                $basepath,
                $user,
                $gallerypath,
                $prefix,
                $baseurl
        ;

        galleryHeader();
        OpenTable();
        $size = $maxSize/1000;
        $nav = navigationGall();
         echo         "
                  <table border=\"0\" width=\"100%\">
                    <tr>
                        <td width=\"100%\" align=\"center\">
                                $nav<br>
                                <font class=\"".$font['title']."\">"._GALUPLOADMEDIA."</font>
                        <br><br>
                        </td>
                    </tr>
                  <tr><td align=\"center\">";

        if (is_user($user) || !$galleryvar['anonpostpics']) {
        echo "        <form enctype=\"multipart/form-data\" action=\"".$baseurl."\" method=\"post\">
                  <input type=\"hidden\" name=\"do\" value=\"upload\">
                  <table border=\"0\" cellpadding=\"5\" cellspacing=\"0\"><tr>
                    <td align=\"left\" valign=\"top\" >
                        <font class=\"".$font['normal']."\">"._GALSELECTCATEGORY."</font><br>
                            <select name=\"Category\">";
                        $categories = listcategories("$gallerypath");
                        if (isset($categories) && $categories!="") {
                                $categories = explode(" ", trim($categories));
                                while(list($key, $val) = each($categories)) {
                                        if (is_user($user))
                                                $result = mysql_query("select gallid, gallname, parent from $prefix"._4nalbum_categories." where visible>0 and gallid=".$val);
                                        else
                                                $result = mysql_query("select gallid, gallname, parent from $prefix"._4nalbum_categories." where visible>1 and gallid=".$val);
                                        if (mysql_num_rows($result)>0) {
                                                list($gallid, $gallname, $parent) = mysql_fetch_row($result);
                                                $nbtabs = indent($gallid);
                                                $tab = "";
                                                for($k=0; $k<$nbtabs; $k++)
                                                        $tab .= "&nbsp;&nbsp;";
                                                echo "<option value=\"$gallid\">$tab$gallname</option>\n";
                                        }
                                }
                        }
                       echo "
                        </select><br><br>
                        <font class=\"".$font['normal']."\">"._GALMEDIANAME."<br></font>
                                <input type=\"text\" name=\"MediaName\" size=\"14\" value=\"\"><br><br>
                        <font class=\"".$font['normal']."\">"._GALFILENAME."</font><br>";
                        if ($galleryvar['limitSize'])
                                echo "<INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$maxSize\">";
                        echo "
                                <input type=\"file\" name=\"userfile\" size=\"14\"><br><br>
                        <font class=\"".$font['normal']."\">"._GALSUBMITTER."</font><br>";
                                if (is_user($user)) {
                                        $userdata=cookiedecode($user);
                                        echo "<input type=\"hidden\" name=\"Submitter\" value=\"".$userdata[1]."\">\n";
                                        echo "<b>".$userdata[1]."</b><br><br>";
                                }
                                else
                                        echo "<input type=\"text\" name=\"Submitter\" size=\"14\"><br><br>";
                     echo "
                     </td>
                     <td align=\"left\" valign=\"top\"><font class=\"".$font['normal']."\">"._GALDESCRIPTION."</font><br>
                        <textarea  name=\"Description\" rows=\"10\" cols=\"25\"></textarea>
                     </td></tr>
                <tr><td align=\"center\" colspan=\"2\"><input type=\"submit\" value=\"Upload\" name=\"add\"></td></tr>
                  </table>
                  </form>

                <table border=\"0\">
                <tr>
                        <td align=\"center\">
                        <font class=\"".$font['normal']."\">";
                        if ($galleryvar['limitSize'])
                                echo "".sprintf(_GALMAXSIZEPOST,$galleryvar['maxSize'])."  <br>";
                        echo _GALCLICKONCE."
                        </font>
                        </td>
                </tr>
                </table>";

        }
        else
                  echo "<p align=\"center\"><font class=\"".$font['normal']."\">"._GALREGISTER2POSTPICS."</font></p>";
        echo "
                         </td></tr>
                 </table>
                ";
        CloseTable();
        galleryFooter();
}


function Add($gid, $file, $submitter, $medianame, $description, $userfile, $userfile_name, $userfile_size) {
    global
            $user,
        $font,
        $galleryvar,
            $basepath,
        $temppath,
            $prefix
    ;

    $sql = "select img from $prefix"._4nalbum_pictures." where gid=$gid and img='$file'";
    //echo $sql;
    $result = mysql_query($sql);
    $numrows = mysql_num_rows($result);
    $navgall = navigationGall();

    if ($file==$galleryvar['GalleryPictureName'] || $numrows>0) {
        galleryHeader();
        OpenTable();
        print '<table border="0" width="100%" align="center" cellpadding="0">'
                 .'<tr><td>'.$navgall;

        echo "<br>";
        echo "<center><b>"._GALPICALREADYEXT."</b><br><br>"
            .""._GOBACK."";
        print '</td></tr></table>';
        CloseTable();
        galleryFooter();
    }
    else {
            if(is_user($user)) {
                $user2 = base64_decode($user);
                $cookie = explode(":", $user2);
                cookiedecode($user);
                $submitter = $cookie[1];
            }

        // Check if Media Name exist
            if (!isset($medianame) || $medianame=="") {
                galleryHeader();
                echo "<br>";
                OpenTable();
                print '<table border="0" width="100%" align="center" cellpadding="0">'
                 .'<tr><td>'.$navgall;
                echo "<br>";
                echo "<center><b>"._GALPICNONAME."</b><br><br>"
                    .""._GOBACK."";
                print '</td></tr></table>';
                CloseTable();
                galleryFooter();
            }
            else
        // Check if Description exist
            if (!isset($description) || $description=="") {
                galleryHeader();
                echo "<br>";
                OpenTable();
                print '<table border="0" width="100%" align="center" cellpadding="0">'
                 .'<tr><td>'.$navgall;
                echo "<center><b>"._GALPICNODESC."</b><br><br>"
                    .""._GOBACK."";
                print '</td></tr></table>';
                CloseTable();
                galleryFooter();
            }
            else
        // Check if Description exist
            if (!isset($submitter) || $submitter=="") {
                galleryHeader();
                echo "<br>";
                OpenTable();
                print '<table border="0" width="100%" align="center" cellpadding="0">'
                 .'<tr><td>'.$navgall;
                echo "<br>";
                echo "<center><b>"._GALPICNOSUBMITTER."</b><br><br>"
                    .""._GOBACK."";
                print '</td></tr></table>';
                CloseTable();
                galleryFooter();
            }
            else {
                $medianame = FixQuotes($medianame);
                $description = FixQuotes($description);
                $wdir = "/";
                $upload_return = UploadFile($temppath, $userfile, $userfile_name, $userfile_size);
                //echo "public uploadFile.php : $upload_return<br>";
                if ($upload_return=="OK") {
                        $ext = substr($file, (strrpos($file,'.') +  1));
                        list($type) = mysql_fetch_row(mysql_query("select filetype from $prefix"._4nalbum_media_types." where extension='$ext'"));
                        $wdir = "/";

                        switch($type) {
                                case 1 :
                                        $size = @getimagesize($temppath.$wdir.$file);
                                        break;
                                case 3 :
                                        $size[0] = 320;
                                        $size[1] = 240;
                                        break;
                                default:
                                        $size[0] = 0;
                                        $size[1] = 0;
                                        break;
                        }
                        $file = traite_nom_fichier($file);
                        if ($size!=NULL)
                                $sql = "insert into $prefix"._4nalbum_pictures_newpicture." (pid, gid, img, counter, submitter, date, name, description, votes, rate, extension, width, height) values(NULL, $gid, '$file', 0, '$submitter', now(), '$medianame', '$description', 0, 0, '$ext', ".$size[0].", ".$size[1].")";
                        else
                                $sql = "insert into $prefix"._4nalbum_pictures_newpicture." (pid, gid, img, counter, submitter, date, name, description, votes, rate, extension, width, height) values(NULL, $gid, '$file', 0, '$submitter', now(), '$medianame', '$description', 0, 0, '$ext', 0, 0)";
                        //echo $sql;
                        mysql_query($sql);
                }
                else
                        return $upload_return;
                return "OK";
        }
    }
}
?>