<?php
//VERSION 2010-02-12
function IN7_FILERENAME($M_DIST_FILES_PARAMS,$FILENAME_POST,$DEST_FOLDER) {
global $BAD_FILE_SIGNS ; $OK_FILE_SIGNS;
if(file_exists(".config.php")) include(".config.php");

//$M_DIST_FILES_PARAMS are:
// [M_DIST_FILES_DEST_FOLDER] => /home/www/biall.com.pl/MARKETING_SMB/BAZA_REKLAM_2008/2008-02-15.127.DRUK_REKLAMY.Prezentacja_w_power_poit_Piotr_Justynski
//    [M_DIST_FILES_PROJECT_TYPE] => on
//        [M_DIST_FILES_DESCRIPTION_AUTO] => on
//            [M_DIST_FILES_DATE_TODAY] => on
 //               [M_DIST_FILES_VERSION_AUTO] => on
//		   [M_DIST_FILES_VERSION_OVERWRITE] - nadpisywanie
 //                   [M_DIST_FILES_EXTENSION_AUTO] => on
//                        [M_DIST_FILES_INDEXNAME] => 
//                            [M_DIST_FILES_TYPE] => NOTATKA
//                                [M_DIST_FILES_DESCRIPTION] => 
//                                    [M_DIST_FILES_DATE] => 
//                                        [M_DIST_FILES_VERSION] => 
//                                        [M_DIST_FILES_EXTENSION] => 
//					[M_DIST_FILES_FOLDER_CREATE_AUTO] - zalozenie katalogu jak nie ma
//					[M_DIST_FILES_TEMP] - tymczasowa wersja                                            
//$FILENAME_POST - wyslana nazwa pliku oryginalnego

//$DEST_FOLDER - folder docelowy - potrzebne do sprawdzenia czy nie bedzie overwrite , zatem automatycznie musi zmienic wersje

echo "<br>FUNCTION IN7_FILERENAME in DEVELOPMENT<br>";
//echo "Parametry z M_DIST_FILES_PARAMS:\n<br>";
//print_r($M_DIST_FILES_PARAMS);
//echo "<br>FILENAME_POST: $FILENAME_POST ";

//Plan dzialania:
//1. Zidentyfikujmy wyslana nazwe pliku i znajdzmy interesujace nas fragmenty -> $GUESS[TYPE]
//2. Przejedzmy po GUESS_Types i uformujmy je odpowiednio w zaleznosci od zadanych wymuszen
//3. Sprawdzmy czy ktores z wymuszen nie jest niemozliwe do realizacji, napiszmy error
//3.1  - Nazwy wpisane normalnie - bez funkcji automatycznych
//4. Uformujmy docelowa nazwe pliku
//5. sprawdzmy czy takiego pliku juz nie ma, a jak jest, to dodajmy wersje + 1
//6. zwrocmy array na wyjsciu z nazwa pliku docelowa

	//1. sekcja identyfikacji pliku
	// zasada: [0]=INDEKS , [1]=TYP_REKORDU(Instrukcja) , [2]=Nazwa(opcj) , [Ost-1 or 3]=Data, [Ost]=.rozszerzenie 
//	echo $FILENAME_POST ;
	$FILE_A=split("\.",$FILENAME_POST);
	$FILE_AB=split("\_",$FILENAME_POST);
	$FILE_P["COUNT"]=count($FILE_A);
	$FILE_PB["COUNT"]=count($FILE_AB);
	//print_r($FILE_A);
	//print_r($FILE_B);
//	echo "\n Count ".$FILE_P[COUNT]." ".$FILE_PB[COUNT];

//     if($PRODUKT_ID==$FILE_A[0]) { //Nazwa produktu jest spelniona i zgodna z oczekiwaniem

	$DST_FILE[0]=$PRODUKT_ID;
	$DST_FILE[1]=$TYP_DANYCH;
	
	//wykrywamy poprawnosc oznaczenia daty

	//dla AB
	if(($FILE_PB[COUNT]>=2)&&($FILE_PB[COUNT]<=7)) { //podstawowa zasada 
		$I=0;		
	foreach($FILE_AB as $FILE_A_A) {
		$I++;
	  $FILE_A_D=split("-",$FILE_A_A);
//	  print_r($FILE_A_D);
	  if(($FILE_A_D[0]>1980)&&
		($FILE_A_D[0]<9999)&&
		(count($FILE_A_D)==3)&&
		($FILE_A_D[1]<=12)&&
		($FILE_A_D[2]<=33)&&
		(strlen($FILE_A_D[2])==2)&&
		(strlen($FILE_A_D[1])==2)&&
		(strlen($FILE_A_D[0])==4)) {
		if(!$M_DIST_FILES_PARAMS[M_DIST_FILES_DATE]) $GUESS[DATE]=$FILE_A_A ; // echo "\nDATA_OK";
		 $GUESS_INDEX[DATE][]=array( 'PREFIX'=>"_" , 'INDEKS'=>"$I" , 'VALUE'=>"$FILE_A_A" );
//		 $GUESS_INDEX[DATE][PREFIX]="_";
//		 $GUESS_INDEX[DATE][INDEKS]="$I";
//		 $GUESS_INDEX[DATE][VALUE]="$FILE_A_A";
		 }
	  }
	 }



	//dla A
	if(($FILE_P[COUNT]>=2)&&($FILE_P[COUNT]<=7)) { //podstawowa zasada 
		$I=0;				
	foreach($FILE_A as $FILE_A_A) {
		$I++;
	  $FILE_A_D=split("-",$FILE_A_A);
//	  echo "<br>Dater dla $FILE_A_D ";
//	  print_r($FILE_A_D);
	  if(($FILE_A_D[0]>1980)&&
		($FILE_A_D[0]<9999)&&
		(count($FILE_A_D)==3)&&
		($FILE_A_D[1]<=12)&&
		($FILE_A_D[2]<=33)&&
		(strlen($FILE_A_D[0])==4)&&
		(strlen($FILE_A_D[1])==2)&&
		(strlen($FILE_A_D[2])==2)) {
		if(!$M_DIST_FILES_PARAMS[M_DIST_FILES_DATE]) if(!$GUESS_INDEX[DATE][1]) $GUESS[DATE]=$FILE_A_D[0]."-".$FILE_A_D[1]."-".$FILE_A_D[2] ; 
//			  echo "\nDATA_OK TUTAJJJJ ";
			 $GUESS_INDEX[DATE][]=array( 'PREFIX'=>"." , 'INDEKS'=>"$I" , 'VALUE'=>$FILE_A_D[0]."-".$FILE_A_D[1]."-".$FILE_A_D[2] );
		}
	  }


	//wykrywamy rozszerzenie
	


	$FILE_A_ROZ=$FILE_A[($FILE_P["COUNT"]-1)];
	//echo "\n Rozszerzenie to $FILE_A_ROZ";
//	$DST_FILE[4]=$FILE_A_ROZ; 
	  if($FILE_A_ROZ) {
	     $GUESS["EXT"]=$FILE_A_ROZ;
	     $GUESS_INDEX["EXT"][]=array( 'PREFIX'=>"." , 'INDEKS'=>($FILE_P["COUNT"]-1) , 'VALUE'=>"$FILE_A_ROZ" );
	  }
	}


//Wyciagamy Opis, usuwamy DATY oraz inne znane ciagi znakow
//Usuwamy date ze stringu


	if(($M_DIST_FILES_PARAMS[M_DIST_FILES_TYPE])&&($M_DIST_FILES_PARAMS[M_DIST_FILES_PROJECT_TYPE])) { //TYPE PROJECT
	$GUESS[TYPE]=$M_DIST_FILES_PARAMS[M_DIST_FILES_TYPE]."_P";
	//TODO wprowadzic typ _P
	$GUESS[TYPE]=$M_DIST_FILES_PARAMS[M_DIST_FILES_TYPE];
	} else if($M_DIST_FILES_PARAMS[M_DIST_FILES_TYPE]) $GUESS[TYPE]=$M_DIST_FILES_PARAMS[M_DIST_FILES_TYPE] ;
	if(($M_DIST_FILES_PARAMS[M_DIST_FILES_TEMP])&&($M_DIST_FILES_PARAMS[M_DIST_FILES_TYPE])&&($GUESS[TYPE]))  $GUESS[TYPE]=$GUESS[TYPE]."_TEMP";

	
	if(($M_DIST_FILES_PARAMS[M_DIST_FILES_VERSION_AUTO])&&(!$M_DIST_FILES_PARAMS[M_DIST_FILES_VERSION])) { //Version Auto
		if($GUESS_INDEX[DATE][1]) {
			$SPLITER=$GUESS_INDEX["DATE"]["1"]["PREFIX"];
			if($SPLITER==".") $SPLITER='\.';	
			$GIS=split($SPLITER,$FILENAME_POST);
//			echo "GIS ".$GUESS_INDEX["DATE"]["1"]["PREFIX"].",".$FILENAME_POST." <br>";
//			print_r($GIS);
			echo $GUESS_INDEX[DATE][1][INDEKS];

			$pattern = '/[[:alpha:]]/';
			$subject = $GIS[$GUESS_INDEX[DATE][1][INDEKS]];
			//$PO=preg_match($pattern, $subject, $GIS[$GUESS_INDEX[DATE][1][INDEKS]]);
			$PO=preg_match($pattern, $subject , $PO2 ,PREG_OFFSET_CAPTURE);
//			print_r($PO);
			if(($PO==0)&&($GIS[$GUESS_INDEX[DATE][1][INDEKS]]>=0)&&($GIS[$GUESS_INDEX[DATE][1][INDEKS]]<=999))   $GUESS[VERSION]=$GIS[$GUESS_INDEX[DATE][1][INDEKS]];
			//$GUESS[VERSION]="";
//			echo "PRZED ".$GIS[$GUESS_INDEX[DATE][1][INDEKS]]."  PO $PO<br>";
		}
	
	}
//Wersja TODO

if($M_DIST_FILES_PARAMS[M_DIST_FILES_VERSION]) $GUESS[VERSION]=$M_DIST_FILES_PARAMS[M_DIST_FILES_VERSION] ;

//Indexname zgadujemy wklepujemy

if($M_DIST_FILES_PARAMS[M_DIST_FILES_INDEXNAME]) $GUESS[INDEXNAME]=$M_DIST_FILES_PARAMS[M_DIST_FILES_INDEXNAME] ;

if((!$GUESS[INDEXNAME])&&(!$M_DIST_FILES_PARAMS[M_DIST_FILES_INDEXNAME])&&($GUESS[DATE])) {
 echo "INDEXNAME AUTO";
   $GUESS[INDEXNAME]=$GUESS[DATE];
   if(($GUESS_INDEX[DATE][0])&&($GUESS_INDEX[DATE][1])) $GUESS[INDEXNAME]=$GUESS_INDEX[DATE][0][VALUE];
} //EOF if((!$GUESS[INDEXNAME...	

   if(($M_DIST_FILES_PARAMS[M_DIST_FILES_DESCRIPTION_AUTO])) {//AUTODESC
	echo "M_DIST_FILES_DESCRIPTION_AUTO L.170<br>\n";
	//	$GUESS[DESCRIPTION]=ereg_replace($GUESS_INDEX[DATE][0][VALUE].$GUESS_INDEX[DATE][0][PREFIX],'',$FILENAME_POST);

       $GUESS[DESCRIPTION]=$M_DIST_FILES_PARAMS[M_DIST_FILES_DESCRIPTION]."_".$FILENAME_POST;
//			echo "<br> GUESS[DESCRIPTION] line 177: ".$GUESS[DESCRIPTION]." should be added ".$M_DIST_FILES_PARAMS[M_DIST_FILES_DESCRIPTION]." EOL.";
		if($GUESS_INDEX[DATE][0]) {
				$SPL=split('_',$GUESS[DESCRIPTION]);
				unset($GUESS[DESCRIPTION]);
				foreach($SPL as $SPLL){
				echo "<br> if $SPLL == ".$GUESS_INDEX[DATE][0][VALUE]." EOL";
				if(!strstr($SPLL,$GUESS_INDEX[DATE][0][VALUE])) {
				 $GUESS[DESCRIPTION].="_".$SPLL;
				 echo "<br>  Added Line 177a | $SPLL |";
				 }
                }
				echo "<br> GUESS[DESCRIPTION] line 177: ".$GUESS[DESCRIPTION]." should be added ".$M_DIST_FILES_PARAMS[M_DIST_FILES_DESCRIPTION]." EOL.";

		if($GUESS_INDEX[DATE][1]) {
				$SPL=split('\.',$GUESS[DESCRIPTION]);
				unset($GUESS[DESCRIPTION]);
				  foreach($SPL as $SPLL){
//				    	echo "<br> if $SPLL == ".$GUESS_INDEX[DATE][1][VALUE]." EOL";
				    if(!strstr($SPLL,$GUESS_INDEX[DATE][1][VALUE])) $GUESS[DESCRIPTION].=".".$SPLL;
				  }
                }

			echo "<br> DESCRIPT TO $GUESS[DESCRIPTION] L.194";
				//	if($GUESS_INDEX[DATE][1]) $GUESS[DESCRIPTION]=ereg_replace($GUESS_INDEX[DATE][1][VALUE].$GUESS_INDEX[DATE][1][PREFIX],'',$FILENAME_POST);
				//		   $GUESS_INDEX[M_DIST_FILES_DESCRIPTION];
                              //wywalamy spacje
                  $GUESS_INDEX[M_DIST_FILES_DESCRIPTION]=str_replace(' ','_',$GUESS_INDEX[M_DIST_FILES_DESCRIPTION]);

  		} else if($FILENAME_POST) {

  } else  $GUESS[DESCRIPTION]=$M_DIST_FILES_PARAMS[M_DIST_FILES_DESCRIPTION]; //EOF ? if((!$M_DIST_FILES_PARAMS[M_DIST_FILES_DESCRIPTION])&&($M_DIST_FILES_PARAMS[M_DIST_FILES_DESCRIPTION_AUTO])) {



		if($GUESS_INDEX[EXT][0]) $GUESS[DESCRIPTION]=ereg_replace($GUESS_INDEX[EXT][0][PREFIX].$GUESS_INDEX[EXT][0][VALUE],'',$GUESS[DESCRIPTION]);
//		if($GUESS[VERSION]) $GUESS[DESCRIPTION]=ereg_replace('\.'.$GUESS[VERSION],'',$FILENAME_POST);
		$SPL=split('\.',$GUESS[DESCRIPTION]);

		unset($GUESS[DESCRIPTION]);
		  $I=0;
		foreach($SPL as $SPLL){
		     if(!strstr($GUESS[VERSION],$SPLL)) {
		       $GUESS[DESCRIPTION].=$SPLL;
		       if($_SESSION[DEBUG])       echo "<br>Sciagam z desc wersje $SPLL == ".$GUESS[VERSION]." |";
		     }

                       if($_SESSION[DEBUG])	  echo "<br>line 203: teraz GUESS[DESCRIPTION]= ".$GUESS[DESCRIPTION]." | a w oryginale z parametru: ".$FILENAME_POST." |";
                }
		  
	}


//







//3.1  - Nazwy wpisane normalnie - bez funkcji automatycznych
 if((!$M_DIST_FILES_PARAMS[M_DIST_FILES_DESCRIPTION_AUTO])&&($M_DIST_FILES_PARAMS[M_DIST_FILES_DESCRIPTION]))
  $GUESS[DESCRIPTION]=$M_DIST_FILES_PARAMS[M_DIST_FILES_DESCRIPTION];
  if((!$M_DIST_FILES_PARAMS[M_DIST_FILES_DESCRIPTION_AUTO])&&(!$M_DIST_FILES_PARAMS[M_DIST_FILES_DESCRIPTION])) $ERROR.="ERROR_EMPTY_M_DIST_FILES_DESCRIPTION (BRAK OPISU),";

//3.1.1 ... Walidacja daty 
 if((!$M_DIST_FILES_PARAMS[M_DIST_FILES_DATE])&&($GUESS[DATE]))
   $GUESS[DATE]=$M_DIST_FILES_PARAMS[DATE];
     
   if($M_DIST_FILES_PARAMS[M_DIST_FILES_DATE_TODAY]) $GUESS[DATE]=date(Y)."-".date(m)."-".date(d);
//      $GDATE=date
     if(!$GUESS[DATE])	if($M_DIST_FILES_PARAMS[M_DIST_FILES_DATE]) $GUESS[DATE]=$M_DIST_FILES_PARAMS[M_DIST_FILES_DATE];
     if((!$GUESS[DATE])) $ERROR.="ERROR_EMPTY_M_DIST_FILES_DATE (PUSTA DATA),"; else {
       //TODO uzyc globalnej funkcji walidacji daty
 	  $FILE_A_D=split("-",$GUESS[DATE]);
//	  echo "<br>Dater dla $FILE_A_D ";
//	  print_r($FILE_A_D);
	  if(($FILE_A_D[0]>1980)&&
		($FILE_A_D[0]<9999)&&
		(count($FILE_A_D)==3)&&
		($FILE_A_D[1]<=12)&&
		($FILE_A_D[2]<=33)&&
		(strlen($FILE_A_D[0])==4)&&
		(strlen($FILE_A_D[1])==2)&&
		(strlen($FILE_A_D[2])==2)) $INFO.="3.1.1 WALIDACJA DATY OK , "    ;
                else $ERROR.="ERROR_BAD_M_DIST_FILES_DATE (zly format daty ".$GUESS[DATE].") , ";     
     }

     
if((!$GUESS[VERSION])&&($M_DIST_FILES_PARAMS[M_DIST_FILES_VERSION])) $GUESS[VERSION]=$M_DIST_FILES_PARAMS[M_DIST_FILES_VERSION];
 else if((!$GUESS[VERSION])&&(!$M_DIST_FILES_PARAMS[M_DIST_FILES_VERSION]))  $GUESS[VERSION]="1";

    



//Ogolne usuniecie spacji
                  $GUESS[VERSION]=str_replace(' ','_',$GUESS[VERSION]);
                  $GUESS[DESCRIPTION]=str_replace($BAD_FILE_SIGNS,$OK_FILE_SIGNS,$GUESS[DESCRIPTION]);
                  $GUESS[M_DIST_FILES_EXTENSION]=str_replace($BAD_FILE_SIGNS,$OK_FILE_SIGNS,$GUESS[M_DIST_FILES_EXTENSION]);
                  $GUESS[M_DIST_FILES_INDEXNAME]=str_replace($BAD_FILE_SIGNS,$OK_FILE_SIGNS,$GUESS[M_DIST_FILES_INDEXNAME]);

//EOF 3.1


//echo "Zgadnieto z pliku";
if($_SESSION[DEBUG]) print_r($GUESS);
//print_r($GUESS_INDEX);




//Listujemy czy nie zrobimy overwrite'u

      
     if(!file_exists($DEST_FOLDER)) {
         if($M_DIST_FILES_PARAMS[M_DIST_FILES_FOLDER_CREATE_AUTO]) {
             mkdir ($DEST_FOLDER, 0777, true);     
             $INFO.="TWORZE_NOWY_FOLDER , ";
          } else $ERROR.="ERROR_DEST_FOLDER_NOT_EXISTS";
          if(!file_exists($DEST_FOLDER)) $ERROR.="ERROR_DEST_FOLDER_NOT_EXISTS";
     }
     //

     //Sprawdzanie wersji i dodanie nowszej, zamiast overwrite (dla funkcji Automatycznie dodaj wersje)
     if ($handle3 = opendir($DEST_FOLDER)) {
             $I=0;
             while($I<1000) {
             $I++;	
                   unset($ERROR_VER);
                   while (false !== ($file = readdir($handle3))) {
                        //   echo "<br>DIRLIST sprawdzam $file pod katem ".$GUESS[DESCRIPTION]."z katalofu $DEST_FOLDER";                   
                           if(strstr($file,$GUESS[INDEXNAME].".".$GUESS[TYPE].".".$GUESS[DESCRIPTION].".".$GUESS[DATE].".".$GUESS[VERSION].".".$GUESS[EXT])) {
               		    echo "<br>WARNING Plik $file istnieje juz, (".$GUESS[VERSION].")";
               		      //Zmieniamy tylko wersje jak jest autowersja oraz nie jest OVERWRITE
                            if(($M_DIST_FILES_PARAMS[M_DIST_FILES_VERSION_AUTO])&&(!$M_DIST_FILES_PARAMS[M_DIST_FILES_VERSION_OVERWRITE])) {
                             $GUESS[VERSION]++;
                             $ERROR_VER="ERROR_OVERWRITE";
                             echo "- zmieniamy wersje ".$GUESS[VERSION]." . ";
                             }
                            if($M_DIST_FILES_PARAMS[M_DIST_FILES_VERSION_OVERWRITE]) $INFO.="OVERWRITE,";
                            echo " - bedziemy nadpisywac - uzycie OVERWRITE";
                           }
               		      //echo "$FOLDER  , $FOLDER_I ,  $file\n";
                   } //EOF while (false !== ($file = readdir($handle3)))
                  if(!$ERROR_VER) {
                     echo "<br>Brak erroru przerywam proces sprawdzania wersji";
                     break;
                  } else {
                    
                    if($M_DIST_FILES_PARAMS[M_DIST_FILES_VERSION_AUTO]) echo "<br>Error powtorzonej wersji , powtarzam petle testu AUTOWERSJI ($I)".$GUESS[VERSION]." .";
                    else break ;
                  }
             } //EOF while($I<1000) 
        } //EOF if ($handle3 = opendir($DEST_FOLDER))

//if(!$ERROR_VER) {

                          $RETURN_ARR=$GUESS;
                          $RETURN_ARR[M_DIST_FILES_DEST_FOLDER]=$DEST_FOLDER;
                          $RETURN_ARR[DST_FILE_NAME]=$GUESS[INDEXNAME].".".$GUESS[TYPE].".".str_replace($BAD_FILE_SIGNS,$OK_FILE_SIGNS,$GUESS[DESCRIPTION]).".".$GUESS[DATE].".".$GUESS[VERSION].".".$GUESS[EXT];
                          $RETURN_ARR[ERROR]=$ERROR_VER.$ERROR;                          
                          $RETURN_ARR[INFO]=$INFO;
                          return($RETURN_ARR);
//             		}             		//             			echo "$file";
//                      



}//eof func


?>
