<?php
/**
* Return templates select
* @return string
* @param string $sFileCurrent
*/
function throwCssSelect( $sFileCurrent = null ){

  if( empty( $sFileCurrent ) ){
    $sFileCurrent = $GLOBALS['config']['template'];
  }

  $oFF =& FlatFiles::getInstance( );

  $oDir = dir( DIR_TEMPLATES );
  while( false !== ( $sFileName = $oDir->read( ) ) ){
    if( is_file( DIR_TEMPLATES.$sFileName ) && $oFF->checkCorrectFile( $sFileName, 'css' ) && !ereg( 'plugins', $sFileName ) ){
      $aFiles[] = $sFileName;
    }
  }
  $oDir->close( );

  if( isset( $aFiles ) ){
    $content = null;
    sort( $aFiles );
    $iCount = count( $aFiles );
    for( $i = 0; $i < $iCount; $i++ ){
      $sSelected  = ( $sFileCurrent == $aFiles[$i] ) ? ' selected="selected"' : null;
      $content .= '<option value="'.$aFiles[$i].'"'.$sSelected.'>'.$aFiles[$i].'</option>';
    } // end for

    return $content;
  }
} // end function throwCssSelect

/**
* Saves variables to config
* @return void
* @param array  $aForm
* @param string $sFile
* @param string $sVariable
*/
function saveVariables( $aForm, $sFile, $sVariable = 'config' ){
  $aFile  = file( $sFile );
  $iCount = count( $aFile );
  $rFile  = fopen( $sFile, 'w' );

  for( $i = 0; $i < $iCount; $i++ ){
    foreach( $aForm as $sKey => $sValue ){
      if( ereg( $sVariable."\['".$sKey."'\]", $aFile[$i] ) && ereg( '=', $aFile[$i] ) ){
        $sValue = changeSpecialChars( $sValue );
        $sValue = ereg_replace( '"', '&quot;', $sValue );
        $sValue = stripslashes( $sValue );
        if( ( is_numeric( $sValue ) || preg_match( '/^(true|false|null)$/', $sValue ) == true ) && !ereg( '0[0-9]+', $sValue ) )
          $aFile[$i] = "\$".$sVariable."['".$sKey."'] = ".$sValue.";\n";
        else
          $aFile[$i] = "\$".$sVariable."['".$sKey."'] = \"".$sValue."\";\n";
      }
    } // end foreach

    fwrite( $rFile, $aFile[$i] );

  } // end for
  fclose( $rFile );
} // end function saveVariables

/**
* Log in and out actions
* @return void
* @param string $p
* @param string $sKey
* @param string $sFile
*/
function loginActions( $p, $sKey = 'bLogged', $sFile ){
  global $sLoginInfo, $sLoginPage;
  $oTpl   =& TplParser::getInstance( );
  $sCheck = 'checkContent';
  
  if( !isset( $_SESSION[$sKey] ) || $_SESSION[$sKey] !== TRUE ){
    if( $p == 'login' && isset( $_POST['sLogin'] ) && isset( $_POST['sPass'] ) ){
      $iCheckLogin = checkLogin( $_POST['sLogin'], $_POST['sPass'], $sKey );
      if( $iCheckLogin == 1 ){
        if( !isset( $_COOKIE['sLogin'] ) || $_COOKIE['sLogin'] != $_POST['sLogin'] )
          @setCookie( 'sLogin', $_POST['sLogin'], time( ) + 2592000 );
        
        $sRedirect = !empty( $_POST['sLoginPageNext'] ) ? $_POST['sLoginPageNext'] : $_SERVER['PHP_SELF'];

        header( 'Location: '.$sRedirect );
        exit;
      }
      elseif( $iCheckLogin == 2 ){
        $sLoginPage     = $_SERVER['PHP_SELF'];
        $sLoginContent  = $oTpl->tbHtml( 'login.tpl', 'INACTIVE' );
      }
      else{
        $sLoginPage     = $_SERVER['PHP_SELF'];
        $sLoginContent  = $oTpl->tbHtml( 'login.tpl', 'INCORRECT' );
      }
    }
    else{
      if( isset( $bLoginExceed ) ){
        $sLoginPage     = $_SERVER['PHP_SELF'];
        $sLoginContent  = $oTpl->tbHtml( 'login.tpl', 'FAILED_LOGIN' );      
      }
      else{
        $sLoginPage    = '?p=login';
        if( !empty( $_SERVER['REQUEST_URI'] ) )
          $_SERVER['REQUEST_URI'] = strip_tags( $_SERVER['REQUEST_URI'] );
        $sLoginContent = $oTpl->tbHtml( 'login.tpl', 'FORM' );
      }
    }

    unset( $GLOBALS['aActions'] );

    $oTpl->setVariables( 'sLoginContent', $sLoginContent );
    $sContent = $oTpl->tbHtml( 'login.tpl', 'PANEL' );
    echo $oTpl->tbHtml( $sFile, 'HEAD' ).$sContent.$oTpl->tbHtml( $sFile, 'FOOT' );
    exit;
  }
  else{
    if( $p == 'logout' ){
      unset( $_SESSION[$sKey] );
      $sLoginPage = $_SERVER['PHP_SELF'];
      header( 'Location: '.$_SERVER['PHP_SELF'] );
      exit;
    }
    if( isset( $sCheck ) )
      $sCheck();
  }
} // end function loginActions

/**
* Check login and password saved in config/general.php
* @return int
* @param string $sLogin
* @param string $sPass
* @param string $sKey
*/
function checkLogin( $sLogin, $sPass, $sKey ){
  if( $GLOBALS['config']['login'] == $sLogin && $GLOBALS['config']['pass'] == $sPass ){
    $_SESSION[$sKey] = true;
    return 1;
  }
  else{
    return 0;
  }
} // end function checkLogin

/**
* Return true/false select
* @return string
* @param bool $bTrueFalse
*/
function throwTrueFalseSelect( $bTrueFalse = false ){
  
  $aSelect = Array( null, null );
  
  if( $bTrueFalse == true )
    $aSelect[1] = 'selected="selected"';
  else
    $aSelect[0] = 'selected="selected"';
  
  $sOption =  '<option value="true" '.$aSelect[1].'>'.LANG_YES_SHORT.'</option>';
  $sOption .= '<option value="false" '.$aSelect[0].'>'.LANG_NO_SHORT.'</option>';
  return $sOption;
} // end function throwTrueFalseSelect

/**
* Return true/null select
* @return string
* @param bool $bTrueNull
*/
function throwTrueNullSelect( $bTrueNull = null ){
  
  $aSelect = Array( null, null );
  
  if( $bTrueNull == true )
    $aSelect[1] = 'selected="selected"';
  else
    $aSelect[0] = 'selected="selected"';
  
  $sOption =  '<option value="true" '.$aSelect[1].'>'.LANG_YES_SHORT.'</option>';
  $sOption .= '<option value="null" '.$aSelect[0].'>'.LANG_NO_SHORT.'</option>';
  return $sOption;
} // end function throwTrueNullSelect

/**
* Return directories select list
* @return string
* @param string $sDir
* @param string $sDirSelected
*/
function listDirectoriesSelect( $sDir, $sDirSelected = null ){
  if( !empty( $sDir ) && is_dir( $sDir ) ){
    $sOption = null;

    $oDir = dir( $sDir );
    while( false !== ( $sDirectoryName = $oDir->read( ) ) ) {
      if( $sDirectoryName != '.' && $sDirectoryName != '..' && is_dir( $sDir.$sDirectoryName ) ){
        $aDirs[$sDirectoryName] = $sDirectoryName;
      }
    } // end while
    $oDir->close( );

    if( isset( $aDirs ) ){
      return throwSelectFromArray( $aDirs, $sDirSelected );
    }
  }
} // end function listDirectories

?>