packages/web/353/control_panel/backend.php

Go to the documentation of this file.
00001 <?php
00040 function getSoftware() {
00041         return "<a href='/docs/" . exec('ls /usr/html/docs') . "'>" . exec('ls /usr/html/docs') . "</a> (get source)";
00042         //return $to_out;
00043         }
00044 
00045 function soft_ver() {
00046         exec('/bin/ls /usr/html/docs', $arr);
00047         return implode($arr);
00048 }
00049 
00050 function ps() {
00051 /*      exec('ps -w', $arr); 
00052         function low_daemon($v)
00053         {
00054          return (substr($v, -1) != ']');
00055         }
00056         $p = (array_filter($arr, "low_daemon"));
00057         return implode($p);
00058 */      
00059     
00060 }
00061 
00062 function getSerial() {
00063   $s=exec("/bin/bootblocktool -x SERNO");
00064   return substr($s,0,2).":".substr($s,2,2).":".substr($s,4,2).":".substr($s,6,2).":".substr($s,8,2).":".substr($s,10,2);
00065 }
00066 
00067 function fpga() {
00068         $arr = array();
00069         $s = exec("/bin/fpcf -r 13", $arr);
00070         return implode($arr);
00071 }
00072 
00073 function cat_interupts() {
00074         $arr = array();
00075         $s = exec("/bin/cat /proc/interrupts", $arr);
00076         echo implode($arr);
00077 }
00078 
00079 function cat_sdram() {
00080         $arr = array();
00081         $s = exec("/bin/cat /var/log/fpga_sdram_phase", $arr);
00082         echo implode($arr);
00083 }
00084 
00085 
00086 function read_ip() {
00087         $myFile = "/etc/conf.d/net.eth0";
00088         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00089         $data = fread($fr, filesize($myFile));
00090         fclose($fr);
00091         $pattern = '/IP=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/';
00092         preg_match($pattern, $data, $matches);
00093         $string = implode($matches);
00094         $output = substr($string, 3);
00095         return $output;
00096 }
00097 
00098 
00099 function read_broadcast() {
00100         $myFile = "/etc/conf.d/net.eth0";
00101         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00102         $data = fread($fr, filesize($myFile));
00103         fclose($fr);
00104         $pattern = '/BROADCAST=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/';
00105         preg_match($pattern, $data, $matches);
00106         $string = implode($matches);
00107         $output = substr($string, 10);
00108         return $output;
00109 }
00110 
00111 
00112 function read_gateway() {
00113         $myFile = "/etc/conf.d/net.eth0";
00114         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00115         $data = fread($fr, filesize($myFile));
00116         fclose($fr);
00117         $pattern = '/GATEWAY=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/';
00118         preg_match($pattern, $data, $matches);
00119         $string = implode($matches);
00120         $output = substr($string, 8);
00121         return $output;
00122         }
00123 
00124 
00125 function read_netmask() {
00126         $myFile = "/etc/conf.d/net.eth0";
00127         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00128         $data = fread($fr, filesize($myFile));
00129         fclose($fr);
00130         $pattern = '/NETMASK=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/';
00131         preg_match($pattern, $data, $matches);
00132         $string = implode($matches);
00133         $output = substr($string, 8);
00134         return $output;
00135 }
00136 
00137 function change_gateway() {
00138         global $new_gateway;
00139         echo "<p>" . $new_gateway . " is set </p>";
00140         $myFile = "/etc/conf.d/net.eth0";
00141         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00142         $data = fread($fr, filesize($myFile));
00143         $replace_value = "GATEWAY=" . $new_gateway;
00144         fclose($fr);
00145         preg_match('/GATEWAY=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/', $data, $matches);
00146         $replace = str_replace($matches[0], $replace_value, $data);
00147         $fw = fopen($myFile, 'w') or die ("Can't open file in write mode");
00148         fwrite($fw, $replace);
00149         fclose($fw);
00150         }
00151         
00152 function change_broadcast() {
00153         global $new_broadcast;
00154         echo "<p>" . $new_broadcast . " is set </p>";
00155         $myFile = "/etc/conf.d/net.eth0";
00156         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00157         $data = fread($fr, filesize($myFile));
00158         $replace_value = "BROADCAST=" . $new_broadcast;
00159         fclose($fr);
00160         preg_match('/BROADCAST=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/', $data, $matches);
00161         $replace = str_replace($matches[0], $replace_value, $data);
00162         $fw = fopen($myFile, 'w') or die ("Can't open file in write mode");
00163         fwrite($fw, $replace);
00164         fclose($fw);
00165         }
00166         
00167 function changeIP() {
00168         global $new_ip;
00169         echo "<p>" . $new_ip . " is set </p>"; //debugging
00170         global $new_ip;
00171         $myFile = "/etc/conf.d/net.eth0";
00172         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00173         $data = fread($fr, filesize($myFile));
00174         $replace_value = "IP=" . $new_ip;
00175         fclose($fr);
00176         preg_match('/IP=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/', $data, $matches);
00177         $replace = str_replace($matches[0], $replace_value, $data);
00178         $fw = fopen($myFile, 'w') or die ("Can't open file in write mode");
00179         fwrite($fw, $replace);
00180         fclose($fw);
00181         }
00182 
00183 function change_netmask() {
00184         global $new_netmask;
00185         echo "<p>" . $new_netmask . " is set </p>";
00186         $myFile = "/etc/conf.d/net.eth0";
00187         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00188         $data = fread($fr, filesize($myFile));
00189         $replace_value = "NETMASK=" . $new_netmask;
00190         fclose($fr);
00191         preg_match('/NETMASK=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/', $data, $matches);
00192         $replace = str_replace($matches[0], $replace_value, $data);
00193         $fw = fopen($myFile, 'w') or die ("Can't open file in write mode");
00194         fwrite($fw, $replace);
00195         fclose($fw);
00196         }
00197         
00198 
00199 function read_date() {
00200         exec("/bin/date", $arr);
00201         return implode($arr);
00202 }
00203 
00204 function read_load() {
00205         exec("/bin/cat /proc/loadavg", $arr);
00206         return implode($arr);
00207         }
00208         
00209 function daemon_counter(){
00210  #this is simply to count the number of keys for use by other functions
00211                 exec("ps -w", $raw_array);
00212                 unset($raw_array['0']); 
00213                         function rm($v){
00214                          return (substr($v, -1) != ']');
00215                                 }
00216                          $no_low_level = array_values(array_filter($raw_array, "rm"));          
00217         $x = count($no_low_level);
00218                 return $x;
00219 }       
00220 $number_of_daemons = daemon_counter();
00221 
00222 /*      Do Not Erase
00223 function daemon_pid(){
00224  #removes low level daemons and trash data from the array.
00225                 exec("ps -w", $raw_array);
00226                 unset($raw_array['0']); 
00227                         function low_daemon($v){
00228                          return (substr($v, -1) != ']');
00229                                 }
00230                          $no_low_level = array_values(array_filter($raw_array, "low_daemon"));
00231                         
00232         //reads pid numbers from each key in the array and puts them in an array
00233         //of their own to be put into xml               
00234         $x = count($no_low_level); #$x = number of keys
00235         $y = $x * 2; #counter for the loop
00236         $key = -1; #makes names for the array keys
00237         $pid_num_array = array();
00238                 while($x < $y) {
00239                 $x += 1;
00240                 $key += 1;
00241                 $pattern = '/\b[A-Za-z0-9]*\b/';
00242                 preg_match($pattern, $no_low_level[$key], $match);
00243                 $string = implode($match);
00244                 $pid_num_array[$key] = $string;
00245                 }
00246                 //$prepared = implode (" ", $pid_num_array);
00247                 return $pid_num_array;
00248 }
00249 * jeff, please learn to test functions before committing*/
00250 //$pid = daemon_pid();
00251 
00252 //Generate xml funtions begin
00253 /*
00254 $xml = new SimpleXMLElement("<?xml version='1.0'?><cam_info/>");
00255 function gen_dynamic_xml() {
00256   //global $xml;
00257         global $pid;
00258         global $number_of_daemons;
00259         $keynum = $number_of_daemons * 2;
00260         $key = -1;
00261 
00262         $xml->addChild ('daemons');
00263         
00264         while($number_of_daemons < $keynum) {
00265         $number_of_daemons += 1;
00266         $key += 1;
00267         $pid = $xml->daemons->addChild('pid' . $pid[$key]);
00268         }
00269                 $rslt=$xml->asXML();
00270 
00271         header("Content-Type: text/xml");
00272         header("Content-Length: ".strlen($rslt)."\n");
00273         header("Pragma: no-cache\n");
00274         printf($rslt);
00275         
00276         print_r($pid);
00277 }
00278 */
00279         
00280 $xml = new SimpleXMLElement("<?xml version='1.0'?><cam_info/>");
00281 function gen_static_xml() {
00282         global $xml;
00283         $xml->addChild ('software');
00284         $version = $xml->software->addChild('version',soft_ver());
00285         $network = $xml->software->addChild('network');
00286                 $ip = $xml->software->network->addChild('ip', read_ip());
00287                 $netmask = $xml->software->network->addChild('netmask', read_netmask());
00288                 $broadcast = $xml->software->network->addChild('broadcast', read_broadcast());
00289                 $gateway = $xml->software->network->addChild('gateway', read_gateway());
00290         $date = $xml->software->addChild('cam_date',read_date());
00291                 //$day = $xml->software->date->addChild('day');
00292                 //$month = $xml->software->date->addChild('month');
00293                 //$year = $xml->software->date->addChild('year');
00294                 //$time = $xml->software->date->addChild('time');
00295         $proc_info = $xml->software->addChild('proc_info');
00296         
00297         //hacked together ps display 
00298         //--needs refactoring into seperate function--
00299         //begin hack
00300         $load = $xml->software->proc_info->addChild('load',read_load());
00301         //array variable types need to be changed into strings
00302         //$proc = $xml->software->proc_info->addChild('proc',ps());
00303         exec('ps -w',$ps_array);
00304     unset($ps_array['0']); //get rid of the original identifiers
00305 
00306     function kill_low_daemon($jj){ 
00307         //junk the unimportant stuff
00308         return (substr($jj, -1) != ']');
00309     }
00310     $proc_array = array_values(array_filter($ps_array, "kill_low_daemon"));
00311 
00312     //print_r($proc_array);
00313     $xml->addChild('ps');
00314     foreach($proc_array as $node){
00315         $proc = $xml->ps->addChild('proc',$node);
00316         //right here k
00317     }
00318     //end hack
00319     
00320         $xml->addChild ('hardware');
00321         $ser_no = $xml->hardware->addChild('serial_number',getSerial());
00322 
00323         $rslt=$xml->asXML();
00324 
00325         header("Content-Type: text/xml");
00326         header("Content-Length: ".strlen($rslt)."\n");
00327         header("Pragma: no-cache\n");
00328         printf($rslt);
00329 }
00330 
00331 
00332 
00333 function gen_dynamic_xml($req) {
00334         //echo $req." requested";
00335         $xml = new SimpleXMLElement(gen_static_xml());
00336         //global $xml;
00337         switch ($req) {
00338                 case 'ps':
00339                         $xml->addChild('software');
00340                         $proc_info = $xml->software->addChild('proc_info');
00341                         $proc = $xml->software->proc_info->addChild('proc',"<pre>".ps()."</pre>");
00342                         break;
00343                 case 'load':
00344                         $load = $xml->software->proc_info->addChild('load',read_load());
00345                         break;
00346                 }
00347         //dynamic xml will go here based on case/request
00348         header("Content-Type: text/xml");
00349         header("Content-Length: ".strlen($rslt)."\n");
00350         header("Pragma: no-cache\n");
00351         $rslt=$xml->asXML();
00352         printf($rslt);
00353 }
00354 //initial GET and POST variables for global use
00355 $req = $_GET['req'];
00356 $q = $_GET['q'];
00357 
00358 if ($_GET['new_ip'] == null) {
00359         $new_ip = $_POST['new_ip'];
00360 } elseif (!($_GET['new_ip'] == null)) {
00361         $new_ip = $_GET['new_ip'];
00362 }
00363 
00364 if ($_GET['new_netmask'] == null) {
00365         $new_netmask = $_POST['new_netmask'];
00366 } elseif (!($_GET['new_netmask'] == null)) {
00367         $new_netmask = $_GET['new_netmask'];
00368 }
00369 
00370 if ($_GET['new_gateway'] == null) {
00371         $new_gateway = $_POST['new_gateway'];
00372 } elseif (!($_GET['new_gateway'] == null)) {
00373         $new_gateway = $_GET['new_gateway'];
00374 }
00375 
00376 if ($_GET['new_broadcast'] == null) {
00377         $new_broadcast = $_POST['new_broadcast'];
00378 } elseif (!($_GET['new_broadcast'] == null)) {
00379         $new_broadcast = $_GET['new_broadcast'];
00380 }
00381 
00382 if ($req == "" && $req == null && $q == "" && $q == null) {
00383         print_use();
00384 } elseif ($q == 'change') {
00385                 switch ($req) {
00386                         case "ip":
00387                                 changeIP();
00388                                 echo "ip will be changed to " . $new_ip . " on reboot<p><a href='./UI.html'>return to main</a></p>";
00389                                 break;
00390                         case "netmask":
00391                                 change_netmask();
00392                                 echo "netmask will be changed to " . $new_netmask . " on reboot<p><a href='./UI.html'>return to main</a></p>";
00393                                 break;
00394                         case "gateway":
00395                                 change_gateway();
00396                                 echo "gateway will be changed to " . $new_gateway . " on reboot<p><a href='./UI.html'>return to main</a></p>";
00397                                 break;
00398                         case "broadcast":
00399                                 change_broadcast();
00400                                 echo "broadcast will be changed to " . $new_broadcast . " on reboot<p><a href='./UI.html'>return to main</a></p>";
00401                                 break;
00402                 }
00403         } else {
00404                 switch ($q) {
00405                         case "dynamic":
00406                                 gen_dynamic_xml($req);
00407                                 break;
00408                         case "static" || "compile":
00409                                 gen_static_xml();
00410                                 break;
00411                         }
00412                 }
00413 function print_use() {
00414         echo "<head><style type='text/css'>code {font-family:monospace} .variable{color:red}</style></head><body>";
00415         echo "<p>Usage:</p>";
00416         echo "<p><code>?q=static</code> or <code>?q=compile</code> &raquo; 
00417         print a compiled information sheet of every function</p>";
00418         echo "<p><code>?q=dynamic&amp;req=</code> or <code></code> &raquo; 
00419         print current information about one function</p>";
00420         
00421         echo "<p>These Network Functions cannot be strung together using 
00422         and in any case require a camera restart. They do not verify if the
00423         network selection is valid, so use at your own risk!</p>";
00424         
00425         echo "<p><code>?q=change&amp;req=ip</code> or 
00426         <code>?q=change&amp;req=ip&amp;new_ip=</code><span class='variable'>new ip</span> &raquo; change 
00427         the ip to the POST variable '<span class='variable'>new_ip</span>' or whatever is specified in the url</p>";
00428         echo "<p><code>?q=change&amp;req=netmask</code> or 
00429         <code>?q=change&amp;req=netmask&amp;new_netmask=</code><span class='variable'>new netmask</span> 
00430         &raquo; change the netmask to the POST variable '<span class='variable'>new_netmask</span>' or 
00431         whatever is specified in the url</p>";
00432         echo "<p><code>?q=change&amp;req=gateway</code> or 
00433         <code>?q=change&amp;req=gateway&amp;new_gateway=</code><span class='variable'>new gateway</span> 
00434         &raquo; change the gateway to the POST variable '<span class='variable'>new_gateway</span>' or 
00435         whatever is specified in the url</p>";
00436         echo "<p><code>?q=change&amp;req=broadcast</code> or
00437         <code>?q=change&amp;req=broadcast&amp;new_broadcast=</code><span class='variable'>new broadcast</span>
00438         &raquo; change the broadcast to the POST variable '<span class='variable'>new_broadcast</span>' or 
00439         whatever is specified in the url</p>";
00440         echo "</body>";
00441 }

Generated on Thu Aug 7 16:19:00 2008 for elphel by  doxygen 1.5.1