File: class.pingtest.inc.php

package info (click to toggle)
phpsysinfo 3.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,900 kB
  • sloc: javascript: 22,511; php: 20,651; xml: 18,293; sh: 196; python: 58; makefile: 12
file content (143 lines) | stat: -rw-r--r-- 5,012 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
 * PingTime Plugin
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PSI_Plugin_PingTest
 * @author    Mieczyslaw Nalewaj <namiltd@users.sourceforge.net>
 * @copyright 2017 phpSysInfo
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
 * @version   SVN: $Id: class.pingtest.inc.php 1 2017-09-01 09:01:15Z namiltd $
 * @link      http://phpsysinfo.sourceforge.net
 */
class PingTest extends PSI_Plugin
{
    /**
     * variable, which holds the content of the command
     * @var array
     */
    private $_filecontent = array();

    /**
     * variable, which holds the result before the xml is generated out of this array
     * @var array
     */
    private $_result = array();

    /**
     * read the data into an internal array and also call the parent constructor
     *
     * @param String $enc target encoding
     */
    public function __construct($enc)
    {
        parent::__construct(__CLASS__, $enc);
        if (defined('PSI_PLUGIN_PINGTEST_ADDRESSES') && is_string(PSI_PLUGIN_PINGTEST_ADDRESSES)) {
            if (preg_match(ARRAY_EXP, PSI_PLUGIN_PINGTEST_ADDRESSES)) {
                $addresses = eval(PSI_PLUGIN_PINGTEST_ADDRESSES);
            } else {
                $addresses = array(PSI_PLUGIN_PINGTEST_ADDRESSES);
            }

            switch (strtolower(PSI_PLUGIN_PINGTEST_ACCESS)) {
            case 'command':
                if (PHP_OS == 'WINNT') {
                    $params = "-n 1";
                    if (defined('PSI_PLUGIN_PINGTEST_TIMEOUT')) {
                        if (($tout = max(intval(PSI_PLUGIN_PINGTEST_TIMEOUT), 0)) > 0) {
                            $params .= " -w ".(1000*$tout);
                        }
                    } else {
                        $params .= " -w 2000";
                    }

                } else {
                    $params = "-c 1";
                    if (defined('PSI_PLUGIN_PINGTEST_TIMEOUT')) {
                        if (($tout = max(intval(PSI_PLUGIN_PINGTEST_TIMEOUT), 0)) > 0) {
                            $params .= " -W ".$tout;
                        }
                    } else {
                        $params .= " -W 2";
                    }
                }
                foreach ($addresses as $address) {
                    CommonFunctions::executeProgram("ping".((strpos($address, ':') === false)?'':((PHP_OS != 'WINNT')?'6':'')), $params." ".$address, $buffer, PSI_DEBUG);
                    if ((strlen($buffer) > 0) && preg_match("/[=<]([\d\.]+)\s*ms/", $buffer, $tmpout)) {
                        $this->_filecontent[] = array($address, $tmpout[1]);
                    }
                }
                break;
            case 'data':
                CommonFunctions::rftsdata("pingtest.tmp", $buffer);
                $addresses = preg_split("/\n/", $buffer, -1, PREG_SPLIT_NO_EMPTY);
                foreach ($addresses as $address) {
                    $pt = preg_split("/[\s]?\|[\s]?/", $address, -1, PREG_SPLIT_NO_EMPTY);
                    if (count($pt) == 2) {
                        $this->_filecontent[] = array(trim($pt[0]), trim($pt[1]));
                    }
                }
                break;
            default:
                $this->global_error->addConfigError("__construct()", "[pingtest] ACCESS");
            }
        }
    }

    /**
     * doing all tasks to get the required informations that the plugin needs
     * result is stored in an internal array
     *
     * @return void
     */
    public function execute()
    {
        if (defined('PSI_PLUGIN_PINGTEST_ADDRESSES') && is_string(PSI_PLUGIN_PINGTEST_ADDRESSES)) {
            if (preg_match(ARRAY_EXP, PSI_PLUGIN_PINGTEST_ADDRESSES)) {
                $addresses = eval(PSI_PLUGIN_PINGTEST_ADDRESSES);
            } else {
                $addresses = array(PSI_PLUGIN_PINGTEST_ADDRESSES);
            }
            foreach ($addresses as $address) {
                $this->_result[] = array($address, $this->address_inarray($address, $this->_filecontent));
            }
        }
    }

    /**
     * generates the XML content for the plugin
     *
     * @return SimpleXMLElement entire XML content for the plugin
     */
    public function xml()
    {
        foreach ($this->_result as $pt) {
            $xmlps = $this->xml->addChild("Ping");
            $xmlps->addAttribute("Address", $pt[0]);
            $xmlps->addAttribute("PingTime", $pt[1]);
        }

        return $this->xml->getSimpleXmlElement();
    }

    /**
     * checks an array if pingtest address is in
     *
     * @param mixed $needle   what to find
     * @param array $haystack where to find
     *
     * @return pingtime - found<br>"lost" - not found
     */
    private function address_inarray($needle, $haystack)
    {
        foreach ($haystack as $stalk) {
            if ($needle === $stalk[0]) {
                return $stalk[1];
            }
        }

        return "lost";
    }
}