<?php
class DataBase
{
  public function SaveData( $db_edit_table , $db_edit_key , $db_edit_val , $db_edit_pols , $db_edit_pass , $db_edit_pols_t )
  {
    global $sys , $lang , $db;

    $sql = '';

    if ( is_array( $db_edit_pols ) ) foreach ( $db_edit_pols as $val )
    {
      if ( $sql && $val != 'image' )
        $sql .= ',';
      if ( $val != 'image' )
        $sql .= $val . "='" . addslashes( \S::saveString( \S::get( $val ) ) ) . "'";
    }

    if ( $db_edit_pass )
    {
      $name   = $db_edit_pass['nazwa1'];
      $pass1  = \S::saveString( \S::get( $name ) );
      $pass2  = \S::saveString( \S::get( $name . '_repeat' ) );
      if ( $pass1 != $pass2 )
        \S::alert( $lang -> getTrans( 'T_HASLA_ROZNE' ) );
      else
      {
        if ( strlen($pass1) >= 5 )
        {
          if ( $sql )
            $sql .= ',';
          $sql .= $name . "='" . md5($pass1) . "'";
        }
        else
        {
          if ( strlen( $pass1 ) < 5 )
            \S::alert( $lang -> getTrans( 'T_HASLA_ZA_KROTKIE' ) );
        }
      }
    }

    if ( $db_edit_table && $db_edit_key && $db_edit_val && $db_edit_pols )
    {
      $db_edit_table  = \S::saveString( $db_edit_table );
      $db_edit_key    = \S::saveString( $db_edit_key );
      $db_edit_val    = \S::saveString( $db_edit_val );

      $query = $db -> query( "UPDATE " . $db_edit_table . " SET " . $sql . " WHERE " . $db_edit_key . "='" . $db_edit_val . "'" );

      for ( $i = 0; $i < count( $db_edit_pols_t ); $i++ )
      {
        if ( $db_edit_pols_t[$i] == 'file' && is_array( $_FILES[$db_edit_pols[$i]] ) )
        {
          $file   = $_FILES[$db_edit_pols[$i]];
          if ( $file['name'] )
            $name   = explode( '.' , $file['name'] );
          if ( isset( $name ) && is_array( $name ) )
            $patch  = '../' . \S::get( 'image_folder' ) . \S::seo( $name[0]) . '.' . $name[1];

          if ( $file['type'] == 'image/pjpeg' || $file['type'] == 'image/jpg' || $file['type'] == 'image/jpeg' || $file['type'] == 'image/gif' || $file['type'] == 'image/png' )
          {
            if ( $file['size'] < 500000 )
            {
              $x = getimagesize( $file['tmp_name'] );
              if ( is_array( $x ) or $x[0] < 2)
              {
                $query = $db -> prepare( "SELECT " . $db_edit_pols[$i] . " FROM " . $db_edit_table . " WHERE " . $db_edit_key . "='" . $db_edit_val . "'" );
                $query -> execute();
                if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
                {
                  $plik = '../' . $row[0];
                  if ( file_exists( $plik ) && $row[0] )
                  unlink( $plik );
                }
                $query -> closeCursor();

                if ( file_exists( $patch ) )
                unlink( $patch );

                rename( $file['tmp_name'] , $patch );
                chmod( $patch , 0755 );
                $patch = str_replace( '../' , '' , $patch );
                $query = $db -> query( "UPDATE " . $db_edit_table . " SET " . $db_edit_pols[$i] ."='" . $patch . "' WHERE " . $db_edit_key . "='" . $db_edit_val . "'" );
              }
            }
          }
        }
      }
      \S::alert( 'Zaktualizowano wybrany element.' );
    }
    $saveFlag = true;
    \S::deleteCacheAdmin();
    \S::deleteCache();
  }

  public function addData( $db_edit_table , $db_edit_pols , $db_edit_pols_hidden , $db_edit_pols_t , $time , $db_edit_pass = '' )
  {
    global $sys , $lang , $db;

    $flag   = true;
    $idk    = '';
    $key    = '';
    $val    = '';
    $value  = '';

    if ( $time == \S::get_session( 'dbedit_add_time' ) ) return false;

    if ( is_array( $db_edit_pols ) ) foreach ( $db_edit_pols as $val )
    {
      if ( !$idk )
        $idk = $val;
      if ( $key )
        $key .= ',';
      if ( $value )
        $value .= ',';
      $key .= $val;
      $value .= "'" . \S::saveString( \S::get( $val ) ) . "'";
    }

    if ( is_array( $db_edit_pols_hidden ) ) foreach ( $db_edit_pols_hidden as $val )
    {
      if ( $key )
        $key .= ',';
      if ( $value )
        $value .= ',';
      $key .= $val;
      $value .= "'" . \S::saveString( \S::get( $val ) ) . "'";
    }

    if ( $db_edit_pass )
    {
      $name   = $db_edit_pass['nazwa1'];
      $pass1  = \S::saveString( \S::get( $name ) );
      $pass2  = \S::saveString( \S::get( $name . '_repeat' ) );
      if ( $pass1 != $pass2 )
      {
        \S::alert( $lang -> getTrans( 'T_HASLA_ROZNE' ) );
        $flag = false;
      }
      else
      {
        if ( strlen($pass1) >= 5 )
        {
          if ( $key )
            $key .= ',';
          $key .= $name;
          if ( $value )
            $value .= ',';
          $value .= "'" . md5( $pass1 ) . "'";
        }
        else
        {
          if ( $pass1 )
          {
            $flag = false;
            \S::alert( $lang -> getTrans( 'T_HASLA_ZA_KROTKIE' ) );
          }
        }
      }
    }

    if ( $db_edit_table && $db_edit_pols && $flag )
    {
    $check = true;
    $db_edit_table  = \S::saveString( $db_edit_table );
    try
    {
      $query = $db -> prepare( "INSERT INTO " . $db_edit_table . " (" . $key . ") VALUES (" . $value . ")");
      $query -> execute();
    }
    catch(PDOException $e)
    {
      $check = false;
    }

    $id = $db -> lastInsertId();
    $query -> closeCursor();

    for ( $i = 0; $i < count( $db_edit_pols_t ); $i++ )
    {
      if ( $db_edit_pols_t[$i] == 'file' && is_array($_FILES[$db_edit_pols[$i]]) )
      {
        $file   = $_FILES[$db_edit_pols[$i]];
        $name   = explode( '.' , $file['name'] );
        $patch  = '../' . \S::get( 'image_folder' ) . \S::seo( $name[0] ) . '.' . $name[1];

        if ( $file['type'] == 'image/pjpeg' || $file['type'] == 'image/jpg' || $file['type'] == 'image/jpeg' || $file['type'] == 'image/gif' || $file['type'] == 'image/png' )
        {
          if ( $file['size'] < 500000 )
          {
            $x = getimagesize( $file['tmp_name'] );
            if ( is_array( $x ) or $x[0] < 2)
            {
              if ( file_exists( $patch ) )
                unlink( $patch );

              @rename( $file['tmp_name'] , $patch );
              chmod( $patch , 0755 );
              $patch = str_replace( '../' , '' , $patch );

              $query = $db -> prepare( "UPDATE " . $db_edit_table . " SET " . $db_edit_pols[$i] . "='" . $patch . "' WHERE id='" . $id . "'" );
              $query -> execute();
              $query -> closeCursor();
            }
          }
        }
      }
    }
    if ( $check )
      \S::alert( 'Dodano nowy element.' );
    else
      \S::alert( 'Nieprawidłowe dane.' );
    $addFlag = true;
  }
  else
    \S::alert( 'Nieprawidłowe dane.' );

    \S::set_session( 'dbedit_add_time' , $time );
    \S::deleteCacheAdmin();
    \S::deleteCache();
  }
}
?>