<?php

	class InitSet {
	
	  var $textpl;
	  var $scarto = Array( ".", "..", "" );
	  var $dpub = KiP_pub;
	  var $kp_version = KiP_version;
	  var $kp_link = KiP_modR;
	  var $fdata = KiP_date;
	  var $merlos;
	  var $rootFold = KiP_hroot;

		function parsa_name ( $tpword ) {
		
		  $elemento = Array (
      "&deg;" => "", " " => "_",
      "%20" => "_", "'" => "",
      "\"" => "", "-" => "_",
      "/" => "_", "!" => "",
      "?" => "", ":" => "",
      "." => "_", "," => "",
      "&#39;" => "_", "&agrave;" => "a",
      "%C3%A0" => "a", "Ã " => "a",
      "à" => "a", "&egrave;" => "e",
      "%C3%A8" => "e", "Ã¨" => "e",
      "è" => "e", "&eacute;" => "e",
      "%C3%A9" => "e", "Ã©" => "e",
      "é" => "e", "&igrave;" => "i",
      "%C3%AC" => "i", "Ã¬" => "i",
      "ì" => "i", "&ograve;" => "o",
      "%C3%B2" => "o", "Ã²" => "o",
      "ò" => "o", "&ugrave;" => "u",
      "%C3%B9" => "u", "Ã¹" => "u",
      "ù" => "u", "&amp;" => "e"
      );
      
      $this->textpl = $tpword;
      
      foreach ( $elemento as $let => $sol ) {
      
        $this->textpl = @str_replace ( $let, $sol, $this->textpl );
      
      }

		  $this->textpl = @strtolower ( $this->textpl );
		  
		  return $this->textpl;
				
		}
		
		function getAlbum ( $KiDir, $graphik ) {

      $i = 0;
      
      $path = $KiDir;
      
      $kifold = "";
      
      if ( $handle = opendir ( $KiDir ) ) {

        while ( false !== ( $file = readdir ( $handle ) ) ) { 
           
           $contenuto[$i] = $file;
           
           $i++;
        
        }

        for( $i = 0; $i < sizeof ( $contenuto ); $i++ ) {
        
          if ( is_dir ( $KiDir . $contenuto[$i] ) ) {
          
            if ( in_array ( $contenuto[$i], $this->scarto ) ) { $kifold .= ""; }
            
            else {
            
              $nome_album = $contenuto[$i];
              
              $link_album = $this->parsa_name ( $nome_album );
              
              $sec_codec = $this->codeLink( $nome_album );
              
              if ( defined ( $this->kp_link ) ) {
                if ( $this->kp_link == "true" ) { $visLink = $sec_codec . "/" . $link_album . ".html"; }
                else { $visLink = "?album=" . $link_album . "&step=" . $sec_codec; }
              } else { $visLink = "?album=" . $link_album . "&step=" . $sec_codec; }
              
              $bimg = $this->getAlbImg( $KiDir . $contenuto[$i] . "/" );
              
              if ( empty ( $bimg ) ) {
              
                $bimg = "no_thumb.png";
                
                $per_img = "images/";
              
              } else { $per_img = $KiDir . $nome_album . "/"; }
            
              include( $graphik . "pages/album.php" );
              $kifold .= $tpl_album;
            
            }

          }
         
        }

        closedir ( $handle );
        
        return $this->textpl = $kifold;
      
      }
    
    }

    function isfile ( $file ) {
    
      if ( preg_match( '/^[^.^:^?^\-][^:^?]*\.(?i)' . $this->getexts() . '$/', $file ) != FALSE ) { $img = $file; }
        
      else { $img = ""; }
      
      return $img;

    }

    function getexts() { return '(ico|jpeg|jpg|jpe|gif|png|bmp|bm|tiff|tif|svg)'; }
    
    function getMime() {
    
      $arr_mime = Array ( "image/x-icon", "image/gif", "image/jpeg", "image/tiff", "image/bmp", "image/x-windows-bmp", "image/png", "image/svg+xml" );
    
      return $arr_mime;
    
    }
    
    function getAlbImg ( $album ) {

      $i = 0;
      
      $path = $album;
      
      $kifold = "";
      
      if ( $handle = opendir ( $album ) ) {

        while ( false !== ( $file = readdir ( $handle ) ) ) {
           
           $contenuto[$i] = $file;
           
           $i++;
        
        }

        for( $i = 0; $i < sizeof ( $contenuto ); $i++ ) {
        
          if ( is_file ( $album . $contenuto[$i] ) ) {
          
            $gmi_str = $this->isfile ( $contenuto[$i] );
            
            if ( !empty ( $gmi_str ) ) { $kifold = $gmi_str; }
            
          }

        }

        closedir ( $handle );
        
        return $this->textpl = $kifold;
      
      }

    }
    
    function getCopy( $langy, $_LANG ) {
        
      $data_pub = $this->dpub;
    
      $year = date("Y");

      if ( intval( $data_pub ) == intval( $year ) ) { $copyd = intval( $data_pub ); } elseif ( intval( $data_pub ) < intval( $year ) ) { $copyd = intval( $data_pub ) . " - " . intval( $year ); }

      $copy = $this->parse_lang("kiphoto_copyright", $langy, $_LANG);
      $power = $this->parse_lang("kiphoto_powered", $langy, $_LANG);
      $cred = $this->parse_lang("kiphoto_credits", $langy, $_LANG);

      $copyright = sprintf ( $copy, $copyd ) . " &nbsp;&nbsp;-&nbsp;&nbsp; " . sprintf ( $power, $this->kp_version ) . " &nbsp;&nbsp;-&nbsp;&nbsp; " . $cred;
    
      return $copyright;
    
    }
    
    function codeLink ( $folder ) {
    
      $fcifr = base64_encode ( $folder );
              
      $tot_len = strlen ( $fcifr );
      
      $ndiv = ( $tot_len / 2 );

      if ( defined ( $this->kp_link ) ) {
        if ( $this->kp_link == "true" ) { $secCode = substr ( $fcifr, 0, $ndiv ) . "read" . substr ( $fcifr, $ndiv ) . "/" . $tot_len; }
        else { $secCode = substr ( $fcifr, 0, $ndiv ) . "read" . substr ( $fcifr, $ndiv ) . "&tpuls=" . $tot_len; }
      } else { $secCode = substr ( $fcifr, 0, $ndiv ) . "read" . substr ( $fcifr, $ndiv ) . "&tpuls=" . $tot_len; }
    
      return $secCode;
    
    }
    
    function getImg ( $album ) {

      $i = $v = 0; $buso = "";

      if ( $handle = opendir ( $album ) ) {

        while ( false !== ( $file = readdir ( $handle ) ) ) {

           $contenuto[$i] = $file;

           $i++;

        }

        closedir ( $handle );
      
      }

      for ( $c = 0; $c < sizeof ( $contenuto ); $c++ ) {
        
        if ( in_array ( $contenuto[$c], $this->scarto ) ) { $kifold .= ""; }
          
        else {
          
          if ( is_file ( $album . $contenuto[$c] ) ) {
            
            $gmi_str = $this->isfile ( $contenuto[$c] );
              
            if ( !empty ( $gmi_str ) ) { $v++; $kifold .= $album . $gmi_str . "|"; }
              
          } elseif ( is_dir ( $album . $contenuto[$c] ) ) {
          
            $buso .= $album . $contenuto[$c] . "noka" . $contenuto[$c] . "|";
          
          }

        }

      }
      
      $kifold = substr ( $kifold, 0, -1 );

      $separ = base64_encode( "KiPhoto" );
      
      $last_time_modified = filemtime ( $album );

      $last_mod = date( "d-m-Y H:i", $last_time_modified );
      
      $report = $kifold . $separ . $v . $separ . $last_mod;
      
      if ( !empty ( $buso ) ) {
        $buso = substr ( $buso, 0, -1 );
        $report .= $separ . "1" . $separ . $buso;
      } else { $report .= $separ . "0"; }

      return $this->textpl = $report;

    }
    
    function VisualImg ( $valood, $graphik, $pag ) {

      $kifold = ""; $jem = explode ( "|", $valood );

      if ( $pag == "0" ) { $in = 0; }
      else { $in = ( ( $pag * 20 ) ); }

      $fin = ( ( $in + 20 ) );

      for ( $m = $in; $m < $fin; $m++ ) {

        if ( in_array ( $jem[$m], $jem ) ) {

          $photog = $jem[$m];
          
          $img_name = array_pop ( explode ( "/", $photog ) );
  
          if ( strlen ( $img_name ) > 25 ) $iname = substr ( $img_name, 0, 25 ) . "...";
          else $iname = $img_name;
  
          include( $graphik . "pages/photo.php" );
          $kifold .= $tpl_photo;
          
        }

      }

      return $this->textpl = $kifold;

    }

		function getElencAlbum ( $KiDir, $graphik ) {

      $i = 0; $kifold = "";
      
      if ( $handle = opendir ( $KiDir ) ) {

        while ( false !== ( $file = readdir ( $handle ) ) ) { 
           
           $contenuto[$i] = $file;
           
           $i++;
        
        }

        for( $x = 0; $x < sizeof ( $contenuto ); $x++ ) {
        
          if ( is_dir ( $KiDir . $contenuto[$x] ) ) {
          
            if ( in_array ( $contenuto[$x], $this->scarto ) ) { $kifold .= ""; }
            
            else {
            
              $nome_album = $contenuto[$x];
              
              $link_album = $this->parsa_name ( $nome_album );
              
              $sec_codec = $this->codeLink( $nome_album );
              
              include( $graphik . "pages/foldel.php" );
              $kifold .= $tpl_album;
            
            }

          }
         
        }

        closedir ( $handle );
        
        return $this->textpl = $kifold;
      
      }
    
    }

    function getError( $status ) {
    
      switch ( $status ) {
      
        case '1':
        
          return "Valori mancanti durante la ricezione dell'album selezionato";
        
        break;
        
        case '2':
        
          return "Valore di controllo non idoneo";
        
        break;
        
        case '3':
        
          return "Valore di controllo non corrispondente all'album selezionato";
        
        break;
      
      }
    
    }

    function ListLang ( $dperc ) {
    
      if ( file_exists ( $dperc ) && is_dir ( $dperc ) ) {
      
        $i = 0; $kilang = ""; $contenuto = Array(); 
      
        if ( $handle = opendir ( $dperc ) ) {

          while ( false !== ( $file = readdir ( $handle ) ) ) { 
           
            $contenuto[$i] = $file; $i++;
        
          }

          for( $n = 0; $n < sizeof ( $contenuto ); $n++ ) {
        
            if ( is_dir ( $dperc . $contenuto[$n] ) ) {
          
              if ( in_array ( $contenuto[$n], $this->scarto ) ) { $kilang .= ""; }
            
              else { $kilang .= $contenuto[$n] . ","; }

            }
         
          }

          closedir ( $handle );

          $this->merlos = substr ( $kilang, 0, -1 );
      
        }

      } else { $this->merlos = ""; }

    }

    function getFlag ( $graphik ) {
    
      $kilang = "";
    
      $dmoz = explode ( ",", $this->merlos );
      
      for ( $z = 0; $z < count ( $dmoz ); $z++ ) {
  
        $dufy = $dmoz[$z];

        list ( $suff, $nation ) = explode ( "-", $dufy );
        $nat = strtolower ( $nation );
        if ( $nat == "us" ) { $f_lang = "gb"; }
        else { $f_lang = $nat; }
                
        include( $graphik . "pages/flag.php" );
        $kilang .= $tpl_flag;
                
      }

      return $this->textpl = $kilang;

    }
    
  	function parse_lang ( $text_l, $langy, $_LANG ) {

      if ( array_key_exists ( $text_l, $_LANG ) ) {
      
        return $this->textpl = str_replace ( $text_l, $_LANG[$text_l], $text_l );
      
      }
    
    }
    
    function check_lang ( $l_check ) {

      $lang_t = explode ( ",", $this->merlos );

      if ( in_array ( $l_check, $lang_t ) && preg_match ( "/[a-zA-Z-]/", $l_check ) )
        return $this->textpl = $l_check;
      else
        return $this->textpl = "";
    
    }
    
    function parse_date ( $date_p, $_LANG ) {
    
      switch ( $this->fdata ) {
        case '1': $zid = "exp_"; break;
        case '2': $zid = "contr_"; break;
        case '3': $zid = ""; break;
        default: $zid = "exp_"; break;
      }
      
      if ( !empty ( $zid ) ) {
      
        $dax = explode ( "-", $date_p ); $gnox = $zid . $dax[1];
      
        $jin = $dax[0] . " " . str_replace ( $gnox, $_LANG[$gnox], $gnox ) . " " . $dax[2];
        
        return $this->textpl = $jin;
        
      } else { return $this->textpl = $date_p; }
    
    }

	}