chess_moduleConfig()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 3
nop 1
dl 0
loc 25
rs 9.4888
c 0
b 0
f 0
1
<?php
2
3
// ------------------------------------------------------------------------- //
4
//  This program is free software; you can redistribute it and/or modify     //
5
//  it under the terms of the GNU General Public License as published by     //
6
//  the Free Software Foundation; either version 2 of the License, or        //
7
//  (at your option) any later version.                                      //
8
//                                                                           //
9
//  You may not change or alter any portion of this comment or credits       //
10
//  of supporting developers from this source code or any supporting         //
11
//  source code which is considered copyrighted (c) material of the          //
12
//  original comment or credit authors.                                      //
13
//                                                                           //
14
//  This program is distributed in the hope that it will be useful,          //
15
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
16
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
17
//  GNU General Public License for more details.                             //
18
//                                                                           //
19
//  You should have received a copy of the GNU General Public License        //
20
//  along with this program; if not, write to the Free Software              //
21
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
22
//  ------------------------------------------------------------------------ //
23
24
/**
25
 * General functions.
26
 *
27
 * @param mixed $text
28
 * @param mixed $allowed_characters
29
 * @package    chess
30
 * @subpackage functions
31
 */
32
33
/**
34
 * Remove unsafe characters from text.
35
 *
36
 * @param string $text               Text to be sanitized
37
 * @param string $allowed_characters Characters permitted in text;
38
 *                                   must not contain any regex symbols such as \w or \s, since preg_quote() mishandles those.  But \' and \" are ok.
39
 * @return string  Sanitized text
40
 */
41
function chess_sanitize($text, $allowed_characters = 'A-Za-z0-9')
42
{
43
    $char_class = preg_replace('/(\.\\\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:\#)/', '\\${1}', $allowed_characters);
44
    //$char_class = preg_quote($allowed_characters, '/');
45
    return preg_replace("/[^$char_class]/i", '_', $text);
46
}
47
48
/**
49
 * Get chess module configuration option value.
50
 *
51
 * @param string $option Name of configuration option
52
 * @return string  Value of configuration option
53
 */
54
function chess_moduleConfig($option)
55
{
56
    global $xoopsModule, $xoopsModuleConfig;
57
58
    $value = null;
59
60
    if (is_object($xoopsModule) && 'chess' == $xoopsModule->getVar('dirname') && isset($xoopsModuleConfig[$option])) {
61
        $value = $xoopsModuleConfig[$option];
62
    } else { // for use within a block
63
        $moduleHandler = xoops_getHandler('module');
64
65
        $module = $moduleHandler->getByDirname('chess');
0 ignored issues
show
Bug introduced by
The method getByDirname() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsModuleHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

65
        /** @scrutinizer ignore-call */ 
66
        $module = $moduleHandler->getByDirname('chess');
Loading history...
66
67
        $configHandler = xoops_getHandler('config');
68
69
        $moduleConfig = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
0 ignored issues
show
Bug introduced by
The method getConfigsByCat() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

69
        /** @scrutinizer ignore-call */ 
70
        $moduleConfig = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
Loading history...
70
71
        if (isset($moduleConfig[$option])) {
72
            $value = $moduleConfig[$option];
73
        } else {
74
            trigger_error("configuration option '$option' not found", E_USER_ERROR);
75
        }
76
    }
77
78
    return $value;
79
}
80
81
/**
82
 * Check whether a user has the play-chess right.
83
 *
84
 * @param int $uid User ID to check, defaults to current user
85
 * @return bool  True if user has play-chess right, otherwise false
86
 */
87
function chess_can_play($uid = null)
88
{
89
    global $xoopsUser;
90
91
    if (isset($uid)) {
92
        $memberHandler = xoops_getHandler('member');
93
94
        $user = $memberHandler->getUser($uid);
0 ignored issues
show
Bug introduced by
The method getUser() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsAvatarHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

94
        /** @scrutinizer ignore-call */ 
95
        $user = $memberHandler->getUser($uid);
Loading history...
95
    } elseif (is_object($xoopsUser)) {
96
        $user = $xoopsUser;
97
    } else {
98
        $user = null;
99
    }
100
101
    $groups_play = chess_moduleConfig('groups_play');
102
103
    $can_play = false;
104
105
    if (in_array(XOOPS_GROUP_ANONYMOUS, $groups_play)) {
0 ignored issues
show
Bug introduced by
$groups_play of type string is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

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

105
    if (in_array(XOOPS_GROUP_ANONYMOUS, /** @scrutinizer ignore-type */ $groups_play)) {
Loading history...
106
        $can_play = true;
107
    } elseif (is_object($user)) {
108
        $can_play = count(array_intersect($user->getGroups(), $groups_play)) > 0;
0 ignored issues
show
Bug introduced by
$groups_play of type string is incompatible with the type array expected by parameter $array2 of array_intersect(). ( Ignorable by Annotation )

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

108
        $can_play = count(array_intersect($user->getGroups(), /** @scrutinizer ignore-type */ $groups_play)) > 0;
Loading history...
109
    }
110
111
    return $can_play;
112
}
113
114
/**
115
 * Check whether a user has the delete-game right.
116
 *
117
 * @param int $uid User ID to check, defaults to current user
118
 * @return bool  True if user has delete-game right, otherwise false
119
 */
120
function chess_can_delete($uid = null)
121
{
122
    global $xoopsUser;
123
124
    if (isset($uid)) {
125
        $memberHandler = xoops_getHandler('member');
126
127
        $user = $memberHandler->getUser($uid);
128
    } elseif (is_object($xoopsUser)) {
129
        $user = $xoopsUser;
130
    } else {
131
        $user = null;
132
    }
133
134
    $groups_delete = chess_moduleConfig('groups_delete');
135
136
    $can_delete = false;
137
138
    if (in_array(XOOPS_GROUP_ANONYMOUS, $groups_delete)) {
0 ignored issues
show
Bug introduced by
$groups_delete of type string is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

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

138
    if (in_array(XOOPS_GROUP_ANONYMOUS, /** @scrutinizer ignore-type */ $groups_delete)) {
Loading history...
139
        $can_delete = true;
140
    } elseif (is_object($user)) {
141
        $can_delete = count(array_intersect($user->getGroups(), $groups_delete)) > 0;
0 ignored issues
show
Bug introduced by
$groups_delete of type string is incompatible with the type array expected by parameter $array2 of array_intersect(). ( Ignorable by Annotation )

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

141
        $can_delete = count(array_intersect($user->getGroups(), /** @scrutinizer ignore-type */ $groups_delete)) > 0;
Loading history...
142
    }
143
144
    return $can_delete;
145
}
146
147
/**
148
 * Build string suitable for output as .pgn (Portable Game Notation) file.
149
 *
150
 * @param array $data Array with keys:
151
 *
152
 *  - 'datetime' - 'YYYY-MM-DD HH:MM:SS'
153
 *       Use '?' for each unknown digit.  If the entire datetime is unknown, use either '????-??-?? ??:??:??' or '0000-00-00 00:00:00'.
154
 *  - 'event', 'site', 'round', 'white', 'black', 'result', 'setup', 'fen', 'movetext' - strings (use '?' for entire string if value unknown)
155
 * @return string
156
 */
157
function chess_to_pgn_string($data)
158
{
159
    #var_dump('chess_to_pgn_string, data=', $data);#*#DEBUG#
160
161
    if ('0000-00-00 00:00:00' == $data['datetime']) {
162
        $datetime = '????.??.?? ??:??:??';
163
    } else {
164
        $datetime = str_replace('-', '.', $data['datetime']);
165
    }
166
167
    [$date, $time] = explode(' ', $datetime);
168
169
    $movetext = wordwrap($data['movetext'], 75);
170
171
    $rtn = <<<END
172
[Event "{$data['event']}"]
173
[Site "{$data['site']}"]
174
[Date "$date"]
175
[Time "$time"]
176
[Round "{$data['round']}"]
177
[White "{$data['white']}"]
178
[Black "{$data['black']}"]
179
[Result "{$data['result']}"]
180
181
END;
182
183
    if ($data['setup'] && $data['fen']) {
184
        $rtn .= "[Setup \"1\"]\n[FEN \"{$data['fen']}\"]\n";
185
    }
186
187
    $rtn .= "\n$movetext\n";
188
189
    return $rtn;
190
}
191
192
/**
193
 * Get user ID corresponding to the specified username.
194
 *
195
 * @param string $uname Username (unsanitized)
196
 * @return int  User ID, or '0' if user not found
197
 */
198
function chess_uname_to_uid($uname)
199
{
200
    $criteria = new \CriteriaCompo();
201
202
    $criteria->add(new \Criteria('uname', MyTextSanitizer::addSlashes($uname)));
0 ignored issues
show
Bug Best Practice introduced by
The method MyTextSanitizer::addSlashes() is not static, but was called statically. ( Ignorable by Annotation )

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

202
    $criteria->add(new \Criteria('uname', MyTextSanitizer::/** @scrutinizer ignore-call */ addSlashes($uname)));
Loading history...
203
204
    $criteria->setLimit(1);
205
206
    $memberHandler = xoops_getHandler('member');
207
208
    $users = $memberHandler->getUserList($criteria);
0 ignored issues
show
Bug introduced by
The method getUserList() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

208
    /** @scrutinizer ignore-call */ 
209
    $users = $memberHandler->getUserList($criteria);
Loading history...
209
210
    $uids = array_keys($users);
211
212
    return $uids[0] ?? 0;
213
}
214
215
/*** #*#DEBUG# testing something
216
 * SELECT
217
 * g.game_id,g.white_uid AS white_uid, g.black_uid AS black_uid, g.pgn_result AS pgn_result, w.rating AS white_rating, b.rating AS black_rating,
218
 * (w.games_won+w.games_lost+w.games_drawn) AS white_games, (b.games_won+b.games_lost+b.games_drawn) AS black_games
219
 * FROM       xoops_chess_games AS g
220
 * LEFT JOIN xoops_chess_ratings AS w ON w.player_uid = g.white_uid
221
 * LEFT JOIN xoops_chess_ratings AS b ON b.player_uid = g.black_uid
222
 * WHERE       g.pgn_result != '*' AND g.is_rated='1' AND (w.player_uid IS NULL OR b.player_uid IS NULL OR w.player_uid != b.player_uid) LIMIT 0, 30
223
 ***/
224