strings get randomly truncated in PHP - php

I have an issue with a PHP script I'm developing. Sometimes (very rarely, but it occurs) strings don't get concatenated correctly, as fragments of the string get lost during the process, resulting in randomly truncated strings.
This is an example, it's part of the code that gathers the order info and sends it via email to the client (that's why I'm forced to use html tables).
$rowList[$rowCode] .= '<tr><td class="lens-price-serv"><small>' . $lang['services'] . ':</small> <strong>' . money_format( '%i', $srvPriceTotal ) . '</strong></td></tr>';
The code works just fine. This morning, though, I got an email with the following html (corresponding to the PHP code I posted):
<tr><td class="lens-price-serv"><s>Services: <strong>€ 10,00</strong></s></td></tr>
I couldn't get the exact html code, this one comes from Chrome's Developer Tools. What I think happened is that the 'small' tag got truncated during string concatenation, becoming an open 's' tag. This tag, without the corresponding closure '/s' tag, got automatically closed by the browser just before closing the 'td'.
This issue is hard to replicate. I tried making a new order using the same parameters, but it worked just fine.
This is the second time this issue presented itself; the first time it was on another part of the code, but the string got truncated as well, resulting in wrong output.
Could this issue be caused by the server? This script is hosted by a shared hosting running Apache 2.2.24 and PHP 5.3.21.

Try to use
$rowList[$rowCode] .= "<tr><td class='lens-price-serv'><small>$lang['services']:</small> <strong>" . money_format( '%i', $srvPriceTotal ) . "</strong></td></tr>";

You can also try
$rowList[$rowCode] .= "<tr><td class='lens-price-serv'><small>{$lang['services']}:</small> <strong>" . money_format( '%i', $srvPriceTotal ) . "</strong></td></tr>";

Related

php plaintext output compatibility with google sheets

I made a script in PHP to fit into a template I made in google sheets. I made the output text for convenience:
header("content-type: text/plain");
I didn't need html when I'm going for the =IMAGE or whatever. Now, I'm trying to make it so I can brainlessly copy and paste the output from my browser to the google sheets. "\t" worked completely fine for newlines, but "\t" is having some issues where google sheets thinks every single space in the tab is an actual space, so it gives an #ERROR! when i try to do multiple tabs at once.
the error: https://puu.sh/IOp2B/34b7f5ce50.png
here's my code:
$tab = "\t";
echo '=IMAGE("https://www.smogon.com/forums//media/minisprites/' . strtolower($final) . '.png")' . htmlspecialchars($tab) . $key . htmlspecialchars($tab) . substr(strval(($value/count($allteams) * 100)), 0, 5) . '%' . $tab . $value . "\n";
I tried "\t" itself, no htmlspecialcharacters, but copying directly from the webpage is consistently making it multiple spaces instead of a tab. I also tried with different browsers.
Here are my questions:
Is there a function to make the tabs copy as tabs instead of what it does now?
Is there a google sheets thing I can do to make another character (not tab) split up the columns? If so, I will replace the tab in my code with a semicolon or something, I tried data -> split columns but it doesn't work for a blank template, only when data is pasted. I want to make the data paste perfectly with no fiddling from the user.
I don't think is a browser thing, I tried like 3 different ones and the issue persists. Also, if I put the output into pastebin/text editor, it works well after, but I don't want the user to HAVE to do this.
I am going to look into importxml, but I have no info on it yet... just thought I'd ask
tldr my plaintext output html code has been great but its not separating columns well for my google sheet template.
Instead of using tab or \t, write your formula as follows
echo '={image("https://i.stack.imgur.com/5iCFq.png"),"other info","next info"}'
use , (or ; according to your locale) to separate the different cells and curly brackets { }
include specific information as usual in php with . and $

Using a mailto link, how do I prevent Gmail from stripping the = sign and everything after it from the URL's query string?

I am trying to format a mailto link that works with a variety of mail clients. This works with Mac's "Mail" and Thunderbird. However, when I click the link on my Android phone, the query string in the URL is stripped starting with the equals sign. I have tried to add code to specify the content type, to parse the query string and re-add it, etc. So far, nothing works. Here's my code:
<?php
ini_set('default_charset', 'UTF-8');
$BASEURL = strtok($_SERVER["REQUEST_URI"], '?');
echo '
<li><a href="mailto:?subject=An Article Worth Reading: ' . $ALTITLE . '&body=I found this article worthwhile and think you will too:%0D%0A %0D%0A
https://worldviewpublications.org' . htmlspecialchars($_SERVER['HTTPS_HOST']) . $BASEURL . "?" . htmlspecialchars($_SERVER['QUERY_STRING']) . "%0D%0A";
echo '">share with a friend</a></li>
';
?>
I'm pretty proficient in html/css but a beginner with PHP, so I am not at all sure about the efficacy of some of the PHP I added. I'd greatly appreciate any help.
I seem to have found the solution to the problem, which I'm posting below. I've removed the code shared in my initial post that didn't help (in case you're wondering, the nonbreaking space after the colon is for Thunderbird, which does not honor the new line code).
The solution was to encode the = sign. In order to do that, I first extracted the first and last parts of the URL: (1) the first part without the query string and (2) the query string after the = sign. Then I concatenated it back together, spelling out the middle part between the main URL and the unique code at the end of the query string — the question mark (encoded as %3f), the query code identifier (IDN), and the equal sign (encoded %3d). This solution passes the entire URL, including all of the query string, to my gmail client when I'm using my Android phone.
<?php
$BASEURL = strtok($_SERVER["REQUEST_URI"], '?');
$IDEN = ltrim(($_SERVER['QUERY_STRING']), 'IDN=');
echo '
<li><a href="mailto:?subject=An Article Worth Reading: ' . $ALTITLE . '&body=%0D%0AI found this article worthwhile and think you will too:
%0D%0A%0D%0A' . htmlspecialchars("\n\r") . 'https://worldviewpublications.org' . htmlspecialchars($_SERVER['HTTPS_HOST']) . $BASEURL . '%3fIDN%3d' . $IDEN;
echo '">share with a friend</a></li>
';
?>

php url string converts "&section=" to "§ion", which does not yield cURL response

I save a php string as
$url = "http://example.com/index.php?q=board/ajax_call&section=get_messages";
The url when printed to screen displays as
"http://example.com/index.php?q=board/ajax_call§ion=get_messages" as "&sect" gets auto converted to special char "§".
How can I prevent this so that I can call the correct URL using cURL .
Your Problem
The problem is that &sect is interpreted by the browser as the HTML entity for §.* So, &section displays as §ion.
The Solution
If you're going to print the URL itself, you need to escape the & and turn it into &. You can do this automatically using htmlentities(). Sample code:
<?php
$url = "http://example.com/index.php?q=board/ajax_call&section=get_messages";
echo "Without htmlentities(): " . $url . "\n";
// output: http://example.com/index.php?q=board/ajax_call&section=get_messages
echo "With htmlentities(): " . htmlentities($url) . "\n";
// output: http://example.com/index.php?q=board/ajax_call&section=get_messages
Here's a demo.
A Note About Security
Note that using htmlentities() here is a good idea for lots of other reasons. What if somebody used this URL?
http://example.com/index.php?q=board/ajax_call&section=get_messages<script src="http://evilsite/evil.js></script>
If you just dumped it out onto the screen, you have just included an evil JavaScript. Congratulations! You just hacked your user and, probably, got your own site hacked. This is a real problem called XSS (Cross-Site Scripting). But if you call htmlentities() first, you get:
http://example.com/index.php?q=board/ajax_call&section=get_messages<script src="http://evilsite/evil.js></script>
That's safe and won't actually run the evil script.
* Technically, the HTML entity is §, with the semicolon, but nearly all browsers with treat it as an HTML entity with or without the semicolon. See this answer for a good explanation.
Change the & to &.
(See w3c markup validator ampersand (&) error for a bit more information.)

php URL encoding error

I have the following line in my code
$lib = simplexml_load_file("http://www.goodreads.com/book/title.xml?key=MYAPIKEY&title=" . $current_book,null,true);
when this is sent, it is sent like this
http://www.goodreads.com/book/title.xml?key=MYAPIKEY&title=Dashing%2BThrough%2Bthe%2BSnow%2BMary%2BHiggins%2BClark%0A
When this happens, the book is not found and an error is returned . . .
However, if i type into my browser
http://www.goodreads.com/book/title.xml?key=MYAPIKEY&title=Dashing+Through+the+Snow+Mary+Higgins+Clark
Then i get a valid XML response, and i can use this to complete my code.
So, how do i send this without the + getting changed to %2B ?
And what is the %0A at the end of the URL?
Your url seems to be encoded twice.
Since you are not encoding at all in the code you show (by the way that would be the proper place to do that) it must have happened before.
The error is not in this code but somewhere before that.
You could correct it like this:
$lib = simplexml_load_file("http://www.goodreads.com/book/title.xml?key=MYAPIKEY&title=" . trim(urldecode($current_book)),null,true);
However that is only a workaround for an existing error, you should fix the prior encoding.
Also the character you mention is a newline character, thats the reason for trim

How to parse a variable?

I have the following test line in my PHP which works fine as a way of posting to Twitter from within my PHP code.
$oauth->post('statuses/update', array('status' => "hello world"));
However I want to post the contents of a variable as opposed to Hello World
If I change the code as follows, then all that gets posted is $message
$oauth->post('statuses/update', array('status' => '$message'));
I also tried without the ' but then nothing got posted, ie
$oauth->post('statuses/update', array('status' => $message));
How can I correctly parse the contents of $message?
$message is created as follows
$message = "http://www.smartphonesoft.com/index.php?option=com_mtree&task=viewlink&link_id=" .$link_id . " " ."Windows Phone Software" . " " .$link_name . " " . $metadesc;
I added an echo $message which showed me what I expected, namely:
http://www.smartphonesoft.com/index.php?option=com_mtree&task=viewlink&link_id=33183073
Windows Phone Software Pocket Player
Pocket Player is a rockin' way to
enjoy music and video on your Windows
Mobile device. Through multiple media
and playlist formats, Internet
connectivity, plugin extensions, and
an intuitive interface, Pocket Player
means less taps, more music!
Thanks,
Greg
From the Twitter API doc for status/update:
status The text of your status update, up to 140 characters. URL encode as necessary.
So I'd say you have to shorten the $message, because yours has 369 characters.
The last code you quote is correct. Are you sure $message has meaningful content?
(Aside: The reason for '$message' posting "$message" verbatim is that single-quoted strings in PHP do not get variable interpolation nor escape characters: '\n' is literally "\n", whereas "\n" would result in a string containing the newline character.)
'$message' can't work because you're actually passing the string "'$message'", and not the $message variable.
If the second code you posted doesn't work, it's either because $message is not defined in your script, or because something else in your script is wrong, but we can't really tell that without seeing the rest of the code.
Since your URL is way too long for twitter, perhaps you'd like to shorten the url before posting it.
The bit.ly API documentation page will help you set up an account and your own api key.
You could then either devise your own code from the official documentation or follow this bit.ly api tutorial by David Walsh

Categories