public function ensureFQDN($hostname)
 {
     $parts = explode('.', $hostname);
     $num_parts = count($parts);
     // Already FQDN, like a boss..
     if ($num_parts >= 2) {
         return $hostname;
     }
     // Don't bother trying to expand on .local
     if ($num_parts > 0 && $parts[$num_parts - 1] == '.local') {
         return $hostname;
     }
     // This relies on reading /etc/hosts.
     if (!($contents = LinfoCommon::getContents('/etc/hosts', false))) {
         return $hostname;
     }
     preg_match_all('/^[^\\s#]+\\s+(.+)/m', $contents, $matches, PREG_SET_ORDER);
     // Lets see if we can do some magic with /etc/hosts..
     foreach ($matches as $match) {
         if (!preg_match_all('/(\\S+)/', $match[1], $hosts, PREG_SET_ORDER)) {
             continue;
         }
         foreach ($hosts as $host) {
             // I don't want to expand on localhost as it's pointlesss
             if (strpos('localhost', $host[1]) !== false) {
                 continue;
             }
             $entry_parts = explode('.', $host[1]);
             if (count($entry_parts) > 1 && $entry_parts[0] == $hostname) {
                 return $host[1];
             }
         }
     }
     // Couldn't make it better :/
     return $hostname;
 }
 /**
  * Get the PCI ids from /sys
  *
  * @access private
  */
 private function _fetchPciIdsLinux()
 {
     foreach ((array) @glob('/sys/bus/pci/devices/*', GLOB_NOSORT) as $path) {
         // See if we can use simple vendor/device files and avoid taking time with regex
         if (($f_device = LinfoCommon::getContents($path . '/device', '')) && ($f_vend = LinfoCommon::getContents($path . '/vendor', '')) && $f_device != '' && $f_vend != '') {
             list(, $v_id) = explode('x', $f_vend, 2);
             list(, $d_id) = explode('x', $f_device, 2);
             $this->_pci_entries[$v_id][$d_id] = 1;
         } elseif (is_readable($path . '/uevent') && preg_match('/pci\\_(?:subsys_)?id=(\\w+):(\\w+)/', strtolower(LinfoCommon::getContents($path . '/uevent')), $match)) {
             $this->_pci_entries[$match[1]][$match[2]] = 1;
         } elseif (is_readable($path . '/modalias') && preg_match('/^pci:v0{4}([0-9A-Z]{4})d0{4}([0-9A-Z]{4})/', LinfoCommon::getContents($path . '/modalias'), $match)) {
             $this->_pci_entries[strtolower($match[1])][strtolower($match[2])] = 1;
         }
     }
 }
 /**
  * Deal with it
  * 
  * @access private
  */
 private function _call()
 {
     // Time this
     $t = new LinfoTimerStart('Transmission extension');
     // Deal with stats, if possible
     if ($this->_folder && ($stats_contents = LinfoCommon::getContents($this->_folder . 'stats.json', false)) && $stats_contents != false) {
         $stats_vals = @json_decode($stats_contents, true);
         if (is_array($stats_vals)) {
             $this->_stats = $stats_vals;
         }
     }
     // Deal with calling it
     try {
         // Start up args
         $args = '';
         // Specifc host/port?
         if (array_key_exists('server', $this->_host) && array_key_exists('port', $this->_host) && is_numeric($this->_host['port'])) {
             $args .= ' \'' . $this->_host['server'] . '\':' . $this->_host['port'];
         }
         // We need some auth?
         if (array_key_exists('user', $this->_auth) && array_key_exists('pass', $this->_auth)) {
             $args .= ' --auth=\'' . $this->_auth['user'] . '\':\'' . $this->_auth['pass'] . '\'';
         }
         // Rest of it, including result
         $result = $this->_CallExt->exec('transmission-remote', $args . ' -l');
     } catch (CallExtException $e) {
         // messed up somehow
         $this->_LinfoError->add('Transmission extension: ', $e->getMessage());
         $this->_res = false;
         // Don't bother going any further
         return false;
     }
     $this->_res = true;
     // Get first line
     $first_line = reset(explode("\n", $result, 1));
     // Invalid host?
     if (strpos($first_line, 'Couldn\'t resolve host name') !== false) {
         $this->_LinfoError->add('Transmission extension: Invalid Host');
         $this->_res = false;
         return false;
     }
     // Invalid auth?
     if (strpos($first_line, '401: Unauthorized') !== false) {
         $this->_LinfoError->add('Transmission extension: Invalid Authentication');
         $this->_res = false;
         return false;
     }
     // Match teh torrents!
     if (preg_match_all('/^\\s+(\\d+)\\*?\\s+(\\d+)\\%\\s+(\\d+\\.\\d+ \\w+|None)\\s+((?:\\d+ )?\\w+)\\s+(\\d+\\.\\d+)\\s+(\\d+\\.\\d+)\\s+(\\d+\\.\\d+|None)\\s+(Up & Down|Seeding|Idle|Stopped)\\s+(.+)$/m', $result, $matches, PREG_SET_ORDER) > 0) {
         // Use this to sort them
         $sort_done = array();
         $sort_ratio = array();
         $sort_name = array();
         // Save the matches
         for ($i = 0, $num = count($matches); $i < $num; $i++) {
             // Save this one
             $this->_torrents[$i] = array('id' => $matches[$i][1], 'done' => $matches[$i][2], 'have' => $matches[$i][3], 'eta' => $matches[$i][4], 'up' => $matches[$i][5] * 1024, 'down' => $matches[$i][6] * 1024, 'ratio' => $matches[$i][7], 'state' => $matches[$i][8], 'torrent' => $matches[$i][9]);
             // Use this for sorting
             $sort_done[$i] = (int) $matches[$i][2];
             $sort_ratio[$i] = (double) $matches[$i][7];
             $sort_name[$i] = $matches[$i][9];
         }
         // Sort
         array_multisort($sort_done, SORT_DESC, $sort_ratio, SORT_DESC, $sort_name, SORT_ASC, $this->_torrents);
     }
 }
Exemple #4
0
 /**
  * Get brand/name of motherboard/server through /sys' interface to dmidecode
  *
  * @access public
  */
 public function getModel()
 {
     $info = array();
     $vendor = LinfoCommon::getContents('/sys/devices/virtual/dmi/id/board_vendor', false);
     $name = LinfoCommon::getContents('/sys/devices/virtual/dmi/id/board_name', false);
     $product = LinfoCommon::getContents('/sys/devices/virtual/dmi/id/product_name', false);
     if (!$name) {
         return false;
     }
     // Don't add vendor to the mix if the name starts with it
     if ($vendor && strpos($name, $vendor) !== 0) {
         $info[] = $vendor;
     }
     $info[] = $name;
     $infostr = implode(' ', $info);
     // product name is usually bullshit, but *occasionally* it's a useful name of the computer, such as
     // dell latitude e6500 or hp z260
     if ($product && strpos($name, $product) === false && strpos($product, 'Filled') === false) {
         return $product . ' (' . $infostr . ')';
     } else {
         return $infostr;
     }
 }
 /**
  * @test
  */
 public function getContents()
 {
     $contents = "lineone\nlinetwo\nline3";
     $file = LINFO_TESTDIR . '/files/lines.txt';
     $this->assertEquals($contents, LinfoCommon::getContents($file));
 }
 /**
  * Deal with it
  * 
  * @access private
  */
 private function _call()
 {
     // Time this
     $t = new LinfoTimerStart('dhcpd3 leases extension');
     // We couldn't find leases file?
     if ($this->_leases_file === false) {
         $this->_LinfoError->add('dhcpd3 leases extension', 'couldn\'t find leases file');
         $this->_res = false;
         return false;
     }
     // Get contents
     $contents = LinfoCommon::getContents($this->_leases_file, false);
     // Couldn't?
     if ($contents === false) {
         $this->_LinfoError->add('dhcpd3 leases extension', 'Error getting contents of leases file');
         $this->_res = false;
         return false;
     }
     // All dates in the file are in UTC format. Attempt finding out local time zone to convert UTC to local.
     // This prevents confusing the hell out of people.
     $do_date_conversion = false;
     $local_timezone = false;
     // Make sure we have what we need. Stuff this requires doesn't exist on certain php installations
     if (function_exists('date_default_timezone_get') && class_exists('DateTime') && class_exists('DateTimeZone')) {
         // I only want this called once, hence value stored here. It also might fail
         $local_timezone = @date_default_timezone_get();
         // Make sure it didn't fail
         if ($local_timezone !== false && is_string($local_timezone)) {
             $do_date_conversion = true;
         }
         // Say we'll allow conversion later on
     }
     // Get it into lines
     $lines = explode("\n", $contents);
     // Store temp entries here
     $curr = false;
     // Parse each line, while ignoring certain useless'ish values
     // I'd do a single preg_match_all() using multiline regex, but the values in each lease block are inconsistent. :-/
     for ($i = 0, $num_lines = count($lines); $i < $num_lines; $i++) {
         // Kill padding whitespace
         $lines[$i] = trim($lines[$i]);
         // Last line in entry
         if ($lines[$i] == '}') {
             // Have we a current entry to save?
             if (is_array($curr)) {
                 $this->_leases[] = $curr;
             }
             // Make it empty for next time
             $curr = false;
         } elseif (preg_match('/^lease (\\d+\\.\\d+\\.\\d+\\.\\d+) \\{$/', $lines[$i], $m)) {
             $curr = array('ip' => $m[1]);
         } elseif ($curr && preg_match('/^starts \\d+ (\\d+\\/\\d+\\/\\d+ \\d+:\\d+:\\d+);$/', $lines[$i], $m)) {
             // Get it in unix time stamp for prettier formatting later and easier tz offset conversion
             $curr['lease_start'] = strtotime($m[1]);
             // Handle offset conversion
             if ($do_date_conversion) {
                 // This handy class helps out with timezone offsets. Pass it original date, not unix timestamp
                 $d = new DateTime($m[1], new DateTimeZone($local_timezone));
                 $offset = $d->getOffset();
                 // If ofset looks good, deal with it
                 if (is_numeric($offset) && $offset != 0) {
                     $curr['lease_start'] += $offset;
                 }
             }
         } elseif ($curr && preg_match('/^ends \\d+ (\\d+\\/\\d+\\/\\d+ \\d+:\\d+:\\d+);$/', $lines[$i], $m)) {
             // Get it in unix time stamp for prettier formatting later and easier tz offset conversion
             $curr['lease_end'] = strtotime($m[1]);
             // Handle offset conversion
             if ($do_date_conversion) {
                 // This handy class helps out with timezone offsets. Pass it original date, not unix timestamp
                 $d = new DateTime($m[1], new DateTimeZone($local_timezone));
                 $offset = $d->getOffset();
                 // If ofset looks good, deal with it
                 if (is_numeric($offset) && $offset != 0) {
                     $curr['lease_end'] += $offset;
                 }
             }
             // Is this old?
             // The file seems to contain all leases since the dhcpd server was started for the first time
             if (time() > $curr['lease_end']) {
                 // Kill current entry and ignore any following parts of this lease
                 $curr = false;
                 // Jump out right now
                 continue;
             }
         } elseif (!$this->_hide_mac && $curr && preg_match('/^hardware ethernet (\\w+:\\w+:\\w+:\\w+:\\w+:\\w+);$/', $lines[$i], $m)) {
             $curr['mac'] = $m[1];
         } elseif ($curr && preg_match('/^client\\-hostname "([^"]+)";$/', $lines[$i], $m)) {
             $curr['hostname'] = $m[1];
         }
     }
 }
 protected function loadDmesg()
 {
     $this->dmesg = LinfoCommon::getContents('/var/run/dmesg.boot');
 }