packages/web/353/control_panel/backend.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  *              Copyright 2008 Elphel <elphel.com>
00005  *      This program is free software; you can redistribute it and/or modify
00006  *      it under the terms of the GNU General Public License as published by
00007  *      the Free Software Foundation; either version 2 of the License, or
00008  *      (at your option) any later version.
00009  *      
00010  *      This program is distributed in the hope that it will be useful,
00011  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *      GNU General Public License for more details.
00014  *      
00015  *      You should have received a copy of the GNU General Public License
00016  *      along with this program; if not, write to the Free Software
00017  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  *      MA 02110-1301, USA.
00019  */
00020 
00021 /*
00022  * FEATURES
00023  * This backend script features many ways to configure the camera for 
00024  * different uses. For example, one can, using this interface, 
00025  * change the ip of their camera, or set up a new netmask. This backend
00026  * also allows the user to interact with it using simple GET requests.
00027  * further documentation to follow.
00028  * 
00029  */
00030 
00031 
00032 function getSoftware() {
00033         return "<a href='/docs/" . exec('ls /usr/html/docs') . "'>" . exec('ls /usr/html/docs') . "</a> (get source)";
00034         //return $to_out;
00035         }
00036 
00037 function soft_ver() {
00038         exec('/bin/ls /usr/html/docs', $arr);
00039         return implode($arr);
00040 }
00041 
00042 function ps() {
00043         exec('ps -w', $arr); 
00044         function low_daemon($v)
00045         {
00046          return (substr($v, -1) != ']');
00047         }
00048         $p = (array_filter($arr, "low_daemon"));
00049         return implode($p);
00050 }
00051 
00052 function getSerial() {
00053   $s=exec("/bin/bootblocktool -x SERNO");
00054   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);
00055 }
00056 
00057 function fpga() {
00058         $arr = array();
00059         $s = exec("/bin/fpcf -r 13", $arr);
00060         return implode($arr);
00061 }
00062 
00063 function cat_interupts() {
00064         $arr = array();
00065         $s = exec("/bin/cat /proc/interrupts", $arr);
00066         echo implode($arr);
00067 }
00068 
00069 function cat_sdram() {
00070         $arr = array();
00071         $s = exec("/bin/cat /var/log/fpga_sdram_phase", $arr);
00072         echo implode($arr);
00073 }
00074 
00075 
00076 function read_ip() {
00077         $myFile = "/etc/conf.d/net.eth0";
00078         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00079         $data = fread($fr, filesize($myFile));
00080         fclose($fr);
00081         $pattern = '/IP=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/';
00082         preg_match($pattern, $data, $matches);
00083         $string = implode($matches);
00084         $output = substr($string, 3);
00085         return $output;
00086 }
00087 
00088 
00089 function read_broadcast() {
00090         $myFile = "/etc/conf.d/net.eth0";
00091         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00092         $data = fread($fr, filesize($myFile));
00093         fclose($fr);
00094         $pattern = '/BROADCAST=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/';
00095         preg_match($pattern, $data, $matches);
00096         $string = implode($matches);
00097         $output = substr($string, 10);
00098         return $output;
00099 }
00100 
00101 
00102 function read_gateway() {
00103         $myFile = "/etc/conf.d/net.eth0";
00104         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00105         $data = fread($fr, filesize($myFile));
00106         fclose($fr);
00107         $pattern = '/GATEWAY=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/';
00108         preg_match($pattern, $data, $matches);
00109         $string = implode($matches);
00110         $output = substr($string, 8);
00111         return $output;
00112         }
00113 
00114 
00115 function read_netmask() {
00116         $myFile = "/etc/conf.d/net.eth0";
00117         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00118         $data = fread($fr, filesize($myFile));
00119         fclose($fr);
00120         $pattern = '/NETMASK=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/';
00121         preg_match($pattern, $data, $matches);
00122         $string = implode($matches);
00123         $output = substr($string, 8);
00124         return $output;
00125 }
00126 
00127 function change_gateway() {
00128         global $new_gateway;
00129         echo "<p>" . $new_gateway . " is set </p>";
00130         $myFile = "/etc/conf.d/net.eth0";
00131         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00132         $data = fread($fr, filesize($myFile));
00133         $replace_value = "GATEWAY=" . $new_gateway;
00134         fclose($fr);
00135         preg_match('/GATEWAY=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/', $data, $matches);
00136         $replace = str_replace($matches[0], $replace_value, $data);
00137         $fw = fopen($myFile, 'w') or die ("Can't open file in write mode");
00138         fwrite($fw, $replace);
00139         fclose($fw);
00140         }
00141         
00142 function change_broadcast() {
00143         global $new_broadcast;
00144         echo "<p>" . $new_broadcast . " is set </p>";
00145         $myFile = "/etc/conf.d/net.eth0";
00146         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00147         $data = fread($fr, filesize($myFile));
00148         $replace_value = "BROADCAST=" . $new_broadcast;
00149         fclose($fr);
00150         preg_match('/BROADCAST=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/', $data, $matches);
00151         $replace = str_replace($matches[0], $replace_value, $data);
00152         $fw = fopen($myFile, 'w') or die ("Can't open file in write mode");
00153         fwrite($fw, $replace);
00154         fclose($fw);
00155         }
00156         
00157 function changeIP() {
00158         global $new_ip;
00159         echo "<p>" . $new_ip . " is set </p>"; //debugging
00160         global $new_ip;
00161         $myFile = "/etc/conf.d/net.eth0";
00162         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00163         $data = fread($fr, filesize($myFile));
00164         $replace_value = "IP=" . $new_ip;
00165         fclose($fr);
00166         preg_match('/IP=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/', $data, $matches);
00167         $replace = str_replace($matches[0], $replace_value, $data);
00168         $fw = fopen($myFile, 'w') or die ("Can't open file in write mode");
00169         fwrite($fw, $replace);
00170         fclose($fw);
00171         }
00172 
00173 function change_netmask() {
00174         global $new_netmask;
00175         echo "<p>" . $new_netmask . " is set </p>";
00176         $myFile = "/etc/conf.d/net.eth0";
00177         $fr = fopen($myFile, 'r') or die("Can't open file in read mode");
00178         $data = fread($fr, filesize($myFile));
00179         $replace_value = "NETMASK=" . $new_netmask;
00180         fclose($fr);
00181         preg_match('/NETMASK=\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/', $data, $matches);
00182         $replace = str_replace($matches[0], $replace_value, $data);
00183         $fw = fopen($myFile, 'w') or die ("Can't open file in write mode");
00184         fwrite($fw, $replace);
00185         fclose($fw);
00186         }
00187         
00188 
00189 function read_date() {
00190         exec("/bin/date", $arr);
00191         return implode($arr);
00192 }
00193 
00194 function read_load() {
00195         exec("/bin/cat /proc/loadavg", $arr);
00196         return implode($arr);
00197         }
00198         
00199 function daemon_counter(){
00200  #this is simply to count the number of keys for use by other functions
00201                 exec("ps -w", $raw_array);
00202                 unset($raw_array['0']); 
00203                         function rm($v){
00204                          return (substr($v, -1) != ']');
00205                                 }
00206                          $no_low_level = array_values(array_filter($raw_array, "rm"));          
00207         $x = count($no_low_level);
00208                 return $x;
00209 }       
00210 $number_of_daemons = daemon_counter();
00211 
00212 /*      Do Not Erase
00213 function daemon_pid(){
00214  #removes low level daemons and trash data from the array.
00215                 exec("ps -w", $raw_array);
00216                 unset($raw_array['0']); 
00217                         function low_daemon($v){
00218                          return (substr($v, -1) != ']');
00219                                 }
00220                          $no_low_level = array_values(array_filter($raw_array, "low_daemon"));
00221                         
00222         //reads pid numbers from each key in the array and puts them in an array
00223         //of their own to be put into xml               
00224         $x = count($no_low_level); #$x = number of keys
00225         $y = $x * 2; #counter for the loop
00226         $key = -1; #makes names for the array keys
00227         $pid_num_array = array();
00228                 while($x < $y) {
00229                 $x += 1;
00230                 $key += 1;
00231                 $pattern = '/\b[A-Za-z0-9]*\b/';
00232                 preg_match($pattern, $no_low_level[$key], $match);
00233                 $string = implode($match);
00234                 $pid_num_array[$key] = $string;
00235                 }
00236                 //$prepared = implode (" ", $pid_num_array);
00237                 return $pid_num_array;
00238 }
00239 * jeff, please learn to test functions before committing*/
00240 //$pid = daemon_pid();
00241 
00242 //Generate xml funtions begin
00243 /*
00244 $xml = new SimpleXMLElement("<?xml version='1.0'?><cam_info/>");
00245 function gen_dynamic_xml() {
00246   //global $xml;
00247         global $pid;
00248         global $number_of_daemons;
00249         $keynum = $number_of_daemons * 2;
00250         $key = -1;
00251 
00252         $xml->addChild ('daemons');
00253         
00254         while($number_of_daemons < $keynum) {
00255         $number_of_daemons += 1;
00256         $key += 1;
00257         $pid = $xml->daemons->addChild('pid' . $pid[$key]);
00258         }
00259                 $rslt=$xml->asXML();
00260 
00261         header("Content-Type: text/xml");
00262         header("Content-Length: ".strlen($rslt)."\n");
00263         header("Pragma: no-cache\n");
00264         printf($rslt);
00265         
00266         print_r($pid);
00267 }
00268 */      
00269 $xml = new SimpleXMLElement("<?xml version='1.0'?><cam_info/>");
00270 function gen_static_xml() {
00271         global $xml;
00272         $xml->addChild ('software');
00273         $version = $xml->software->addChild('version',soft_ver());
00274         $network = $xml->software->addChild('network');
00275                 $ip = $xml->software->network->addChild('ip', read_ip());
00276                 $netmask = $xml->software->network->addChild('netmask', read_netmask());
00277                 $broadcast = $xml->software->network->addChild('broadcast', read_broadcast());
00278                 $gateway = $xml->software->network->addChild('gateway', read_gateway());
00279         $date = $xml->software->addChild('cam_date',read_date());
00280                 //$day = $xml->software->date->addChild('day');
00281                 //$month = $xml->software->date->addChild('month');
00282                 //$year = $xml->software->date->addChild('year');
00283                 //$time = $xml->software->date->addChild('time');
00284         $proc_info = $xml->software->addChild('proc_info');
00285         $load = $xml->software->proc_info->addChild('load',read_load());
00286         //array variable types need to be changed into strings
00287         $proc = $xml->software->proc_info->addChild('proc',"<pre>".ps()."</pre>");
00288         $xml->addChild ('hardware');
00289         $ser_no = $xml->hardware->addChild('serial_number',getSerial());
00290 
00291         $rslt=$xml->asXML();
00292 
00293         header("Content-Type: text/xml");
00294         header("Content-Length: ".strlen($rslt)."\n");
00295         header("Pragma: no-cache\n");
00296         printf($rslt);
00297 }
00298 
00299 
00300 
00301 function gen_dynamic_xml($req) {
00302         //echo $req." requested";
00303         $xml = new SimpleXMLElement(gen_static_xml());
00304         //global $xml;
00305         switch ($req) {
00306                 case 'ps':
00307                         $xml->addChild('software');
00308                         $proc_info = $xml->software->addChild('proc_info');
00309                         $proc = $xml->software->proc_info->addChild('proc',"<pre>".ps()."</pre>");
00310                         break;
00311                 case 'load':
00312                         $load = $xml->software->proc_info->addChild('load',read_load());
00313                         break;
00314                 }
00315         //dynamic xml will go here based on case/request
00316         header("Content-Type: text/xml");
00317         header("Content-Length: ".strlen($rslt)."\n");
00318         header("Pragma: no-cache\n");
00319         $rslt=$xml->asXML();
00320         printf($rslt);
00321 }
00322 //initial GET and POST variables for global use
00323 $req = $_GET['req'];
00324 $q = $_GET['q'];
00325 
00326 if ($_GET['new_ip'] == null) {
00327         $new_ip = $_POST['new_ip'];
00328 } elseif (!($_GET['new_ip'] == null)) {
00329         $new_ip = $_GET['new_ip'];
00330 }
00331 
00332 if ($_GET['new_netmask'] == null) {
00333         $new_netmask = $_POST['new_netmask'];
00334 } elseif (!($_GET['new_netmask'] == null)) {
00335         $new_netmask = $_GET['new_netmask'];
00336 }
00337 
00338 if ($_GET['new_gateway'] == null) {
00339         $new_gateway = $_POST['new_gateway'];
00340 } elseif (!($_GET['new_gateway'] == null)) {
00341         $new_gateway = $_GET['new_gateway'];
00342 }
00343 
00344 if ($_GET['new_broadcast'] == null) {
00345         $new_broadcast = $_POST['new_broadcast'];
00346 } elseif (!($_GET['new_broadcast'] == null)) {
00347         $new_broadcast = $_GET['new_broadcast'];
00348 }
00349 
00350 if ($req == "" && $req == null && $q == "" && $q == null) {
00351         print_use();
00352 } elseif ($q == 'change') {
00353                 switch ($req) {
00354                         case "ip":
00355                                 changeIP();
00356                                 echo "ip will be changed to " . $new_ip . " on reboot<p><a href='./UI.html'>return to main</a></p>";
00357                                 break;
00358                         case "netmask":
00359                                 change_netmask();
00360                                 echo "netmask will be changed to " . $new_netmask . " on reboot<p><a href='./UI.html'>return to main</a></p>";
00361                                 break;
00362                         case "gateway":
00363                                 change_gateway();
00364                                 echo "gateway will be changed to " . $new_gateway . " on reboot<p><a href='./UI.html'>return to main</a></p>";
00365                                 break;
00366                         case "broadcast":
00367                                 change_broadcast();
00368                                 echo "broadcast will be changed to " . $new_broadcast . " on reboot<p><a href='./UI.html'>return to main</a></p>";
00369                                 break;
00370                 }
00371         } else {
00372                 switch ($q) {
00373                         case "dynamic":
00374                                 gen_dynamic_xml($req);
00375                                 break;
00376                         case "static" || "compile":
00377                                 gen_static_xml();
00378                                 break;
00379                         }
00380                 }
00381 function print_use() {
00382         echo "<head><style type='text/css'>code {font-family:monospace} .variable{color:red}</style></head><body>";
00383         echo "<p>Usage:</p>";
00384         echo "<p><code>?q=static</code> or <code>?q=compile</code> &raquo; 
00385         print a compiled information sheet of every function</p>";
00386         echo "<p><code>?q=dynamic&amp;req=</code> or <code></code> &raquo; 
00387         print current information about one function</p>";
00388         
00389         echo "<p>These Network Functions cannot be strung together using 
00390         and in any case require a camera restart. They do not verify if the
00391         network selection is valid, so use at your own risk!</p>";
00392         
00393         echo "<p><code>?q=change&amp;req=ip</code> or 
00394         <code>?q=change&amp;req=ip&amp;new_ip=</code><span class='variable'>new ip</span> &raquo; change 
00395         the ip to the POST variable '<span class='variable'>new_ip</span>' or whatever is specified in the url</p>";
00396         echo "<p><code>?q=change&amp;req=netmask</code> or 
00397         <code>?q=change&amp;req=netmask&amp;new_netmask=</code><span class='variable'>new netmask</span> 
00398         &raquo; change the netmask to the POST variable '<span class='variable'>new_netmask</span>' or 
00399         whatever is specified in the url</p>";
00400         echo "<p><code>?q=change&amp;req=gateway</code> or 
00401         <code>?q=change&amp;req=gateway&amp;new_gateway=</code><span class='variable'>new gateway</span> 
00402         &raquo; change the gateway to the POST variable '<span class='variable'>new_gateway</span>' or 
00403         whatever is specified in the url</p>";
00404         echo "<p><code>?q=change&amp;req=broadcast</code> or
00405         <code>?q=change&amp;req=broadcast&amp;new_broadcast=</code><span class='variable'>new broadcast</span>
00406         &raquo; change the broadcast to the POST variable '<span class='variable'>new_broadcast</span>' or 
00407         whatever is specified in the url</p>";
00408         echo "</body>";
00409 }
00410 
00411 
00412 ?>

Generated on Fri Nov 28 00:06:24 2008 for elphel by  doxygen 1.5.1