cvs: pear /Net_Wifi Wifi.php package.xml /Net_Wifi/Net Wifi.php
| From: | Christian Weiske | Date: | Fri, 27 Oct 2006 20:54:13 +0000 |
| Subject: | cvs: pear /Net_Wifi Wifi.php package.xml /Net_Wifi/Net Wifi.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-42835@lists.php.net to get a copy of this message | ||
cweiske Fri Oct 27 20:54:13 2006 UTC
Added files:
/pear/Net_Wifi/Net Wifi.php
Removed files:
/pear/Net_Wifi Wifi.php
Modified files:
/pear/Net_Wifi package.xml
Log:
- Switching to package.xml version 2
- Moving Wifi.php into Net/ directory to reflect directory
structure
http://cvs.php.net/viewvc.cgi/pear/Net_Wifi/package.xml?r1=1.1&r2=1.2&diff_format=u Index: pear/Net_Wifi/package.xml diff -u pear/Net_Wifi/package.xml:1.1 pear/Net_Wifi/package.xml:1.2 --- pear/Net_Wifi/package.xml:1.1 Wed Jul 13 19:39:29 2005 +++ pear/Net_Wifi/package.xml Fri Oct 27 20:54:13 2006 @@ -1,53 +1,89 @@ -<?xml version="1.0" encoding="ISO-8859-1" ?> -<!DOCTYPE package SYSTEM "http://pear.php.net/dtd/package-1.0"> -<package version="1.0"> - <name>Net_Wifi</name> - <summary>Scans for wireless networks</summary> - <description>Net_Wifi utilizes the command line tools "iwconfig" and "iwlist" to get information - about wireless lan interfaces on the system and the current configuration. - The class enables you to scan for wireless networks - and get a bunch of information about them.</description> - <license>PHP License</license> - - <maintainers> - <maintainer> - <user>cweiske</user> - <role>lead</role> - <name>Christian Weiske</name> - <email>cweiske@cweiske.de</email> - </maintainer> - </maintainers> +<?xml version="1.0" encoding="UTF-8"?> +<package packagerversion="1.4.11" version="2.0" + xmlns="http://pear.php.net/dtd/package-2.0" + xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 + http://pear.php.net/dtd/tasks-1.0.xsd + http://pear.php.net/dtd/package-2.0 + http://pear.php.net/dtd/package-2.0.xsd"> + <name>Net_Wifi</name> + <channel>pear.php.net</channel> + <summary>Scans for wireless networks</summary> + <description> + Net_Wifi utilizes the command line tools + "iwconfig" and "iwlist" to get information + about wireless lan interfaces on the system and the current configuration. + The class enables you to scan for wireless networks + and get a bunch of information about them. + </description> + <lead> + <name>Christian Weiske</name> + <user>cweiske</user> + <email>cweiske@cweiske.de</email> + <active>yes</active> + </lead> + <date>2006-10-27</date> + <time>22:50:37</time> + + <version> + <release>0.1.0</release> + <api>0.1.0</api> + </version> + <stability> + <release>beta</release> + <api>beta</api> + </stability> + <license uri="http://www.php.net/license">PHP License</license> + + <notes> + Initial release + </notes> + + <contents> + <dir name="/"> + <dir name="Net"> + <file name="Wifi.php" role="php" /> + </dir> + <dir name="examples"> + <file baseinstalldir="Net" name="Wifi_usage.php" role="doc" /> + </dir> + <dir name="tests"> + <file baseinstalldir="Net" name="runtest.php" role="test" /> + <file baseinstalldir="Net" name="Wifi_testcase.php" role="test" /> + </dir> + </dir> + </contents> + + <dependencies> + <required> + <php> + <min>4.0.0</min> + </php> + <pearinstaller> + <min>1.4.0b1</min> + </pearinstaller> + </required> + </dependencies> + + <phprelease /> + <changelog> <release> - <version>0.1.0</version> - <date>2005-07-13</date> - <notes> - Initial release - </notes> - <state>beta</state> - <filelist> - <dir name="/" baseinstalldir="Net" role="php"> - <file md5sum="893c8d21e3615bb0107460f7a4ddd66d">Wifi.php</file> - <dir name="examples" role="doc"> - <file md5sum="e88e4ada9718d447c66d3bbb3ecb1407">Wifi_usage.php</file> - </dir> - <dir name="tests" role="test"> - <file name="runtest.php" md5sum="d341daa6754370b1eb7e76f85cf7a3ae"/> - <file name="Wifi_testcase.php" md5sum="8783567ef03ee283ab5e4f4ffb9cdff8"/> - </dir> - </dir> - </filelist> + <version> + <release>0.1.0</release> + <api>0.1.0</api> + </version> + <stability> + <release>beta</release> + <api>beta</api> + </stability> + <date>2005-07-13</date> + <license uri="http://www.php.net/license">PHP License</license> + <notes> + Initial release + </notes> </release> - <changelog> - <release> - <version>0.1.0</version> - <date>2005-07-13</date> - <notes> - Initial release - </notes> - <state>beta</state> - </release> - </changelog> - + </changelog> </package> http://cvs.php.net/viewvc.cgi/pear/Net_Wifi/Net/Wifi.php?view=markup&rev=1.1 Index: pear/Net_Wifi/Net/Wifi.php +++ pear/Net_Wifi/Net/Wifi.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * A class for scanning wireless networks and identifying * local wireless network interfaces * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @author Christian Weiske <cweiske@php.net> * @category Net * @package Net * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: Wifi.php,v 1.1 2006/10/27 20:54:13 cweiske Exp $ * * @date 2005-07-10 08:23 */ //required for System::which() functionality require_once('System.php'); /** * This class uses tools like iwconfig and iwlist to scan * for wireless networks * * @author Christian Weiske <cweiske@php.net> */ class Net_Wifi { /** * Various locations of programs * @var array */ var $arFileLocation = array( 'iwconfig' => '/usr/sbin/iwconfig', 'iwlist' => '/usr/sbin/iwlist', '/proc/net/wireless' => '/proc/net/wireless' ); /** * Constructor which tries to guess the paths of the tools */ function Net_Wifi() { //try to find the paths $iwconfig = System::which('iwconfig'); if ($iwconfig !== false) { $this->setPathIwconfig($iwconfig); } $iwlist = System::which('iwlist'); if ($iwlist !== false) { $this->setPathIwconfig($iwlist); } }//function Net_Wifi() /** * Returns an object with the current state of the interface (connected/not connected, AP,...). * * @access public * @param string The interface to check * @return Net_Wifi_Config The state information */ function getCurrentConfig($strInterface) { //get the plain config $arLines = array(); exec($this->arFileLocation['iwconfig'] . ' ' . escapeshellarg($strInterface), $arLines); $strAll = implode("\n", $arLines); return $this->parseCurrentConfig($strAll); }//function getCurrentConfig($strInterface) /** * Parses the iwconfig output to collect the current config information. * * @access protected * @param string The iwconfig output to parse * @return Net_Wifi_Config The current config object */ function parseCurrentConfig($strAll) { $objConfig = new Net_Wifi_Config(); $arMatches = array(); if (preg_match('/ESSID:"([^"]+)"/', $strAll, $arMatches)) { $objConfig->ssid = $arMatches[1]; } if (preg_match('/Access Point: ([0-9:A-F]{17})/', $strAll, $arMatches)) { $objConfig->ap = $arMatches[1]; } if (preg_match('/Nickname:"([^"]+)"/', $strAll, $arMatches)) { $objConfig->nick = $arMatches[1]; } if (strpos($strAll, 'Mode:Managed')) { $objConfig->mode = 'managed'; } else if (strpos($strAll, 'Mode:Ad-Hoc')) { $objConfig->mode = 'ad-hoc'; } if (preg_match('/Bit Rate=([0-9.]+) [mk]b\\/s/i', $strAll, $arMatches)) { $objConfig->rate = $arMatches[1]; } if (preg_match('/Power=([0-9]+) dBm/', $strAll, $arMatches)) { $objConfig->power = $arMatches[1]; } if (preg_match('/Signal level=(-?[0-9]+) dBm/', $strAll, $arMatches)) { $objConfig->rssi = $arMatches[1]; } if (preg_match('/(IEEE [0-9.]+[a-z])/', $strAll, $arMatches)) { $objConfig->protocol = $arMatches[1]; } //available in ipw2200 1.0.3 only if (strpos($strAll, 'radio off')) { $objConfig->activated = false; } if (strpos($strAll, 'unassociated') === false && $objConfig->ap != null && $objConfig->ap != '00:00:00:00:00:00') { $objConfig->associated = true; } return $objConfig; }//function parseCurrentConfig($strAll) /** * Checks if a network interface is connected to an access point. * * @access public * @param string The network interface to check * @return boolean If the interface is connected */ function isConnected($strInterface) { $objConfig = $this->getCurrentConfig($strInterface); return $objConfig->associated; }//function isConnected($strInterface) /** * Returns an array with the names/device files of all supported wireless lan devices. * * @access public * @return array Array with wireless interfaces as values */ function getSupportedInterfaces() { $arWirelessInterfaces = array(); if (file_exists($this->arFileLocation['/proc/net/wireless'])) { /** * use /proc/net/wireless */ $arLines = file($this->arFileLocation['/proc/net/wireless']); //begin with 3rd line if (count($arLines) > 2) { for ($nA = 2; $nA < count($arLines); $nA++) { $nPos = strpos($arLines[$nA], ':', 0); $strInterface = trim(substr($arLines[$nA], 0, $nPos)); $arWirelessInterfaces[] = $strInterface; } }//we've got more than 2 lines } else { /** * use iwconfig */ $arLines = array(); exec($this->arFileLocation['iwconfig'], $arLines); foreach ($arLines as $strLine) { if (trim($strLine[0]) != '' && strpos($strLine, 'no wireless extensions') === false) { //there is something $arWirelessInterfaces[] = substr($strLine, 0, strpos($strLine, ' ')); } }//foreach line }//use iwconfig return $arWirelessInterfaces; }//function getSupportedInterfaces() /** * Scans for access points / ad hoc cells and returns them. * * @access public * @param string The interface to use * @return array Array with cell information objects (Net_Wifi_Cell) */ function scan($strInterface) { $arLines = array(); exec($this->arFileLocation['iwlist'] . ' ' . escapeshellarg($strInterface) . ' scanning', $arLines); return $this->parseScan( $arLines); }//function scan($strInterface) /** * Parses the output of iwlist and returns the recognized cells. * * @access protected * @param array Lines of the iwlist output as an array * @return array Array with cell information objects */ function parseScan($arLines) { if (count($arLines) == 1) { //one line only -> no cells there return array(); } //split into cells $arCells = array(); $nCurrentCell = -1; $nCount = count($arLines); for ($nA = 1; $nA < $nCount; $nA++) { $strLine = trim($arLines[$nA]); if ($strLine == '') { continue; } if (substr($strLine, 0, 4) == 'Cell') { //we've got a new cell $nCurrentCell++; //get cell number $nCell = substr($strLine, 5, strpos($strLine, ' ', 5) - 5); $arCells[$nCurrentCell] = new Net_Wifi_Cell(); $arCells[$nCurrentCell]->cell = $nCell; //remove cell information from line for further interpreting $strLine = substr($strLine, strpos($strLine, '- ') + 2); } $nPos = strpos($strLine, ':'); $nPosEquals = strpos($strLine, '='); if ($nPosEquals !== false && ($nPos === false || $nPosEquals < $nPos)) { //sometimes there is a "=" instead of a ":" $nPos = $nPosEquals; } $nPos++; $strId = strtolower(substr($strLine, 0, $nPos - 1)); $strValue = trim(substr($strLine, $nPos)); switch ($strId) { case 'address': $arCells[$nCurrentCell]->mac = $strValue; break; case 'essid': if ($strValue[0] == '"') { //has quotes around $arCells[$nCurrentCell]->ssid = substr($strValue, 1, -1); } else { $arCells[$nCurrentCell]->ssid = $strValue; } break; case 'bit rate': $nRate = floatval(substr($strValue, 0, strpos($strValue, 'Mb/s'))); $arCells[$nCurrentCell]->rate = $nRate; $arCells[$nCurrentCell]->rates[] = $nRate; break; case 'protocol': $arCells[$nCurrentCell]->protocol = $strValue; break; case 'channel': $arCells[$nCurrentCell]->channel = intval($strValue); break; case 'encryption key': if ($strValue == 'on') { $arCells[$nCurrentCell]->encryption = true; } else { $arCells[$nCurrentCell]->encryption = false; } break; case 'mode': if (strtolower($strValue) == 'master') { $arCells[$nCurrentCell]->mode = 'master'; } else { $arCells[$nCurrentCell]->mode = 'ad-hoc'; } break; case 'signal level': $arCells[$nCurrentCell]->rssi = substr($strValue, 0, strpos($strValue, ' ')); break; case 'quality': $arData = explode(' ', $strValue); $arCells[$nCurrentCell]->quality = $arData[0]; if (trim($arData[1]) != '') { //bad hack $arLines[$nA] = $arData[1]; $nA--; if (isset($arData[2])) { $arLines[$nA - 1] = $arData[1]; $nA--; } } break; case 'frequency': $arCells[$nCurrentCell]->frequency = $strValue; break; case 'extra': $nPos = strpos($strValue, ':'); $strSubId = strtolower(trim(substr($strValue, 0, $nPos))); $strValue = trim(substr($strValue, $nPos + 1)); switch ($strSubId) { case 'rates (mb/s)': //1 2 5.5 11 54 $arRates = explode(' ', $strValue); //convert to float values foreach ($arRates as $nB => $strRate) { $arCells[$nCurrentCell]->rates[$nB] = floatval($strRate); } break; case 'signal': case 'rssi': //-53 dBm $arCells[$nCurrentCell]->rssi = intval(substr($strValue, 0, strpos($strValue, ' '))); break; case 'last beacon': //25ms ago $arCells[$nCurrentCell]->beacon = intval(substr($strValue, 0, strpos($strValue, 'ms'))); break; default: echo 'unknown iwconfig extra information: ' . $strSubId . "\r\n"; break; } break; default: echo 'unknown iwconfig information: ' . $strId . "\r\n"; break; } }//foreach line //not all outputs are sorted (note the 6) //Extra: Rates (Mb/s): 1 2 5.5 9 11 6 12 18 24 36 48 54 //additionally, some drivers have many single "Bit Rate:" fields instead of one big one foreach ($arCells as $nCurrentCell => $arData) { sort($arCells[$nCurrentCell]->rates); $arCells[$nCurrentCell]->rates = array_unique($arCells[$nCurrentCell]->rates); } return $arCells; }//function parseScan($arLines) /** * Tells the driver to use the access point with the given MAC address only. * * You can use "off" to enable automatic mode again without * changing the current AP, or "any" resp. "auto" to force * the card to re-associate with the currently best AP * * EXPERIMENTAL! WILL CHANGE IN FUTURE VERSIONS * @access public * @param string The interface to use * @param string The mac address of the access point * @return boolean True if setting was ok, false if not */ function connectToAccessPoint($strInterface, $strMac) { $arLines = array(); $nReturnVar = 0; exec($this->arFileLocation['iwconfig'] . ' ' . escapeshellarg($strInterface) . ' ap ' . escapeshellarg($strMac), $arLines, $nReturnVar); return $nReturnVar == 0; }//function connectToAccessPoint($strInterface, $strMac) /** * and now some dumb getters and setters */ /** * Returns the set path to /proc/wireless. * @access public * @return string The path to "/proc/net/wireless" */ function getPathProcWireless() { return $this->arFileLocation['/proc/net/wireless']; }//function getPathProcWireless() /** * Returns the set path to /proc/net/wireless. * @access public * @param string The new /proc/net/wireless path */ function setPathProcWireless( $strProcWireless) { $this->arFileLocation['/proc/net/wireless'] = $strProcWireless; }//function setPathProcWireless( $strProcWireless) /** * Returns the set path to iwconfig. * @access public * @return string The path to iwconfig */ function getPathIwconfig() { return $this->arFileLocation['iwconfig']; }//function getPathIwconfig() /** * Returns the set path to iwconfig. * @access public * @param string The new ifwconfig path */ function setPathIwconfig( $strPathIwconfig) { $this->arFileLocation['iwconfig'] = $strPathIwconfig; }//function setPathIwconfig( $strPathIwconfig) /** * Returns the set path to iwlist. * @access public * @return string The path to iwlist */ function getPathIwlist() { return $this->arFileLocation['iwlist']; }//function getPathIwlist() /** * Returns the set path to iwlist. * @access public * @param string The new iwlist path */ function setPathIwlist( $strPathIwlist) { $this->arFileLocation['iwlist'] = $strPathIwlist; }//function setPathIwlist( $strPathIwlist) }//class Net_Wifi /** * Cell information from a wifi scan. */ class Net_Wifi_Cell { /** * The current cell number. * Has nothing to say. * @var int */ var $cell = null; /** * MAC address of the cell (access point or ad-hoc station). * example: 00:40:05:28:EB:45 * @var string */ var $mac = null; /** * "Service Set IDentifier" of the cell which identifies the network. * Max. 32 alphanumeric characters * example: "My Network" (without quotes) * @var string */ var $ssid = null; /** * Network type. * can be "master" or "ad-hoc" (without quotes) * @var string */ var $mode = null; /** * Channel number used for communication. * number from 1 to 12 or so * @var int */ var $channel = null; /** * If encryption is used. * @var boolean */ var $encryption = null; /** * Channel frequency. * example: 2.412GHz * @var string */ var $frequency = null; /** * The protocol version used. * example: IEEE 802.11b * @var string */ var $protocol = null; /** * Bit rate which is used (when connected). * @var float */ var $rate = null; /** * Array of supported bit rates. * @var array(float) */ var $rates = array(); /** * Signal strength. * example: -59 * @var int */ var $rssi = null; /** * The time since the last beacon (time sync) frame has been sent, in ms. * @var int */ var $beacon = null; }//class Net_Wifi_Cell /** * Configuration settings of a wifi network interface */ class Net_Wifi_Config { /** * If the interface is activated. * Some notebooks have a button which deactivates wifi, this is recognized here. * Note that this setting can't be read by all drivers, and so * it's "true" if it can't be determined. You can be sure that it's deactivated * if this setting is false, but not that it's activated if it's true * @var boolean */ var $activated = true; /** * If the interface is connected to an access point or an ad-hoc network. * @var boolean */ var $associated = false; /** * MAC address of the associated access point. * @var string */ var $ap = null; /** * "Service Set IDentifier" of the cell which identifies current network. * Max. 32 alphanumeric characters * example: "My Network" (without quotes) * @var string */ var $ssid = null; /** * Network type. * Can be "master" or "ad-hoc" (without quotes) * @var string */ var $mode = null; /** * The nickname which the interface (computer) uses. * Something like a computer name * @var string */ var $nick = null; /** * The bit rate of the connection. * @var float */ var $rate = null; /** * Power setting of the interface. * @var int */ var $power = null; /** * Protocol version which is used for connection. * example: "IEEE 802.11g" without quotes * @var string */ var $protocol = null; /** * Signal strength. * example: -59 * @var int */ var $rssi = null; }//class Net_Wifi_Config ?>
http://cvs.php.net/viewvc.cgi/pear/Net_Wifi/package.xml?r1=1.1&r2=1.2&diff_format=u Index: pear/Net_Wifi/package.xml diff -u pear/Net_Wifi/package.xml:1.1 pear/Net_Wifi/package.xml:1.2 --- pear/Net_Wifi/package.xml:1.1 Wed Jul 13 19:39:29 2005 +++ pear/Net_Wifi/package.xml Fri Oct 27 20:54:13 2006 @@ -1,53 +1,89 @@ -<?xml version="1.0" encoding="ISO-8859-1" ?> -<!DOCTYPE package SYSTEM "http://pear.php.net/dtd/package-1.0"> -<package version="1.0"> - <name>Net_Wifi</name> - <summary>Scans for wireless networks</summary> - <description>Net_Wifi utilizes the command line tools "iwconfig" and "iwlist" to get information - about wireless lan interfaces on the system and the current configuration. - The class enables you to scan for wireless networks - and get a bunch of information about them.</description> - <license>PHP License</license> - - <maintainers> - <maintainer> - <user>cweiske</user> - <role>lead</role> - <name>Christian Weiske</name> - <email>cweiske@cweiske.de</email> - </maintainer> - </maintainers> +<?xml version="1.0" encoding="UTF-8"?> +<package packagerversion="1.4.11" version="2.0" + xmlns="http://pear.php.net/dtd/package-2.0" + xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 + http://pear.php.net/dtd/tasks-1.0.xsd + http://pear.php.net/dtd/package-2.0 + http://pear.php.net/dtd/package-2.0.xsd"> + <name>Net_Wifi</name> + <channel>pear.php.net</channel> + <summary>Scans for wireless networks</summary> + <description> + Net_Wifi utilizes the command line tools + "iwconfig" and "iwlist" to get information + about wireless lan interfaces on the system and the current configuration. + The class enables you to scan for wireless networks + and get a bunch of information about them. + </description> + <lead> + <name>Christian Weiske</name> + <user>cweiske</user> + <email>cweiske@cweiske.de</email> + <active>yes</active> + </lead> + <date>2006-10-27</date> + <time>22:50:37</time> + + <version> + <release>0.1.0</release> + <api>0.1.0</api> + </version> + <stability> + <release>beta</release> + <api>beta</api> + </stability> + <license uri="http://www.php.net/license">PHP License</license> + + <notes> + Initial release + </notes> + + <contents> + <dir name="/"> + <dir name="Net"> + <file name="Wifi.php" role="php" /> + </dir> + <dir name="examples"> + <file baseinstalldir="Net" name="Wifi_usage.php" role="doc" /> + </dir> + <dir name="tests"> + <file baseinstalldir="Net" name="runtest.php" role="test" /> + <file baseinstalldir="Net" name="Wifi_testcase.php" role="test" /> + </dir> + </dir> + </contents> + + <dependencies> + <required> + <php> + <min>4.0.0</min> + </php> + <pearinstaller> + <min>1.4.0b1</min> + </pearinstaller> + </required> + </dependencies> + + <phprelease /> + <changelog> <release> - <version>0.1.0</version> - <date>2005-07-13</date> - <notes> - Initial release - </notes> - <state>beta</state> - <filelist> - <dir name="/" baseinstalldir="Net" role="php"> - <file md5sum="893c8d21e3615bb0107460f7a4ddd66d">Wifi.php</file> - <dir name="examples" role="doc"> - <file md5sum="e88e4ada9718d447c66d3bbb3ecb1407">Wifi_usage.php</file> - </dir> - <dir name="tests" role="test"> - <file name="runtest.php" md5sum="d341daa6754370b1eb7e76f85cf7a3ae"/> - <file name="Wifi_testcase.php" md5sum="8783567ef03ee283ab5e4f4ffb9cdff8"/> - </dir> - </dir> - </filelist> + <version> + <release>0.1.0</release> + <api>0.1.0</api> + </version> + <stability> + <release>beta</release> + <api>beta</api> + </stability> + <date>2005-07-13</date> + <license uri="http://www.php.net/license">PHP License</license> + <notes> + Initial release + </notes> </release> - <changelog> - <release> - <version>0.1.0</version> - <date>2005-07-13</date> - <notes> - Initial release - </notes> - <state>beta</state> - </release> - </changelog> - + </changelog> </package> http://cvs.php.net/viewvc.cgi/pear/Net_Wifi/Net/Wifi.php?view=markup&rev=1.1 Index: pear/Net_Wifi/Net/Wifi.php +++ pear/Net_Wifi/Net/Wifi.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * A class for scanning wireless networks and identifying * local wireless network interfaces * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @author Christian Weiske <cweiske@php.net> * @category Net * @package Net * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: Wifi.php,v 1.1 2006/10/27 20:54:13 cweiske Exp $ * * @date 2005-07-10 08:23 */ //required for System::which() functionality require_once('System.php'); /** * This class uses tools like iwconfig and iwlist to scan * for wireless networks * * @author Christian Weiske <cweiske@php.net> */ class Net_Wifi { /** * Various locations of programs * @var array */ var $arFileLocation = array( 'iwconfig' => '/usr/sbin/iwconfig', 'iwlist' => '/usr/sbin/iwlist', '/proc/net/wireless' => '/proc/net/wireless' ); /** * Constructor which tries to guess the paths of the tools */ function Net_Wifi() { //try to find the paths $iwconfig = System::which('iwconfig'); if ($iwconfig !== false) { $this->setPathIwconfig($iwconfig); } $iwlist = System::which('iwlist'); if ($iwlist !== false) { $this->setPathIwconfig($iwlist); } }//function Net_Wifi() /** * Returns an object with the current state of the interface (connected/not connected, AP,...). * * @access public * @param string The interface to check * @return Net_Wifi_Config The state information */ function getCurrentConfig($strInterface) { //get the plain config $arLines = array(); exec($this->arFileLocation['iwconfig'] . ' ' . escapeshellarg($strInterface), $arLines); $strAll = implode("\n", $arLines); return $this->parseCurrentConfig($strAll); }//function getCurrentConfig($strInterface) /** * Parses the iwconfig output to collect the current config information. * * @access protected * @param string The iwconfig output to parse * @return Net_Wifi_Config The current config object */ function parseCurrentConfig($strAll) { $objConfig = new Net_Wifi_Config(); $arMatches = array(); if (preg_match('/ESSID:"([^"]+)"/', $strAll, $arMatches)) { $objConfig->ssid = $arMatches[1]; } if (preg_match('/Access Point: ([0-9:A-F]{17})/', $strAll, $arMatches)) { $objConfig->ap = $arMatches[1]; } if (preg_match('/Nickname:"([^"]+)"/', $strAll, $arMatches)) { $objConfig->nick = $arMatches[1]; } if (strpos($strAll, 'Mode:Managed')) { $objConfig->mode = 'managed'; } else if (strpos($strAll, 'Mode:Ad-Hoc')) { $objConfig->mode = 'ad-hoc'; } if (preg_match('/Bit Rate=([0-9.]+) [mk]b\\/s/i', $strAll, $arMatches)) { $objConfig->rate = $arMatches[1]; } if (preg_match('/Power=([0-9]+) dBm/', $strAll, $arMatches)) { $objConfig->power = $arMatches[1]; } if (preg_match('/Signal level=(-?[0-9]+) dBm/', $strAll, $arMatches)) { $objConfig->rssi = $arMatches[1]; } if (preg_match('/(IEEE [0-9.]+[a-z])/', $strAll, $arMatches)) { $objConfig->protocol = $arMatches[1]; } //available in ipw2200 1.0.3 only if (strpos($strAll, 'radio off')) { $objConfig->activated = false; } if (strpos($strAll, 'unassociated') === false && $objConfig->ap != null && $objConfig->ap != '00:00:00:00:00:00') { $objConfig->associated = true; } return $objConfig; }//function parseCurrentConfig($strAll) /** * Checks if a network interface is connected to an access point. * * @access public * @param string The network interface to check * @return boolean If the interface is connected */ function isConnected($strInterface) { $objConfig = $this->getCurrentConfig($strInterface); return $objConfig->associated; }//function isConnected($strInterface) /** * Returns an array with the names/device files of all supported wireless lan devices. * * @access public * @return array Array with wireless interfaces as values */ function getSupportedInterfaces() { $arWirelessInterfaces = array(); if (file_exists($this->arFileLocation['/proc/net/wireless'])) { /** * use /proc/net/wireless */ $arLines = file($this->arFileLocation['/proc/net/wireless']); //begin with 3rd line if (count($arLines) > 2) { for ($nA = 2; $nA < count($arLines); $nA++) { $nPos = strpos($arLines[$nA], ':', 0); $strInterface = trim(substr($arLines[$nA], 0, $nPos)); $arWirelessInterfaces[] = $strInterface; } }//we've got more than 2 lines } else { /** * use iwconfig */ $arLines = array(); exec($this->arFileLocation['iwconfig'], $arLines); foreach ($arLines as $strLine) { if (trim($strLine[0]) != '' && strpos($strLine, 'no wireless extensions') === false) { //there is something $arWirelessInterfaces[] = substr($strLine, 0, strpos($strLine, ' ')); } }//foreach line }//use iwconfig return $arWirelessInterfaces; }//function getSupportedInterfaces() /** * Scans for access points / ad hoc cells and returns them. * * @access public * @param string The interface to use * @return array Array with cell information objects (Net_Wifi_Cell) */ function scan($strInterface) { $arLines = array(); exec($this->arFileLocation['iwlist'] . ' ' . escapeshellarg($strInterface) . ' scanning', $arLines); return $this->parseScan( $arLines); }//function scan($strInterface) /** * Parses the output of iwlist and returns the recognized cells. * * @access protected * @param array Lines of the iwlist output as an array * @return array Array with cell information objects */ function parseScan($arLines) { if (count($arLines) == 1) { //one line only -> no cells there return array(); } //split into cells $arCells = array(); $nCurrentCell = -1; $nCount = count($arLines); for ($nA = 1; $nA < $nCount; $nA++) { $strLine = trim($arLines[$nA]); if ($strLine == '') { continue; } if (substr($strLine, 0, 4) == 'Cell') { //we've got a new cell $nCurrentCell++; //get cell number $nCell = substr($strLine, 5, strpos($strLine, ' ', 5) - 5); $arCells[$nCurrentCell] = new Net_Wifi_Cell(); $arCells[$nCurrentCell]->cell = $nCell; //remove cell information from line for further interpreting $strLine = substr($strLine, strpos($strLine, '- ') + 2); } $nPos = strpos($strLine, ':'); $nPosEquals = strpos($strLine, '='); if ($nPosEquals !== false && ($nPos === false || $nPosEquals < $nPos)) { //sometimes there is a "=" instead of a ":" $nPos = $nPosEquals; } $nPos++; $strId = strtolower(substr($strLine, 0, $nPos - 1)); $strValue = trim(substr($strLine, $nPos)); switch ($strId) { case 'address': $arCells[$nCurrentCell]->mac = $strValue; break; case 'essid': if ($strValue[0] == '"') { //has quotes around $arCells[$nCurrentCell]->ssid = substr($strValue, 1, -1); } else { $arCells[$nCurrentCell]->ssid = $strValue; } break; case 'bit rate': $nRate = floatval(substr($strValue, 0, strpos($strValue, 'Mb/s'))); $arCells[$nCurrentCell]->rate = $nRate; $arCells[$nCurrentCell]->rates[] = $nRate; break; case 'protocol': $arCells[$nCurrentCell]->protocol = $strValue; break; case 'channel': $arCells[$nCurrentCell]->channel = intval($strValue); break; case 'encryption key': if ($strValue == 'on') { $arCells[$nCurrentCell]->encryption = true; } else { $arCells[$nCurrentCell]->encryption = false; } break; case 'mode': if (strtolower($strValue) == 'master') { $arCells[$nCurrentCell]->mode = 'master'; } else { $arCells[$nCurrentCell]->mode = 'ad-hoc'; } break; case 'signal level': $arCells[$nCurrentCell]->rssi = substr($strValue, 0, strpos($strValue, ' ')); break; case 'quality': $arData = explode(' ', $strValue); $arCells[$nCurrentCell]->quality = $arData[0]; if (trim($arData[1]) != '') { //bad hack $arLines[$nA] = $arData[1]; $nA--; if (isset($arData[2])) { $arLines[$nA - 1] = $arData[1]; $nA--; } } break; case 'frequency': $arCells[$nCurrentCell]->frequency = $strValue; break; case 'extra': $nPos = strpos($strValue, ':'); $strSubId = strtolower(trim(substr($strValue, 0, $nPos))); $strValue = trim(substr($strValue, $nPos + 1)); switch ($strSubId) { case 'rates (mb/s)': //1 2 5.5 11 54 $arRates = explode(' ', $strValue); //convert to float values foreach ($arRates as $nB => $strRate) { $arCells[$nCurrentCell]->rates[$nB] = floatval($strRate); } break; case 'signal': case 'rssi': //-53 dBm $arCells[$nCurrentCell]->rssi = intval(substr($strValue, 0, strpos($strValue, ' '))); break; case 'last beacon': //25ms ago $arCells[$nCurrentCell]->beacon = intval(substr($strValue, 0, strpos($strValue, 'ms'))); break; default: echo 'unknown iwconfig extra information: ' . $strSubId . "\r\n"; break; } break; default: echo 'unknown iwconfig information: ' . $strId . "\r\n"; break; } }//foreach line //not all outputs are sorted (note the 6) //Extra: Rates (Mb/s): 1 2 5.5 9 11 6 12 18 24 36 48 54 //additionally, some drivers have many single "Bit Rate:" fields instead of one big one foreach ($arCells as $nCurrentCell => $arData) { sort($arCells[$nCurrentCell]->rates); $arCells[$nCurrentCell]->rates = array_unique($arCells[$nCurrentCell]->rates); } return $arCells; }//function parseScan($arLines) /** * Tells the driver to use the access point with the given MAC address only. * * You can use "off" to enable automatic mode again without * changing the current AP, or "any" resp. "auto" to force * the card to re-associate with the currently best AP * * EXPERIMENTAL! WILL CHANGE IN FUTURE VERSIONS * @access public * @param string The interface to use * @param string The mac address of the access point * @return boolean True if setting was ok, false if not */ function connectToAccessPoint($strInterface, $strMac) { $arLines = array(); $nReturnVar = 0; exec($this->arFileLocation['iwconfig'] . ' ' . escapeshellarg($strInterface) . ' ap ' . escapeshellarg($strMac), $arLines, $nReturnVar); return $nReturnVar == 0; }//function connectToAccessPoint($strInterface, $strMac) /** * and now some dumb getters and setters */ /** * Returns the set path to /proc/wireless. * @access public * @return string The path to "/proc/net/wireless" */ function getPathProcWireless() { return $this->arFileLocation['/proc/net/wireless']; }//function getPathProcWireless() /** * Returns the set path to /proc/net/wireless. * @access public * @param string The new /proc/net/wireless path */ function setPathProcWireless( $strProcWireless) { $this->arFileLocation['/proc/net/wireless'] = $strProcWireless; }//function setPathProcWireless( $strProcWireless) /** * Returns the set path to iwconfig. * @access public * @return string The path to iwconfig */ function getPathIwconfig() { return $this->arFileLocation['iwconfig']; }//function getPathIwconfig() /** * Returns the set path to iwconfig. * @access public * @param string The new ifwconfig path */ function setPathIwconfig( $strPathIwconfig) { $this->arFileLocation['iwconfig'] = $strPathIwconfig; }//function setPathIwconfig( $strPathIwconfig) /** * Returns the set path to iwlist. * @access public * @return string The path to iwlist */ function getPathIwlist() { return $this->arFileLocation['iwlist']; }//function getPathIwlist() /** * Returns the set path to iwlist. * @access public * @param string The new iwlist path */ function setPathIwlist( $strPathIwlist) { $this->arFileLocation['iwlist'] = $strPathIwlist; }//function setPathIwlist( $strPathIwlist) }//class Net_Wifi /** * Cell information from a wifi scan. */ class Net_Wifi_Cell { /** * The current cell number. * Has nothing to say. * @var int */ var $cell = null; /** * MAC address of the cell (access point or ad-hoc station). * example: 00:40:05:28:EB:45 * @var string */ var $mac = null; /** * "Service Set IDentifier" of the cell which identifies the network. * Max. 32 alphanumeric characters * example: "My Network" (without quotes) * @var string */ var $ssid = null; /** * Network type. * can be "master" or "ad-hoc" (without quotes) * @var string */ var $mode = null; /** * Channel number used for communication. * number from 1 to 12 or so * @var int */ var $channel = null; /** * If encryption is used. * @var boolean */ var $encryption = null; /** * Channel frequency. * example: 2.412GHz * @var string */ var $frequency = null; /** * The protocol version used. * example: IEEE 802.11b * @var string */ var $protocol = null; /** * Bit rate which is used (when connected). * @var float */ var $rate = null; /** * Array of supported bit rates. * @var array(float) */ var $rates = array(); /** * Signal strength. * example: -59 * @var int */ var $rssi = null; /** * The time since the last beacon (time sync) frame has been sent, in ms. * @var int */ var $beacon = null; }//class Net_Wifi_Cell /** * Configuration settings of a wifi network interface */ class Net_Wifi_Config { /** * If the interface is activated. * Some notebooks have a button which deactivates wifi, this is recognized here. * Note that this setting can't be read by all drivers, and so * it's "true" if it can't be determined. You can be sure that it's deactivated * if this setting is false, but not that it's activated if it's true * @var boolean */ var $activated = true; /** * If the interface is connected to an access point or an ad-hoc network. * @var boolean */ var $associated = false; /** * MAC address of the associated access point. * @var string */ var $ap = null; /** * "Service Set IDentifier" of the cell which identifies current network. * Max. 32 alphanumeric characters * example: "My Network" (without quotes) * @var string */ var $ssid = null; /** * Network type. * Can be "master" or "ad-hoc" (without quotes) * @var string */ var $mode = null; /** * The nickname which the interface (computer) uses. * Something like a computer name * @var string */ var $nick = null; /** * The bit rate of the connection. * @var float */ var $rate = null; /** * Power setting of the interface. * @var int */ var $power = null; /** * Protocol version which is used for connection. * example: "IEEE 802.11g" without quotes * @var string */ var $protocol = null; /** * Signal strength. * example: -59 * @var int */ var $rssi = null; }//class Net_Wifi_Config ?>