https://wiki.uniformserver.com/index.php?title=My_Ini_Design:_Ini_Set&feed=atom&action=historyMy Ini Design: Ini Set - Revision history2024-03-29T15:40:48ZRevision history for this page on the wikiMediaWiki 1.41.0https://wiki.uniformserver.com/index.php?title=My_Ini_Design:_Ini_Set&diff=3907&oldid=prevRic: New page: {{Nav My Ini Design}} Previous page covered a test set-up this pages uses that to develop a my_ini_set function. I like to keep things simple and test code before wrapping it into a funct...2009-10-09T16:59:17Z<p>New page: {{Nav My Ini Design}} Previous page covered a test set-up this pages uses that to develop a my_ini_set function. I like to keep things simple and test code before wrapping it into a funct...</p>
<p><b>New page</b></p><div>{{Nav My Ini Design}}<br />
Previous page covered a test set-up this pages uses that to develop a my_ini_set function.<br />
<br />
I like to keep things simple and test code before wrapping it into a function. This approach allows proof of concept before committing to a function.<br />
<br />
== Requirement ==<br />
Basic requirement is to target a specific configuration block and option, once targeted to change that options value.<br />
{|<br />
|-valign="top"<br />
|<br />
'''''For Example'':'''<br />
<br />
Suppose we want to target block '''[drupal]''' and change the option '''period''' from daily to weekly.<br />
<br />
* All blocks are uniquely named and placed in square brackets. <br />
**The only issue with detecting this block is one of spaces. Easily resolved using the trim() function<br />
<br />
<br />
* All options within a block are uniquely named. <br />
**Again an issue with spaces resolved using the trim() function<br />
**If the option does not exist! A potential issue of corrupting next block.<br />
<br />
<br />
*Replacing the value is relatively easy; just replace the value in array and exit.<br />
**The new line will have been lost hence add when building new value.<br />
<br />
|<br />
UniServer\plugins\test\'''cron.ini'''<br />
<pre><br />
; Test example file for cron timers<br />
; Period values hourly, daily or weekly<br />
<br />
[moodle]<br />
path = http://localhost/moodle/admin/cron.php<br />
period = daily<br />
ref = <br />
<br />
[drupal]<br />
path = http://localhost/drupal/cron.php<br />
period = daily<br />
ref = <br />
<br />
[dtdns]<br />
path = ..\..\plugins\dtdns_updater\dtdns_updater.php<br />
period = hourly<br />
ref = <br />
</pre><br />
|}<br />
'''''[[#top | Top]]'''''<br />
== Example 1 ==<br />
{|<br />
|-valign="top"<br />
|<br />
'''''Modify Test Script as shown on right'':'''<br />
* '''trim($value)''' Removes all spaces either side of the string including new line character.<br />
* This cleaned variable allows direct comparison with the block we wish to find<br />
* On finding a match variable '''$flag''' is set to true<br />
* Print is just for testing<br />
<br />
<br />
* Run the batch file (Double click on Run_test.bat)<br />
* Result '''Drupal found''' is displayed<br />
<br />
That's targeted the Moodle block next example shows how to target an option. <br />
|<br />
UniServer\plugins\test\'''test.php'''<br />
<pre><br />
<?php<br />
$ini_array = file("cron.ini");<br />
<br />
$flag=false;<br />
foreach($ini_array as $key => $value){<br />
<br />
if(trim($value) == "[drupal]"){ <br />
$flag=true; <br />
print "Drupal found \n";<br />
}<br />
}<br />
<br />
file_put_contents("out_cron.txt",$ini_array);<br />
?><br />
</pre><br />
|}<br />
'''''[[#top | Top]]'''''<br />
== Example 2 ==<br />
{|<br />
|-valign="top"<br />
|<br />
'''''Modify Test Script as shown on right'':'''<br />
<br />
We have the desired block targeted lines that follow will contain the option we wish to target.<br />
<br />
* If '''$flag''' and '''preg_match''' are true we have found a match.<br />
* '''$ini_array[$key]''' defines location in array where new value is to be saved<br />
* A new string is constructed: "'''period = weekly\n'''"<br />
* This is saved to the array in location mentioned above<br />
**Note the newline character has been added<br />
* That's finished the task hence break out of the foreach loop<br />
<br />
<br />
* Run the batch file (Double click on Run_test.bat)<br />
* Open file '''out_cron.txt''' note the new value for period.<br />has changed from '''daily''' to '''weekly''' <br />
|<br />
UniServer\plugins\test\'''test.php'''<br />
<pre><br />
<?php<br />
$ini_array = file("cron.ini");<br />
<br />
$flag=false;<br />
foreach($ini_array as $key => $value){<br />
<br />
if(trim($value) == "[drupal]"){ <br />
$flag=true; <br />
print "Drupal found \n";<br />
}<br />
<br />
if($flag && preg_match('/^\s*period/',$value)){ <br />
$ini_array[$key] = "period = weekly\n"; <br />
break; <br />
} <br />
}<br />
<br />
file_put_contents("out_cron.txt",$ini_array);<br />
?><br />
</pre><br />
|}<br />
<br />
{|<br />
|-<br />
|<br />
<pre><br />
[drupal]<br />
path = http://localhost/drupal/cron.php<br />
;period = daily <br />
ref = <br />
</pre><br />
|<br />
One quick test before looking at example 3.<br />
* Edit file '''cron.ini''' comment out line '''period = daily''' as shown <br />
* Run the batch file (Double click on Run_test.bat)<br />
* Open file '''out_cron.txt''' Note: Corruption of [dtdns]period now changed to '''weekly'''<br />
|}<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Example 3 ==<br />
{|<br />
|-valign="top"<br />
|<br />
'''''Modify Test Script as shown on right'':'''<br />
<br />
Above test shows it is possible to corrupt the next block if a option is missing or commented. <br />
<br />
A boundary change is eassily detected by adding a new if command at the start of foreach loop.<br />
<br />
* If '''$flag''' and '''preg_match''' are true we have detected a boundary change.<br />
* Setting ''' $flag=false''' effectively breaks out of the previous block<br />
<br />
<br />
* Run the batch file (Double click on Run_test.bat)<br />
* Open file out_cron.txt<br />
** Note the value for [dtdns]period remains unchanged (hourly).<br />
<br />
'''''Restore file'':'''<br />
*Edit file '''cron.ini''' un-comment line period as shown <br />
{|<br />
|-<br />
|<br />
<pre><br />
[drupal]<br />
path = http://localhost/drupal/cron.php<br />
period = daily <br />
ref = <br />
</pre><br />
|}<br />
<br />
|<br />
UniServer\plugins\test\'''test.php'''<br />
<pre><br />
<?php<br />
$ini_array = file("cron.ini");<br />
<br />
$flag=false;<br />
foreach($ini_array as $key => $value){<br />
<br />
if($flag && preg_match('/^\s*\[/',$value)){ <br />
$flag=false; <br />
}<br />
<br />
if(trim($value) == "[drupal]"){ <br />
$flag=true; <br />
print "Drupal found \n";<br />
}<br />
<br />
if($flag && preg_match('/^\s*period/',$value)){ <br />
$ini_array[$key] = "period = weekly\n"; <br />
break; <br />
} <br />
}<br />
<br />
file_put_contents("out_cron.txt",$ini_array);<br />
?><br />
</pre><br />
|}<br />
'''''[[#top | Top]]'''''<br />
<br />
== Summary ==<br />
I mentioned in the opening comment proof of concept before committing to a function the above examples have proved the concept.<br />
<br />
Before converting to a function its worth looking at function my_ini_get covered on [[My Ini Design: Ini Get | '''next page''']].<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
[[Category: Uniform Server 5.0-Nano]]</div>Ric