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

index.php (2 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
//
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 __DIR__ . '/../../mainfile.php';
27
//include_once(XOOPS_ROOT_PATH."/modules/".$xoopsModule->dirname()."/class/msg.php");
28
include_once XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->dirname() . '/class/util.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
$msgHandler = 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 $msgHandler, $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        = $msgHandler->get($msg_id);
59
        $del_msg_ok = $msgHandler->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_XFGUESTBOOK_MSGDELETED;
67
        } else {
68
            $messagesent = MD_XFGUESTBOOK_ERRORDEL;
69
        }
70
        redirect_header('index.php', 2, $messagesent);
71
    } else {
72
        xoops_confirm(['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 $msgHandler;
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 = $msgHandler->get($msg_id);
84
    $msg->setVar('moderate', 0);
85
    if (!$msgHandler->insert($msg)) {
86
        $messagesent = MD_XFGUESTBOOK_ERRORVALID;
87
    } else {
88
        $messagesent = MD_XFGUESTBOOK_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 = XfguestbookUtil::getAllCountry();
101
    $xoopsTpl->assign('display_msg', true);
102
    foreach ($msg as $onemsg) {
103
        if ($poster = XfguestbookUtil::get_user_data($onemsg->getVar('user_id'))) {
104
            $a_msg = &$poster;
105
        } else {
106
            $a_msg             = [];
107
            $a_msg['poster']   = $onemsg->getVar('uname');
108
            $a_msg['rank']     = '';
109
            $a_msg['rank_img'] = '';
110
            $a_msg['avatar']   = '';
111
        }
112
        $memberHandler = xoops_getHandler('member');
113
        $user          = $memberHandler->getUser($onemsg->getVar('user_id'));
114
        // email
115
        if ($xoopsModuleConfig['showemail']
116
            || ($onemsg->getVar('email')
117
                && (($user->getVar('user_viewemail') == 1
118
                     || $onemsg->getVar('user_id') == 0)
119
                    && is_object($xoopsUser)))
120
        ) {
121
            $a_msg['email'] = "<a href=\"javascript:openWithSelfMain('"
122
                              . XOOPS_URL
123
                              . '/modules/xfguestbook/contact.php?msg_id='
124
                              . $onemsg->getVar('msg_id')
125
                              . '\', \'contact\', 600, 450);"><img src="'
126
                              . XOOPS_URL
127
                              . '/images/icons/email.gif" alt="'
128
                              . _SENDEMAILTO
129
                              . '" /></a>';
130
        }
131
        // url
132
        if ($onemsg->getVar('url')) {
133
            $a_msg['url'] = '<a href="' . $onemsg->getVar('url') . '" target="_blank"><img src="' . XOOPS_URL . '/images/icons/www.gif" alt="' . _VISITWEBSITE . '"></a>';
134
        }
135
        // gender
136
        if ($onemsg->getVar('gender') !== '') {
137
            $a_msg['gender'] = '<a href="index.php?op=show_gender&param=' . $onemsg->getVar('gender') . '"><img src="assets/images/' . $onemsg->getVar('gender') . '.gif"</a>';
138
        }
139
        // flag
140
        if ($onemsg->getVar('country') !== '') {
141
            if ($onemsg->getVar('country') !== 'other') {
142
                $flag = XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->dirname() . '/assets/images/flags/' . $onemsg->getVar('flagdir') . '/' . $onemsg->getVar('country') . '.gif';
143
                if (array_key_exists($onemsg->getVar('flagdir') . '/' . $onemsg->getVar('country'), $arr_country)) {
144
                    $country_name = $arr_country[$onemsg->getVar('flagdir') . '/' . $onemsg->getVar('country')];
145
                } else {
146
                    $country_name = '';
147
                }
148 View Code Duplication
                if (file_exists($flag)) {
149
                    $a_msg['country'] = '<img src="'
150
                                        . XOOPS_URL
151
                                        . '/modules/xfguestbook/assets/images/flags/'
152
                                        . $onemsg->getVar('flagdir')
153
                                        . '/'
154
                                        . $onemsg->getVar('country')
155
                                        . '.gif" alt="'
156
                                        . $country_name
157
                                        . '">';
158
                } else {
159
                    $a_msg['country'] = $country_name;
160
                }
161
                $a_msg['country'] = '<a href="index.php?op=show_country&param=' . $onemsg->getVar('flagdir') . '/' . $onemsg->getVar('country') . '">' . $a_msg['country'] . '</a>';
162
            } else {
163
                $a_msg['country'] = $onemsg->getVar('other');
164
            }
165
        }
166
        $a_msg['msg_id']  = $onemsg->getVar('msg_id');
167
        $a_msg['i']       = $nbmsg;
168
        $a_msg['title']   = $onemsg->getVar('title');
169
        $a_msg['date']    = formatTimestamp($onemsg->getVar('post_time'), 's');
170
        $a_msg['message'] = $onemsg->getVar('message');
171
        if ($options['opt_url'] == 1) {
172
            $a_msg['message'] = str_replace('target="_blank"', 'target="_blank" rel="nofollow"', $a_msg['message']);
173
        }
174
        $a_msg['note_msg']  = $onemsg->getVar('note');
175
        $a_msg['poster_ip'] = $onemsg->getVar('poster_ip');
176
        $a_msg['moderate']  = $onemsg->getVar('moderate');
177
        if (isset($country_name)) {
178
            $a_msg['local'] = '<a href="index.php?op=show_country&param=' . $onemsg->getVar('flagdir') . '/' . $onemsg->getVar('country') . '">' . $country_name . '</a>';
179
        }
180
        $a_msg['photo'] = $onemsg->getVar('photo');
181
        $xoopsTpl->append('msg', $a_msg);
182
        $nbmsg--;
183
    }
184
}
185
186
function xfgb_genderlist()
187
{
188
    global $options, $xoopsTpl, $xoopsModuleConfig, $xoopsModule, $msgHandler;
189
    $criteria = new Criteria('moderate', 0);
190
    $arr_msg  = $msgHandler->countMsgByGender($criteria);
191
    $i        = 0;
192
    $gender = [];
193
    foreach ($arr_msg as $k => $v) {
194
        if ($k === 'M') {
195
            $gender[$i] = MD_XFGUESTBOOK_MALES . '<br>';
196
            $gender[$i] .= '<img src="assets/images/M.gif" alt="' . MD_XFGUESTBOOK_MALES . '"><br><br>';
197
            $gender[$i] .= '<a href="index.php?op=show_gender&param=M">' . $v . MD_XFGUESTBOOK_MESSAGES . '</a>';
198
        } elseif ($k === 'F') {
199
            $gender[$i] = MD_XFGUESTBOOK_FEMALES . '<br>';
200
            $gender[$i] .= '<img src="assets/images/F.gif" alt="' . MD_XFGUESTBOOK_FEMALES . '"><br><br>';
201
            $gender[$i] .= '<a href="index.php?op=show_gender&param=F">' . $v . MD_XFGUESTBOOK_MESSAGES . '</a>';
202
        } else {
203
            $gender[$i] = MD_XFGUESTBOOK_UNKNOW2 . '<br>';
204
            $gender[$i] .= '<img src="assets/images/U.gif"><br><br>';
205
            $gender[$i] .= $v . MD_XFGUESTBOOK_MESSAGES;
206
        }
207
        $i++;
208
    }
209
    $xoopsTpl->assign('gender', $gender);
210
    $xoopsTpl->assign('display_gender', $options['opt_gender']);
211
}
212
213
// end functions
214
215
// if op = show_***, functions needed
216
//if (substr($op, 0, 4) == 'show') {
217
if (0 === strpos($op, 'show')) {
218
    $debut = isset($_GET['debut']) ? (int)$_GET['debut'] : 0;
219
    $param = isset($_GET['param']) ? $_GET['param'] : '';
220
221
    include_once __DIR__ . '/class/util.php';
222
    $GLOBALS['xoopsOption']['template_main'] = 'xfguestbook_index.tpl';
223
    include_once XOOPS_ROOT_PATH . '/header.php';
224
    include_once XOOPS_ROOT_PATH . '/class/pagenav.php';
225
    include_once XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->dirname() . '/include/config.inc.php';
226
    $options = getOptions();
227
228
    $criteria = new Criteria('moderate', 0);
229
    $nbmsg    = $msgHandler->countMsg($criteria);
230
231
    $xoopsTpl->assign('msg_message_count', sprintf(MD_XFGUESTBOOK_THEREIS, '<b>' . $nbmsg . '</b>'));
232
    $xoopsTpl->assign('msg_moderated', $xoopsModuleConfig['moderate']);
233
    $xoopsTpl->assign('msg_lang_name', $xoopsConfig['language']);
234
    $xoopsTpl->assign('xoops_pagetitle', $xoopsModule->name() . ' -messages');
235
    if ($adminview) {
236
        $nbwait = $msgHandler->countMsg(new Criteria('moderate', '1'));
237
        $xoopsTpl->assign('msg_moderate_text', sprintf(MD_XFGUESTBOOK_MODERATING, "<font class='fg2'><a href='" . XOOPS_URL . "/modules/xfguestbook/index.php?op=show_waiting'>" . $nbwait . '</a></font>'));
238
    }
239
}
240
241
switch ($op) {
242 View Code Duplication
    case 'delete':
243
        if ($adminview) {
244
            include_once XOOPS_ROOT_PATH . '/header.php';
245
            delete($msg_id);
246
        } else {
247
            redirect_header('index.php', 1, '');
248
        }
249
        break;
250
251 View Code Duplication
    case 'approve':
252
        if ($adminview) {
253
            include_once XOOPS_ROOT_PATH . '/header.php';
254
            approve($msg_id);
255
        } else {
256
            redirect_header('index.php', 1, '');
257
        }
258
        break;
259
260
    case 'show_stat':
261
        if ($options['opt_gender'] > 0) {
262
            xfgb_genderlist();
263
        }
264
        break;
265
266 View Code Duplication
    case 'show_waiting':
267
        $pagenav = new XoopsPageNav($nbwait, $xoopsModuleConfig['perpage'], $debut, 'debut', 'op=show_waiting');
268
        $xoopsTpl->assign('msg_page_nav', $pagenav->renderNav());
269
        $criteria = new Criteria('moderate', 1);
270
        $criteria->setOrder('DESC');
271
        $criteria->setLimit($xoopsModuleConfig['perpage']);
272
        $criteria->setStart($debut);
273
        $msg    =& $msgHandler->getObjects($criteria);
274
        $nbwait -= $debut;
275
        $nbmsg  = $nbwait;
276
        xfgb_getmsg($msg);
277
        break;
278
279
    case 'show_one':
280
        if ($adminview) {
281
            $criteria = new Criteria('msg_id', $msg_id);
282
        } else {
283
            $criteria = new CriteriaCompo(new Criteria('moderate', '0'));
284
            $criteria->add(new Criteria('msg_id', $msg_id));
285
        }
286
        $msg =& $msgHandler->getObjects($criteria);
287
        xfgb_getmsg($msg);
288
        if ($options['opt_gender'] > 0) {
289
            xfgb_genderlist();
290
        }
291
        break;
292
293
    case 'show_country':
294
        list($flagdir, $country) = explode('/', $param);
295
        $criteria = new CriteriaCompo(new Criteria('moderate', '0'));
296
        if ($flagdir == $xoopsModuleConfig['flagdir']) {
297
            $criteria->add(new Criteria('flagdir', $flagdir));
298
        }
299
        $criteria->add(new Criteria('country', $country));
300
        $nbmsg   = $msgHandler->countMsg($criteria);
301
        $pagenav = new XoopsPageNav($nbmsg, $xoopsModuleConfig['perpage'], $debut, 'debut', 'op=show_country&param=' . $param);
302
        $criteria->setOrder('DESC');
303
        $criteria->setLimit($xoopsModuleConfig['perpage']);
304
        $criteria->setStart($debut);
305
        $msg   =& $msgHandler->getObjects($criteria);
306
        $nbmsg -= $debut;
307
        $xoopsTpl->assign('msg_page_nav', $pagenav->renderNav());
308
        xfgb_getmsg($msg);
309
        break;
310
311
    case 'show_gender':
312
        $criteria = new CriteriaCompo(new Criteria('moderate', '0'));
313
        $criteria->add(new Criteria('gender', $param));
314
        $nbmsg   = $msgHandler->countMsg($criteria);
315
        $pagenav = new XoopsPageNav($nbmsg, $xoopsModuleConfig['perpage'], $debut, 'debut', 'op=show_gender&param=' . $param);
316
        $criteria->setOrder('DESC');
317
        $criteria->setLimit($xoopsModuleConfig['perpage']);
318
        $criteria->setStart($debut);
319
        $msg   =& $msgHandler->getObjects($criteria);
320
        $nbmsg -= $debut;
321
        $xoopsTpl->assign('msg_page_nav', $pagenav->renderNav());
322
        xfgb_getmsg($msg);
323
        if ($options['opt_gender'] > 0) {
324
            xfgb_genderlist();
325
        }
326
        break;
327
328
    case 'show_all':
329 View Code Duplication
    default:
330
        $pagenav = new XoopsPageNav($nbmsg, $xoopsModuleConfig['perpage'], $debut, 'debut', '');
331
        $xoopsTpl->assign('msg_page_nav', $pagenav->renderNav());
332
        $criteria = new Criteria('moderate', 0);
333
        $criteria->setOrder('DESC');
334
        $criteria->setLimit($xoopsModuleConfig['perpage']);
335
        $criteria->setStart($debut);
336
        $msg   =& $msgHandler->getObjects($criteria);
337
        $nbmsg -= $debut;
338
        xfgb_getmsg($msg);
339
        if ($options['opt_gender'] > 0) {
340
            xfgb_genderlist();
341
        }
342
        break;
343
344 View Code Duplication
    case 'cancel':
345
        $photos_dir     = XOOPS_UPLOAD_PATH . '/' . $xoopsModule->getVar('dirname');
346
        $nb_removed_tmp = XfguestbookUtil::clear_tmp_files($photos_dir);
347
        redirect_header('index.php', 0);
348
        break;
349
}
350
$sql = $xoopsDB->query('SELECT * FROM ' . $xoopsDB->prefix('xfguestbook_country') . ' ORDER BY country_name ASC');
351
352
while ($coun = $xoopsDB->fetchArray($sql)) {
353
    $sql2 = $xoopsDB->query('SELECT COUNT(country) tot FROM ' . $xoopsDB->prefix('xfguestbook_msg') . " WHERE country='" . $coun['country_code'] . '\'');
354
    list($tlocal) = $xoopsDB->fetchRow($sql2);
355
    $tlocal = $tlocal ?: '0';
356
    if ($tlocal > 0) {
357
        $opt['<a href="index.php?op=show_country&param=' . $xoopsModuleConfig['flagdir'] . '/' . $coun['country_code'] . '">' . $coun['country_name'] . '</a>'] = $tlocal;
358
    } else {
359
        $opt[$coun['country_name']] = $tlocal;
360
    }
361
}
362
$xoopsTpl->assign('country_l', $opt);
363
364
include XOOPS_ROOT_PATH . '/footer.php';
365