insert_ntp.php 1.01 KB
<?php
 //include('../cfg/file_name.php');
 $open_ntp = '/etc/ntp.conf';
 $ntp_title = array('# pool: <http://www.pool.ntp.org/join.html>','#end');
 shell_exec("sudo chmod 777 ".$open_ntp);
 $nn = 0;
 $i = 0; 
 $handle = @fopen($open_ntp,"r");
 //讀檔案
 while(($data = fgetcsv($handle,1000,"\n")) != false){
  foreach($data as $key => $value){
   $aa[$i] = $value;
   $i++;
  }
  
 }
 //計算出server之前和之後的key抓出來
 foreach($aa as $num => $num_value){
  if(in_array($num_value,$ntp_title)){
   $max_min[] = $num;
  }
 }

 foreach($aa as $key => $value){
  if($key==($max_min[0]+2)){
   $update[$nn][] = $value."\n"."server ".$_POST['server']." minpoll 3 maxpoll 10 iburst prefer\n fudge ".$_POST['server']." refid GPS stratum 1\n";
  }else{
   $update[$nn][] = $value."\n";
  }
  $nn++;
 }
 $fp = fopen($open_ntp,'w')  or die("Unable to open file!");
 foreach($update as $key => $value){
  foreach($value as $k => $v){
   fwrite($fp, $v);
  }
 }
 fclose($fp);
 echo "1";
?>