My Ini Design: Ini Set

MPG UniCenter

MPG UniCenter

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.

  • All blocks are uniquely named and placed in square brackets.
    • The only issue with detecting this block is one of spaces. Easily resolved using the trim() function


  • All options within a block are uniquely named.
    • Again an issue with spaces resolved using the trim() function
    • If the option does not exist! A potential issue of corrupting next block.


  • Replacing the value is relatively easy; just replace the value in array and exit.
    • The new line will have been lost hence add when building new value.

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 = 

Top

Example 1

Modify Test Script as shown on right:

  • trim($value) Removes all spaces either side of the string including new line character.
  • This cleaned variable allows direct comparison with the block we wish to find
  • On finding a match variable $flag is set to true
  • Print is just for testing


  • Run the batch file (Double click on Run_test.bat)
  • Result Drupal found is displayed

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);
?>

Top

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.

  • If $flag and preg_match are true we have found a match.
  • $ini_array[$key] defines location in array where new value is to be saved
  • A new string is constructed: "period = weekly\n"
  • This is saved to the array in location mentioned above
    • Note the newline character has been added
  • That's finished the task hence break out of the foreach loop


  • Run the batch file (Double click on Run_test.bat)
  • Open file out_cron.txt note the new value for period.
    has changed from daily to weekly

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.

  • Edit file cron.ini comment out line period = daily as shown
  • Run the batch file (Double click on Run_test.bat)
  • Open file out_cron.txt Note: Corruption of [dtdns]period now changed to weekly

Top

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.

  • If $flag and preg_match are true we have detected a boundary change.
  • Setting $flag=false effectively breaks out of the previous block


  • Run the batch file (Double click on Run_test.bat)
  • Open file out_cron.txt
    • Note the value for [dtdns]period remains unchanged (hourly).

Restore file:

  • Edit file cron.ini un-comment line period as shown
[drupal]
path = http://localhost/drupal/cron.php
period = daily   
ref = 

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);
?>

Top

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.

Top