Location: PHPKode > scripts > SMB Conf > smb-conf/CSMBConf.php
<?php 
/*
*	This script was writed by Setec Astronomy - hide@address.com
*
*	This class allows to edit (parse and recreate) and smb.conf file.
*
*	This script is distributed  under the GPL License
*
*	This program is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* 	GNU General Public License for more details.
*
*	http://www.gnu.org/licenses/gpl.txt
*
*/

class CSMBConf
{
	var $filename;
	
	function CSMBConf ($filename)
	{
		if (isset ($filename)) {
			$this->filename = $filename;		
		}
		$this->smbconf = array ();
	}
	
	function parse ($filename = "") {
		if (!empty ($filename)) {
			$this->filename = $filename;		
		}
		if (is_readable ($this->filename)) {
			return $this->_parse ();
		} else {
			return false;
		}
	}

	function _parse () {
		// initialize //
		$return = false;
		$this->smbconf = array ();
		$section_name = "UNKNOWN";
		$value_name = "UNKNOWN";
		$join_line = false;
		// initialize //

		$lines = file ($this->filename);

		// parser //
		foreach ($lines as $line) {
			$trim_line = trim ($line);
			$begin_char = substr($trim_line, 0, 1);
			$end_char = substr($trim_line, -1);
			if (($begin_char == "#") || ($begin_char == ";")) { // comment
				$raw = $trim_line; 
			} elseif (($begin_char == "[") && ($end_char == "]")) { // section
				$raw = $trim_line;
				$section_name = substr ($trim_line, 1, -1);
			} elseif ($trim_line != "") { // values
				$raw = $trim_line;
				$pieces = explode("=", $trim_line, 2); 
				if ($join_line) {
					$this->smbconf[$section_name][$value_name][] = $trim_line;	
				} elseif (count ($pieces) == 2) {
					$value_name = trim ($pieces[0]);
					$this->smbconf[$section_name][$value_name][] = trim ($pieces[1]);	
				}
			}
			$join_line = $end_char == "\\";
		}
		return $this->smbconf;
	}

	function recreate ($smbconf = array ()) {
		if (!empty ($smbconf)) {
			$this->smbconf = $smbconf;
		}
		$return = false;
		foreach ($this->smbconf as $section => $content) {
			$return .= "[" . $section . "]\n";
			foreach ($content as $key => $values) {
				$rows = implode("\n\t\t", $values);
				$return .= "\t" . $key . " = " . $rows . "\n";
			}
			$return .= "\n";
		}
		return $return;
	}

}
?>
Return current item: SMB Conf