<?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>PHP Log &#187; PEAR</title>
	<atom:link href="http://www.php-log.de/programmierer/php/pear/feed" rel="self" type="application/rss+xml" />
	<link>http://www.php-log.de</link>
	<description>Eine feine Sammlung nützlicher PHP Notizen</description>
	<lastBuildDate>Mon, 20 Sep 2010 00:22:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Von CakePHP, Ajax Problemen und der Security Komponente</title>
		<link>http://www.php-log.de/128/php/von-cakephp-ajax-problemen-und-der-security-komponente</link>
		<comments>http://www.php-log.de/128/php/von-cakephp-ajax-problemen-und-der-security-komponente#comments</comments>
		<pubDate>Tue, 03 Feb 2009 23:52:00 +0000</pubDate>
		<dc:creator>ralle</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Mail]]></category>
		<category><![CDATA[PEAR]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.php-log.de/?p=128</guid>
		<description><![CDATA[Wer die &#8220;Security&#8221; Komponente von CakePHP in seinen Projekte einsetzt, kann bei der Ajax-Entwicklung die ein oder andere fröhliche, kleine Überraschung erleben. Und zwar in Form eines 404 Errors. Oder zu Deutsch: &#8220;Ajax geht nicht&#8221;. Warum nicht? Weil die Security Komponente verschlüsselte Daten (Security Tokens) in die Session schreibt die, im Falle eines Ajax Requests, [...]]]></description>
			<content:encoded><![CDATA[<p>Wer die &#8220;Security&#8221; Komponente von CakePHP in seinen Projekte einsetzt, kann bei der Ajax-Entwicklung die ein oder andere fröhliche, kleine Überraschung erleben. Und zwar in Form eines 404 Errors. Oder zu Deutsch: &#8220;Ajax geht nicht&#8221;. Warum nicht? Weil die Security Komponente verschlüsselte Daten (Security Tokens) in die Session schreibt die, im Falle eines Ajax Requests, nicht validiert werden können. Da steht sich CakePHP irgendwie selbst im Weg, vermutet böse Dinge und gibt deshalb lieber einen verwirrenden 404 Not found.<span id="more-128"></span></p>
<blockquote><p>Am Rande bemerkt: Der Einsatz des Firfox Addons <a href="https://addons.mozilla.org/de/firefox/addon/1843" target="_blank">Firebug</a> ist für die Entwicklung von Ajax dringend zu empfehlen.</p></blockquote>
<p>Zum Glück bietet CakePHP, wie es sich für ein gutes Framework gehört, auch hier eine beglückend einfache Lösung an. Den blackHoleCallback der Komponente Security. Dieser behandelt das Auftreten von Sicherheitsverstößen, ist in der Lage den 404er zu unterbinden und lässt das Framework weiterlaufen. Mein Lösung sieht so aus.</p>
<ol>
<li>blackHoleCallback im beforeFilter des AppController definieren.</li>
<li>In der Callback-Methode prüfen ob es eine genehme AJAX Methode betrifft.</li>
<li>Wenn &#8220;Ja&#8221;, einfach return. Sobald der Callback definiert wurde ist er für alles verantworlich, inkl. Terminerung des Scripts etc.</li>
<li>Wenn &#8220;Nein&#8221;, exit() und ggfl. Logging und/oder andere Maßnahmen.</li>
</ol>
<p>In Code gegossen sieht das so aus:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AppController <span style="color: #000000; font-weight: bold;">extends</span> Controller <span style="color: #009900;">&#123;</span>	
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$components</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Security'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Session'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> is_allowed_ajax_request<span style="color: #009900;">&#40;</span><span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>		
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'controller'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;users&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'action'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;auto_complete&quot;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'invalid request'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> beforeFilter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>		
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Security</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">blackHoleCallback</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;is_allowed_ajax_request&quot;</span><span style="color: #339933;">;</span>		
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.php-log.de/128/php/von-cakephp-ajax-problemen-und-der-security-komponente/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>E-Mail mit Anhang über PHP 4 versenden</title>
		<link>http://www.php-log.de/23/php/e-mail-mit-anhang-uber-php-4-versenden</link>
		<comments>http://www.php-log.de/23/php/e-mail-mit-anhang-uber-php-4-versenden#comments</comments>
		<pubDate>Tue, 27 Jan 2009 11:43:30 +0000</pubDate>
		<dc:creator>ralle</dc:creator>
				<category><![CDATA[Mail]]></category>
		<category><![CDATA[PEAR]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Email]]></category>

		<guid isPermaLink="false">http://www.php-log.de/?p=23</guid>
		<description><![CDATA[Immer wieder stößt man auf Server bzw. Webseiten die noch mit PHP 4 betrieben werden. Daran wird sich auch in absehbarer Zukunft nichts ändern. Wer also mal eben schnell was in so einer antiquierten Umgegebung erledigen möchte kann nicht auf moderne Frameworks setzten, da diese meist PHP 5 erfordern. Eines der bekanntesten Frameworks die PHP [...]]]></description>
			<content:encoded><![CDATA[<p>Immer wieder stößt man auf Server bzw. Webseiten die noch mit PHP 4 betrieben werden. Daran wird sich auch in absehbarer Zukunft nichts ändern. Wer also mal eben schnell was in so einer antiquierten Umgegebung erledigen möchte kann nicht auf moderne Frameworks setzten, da diese meist PHP 5 erfordern. Eines der bekanntesten Frameworks die PHP 5 unterstützen ist sicherlich <a href="http://pear.php.net/">PEAR</a>, das obwohl es zu &#8220;The PHP Group&#8221; gehört, eine vom Aussterben bedrohte Spezies ist. Wie auch immer, für alte Systeme sollte es dennoch nützlich sein. Sollte&#8230;</p>
<p><span id="more-23"></span>Für das Arbeiten mit PEAR haben die Amerikaner extra den Begriff &#8220;pain in the ass&#8221; geprägt (oder gab es den schon vorher und waren es gar die Engländer?). Mal fehlen Komponenten mal werden Klassen nicht gefunden, mal ist PEAR nicht auf dem Server installiert etc. pp&#8230;Ich denke jeder kennt das Dilemma, es ist nicht Fleisch und nicht Fisch.</p>
<p>Hier ist nun meine Lösung für das Versenden einer Mail mit Attachment und der Hilfe von PEAR. Es handelt sich um eine Zusammenstellung der benötigten PEAR Komponenten und einem Script das alles zusammenfügt. Die PEAR Scripte sind leicht modifiziert, ich musste einfach ein paar Pfade anpassen. Für Server bei denen der Versand über die native mail() Funktion von PHP nicht konfiguriert ist, was die absolute Ausnahme ist, habe ich SMTP Versand als Option mit eingebaut.</p>
<p><a href="http://www.php-log.de/wp-content/uploads/2009/01/pear-mail-with-attachments.zip"></a><a href="http://www.php-log.de/wp-content/uploads/2009/01/pear-mail-with-attachments.zip">PEAR Mailer mit Anhang</a></p>
<p>Hier nun das Demo Script für den Mailversand:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;display_errors&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FROM'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Ralf Kramer &lt;rk@belisar.de&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SUBJECT'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Happy Mail!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'USE_SMTP'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SMTP_HOST'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'mail.mustersample.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SMTP_USER'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'max@mustersample.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SMTP_PASSWORD'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'yoursecret'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'EMAIL_RECIPIENT'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'max@mustersample.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> send_mail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">require_once</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Pear/Mail.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// uncomment this line if php complainy about a missing nt_smtp class</span>
	<span style="color: #666666; font-style: italic;">#include_once (&quot;Pear/SMTP.php&quot;);
</span>
	<span style="color: #b1b100;">require_once</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Pear/Mail/mime.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'A happy little mail for you. Enjoy!'</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'A happy little mail for you. Enjoy!'</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$crlf</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'From'</span> <span style="color: #339933;">=&gt;</span> FROM<span style="color: #339933;">,</span> <span style="color: #0000ff;">'Subject'</span> <span style="color: #339933;">=&gt;</span> SUBJECT <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;sendme.txt&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$mime</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Mail_mime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$crlf</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$mime</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTXTBody</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$mime</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHTMLBody</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$mime</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAttachment</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'text/plain'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mime</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mime</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">headers</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$header</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   	<span style="color: #000088;">$smtp</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;host&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> SMTP_HOST<span style="color: #339933;">;</span>
    <span style="color: #000088;">$smtp</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;port&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;25&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$smtp</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;auth&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$smtp</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$smtp</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;timeout&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$smtp</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> SMTP_USER<span style="color: #339933;">;</span>
    <span style="color: #000088;">$smtp</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> SMTP_PASSWORD<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>USE_SMTP<span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$mail</span> <span style="color: #339933;">=&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #990000;">Mail</span><span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'smtp'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$smtp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span>
		<span style="color: #000088;">$mail</span> <span style="color: #339933;">=&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #990000;">Mail</span><span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mail'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">send</span><span style="color: #009900;">&#40;</span>EMAIL_RECIPIENT<span style="color: #339933;">,</span> <span style="color: #000088;">$header</span><span style="color: #339933;">,</span> <span style="color: #000088;">$body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Mail send.&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span>
		debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Mail sending failed.'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> debug<span style="color: #009900;">&#40;</span><span style="color: #000088;">$msg</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Status: 400 Bad Request&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$msg</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
send_mail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.php-log.de/23/php/e-mail-mit-anhang-uber-php-4-versenden/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

