<?PHP
/************************************************************************/
/* Upload / meta fonction and automation for NPDS V                     */
/* This version name NPDS Copyright (c) 2001-2003 by Philippe Brunier   */
/*                                                                      */
/* 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 prepa_upload($Xname, $Xstring) {
   global $user, $cookie;
   global $DOCUMENT_ROOT, $DOCUMENTROOT;
   if ($DOCUMENTROOT!="") {
      $rep=$DOCUMENTROOT;
   } else {
      if ($DOCUMENT_ROOT) {
         $rep=$DOCUMENT_ROOT;
      } else {
         $rep=$_SERVER['DOCUMENT_ROOT'];
      }
   }

   include("modules/upload/upload.conf.php");
   if ($Lnb_file=substr_count($Xstring,"file://")) {
      $pos=0; $fic="";
      if (empty($Xname)) {$Xname=uniqid("upl",true);}
      if ($fp = fopen($rep.$rep_cache.$Xname.".tmp", 'a')) {
         for ($i = 1; $i <= $Lnb_file; $i++) {
            if (substr_count($Xstring,"file://")>0) {
               if ($pos1>0) {$pos=$pos1;}
               $pos=strpos($Xstring,"file://",$pos);
               $pos1=strpos($Xstring,"\"",$pos);
               if (substr($Xstring,$pos+7,1)=="/")
                  $decalX=8;
               else
                  $decalX=7;
               $fic=substr($Xstring,$pos+$decalX,$pos1-$pos-$decalX);
               fwrite($fp,$fic."\n");

               if (strrpos($fic,"/"))
                  $ficname=substr($fic,strrpos($fic,"/")+1);
               elseif (strrpos($fic,"\\"))
                  $ficname=substr($fic,strrpos($fic,"\\")+1);
               else
                  $ficname=substr($fic,strrpos($fic,"%5C")+3);

               // si membre est autorise_upload_p=true
               if ($user) {
                  $uname=$cookie[1];
                  if ($autorise_upload_p) {
                     $rep_upload=$rep.$racine."/users_private/".$uname."/";
                     if (!is_dir($rep_upload)) {
                        @umask("0000");
                        if (@mkdir($rep_upload,0777)) {
                           $fpt = fopen($rep_upload."index.html", 'w');
                           fclose($fpt);
                           $url_upload_dest=$url_upload.$racine."/users_private/".$uname."/";
                        } else {
                           $url_upload_dest=$url_upload.$rep_upload;
                        }
                     } else {
                        $url_upload_dest=$url_upload.$racine."/users_private/".$uname."/";
                     }
                  } else {
                     $url_upload_dest=$url_upload.$rep_upload;
                  }
               } else {
                  $url_upload_dest=$url_upload.$rep_upload;
               }
               // transforme les liens file en http
               if ($decalX==8) {
                  $Xstring=str_replace("file:///$fic",$url_upload_dest.$ficname,$Xstring);
               } else {
                  $Xstring=str_replace("file://$fic",$url_upload_dest.$ficname,$Xstring);
               }
               $Xstring=str_replace($fic,"",$Xstring);
            }
         }
         fclose($fp);
      }
      return array($Xstring, $Xname);
   } else {
      return (false);
   }
}

function win_upload($Xname) {
   echo "<script type=\"text/javascript\" language=\"JavaScript\">
        <!--
        window.open(\"modules/upload/upload.php?name_tmp=$Xname\",\"wtmp\", \"menubar=no,location=no,directories=no,status=no,copyhistory=no,toolbar=no,scrollbars=no, width=540, height=125\");
        //-->
        </script>\n";
}
?>