<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LogikDevelopment &#187; PHP</title>
	<atom:link href="http://www.logikdev.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.logikdev.com</link>
	<description>&#34;Il n&#039;y a pas de problème, il n&#039;y a que des solutions. L&#039;esprit de l&#039;homme invente ensuite le problème.&#34; André Gide</description>
	<lastBuildDate>Sun, 06 May 2012 12:29:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Send email attachment with PHP</title>
		<link>http://www.logikdev.com/2011/09/01/send-email-attachment-with-php/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=send-email-attachment-with-php</link>
		<comments>http://www.logikdev.com/2011/09/01/send-email-attachment-with-php/#comments</comments>
		<pubDate>Thu, 01 Sep 2011 20:50:04 +0000</pubDate>
		<dc:creator>smoreau</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[boundary]]></category>
		<category><![CDATA[email attachment]]></category>

		<guid isPermaLink="false">http://www.logikdev.com/?p=1404</guid>
		<description><![CDATA[Everybody probably knows how to send an email using PHP, but do you know how to send an email with an attachment? This is actually not very difficult and you can find examples all over the web. However, it seems that all the examples I found didn&#8217;t work! But after some debugging and testing, I [...]]]></description>
			<content:encoded><![CDATA[<p>Everybody probably knows how to send an email using PHP, but do you know how to send an email with an attachment?</p>
<p>This is actually not very difficult and you can find examples all over the web. However, it seems that all the examples I found didn&#8217;t work! <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /><br />
But after some debugging and testing, I finally got a working solution. <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Please see below the code with comments:</p>
<pre class="brush: php; title: ; notranslate">
// Define some variables
$to = 'test@logikdev.com'
$subject = 'Test email';
// Create a boundary string. It must be unique so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));

// Define the headers
$headers = &quot;From: noreply@logikdev.com\r\n&quot;;
// Add boundary string and mime type specification
$headers .= &quot;Content-Type: multipart/mixed; boundary=\&quot;PHP-mixed-&quot;.$random_hash.&quot;\&quot;&quot;;

// Read the attachment file contents into a string, encode it with MIME base64, and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));

// Define the body of the message
$message = &quot;
--PHP-mixed-$random_hash
Content-Type: text/plain; charset='iso-8859-1'

This is a test email message sent with PHP.

--PHP-mixed-$random_hash
Content-Type: application/zip; name=attachment.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment
--PHP-mixed-$random_hash--&quot;;

// Send the email
if (@mail($to, $subject, $message, $headers)) {
	echo &quot;The mail has been sent.&quot;;
} else {
	echo &quot;The mail has NOT been sent!&quot;;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.logikdev.com/2011/09/01/send-email-attachment-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Segmentation fault at end of PHP script</title>
		<link>http://www.logikdev.com/2011/03/14/segmentation-fault-at-end-of-php-script/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=segmentation-fault-at-end-of-php-script</link>
		<comments>http://www.logikdev.com/2011/03/14/segmentation-fault-at-end-of-php-script/#comments</comments>
		<pubDate>Mon, 14 Mar 2011 22:35:18 +0000</pubDate>
		<dc:creator>smoreau</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[cURL]]></category>
		<category><![CDATA[extensions]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Segmentation fault]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.logikdev.com/?p=1167</guid>
		<description><![CDATA[This one is a very strange problem! I created a little PHP script which connects to a MySQL database. Nothing amazing, except that I needed to run it via command line using the php command. What happened is that the script ran perfectly fine but, for some reason, it returned &#8220;Segmentation fault&#8221; when it reached [...]]]></description>
			<content:encoded><![CDATA[<p>This one is a very strange problem!<br />
I created a little PHP script which connects to a MySQL database. Nothing amazing, except that I needed to run it via command line using the <code>php</code> command. What happened is that the script ran perfectly fine but, for some reason, it returned &#8220;Segmentation fault&#8221; when it reached the end of the file! <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_eek.gif' alt='8-O' class='wp-smiley' /><br />
It reminds me of the old days when I worked on C language, I used to see this not-very-useful message quite often. <img src='http://www.logikdev.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Anyway, why do we get a &#8220;Segmentation fault&#8221; at the end of a PHP script? And when I say &#8216;the end&#8217;, I mean after any other lines!</p>
<p>Looking around the web, I found the following bug report: <a href="http://bugs.php.net/bug.php?id=43823">http://bugs.php.net/bug.php?id=43823</a><br />
This user seems to have the same problem than me except he is using a PostgreSQL database instead of a MySQL database.</p>
<p>Reading through the page, it looks like the problem comes from the order in which PHP loads his extensions! They explain that PostgreSQL should be loaded before cURL. If I apply the same logic, MySQL should then be loaded before cURL too.</p>
<p>To do that, we need to comment out the line <code>extension=curl.so</code> from the file <code>/etc/php5/cli/conf.d/curl.ini</code>:</p>
<pre class="brush: bash; title: ; notranslate">
# configuration for php CURL module
#extension=curl.so
</pre>
<p>And add it to the file <code>/etc/php5/cli/conf.d/mysql.ini</code> AFTER the line <code>extension=mysql.so</code>:</p>
<pre class="brush: bash; title: ; notranslate">
# configuration for php MySQL module
extension=mysql.so
extension=curl.so
</pre>
<p>To be honest, I don&#8217;t really like this solution as it is a bit messy. I would prefer PHP developers to fix the root cause or at least have a way to change the order in which PHP loads the extensions.</p>
<p><strong>NB:</strong> I was executing this PHP script on a Debian GNU/Linux 5.0 with Apache 2.2.9 and PHP 5.2.6.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.logikdev.com/2011/03/14/segmentation-fault-at-end-of-php-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Encrypt with PHP &#8211; Decrypt with Java</title>
		<link>http://www.logikdev.com/2010/11/01/encrypt-with-php-decrypt-with-java/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=encrypt-with-php-decrypt-with-java</link>
		<comments>http://www.logikdev.com/2010/11/01/encrypt-with-php-decrypt-with-java/#comments</comments>
		<pubDate>Mon, 01 Nov 2010 23:14:22 +0000</pubDate>
		<dc:creator>smoreau</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[decryption]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[javax.crypto]]></category>
		<category><![CDATA[mcrypt]]></category>

		<guid isPermaLink="false">http://www.logikdev.com/?p=887</guid>
		<description><![CDATA[For security reason, I wanted to encrypt the data transferred between PHP web services and a Java application. But the problem was to encrypt the data with PHP in a way that it is possible to decrypt it using Java. It obviously exists a lot of ways of doing this. But here is the way [...]]]></description>
			<content:encoded><![CDATA[<p>For security reason, I wanted to encrypt the data transferred between PHP web services and a Java application. But the problem was to encrypt the data with PHP in a way that it is possible to decrypt it using Java.</p>
<p>It obviously exists a lot of ways of doing this. But here is the way I choose:</p>
<ul>
<li>Use a secret key and an initialisation vector for the encryption and decryption</li>
<li>Use the <strong>mcrypt</strong> PHP module for the encryption</li>
<li>Use the <strong>javax.crypto</strong> Java package for the decryption</li>
</ul>
<p>Please find below the PHP code for the encryption:</p>
<pre class="brush: php; title: ; notranslate">
function encrypt($message, $initialVector, $secretKey) {
    return base64_encode(
        mcrypt_encrypt(
            MCRYPT_RIJNDAEL_128,
            md5($secretKey),
            $message,
            MCRYPT_MODE_CFB,
            $initialVector
        )
    );
}
</pre>
<p>And please see below the Java code for the decryption:</p>
<pre class="brush: java; title: ; notranslate">
public static String md5(String input) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance(&quot;MD5&quot;);
    byte[] messageDigest = md.digest(input.getBytes());
    BigInteger number = new BigInteger(1, messageDigest);
    return number.toString(16);
}

public String decrypt(String encryptedData, String initialVectorString, String secretKey) {
    String decryptedData = null;
    try {
        SecretKeySpec skeySpec = new SecretKeySpec(md5(secretKey).getBytes(), &quot;AES&quot;);
        IvParameterSpec initialVector = new IvParameterSpec(initialVectorString.getBytes());
        Cipher cipher = Cipher.getInstance(&quot;AES/CFB8/NoPadding&quot;);
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, initialVector);
        byte[] encryptedByteArray = (new org.apache.commons.codec.binary.Base64()).decode(encryptedData.getBytes());
        byte[] decryptedByteArray = cipher.doFinal(encryptedByteArray);
        decryptedData = new String(decryptedByteArray, &quot;UTF8&quot;);
    } catch (Exception e) {
        LOGGER.debug(&quot;Problem decrypting the data&quot;, e);
    }
    return decryptedData;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.logikdev.com/2010/11/01/encrypt-with-php-decrypt-with-java/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
	</channel>
</rss>

