<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[FrontAccounting forum — Saving text with apostrophe converts to &#039;s]]></title>
	<link rel="self" href="https://frontaccounting.com/punbb/extern.php?action=feed&amp;tid=6391&amp;type=atom" />
	<updated>2016-08-08T06:10:03Z</updated>
	<generator>PunBB</generator>
	<id>https://frontaccounting.com/punbb/viewtopic.php?id=6391</id>
		<entry>
			<title type="html"><![CDATA[Re: Saving text with apostrophe converts to &#039;s]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=25982#p25982" />
			<content type="html"><![CDATA[<p>@apmuthu<br />Sorry I editted my post earlier. It worked. I was looking at the wrong PDF file. Thanks</p>]]></content>
			<author>
				<name><![CDATA[jnunez]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=31978</uri>
			</author>
			<updated>2016-08-08T06:10:03Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=25982#p25982</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Saving text with apostrophe converts to &#039;s]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=25981#p25981" />
			<content type="html"><![CDATA[<p>@jnunez: Hope the reports display correctly, in which case make sure that double-encoding does not take place when it is saved again.</p><p>BTW, for me, it prints and displays on screen correctly though it is stored encoded in the table field for both the <strong>sys_prefs</strong> table records and the <strong>item_codes</strong> table as well.</p><p>Tested on XAMPP v1.7.3 on WinXP SP3 (and Debian 6) and FF 37 browser.</p>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2016-08-08T05:52:08Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=25981#p25981</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Saving text with apostrophe converts to &#039;s]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=25980#p25980" />
			<content type="html"><![CDATA[<p>@itronics<br />Oh I see. Thank you for nice explanation.</p><p>@apmuthu<br />I tried without mb_convert_encoding() and without your earlier post&#039;s fix, It worked!.</p>]]></content>
			<author>
				<name><![CDATA[jnunez]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=31978</uri>
			</author>
			<updated>2016-08-08T05:29:03Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=25980#p25980</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Saving text with apostrophe converts to &#039;s]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=25975#p25975" />
			<content type="html"><![CDATA[<p>Backport of @itronix fix above is <a href="https://github.com/apmuthu/frontaccounting/commit/095b3c2d84c07e542262c6bd8670b425cda89318">in my FA 2.3 repo</a>.</p><p>The fix in my post above will not need any rewriting of the code for displayed back to browser as simple sql escaping will not be touched by the db_escape&#039;s decoding code. </p><p>You&#039;re right about the XSS vulnerability and hence my earlier post&#039;s fix should not normally be used. Thanks @itronics for the explanation.</p><p>@jnunex: please test with my backport in this post without the earlier post&#039;s fix and see if all is well.</p>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2016-08-06T13:26:52Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=25975#p25975</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Saving text with apostrophe converts to &#039;s]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=25973#p25973" />
			<content type="html"><![CDATA[<p>I have made suggested tests. There is indeed small bug in html entities handling in support for TextWrap pdf fields. When text is longer then place for printing, it was stripped and sometimes not decoded properly. This is the only problem found. Regarding display, encoded names are always handled properly, and edition does not change anything here. Fix for the pdf problem is <a href="https://sourceforge.net/p/frontaccounting/git/ci/135fa36e58e67cf54681b896b3f60d7c3dce32d2/">here</a>.</p><p>Small explanation to the db_escape function. We are not aware about backup file readability (they are created just for database restoring), our main concern is application security, and this is why db_escape works as is. When you will change the escaping code removing html entities encoding, you will make an application vulnerable to XSS attacks. </p><p>To prevent permanent XSS vunerability you have to prevent special html characters to be stored in database, or have to be encoded whenever it is displayed back to browser. We have chosen the former approach&nbsp; and implemented it in db_escape() function code (beside simple sql escaping).<br />The latter approach would require rewriting all the data displaying code in FA, which is just time wasting.</p><p>Regarding the lacking support for ISO-8859-2 in htmletities, this is old, never solved php problem, so we have to ommit is somehow. As all dangerous chars are included and have the same placement in both ISO-8859-1 and ISO-8859-2 sets, we can just fake encoding declaration here.</p><p>Janusz</p>]]></content>
			<author>
				<name><![CDATA[itronics]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=89</uri>
			</author>
			<updated>2016-08-06T09:18:14Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=25973#p25973</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Saving text with apostrophe converts to &#039;s]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=25967#p25967" />
			<content type="html"><![CDATA[<p>In some installs, the </p><div class="codebox"><pre><code>&amp;#039;</code></pre></div><p> gets displayed as is without being decoded on screen / in screen field.</p><p>It will be better if it is stored as:<br /></p><div class="codebox"><pre><code>UPDATE sys_prefs SET value = &#039;Carmen\&#039;s&#039; WHERE name=&#039;coy_name&#039;;</code></pre></div><p>1. Store <strong>3/4&quot; nuts and 4&#039; 2&quot; bolt</strong> as a new item in inventory.<br />2. Edit it and see what you get in the Name field.<br />3. Make some change say <strong>3/4&quot; nuts with 4&#039; 2&quot; bolt</strong> and save.<br />4. View the item on screen and in the report and check if it gets mangled with double encoding....</p><p>What is the problem with using backslashes for single quotes as it is more readable in the sql backup?</p><p>What benefit do we get by using <strong>htmlspecialchars()</strong> when <strong>mysql_real_escape_string()</strong> is used alone?</p><p>What other characters are usefully modified when <strong>iso-8859-2</strong> encoding is switched to <strong>iso-8859-1</strong> in <strong>htmlspecialchars()</strong>?</p>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2016-08-06T01:25:09Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=25967#p25967</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Saving text with apostrophe converts to &#039;s]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=25966#p25966" />
			<content type="html"><![CDATA[<p>I&#039;m not sure what you mean. </p><p>If you have apostrophe in company name, it should be stored encoded. So exact sql is:<br /></p><div class="codebox"><pre><code>UPDATE sys_prefs SET value = &#039;Carmen&amp;#039;s&#039; WHERE name=&#039;coy_name&#039;;</code></pre></div><p>So, when you look into your database with e.g. phpmyadmin you will see in one sys_prefs record Carmen&amp;#039;s, and this is perfectly OK.<br />Where you encounter problem with this?<br />Janusz</p>]]></content>
			<author>
				<name><![CDATA[itronics]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=89</uri>
			</author>
			<updated>2016-08-05T11:29:13Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=25966#p25966</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Saving text with apostrophe converts to &#039;s]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=25965#p25965" />
			<content type="html"><![CDATA[<p>Yes, the db_escape() role is crucial. But actually saving the decoded apostrophe is the problem. When I look the sql_trail table to see how the query looks like. It showed like this </p><p>&quot;UPDATE sys_prefs SET value = &amp;#039;Carmen&amp;#039;s &amp;#039; WHERE name=&amp;#039;coy_name&amp;#039;&quot;</p><p>-- which is I think the proper: before the db_escape()</p><p>UPDATE sys_prefs SET value = &#039;Carmen&#039;s&#039; WHERE name=&#039;coy_name&#039;;</p><p>-- after the db_escape() it should like this</p><p>UPDATE sys_prefs SET value = &amp;#039;Carmen&#039;s &amp;#039; WHERE name=&amp;#039;coy_name&amp;#039;</p><p>Sorry for my bad english.</p>]]></content>
			<author>
				<name><![CDATA[jnunez]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=31978</uri>
			</author>
			<updated>2016-08-05T09:49:12Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=25965#p25965</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Saving text with apostrophe converts to &#039;s]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=25961#p25961" />
			<content type="html"><![CDATA[<p>db_escape is crucial for application security. It prevents SQL injection and XSS attacks encoding all the content once before it is stored in database. This way we have no need to clean the db content later, in all the places where it is displayed in application.<br />If there is any problem with encoded chars in generated PDF file, the reason is probably either in PDF generation class or in php report file. To fix it I would like to see real step by step scenario ending with the problematic pdf file.</p><p>Janusz</p>]]></content>
			<author>
				<name><![CDATA[itronics]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=89</uri>
			</author>
			<updated>2016-08-05T07:11:11Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=25961#p25961</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Saving text with apostrophe converts to &#039;s]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=25957#p25957" />
			<content type="html"><![CDATA[<p>Yes, wonder what characters other than the 5 listed are handled by <strong>htmlspecialchars()</strong> in <em>iso-8859-1</em> encoding languages like Polish. Also how would the boolean variables be escaped as they would fit the non string / non numeric conditions and return an error - hence the usage of the db_escape function would have to be prudent.</p><p>These <a href="https://www.owasp.org/index.php/OWASP_PHP_Filters">filters</a> may be useful.</p><p>Example <strong>filter_input()</strong> from the php manual (<a href="http://www.php.net/manual/en/filter.filters.sanitize.php">List of available Filters</a>):<br /></p><div class="codebox"><pre><code>&lt;?php
$search_html = filter_input(INPUT_GET, &#039;search&#039;, FILTER_SANITIZE_SPECIAL_CHARS);
$search_url = filter_input(INPUT_GET, &#039;search&#039;, FILTER_SANITIZE_ENCODED);
echo &quot;You have searched for $search_html.\n&quot;;
echo &quot;&lt;a href=&#039;?search=$search_url&#039;&gt;Search again.&lt;/a&gt;&quot;;
?&gt;</code></pre></div><p>The above example will output something similar to:</p><div class="codebox"><pre><code>You have searched for Me &amp;#38; son.
&lt;a href=&#039;?search=Me%20%26%20son&#039;&gt;Search again.&lt;/a&gt;</code></pre></div>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2016-08-04T15:59:08Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=25957#p25957</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Saving text with apostrophe converts to &#039;s]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=25954#p25954" />
			<content type="html"><![CDATA[<p>Again this is written by Janusz. I will ask him to have a look.</p><p>Joe</p>]]></content>
			<author>
				<name><![CDATA[joe]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=3</uri>
			</author>
			<updated>2016-08-04T13:37:11Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=25954#p25954</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Saving text with apostrophe converts to &#039;s]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=25947#p25947" />
			<content type="html"><![CDATA[<p>These functions are part of the <strong>db_escape()</strong> function.</p><p>@joe: looks like you can commit it now.</p>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2016-08-04T10:47:07Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=25947#p25947</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Saving text with apostrophe converts to &#039;s]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=25945#p25945" />
			<content type="html"><![CDATA[<p>It works! I also think that db_escape() might be doing something. Thank you for your immediate response.</p>]]></content>
			<author>
				<name><![CDATA[jnunez]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=31978</uri>
			</author>
			<updated>2016-08-04T09:49:23Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=25945#p25945</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Saving text with apostrophe converts to &#039;s]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=25939#p25939" />
			<content type="html"><![CDATA[<p>Thanks. Here is the solution:</p><p>All data stored in the tables pass through <strong>function db_escape()</strong>. The <em>Setup -&gt; Company Setup</em> form acquires the data and stores it using <strong>function update_company_prefs()</strong> in <strong>admin/db/company_db.inc</strong> file.</p><p>It is the &quot;ENT_QUOTES&quot; parameter that causes the apostrophe to get encoded in the function <strong>htmlspecialchars()</strong>.</p><p>The real solution will be to use the <strong>htmlspecialchars()</strong> function when the <strong>mysql_real_escape_string()</strong> is <span class="bbu">not available</span> by altering the <strong>function db_excape()</strong> in <strong>includes/db/connect_db.inc</strong> :<br /></p><div class="codebox"><pre><code>function db_escape($value = &quot;&quot;, $nullify = false)
{
    $value = @html_entity_decode($value, ENT_QUOTES, $_SESSION[&#039;language&#039;]-&gt;encoding);
    $value = @htmlspecialchars($value, ENT_QUOTES, $_SESSION[&#039;language&#039;]-&gt;encoding==&#039;iso-8859-2&#039; ? &#039;ISO-8859-1&#039; : $_SESSION[&#039;language&#039;]-&gt;encoding);

      //reset default if second parameter is skipped
    $nullify = ($nullify === null) ? (false) : ($nullify);

      //check for null/unset/empty strings
    if ((!isset($value)) || (is_null($value)) || ($value === &quot;&quot;)) {
        $value = ($nullify) ? (&quot;NULL&quot;) : (&quot;&#039;&#039;&quot;);
    } else {
        if (is_string($value)) {
              //value is a string and should be quoted; determine best method based on available extensions
            if (function_exists(&#039;mysql_real_escape_string&#039;)) {
                  $value = &quot;&#039;&quot; . mysql_real_escape_string($value) . &quot;&#039;&quot;;
            } else {
              $value = &quot;&#039;&quot; . mysql_escape_string($value) . &quot;&#039;&quot;;
            }
        } else if (!is_numeric($value)) {
            //value is not a string nor numeric
            display_error(&quot;ERROR: incorrect data type send to sql query&quot;);
            echo &#039;&lt;br&gt;&lt;br&gt;&#039;;
            exit();
        }
    }
    return $value;
}</code></pre></div><p>to be<br /></p><div class="codebox"><pre><code>function db_escape($value = &quot;&quot;, $nullify = false)
{
    $value = @html_entity_decode($value, ENT_QUOTES, $_SESSION[&#039;language&#039;]-&gt;encoding);
    if ($_SESSION[&#039;language&#039;]-&gt;encoding==&#039;iso-8859-2&#039;) $value = @htmlspecialchars($value, ENT_QUOTES, &#039;ISO-8859-1&#039;);

      //reset default if second parameter is skipped
    $nullify = ($nullify === null) ? (false) : ($nullify);

      //check for null/unset/empty strings
    if ((!isset($value)) || (is_null($value)) || ($value === &quot;&quot;)) {
        $value = ($nullify) ? (&quot;NULL&quot;) : (&quot;&#039;&#039;&quot;);
    } else {
        if (is_string($value)) {
              //value is a string and should be quoted; determine best method based on available extensions
            if (function_exists(&#039;mysql_real_escape_string&#039;)) {
                  $value = &quot;&#039;&quot; . mysql_real_escape_string($value) . &quot;&#039;&quot;;
            } else {
                $value = &quot;&#039;&quot; . mysql_escape_string($value) . &quot;&#039;&quot;;
            }
        } else if (!is_numeric($value)) {
            //value is not a string nor numeric
            display_error(&quot;ERROR: incorrect data type send to sql query&quot;);
            echo &#039;&lt;br&gt;&lt;br&gt;&#039;;
            exit();
        }
    }
    return $value;
}</code></pre></div><p>We are only conditionally using the line <strong>$value = @htmlspecialchars......</strong> for Polish like languages and can later remove it altogether. The old <strong>mysql_escape_string()</strong> did not use the link identifier and the encoding charset and may have needed <em>@htmlspecialchars</em> in which case it can be moved to just above that function.</p><p>The <strong><a href="http://php.net/manual/en/function.htmlspecialchars.php">htmlspecialchars()</a></strong> encodes<strong> &amp;,&#039;,&quot;,&lt;,&gt;</strong> only.</p><p>@joe: want to commit it?</p>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2016-08-04T06:54:43Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=25939#p25939</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Saving text with apostrophe converts to &#039;s]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=25936#p25936" />
			<content type="html"><![CDATA[<p>Yes it displayed the apostrophe without using the mb_convert_encoding() funtion. How can I help? I&#039;m not that very skilled on PHP but I&#039;ll do what I can to help.</p>]]></content>
			<author>
				<name><![CDATA[jnunez]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=31978</uri>
			</author>
			<updated>2016-08-04T06:09:10Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=25936#p25936</id>
		</entry>
</feed>
