note 98438 added to function.parse-ini-file

From: Date: Tue, 15 Jun 2010 19:46:05 +0000
Subject: note 98438 added to function.parse-ini-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-170155@lists.php.net to get a copy of this message
Tip: you cannot parse an ini-file with this safer structure: <?php exit(); $data=" [section_one] test = abc [section_two] and_so=on "; ?> (strangers are not able to see this file because php closed the file previously by executing exit();) But here is a very simple code to prevent this: <?php class iniParser{ private $IniFile; private $SafeFile; private $ParseClasses; public $KeysWithoutSections; public $KeysWithSections; public function __construct($FileName, $SafeFile = false){ $this->IniFile = $FileName; $this->SafeFile = $SafeFile; } public function parseIni($SaveInClass = true){ $FileHandle = file($this->IniFile); $CountLines = count($FileHandle); $Counter = 0; $NKeys = ""; if ( $this->SafeFile ){ $Counter += 2; $CountLines -= 2; } while ( $Counter < $CountLines ){ $CurLine = $FileHandle[$Counter]; $CurLineSplit = explode("=", $CurLine); $CurKey = $CurLineSplit[0]; $CurValue = $CurLineSplit[1]; if( $SaveInClass ) $this->Keys[trim($CurKey)] = trim($CurValue); else $NKeys[trim($CurKey)] = trim($CurValue); $Counter++; } if( $SaveInClass ) return $this->KeysWithoutSections; else return $NKeys; } public function parseIniWithSections($SaveInClass = true){ $FileHandle = file($this->IniFile); $CountLines = count($FileHandle); $Counter = 0; $LastSection = ""; $NKeys = ""; if ( $this->SafeFile ){ $CountLines -= 2; $Counter += 2; } while ( $Counter < $CountLines ){ $CurLine = $FileHandle[$Counter]; if ( strpos($CurLine, "[") == 1 ){ $LastSection = $CurLine; continue; } $Explosion = explode("=", $CurLine); $CurKey = trim($Explosion[0]); $CurValue = trim($Explosion[1]); if ( $SaveInClass ) $this->KeysWithSections[$LastSection][$CurKey] = $CurValue; else $NKeys[$LastSection][$CurKey] = $CurValue; } if ( $SaveInClass ) return $this->KeysWithSections; else return $NKeys; } }; ?> To use this class just try this script here: <?php include "iniparser.php" // class above $SafeIniParser = new iniParser("test.php", true); // file: test.php, safefile. $Keys = $SafeIniParser->parseIniWithSections(false); echo $Keys["section_one"]["test"]; ?> i used this file: <?php exit(); $data=" [section_one] test = abc [section_two] and_so=on "; ?> ---- Server IP: 85.214.154.209 Probable Submitter: 79.206.74.45 ---- Manual Page -- http://www.php.net/manual/en/function.parse-ini-file.php Edit -- https://master.php.net/note/edit/98438 Del: integrated -- https://master.php.net/note/delete/98438/integrated Del: useless -- https://master.php.net/note/delete/98438/useless Del: bad code -- https://master.php.net/note/delete/98438/bad+code Del: spam -- https://master.php.net/note/delete/98438/spam Del: non-english -- https://master.php.net/note/delete/98438/non-english Del: in docs -- https://master.php.net/note/delete/98438/in+docs Del: other reasons-- https://master.php.net/note/delete/98438 Reject -- https://master.php.net/note/reject/98438 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#170155) next »