Issues (1844)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  Header Injection
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

admin/upgrade.php (5 issues)

1
<?php declare(strict_types=1);
2
3
/*
4
 * You may not change or alter any portion of this comment or credits
5
 * of supporting developers from this source code or any supporting source code
6
 * which is considered copyrighted (c) material of the original comment or credit authors.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * @copyright    {@link https://xoops.org/ XOOPS Project}
15
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
16
 * @author       Brian Wahoff <[email protected]>
17
 * @author       Eric Juden <[email protected]>
18
 * @author       XOOPS Development Team
19
 */
20
21
use Xmf\Module\Admin;
22
use Xmf\Request;
23
use XoopsModules\Xhelp;
24
25
require_once __DIR__ . '/admin_header.php';
26
xoops_load('XoopsPagenav');
27
28
$helper = Xhelp\Helper::getInstance();
29
30
global $xoopsModule;
31
$module_id = $xoopsModule->getVar('mid');
32
33
$op = 'default';
34
35
if (Request::hasVar('op', 'REQUEST')) {
36
    $op = $_REQUEST['op'];
37
}
38
39
switch ($op) {
40
    case 'checkTables':
41
        checkTables();
42
        break;
43
    case 'upgradeDB':
44
        try {
45
            upgradeDB();
46
        } catch (Exception $e) {
47
        }
48
        break;
49
    default:
50
        $helper->redirect('admin/index.php');
51
        break;
52
}
53
54
/**
55
 * @param string $oldName
56
 * @param string $newName
57
 * @return bool
58
 */
59
function renameTable(string $oldName, string $newName): bool
60
{
61
    global $xoopsDB;
62
    $qry = runQuery(sprintf('ALTER TABLE %s RENAME %s', $xoopsDB->prefix($oldName), $xoopsDB->prefix($newName)), sprintf(_AM_XHELP_MSG_RENAME_TABLE, $oldName, $newName), sprintf(_AM_XHELP_MSG_RENAME_TABLE_ERR, $oldName));
63
64
    return $qry;
65
}
66
67
/**
68
 * @param string $query
69
 * @param string $goodmsg
70
 * @param string $badmsg
71
 * @return bool
72
 */
73
function runQuery(string $query, string $goodmsg, string $badmsg): bool
74
{
75
    global $xoopsDB;
76
    $ret = $xoopsDB->query($query);
77
    if (!$ret) {
78
        echo "<li class='err'>$badmsg</li>";
79
80
        return false;
81
    }
82
83
    echo "<li class='ok'>$goodmsg</li>";
84
85
    return true;
86
}
87
88
function checkTables()
89
{
90
    global $xoopsModule;
91
    xoops_cp_header();
92
    //echo $oAdminButton->renderButtons('');
93
    $adminObject = Admin::getInstance();
94
    $adminObject->displayNavigation('upgrade.php?op=checkTables');
95
    //1. Determine previous release
96
    if (!Xhelp\Utility::tableExists('xhelp_meta')) {
97
        $ver = '0.5';
98
    } elseif (!$ver = Xhelp\Utility::getMeta('version')) {
99
        echo('Unable to determine previous version.');
100
    }
101
102
    $currentVer = round($xoopsModule->getVar('version') / 100, 2);
103
104
    printf('<h2>' . _AM_XHELP_CURRENTVER . '</h2>', $currentVer);
105
    printf('<h2>' . _AM_XHELP_DBVER . '</h2>', $ver);
106
107
    if ($ver == $currentVer) {
108
        //No updates are necessary
109
        echo '<div>' . _AM_XHELP_DB_NOUPDATE . '</div>';
110
    } elseif ($ver < $currentVer) {
111
        //Needs to upgrade
112
        echo '<div>' . _AM_XHELP_DB_NEEDUPDATE . '</div>';
113
        echo '<form method="post" action="upgrade.php"><input type="hidden" name="op" value="upgradeDB"><input type="submit" value="' . _AM_XHELP_UPDATE_NOW . "\" onclick='_openProgressWindow();'></form>";
114
    } else {
115
        //Tried to downgrade
116
        echo '<div>' . _AM_XHELP_DB_NEEDINSTALL . '</div>';
117
    }
118
119
    require_once __DIR__ . '/admin_footer.php';
120
}
121
122
echo "<script type='text/javascript'>
123
function _openProgressWindow()
124
{
125
    newwindow = openWithSelfMain('upgradeProgress.php','progress','430','100', true);
126
}
127
    </script>";
128
129
/**
130
 * @throws \Exception
131
 */
132
function upgradeDB()
133
{
134
    global $xoopsModule;
135
    $helper = Xhelp\Helper::getInstance();
136
    /** @var \XoopsMySQLDatabase $xoopsDB */
137
    $xoopsDB = \XoopsDatabaseFactory::getDatabaseConnection();
138
    //1. Determine previous release
139
    //   *** Update this in sql/mysql.sql for each release **
140
    if (!Xhelp\Utility::tableExists('xhelp_meta')) {
141
        $ver = '0.5';
142
    } elseif (!$ver = Xhelp\Utility::getMeta('version')) {
143
        exit(_AM_XHELP_VERSION_ERR);
144
    }
145
146
    /** @var \XoopsModules\Xhelp\StaffHandler $staffHandler */
147
    $staffHandler = $helper->getHandler('Staff');
148
    /** @var \XoopsModules\Xhelp\MembershipHandler $membershipHandler */
149
    $membershipHandler = $helper->getHandler('Membership');
150
    /** @var \XoopsModules\Xhelp\TicketHandler $ticketHandler */
151
    $ticketHandler = $helper->getHandler('Ticket');
152
    /** @var \XoopsMemberHandler $memberHandler */
153
    $memberHandler = xoops_getHandler('member');
154
    /** @var \XoopsModules\Xhelp\RoleHandler $roleHandler */
155
    $roleHandler = $helper->getHandler('Role');
156
157
    $mid = $xoopsModule->getVar('mid');
158
159
    xoops_cp_header();
160
    //echo $oAdminButton->renderButtons('');
161
    $adminObject = Admin::getInstance();
162
    $adminObject->displayNavigation(basename(__FILE__));
163
164
    echo '<h2>' . _AM_XHELP_UPDATE_DB . '</h2>';
165
    $ret = true;
166
    //2. Do All Upgrades necessary to make current
167
    //   Break statements are omitted on purpose
168
    switch ($ver) {
169
        case '0.5':
170
            set_time_limit(60);
171
            printf('<h3>' . _AM_XHELP_UPDATE_TO . '</h3>', '0.6');
172
            echo '<ul>';
173
            //Create meta table
174
            $ret = $ret
175
                   && runQuery(
176
                       sprintf("CREATE TABLE %s (metakey VARCHAR(50) NOT NULL DEFAULT '', metavalue VARCHAR(255) NOT NULL DEFAULT '', PRIMARY KEY (metakey)) ENGINE=MyISAM;", $xoopsDB->prefix('xhelp_meta')),
177
                       sprintf(_AM_XHELP_MSG_ADDTABLE, 'xhelp_meta'),
178
                       sprintf(_AM_XHELP_MSG_ADDTABLE_ERR, 'xhelp_meta')
179
                   );
180
181
            //Insert Current Version into table
182
            $qry = $xoopsDB->query(sprintf("INSERT INTO `%s` VALUES('version', %s)", $xoopsDB->prefix('xhelp_meta'), $xoopsDB->quoteString($ver)));
183
184
            //Update xhelp_responses table
185
            $ret = $ret
186
                   && runQuery(sprintf("ALTER TABLE %s ADD private INT(11) NOT NULL DEFAULT '0'", $xoopsDB->prefix('xhelp_responses')), sprintf(_AM_XHELP_MSG_MODIFYTABLE, 'xhelp_responses'), sprintf(_AM_XHELP_MSG_MODIFYTABLE_ERR, 'xhelp_responses'));
187
188
            //Retrieve uid's of all staff members
189
            $qry = $xoopsDB->query('SELECT uid FROM ' . $xoopsDB->prefix('xhelp_staff') . ' ORDER BY uid');
190
191
            //Get email addresses in user profile
192
            $staff = [];
193
            while (false !== ($arr = $xoopsDB->fetchArray($qry))) {
0 ignored issues
show
It seems like $qry can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

193
            while (false !== ($arr = $xoopsDB->fetchArray(/** @scrutinizer ignore-type */ $qry))) {
Loading history...
194
                $staff[$arr['uid']] = '';
195
            }
196
            $xoopsDB->freeRecordSet($qry);
0 ignored issues
show
It seems like $qry can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::freeRecordSet() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

196
            $xoopsDB->freeRecordSet(/** @scrutinizer ignore-type */ $qry);
Loading history...
197
198
            $query = 'SELECT uid, email FROM ' . $xoopsDB->prefix('users') . ' WHERE uid IN (' . implode(',', array_keys($staff)) . ')';
199
            $qry   = $xoopsDB->query($query);
200
            while (false !== ($arr = $xoopsDB->fetchArray($qry))) {
201
                $staff[$arr['uid']] = $arr['email'];
202
            }
203
            $xoopsDB->freeRecordSet($qry);
204
205
            //Update xhelp_staff table
206
            $ret = $ret
207
                   && runQuery(sprintf("ALTER TABLE %s ADD email VARCHAR(255) NOT NULL DEFAULT '' AFTER uid, ADD notify INT(11) NOT NULL DEFAULT '0'", $xoopsDB->prefix('xhelp_staff')), sprintf(_AM_XHELP_MSG_MODIFYTABLE, 'xhelp_staff'), sprintf(_AM_XHELP_MSG_MODIFYTABLE_ERR, 'xhelp_staff'));
208
209
            //Update existing staff records
210
            $staff_tbl = $xoopsDB->prefix('xhelp_staff');
211
            $notif_tbl = $xoopsDB->prefix('xoopsnotifications');
212
            $email_tpl = $xoopsModule->getInfo('_email_tpl');
213
            foreach ($staff as $uid => $email) {
214
                //get notifications for current user
215
                $usernotif = 0;
216
                $qry       = $xoopsDB->query(sprintf("SELECT DISTINCT not_category, not_event FROM `%s` WHERE not_uid = %u AND not_category='dept' AND not_modid=%u", $notif_tbl, $uid, $mid));
217
                while (false !== ($arr = $xoopsDB->fetchArray($qry))) {
218
                    //Look for current event information in $email_tpl
219
                    foreach ($email_tpl as $tpl) {
220
                        if (($tpl['name'] == $arr['not_event']) && ($tpl['category'] == $arr['not_category'])) {
221
                            $usernotif |= 2 ** $tpl['bit_value'];
222
                            break;
223
                        }
224
                    }
225
                }
226
227
                //Update xhelp_staff with user notifications & email address
228
                $ret = $ret
229
                       && runQuery(sprintf('UPDATE `%s` SET email = %s, notify = %u WHERE uid = %u', $staff_tbl, $xoopsDB->quoteString($email), $usernotif, $uid), sprintf(_AM_XHELP_MSG_UPDATESTAFF, $uid), sprintf(_AM_XHELP_MSG_UPDATESTAFF_ERR, $uid));
230
            }
231
            echo '</ul>';
232
        // no break
233
        case '0.6':
234
            set_time_limit(60);
235
            //Do DB updates to make 0.7
236
            printf('<h3>' . _AM_XHELP_UPDATE_TO . '</h3>', '0.7');
237
238
            echo '<ul>';
239
            // change table names to lowercase
240
            $ret = $ret && renameTable('xhelp_logMessages', 'xhelp_logmessages');
241
            $ret = $ret && renameTable('xhelp_responseTemplates', 'xhelp_responsetemplates');
242
            $ret = $ret && renameTable('xhelp_jStaffDept', 'xhelp_jstaffdept');
243
            $ret = $ret && renameTable('xhelp_staffReview', 'xhelp_staffreview');
244
            $ret = $ret && renameTable('xhelp_emailTpl', 'xhelp_emailtpl');
245
246
            // Remove unused table - xhelp_emailtpl
247
            $ret = $ret
248
                   && runQuery(sprintf('DROP TABLE %s', $xoopsDB->prefix('xhelp_emailtpl')), sprintf(_AM_XHELP_MSG_REMOVE_TABLE, 'xhelp_emailtpl'), sprintf(_AM_XHELP_MSG_NOT_REMOVE_TABLE, 'xhelp_emailtpl'));
249
250
            // xhelp_staff table - permTimestamp
251
            $ret = $ret
252
                   && runQuery(sprintf("ALTER TABLE %s ADD permTimestamp INT(11) NOT NULL DEFAULT '0'", $xoopsDB->prefix('xhelp_staff')), sprintf(_AM_XHELP_MSG_MODIFYTABLE, 'xhelp_staff'), sprintf(_AM_XHELP_MSG_MODIFYTABLE_ERR, 'xhelp_staff'));
253
254
            //Update xhelp_tickets table
255
            $ret = $ret
256
                   && runQuery(sprintf("ALTER TABLE %s MODIFY SUBJECT VARCHAR(100) NOT NULL DEFAULT ''", $xoopsDB->prefix('xhelp_tickets')), sprintf(_AM_XHELP_MSG_MODIFYTABLE, 'xhelp_tickets'), sprintf(_AM_XHELP_MSG_MODIFYTABLE_ERR, 'xhelp_tickets'));
257
258
            $ret = $ret
259
                   && runQuery(
260
                       sprintf(
261
                           "ALTER TABLE %s ADD (serverid INT(11) DEFAULT NULL,
262
                                                             emailHash VARCHAR(100) DEFAULT NULL,
263
                                                             email VARCHAR(100) DEFAULT NULL,
264
                                                             overdueTime INT(11) NOT NULL DEFAULT '0',
265
                                                             KEY emailHash (emailHash))",
266
                           $xoopsDB->prefix('xhelp_tickets')
267
                       ),
268
                       sprintf(_AM_XHELP_MSG_MODIFYTABLE, 'xhelp_tickets'),
269
                       sprintf(_AM_XHELP_MSG_MODIFYTABLE_ERR, 'xhelp_tickets')
270
                   );
271
272
            // Create xhelp_department_mailbox table
273
            $ret = $ret
274
                   && runQuery(
275
                       sprintf(
276
                           'CREATE TABLE %s (id INT(11) NOT NULL AUTO_INCREMENT,
277
                                                          departmentid INT(11) DEFAULT NULL,
278
                                                          emailaddress VARCHAR(255) DEFAULT NULL,
279
                                                          SERVER VARCHAR(50) DEFAULT NULL,
280
                                                          serverport INT(11) DEFAULT NULL,
281
                                                          username VARCHAR(50) DEFAULT NULL,
282
                                                          PASSWORD VARCHAR(50) DEFAULT NULL,
283
                                                          priority TINYINT(4) DEFAULT NULL,
284
                                                          mboxtype INT(11) NOT NULL DEFAULT 1,
285
                                                          PRIMARY KEY  (id),
286
                                                          UNIQUE KEY id (id),
287
                                                          KEY departmentid (departmentid),
288
                                                          KEY emailaddress (emailaddress),
289
                                                          KEY mboxtype (mboxtype)
290
                                                         )ENGINE=MyISAM;',
291
                           $xoopsDB->prefix('xhelp_department_mailbox')
292
                       ),
293
                       sprintf(_AM_XHELP_MSG_ADDTABLE, 'xhelp_department_mailbox'),
294
                       sprintf(_AM_XHELP_MSG_ADDTABLE_ERR, 'xhelp_department_mailbox')
295
                   );
296
297
            // Create xhelp_mailevent table
298
            $ret = $ret
299
                   && runQuery(
300
                       sprintf(
301
                           "CREATE TABLE %s (id INT(11) NOT NULL AUTO_INCREMENT,
302
                                                           mbox_id INT(11) NOT NULL DEFAULT '0',
303
                                                           event_desc TEXT,
304
                                                           event_class INT(11) NOT NULL DEFAULT '0',
305
                                                           posted INT(11) NOT NULL DEFAULT '0',
306
                                                           PRIMARY KEY(id)
307
                                                          )ENGINE=MyISAM;",
308
                           $xoopsDB->prefix('xhelp_mailevent')
309
                       ),
310
                       sprintf(_AM_XHELP_MSG_ADDTABLE, 'xhelp_mailevent'),
311
                       sprintf(_AM_XHELP_MSG_ADDTABLE_ERR, 'xhelp_mailevent')
312
                   );
313
314
            // Create xhelp_roles table
315
            $ret = $ret
316
                   && runQuery(
317
                       sprintf(
318
                           "CREATE TABLE %s (id INT(11) NOT NULL AUTO_INCREMENT,
319
                                                          name VARCHAR(35) NOT NULL DEFAULT '',
320
                                                          description MEDIUMTEXT,
321
                                                          tasks INT(11) NOT NULL DEFAULT '0',
322
                                                          PRIMARY KEY(id)
323
                                                         )ENGINE=MyISAM;",
324
                           $xoopsDB->prefix('xhelp_roles')
325
                       ),
326
                       sprintf(_AM_XHELP_MSG_ADDTABLE, 'xhelp_roles'),
327
                       sprintf(_AM_XHELP_MSG_ADDTABLE_ERR, 'xhelp_roles')
328
                   );
329
330
            // Create xhelp_staffroles table
331
            $ret = $ret
332
                   && runQuery(
333
                       sprintf(
334
                           "CREATE TABLE %s (uid INT(11) NOT NULL DEFAULT '0',
335
                                                         roleid INT(11) NOT NULL DEFAULT '0',
336
                                                         deptid INT(11) NOT NULL DEFAULT '0',
337
                                                         PRIMARY KEY(uid, roleid, deptid)
338
                                                        )ENGINE=MyISAM;",
339
                           $xoopsDB->prefix('xhelp_staffroles')
340
                       ),
341
                       sprintf(_AM_XHELP_MSG_ADDTABLE, 'xhelp_staffroles'),
342
                       sprintf(_AM_XHELP_MSG_ADDTABLE_ERR, 'xhelp_staffroles')
343
                   );
344
345
            // Add default roles to db
346
            if ($hasRoles = Xhelp\Utility::createRoles()) {
347
                echo '<li>' . _AM_XHELP_MESSAGE_DEF_ROLES . '</li>';
348
            } else {
349
                echo '<li>' . _AM_XHELP_MESSAGE_DEF_ROLES_ERROR . '</li>';
350
            }
351
352
            // Set all staff members to have admin permission role
353
            $staffArray = $staffHandler->getObjects();
354
            if ($staffArray) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $staffArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
355
                foreach ($staffArray as $staff) {
356
                    $uid   = $staff->getVar('uid');
357
                    $depts = $membershipHandler->membershipByStaff($uid, true);
358
                    if ($staffHandler->addStaffRole($uid, 1, 0)) {
359
                        echo '<li>' . sprintf(_AM_XHELP_MSG_GLOBAL_PERMS, $uid) . '</li>';
360
                    }
361
362
                    foreach ($depts as $dept) {
363
                        $deptid = $dept->getVar('id');
364
                        if ($staffHandler->addStaffRole($uid, 1, $deptid)) {    // Departmental permissions
365
                            echo '<li>' . sprintf(_AM_XHELP_MSG_UPD_PERMS, $uid, $dept->getVar('department')) . '</li>';
366
                        }
367
                    }
368
369
                    $staff->setVar('permTimestamp', time());        // Set initial value for permTimestamp field
370
                    if ($staffHandler->insert($staff)) {
371
                        echo '<li>' . sprintf(_AM_XHELP_MSG_UPDATESTAFF, $uid) . '</li>';
372
                    } else {
373
                        echo '<li>' . sprintf(_AM_XHELP_MSG_UPDATESTAFF_ERR, $uid) . '</li>';
374
                    }
375
                }
376
            }
377
            echo '</ul>';
378
379
        // no break
380
        case '0.7':
381
            set_time_limit(60);
382
            //Do DB updates to make 0.71
383
            printf('<h3>' . _AM_XHELP_UPDATE_TO . '</h3>', '0.71');
384
385
            echo '<ul>';
386
            echo '</ul>';
387
388
        // no break
389
        case '0.71':
390
            set_time_limit(60);
391
            //Do DB updates to make 0.75
392
            printf('<h3>' . _AM_XHELP_UPDATE_TO . '</h3>', '0.75');
393
394
            echo '<ul>';
395
396
            //Changes for php5 compabibility
397
            $ret = $ret
398
                   && runQuery(sprintf("ALTER TABLE %s MODIFY lastUpdated INT(11) NOT NULL DEFAULT '0'", $xoopsDB->prefix('xhelp_logmessages')), sprintf(_AM_XHELP_MSG_MODIFYTABLE, 'xhelp_logmessages'), sprintf(_AM_XHELP_MSG_MODIFYTABLE_ERR, 'xhelp_logmessages'));
399
            $ret = $ret
400
                   && runQuery(sprintf("ALTER TABLE %s MODIFY department INT(11) NOT NULL DEFAULT '0'", $xoopsDB->prefix('xhelp_jstaffdept')), sprintf(_AM_XHELP_MSG_MODIFYTABLE, 'xhelp_jstaffdept'), sprintf(_AM_XHELP_MSG_MODIFYTABLE_ERR, 'xhelp_jstaffdept'));
401
402
            // Create table for email template information
403
            $ret = $ret
404
                   && runQuery(
405
                       sprintf(
406
                           "CREATE TABLE %s (notif_id INT(11) NOT NULL DEFAULT '0',
407
                                                           staff_setting INT(11) NOT NULL DEFAULT '0',
408
                                                           user_setting INT(11) NOT NULL DEFAULT '0',
409
                                                           staff_options MEDIUMTEXT NOT NULL,
410
                                                           PRIMARY KEY (notif_id)
411
                                                          )ENGINE=MyISAM;",
412
                           $xoopsDB->prefix('xhelp_notifications')
413
                       ),
414
                       sprintf(_AM_XHELP_MSG_ADDTABLE, 'xhelp_notifications'),
415
                       sprintf(_AM_XHELP_MSG_ADDTABLE_ERR, 'xhelp_notifications')
416
                   );
417
418
            // Add xhelp_status table
419
            $ret = $ret
420
                   && runQuery(
421
                       sprintf(
422
                           "CREATE TABLE %s (id INT(11) NOT NULL AUTO_INCREMENT,
423
                                                           state INT(11) NOT NULL DEFAULT '0',
424
                                                           description VARCHAR(50) NOT NULL DEFAULT '',
425
                                                           PRIMARY KEY(id),
426
                                                           KEY state (state)
427
                                                          )ENGINE=MyISAM;",
428
                           $xoopsDB->prefix('xhelp_status')
429
                       ),
430
                       sprintf(_AM_XHELP_MSG_ADDTABLE, 'xhelp_status'),
431
                       sprintf(_AM_XHELP_MSG_ADDTABLE_ERR, 'xhelp_status')
432
                   );
433
434
            // Give default statuses for upgrade
435
            /** @var \XoopsModules\Xhelp\StatusHandler $statusHandler */
436
            $statusHandler = $helper->getHandler('Status');
437
            $startStatuses = [_XHELP_STATUS0 => 1, _XHELP_STATUS1 => 1, _XHELP_STATUS2 => 2];
438
439
            $count = 1;
440
            set_time_limit(60);
441
            foreach ($startStatuses as $desc => $state) {
442
                /** @var \XoopsModules\Xhelp\Status $newStatus */
443
                $newStatus = $statusHandler->create();
444
                $newStatus->setVar('id', $count);
445
                $newStatus->setVar('description', $desc);
446
                $newStatus->setVar('state', $state);
447
                if ($statusHandler->insert($newStatus)) {
448
                    echo '<li>' . sprintf(_AM_XHELP_MSG_ADD_STATUS, $desc) . '</li>';
449
                } else {
450
                    echo '<li>' . sprintf(_AM_XHELP_MSG_ADD_STATUS_ERR, $desc) . '</li>';
451
                }
452
                ++$count;
453
            }
454
455
            // Change old status values to new status values
456
            $oldStatuses = [2 => 3, 1 => 2, 0 => 1];
457
458
            foreach ($oldStatuses as $cStatus => $newStatus) {
459
                $criteria = new \Criteria('status', (string)$cStatus);
460
                $success  = $ticketHandler->updateAll('status', $newStatus, $criteria);
461
            }
462
            if ($success) {
463
                echo '<li>' . _AM_XHELP_MSG_CHANGED_STATUS . '</li>';
464
            } else {
465
                echo '<li>' . _AM_XHELP_MSG_CHANGED_STATUS_ERR . '</li>';
466
            }
467
468
            // Add xhelp_ticket_submit_emails table
469
            $ret = $ret
470
                   && runQuery(
471
                       sprintf(
472
                           "CREATE TABLE %s (ticketid INT(11) NOT NULL DEFAULT '0',
473
                                                           uid INT(11) NOT NULL DEFAULT '0',
474
                                                           email VARCHAR(100) NOT NULL DEFAULT '',
475
                                                           suppress INT(11) NOT NULL DEFAULT '0',
476
                                                           PRIMARY KEY(ticketid, email)
477
                                                          )ENGINE=MyISAM;",
478
                           $xoopsDB->prefix('xhelp_ticket_submit_emails')
479
                       ),
480
                       sprintf(_AM_XHELP_MSG_ADDTABLE, 'xhelp_ticket_submit_emails'),
481
                       sprintf(_AM_XHELP_MSG_ADDTABLE_ERR, 'xhelp_ticket_submit_emails')
482
                   );
483
484
            // Add records to xhelp_ticket_submit_emails for existing tickets
485
            $count     = $ticketHandler->getCount();
486
            $batchsize = 100;
487
488
            $criteria = new \Criteria('', '');
489
            $criteria->setLimit($batchsize);
490
            $i = 0;
491
492
            while ($i <= $count) {
493
                set_time_limit(60);
494
                $criteria->setStart($i);
495
                $tickets = $ticketHandler->getObjects($criteria);
496
497
                $all_users = [];
498
                foreach ($tickets as $ticket) {
499
                    $all_users[$ticket->getVar('uid')] = $ticket->getVar('uid');
500
                }
501
502
                $criteria = new \Criteria('uid', '(' . implode(',', array_keys($all_users)) . ')', 'IN');
503
                $users    = $memberHandler->getUsers($criteria, true);
504
505
                foreach ($users as $user) {
506
                    $all_users[$user->getVar('uid')] = $user->getVar('email');
507
                }
508
                unset($users);
509
510
                foreach ($tickets as $ticket) {
511
                    set_time_limit(60);
512
                    $ticket_uid = $ticket->getVar('uid');
513
                    if (array_key_exists($ticket_uid, $all_users)) {
514
                        $ticket_email = $all_users[$ticket_uid];
515
                        $success      = $ticket->addSubmitter($ticket_email, $ticket_uid);
516
                    }
517
                }
518
                unset($tickets);
519
                //increment
520
                $i += $batchsize;
521
            }
522
523
            set_time_limit(60);
524
            // Update xhelp_roles Admin record with new value (2047)
525
            $criteria   = new \Criteria('tasks', '511');
526
            $adminRoles = $roleHandler->getObjects($criteria);
527
528
            foreach ($adminRoles as $role) {
529
                $role->setVar('tasks', 2047);
530
                if ($roleHandler->insert($role)) {
531
                    echo '<li>' . sprintf(_AM_XHELP_MSG_UPDATE_ROLE, $role->getVar('name')) . '</li>';
532
                } else {
533
                    echo '<li>' . sprintf(_AM_XHELP_MSG_UPDATE_ROLE_ERR, $role->getVar('name')) . '</li>';
534
                }
535
            }
536
537
            set_time_limit(60);
538
            $ret = $ret
539
                   && runQuery(
540
                       sprintf(
541
                           'ALTER TABLE %s ADD (active INT(11) NOT NULL DEFAULT 1,
542
                                                          KEY ACTIVE (ACTIVE))',
543
                           $xoopsDB->prefix('xhelp_department_mailbox')
544
                       ),
545
                       sprintf(_AM_XHELP_MSG_MODIFYTABLE, 'xhelp_department_mailbox'),
546
                       sprintf(_AM_XHELP_MSG_MODIFYTABLE_ERR, 'xhelp_department_mailbox')
547
                   );
548
549
            // Add xhelp_saved_searches table
550
            $ret = $ret
551
                   && runQuery(
552
                       sprintf(
553
                           "CREATE TABLE %s (id INT(11) NOT NULL AUTO_INCREMENT,
554
                                                           uid INT(11) NOT NULL DEFAULT '0',
555
                                                           name VARCHAR(50) NOT NULL DEFAULT '',
556
                                                           search MEDIUMTEXT NOT NULL,
557
                                                           pagenav_vars MEDIUMTEXT NOT NULL,
558
                                                           PRIMARY KEY(id)
559
                                                          )ENGINE=MyISAM;",
560
                           $xoopsDB->prefix('xhelp_saved_searches')
561
                       ),
562
                       sprintf(_AM_XHELP_MSG_ADDTABLE, 'xhelp_saved_searches'),
563
                       sprintf(_AM_XHELP_MSG_ADDTABLE_ERR, 'xhelp_saved_searches')
564
                   );
565
566
            set_time_limit(60);
567
            $ret = $ret
568
                   && runQuery(
569
                       sprintf(
570
                           "CREATE TABLE %s (fieldid INT(11) NOT NULL DEFAULT '0',
571
                                                           deptid INT(11) NOT NULL DEFAULT '0',
572
                                                           PRIMARY KEY  (fieldid, deptid)
573
                                                          )ENGINE=MyISAM;",
574
                           $xoopsDB->prefix('xhelp_ticket_field_departments')
575
                       ),
576
                       sprintf(_AM_XHELP_MSG_ADDTABLE, 'xhelp_ticket_field_departments'),
577
                       sprintf(_AM_XHELP_MSG_ADDTABLE_ERR, 'xhelp_ticket_field_departments')
578
                   );
579
580
            $ret = $ret
581
                   && runQuery(
582
                       sprintf(
583
                           "CREATE TABLE %s (ticketid INT(11) NOT NULL DEFAULT '0',
584
                                                           PRIMARY KEY  (ticketid)
585
                                                          )ENGINE=MyISAM;",
586
                           $xoopsDB->prefix('xhelp_ticket_values')
587
                       ),
588
                       sprintf(_AM_XHELP_MSG_ADDTABLE, 'xhelp_ticket_values'),
589
                       sprintf(_AM_XHELP_MSG_ADDTABLE_ERR, 'xhelp_ticket_values')
590
                   );
591
592
            set_time_limit(60);
593
            $ret = $ret
594
                   && runQuery(
595
                       sprintf(
596
                           "CREATE TABLE %s (id INT(11) NOT NULL AUTO_INCREMENT,
597
                                                           NAME VARCHAR(64) NOT NULL DEFAULT '',
598
                                                           description TINYTEXT NOT NULL,
599
                                                           fieldname VARCHAR(64) NOT NULL DEFAULT '',
600
                                                           controltype INT(11) NOT NULL DEFAULT '0',
601
                                                           datatype VARCHAR(64) NOT NULL DEFAULT '',
602
                                                           required TINYINT(1) NOT NULL DEFAULT '0',
603
                                                           fieldlength INT(11) NOT NULL DEFAULT '0',
604
                                                           weight INT(11) NOT NULL DEFAULT '0',
605
                                                           fieldvalues MEDIUMTEXT NOT NULL,
606
                                                           defaultvalue VARCHAR(100) NOT NULL DEFAULT '',
607
                                                           VALIDATION MEDIUMTEXT NOT NULL,
608
                                                           PRIMARY KEY (id),
609
                                                           KEY weight (weight)
610
                                                          )ENGINE=MyISAM;",
611
                           $xoopsDB->prefix('xhelp_ticket_fields')
612
                       ),
613
                       sprintf(_AM_XHELP_MSG_ADDTABLE, 'xhelp_ticket_fields'),
614
                       sprintf(_AM_XHELP_MSG_ADDTABLE_ERR, 'xhelp_ticket_fields')
615
                   );
616
617
            set_time_limit(60);
618
            // Add notifications to new table
619
            set_time_limit(60);
620
            $hasNotifications = Xhelp\Utility::createNotifications();
621
622
            // Make all departments visible to all groups
623
            $hasDeptVisibility = xhelpCreateDepartmentVisibility();
624
625
            // Update staff permTimestamp
626
            $staffHandler->updateAll('permTimestamp', time());
627
628
            set_time_limit(60);
629
            //Update xhelp_tickets table
630
            set_time_limit(60);
631
            $ret = $ret
632
                   && runQuery(sprintf("ALTER TABLE %s MODIFY SUBJECT VARCHAR(255) NOT NULL DEFAULT ''", $xoopsDB->prefix('xhelp_tickets')), sprintf(_AM_XHELP_MSG_MODIFYTABLE, 'xhelp_tickets'), sprintf(_AM_XHELP_MSG_MODIFYTABLE_ERR, 'xhelp_tickets'));
633
634
        // no break
635
        case '0.75':
636
            set_time_limit(60);
637
            // Set default department
638
            //            $xoopsModuleConfig = Xhelp\Utility::getModuleConfig();
639
            if (null !== $helper->getConfig('xhelp_defaultDept') && 0 != $helper->getConfig('xhelp_defaultDept')) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $helper->getConfig('xhelp_defaultDept') of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
640
                $ret = Xhelp\Utility::setMeta('default_department', $helper->getConfig('xhelp_defaultDept'));
0 ignored issues
show
It seems like $helper->getConfig('xhelp_defaultDept') can also be of type null; however, parameter $value of XoopsModules\Xhelp\Utility::setMeta() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

640
                $ret = Xhelp\Utility::setMeta('default_department', /** @scrutinizer ignore-type */ $helper->getConfig('xhelp_defaultDept'));
Loading history...
641
            } else {
642
                /** @var \XoopsModules\Xhelp\DepartmentHandler $departmentHandler */
643
                $departmentHandler = $helper->getHandler('Department');
644
                $depts             = $departmentHandler->getObjects();
645
                $aDepts            = [];
646
                foreach ($depts as $dpt) {
647
                    $aDepts[] = $dpt->getVar('id');
648
                }
649
                $ret = Xhelp\Utility::setMeta('default_department', $aDepts[0]);
650
            }
651
652
            $qry = $xoopsDB->query(sprintf('ALTER TABLE %s DROP PRIMARY KEY', $xoopsDB->prefix('xhelp_ticket_submit_emails')));
653
            $ret = $ret
654
                   && runQuery(sprintf('ALTER TABLE %s ADD PRIMARY KEY(ticketid, uid, email)', $xoopsDB->prefix('xhelp_ticket_submit_emails')), sprintf(_AM_XHELP_MSG_MODIFYTABLE, 'xhelp_ticket_submit_emails'), sprintf(_AM_XHELP_MSG_MODIFYTABLE_ERR, 'xhelp_ticket_submit_emails'));
655
656
            $ret = $ret
657
                   && runQuery(sprintf("ALTER TABLE %s MODIFY department INT(11) NOT NULL DEFAULT '0'", $xoopsDB->prefix('xhelp_jstaffdept')), sprintf(_AM_XHELP_MSG_MODIFYTABLE, 'xhelp_jstaffdept'), sprintf(_AM_XHELP_MSG_MODIFYTABLE_ERR, 'xhelp_jstaffdept'));
658
659
            echo '<li>' . _AM_XHELP_MSG_CHANGED_DEFAULT_DEPT . '</li>';
660
661
            // Add field to xhelp_saved_searches to determine if custom fields table is needed
662
            $ret = $ret
663
                   && runQuery(sprintf("ALTER TABLE %s ADD (hasCustFields INT(11) NOT NULL DEFAULT '0')", $xoopsDB->prefix('xhelp_saved_searches')), sprintf(_AM_XHELP_MSG_MODIFYTABLE, 'xhelp_saved_searches'), sprintf(_AM_XHELP_MSG_MODIFYTABLE_ERR, 'xhelp_saved_searches'));
664
665
            // Take existing saved searches and add 'query' field
666
            /** @var \XoopsModules\Xhelp\SavedSearchHandler $savedSearchHandler */
667
            $savedSearchHandler = $helper->getHandler('SavedSearch');
668
            $savedSearches      = $savedSearchHandler->getObjects();
669
670
            foreach ($savedSearches as $savedSearch) {
671
                set_time_limit(60);
672
                $criteria = unserialize($savedSearch->getVar('search'));
673
                if (is_object($criteria)) {
674
                    $savedSearch->setVar('query', $criteria->render());
675
676
                    if ($savedSearchHandler->insert($savedSearch)) {
677
                        echo '<li>' . sprintf(_AM_XHELP_MSG_UPDATE_SEARCH, $savedSearch->getVar('id')) . '</li>';
678
                    } else {
679
                        echo '<li>' . sprintf(_AM_XHELP_MSG_UPDATE_SEARCH_ERR, $savedSearch->getVar('id')) . '</li>';
680
                    }
681
                }
682
            }
683
            unset($savedSearches);
684
685
            // Add ticket list table
686
            set_time_limit(60);
687
            $ret = $ret
688
                   && runQuery(
689
                       sprintf(
690
                           "CREATE TABLE %s (id INT(11) NOT NULL AUTO_INCREMENT,
691
                                                           uid INT(11) NOT NULL DEFAULT '0',
692
                                                           searchid INT(11) NOT NULL DEFAULT '0',
693
                                                           weight INT(11) NOT NULL DEFAULT '0',
694
                                                           PRIMARY KEY (id),
695
                                                           KEY ticketList (uid, searchid)
696
                                                          )ENGINE=MyISAM;",
697
                           $xoopsDB->prefix('xhelp_ticket_lists')
698
                       ),
699
                       sprintf(_AM_XHELP_MSG_ADDTABLE, 'xhelp_ticket_lists'),
700
                       sprintf(_AM_XHELP_MSG_ADDTABLE_ERR, 'xhelp_ticket_lists')
701
                   );
702
703
            // Add global ticket lists for staff members
704
            Xhelp\Utility::createDefaultTicketLists();
705
706
            set_time_limit(60);
707
            // Update xhelp_roles Admin record with new value (4095)
708
            $criteria   = new \Criteria('tasks', '2047');
709
            $adminRoles = $roleHandler->getObjects($criteria);
710
711
            foreach ($adminRoles as $role) {
712
                $role->setVar('tasks', 4095);
713
                if ($roleHandler->insert($role)) {
714
                    echo '<li>' . sprintf(_AM_XHELP_MSG_UPDATE_ROLE, $role->getVar('name')) . '</li>';
715
                } else {
716
                    echo '<li>' . sprintf(_AM_XHELP_MSG_UPDATE_ROLE_ERR, $role->getVar('name')) . '</li>';
717
                }
718
            }
719
720
        // no break
721
        case '0.77':
722
            // No schema changes for 0.78
723
724
        case '0.78':
725
            echo '</ul>';
726
            break;
727
        default:
728
            throw new \RuntimeException('Unexpected value');
729
    }
730
731
    $newversion = round($xoopsModule->getVar('version') / 100, 2);
732
    //if successful, update xhelp_meta table with new ver
733
    if ($ret) {
734
        printf(_AM_XHELP_UPDATE_OK, $newversion);
735
        $ret = Xhelp\Utility::setMeta('version', (string)$newversion);
736
    } else {
737
        printf(_AM_XHELP_UPDATE_ERR, $newversion);
738
    }
739
740
    require_once __DIR__ . '/admin_footer.php';
741
}
742
743
if ('upgradeDB' === $op) {
744
    echo "<script language='JavaScript' type='text/javascript'>
745
window.onload=function() {
746
    var objWindow=window.open('about:blank', 'progress', '');
747
    objWindow.close();
748
}
749
</script>";
750
}
751