<?php

// ------------------------------------------------------------------------- //
// PHPMyINI                                                                  //
// ------------------------------------------------------------------------- //
// Auteur: Virgile Boulanger                                                 //
// Email:  virux@crosswinds.net                                              //
// Web:    http://www.passiondelphi.net                                      //
// ------------------------------------------------------------------------- //

/*********************************************************
*                   PHPMyIni By Virux                    *
**********************************************************
*                                                        *
* Date : 08/08/2001                                      *
* Version : 1.0                                          *
* Auteur : virgile boulanger (Virux)                     *
* Email  : Virux@crosswinds.net / virux@passiondelphi.net*
* Site   : Http://www.passiondelphi.net                  *
*                                                        *
* Vous pouvez utiliser, modifier et distribuer ce script *
* a votre guise, mais cette entete doit toujours         *
* accompagner ce script.                                 *
**********************************************************
* Attention le fichier ini doit etre dans un repertoire  *
* Proteger s'il contient des informations sensibles      *
**********************************************************/

class inifile
{
/*Interface*/

    /* Proprietes */
    
var $FFileName; // Filename of inifile
    
var $Sections;  // Section list
    
/* Methodes */
/*
function Inifile($FileName);              // Read file from HDD (create)
function Read($Section, $Ident, $Default) // Return(reading value)
function Write($Section, $Ident, $Value)  // Return(boolean) -= false on error =-
function DeleteKey($Section, $Ident)      // Return(boolean) -= false on error =-
function EraseSection($Section)           // Return(boolean) -= false on error =-
function ReadSections()                   // Return(Array with all session name)
function ReadSectionValues($Section)      // Return(Array with all value of the
                                          // session name)
function SectionExists($Section)          // Return(boolean)
function ValueExists($Section, $Ident)    // Return(boolean)
function UpdateFile                       // Write file to HDD
*/
    
/*Implementation*/    

    
function Inifile($FileName)
    {
        
$this->FFileName = $FileName;
        if(@
$F = fopen($this->FFileName ,"r"))
          {
            while (!
feof($F))
            {
              
$txt = fgets($F, 1024);

              if (
strlen($txt) > 2) // >2 because CRLF
              
{
                  if (
$txt[0]=="[")
                  {
                    
$lastsection = substr($txt,1,strpos($txt,"]")-1);
                  } else {
                    
$ident = trim(substr($txt,0,strpos($txt,"=")));
                    
$value = trim(substr($txt,strpos($txt,"=")+1,strlen($txt)));
                    
$this->Sections[$lastsection][$ident] = $value;
                  }
              }  
            }
            
fclose($F);
            return(
True);
          }else{
            return(
False);
          }    
    }

    function
read($Section, $Ident, $Default)
    {
      
$ReadStr = trim($this->Sections[$Section][$Ident]);
      
      if(
strlen($ReadStr) > 0)
      {
        Return(
$ReadStr);
      }else{
        Return(
$Default);   
      }
    }
    
    function
write($Section, $Ident, $Value)
    {
        
$this->Sections[$Section][$Ident] = trim($Value);
    }
    
    function
DeleteKey($Section, $Ident)
    {
        
$pos=0;
        foreach (
$this->Sections[$Section] as $ind=>$value)
        {
          if (
$Ident == $ind){$find = True;};
          if (!
$find){$pos++;}  
        }
        
array_splice($this->Sections[$Section],$pos,1);
    }
    
    function
EraseSection($Section)
    {     
        
$pos=0;
        foreach (
$this->Sections as $ind=>$value)
        {
          if (
$Section == $ind){$find = True;};
          if (!
$find){$pos++;}  
        }
        
array_splice($this->Sections,$pos,1);
    }
    
    function
ReadSections()
    {
      foreach (
$this->Sections as $ind=>$value)
      {
        
$TmpAray[] = $ind;    
      }  
        Return(
$TmpAray);
    }
    
    function
ReadSectionValues($Section)
    {
        Return(
$this->Sections[$Section]);
    }
    
    function
SectionExists($Section)
    {
        
$find = False;
        foreach (
$this->Sections as $ind=>$value)
        {
          if (
$Section == $ind){$find = True;};   
        }

        Return(
$find);
        
    }
    
    function
ValueExists($Section, $Ident)
    {
        
$find = False;
        foreach (
$this->Sections[$Section] as $ind=>$value)
        {
          if (
$Ident == $ind){$find = True;};   
        }

        Return(
$find);
    }
    
    function
UpdateFile()
    {
     if(
$F = fopen($this->FFileName ,"w"))
       {
        foreach (
$this->Sections as $ind=>$value)
        {
          
fputs($F ,"\n[".trim($ind)."]\n");
          foreach (
$this->Sections[$ind] as $ind2=>$value2)
          {
            
fputs($F ,trim($ind2)."=".trim($value2)."\n");
          }   
        }
         
fclose($F);
       }
    }
}    
?>


/*****************************************************************************/

Fichier Exemple.php:

<HTML>
<HEAD>
<TITLE>PHPMyIni by Virux</TITLE>
</HEAD>
<BODY>

<?
include("phpmyini.php");
if (empty(
$fichierini)){$fichierini = "option.ini";}    
?>
Ajouter une valeur :
<FORM ACTION="<?echo basename($PHP_SELF);?>" METHOD="POST"  Name="ajoutvaleur">
    <TABLE BORDER=1 width=95%>
      <TR>
          <TD>Section :</TD>
          <TD><INPUT TYPE="TEXT" VALUE="<?echo $section;?>" NAME="section"
          SIZE=70></TD>
      </TR>
      <TR>
          <TD>Nom :</TD>
          <TD><INPUT TYPE="TEXT" VALUE="<?echo $nom;?>" NAME="nom" SIZE=70></TD>
      </TR>
      <TR>
          <TD>Valeur :</TD>
          <TD><INPUT TYPE="TEXT" VALUE="<?echo $valeur;?>" NAME="valeur" SIZE=70>
          </TD>
      </TR>
      <TR>
          <TD colspan=2><CENTER><INPUT TYPE="SUBMIT" VALUE="Ajouter" NAME="action">
          </CENTER></TD>
      </TR>
    </TABLE>
</FORM>      

<?
$myini
= new inifile($fichierini);

if (
$action=="Ajouter")
{
   
$myini->write($section,$nom,$valeur);
   
$myini->UpdateFile();
}

if (
$action=="Effacer")
{
   
$myini->DeleteKey($section, $nom);
   
$myini->UpdateFile();
}

if (
$action=="Modifier")
{
   
$myini->write($section,$nom,$valeur);
   
$myini->UpdateFile();
}
if (
$saction=="Effacer")
{
   
$myini->EraseSection($section);
   
$myini->UpdateFile();
}
?>

Modifier une valeur :
<BR>
<TABLE BORDER=1 width=95%>
  <TR>
      <TD align="center">Nom</TD>
      <TD align="center">valeur</TD>
      <TD align="center">Action</TD>
  </TR>
<?
$Stringsection
= $myini->ReadSections();    

if (
count($Stringsection)>0)
{
         echo(
"count : ".count($Stringsection)."<br>");  
    foreach (
$Stringsection as $index=>$section)
    {   
?>
  <FORM ACTION="<?echo basename($PHP_SELF);?>" METHOD="POST"  Name="erazesection">
  <TR>
    <TD colspan=2>Section : <?echo $section;?><INPUT TYPE="HIDDEN"
    VALUE="<?echo $section;?>" NAME="section"></TD>
    <TD><CENTER><INPUT TYPE="SUBMIT" VALUE="Effacer" NAME="saction"></CENTER></TD>
  </TR>
  </FORM>
<?   
    $Strings
= $myini->ReadSectionValues($section);
      if (
count($Strings)>0)
      {
        foreach (
$Strings as $ind=>$value)
        {
?>
  <FORM ACTION="<?echo basename($PHP_SELF);?>" METHOD="POST"  Name="modif">
  <TR>
      <INPUT TYPE="HIDDEN" VALUE="<?echo $section;?>" NAME="section">
      <TD><?echo $ind;?> :<INPUT TYPE="HIDDEN" VALUE="<?echo $ind;?>" NAME="nom"></TD>
      <TD><INPUT TYPE="TEXT" VALUE="<?echo $value;?>" NAME="valeur" SIZE=30></TD>
      <TD><CENTER><INPUT TYPE="SUBMIT" VALUE="Modifier" NAME="action">
      &nbsp;&nbsp;&nbsp;&nbsp;<INPUT TYPE="SUBMIT" VALUE="Effacer" NAME="action">
      </CENTER></TD>
  </TR>
  </FORM>
<?  
        
}// fin du section values
      
}
    }
// fin du section
}
?>
</TABLE>
</BODY>
</HTML>