<?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>Yet another web log &#187; PHP</title>
	<atom:link href="http://blog.philippheckel.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.philippheckel.com</link>
	<description>Life, Linux and other things</description>
	<lastBuildDate>Thu, 17 Mar 2011 10:04:42 +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>Roundcube login via PHP script</title>
		<link>http://blog.philippheckel.com/2008/05/16/roundcube-login-via-php-script/</link>
		<comments>http://blog.philippheckel.com/2008/05/16/roundcube-login-via-php-script/#comments</comments>
		<pubDate>Fri, 16 May 2008 00:44:04 +0000</pubDate>
		<dc:creator>Philipp C. Heckel</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Mail]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Roundcube]]></category>
		<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://blog.philippheckel.com/2008/05/16/roundcube-login-via-php-script/</guid>
		<description><![CDATA[Roundcube is an AJAX/PHP based e-mail application which is really flexible and easy to use in comparison to other free web based solutions. For the customer interface of Silversun, I wanted to use RC as the internal web mail application and therefore had to embed it into my system. To avoid that the customer has [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.roundcube.net/">Roundcube</a> is an AJAX/PHP based e-mail application which is really flexible and easy to use in comparison to other free web based solutions. </p>
<p>For the customer interface of <a href="http://www.silversun.de/">Silversun</a>, I wanted to use RC as the internal web mail application and therefore had to embed it into my system. To avoid that the customer has to log in twice (customer interface and Roundcube), I had to simulate the login request with a PHP script.</p>
<p><span id="more-4"></span></p>
<h3 id="toc-updates">Updates</h3>
<p><strong>November 2008</strong>: After the comment of <a href="#comment-1171">Matias</a>, I reviewed the code and fixed some issues. Now it should work properly even with the newest Roundcube version (0.2-beta). The class file itself contains installation instructions. Please read them carefully.</p>
<p><strong>March 2009</strong>: Just tested the script with version 0.2.1 and it works like a charm, at least for my installation.</p>
<p><strong>December 2009</strong>: <a href="#comment-13861">Diego</a> just confirmed (via e-mail) that the script also works for 0.3.1 without modification. </p>
<p><strong>May 2010</strong>: I just tested the scripts with Roundcube 0.4-beta, and it still works without modification. I also added the section <a href="#Debugging">Debugging</a> make it easier to figure out what&#8217;s wrong.</p>
<p><strong>March 2011</strong>: After <a href="#comment-19140">Alex&#8217; comment</a>, I adjusted a small part of the script. It should now also work with Roundcube 0.5.1. It now handles the new request token correctly. The pre-0.5.1 script is still available for download here: <a href="/uploads/2008/05/RoundcubeLogin.pre-0.5.1.class.phps">RoundcubeLogin.pre-0.5.1.class.php</a></p>
<h3 id="toc-prepare-rc">Prepare RC</h3>
<p>To perform the Roundcube login via a web site, it is necessary to turn off the <em>check_ip/ip_check</em> option in the <em>main.inc.php</em> file, because our script (= server IP address) will send the login data and pass it to RC instead of the user&#8217;s browser (= user IP address). </p>
<h3 id="toc-the-roundcubelogin-class">The RoundcubeLogin class</h3>
<p>This small class only consists of four functions and it shouldn&#8217;t be necessary to modify it in order to get the login to work.</p>
<ul>
<li><a href="http://blog.philippheckel.com/uploads/2008/05/RoundcubeLogin.class.phps">RoundcubeLogin.class.php</a><br />Provides the functionality to login, logout and check the login status.</li>
<li><a href="http://blog.philippheckel.com/uploads/2008/05/rclogin.phps">rclogin.php</a><br />A small script to test if everything works as expected.</li>
</ul>
<p>The class provides four public methods:</p>
<ul>
<li>
  <strong>login($username, $password)</strong><br />
  Perform a login to the Roundcube mail system.<br />
  <u>Note</u>: If the client is already logged in, the script will re-login the user (logout/login). To prevent this behaviour, use the <em>isLoggedIn()</em>-function.<br />
  <u>Returns</u>: <em>TRUE</em> if the login suceeds, <em>FALSE</em> if the user/pass-combination is wrong<br />
  <u>Throws</u>: May throw a <em>RoundcubeLoginException</em> if Roundcube sends an unexpected answer (that might happen if a new Roundcube version behaves differently)
</li>
<li>
  <strong>isLoggedIn()</strong><br />
  Checks whether the client/browser is logged in and has a valid Roundcube session.<br />
  <u>Returns</u>: <em>TRUE</em> if the user is logged in, <em>FALSE</em> otherwise.<br />
  <u>Throws</u>: May also throw a <em>RoundcubeLoginException</em> (see above).
</li>
<li>
  <strong>logout()</strong><br />
  Performs a logout on the current Roundcube session.<br />
  <u>Returns</u>: <em>TRUE</em> if the logout was a success, <em>FALSE</em> otherwise.<br />
  <u>Throws</u>: May also throw a <em>RoundcubeLoginException</em> (see above).
</li>
<li>
  <strong>redirect()</strong><br />
  Simply redirects to Roundcube.
</li>
</ul>
<h3 id="toc-sample-usage">Sample usage</h3>
<p>The script below demonstrates how the class can be used. If the client is already logged in, it simply redirects the browser to the Roundcube application. If not, it performs a login and then redirects to Roundcube.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;RoundcubeLogin.class.php&quot;</span><span style="color: #339933;">;</span>	
&nbsp;
<span style="color: #666666; font-style: italic;"># Create RC login object.
</span><span style="color: #666666; font-style: italic;"># Note: The first parameter is the URL-path of the RC inst.,
</span><span style="color: #666666; font-style: italic;">#       NOT the file-system path
</span><span style="color: #666666; font-style: italic;"># e.g. http://host.com/path/to/roundcube/ --&gt; &quot;/path/to/roundcube&quot;
</span><span style="color: #000088;">$rcl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RoundcubeLogin<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/roundcube/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$debug</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
try <span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;"># If we are already logged in, simply redirect
</span>   <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rcl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isLoggedIn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$rcl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;"># If not, try to login and simply redirect on success
</span>   <span style="color: #000088;">$rcl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">login</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;some-email-address&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;plain-text-password&quot;</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;">$rcl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isLoggedIn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$rcl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;"># If the login fails, display an error message
</span>   <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ERROR: Login failed due to a wrong user/pass combination.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch <span style="color: #009900;">&#40;</span>RoundcubeLoginException <span style="color: #000088;">$ex</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;ERROR: Technical problem, &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$ex</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$rcl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dumpDebugStack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3 id="Debugging" >Debugging</h3>
<p>If you&#8217;re having problems with the <a href="/uploads/2008/05/RoundcubeLogin.class.phps">RoundcubeLogin.class.php</a> class itself, try using the <a href="/uploads/2008/05/rclogin.phps">rclogin.php</a>-file for debugging: open the file in your browser (http://myhost/roundcube/rclogin.php), and take a look at the output. The <tt>RoundcubeLogin</tt>-class performs a series of request/response cycles and parses the output to figure out if you&#8217;re logged in. </p>
<p>Known issues:</p>
<ol>
<li><b>No Roundcube installation found at &#8216;&#8230;&#8217;</b><br />
   This error message is thrown if the path-value in the <tt>RoundcubeLogin</tt> constructur was not set correctly. It must be set to the part of the URL that represents the path, e.g. in case of http://myhost/roundcube/ you must create the object like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$rcl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RoundcubeLogin<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/roundcube/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li><b>Unable to determine login-status due to technical problems.</b><br />
  This error can occur in the methods login(), logout() and isLoggedIn(). The <tt>RoundcubeLogin</tt>-class expects Roundcube to send certain headers in response to the login/logout-requests. If those headers could not be found, this error is thrown. Possible reasons are: </p>
<ul>
<li>New RC version</li>
<li>Cookies must be enabled</li>
<li><em>ip_check/check_ip</em> option in the <em>main.inc.php</em> must be <tt>false</tt></li>
</ul>
</li>
<li><b>Unable to determine the login status. Unable to continue due to technical problems.</b><br />
  This error occurs if the script cannot determine if you are logged in or not, because the returned HTML code neither contains the login-form (= logged out) nor the message DIV (= logged in). This might happen if Roundcube changed the HTML-code.</li>
</ol>
<h3 id="toc-im-open-for-suggestions">I&#8217;m open for suggestions</h3>
<p>Please feel free to post your comment or suggestions. That&#8217;s the only way to ensure that it works with all versions.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.philippheckel.com/2008/05/16/roundcube-login-via-php-script/feed/</wfw:commentRss>
		<slash:comments>76</slash:comments>
		</item>
	</channel>
</rss>

