<?
$xajax = new xajax();
$xajax -> register( XAJAX_FUNCTION , 'sendContactForm' );
$xajax -> register( XAJAX_FUNCTION , 'sendGuestBookEntry' );
$xajax -> register( XAJAX_FUNCTION , 'changeLang');
$xajax -> register( XAJAX_FUNCTION , 'checkRegisterForm' );
$xajax -> register( XAJAX_FUNCTION , 'registerUser' );
$xajax -> register( XAJAX_FUNCTION , 'recoverPassword' );
$xajax -> register( XAJAX_FUNCTION , 'loginUser' );
$xajax -> register( XAJAX_FUNCTION , 'logout' );
$xajax -> register( XAJAX_FUNCTION , 'closeAdvert' );

if ( isset( $user ) && is_object( $user ) && get_class( $user ) == 'user\User' && $user -> get_admin() )
{
  $xajax -> register( XAJAX_FUNCTION , 'saveArticleTranslation' );
  $xajax -> register( XAJAX_FUNCTION , 'saveLayout' );
  $xajax -> register( XAJAX_FUNCTION , 'sendNewsletter' );
  $xajax -> register( XAJAX_FUNCTION , 'saveArticle' );
  $xajax -> register( XAJAX_FUNCTION , 'saveArticlesOrder' );
  $xajax -> register( XAJAX_FUNCTION , 'deleteArticleImg' );
  $xajax -> register( XAJAX_FUNCTION , 'moveArticlesToArchive' );
  $xajax -> register( XAJAX_FUNCTION , 'deleteArticles' );
}
$xajax -> processRequest();
$xajax -> configure( 'javascript URI', '../resources/xajax/' );

function deleteArticles( $articles )
{
  $articles = explode( ':', $articles );
  if ( is_array( $articles ) ) foreach ( $articles as $art )
   \admin\factory\ArticleArchiveManager::delete( $art );

  $objResponse  = new xajaxResponse();
  $objResponse -> script( 'document.location.href="./";' );
  return $objResponse;
}

function moveArticlesToArchive( $articles )
{
  $articles = explode( ':', $articles );
  if ( is_array( $articles ) ) foreach ( $articles as $art )
   \admin\factory\ArticleManager::setAsArchive( $art );

  $objResponse  = new xajaxResponse();
  $objResponse -> script( 'document.location.href="./";' );
  return $objResponse;
}

function closeAdvert()
{
  \System::setSessionVar( 'advert_close' , true );
}

function deleteArticleImg( $id )
{
  global $db;

  $query = $db -> prepare( 'SELECT * FROM pcms_article_images WHERE id = :id' );
  $query -> bindValue( ':id' , $id , \PDO::PARAM_INT );
  $query -> execute();
  if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
  {
    if ( file_exists( "../" . $row['src'] ) )
      unlink(  "../" . $row['src'] );
  }
  $query -> closeCursor();

  $query = $db -> prepare( 'DELETE FROM pcms_article_images WHERE id = :id' );
  $query -> bindValue( ':id' , $id , \PDO::PARAM_INT );
  $query -> execute();
  $query -> closeCursor();

  $objResponse  = new xajaxResponse();
  $objResponse -> script( '$( "#li_' . $id . '").remove();' );
  return $objResponse;
}

function saveArticlesOrder( $id , $order )
{
  global $db;

  $order = stripslashes( $order );
  $order = str_replace( '"', '', $order );
  $order = str_replace( ']', '', $order );
  $order = str_replace( '[', '', $order );
  $order = explode( ',', $order );

  $i = 1;
  if ( is_array( $order ) ) foreach ( $order as $o )
  {
    $query = $db -> prepare( 'UPDATE pcms_article_page SET o = :o WHERE id = :id' );
    $query -> bindValue( ':id' , $o  , \PDO::PARAM_INT );
    $query -> bindValue( ':o'  , $i  , \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();
    $i++;
  }

  \System::deleteCacheAdmin();
  \System::deleteCache();

  $objResponse  = new xajaxResponse();
  $message = '$(".jqibox").css("display","none");';
  $objResponse -> script( $message );
  return $objResponse;
}

function logout()
{
  \System::deleteSessionVar( 'user' );
  $objResponse  = new xajaxResponse();
  $message      = 'document.location.href="' . \System::getMainPage() . '";';
  $objResponse -> script( $message );
  return $objResponse;
}

function loginUser( $login , $password ) {
  global $db, $lang;

  $objResponse  = new xajaxResponse();
  $login        = \System::saveString( $login , true );
  $password     = \System::saveString( $password , true );

  $data = array(
      'table'     => 'pcms_user',
      'fields'    => 'id',
      'condition' => 'login = "' . \mySQL::sec( $login ) . '" AND password = "' . md5( $password ) . '" AND enabled = "1"'
  );
  $result = $dbs -> get( $data , false , true );

  $query = $db -> prepare( 'SELECT * FROM pcms_user WHERE login = :login AND password = :password AND enabled = 1' );
  $query -> bindValue( ':login',    mysql_real_escape_string( $login ), \PDO::PARAM_STR );
  $query -> bindValue( ':password', md5( $password ),                   \PDO::PARAM_STR );
  $query -> execute();
  if ( $query -> rowCount() )
  {
    $user = \user\FUser::login( $login , $password );
    \System::setSessionVar( 'user' , $user );
    $message = 'document.location.href="' . \System::getMainPage() . '";';
  }
  else
  {
    $message = '$(".jqibox").css("display","none");';
    $message .= "$.prompt('" . $lang -> getTrans( 'T_BRAK_UZYTKOWNIKA_KONTO_NIEAKTYWNE' ) . "',{ buttons: { " . $lang -> getTrans( 'T_ZAMKNIJ' ) . ": true }, focus: 1 });";
    $message .= 'setTimeout(function(){$(".jqibox").css("display","none");},3000);';
  }
  $query -> closeCursor();

  $objResponse -> script( $message );

  return $objResponse;
}

function recoverPassword( $email )
{
  global $db , $lang;

  $objResponse  = new xajaxResponse();
  $message      = '';
  $hash         = \System::gen_hash( 16 );
  $email        = \System::saveString( $email , true );

  if ( !\System::checkEmail( $email ) )
  {
    $message = '$(".jqibox").css("display","none");';
    $message .= "$.prompt('" . $lang -> getTrans( 'T_NIEPRAWIDLOWY_EMAIL_REJESTRACJA' ) . "',{ buttons: { " . $lang -> getTrans( 'T_ZAMKNIJ' ) . ": true }, focus: 1 });";
    $message .= 'setTimeout(function(){$(".jqibox").css("display","none");},3000);';
  }
  else
  {
    if ( !System::isEmailFree( $email ) )
    {
      $query = $db -> prepare( 'UPDATE pcms_user SET recover = :recover WHERE email = :email' );
      $query -> bindValue( ':recover' , $hash   , \PDO::PARAM_STR );
      $query -> bindValue( ':email'   , $email  , \PDO::PARAM_STR );
      $query -> execute();
      $query -> closeCursor();

      $query = $db -> prepare( 'SELECT id FROM pcms_user WHERE email = :email' );
      $query -> bindValue( ':email' , $email , \PDO::PARAM_STR );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        $user_id = $row['id'];
      $query -> closeCursor();

      $link = "<a href='http://" . $_SERVER['SERVER_NAME'] . "/index.php?recover_password=" . $hash . "&value=" . $user_id . "'>" . $lang -> getTrans( 'T_RESETUJ' ) . "</a>";

      $tresc = str_replace( '{LINK}'    , $link                                     , $lang -> getTrans( 'T_ODZYSKIWANIE_TRESC_UZYTKOWNIK' ) );
      $tresc = str_replace( '{SERWER}'  , \System::getSystemSettings( 'firm_name' ) , $tresc );

      \System::sendEmail(
              $email ,
              str_replace( '{SERWER}' , \System::getSystemSettings( 'firm_name' ) , $lang -> getTrans( 'T_ODZYSKIWANIE_HASLA' ) ),
              $tresc
            );

      $message = '$(".jqibox").css("display","none");';
      $message .= "$.prompt('" . $lang -> getTrans( 'T_ODZYSKIWANIE_HASLA_ALERT' ) . "',{ buttons: { " . $lang -> getTrans( 'T_ZAMKNIJ' ) . ": true }, focus: 1 });";
      $message .= 'setTimeout(function(){$(".jqibox").css("display","none");},3000);';
    }
    else
    {
      $message = '$(".jqibox").css("display","none");';
      $message .= "$.prompt('" . $lang -> getTrans( 'T_BRAK_EMAILA_W_BAZIE' ) . "',{ buttons: { " . $lang -> getTrans( 'T_ZAMKNIJ' ) . ": true }, focus: 1 });";
      $message .= 'setTimeout(function(){$(".jqibox").css("display","none");},3000);';
    }
  }
  $objResponse -> script( $message );

  return $objResponse;
}

function registerUser( $login , $password , $email )
{
  global $db , $lang;

  $login    = \System::saveString( $login , true );
  $password = \System::saveString( $password , true );
  $email    = \System::saveString( $email , true );
  $hash     = \System::gen_hash( 16 );

  $c_login    = \System::checkLoginRegister( $login );
  $c_password = \System::checkPasswordRegister( $password , $password );
  $c_email    = \System::checkEmailRegister( $email );

  $objResponse = new xajaxResponse();

  if ( $c_login == 0 && $c_password == 0 && $c_email == 0 && \System::getSystemSettings( 'register' ) != 0 )
  {
    $query = $db -> prepare( 'INSERT INTO pcms_user ( login , password , email , registered , hash ) VALUES ( :login , :password , :email , :registered , :hash )' );
    $query -> bindValue( ':login'       , $login              , \PDO::PARAM_STR );
    $query -> bindValue( ':password'    , md5( $password )    , \PDO::PARAM_STR );
    $query -> bindValue( ':email'       , $email              , \PDO::PARAM_STR );
    $query -> bindValue( ':registered'  , \System::getDate()  , \PDO::PARAM_STR );
    $query -> bindValue( ':hash'        , $hash               , \PDO::PARAM_STR );
    $query -> execute();
    $query -> closeCursor();

    $user_id = $db -> lastInsertId();

    if ( \System::getSystemSettings( 'register' ) == 1 )
    {

      \System::sendEmail(
              \System::getSystemSettings( 'admin_email' ) ,
              str_replace( '{SERWER}' , \System::getSystemSettings( 'firm_name' ) , $lang -> getTrans( 'T_REJESTRACJA_UZYTKOWNIKA' ) ),
              str_replace( '{LOGIN}'  , $login                                    , $lang -> getTrans( 'T_REJESTRACJA_TRESC_ADMIN' ) )
            );

      $tresc = str_replace( '{LOGIN}'   , $login                                    , $lang -> getTrans( 'T_REJESTRACJA_TRESC_UZYTKOWNIK' ) );
      $tresc = str_replace( '{SERWER}'  , \System::getSystemSettings( 'firm_name' ) , $tresc );
      $tresc = str_replace( '{HASLO}'   , $password                                 , $tresc );

      \System::sendEmail(
              $email ,
              str_replace( '{SERWER}' , \System::getSystemSettings( 'firm_name' ) , $lang -> getTrans( 'T_REJESTRACJA_UZYTKOWNIKA' ) ),
              $tresc
            );
    }
    else if ( \System::getSystemSettings( 'register' ) == 2 )
    {

      $link = "<a href='http://" . $_SERVER['SERVER_NAME'] . "/index.php?account_activation=" . $hash . "&value=" . $user_id . "'>" . $lang -> getTrans( 'T_AKTYWUJ' ) . "</a>";

      $tresc = str_replace( '{LOGIN}'   , $login                                    , $lang -> getTrans( 'T_REJESTRACJA_TRESC_UZYTKOWNIK_AKTYWACJA' ) );
      $tresc = str_replace( '{SERWER}'  , \System::getSystemSettings( 'firm_name' ) , $tresc );
      $tresc = str_replace( '{HASLO}'   , $password                                 , $tresc );
      $tresc = str_replace( '{LINK}'    , $link                                     , $tresc );

      \System::sendEmail(
              $email ,
              str_replace( '{SERWER}' , \System::getSystemSettings( 'firm_name' ) , $lang -> getTrans( 'T_REJESTRACJA_UZYTKOWNIKA' ) ),
              $tresc
            );
    }

    $message = '$(".jqibox").css("display","none");';
    $message .= "$.prompt('" . $lang -> getTrans( 'T_REJESTRACJA_SUKCES' ) . "',{ buttons: { " . $lang -> getTrans( 'T_ZAMKNIJ' ) . ": true }, focus: 1 });";
    $message .= 'setTimeout(function(){$(".jqibox").css("display","none");},3000);';
  }
  else
  {
    $message = '$(".jqibox").css("display","none");';
    $message .= "$.prompt('" . $lang -> getTrans( 'T_BLEDY_REJESTRACJA_FORMULARZ' ) . "',{ buttons: { " . $lang -> getTrans( 'T_ZAMKNIJ' ) . ": true }, focus: 1 });";
    $message .= 'setTimeout(function(){$(".jqibox").css("display","none");},3000);';
  }
  $objResponse -> script( $message );

  return $objResponse;
}

function checkRegisterForm( $param , $value , $value2 = '' )
{
  global $lang;

  $value  = \System::saveString( $value   , true );
  $value2 = \System::saveString( $value2  , true );

  if ( $param == 'login' )
  {
    $r_login = false;
    $c_login = \System::checkLoginRegister( $value );

    if ( $c_login == 1 )
      $out = '<div class="alert">' . $lang -> getTrans( 'T_NIEPRAWIDLOWY_LOGIN_REJESTRACJA' ) . '</div>';
    else if ( $c_login == 2 )
      $out = '<div class="alert">' . $lang -> getTrans( 'T_LOGIN_ZAJETY' ) . '</div>';
    else if ( $c_login == 0 )
      $r_login = true;
    else
      $out = '<div class="alert">' . $lang -> getTrans( 'T_NIEPRAWIDLOWY_LOGIN_REJESTRACJA' ) . '</div>';
  }

  if ( $param == 'password' )
  {
    $r_password = false;
    $c_password = \System::checkPasswordRegister( $value , $value2 );

    if ( $c_password == 1 )
      $out = '<div class="alert">' . $lang -> getTrans( 'T_NIEPRAWIDLOWE_HASLO_REJESTRACJA' ) . '</div>';
    else if ( $c_password == 2 )
      $out = '<div class="alert">' . $lang -> getTrans( 'T_HASLA_ROZNE' ) . '</div>';
    else if ( $c_password == 0 )
      $r_password = true;
    else
      $out = '<div class="alert">' . $lang -> getTrans( 'T_NIEPRAWIDLOWE_HASLO_REJESTRACJA' ) . '</div>';
  }

  if ( $param == 'email' )
  {
    $r_email    = false;
    $c_password = \System::checkEmailRegister( $value );

    if ( $c_password == 1 )
      $out .= '<div class="alert">' . $lang -> getTrans( 'T_NIEPRAWIDLOWY_EMAIL_REJESTRACJA' ) . '</div>';
    else if ( $c_password == 2 )
      $out = '<div class="alert">' . $lang -> getTrans( 'T_EMAIL_ZAJETY' ) . '</div>';
    else if ( $c_password == 0 )
      $r_email = true;
    else
      $out .= '<div class="alert">' . $lang -> getTrans( 'T_NIEPRAWIDLOWY_EMAIL_REJESTRACJA' ) . '</div>';
  }


  $objResponse = new xajaxResponse();
  $objResponse -> script( '$(".jqibox").css("display","none");' );
  $objResponse -> assign( "r_" . $param . "_check" , 'innerHTML' , $out );
  if ( $param == "login" )
    $objResponse -> script( '$( "#r_login" ).val( "' . $value . '");' );

  if ( $param == 'login' && $r_login )
    $objResponse -> script( 'r_login = true; $( "#r_password" ).focus();' );
  else if ( $param == "login" )
    $objResponse -> script( 'r_login = false; $( "#r_login" ).focus();' );
  if ( $param == 'password' && $r_password )
    $objResponse -> script( 'r_password = true; $( "#r_email" ).focus();' );
  else if ( $param == "password" )
    $objResponse -> script( 'r_password = false; $( "#r_password" ).focus();' );
  if ( $param == 'email' && $r_email )
    $objResponse -> script( 'r_email = true' );
  else if ( $param == "email ")
    $objResponse -> script( 'r_email = false; $( "#r_email" ).focus();' );

  return $objResponse;
}

function changeLang( $lang_id )
{
  global $sys;

  $sys -> deleteSessionVar('lang');
  $objResponse = new xajaxResponse();
  $lang = \language\FLanguage::loadLang($lang_id, true);
  $sys -> setSessionVar( 'lang' , $lang , true );

  $objResponse -> script( "document.location.href='index.php';" );
  return $objResponse;
}

function saveArticle( $id, $enabled, $comments, $print, $show_title, $show_date, $pages , $show_author , $keep_archive, $layout )
{
  global $db, $user;

  $new = false;

  if ( $id )
  {
    $sql = 'UPDATE
                pcms_article
              SET
                show_title = :show_title,
                enabled = :enabled,
                print = :print,
                show_date = :show_date,
                date_modify = :date_modify,
                show_author = :show_author,
                keep_archive = :keep_archive,
                id_layout = :id_layout
              WHERE
                id = :id';
    $query = $db -> prepare( $sql );
    $query -> bindValue( ':show_title'    , $show_title    , \PDO::PARAM_STR );
    $query -> bindValue( ':enabled'       , $enabled       , \PDO::PARAM_STR );
    $query -> bindValue( ':print'         , $print         , \PDO::PARAM_STR );
    $query -> bindValue( ':show_date'     , $show_date     , \PDO::PARAM_STR );
    $query -> bindValue( ':show_author'   , $show_author   , \PDO::PARAM_STR );
    $query -> bindValue( ':keep_archive'  , $keep_archive  , \PDO::PARAM_STR );
    $query -> bindValue( ':id'            , $id            , \PDO::PARAM_INT );
    $query -> bindValue( ':id_layout'     , $layout        , \PDO::PARAM_INT );
    $query -> bindValue( ':date_modify'   , \System::getDate()  , \PDO::PARAM_STR );
    $query -> execute();
    $query -> closeCursor();
  }
  else
  {
    $sql = 'INSERT INTO
                pcms_article
                  ( show_title , enabled , print , show_date , date_add , date_modify , show_author , author , keep_archive, id_layout )
              VALUES
                  ( :show_title , :enabled , :print , :show_date , :date_add , :date_modify , :show_author , :author , :keep_archive, :id_layout )';
    $query = $db -> prepare( $sql );
    $query -> bindValue( ':show_title'    , $show_title         , \PDO::PARAM_STR );
    $query -> bindValue( ':enabled'       , $enabled            , \PDO::PARAM_STR );
    $query -> bindValue( ':print'         , $print              , \PDO::PARAM_STR );
    $query -> bindValue( ':show_date'     , $show_date          , \PDO::PARAM_STR );
    $query -> bindValue( ':show_author'   , $show_author        , \PDO::PARAM_STR );
    $query -> bindValue( ':author'        , $user -> get_id()   , \PDO::PARAM_STR );
    $query -> bindValue( ':keep_archive'  , $keep_archive       , \PDO::PARAM_STR );
    $query -> bindValue( ':id_layout'     , $layout             , \PDO::PARAM_INT );
    $query -> bindValue( ':date_add'      , \System::getDate()  , \PDO::PARAM_STR );
    $query -> bindValue( ':date_modify'   , \System::getDate()  , \PDO::PARAM_STR );
    $query -> execute();
    $query -> closeCursor();

    $id   = $db -> lastInsertId();

    $query = $db -> query( 'SELECT * FROM pcms_article_images WHERE id_article = 0' );
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      $dir   = 'upload/galeries/article_' . $id  . '/';
      if ( !is_dir( "../ " . $dir ) )
        mkdir( "../" . $dir , 0755 , true );

      $file = explode( '/', $row['src'] );
      $file = $file[3];

      if ( file_exists( '../' . $row['src'] ) )
      {
        rename( '../' . $row['src'], '../' . $dir . $file );
        $query2 = $db -> prepare( 'UPDATE pcms_article_images SET id_article = :id_article, src = :src WHERE id = :id' );
        $query2 -> bindValue( ':id_article', $id,          \PDO::PARAM_INT );
        $query2 -> bindValue( ':src',        $dir . $file, \PDO::PARAM_STR );
        $query2 -> bindValue( ':id',         $row['id'],   \PDO::PARAM_INT );
        $query2 -> execute();
        $query2 -> closeCursor();
      }
    }
    $query -> closeCursor();

    $new  = true;
  }

  $pages  = explode(':', $pages);
  $not_in = '';

  if ( is_array( $pages ) ) foreach ( $pages as $pag )
  {
    if ( $not_in )
      $not_in .= ',';
    $not_in .= $pag;
  }

  if ( $not_in )
  {
    $query = $db -> prepare( 'DELETE FROM pcms_article_page WHERE article_id = :article_id AND page_id NOT IN (' . $not_in . ')' );
    $query -> bindValue( ':article_id' , (int)$id , \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();
  }

  $query = $db -> prepare( 'SELECT page_id FROM pcms_article_page WHERE article_id = :article_id' );
  $query -> bindValue( ':article_id' , (int)$id , \PDO::PARAM_INT );
  $query -> execute();
  if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    $tab_tmp[] = $row['page_id'];
  $query -> closeCursor();

  $query  = $db -> prepare( 'INSERT INTO pcms_article_page ( page_id, article_id, o ) VALUES ( :page_id, :article_id, :o )' );
  foreach ( $pages as $pag )
  {
    if ( !is_array( $tab_tmp ) || !in_array( $pag , $tab_tmp ) )
    {
      $o = \admin\factory\ArticleManager::getMaxArticleOrder( $pag ) + 1;
      $query -> bindValue( ':page_id'     , $pag     , \PDO::PARAM_INT );
      $query -> bindValue( ':article_id'  , (int)$id , \PDO::PARAM_INT );
      $query -> bindValue( ':o'           , $o       , \PDO::PARAM_INT );
      $query -> execute();
      $o++;
    }
  }
  $query -> closeCursor();

  \System::rewriteHtacces();
  \System::deleteCacheAdmin();

  $objResponse = new xajaxResponse();
  $objResponse -> script( '$( "#idk" ).val( ' . $id . ' ); saveArticleTranslation( ' . $new . ');' );
  return $objResponse;
}

function saveArticleTranslation( $lang_id , $id , $title , $meta_description , $meta_keywords , $text , $message , $keep_archive, $version, $seo_link, $new )
{
  global $db;

  if ( is_null( $version ) )
    $version = 1;

  $query = $db -> prepare( 'SELECT id, version FROM pcms_article_translation WHERE article_id = :article_id AND lang_id = :lang_id ORDER BY version DESC LIMIT 1' );
  $query -> bindValue( ':article_id'  , $id       , \PDO::PARAM_INT );
  $query -> bindValue( ':lang_id'     , $lang_id  , \PDO::PARAM_STR );
  $query -> execute();
  if ( $query -> rowCount() && !$keep_archive )
  {
    while ( $row = $query -> fetch() )
    {
      $sql = 'UPDATE
                pcms_article_translation
              SET
                text = :text, meta_description = :meta_description, meta_keywords = :meta_keywords, title = :title, seo_link = :seo_link
              WHERE
                article_id = :article_id AND lang_id = :lang_id AND version = :version';
      $query2 = $db -> prepare( $sql );
      $query2 -> bindValue( ':text'             , stripslashes( $text )             , \PDO::PARAM_STR );
      $query2 -> bindValue( ':meta_description' , stripslashes( $meta_description ) , \PDO::PARAM_STR );
      $query2 -> bindValue( ':meta_keywords'    , stripslashes( $meta_keywords )    , \PDO::PARAM_STR );
      $query2 -> bindValue( ':title'            , stripslashes( $title )            , \PDO::PARAM_STR );
      $query2 -> bindValue( ':lang_id'          , $lang_id                          , \PDO::PARAM_STR );
      $query2 -> bindValue( ':article_id'       , $id                               , \PDO::PARAM_INT );
      $query2 -> bindValue( ':version'          , $row['version']                   , \PDO::PARAM_INT );
      $query2 -> bindValue( ':seo_link'         , $seo_link                         , \PDO::PARAM_STR );
      $query2 -> execute();
      $query2 -> closeCursor();
    }
  }
  else
  {
    $sql = 'INSERT INTO
              pcms_article_translation
                ( text, meta_description, meta_keywords, title, article_id, lang_id, version, seo_link )
            VALUES
                ( :text, :meta_description, :meta_keywords, :title, :article_id, :lang_id, :version, :seo_link )';
    $query2 = $db -> prepare( $sql );
    $query2 -> bindValue( ':text'             , stripslashes( $text )             , \PDO::PARAM_STR );
    $query2 -> bindValue( ':meta_description' , stripslashes( $meta_description ) , \PDO::PARAM_STR );
    $query2 -> bindValue( ':meta_keywords'    , stripslashes( $meta_keywords )    , \PDO::PARAM_STR );
    $query2 -> bindValue( ':title'            , stripslashes( $title )            , \PDO::PARAM_STR );
    $query2 -> bindValue( ':seo_link'         , $seo_link         , \PDO::PARAM_STR );
    $query2 -> bindValue( ':lang_id'          , $lang_id          , \PDO::PARAM_STR );
    $query2 -> bindValue( ':article_id'       , $id               , \PDO::PARAM_INT );
    $query2 -> bindValue( ':version'          , $version          , \PDO::PARAM_INT );
    $query2 -> execute();
    $query2 -> closeCursor();
  }

  if ( $message )
  {
    $objResponse = new xajaxResponse();
    $objResponse -> script( '$( "#version" ).val(' . ( $version + 1 ) . ');' );
    $objResponse -> script( '$(".jqibox").css("display","none");' );
    if ( $new )
      $objResponse -> script( 'document.location.href="./?rw=edit&id=' . $id . '";' );
    return $objResponse;
  }
}

function sendGuestBookEntry( $signature , $text , $code , $check , $val_check )
{
  global $lang , $securimage;

  if ( $val_check != \System::getSessionVar( 'token' ) )
    return false;

  $signature  = \System::saveString( $signature , true );
  $text       = \System::saveString( $text , true );

  if ( $signature && $text && $code )
  {
    $signature = '~' . $signature;

    if ( $check != \System::getSessionVar( 'guest_book_time' ) )
    {
      $date = \System::getSessionVar( 'guest_book_time' );
      if ( !$date )
        $date = "1980-00-00 00:00:00";
      if ( \System::getDateDiff( $date , date( 'Y-m-d H:i:s' ) ) > 1 )
      {
        if ( $securimage -> check( $code ) == false )
          $out = $lang -> getTrans( 'T_NIEPRAWIDLOWY_KOD' );
        else
        {
          \guestbook\FGuestBook::addEntry( $signature , $text );
          \System::setSessionVar( 'guest_book_time' , $check );
          \System::deleteCache();
          $out = $lang -> getTrans( 'T_DZIEKUJEMY_ZA_WPIS' );
        }
      }
      else
        $out = $lang -> getTrans( 'T_ZACZEKAJ_MIN_WPIS' );
    }
    else
      $out = $lang -> getTrans( 'T_TEN_WPIS_ZOSTAL_JUZ_DODANY' );
  }
  else
    $out = $lang -> getTrans( 'T_BRAK_WSZYSTKICH_DANYCH' );

  $objResponse  = new xajaxResponse();
  $message      = '$(".jqibox").css("display","none");';
  $message      .= "$.prompt('" . $out . "',{ buttons: { " . $lang -> getTrans( 'T_ZAMKNIJ' ) . ": true }, focus: 1 });";
  $message      .= 'setTimeout(function(){$(".jqibox").css("display","none");},3000);';
  $objResponse -> script( $message );
  return $objResponse;
}

function sendContactForm( $email , $subject , $text , $check , $val_check )
{
  global $lang;

  $email    = \System::saveString( $email , true );
  $subject  = \System::saveString( $subject , true );
  $text     = \System::saveString( $text , true );

  if ( $val_check != \System::getSessionVar( 'token' ) )
    return false;

  if ( \System::checkEmail( $email ) && $email && $subject && $text )
  {
    if ( $check != \System::getSessionVar( 'contact_time' ) )
    {
      $date = \System::getSessionVar( 'contact_time' );
      if ( !$date )
        $date = "1980-00-00 00:00:00";
      if ( \System::getDateDiff( $date , date( 'Y-m-d H:i:s' ) ) > 1 )
      {
        $admin_email = \System::getSystemSettings( 'admin_email' );
        \System::sendEmail( $admin_email , $subject , $text , $email );
        \System::setSessionVar( 'contact_time' , $check );
        $out = $lang -> getTrans( 'T_ZAPYTANIE_ZOSTALO_WYSLANE' );
      }
      else
        $out = $lang -> getTrans( 'T_ZACZEKAJ_MIN_WIADOMOSC' );
    }
    else
      $out = $lang -> getTrans( 'T_TEN_FORMULARZ_BYL_JUZ_WYSLANY' );
  }
  else
  {
    if ( \System::checkEmail( $email ) )
      $out = $lang -> getTrans( 'T_NIEPOPRAWNY_ADRES_EMAIL' );
    else
      $out = $lang -> getTrans( 'T_BRAK_WSZYSTKICH_DANYCH' );
  }

  $objResponse = new xajaxResponse();
  $message = '$(".jqibox").css("display","none");';
  $message .= "$.prompt('" . $out . "',{ buttons: { " . $lang -> getTrans( 'T_ZAMKNIJ' ) . ": true }, focus: 1 });";
  $message .= 'setTimeout(function(){$(".jqibox").css("display","none");},3000);';
  $objResponse -> script( $message );
  return $objResponse;
}

function saveLayout( $id , $name , $enabled , $html , $css , $js , $pages )
{
  global $sys , $lang;

  $pages = explode( ':' , $pages );

  \admin\factory\LayoutManager::save( $id , $name , $enabled , $html , $css , $js , true , $pages );
  $objResponse = new xajaxResponse();
  $message = '$(".jqibox").css("display","none");';
  $objResponse -> script( $message );

  \System::deleteCacheAdmin();
  return $objResponse;
}
?>