<?php
error_reporting( E_ALL ^ E_NOTICE ^ E_STRICT ^ E_WARNING ^ E_DEPRECATED );
function __autoload_my_classes( $classname )
{
  $q = explode( '\\' , $classname );
  $c = array_pop( $q );
  $f = 'autoload/' . implode( '/' , $q ) . '/class.' . $c . '.php';

  if ( file_exists( $f ) )
    require_once( $f );
}
spl_autoload_register( '__autoload_my_classes' );
date_default_timezone_set( 'Europe/Warsaw' );

require_once 'config.php';
require_once 'libraries/medoo/medoo.php';

session_start();

$mdb = new medoo( [
                    'database_type' => 'mysql',
                    'database_name' => $database['name'],
                    'server'        => $database['host'],
                    'username'      => $database['user'],
                    'password'      => $database['password'],
                    'charset'       => 'utf8'
                ] );

if ( !$lang_id = \S::get_session( 'current-lang' ) )
{
  $lang_id = \front\factory\Languages::default_language();
  \S::set_session( 'current-lang', $lang_id );
}

if ( !$lang = \S::get_session( 'lang' ) )
{
  $lang = \front\factory\Languages::lang_translations();
  \S::set_session( 'lang', $lang );
}

\front\controls\Site::check_url_params();

if ( !$settings = \S::get_session( 'settings' ) )
{
  $settings = \front\factory\Settings::settings_details();
  \S::set_session( 'settings', $settings );
}

if ( \S::get( 'a' ) == 'calendar' )
{
  echo \front\view\Site::calendar( \S::get( 'month' ), \S::get( 'year' ), true );
  exit;
}

if ( \S::get( 'a' ) == 'widget-phone-now' )
{
  $response = [ 'status' => 'error' ];

  if ( \S::get( 'phone' ) )
  {
    if ( \S::send_email(
            $settings['contact_email'],
            'Prośba o oddzwonienie',
            '<p>Nr telefonu: ' . \S::get( 'phone' ) . '</p>',
            $settings['contact_email']
          ) )
    $response = [ 'status' => 'ok' ];
  }
  echo json_encode( $response );
  exit;
}

if ( \S::get( 'a' ) == 'contact_form' )
{
  $response = [ 'status' => 'error' ];

  if ( \S::get( 'email' ) )
  {
    if ( \S::send_email(
            $settings['contact_email'],
            \S::get( 'subject' ),
            '<p>Nr telefonu: ' . \S::get( 'phone' ) . '</p>'
            . '<p>Email: ' . \S::get( 'email' ) . '</p>'
            . '<p>' . \S::get( 'text' ) . '</p>',
            \S::get( 'email' )
          ) )
       {
          $mdb -> insert( 'pp_contact_emails', [
            'email' => \S::get( 'email' ),
            'phone' => \S::get('phone' ),
            'title' => \S::get( 'subject' ),
            'mail'  => \S::get( 'text' ),
            'add_date' => date("Y-m-d, H:i")
             ]);

          $response = [ 'status' => 'ok' ];
       }
  }
  echo json_encode( $response );
  exit;
}
if ( \S::get( 'a' ) == 'cookie_close' )
{
  setcookie( "cookie_information", "true", mktime( 0, 0, 0, 12, 31, 2115 ) );
  exit;
}

if ( \S::get( 'a' ) == 'banner_close' )
{
  \S::set_session( 'banner_close', true );
  exit;
}

if ( \S::get( 'a' ) == 'contrast' )
{
  \S::set_session( 'contrast', !\S::get_session( 'contrast' ) );
  exit;
}

if ( \S::get( 'a' ) == 'article_unlock' )
{
  \front\controls\Articles::article_unlock(
            \S::get( 'password' ),
            \S::get( 'article_id' )
          );
  exit;
}

if ( \S::get( 'a' ) == 'pixieset_gallery_download' ) {
  $url = \front\controls\Articles::images_download( \S::get( 'hash' ) );
  echo json_encode( [ 'url' => $url ] );
  exit;
}

if ( \S::get( 'a' ) == 'pixieset_image_favorite' ) {
  $status = \front\controls\Articles::pixieset_image_favorite( \S::get( 'image_id' ), \S::get( 'hash' ) );
  echo json_encode( [ 'status' => $status ] );
  exit;
}

if ( \S::get( 'a' ) == 'pixieset_save_favorite_images' ) {
  $result = \front\controls\Articles::pixieset_save_favorite_images( \S::get( 'hash' ) );
  echo json_encode( [ 'status' => $result ] );
  exit;
}

if ( \S::get( 'a' ) == 'inline-edit-save' )
{
  if ( strpos( \S::get( 'element_id' ), 'scontainer' ) !== false )
  {
    $scontainer_id = str_replace( [ 'scontainer-', '-content' ], [ '', '' ], \S::get( 'element_id' ) );
    \Scontainer::updateByFrontEditor( $scontainer_id, \S::get( 'element_value' ), \S::get( 'lang_id' ) );
    \S::delete_cache();
    exit;
  }
  else
  {
    if ( \S::get( 'alltemplates' ) )
      $layouts = $mdb -> select( 'pp_layouts', 'id' );
    else
      $layouts[] = \S::get( 'layout_id' );

    foreach ( $layouts as $layout_id )
    {
      $layout = \front\factory\Layouts::layout_details( $layout_id );
      $html = $layout['html'];

      if ( strpos( $html, '[META_INDEX]' ) !== false )
      {
        $meta_index = true;
        $html = str_replace( '[META_INDEX]', '', $html );
      }

      if ( strpos( $html, '[CSS]' ) !== false )
      {
        $css = true;
        $html = str_replace( '[CSS]', '', $html );
      }

      if ( strpos( $html, '[JAVA_SCRIPT]' ) !== false )
      {
        $java_script = true;
        $html = str_replace( '[JAVA_SCRIPT]', '', $html );
      }

      $html = preg_replace( '/^[ \t]*[\r\n]+/m', '', $html );

      \S::suSetHtmlById( $html, \S::get( 'element_id' ), \S::get( 'element_value' ) );

      if ( $meta_index )
        $html = str_replace( '</head>', '    [META_INDEX]' . PHP_EOL . '</head>', $html );

      if ( $css )
        $html = str_replace( '</head>', '    [CSS]' . PHP_EOL . '</head>', $html );

      if ( $java_script )
        $html = str_replace( '</head>', '    [JAVA_SCRIPT]' . PHP_EOL . '</head>', $html );

      $html = str_replace( [ '%5B', '%5D', '%20', '%24' ], [ '[', ']', ' ', '$' ], $html );

      $mdb -> update( 'pp_layouts', [ 'html' => $html ], [ 'id' => $layout_id ] );
    }
  }

  \S::delete_cache();
}
?>
