My Ini Design: Ini Set
My Ini Design: Introduction | Ini Set | Ini Get | Final Functions | parse_ini_file
|
Uniform Server Cron Configuration |
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 function. This approach allows proof of concept before committing to a function.
Requirement
Basic requirement is to target a specific configuration block and option, once targeted to change that options value.
For Example: Suppose we want to target block [drupal] and change the option period from daily to weekly.
|
UniServer\plugins\test\cron.ini ; Test example file for cron timers ; Period values hourly, daily or weekly [moodle] path = http://localhost/moodle/admin/cron.php period = daily ref = [drupal] path = http://localhost/drupal/cron.php period = daily ref = [dtdns] path = ..\..\plugins\dtdns_updater\dtdns_updater.php period = hourly ref = |
Example 1
Modify Test Script as shown on right:
That's targeted the Moodle block next example shows how to target an option. |
UniServer\plugins\test\test.php <?php $ini_array = file("cron.ini"); $flag=false; foreach($ini_array as $key => $value){ if(trim($value) == "[drupal]"){ $flag=true; print "Drupal found \n"; } } file_put_contents("out_cron.txt",$ini_array); ?> |
Example 2
Modify Test Script as shown on right: We have the desired block targeted lines that follow will contain the option we wish to target.
|
UniServer\plugins\test\test.php <?php $ini_array = file("cron.ini"); $flag=false; foreach($ini_array as $key => $value){ if(trim($value) == "[drupal]"){ $flag=true; print "Drupal found \n"; } if($flag && preg_match('/^\s*period/',$value)){ $ini_array[$key] = "period = weekly\n"; break; } } file_put_contents("out_cron.txt",$ini_array); ?> |
[drupal] path = http://localhost/drupal/cron.php ;period = daily ref = |
One quick test before looking at example 3.
|
Example 3
Modify Test Script as shown on right: Above test shows it is possible to corrupt the next block if a option is missing or commented. A boundary change is eassily detected by adding a new if command at the start of foreach loop.
Restore file:
|
UniServer\plugins\test\test.php <?php $ini_array = file("cron.ini"); $flag=false; foreach($ini_array as $key => $value){ if($flag && preg_match('/^\s*\[/',$value)){ $flag=false; } if(trim($value) == "[drupal]"){ $flag=true; print "Drupal found \n"; } if($flag && preg_match('/^\s*period/',$value)){ $ini_array[$key] = "period = weekly\n"; break; } } file_put_contents("out_cron.txt",$ini_array); ?> |
Summary
I mentioned in the opening comment proof of concept before committing to a function the above examples have proved the concept.
Before converting to a function its worth looking at function my_ini_get covered on next page.