<?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>Rob Rosenbaum&#039;s Development Blog &#187; PHP</title>
	<atom:link href="http://robrosenbaum.com/tags/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://robrosenbaum.com</link>
	<description>PHP, JavaScript, and Other Things</description>
	<lastBuildDate>Mon, 12 Mar 2012 21:51:40 +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>HOWTO: Disable Session Timeout in Symfony</title>
		<link>http://robrosenbaum.com/php/howto-disable-session-timeout-in-symfony/</link>
		<comments>http://robrosenbaum.com/php/howto-disable-session-timeout-in-symfony/#comments</comments>
		<pubDate>Wed, 16 Jan 2008 14:10:10 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://robrosenbaum.com/php/howto-disable-session-timeout-in-symfony/</guid>
		<description><![CDATA[Tell Symfony to expire sessions when the browser closes.]]></description>
			<content:encoded><![CDATA[<p>The problem: make symfony use PHP&#8217;s default session behavior.<br />
Symfony session handling is based on timed sessions &#8211; you set a time<br />
after which the session will expire &#8211; but the default PHP behavior is to<br />
 expire when the user closes the browser window. This is also the most<br />
commonly desired behavior for sessions. The drawback of using a timed<br />
session is that it could expire while the user is still on the site.<br />
This is an issue in <a href="http://web.archive.org/web/20091218091208/http://www.lecturetools.org/">LectureTools</a>,<br />
 because students may sit for an hour or more without loading a new<br />
page, and they may even have logged in before class. You can solve this<br />
little problem easily if you are using symfony 1.1 (currently in beta).<br />
Just set the timeout to <code>false</code> in settings.yml:</p>
<pre class="YAML">all:
  .settings:
    timeout:    false
</pre>
<p>But if you are using the stable 1.0 branch of symfony, this will do just<br />
 the opposite &#8211; it will make your sessions time out immediately! To make<br />
 this work, we have to override <code>sfBasicSecurityUser</code>&#8216;s <code>initialize()</code> method. I will describe two ways<br />
to do this; one is a short hack, the other is longer and somewhat more proper.</p>
<p class="leadingnoindent">Here&#8217;s the short way:</p>
<pre class="PHP">class myUser extends sfBasicSecurityUser
{
  public function initialize($context, $parameters = null)
  {
    if (sfConfig::get('sf_timeout') == 0) {
      // session will expire if window is open for a day
      sfConfig::set('sf_timeout', 86400);
    }

    return parent::initialize($context, $parameters);
  }
}
</pre>
<p>This works because the <code>sf_timeout</code> setting is not used by<br />
any other class (at the moment), so changing it doesn&#8217;t have any side<br />
effects. If you set your timeout to 0 or false, symfony will always<br />
think you have set the session timeout to 24 hours, but the session will<br />
 actually expire when the user closes the browser window.<br />
If you are not comfortable with &#8220;hacks&#8221; like the above, you could copy <code>the initialize()</code> method from<br />
<code>sfBasicSecurityUser</code>, change it in the appropriate place, and skip calling the parent constructor altogether.<br />
Unfortunately, <code>sfBasicSecurityUser</code> calls <em>its</em> parent constructor, so you have to rewrite them both:</p>
<pre class="php">class myUser extends sfBasicSecurityUser
{
  public function initialize($context, $parameters = null)
  {
    $this-&gt;context = $context;

    $this-&gt;parameterHolder = new sfParameterHolder();
    $this-&gt;parameterHolder-&gt;add($parameters);

    $this-&gt;attributeHolder = new sfParameterHolder(self::ATTRIBUTE_NAMESPACE);

    // read attributes from storage
    $attributes = $context-&gt;getStorage()-&gt;read(self::ATTRIBUTE_NAMESPACE);
    if (is_array($attributes))
    {
      foreach ($attributes as $namespace =&gt; $values)
      {
        $this-&gt;attributeHolder-&gt;add($values, $namespace);
      }
    }

    // set the user culture to sf_culture parameter if present in the request
    // otherwise
    //  - use the culture defined in the user session
    //  - use the default culture set in i18n.yml
    if (!($culture = $context-&gt;getRequest()-&gt;getParameter('sf_culture')))
    {
      if (null === ($culture = $context-&gt;getStorage()-&gt;read(self::CULTURE_NAMESPACE)))
      {
        $culture = sfConfig::get('sf_i18n_default_culture', 'en');
      }
    }

    $this-&gt;setCulture($culture);

    // read data from storage
    $storage = $this-&gt;getContext()-&gt;getStorage();

    $this-&gt;authenticated = $storage-&gt;read(self::AUTH_NAMESPACE);
    $this-&gt;credentials   = $storage-&gt;read(self::CREDENTIAL_NAMESPACE);
    $this-&gt;lastRequest   = $storage-&gt;read(self::LAST_REQUEST_NAMESPACE);

    if ($this-&gt;authenticated == null)
    {
      $this-&gt;authenticated = false;
      $this-&gt;credentials   = array();
    }
    else
    {
      // Automatic logout logged in user if no request within [sf_timeout] setting
      if (0 != sfConfig::get('sf_timeout') &amp;&amp; null !== $this-&gt;lastRequest &amp;&amp; (time() -
$this-&gt;lastRequest) &gt; sfConfig::get('sf_timeout'))
      {
        if (sfConfig::get('sf_logging_enabled'))
        {
          $this-&gt;getContext()-&gt;getLogger()-&gt;info('{sfUser} automatic user logout due to timeout');
        }
        $this-&gt;setTimedOut();
        $this-&gt;setAuthenticated(false);
      }
    }

    $this-&gt;lastRequest = time();

  }
}
</pre>
<p>That should do it!</p>
]]></content:encoded>
			<wfw:commentRss>http://robrosenbaum.com/php/howto-disable-session-timeout-in-symfony/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>sfModelTestPlugin &#8211; Simple unit tests for any ORM!</title>
		<link>http://robrosenbaum.com/php/sfmodeltestplugin-simple-unit-tests-for-any-orm/</link>
		<comments>http://robrosenbaum.com/php/sfmodeltestplugin-simple-unit-tests-for-any-orm/#comments</comments>
		<pubDate>Wed, 14 Nov 2007 19:02:31 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://robrosenbaum.com/uncategorized/sfmodeltestplugin-simple-unit-tests-for-any-orm/</guid>
		<description><![CDATA[Easy unit testing with model objects and a test database with sfModelTest plugin. Propel, Doctrine, and Propel 1.3 are supported.]]></description>
			<content:encoded><![CDATA[<p>I have redone the <a href="http://trac.symfony-project.com/wiki/sfPropelTestPlugin">sfPropelTestPlugin</a>, and it is now <a href="http://trac.symfony-project.com/wiki/sfModelTestPlugin">sfModelTestPlugin</a>, with support for Doctrine and Propel 1.3! Also new &#8211; you can now specify the test data file/directory at runtime, allowing for more flexibility with your tests. I want to thank Anders Betnér and my readers for bug reports and patches!</p>
]]></content:encoded>
			<wfw:commentRss>http://robrosenbaum.com/php/sfmodeltestplugin-simple-unit-tests-for-any-orm/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>New symfony plugin &#8211; sfPropelTestPlugin simplifies unit tests</title>
		<link>http://robrosenbaum.com/php/new-symfony-plugin-sfpropeltestplugin-simplifies-unit-tests/</link>
		<comments>http://robrosenbaum.com/php/new-symfony-plugin-sfpropeltestplugin-simplifies-unit-tests/#comments</comments>
		<pubDate>Thu, 13 Sep 2007 12:59:47 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://robrosenbaum.com/symfony/new-symfony-plugin-sfpropeltestplugin-simplifies-unit-tests/</guid>
		<description><![CDATA[Easy unit testing with Propel objects and a test database with sfPropelTestPlugin.]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever tried to write a symfony unit test for a model class &#8211; or for any piece of code that interacts with the database &#8211; then you know what a headache it can be. <a href="http://trac.symfony-project.com/trac/wiki/sfPropelTestPlugin">sfPropelTestPlugin</a> to the rescue! This plugin loads all the necessary symfony components for database interaction, and goes a step further: test data is automatically reloaded to the test database at the beginning of each test case, so you don&#8217;t have to worry about your tests interacting with each other. Here&#8217;s an example of a unit test written with <a href="http://trac.symfony-project.com/trac/wiki/sfPropelTestPlugin">sfPropelTestPlugin</a>:</p>
<pre class="php">
class myUnitTest extends sfPropelTest
{
  public function setup()
  {
    $this->bob = UserPeer::retrieveByPK('bob');
  }

  public function teardown()
  {
    $this->diag('Test method complete!');
  }

  public function test_user()
  {
    $this->is($this->bob->getId(), 'bob', 'Bob exists!');

    $user = new User();
    $user->setFirstName('Joe');
    $user->setLastName('Smith');
    $user->setUsername('joe');
    $user->save();

    $joe = UserPeer::getBy(UserPeer::USERNAME, 'joe');
    $this->isa_ok($joe, 'User', 'Joe exists!');
  }

  public function test_dataDelete()
  {
    $this->is($this->bob->getId(), 'bob', 'Bob still exists!');

    $joe = UserPeer::getBy(UserPeer::USERNAME, 'joe');
    $this->ok(!$joe, 'Joe no longer exists.');
  }
}

$test = new myUnitTest();
$test->execute();
</pre>
<p class="leadingnoindent">If you&#8217;ve written tests in Ruby on Rails, the above should look familiar. If not, let&#8217;s go through the code:</p>
<p>The first two lines are setup: The first line tells symfony what application to test &#8211; in this case, &#8220;myApp&#8221; &#8211; and the second line includes the code we need to test against the database. Next we define our &#8220;test case&#8221; &#8211; a class that extends <code class="php">sfPropelTest</code>, which is itself a child of <code class="php">lime_test</code>. Every method that begins with &#8220;test_&#8221; will be called in turn, with our test data being reloaded between each call. Also, the <code class="php">setup()</code> method, if defined, will be called immediately before each test method, and the <code class="php">teardown()</code> method, if defined, will be called immediately after. The last two lines create a new instance of our test suite, and call its <code class="php">execute()</code> method, which runs our tests.</p>
<p>There you have it &#8211; a quick and painless way to run unit tests in symfony. One less excuse for not writing them!</p>
]]></content:encoded>
			<wfw:commentRss>http://robrosenbaum.com/php/new-symfony-plugin-sfpropeltestplugin-simplifies-unit-tests/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Improved Plugin for Model Validation: sfPropelValidatePlugin</title>
		<link>http://robrosenbaum.com/php/improved-plugin-for-model-validation-sfpropelvalidateplugin/</link>
		<comments>http://robrosenbaum.com/php/improved-plugin-for-model-validation-sfpropelvalidateplugin/#comments</comments>
		<pubDate>Mon, 10 Sep 2007 01:08:37 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://robrosenbaum.com/symfony/improved-plugin-for-model-validation-sfpropelvalidateplugin/</guid>
		<description><![CDATA[Put your Symfony validation in the model, where it belongs, with sfPropelValidate plugin. New version is more efficient.]]></description>
			<content:encoded><![CDATA[<p>I have updated my plugin for doing <a href="http://trac.symfony-project.com/trac/wiki/sfPropelValidatePlugin">model-based validation in symfony</a>. The new plugin uses the Propel builders instead of behaviors, so it is now called <a href="http://trac.symfony-project.com/trac/wiki/sfPropelValidatePlugin">sfPropelValidatePlugin</a> instead of the former (unwieldy) name sfPropelValidateBehaviorPlugin. If you have not investigated the Propel builder classes, I suggest you give them a look. By overriding the default classes used to build your Propel object and peer classes, you can write your modifications directly into your model&#8217;s base classes. This is a much cleaner and more readable solution than using Propel behaviors, and you avoid the performance hit that behaviors incur. </p>
<p>The default Propel builders can be found in the symfony source in these places:</p>
<pre class="filesystem">
symfony/addon/propel/builder/sfObjectBuilder.php
symfony/addon/propel/builder/sfPeerBuilder.php
</pre>
<p class="leadingnoindent">The classes these inherit from are buried. They can be found here:</p>
<pre class="filesystem">
symfony/vendor/propel-generator/classes/propel/engine/builder/om/php5/PHP5ComplexObjectBuilder.php
symfony/vendor/propel-generator/classes/propel/engine/builder/om/php5/PHP5ComplexPeerBuilder.php
</pre>
]]></content:encoded>
			<wfw:commentRss>http://robrosenbaum.com/php/improved-plugin-for-model-validation-sfpropelvalidateplugin/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using Flash Upload with PHP &amp; symfony</title>
		<link>http://robrosenbaum.com/php/using-flash-upload-with-php-symfony/</link>
		<comments>http://robrosenbaum.com/php/using-flash-upload-with-php-symfony/#comments</comments>
		<pubDate>Tue, 04 Sep 2007 22:50:09 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://robrosenbaum.com/flash/using-flash-upload-with-php-symfony/</guid>
		<description><![CDATA[Using Flash-based multi-file uploads with session cookies.]]></description>
			<content:encoded><![CDATA[<p>Through Flash&#8217;s <a href="http://livedocs.adobe.com/flex/201/langref/flash/net/FileReference.html"><code class="ActionScript">FileReference</code></a> and <a href="http://livedocs.adobe.com/flex/201/langref/flash/net/FileReferenceList.html"><code class="ActionScript">FileReferenceList</code></a> classes, you  can create a powerful file uploader that allows the user to upload multiple files with a single form element. But beware: unless the user happens to be using IE, the <code class="ActionScript">upload</code> will use a new browser session to upload the files. This means that if you require that a user be authenticated before uploading something (and you better be), the upload won&#8217;t work &#8211; the request will be forwarded to the login form, or to wherever your system forwards unauthorized requests. This is a maddening bug to track down, and there is nothing you can do to make Flash use the right session. The work around is to send the session cookie in the url and, on the server side, use that to override the new (and wrong) session cookie sent by Flash. Here&#8217;s how that works in symfony, although much of the following is useful for other languages or frameworks:</p>
<p>First, we have to tell our Flash component what the cookie is, so it can roll it into the URL. One solution would be to pass the session cookie as an argument in <a href="http://www.permadi.com/tutorial/flashVars/">FlashVars</a>, but then the user&#8217;s session cookie is sitting unencrypted in the HTML, leaving them vulnerable to <a href="http://shiflett.org/articles/cross-site-request-forgeries">cross-site request forgery</a>. Better to use javascript to fetch the cookie, and <em>then</em> put it in FlashVars. If you are using <a href="http://www.bobbyvandersluis.com/ufo/">ufo.js</a>, the first argument to <code class="javascript">UFO.create()</code> should look like this:</p>
<pre class="javascript">
{
  movie: '/flash/uploader.swf',
  id: 'uploader',
  name: 'uploader',

  flashvars: 'cookie=' + document.cookie,

  // other options ...
}
</pre>
<p>Note that if you are dealing with multilple cookies, you will want to parse the output of <code class="javascript">document.cookie</code> and send only the desired cookie.</p>
<p class="leadingnoindent">Next, add the cookie to the URL in your ActionScript (this is for ActionScript 2.0):</p>
<pre class="ActionScript2">
var list:Array = myFileRefList.fileList;
var item:FileReference;
var url:String = _root.uploadURL + '?cookie=' + _root.cookie;

for (var i:Number = 0; i < list.length; i++) {
  item.upload (url);
}
</pre>
</p>
<p class="leadingnoindent">That does it for the client side. Now, we need to tell PHP to use our cookie instead of the one the browser sent. This does the trick:</p>
<pre class="PHP">
list($cookieName, $cookieValue) = str_split('=', $_GET['cookie']);

session_name($cookieName);
session_id($cookieValue);

session_start();
</pre>
<p>If you are using symfony, things are a little more complex. We need to call <code class="PHP">session_name()</code> and <code class="PHP">session_id()</code> <em>before</em> <code class="PHP">session_start()</code>; looking at the symfony source code, we find that <code class="PHP">session_start()</code> is called in <code class="PHP">sfSessionStorage::initialize()</code>. So a simple solution is to extend the sfSessionStorage class:</p>
<pre class="PHP">
class mySessionStorage extends sfSessionStorage
{
  public function initialize($context, $parameters = null)
  {
    if ( /* whatever the condition is when we want to do this */ ) {
      if ($cookie = $context->getRequest()->getParameter('cookie')) {
        $name = 'symfony';
        preg_match('/^' . $name.'=(.*)$/', $cookie, $asMatch);
        $value = $asMatch[1];

        session_name($name);
        session_id($value);
      }
    }

    parent::initialize($context, $parameters);
  }
}
</pre>
<p>Finally, tell symfony to use the mySessionStorage class by editting factories.yml:</p>
<pre class="YAML">
all:
  storage:
    class: mySessionStorage
    param:
      session_name: symfony
</pre>
<p>And you're done! Clear your cache, and try it out. An easy way to check if it's working is to add the following line to the end of index.php (or frontend.php):</p>
<pre class="PHP">file_put_contents('testSessionOverwrite.txt', sfContext::getInstance()->getRequest()->getActionName());</pre>
<p>Now try uploading a file. If the action name in testSessionOverwrite.txt is the action you're uploading to, you're golden. If instead it is the name of the action that authenticates users, you have a problem somewhere, and you get to have the enjoyable experience of debugging PHP without browser output. Remember to erase the debugging line from your front controller when you get everything working.</p>
<p>Next time, just make the users upload their damn files one-at-a-time...</p>
]]></content:encoded>
			<wfw:commentRss>http://robrosenbaum.com/php/using-flash-upload-with-php-symfony/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

