Completed
Pull Request — master (#10)
by Michael
02:00
created

index.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
// $Id: index.php,v 1.3 2006/03/12 C. Felix AKA the Cat
3
//  ------------------------------------------------------------------------ //
4
//             XF Guestbook                                                  //
5
// ------------------------------------------------------------------------- //
6
//  This program is free software; you can redistribute it and/or modify     //
7
//  it under the terms of the GNU General Public License as published by     //
8
//  the Free Software Foundation; either version 2 of the License, or        //
9
//  (at your option) any later version.                                      //
10
//                                                                           //
11
//  You may not change or alter any portion of this comment or credits       //
12
//  of supporting developers from this source code or any supporting         //
13
//  source code which is considered copyrighted (c) material of the          //
14
//  original comment or credit authors.                                      //
15
//                                                                           //
16
//  This program is distributed in the hope that it will be useful,          //
17
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
18
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
19
//  GNU General Public License for more details.                             //
20
//                                                                           //
21
//  You should have received a copy of the GNU General Public License        //
22
//  along with this program; if not, write metalslugto the Free Software              //
23
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
24
//  ------------------------------------------------------------------------ //
25
26
include dirname(dirname(__DIR__)) . '/mainfile.php';
27
//include_once(XOOPS_ROOT_PATH."/modules/".$xoopsModule->dirname()."/class/msg.php");
28
include_once(XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->dirname() . '/include/functions.php');
29 View Code Duplication
if (isset($_GET['msg_id'])) {
30
    $msg_id = (int)$_GET['msg_id'];
31
} elseif (isset($_POST['msg_id'])) {
32
    $msg_id = (int)$_POST['msg_id'];
33
} else {
34
    $msg_id = 0;
35
}
36
37 View Code Duplication
if (isset($_GET['op'])) {
38
    $op = $_GET['op'];
39
} elseif (isset($_POST['op'])) {
40
    $op = $_POST['op'];
41
} else {
42
    $op = 'show_all';
43
}
44
45
$msg_handler = xoops_getModuleHandler('msg');
46
47
//Admin or not
48
$xoopsUser ? $adminview = $xoopsUser->isAdmin() : $adminview = 0;
49
50
/**
51
 * @param $msg_id
52
 */
53
function delete($msg_id)
54
{
55
    global $msg_handler, $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
56
    $ok = isset($_POST['ok']) ? (int)$_POST['ok'] : 0;
57
    if ($ok == 1) {
58
        $msg        = &$msg_handler->get($msg_id);
59
        $del_msg_ok = $msg_handler->delete($msg);
60
        $filename   = $msg->getVar('photo');
61 View Code Duplication
        if ($filename !== '') {
62
            $filename = XOOPS_UPLOAD_PATH . '/' . $xoopsModule->getVar('dirname') . '/' . $filename;
63
            unlink($filename);
64
        }
65
        if ($del_msg_ok) {
66
            $messagesent = _MD_XFGB_MSGDELETED;
67
        } else {
68
            $messagesent = _MD_XFGB_ERRORDEL;
69
        }
70
        redirect_header('index.php', 2, $messagesent);
71
    } else {
72
        xoops_confirm(array('op' => 'delete', 'msg_id' => $msg_id, 'ok' => 1), 'index.php', _DELETE);
73
    }
74
}
75
76
/**
77
 * @param $msg_id
78
 */
79
function approve($msg_id)
80
{
81
    global $msg_handler;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
82
83
    $msg = &$msg_handler->get($msg_id);
84
    $msg->setVar('moderate', 0);
85
    if (!$msg_handler->insert($msg)) {
86
        $messagesent = _MD_XFGB_ERRORVALID;
87
    } else {
88
        $messagesent = _MD_XFGB_VALIDATE;
89
    }
90
    redirect_header('index.php?op=show_waiting', 2, $messagesent);
91
}
92
93
/**
94
 * @param $msg
95
 */
96
function xfgb_getmsg($msg)
97
{
98
    global $nbmsg, $xoopsModule, $xoopsUser, $xoopsModuleConfig, $xoopsTpl, $xoopsConfig, $options, $opt, $xoopsDB;
99
100
    $arr_country = xfgb_getAllCountry();
101
    $xoopsTpl->assign('display_msg', true);
102
    foreach ($msg as $onemsg) {
103
        if ($poster = xfgb_get_user_data($onemsg->getVar('user_id'))) {
104
            $a_msg = &$poster;
105
        } else {
106
            $a_msg             = array();
107
            $a_msg['poster']   = $onemsg->getVar('uname');
108
            $a_msg['rank']     = '';
109
            $a_msg['rank_img'] = '';
110
            $a_msg['avatar']   = '';
111
        }
112
        $member_handler = xoops_getHandler('member');
113
        $user           = $member_handler->getUser($onemsg->getVar('user_id'));
114
        // email
115
        if ($xoopsModuleConfig['showemail'] || ($onemsg->getVar('email') && (($user->getVar('user_viewemail') == 1 || $onemsg->getVar('user_id') == 0) && is_object($xoopsUser)))) {
116
            $a_msg['email'] =
117
                "<a href=\"javascript:openWithSelfMain('" . XOOPS_URL . '/modules/xfguestbook/contact.php?msg_id=' . $onemsg->getVar('msg_id') . "', 'contact', 600, 450);\"><img src=\"" . XOOPS_URL
118
                . "/images/icons/email.gif\" alt=\"" . _SENDEMAILTO . "\" /></a>";
119
        }
120
        // url
121
        if ($onemsg->getVar('url')) {
122
            $a_msg['url'] = '<a href="' . $onemsg->getVar('url') . '" target="_blank"><img src="' . XOOPS_URL . '/images/icons/www.gif" alt="' . _VISITWEBSITE . '"></a>';
123
        }
124
        // gender
125
        if ($onemsg->getVar('gender') !== '') {
126
            $a_msg['gender'] = '<a href="index.php?op=show_gender&param=' . $onemsg->getVar('gender') . '"><img src="assets/images/' . $onemsg->getVar('gender') . '.gif"</a>';
127
        }
128
        // flag
129
        if ($onemsg->getVar('country') !== '') {
130
            if ($onemsg->getVar('country') != 'other') {
131
                $flag = XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->dirname() . '/assets/images/flags/' . $onemsg->getVar('flagdir') . '/' . $onemsg->getVar('country') . '.gif';
132
                if (array_key_exists($onemsg->getVar('flagdir') . '/' . $onemsg->getVar('country'), $arr_country)) {
133
                    $country_name = $arr_country[$onemsg->getVar('flagdir') . '/' . $onemsg->getVar('country')];
134
                } else {
135
                    $country_name = '';
136
                }
137 View Code Duplication
                if (file_exists($flag)) {
138
                    $a_msg['country'] =
139
                        "<img src=\"" . XOOPS_URL . '/modules/xfguestbook/assets/images/flags/' . $onemsg->getVar('flagdir') . '/' . $onemsg->getVar('country') . ".gif\" alt=\"" . $country_name
140
                        . "\">";
141
                } else {
142
                    $a_msg['country'] = $country_name;
143
                }
144
                $a_msg['country'] = "<a href=\"index.php?op=show_country&param=" . $onemsg->getVar('flagdir') . '/' . $onemsg->getVar('country') . "\">" . $a_msg['country'] . '</a>';
145
            } else {
146
                $a_msg['country'] = $onemsg->getVar('other');
147
            }
148
        }
149
        $a_msg['msg_id']  = $onemsg->getVar('msg_id');
150
        $a_msg['i']       = $nbmsg;
151
        $a_msg['title']   = $onemsg->getVar('title');
152
        $a_msg['date']    = formatTimestamp($onemsg->getVar('post_time'), 's');
153
        $a_msg['message'] = $onemsg->getVar('message');
154
        if ($options['opt_url'] == 1) {
155
            $a_msg['message'] = str_replace('target="_blank"', 'target="_blank" rel="nofollow"', $a_msg['message']);
156
        }
157
        $a_msg['note_msg']  = $onemsg->getVar('note');
158
        $a_msg['poster_ip'] = $onemsg->getVar('poster_ip');
159
        $a_msg['moderate']  = $onemsg->getVar('moderate');
160
        if (isset($country_name)) {
161
            $a_msg['local'] = "<a href=\"index.php?op=show_country&param=" . $onemsg->getVar('flagdir') . '/' . $onemsg->getVar('country') . "\">" . $country_name . '</a>';
162
        }
163
        $a_msg['photo'] = $onemsg->getVar('photo');
164
        $xoopsTpl->append('msg', $a_msg);
165
        $nbmsg--;
166
    }
167
}
168
169
function xfgb_genderlist()
170
{
171
    global $options, $xoopsTpl, $xoopsModuleConfig, $xoopsModule, $msg_handler;
172
    $criteria = new Criteria('moderate', 0);
173
    $arr_msg  = $msg_handler->countMsgByGender($criteria);
174
    $i        = 0;
175
    foreach ($arr_msg as $k => $v) {
176
        if ($k === 'M') {
177
            $gender[$i] = _MD_XFGB_MALES . '<br>';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$gender was never initialized. Although not strictly required by PHP, it is generally a good practice to add $gender = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
178
            $gender[$i] .= '<img src="assets/images/M.gif" alt="' . _MD_XFGB_MALES . '"><br><br>';
0 ignored issues
show
The variable $gender does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
179
            $gender[$i] .= '<a href="index.php?op=show_gender&param=M">' . $v . _MD_XFGB_MESSAGES . '</a>';
180
        } elseif ($k === 'F') {
181
            $gender[$i] = _MD_XFGB_FEMALES . '<br>';
182
            $gender[$i] .= '<img src="assets/images/F.gif" alt="' . _MD_XFGB_FEMALES . '"><br><br>';
183
            $gender[$i] .= '<a href="index.php?op=show_gender&param=F">' . $v . _MD_XFGB_MESSAGES . '</a>';
184
        } else {
185
            $gender[$i] = _MD_XFGB_UNKNOW2 . '<br>';
186
            $gender[$i] .= '<img src="assets/images/U.gif"><br><br>';
187
            $gender[$i] .= $v . _MD_XFGB_MESSAGES;
188
        }
189
        $i++;
190
    }
191
    $xoopsTpl->assign('gender', $gender);
192
    $xoopsTpl->assign('display_gender', $options['opt_gender']);
193
}
194
195
// end functions
196
197
// if op = show_***, functions needed
198
//if (substr($op, 0, 4) == 'show') {
199
if (0 === strpos($op, 'show')) {
200
    $debut = isset($_GET['debut']) ? (int)$_GET['debut'] : 0;
201
    $param = isset($_GET['param']) ? $_GET['param'] : '';
202
203
    include_once('include/functions.php');
204
    $xoopsOption['template_main'] = 'xfguestbook_index.tpl';
205
    include_once(XOOPS_ROOT_PATH . '/header.php');
206
    include_once(XOOPS_ROOT_PATH . '/class/pagenav.php');
207
    include_once(XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->dirname() . '/include/config.inc.php');
208
    $options = getOptions();
209
210
    $criteria = new Criteria('moderate', 0);
211
    $nbmsg    = $msg_handler->countMsg($criteria);
212
213
    $xoopsTpl->assign('msg_message_count', sprintf(_MD_XFGB_THEREIS, '<b>' . $nbmsg . '</b>'));
214
    $xoopsTpl->assign('msg_moderated', $xoopsModuleConfig['moderate']);
215
    $xoopsTpl->assign('msg_lang_name', $xoopsConfig['language']);
216
    $xoopsTpl->assign('xoops_pagetitle', $xoopsModule->name() . ' -messages');
217
    if ($adminview) {
218
        $nbwait = $msg_handler->countMsg(new Criteria('moderate', '1'));
219
        $xoopsTpl->assign('msg_moderate_text', sprintf(_MD_XFGB_MODERATING, "<font class='fg2'><a href='" . XOOPS_URL . "/modules/xfguestbook/index.php?op=show_waiting'>" . $nbwait . '</a></font>'));
220
    }
221
}
222
223
switch ($op) {
224 View Code Duplication
    case 'delete':
225
        if ($adminview) {
226
            include_once(XOOPS_ROOT_PATH . '/header.php');
227
            delete($msg_id);
228
        } else {
229
            redirect_header('index.php', 1, '');
230
        }
231
        break;
232
233 View Code Duplication
    case 'approve':
234
        if ($adminview) {
235
            include_once(XOOPS_ROOT_PATH . '/header.php');
236
            approve($msg_id);
237
        } else {
238
            redirect_header('index.php', 1, '');
239
        }
240
        break;
241
242
    case 'show_stat':
243
        if ($options['opt_gender'] > 0) {
244
            xfgb_genderlist();
245
        }
246
        break;
247
248 View Code Duplication
    case 'show_waiting':
249
        $pagenav = new XoopsPageNav($nbwait, $xoopsModuleConfig['perpage'], $debut, 'debut', 'op=show_waiting');
250
        $xoopsTpl->assign('msg_page_nav', $pagenav->renderNav());
251
        $criteria = new Criteria('moderate', 1);
252
        $criteria->setOrder('DESC');
253
        $criteria->setLimit($xoopsModuleConfig['perpage']);
254
        $criteria->setStart($debut);
255
        $msg =& $msg_handler->getObjects($criteria);
256
        $nbwait -= $debut;
257
        $nbmsg = $nbwait;
258
        xfgb_getmsg($msg);
259
        break;
260
261
    case 'show_one':
262
        if ($adminview) {
263
            $criteria = new Criteria('msg_id', $msg_id);
264
        } else {
265
            $criteria = new CriteriaCompo(new Criteria('moderate', '0'));
266
            $criteria->add(new Criteria('msg_id', $msg_id));
267
        }
268
        $msg =& $msg_handler->getObjects($criteria);
269
        xfgb_getmsg($msg);
270
        if ($options['opt_gender'] > 0) {
271
            xfgb_genderlist();
272
        }
273
        break;
274
275
    case 'show_country':
276
        list($flagdir, $country) = explode('/', $param);
277
        $criteria = new CriteriaCompo(new Criteria('moderate', '0'));
278
        if ($flagdir == $xoopsModuleConfig['flagdir']) {
279
            $criteria->add(new Criteria('flagdir', $flagdir));
280
        }
281
        $criteria->add(new Criteria('country', $country));
282
        $nbmsg   = $msg_handler->countMsg($criteria);
283
        $pagenav = new XoopsPageNav($nbmsg, $xoopsModuleConfig['perpage'], $debut, 'debut', 'op=show_country&param=' . $param);
284
        $criteria->setOrder('DESC');
285
        $criteria->setLimit($xoopsModuleConfig['perpage']);
286
        $criteria->setStart($debut);
287
        $msg =& $msg_handler->getObjects($criteria);
288
        $nbmsg -= $debut;
289
        $xoopsTpl->assign('msg_page_nav', $pagenav->renderNav());
290
        xfgb_getmsg($msg);
291
        break;
292
293
    case 'show_gender':
294
        $criteria = new CriteriaCompo(new Criteria('moderate', '0'));
295
        $criteria->add(new Criteria('gender', $param));
296
        $nbmsg   = $msg_handler->countMsg($criteria);
297
        $pagenav = new XoopsPageNav($nbmsg, $xoopsModuleConfig['perpage'], $debut, 'debut', 'op=show_gender&param=' . $param);
298
        $criteria->setOrder('DESC');
299
        $criteria->setLimit($xoopsModuleConfig['perpage']);
300
        $criteria->setStart($debut);
301
        $msg =& $msg_handler->getObjects($criteria);
302
        $nbmsg -= $debut;
303
        $xoopsTpl->assign('msg_page_nav', $pagenav->renderNav());
304
        xfgb_getmsg($msg);
305
        if ($options['opt_gender'] > 0) {
306
            xfgb_genderlist();
307
        }
308
        break;
309
310
    case 'show_all':
311 View Code Duplication
    default:
312
        $pagenav = new XoopsPageNav($nbmsg, $xoopsModuleConfig['perpage'], $debut, 'debut', '');
313
        $xoopsTpl->assign('msg_page_nav', $pagenav->renderNav());
314
        $criteria = new Criteria('moderate', 0);
315
        $criteria->setOrder('DESC');
316
        $criteria->setLimit($xoopsModuleConfig['perpage']);
317
        $criteria->setStart($debut);
318
        $msg =& $msg_handler->getObjects($criteria);
319
        $nbmsg -= $debut;
320
        xfgb_getmsg($msg);
321
        if ($options['opt_gender'] > 0) {
322
            xfgb_genderlist();
323
        }
324
        break;
325
326 View Code Duplication
    case 'cancel':
327
        $photos_dir     = XOOPS_UPLOAD_PATH . '/' . $xoopsModule->getVar('dirname');
328
        $nb_removed_tmp = xfgb_clear_tmp_files($photos_dir);
329
        redirect_header('index.php', 0);
330
        break;
331
}
332
$sql = $xoopsDB->query('SELECT * FROM ' . $xoopsDB->prefix('xfguestbook_country') . ' ORDER BY country_name ASC');
333
334
while ($coun = $xoopsDB->fetchArray($sql)) {
335
    $sql2 = $xoopsDB->query('SELECT COUNT(country) tot FROM ' . $xoopsDB->prefix('xfguestbook_msg') . " WHERE country='" . $coun['country_code'] . "'");
336
    list($tlocal) = $xoopsDB->fetchRow($sql2);
337
    $tlocal = $tlocal ?: '0';
338
    if ($tlocal > 0) {
339
        $opt["<a href=\"index.php?op=show_country&param=" . $xoopsModuleConfig['flagdir'] . '/' . $coun['country_code'] . "\">" . $coun['country_name'] . '</a>'] = $tlocal;
340
    } else {
341
        $opt[$coun['country_name']] = $tlocal;
342
    }
343
}
344
$xoopsTpl->assign('country_l', $opt);
345
346
include(XOOPS_ROOT_PATH . '/footer.php');
347