<?php
/*
 * 2011-09-27
 * Obsługa include_php do dodawania kodu php w treści artykułu
 */

/*
 * 2011-09-30
 * Dodanie slidera w artykule poprzez {slider}ścieżka{/slider}
 */

namespace article;

class Article
{
  private $_id;
  private $_date_add;
  private $_date_modify;
  private $_print_enabled;
  private $_show_date;
  private $_show_title;
  private $_title;
  private $_text;
  private $_meta_description;
  private $_meta_keywords;
  private $_display;
  private $_show_author;
  private $_author;
  private $_keep_archive;
  private $_seo_link;
  private $_id_layout;
  private $_images;
  private $_files;
  
  public function get_files() {
    return $this->_files;
  }

  public function set_files($_files) {
    $this->_files = $_files;
  }
  
  public function get_images() {
    return $this->_images;
  }

  public function set_images($_images) {
    $this->_images = $_images;
  }
  
  public function get_id_layout() {
    return $this->_id_layout;
  }

  public function set_id_layout($_id_layout) {
    $this->_id_layout = $_id_layout;
  }
  
  public function get_seo_link() {
    return $this->_seo_link;
  }

  public function set_seo_link($_seo_link) {
    $this->_seo_link = $_seo_link;
  }

  public function get_keep_archive() {
    return $this->_keep_archive;
  }

  public function set_keep_archive($_keep_archive) {
    $this->_keep_archive = $_keep_archive;
  }

  public function get_show_author()
  {
    return $this->_show_author;
  }

  public function set_show_author($_show_author)
  {
    $this->_show_author = $_show_author;
  }

  public function get_author()
  {
    return $this->_author;
  }

  public function set_author($_author)
  {
    $this->_author = $_author;
  }

  public function get_id()
  {
    return $this->_id;
  }

  public function set_id($_id)
  {
    $this->_id = $_id;
  }

  public function get_date_add()
  {
    return $this->_date_add;
  }

  public function set_date_add($_date_add)
  {
    $this->_date_add = $_date_add;
  }

  public function get_date_modify()
  {
    return $this->_date_modify;
  }

  public function set_date_modify($_date_modify)
  {
    $this->_date_modify = $_date_modify;
  }

  public function get_print_enabled()
  {
    return $this->_print_enabled;
  }

  public function set_print_enabled($_print_enabled)
  {
    $this->_print_enabled = $_print_enabled;
  }

  public function get_show_date()
  {
    return $this->_show_date;
  }

  public function set_show_date($_show_date)
  {
    $this->_show_date = $_show_date;
  }

  public function get_show_title()
  {
    return $this->_show_title;
  }

  public function set_show_title($_show_title)
  {
    $this->_show_title = $_show_title;
  }

  public function get_title()
  {
    return $this->_title;
  }

  public function set_title($_title)
  {
    $this->_title = $_title;
  }

  public function get_text()
  {
    return $this->_text;
  }

  public function set_text($_text)
  {
    $this->_text = $_text;
  }

  public function get_meta_description()
  {
    return $this->_meta_description;
  }

  public function set_meta_description($_meta_description)
  {
    $this->_meta_description = $_meta_description;
  }

  public function get_meta_keywords()
  {
    return $this->_meta_keywords;
  }

  public function set_meta_keywords($_meta_keywords)
  {
    $this->_meta_keywords = $_meta_keywords;
  }

  public function get_display()
  {
    return $this->_display;
  }

  public function set_display($_display)
  {
    $this->_display = $_display;
  }

  public function getImg()
  {
    $src = '';

    $regex_img = "|<[\s\v]*img[\s\v]([^>]*[^>]*)>|Ui";
    preg_match_all($regex_img, $this -> _text,$matches_img);
    $count_img = count($matches_img[0]);
    if ( $count_img )
    {
      if ( @$matches_img[1][0] )
      {
        $inline_params = $matches_img[1][0];
        $asrc = array();
        preg_match( "#src=\"(.*?)\"#s", $inline_params, $asrc );
        if ( isset($asrc[1]) )
          $src = trim($asrc[1]);
        else
        $src = "";
        $syn = substr($src,0,1);
        if ( $syn === "/" or $syn === "\\" )
          $src = substr($src,1,99999);
        $syn = substr($src,0,3);
        if ( $syn == "../" )
          $src = substr($src,3,99999);
        $syn = substr($src,0,7);
        if ( $syn == "example" )
          $src = substr($src,8,99999);
      }
    }
    else
    {
      $image = false;
      if ( strpos($this -> _text,"{galeria}") !== false )
      {
        $text = explode("{galeria}",$this -> _text);
        for ( $i=1; $i<count($text); $i++ )
        {
          $temp = explode("{/galeria}",$text[$i]);
          $path = $temp[0];
          if ( $path != "" && is_dir( $path )  )
          {
            @$fh = opendir($path);
            while ( false !== ( $filename = @readdir($fh) ) && !$image )
            {
              if ( $filename == "." || $filename == ".." ) continue;
              if ( file_exists($path.$filename) )
              {
                $src = $path.$filename;
                $image = true;
              }
            }
          }
        }
      }
    }
    return $src;
  }

  public function getModifyText( $entry = false ) {
    if ( $entry )
      $this -> removeImages();
    if ( !$entry ) {
      $this -> drawGallery();
      $this -> drawShadowBox();
    }
    $this -> includePHP( $entry );
    $this -> drawSlider( $entry );
    $this -> getMap();
    return $this -> _text;
  }

  public function getMap()
  {
    $out = '';
    if ( strpos($this->_text,"{mapa}") !== false )
    {
      $text = explode("{mapa}",$this->_text);
      $before = $text[0];

      for ( $i=1; $i<count($text); $i++ )
      {
        $temp = explode("{/mapa}",$text[$i]);
        $settings_temp = $temp[0];

        $settings = explode("|",$settings_temp);
        for ( $j=0; $j<count($settings); $j++ )
        {
          $params = explode(":",$settings[$j]);
          if ( trim($params[0]) == "adres" )
            $adress = $params[1];
          else if ( trim($params[0]) == "szerokosc" )
            $width = $params[1];
          else if ( trim($params[0]) == "wysokosc" )
            $height = $params[1];
          else if ( trim($params[0]) == "x_coord" )
            $x_coord = \System::saveString($params[1]);
          else if ( trim($params[0]) == "y_coord" )
            $y_coord = \System::saveString($params[1]);
          else if ( trim($params[0]) == "zoom" )
            $zoom = (int)$params[1];
        }

        $id = rand(0,10000000000000);

        if ( !isset( $zoom ) || !$zoom )
          $zoom = 13;

        if ( $adress != "" && $adress != " " )
        {
          if ( $width == "" )
            $width = 400;
          if ( $height == "" )
            $height = 250;

            $tpl = new \Savant3;
            $tpl -> _google_map_key = \System::getSystemSettings( 'google_map_key' );
            $tpl -> _adress     = trim( strip_tags( $adress ) );
            $tpl -> _zoom     = (int)$zoom;
            $tpl -> _width      = (int)$width;
            $tpl -> _height     = (int)$height;
            $tpl -> _id       = $id;
            $out .= $tpl -> fetch( 'templates/article-map.php' );
          }
          else if ( $x_coord && $y_coord )
          {
            $tpl = new \Savant3;
            $tpl -> _google_map_key = \System::getSystemSettings( 'google_map_key' );
            $tpl -> _x_coord    = $x_coord;
            $tpl -> _y_coord    = $y_coord;
            $tpl -> _zoom     = (int)$zoom;
            $tpl -> _width      = (int)$width;
            $tpl -> _height     = (int)$height;
            $tpl -> _id       = $id;
            $out .= $tpl -> fetch( 'templates/article-map.php' );
          }
        $out .= $temp[1];
      }
      $this -> _text = $before . $out;
    }
  }

  public function drawSlider( $entry ) {
    $out = '';

    if ( strpos( $this -> _text , '{slider}' ) !== false ) {

      $text = explode( '{slider}' , $this -> _text );
      $before = $text[0];
      for ( $i = 1; $i < count( $text ); $i++ ) {

        $temp   = explode( '{/slider}' , $text[$i] );
        $path   = $temp[0];
        if ( $path && !$entry && is_dir( $path ) ) {

          $fh = @opendir($path);
          while ( false !== ( $filename = @readdir($fh) ) ) {
            if ( $filename == "." || $filename == ".." ) continue;
            if ( file_exists($path.$filename) ) {
              $nazwa = pathinfo( $path  . $filename );
              $pliki[$nazwa['basename']] = $nazwa['extension'];
            }
          }
          if ( is_array( $pliki ) ) ksort($pliki);
          if ( is_array( $pliki ) ) foreach ( $pliki as $key => $val ) {
            $img['src'] = $path . $key;
            $images[] = $img;
          }
          $out .= \article\VArticle::drawSlider( $this -> _id , $images );
        }
        $out .= $temp[1];
      }
      $this -> _text = $before . $out;
    }
  }

  public function includePHP( $entry ) {
    $out = '';

    if ( strpos( $this -> _text , '{include_php}' ) !== false ) {

      $text = explode( '{include_php}' , $this -> _text );
      $before = $text[0];
      for ( $i = 1; $i < count( $text ); $i++ ) {

        $temp   = explode( '{/include_php}' , $text[$i] );
        $path   = $temp[0];
        if ( $path && !$entry ) {
          if ( file_exists( $path ) ) {
            ob_start();
            include( $path );
            $out .= ob_get_contents();
            ob_end_clean();
          }
        }
        $out .= $temp[1];
      }
      $this -> _text = $before . $out;
    }
  }

  public function drawGallery() {
    $out = '';

    if ( strpos( $this -> _text , '{galeria}' ) !== false ) {

      $text = explode( '{galeria}' , $this -> _text );
      $before = $text[0];
      for ( $i = 1; $i < count( $text ); $i++ ) {

        if ( trim( strip_tags( $text[$i] ) ) != '' ) {

          $images = '';
          $temp   = explode( '{/galeria}' , $text[$i] );
          $path   = strip_tags( $temp[0] );

          if ( isset( $path ) && is_dir( $path ) && $path != '' ) {
            $fh = @opendir($path);
            while ( false !== ( $filename = @readdir($fh) ) ) {
              if ( $filename == "." || $filename == ".." ) continue;
              if ( file_exists($path.$filename) ) {
                $nazwa = pathinfo( $path . $filename );
                $pliki[$nazwa['basename']] = $nazwa['extension'];
              }
            }
          }
          if ( isset( $pliki ) && is_array( $pliki ) )
            ksort($pliki);
          if ( isset( $pliki ) && is_array( $pliki ) ) foreach ( $pliki as $key => $val ) {
            $img['src'] = $path . $key;
            $images[] = $img;
          }
          $out .= \article\VArticle::drawGallery( $this -> _id , $images );
          if ( isset( $temp[1] ) )
            $out .= $temp[1];
        }
      }
      $this -> _text = $before . $out;
    }
  }

  public function removeImages()
  {
    $out        = '';
    $regex_img  = "|<[\s\v]*img[\s\v]([^>]*[^>]*)>|Ui";

    preg_match_all( $regex_img, $this -> _text, $matches_img);
    $count_img = count( $matches_img[0] );
    if ( $count_img )
    {
      for ( $i = 0; $i < $count_img; $i++ )
      {
        if ( @$matches_img[1][$i] )
        {
          $this -> _text = str_replace( $matches_img[0][$i] , '' , $this -> _text );
        }
      }
    }

    if ( strpos( $this -> _text , '{galeria}' ) !== false )
    {
      $text   = explode( '{galeria}' , $this -> _text );
      $before = $text[0];
      for ( $i = 1; $i < count( $text ); $i++ )
      {
        $temp = explode( '{/galeria}' , $text[$i] );
        $out  .= $temp[1];
      }
      $this -> _text = $before . $out;
    }
  }

  public function drawShadowBox()
  {
    $regex_img = "|<[\s\v]*img[\s\v]([^>]*[^>]*)>|Ui";
    preg_match_all( $regex_img, $this -> _text, $matches_img);
    $count_img = count( $matches_img[0] );
    if ( $count_img )
    {
      for ( $i=0; $i < $count_img; $i++ )
      {
        if (@$matches_img[1][$i])
        {
          $inline_params = $matches_img[1][$i];
          $aclass = array();
          preg_match( "#class=\"(.*?)\"#s", $inline_params, $aclass );
          if ( isset($aclass[1]) )
            $individ_class = trim($aclass[1]);
          else
              $individ_class="";
          $awidth = array();
          preg_match( "#width:(.*?);#s", $inline_params, $awidth );
          if ( isset($awidth[1]) )
            $individ_width = trim($awidth[1]);
          else
            $individ_width="";
          $aheight = array();
          preg_match( "#height:(.*?);#s", $inline_params, $aheight );
          if ( isset($aheight[1]) )
            $individ_height = trim($aheight[1]);
          else
            $individ_height="";
          $aborder = array();
          preg_match( "#border:(.*?);#s", $inline_params, $aborder );
          if ( isset($aborder[0]) )
            $individ_border = trim($aborder[0]);
          else
            $individ_border ="";
          $amagin = array();
          preg_match( "#margin:(.*?);#s", $inline_params, $amagin );
          if ( isset($amagin[0]) )
            $individ_margin = trim($amagin[0]);
          else
            $individ_margin ="";
          $amaginl = array();
          preg_match( "#margin-left:(.*?);#s", $inline_params, $amaginl );
          if ( isset($amaginl[0]) )
            $individ_marginl = trim($amaginl[0]);
          else
            $individ_marginl ="";
          $amaginr = array();
          preg_match( "#margin-right:(.*?);#s", $inline_params, $amaginr );
          if ( isset($amaginr[0]) )
            $individ_marginr = trim($amaginr[0]);
          else
            $individ_marginr ="";
          $amagint = array();
          preg_match( "#margin-top:(.*?);#s", $inline_params, $amagint );
          if ( isset($amagint[0]) )
            $individ_margint = trim($amagint[0]);
          else
            $individ_margint ="";
          $amaginb = array();
          preg_match( "#margin-bottom:(.*?);#s", $inline_params, $amaginb );
          if ( isset($amaginb[0]) )
            $individ_marginb = trim($amaginb[0]);
          else
            $individ_marginb ="";
          $afloat = array();
          preg_match( "#float:(.*?);#s", $inline_params, $afloat );
          if ( isset($afloat[0]) )
            $individ_afloat = trim($afloat[0]);
          else
            $individ_afloat ="";
          $asrc = array();
          preg_match( "#src=\"(.*?)\"#s", $inline_params, $asrc );
          if ( isset($asrc[1]) )
            $src = trim($asrc[1]);
          else
            $src = "";
          $syn = substr($src,0,1);
          if ( $syn === "/" or $syn === "\\" )
            $src = substr($src,1,99999);
          $syn = substr($src,0,3);
          if ( $syn == "../" )
            $src = substr($src,3,99999);
          $syn = substr($src,0,7);
          if ( $syn == "example" )
            $src = substr($src,8,99999);

          if ( file_exists($src) )
          {
            $check = GetImageSize($src);
            $width = $check[0];
            $height = $check[1];
            if ( ( $width > 400 || $height > 300 ) && ( $width != (int)$individ_width || $height != (int)$individ_height ) && $individ_class != 'noshadow' )
            {
              $out = "<a href='" . $src . "' class='fancybox-buttons' rel='" . $this -> _id . "'>";
              $out .= "<img class='" . $individ_class . "' src='" . $src . "'
                  style='
                    width:" . $individ_width . ";
                    height:" . $individ_height . ";
                    " . $individ_border . "
                    " . $individ_margin . "
                    " . $individ_marginr . "
                    " . $individ_marginl . "
                    " . $individ_margint . "
                    " . $individ_marginb . "
                    " . $individ_afloat . "'>";
              $out .= "</a>";
              $this -> _text = str_replace( $matches_img[0][$i], $out , $this -> _text );
            }
          }
        }
      }
    }
  }

  public function drawPrint()
  {
    $out = $this -> _title.", ".date( "d/m/Y H:i" , strtotime( $this -> _date_modify ) );
    $out .= $this -> getModifyText( false );
    return $out;
  }
}
?>