<?php function ChangeIni($file, $change_ini, $order="",$handle_keys="") { // Swedé 14.12.2003 http://collection.com.ua/webmaster // last change 24.01.2005 (some fixes) // this function make changes in yours ini $file // $change_ini - array of sections array of keys of values ( $change_ini["section"]["key"]="value" ) // $order - set the order of keys or sections in ini file by keys order in array $change_ini: // "" - default, don't change position of changed keys and section, // new keys and sections placed to the end of section or file // "keys_first" - changed and new keys placed to the begin of section // "keys_last" - changed and new keys placed to the end of section // "sec_first" - changed and new sections placed to the begin of file // "first" - also as keys_first + sec_first, changed and new section and key to begin // "last" - alias for keys_last // $handle_keys - what to do with values of specified keys in $change_ini ( $change_ini["section"]["key"]="value" ) // or set comments by all specified keys (see below). // "" - default, treat empty keys as usual key ( in ini file be as key = "" ) // "set_order" - For empty keys - don't change value in ini file, only set order of specified keys in $change_ini, // if $order set certainly, otherwise do nothing // "delete" - delete all specified keys from ini file independently values. // "delete_empty"- delete only empty of specified keys from ini file another save. // "new_ini" or "rewrite_ini" - do not read old ini. Ini data from $change_ini will be saved as new. Old file will be rewrite // "comments" - write values of specified keys in $change_ini as a comments after real values, // if in ini file values set without double quotes - this set it. // Comments - all after value in double quotes to end string //print "<pre> $handle_keys"; //print_r($change_ini); if (!is_array($change_ini)) return 0; $comment_char = ";"; // comment symbol if (!in_array(strtolower($handle_keys),array("new_ini","rewrite_ini")) and file_exists($file)) $content = str_replace("\r","",stripcslashes(file_get_contents($file))); // read ini file if exist and allowed $order = strtolower($order); $handle_keys = strtolower($handle_keys); // parameters can be in UPPER CASE $comments_begin = substr($content,0,strpos($content,"[")); preg_match_all("/^\[(.*?)\](.*?)(?=^\[|\z)/sm",$content,$out); // split ini file by sections to $sections[section]= "key = value\n for ($i=0;$i<count($out[1]);$i++) $sections[$out[1][$i]] = rtrim($out[2][$i]); // key1 = value1 ; comments\n key3 = value3\n ...." foreach (array_keys($change_ini) as $section) { // check every section of ini file for changes if (!is_array($change_ini[$section])) { // if empty or not array if ($handle_keys=="delete" or ($value=="" and $handle_keys=="delete_empty")) unset($sections[$section]); // delete section continue; } if (preg_match("/^(keys_){0,1}first$/i",$order)) $cursection=array_reverse($change_ini[$section]); else $cursection=$change_ini[$section]; // set order of keys in section foreach ($cursection as $key => $value) { if (!$key) continue; // do not create empty keys names! $value = str_replace('"',""",$value); // parse_ini_file() don't work with more then two double quotes in one string \" - don't work :( if ($handle_keys=="comments") $set_string = "\n$key = \"\" $comment_char ".$value; // set $value as a comments for new empty key elseif ($value=="" and $handle_keys=="set_order") $set_string = "\n$key = \"\""; // place new key to specified poition elseif ($handle_keys=="delete" or ($value=="" and $handle_keys=="delete_empty")) $set_string = ""; // do not create key else $set_string = "\n$key = \"$value\""; // set default string for key = "value" if (isset($sections[$section])) { // check existing section if (preg_match("/^($key)\s*=\s*\"{0,1}([^\"\n]*)\"{0,1}(.*)/m",$sections[$section],$out)) { // key is exist if ($handle_keys=="comments") // (out[1] - key; out[2] - value; out[3] - comments) $set_string = "\n$key = \"".$out[2]."\" $comment_char ".$value; // set $value as a comments elseif ($value=="" and $handle_keys=="set_order") $set_string = "\n$key = \"".$out[2]."\"".$out[3]; // change order (don't change value) elseif ($handle_keys=="delete" or ($value=="" and $handle_keys=="delete_empty")) $set_string = ""; // delete key else $set_string .= $out[3]; // default for exist key (+ comments) $replaced_str = "/[\n]*$key\s*=\s*\"{0,1}".str_replace("/","\/",quotemeta($out[2]))."\"{0,1}(.*)/"; // set replaced param for remove old key strings switch (str_replace("keys_","",$order)) { // set keys of current section case "first": // order - to begin of section $temp = split("\n",preg_replace ($replaced_str,"",$sections[$section])); // first string of section - "<comments>\n" leave it $sections[$section] = array_shift($temp).$set_string."\n".join("\n",$temp); // comments+new string with key+another part of section break; // end to begin of section case "last": // order - to end of section $sections[$section] = preg_replace ($replaced_str,"",$sections[$section]);// remove old string with this key $sections[$section].=$set_string; // add string with key to end break; // end order - to end of section default: // replace old string to new with key //print "<pre> \$sections[$section] = $sections[$section] \n\$replaced_str = $replaced_str\n"; $sections[$section] = preg_replace ($replaced_str,$set_string,$sections[$section]); } } else { // add new key to existing section if (preg_match("/^(keys_){0,1}first$/i",$order)) $sections[$section]=$set_string.$sections[$section]; // add to begin else $sections[$section].= $set_string; // default - to end } // and check keys } else $sections[$section] .= $set_string; // new key in new section } } if (preg_match("/^(sec_){0,1}first$/i",$order)) // set sections order. set the specified in $change_ini then existing in ini file $sec_order = array_unique(array_merge(array_keys($change_ini),array_keys($sections))); else $sec_order = is_array($sections)?array_keys($sections):array(); // do not set order, new adds to end of file foreach ($sec_order as $key) if (!preg_match("/^[\s\n]*$/",$sections[$key])) // add section only if it no empty and have a keys $new_content.="[$key]".$sections[$key]."\n\n"; $new_content = $comments_begin.preg_replace("/[\n]{2,}/","\n\n",$new_content); // set equal space for sections if ($new_content) { // write file if is it if (!$handle_file = fopen($file,"w")) return 0; // Can't open file $file if (!fwrite ($handle_file, $new_content)) { fclose($handle); return 0; } // Can't write file $file fclose($handle_file); } else if (file_exists($file)) unlink($file); // delete file if it empty return true; // no errors - well done } /* ------------ testing $inifile="../ini/site.ini"; print "<pre>"; print "<p style='font-size:10px'>curent ini:";print_r(parse_ini_file($inifile,true)); print "</p><p>\n"; $ini["news"]["file"] = "exist key"; $ini["news"]["test"] = "exist key whitout \""; $ini["news"]["new key"] = "new key in exist section"; $ini["new section"]["test"] = "new key in new section"; $ini["dirs"]["empty"]=""; $ini["empty"]["empty"]=""; print_r($ini); $order = "last"; $handle = "comments"; if (ChangeIni($inifile,$ini)) print "\nSaved"; else print "\nError!" /* ------------ testing */ ?>
|