コード例 #1
0
ファイル: System.php プロジェクト: Victopia/prefw
 /**
  * Inspired by os.networkInterfaces() from node.js.
  *
  * @return {array} Returns a brief info of available network interfaces.
  */
 public static function networkInterfaces()
 {
     switch (strtoupper(PHP_OS)) {
         case 'DARWIN':
             // MAC OS X
             $res = preg_split('/\\n/', @`ifconfig`);
             $res = array_filter(array_map('trim', $res));
             $result = array();
             foreach ($res as $row) {
                 if (preg_match('/^(\\w+\\d+)\\:\\s+(.+)/', $row, $matches)) {
                     $result['__currentInterface'] = $matches[1];
                     $result[$result['__currentInterface']]['__internal'] = false !== strpos($matches[2], 'LOOPBACK');
                 } else {
                     if (preg_match('/^inet(6)?\\s+([^\\/\\s]+)(?:%.+)?/', $row, $matches)) {
                         $iface =& $result[$result['__currentInterface']];
                         @($iface[] = array('address' => $matches[2], 'family' => $matches[1] ? 'IPv6' : 'IPv4', 'internal' => $iface['__internal']));
                         unset($iface);
                     }
                 }
                 unset($matches);
             }
             unset($row, $res);
             unset($result['__currentInterface']);
             return array_filter(array_map(compose('array_filter', removes('__internal')), $result));
         case 'LINUX':
             // $ifaces = `ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d'`;
             // $ifaces = preg_split('/\s+/', $ifaces);
             $res = preg_split('/\\n/', @`ip addr`);
             $res = array_filter(array_map('trim', $res));
             $result = array();
             foreach ($res as $row) {
                 if (preg_match('/^\\d+\\:\\s+(\\w+)/', $row, $matches)) {
                     $result['__currentInterface'] = $matches[1];
                 } else {
                     if (preg_match('/^link\\/(\\w+)/', $row, $matches)) {
                         $result[$result['__currentInterface']]['__internal'] = strtolower($matches[1]) == 'loopback';
                     } else {
                         if (preg_match('/^inet(6)?\\s+([^\\/]+)(?:\\/\\d+)?.+\\s([\\w\\d]+)(?:\\:\\d+)?$/', $row, $matches)) {
                             @($result[$matches[3]][] = array('address' => $matches[2], 'family' => $matches[1] ? 'IPv6' : 'IPv4', 'internal' => Utility::cascade(@$result[$matches[3]]['__internal'], false)));
                         }
                     }
                 }
                 unset($matches);
             }
             unset($row, $res);
             unset($result['__currentInterface']);
             return array_filter(array_map(compose('array_filter', removes('__internal')), $result));
         case 'WINNT':
             // Currently not supported.
         // Currently not supported.
         default:
             return false;
     }
 }