Skip to content

Commit 9361ee0

Browse files
authored
Merge pull request #948 from pupi1985/patch-153
Fix many string management functions in PHP 8.1
2 parents 2cf739b + 3bdef70 commit 9361ee0

67 files changed

Lines changed: 6985 additions & 225 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

qa-include/Q2A/Plugin/PluginManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public function readAllPluginMetadatas()
4949
}
5050

5151
// skip plugin which requires a later version of Q2A
52-
if (isset($metadata['min_q2a']) && qa_qa_version_below($metadata['min_q2a'])) {
52+
if (qa_qa_version_below($metadata['min_q2a'] ?? '')) {
5353
continue;
5454
}
5555
// skip plugin which requires a later version of PHP
56-
if (isset($metadata['min_php']) && qa_php_version_below($metadata['min_php'])) {
56+
if (qa_php_version_below($metadata['min_php'] ?? '')) {
5757
continue;
5858
}
5959

qa-include/ajax/asktitle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
// Collect the information we need from the database
2929

30-
$intitle = qa_post_text('title');
30+
$intitle = (string)qa_post_text('title');
3131
$doaskcheck = qa_opt('do_ask_check_qs');
3232
$doexampletags = qa_using_tags() && qa_opt('do_example_tags');
3333

qa-include/ajax/category.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
$categoryid = qa_post_text('categoryid');
26-
if (!strlen($categoryid))
26+
if (!strlen((string)$categoryid))
2727
$categoryid = null;
2828

2929
list($fullcategory, $categories) = qa_db_select_with_pending(
@@ -33,7 +33,7 @@
3333

3434
echo "QA_AJAX_RESPONSE\n1\n";
3535

36-
echo qa_html(strtr(@$fullcategory['content'], "\r\n", ' ')); // category description
36+
echo qa_html(strtr($fullcategory['content'] ?? '', "\r\n", ' ')); // category description
3737

3838
foreach ($categories as $category) {
3939
// subcategory information

qa-include/ajax/version.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,22 @@
5050
$metadataUtil = new Q2A_Util_Metadata();
5151
$metadata = $metadataUtil->fetchFromUrl($uri);
5252

53-
if (strlen(@$metadata['version']) > 0) {
53+
if (strlen($metadata['version'] ?? '') > 0) {
5454
if (version_compare($currentVersion, $metadata['version']) < 0) {
55-
if (qa_qa_version_below(@$metadata['min_q2a'])) {
55+
if (qa_qa_version_below($metadata['min_q2a'] ?? '')) {
5656
$response = strtr(qa_lang_html('admin/version_requires_q2a'), array(
5757
'^1' => qa_html('v' . $metadata['version']),
5858
'^2' => qa_html($metadata['min_q2a']),
5959
));
60-
} elseif (qa_php_version_below(@$metadata['min_php'])) {
60+
} elseif (qa_php_version_below($metadata['min_php'] ?? '')) {
6161
$response = strtr(qa_lang_html('admin/version_requires_php'), array(
6262
'^1' => qa_html('v' . $metadata['version']),
6363
'^2' => qa_html($metadata['min_php']),
6464
));
6565
} else {
6666
$response = qa_lang_html_sub('admin/version_get_x', qa_html('v' . $metadata['version']));
6767

68-
if (strlen(@$metadata['uri'])) {
68+
if (strlen($metadata['uri'] ?? '')) {
6969
$response = '<a href="' . qa_html($metadata['uri']) . '" style="color:#d00;">' . $response . '</a>';
7070
}
7171
}

qa-include/ajax/wallpost.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
$errorhtml = qa_wall_error_html($loginuserid, $touseraccount['userid'], $touseraccount['flags']);
3636

37-
if ($errorhtml || !strlen($message) || !qa_check_form_security_code('wall-' . $tohandle, qa_post_text('code'))) {
37+
if ($errorhtml || !strlen((string)$message) || !qa_check_form_security_code('wall-' . $tohandle, qa_post_text('code'))) {
3838
echo "QA_AJAX_RESPONSE\n0"; // if there's an error, process in non-Ajax way
3939
} else {
4040
$messageid = qa_wall_add_post($loginuserid, qa_get_logged_in_handle(), qa_cookie_get(),

qa-include/app/blobs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function qa_write_blob_file($blobid, $content, $format)
114114

115115
$file = fopen($filename, 'xb');
116116
if (is_resource($file)) {
117-
if (fwrite($file, $content) >= strlen($content))
117+
if (fwrite($file, $content) >= strlen((string)$content))
118118
$written = true;
119119

120120
fclose($file);

qa-include/app/emails.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ function qa_send_email($params)
133133

134134
// @error_log(print_r($params, true));
135135

136-
require_once QA_INCLUDE_DIR . 'vendor/PHPMailer/PHPMailerAutoload.php';
136+
require_once QA_INCLUDE_DIR . 'vendor/PHPMailer6/PHPMailer.php';
137+
require_once QA_INCLUDE_DIR . 'vendor/PHPMailer6/Exception.php';
137138

138-
PHPMailer::$validator = 'php';
139-
$mailer = new PHPMailer();
139+
\PHPMailer\PHPMailer\PHPMailer::$validator = 'php';
140+
$mailer = new \PHPMailer\PHPMailer\PHPMailer();
140141
$mailer->CharSet = 'utf-8';
141142

142143
$mailer->From = $params['fromemail'];
@@ -153,6 +154,8 @@ function qa_send_email($params)
153154
$mailer->isHTML(true);
154155

155156
if (qa_opt('smtp_active')) {
157+
require_once QA_INCLUDE_DIR . 'vendor/PHPMailer6/SMTP.php';
158+
156159
$mailer->isSMTP();
157160
$mailer->Host = qa_opt('smtp_address');
158161
$mailer->Port = qa_opt('smtp_port');

qa-include/app/favorites.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ function qa_favorite_categories_view($categories)
197197
$cat_anchor = $category['qcount'] == 1
198198
? qa_lang_html_sub('main/1_question', '1', '1')
199199
: qa_lang_html_sub('main/x_questions', qa_format_number($category['qcount'], 0, true));
200-
$cat_descr = strlen($category['content']) ? qa_html(' - ' . $category['content']) : '';
200+
$cat_descr = strlen((string)$category['content']) ? qa_html(' - ' . $category['content']) : '';
201201

202202
$nav_list_categories['nav'][$category['categoryid']] = array(
203203
'label' => qa_html($category['title']),

qa-include/app/format.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function qa_post_is_by_user($post, $userid, $cookieid)
8383
if (@$post['userid'] || $userid)
8484
return @$post['userid'] == $userid;
8585
elseif (@$post['cookieid'])
86-
return strcmp($post['cookieid'], $cookieid) == 0;
86+
return $post['cookieid'] === $cookieid;
8787

8888
return false;
8989
}
@@ -209,6 +209,10 @@ function qa_tag_html($tag, $microdata = false, $favorited = false)
209209
*/
210210
function qa_category_path($navcategories, $categoryid)
211211
{
212+
if ($categoryid === null) {
213+
return array();
214+
}
215+
212216
$upcategories = array();
213217

214218
for ($upcategory = @$navcategories[$categoryid]; isset($upcategory); $upcategory = @$navcategories[$upcategory['parentid']])
@@ -266,7 +270,7 @@ function qa_ip_anchor_html($ip, $anchorhtml = null)
266270
{
267271
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
268272

269-
if (!strlen($anchorhtml))
273+
if (!strlen((string)$anchorhtml))
270274
$anchorhtml = qa_html($ip);
271275

272276
return '<a href="' . qa_path_html('ip/' . $ip) . '" title="' . qa_lang_html_sub('main/ip_address_x', qa_html($ip)) . '" class="qa-ip-link">' . $anchorhtml . '</a>';
@@ -432,7 +436,7 @@ function qa_post_html_fields($post, $userid, $cookieid, $usershtml, $dummy, $opt
432436
// Voting stuff
433437

434438
if (@$options['voteview']) {
435-
$voteview = $options['voteview'];
439+
$voteview = (string)$options['voteview'];
436440

437441
// Calculate raw values and pass through
438442

@@ -573,7 +577,7 @@ function qa_post_html_fields($post, $userid, $cookieid, $usershtml, $dummy, $opt
573577
if (@$options['whatview']) {
574578
$fields['what'] = qa_lang_html($isquestion ? 'main/asked' : ($isanswer ? 'main/answered' : 'main/commented'));
575579

576-
if (@$options['whatlink'] && strlen(@$options['q_request'])) {
580+
if (@$options['whatlink'] && strlen($options['q_request'] ?? '')) {
577581
$fields['what_url'] = $post['basetype'] == 'Q'
578582
? qa_path_html($options['q_request'])
579583
: qa_path_html($options['q_request'], array('show' => $postid), null, null, qa_anchor($post['basetype'], $postid));
@@ -754,7 +758,7 @@ function qa_who_to_html($isbyuser, $postuserid, $usershtml, $ip = null, $microda
754758
if (isset($postuserid) && isset($usershtml[$postuserid])) {
755759
$whohtml = $usershtml[$postuserid];
756760
} else {
757-
if (strlen($name))
761+
if (strlen((string)$name))
758762
$whohtml = qa_html($name);
759763
elseif ($isbyuser)
760764
$whohtml = qa_lang_html('main/me');
@@ -1293,7 +1297,7 @@ function qa_html_suggest_qs_tags($usingtags = false, $categoryrequest = null)
12931297
{
12941298
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
12951299

1296-
$hascategory = strlen($categoryrequest);
1300+
$hascategory = strlen((string)$categoryrequest);
12971301

12981302
$htmlmessage = $hascategory ? qa_lang_html('main/suggest_category_qs') :
12991303
($usingtags ? qa_lang_html('main/suggest_qs_tags') : qa_lang_html('main/suggest_qs'));
@@ -1326,7 +1330,7 @@ function qa_html_suggest_ask($categoryid = null)
13261330
$htmlmessage,
13271331

13281332
array(
1329-
'^1' => '<a href="' . qa_path_html('ask', strlen($categoryid) ? array('cat' => $categoryid) : null) . '">',
1333+
'^1' => '<a href="' . qa_path_html('ask', strlen((string)$categoryid) ? array('cat' => $categoryid) : null) . '">',
13301334
'^2' => '</a>',
13311335
)
13321336
);
@@ -1723,7 +1727,7 @@ function qa_get_tags_field_value($fieldname)
17231727
{
17241728
require_once QA_INCLUDE_DIR . 'util/string.php';
17251729

1726-
$text = qa_remove_utf8mb4(qa_post_text($fieldname));
1730+
$text = qa_remove_utf8mb4((string)qa_post_text($fieldname));
17271731

17281732
if (qa_opt('tag_separator_comma'))
17291733
return array_unique(preg_split('/\s*,\s*/', trim(qa_strtolower(strtr($text, '/', ' '))), -1, PREG_SPLIT_NO_EMPTY));
@@ -1796,21 +1800,21 @@ function qa_set_up_category_field(&$qa_content, &$field, $fieldname, $navcategor
17961800
$depth++; // to count category itself
17971801

17981802
foreach ($navcategories as $navcategory) // now get siblings and self
1799-
if (!strcmp($navcategory['parentid'], $category['parentid']))
1803+
if (!strcmp($navcategory['parentid'] ?? '', $category['parentid'] ?? ''))
18001804
$keycategoryids[$navcategory['categoryid']] = true;
18011805
}
18021806

18031807
if ($depth < $maxdepth)
18041808
foreach ($navcategories as $navcategory) // now get children, if not too deep
1805-
if (!strcmp($navcategory['parentid'], $categoryid))
1809+
if (!strcmp($navcategory['parentid'] ?? '', $categoryid ?? ''))
18061810
$keycategoryids[$navcategory['categoryid']] = true;
18071811

18081812
} else {
18091813
$haschildren = false;
18101814

18111815
foreach ($navcategories as $navcategory) {
18121816
// check if it has any children
1813-
if (!strcmp($navcategory['parentid'], $categoryid)) {
1817+
if (!strcmp($navcategory['parentid'] ?? '', $categoryid ?? '')) {
18141818
$haschildren = true;
18151819
break;
18161820
}
@@ -1821,7 +1825,7 @@ function qa_set_up_category_field(&$qa_content, &$field, $fieldname, $navcategor
18211825
}
18221826

18231827
foreach ($keycategoryids as $keycategoryid => $dummy)
1824-
if (strcmp($keycategoryid, $excludecategoryid))
1828+
if (strcmp($keycategoryid, $excludecategoryid ?? ''))
18251829
$field['options'][$keycategoryid] = qa_category_path_html($navcategories, $keycategoryid);
18261830

18271831
$field['value'] = @$field['options'][$categoryid];
@@ -1841,13 +1845,13 @@ function qa_get_category_field_value($fieldname)
18411845
{
18421846
for ($level = QA_CATEGORY_DEPTH; $level >= 1; $level--) {
18431847
$levelid = qa_post_text($fieldname . '_' . $level);
1844-
if (strlen($levelid))
1848+
if (strlen((string)$levelid))
18451849
return $levelid;
18461850
}
18471851

18481852
if (!isset($levelid)) { // no Javascript-generated menu was present so take original menu
18491853
$levelid = qa_post_text($fieldname . '_0');
1850-
if (strlen($levelid))
1854+
if (strlen((string)$levelid))
18511855
return $levelid;
18521856
}
18531857

@@ -2008,7 +2012,7 @@ function qa_load_theme_class($theme, $template, $content, $request)
20082012

20092013
foreach ($loadlayers as $layer) {
20102014
$filename = $layer['directory'] . $layer['include'];
2011-
$layerphp = file_get_contents($filename);
2015+
$layerphp = (string)file_get_contents($filename);
20122016

20132017
if (strlen($layerphp)) {
20142018
// include file name in layer class name to make debugging easier if there is an error
@@ -2185,7 +2189,7 @@ function qa_get_post_title($fieldname)
21852189
{
21862190
require_once QA_INCLUDE_DIR . 'util/string.php';
21872191

2188-
return qa_remove_utf8mb4(qa_post_text($fieldname));
2192+
return qa_remove_utf8mb4((string)qa_post_text($fieldname));
21892193
}
21902194

21912195
/**
@@ -2247,7 +2251,7 @@ function qa_get_avatar_blob_html($blobId, $width, $height, $size, $padding = fal
22472251
require_once QA_INCLUDE_DIR . 'util/image.php';
22482252
require_once QA_INCLUDE_DIR . 'app/users.php';
22492253

2250-
if (strlen($blobId) == 0 || (int)$size <= 0) {
2254+
if (strlen((string)$blobId) == 0 || (int)$size <= 0) {
22512255
return null;
22522256
}
22532257

qa-include/app/limits.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,14 @@ function qa_is_ip_blocked()
151151
if (isset($qa_curr_ip_blocked))
152152
return $qa_curr_ip_blocked;
153153

154-
$qa_curr_ip_blocked = false;
155-
$blockipclauses = qa_block_ips_explode(qa_opt('block_ips_write'));
156154
$ip = qa_remote_ip_address();
157155

156+
$qa_curr_ip_blocked = false;
157+
if ($ip === null) {
158+
return false;
159+
}
160+
161+
$blockipclauses = qa_block_ips_explode(qa_opt('block_ips_write'));
158162
foreach ($blockipclauses as $blockipclause) {
159163
if (qa_block_ip_match($ip, $blockipclause)) {
160164
$qa_curr_ip_blocked = true;
@@ -172,7 +176,7 @@ function qa_is_ip_blocked()
172176
*/
173177
function qa_block_ips_explode($blockipstring)
174178
{
175-
$blockipstring = preg_replace('/\s*\-\s*/', '-', $blockipstring); // special case for 'x.x.x.x - x.x.x.x'
179+
$blockipstring = preg_replace('/\s*\-\s*/', '-', (string)$blockipstring); // special case for 'x.x.x.x - x.x.x.x'
176180

177181
return preg_split('/[^0-9A-Fa-f\.:\-\*]/', $blockipstring, -1, PREG_SPLIT_NO_EMPTY);
178182
}
@@ -248,7 +252,6 @@ function qa_ip_between($ip, $startip, $endip)
248252
function qa_ipv6_expand($ip)
249253
{
250254
$ipv6_wildcard = false;
251-
$wildcards = '';
252255
$wildcards_matched = array();
253256
if (strpos($ip, "*") !== false) {
254257
$ipv6_wildcard = true;

0 commit comments

Comments
 (0)