ElkArte Community

Elk Development => Bug Reports => Exterminated Bugs => Topic started by: Adrek on April 22, 2017, 06:09:25 pm

Title: [1.0.10] Total karma mode broken on PHP 7.0/7.1
Post by: Adrek on April 22, 2017, 06:09:25 pm
On PHP 7.1 with enabled total karma user profile in post show only negative karma points.

PHP 7.0 has the same issue but error log is empty. In both cases Karma label set in settings is not displayed in topic view.
Karma count and label on action?profile is correct.

With enabled karma positive/negative label is visible in topic view, so maybe it's only related to Total Karma mode.

Code: [Select]
This version: ElkArte 1.0.10 (more detailed)
Current version: 1.0.10
GD version: bundled (2.1.0 compatible)
MySQL version: 10.1.21-MariaDB-cll-lve
Zend OPcache: 7.1.3
PHP: 7.1.3 (litespeed) (more detailed)
Server version: LiteSpeed
Title: Re: [1.0.10] Total karma mode broken on PHP 7.0/7.1
Post by: Spuds on April 23, 2017, 10:33:05 am
I just installed 7.1 on one of my sites so will be able to check soon
Title: Re: [1.0.10] Total karma mode broken on PHP 7.0/7.1
Post by: Adrek on April 23, 2017, 10:59:12 am
maybe it was even before 1.0.10, but only yesterday I moved my sites to new host where I can choose PHP versions
Title: Re: [1.0.10] Total karma mode broken on PHP 7.0/7.1
Post by: Spuds on April 24, 2017, 09:39:00 am
Well this was not what I thought it would be. 

I thought this would be some cast problem, where we passing a string or null value and expected PHP to auto cast when the "math" was done.  But after ensuring that ints were being passed to the template the error persisted.  In doing some testing, its actually a template error, at least in php 7.x

In GenericMessages.template.php
Code: (find) [Select]
									<li class="listlevel2 karma">' . $modSettings['karmaLabel'] . ' ' . $message['member']['karma']['good'] - $message['member']['karma']['bad'];

Code: (replace) [Select]
									<li class="listlevel2 karma">' . $modSettings['karmaLabel'] . ' ' . ($message['member']['karma']['good'] - $message['member']['karma']['bad']);
Wrapping the match in ()'s seems to fix the problem.  Looks like php7 has done some optimization on echo concatenation, as such by the time it gets to the minus sign, its using the entire left side which is a concatenated string, so it attempts to eval "Karma 2 - 1" or some such thing.  Anyway the ()'s should ensure the correct order of things, so give it a try.
Title: Re: [1.0.10] Total karma mode broken on PHP 7.0/7.1
Post by: Frenzie on April 24, 2017, 01:45:56 pm
Regardless of how PHP treats it, I think it's much clearer what's going on in the replacement. :)
Title: Re: [1.0.10] Total karma mode broken on PHP 7.0/7.1
Post by: Adrek on April 24, 2017, 03:46:24 pm
seems to work good now :)

In find/replace code is a typo, so if anyone will copy/paste code it can break template, corrected replacement:
Code: (Find) [Select]
<li class="listlevel2 karma">' . $modSettings['karmaLabel'] . ' ' . $message['member']['karma']['good'] - $message['member']['karma']['bad'] . '</li>';
Code: (Replace with) [Select]
<li class="listlevel2 karma">' . $modSettings['karmaLabel'] . ' ' . ($message['member']['karma']['good'] - $message['member']['karma']['bad']) . '</li>';
Title: Re: [1.0.10] Total karma mode broken on PHP 7.0/7.1
Post by: emanuele on April 24, 2017, 03:47:48 pm
/me thinks we should do an str_replace. :P

Code: [Select]
									<li class="listlevel2 karma">' . str_replace('{karma_total}', ($message['member']['karma']['good'] - $message['member']['karma']['bad']), $modSettings['karmaLabel']);
Title: Re: [1.0.10] Total karma mode broken on PHP 7.0/7.1
Post by: Adrek on April 24, 2017, 03:48:47 pm
@emanuele - also missing closing </li> :)
Title: Re: [1.0.10] Total karma mode broken on PHP 7.0/7.1
Post by: live627 on April 24, 2017, 04:05:54 pm
Quote from: emanuele – /me thinks we should do an str_replace. :P

Code: [Select]
									<li class="listlevel2 karma">' . str_replace('{karma_total}', ($message['member']['karma']['good'] - $message['member']['karma']['bad']), $modSettings['karmaLabel']);
for i18n?
Title: Re: [1.0.10] Total karma mode broken on PHP 7.0/7.1
Post by: emanuele on May 07, 2017, 08:02:28 am
Yep, I think it may make sense.
But it may also be overkill, who knows. xD

Is that a 1.1 bug as well?
Title: Re: [1.0.10] Total karma mode broken on PHP 7.0/7.1
Post by: radu81 on May 07, 2017, 01:34:57 pm
sorry for the off-topic but with Likes enabled do we really need the Karma function? I have to admit I never used the Karma system
Title: Re: [1.0.10] Total karma mode broken on PHP 7.0/7.1
Post by: emanuele on May 07, 2017, 02:21:56 pm
Karma and likes are two different things: one targets the user, one the content.
I never used it myself, but considering the amount of code involved it doesn't really make much difference to have it or not in terms of maintenance.