Issues (525)

class/CheckonlineHandler.php (1 issue)

1
<?php
2
3
namespace XoopsModules\Wgsitenotice;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
/**
15
 * wgSitenotice module for xoops
16
 *
17
 * @copyright       XOOPS Project (https://xoops.org)
18
 * @license         GPL 2.0 or later
19
 * @package         wgsitenotice
20
 * @since           1.0
21
 * @min_xoops       2.5.11
22
 * @author          Goffy (xoops.wedega.com) - Email:<[email protected]> - Website:<https://xoops.wedega.com>
23
 */
24
25
\defined('XOOPS_ROOT_PATH') || exit('Restricted access');
26
27
/**
28
 * Class Object Handler Checkonline
29
 */
30
class CheckonlineHandler extends \XoopsPersistableObjectHandler
31
{
32
    /**
33
     * Constructor
34
     *
35
     * @param \XoopsDatabase $db
36
     */
37
    public function __construct(\XoopsDatabase $db)
38
    {
39
        parent::__construct($db, 'mod_wgsitenotice_checkonline', Checkonline::class, 'onl_id', 'onl_text1');
40
    }
41
42
    /**
43
     * request data from given website
44
     *
45
     * @param $oc_server
46
     * @return string result of http post
47
     */
48
    public function getData($oc_server) {
49
50
        $postdata = \http_build_query(
51
            [
52
                'ptype' => 'get-data'
53
            ]
54
        );
55
56
        if (\function_exists('curl_init')) {
57
            //open connection
58
            $ch = \curl_init();
59
60
            //set the url, number of POST vars, POST data
61
            \curl_setopt($ch, CURLOPT_URL, $oc_server);
62
            \curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
63
            \curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 25);
64
            \curl_setopt($ch, CURLOPT_TIMEOUT, 25);
65
            \curl_setopt($ch, CURLOPT_VERBOSE, false);
66
            \curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
67
            \curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
68
            \curl_setopt($ch, CURLOPT_POST, 1);
69
            \curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
70
71
            //execute post
72
            $result = \curl_exec($ch);
73
            // print_r(\curl_getinfo($ch));
74
            if (false == $result)  {
75
                //echo '<br>unexpected curl_error:' . \curl_error($ch) . '<br>';
76
                $GLOBALS['xoopsTpl']->assign('error',\curl_error($ch));
77
            }
78
79
            \curl_close($ch);
80
        } else {
81
            $opts = ['http' =>
82
                [
83
                    'method'  => 'POST',
84
                    'header'  => 'Content-type: application/x-www-form-urlencoded',
85
                    'content' => $postdata
86
                ]
87
            ];
88
89
            $context  = \stream_context_create($opts);
90
            $result = \file_get_contents($oc_server, false, $context);
91
        }
92
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result also could return the type true which is incompatible with the documented return type string.
Loading history...
93
    }
94
95
    /**
96
     * read the given xml string (by getData) and create an array
97
     *
98
     * @param string $xml_string
99
     * @return array|\SimpleXMLElement|string
100
     */
101
    public function readXML(string $xml_string){
102
        // creating temporary string for avoiding entitiy errors
103
        $xml_string = \str_replace('&', '[[avoid_entity_error]]', $xml_string);
104
        $xml_arr = \simplexml_load_string($xml_string);
105
        if (!$xml_arr) {
106
            //error when loading xml
107
            $xml = \explode("\n", $xml_string);
108
            $errors = \libxml_get_errors();
109
            foreach ($errors as $error) {
110
                //echo $this->display_xml_error($error, $xml);
111
                $GLOBALS['xoopsTpl']->assign('error',$this->display_xml_error($error, $xml));
112
            }
113
            \libxml_clear_errors();
114
        }
115
116
        return $xml_arr;
117
    }
118
119
    /**
120
     * convert xml content to html text
121
     *
122
     * @param string $xml
123
     * @return string
124
     */
125
    public function xml2str(string $xml){
126
        // replace temporary string for avoiding entitiy errors
127
        $str = \str_replace('[[avoid_entity_error]]', '&', (string)$xml);
128
        // rebuild html tags
129
        $search  = ['&lt;', '&gt;', '&quot;', '&amp;'];
130
        $replace = ['<', '>', '"', '&'];
131
        $str = \str_replace($search, $replace, (string)$str);
132
        return $str;
133
    }
134
135
    /**
136
     * @param $error
137
     * @param $xml
138
     * @return string
139
     */
140
    public function display_xml_error($error, $xml)
141
    {
142
        $return  = $xml[$error->line - 1] . "\n";
143
        $return .= \str_repeat('-', $error->column) . "^\n";
144
145
        switch ($error->level) {
146
            case LIBXML_ERR_WARNING:
147
                $return .= "Warning $error->code: ";
148
                break;
149
            case LIBXML_ERR_ERROR:
150
                $return .= "Error $error->code: ";
151
                break;
152
            case LIBXML_ERR_FATAL:
153
                $return .= "Fatal Error $error->code: ";
154
                break;
155
        }
156
157
        $return .= \trim($error->message) .
158
            "\n  Line: $error->line" .
159
            "\n  Column: $error->column";
160
161
        if ($error->file) {
162
            $return .= "\n  File: $error->file";
163
        }
164
165
        return "$return\n\n--------------------------------------------\n\n";
166
    }
167
}
168