Splitting up my Beanstalk Repository

June 30, 2011 – 10:51 am

Boy, it’s been a while since my last post (being busy is good I guess), but to get back into it I thought I share a nice moment from yesterday where something “just worked”. Yes, those moments are that rare! :)

When I started my first repository at Beanstalk almost 2 years ago I decided to keep all my client projects in the same repository, mostly for convenience and cost reasons as Beanstalk’s account levels are capped by number of repositories and I wanted to stick with a small (cheap) plan (who wouldn’t ;). So far it’s been a great ride and the one-repo approach has not caused any problems.

Well, yesterday came that day where one of my clients asked to play with his code.. I had dreaded that day a bit, because I thought that would mean giving access to other client’s code as well, seeing that Beanstalk doesn’t offer the same permissions-granularity that a self-hosted repo would offer (which is fine, their service is voluntarily simple and therefore solid). Now, after looking around for a little, and discovering Beanstalk’s export/import functionality it turned out I wasn’t in bad shape after all.. I simply exported the entire client repository to my laptop and ran it through the svndumfilter command to filter all file modifications pertaining to said client. All the required commands I found here: http://blogs.nuxeo.com/dev/2006/04/dump-load-svn-repositories-using-svnadmin-svndumpfilter.html (thanks much), and after this I had a nice small .svndump file that I could re-import into a new Beanstalk repo. Voila, another happy customer.


Filed under: Rnadom Sftuf — by Richtermeister

PHP Error: Trying to clone an uncloneable object of class ReflectionClass

August 11, 2009 – 7:35 am

I’ve recently run into this error when deploying symfony applications:

“Trying to clone an uncloneable object of class ReflectionClass”.

What’s causing this is a relatively common setting in php.ini, called “zend.ze1_compatibility_mode”, which enables php4 compatibility mode in php5. Specifically it affects a couple of object handling conventions (empty objects cast to FALSE, and - oh this is my favorite - objects are passed by value instead of by reference.. can you imagine how much fun debugging your php5 application would be if this setting didn’t actually save you by breaking things altogether?) Needless to say some hosts like to keep this turned on to me “more compatible” with the past. Also needless to say I hate some hosts.

The way around it is either changing the php.ini setting to:

zend.ze1_compatibility_mode=Off

or disabling it at runtime, before the application bootstraps, via:

ini_set("zend.ze1_compatibility_mode", "off");

Hope it helps someone.


Filed under: Rnadom Sftuf — Tags: — by Richtermeister

Secure Certificates and missing www

April 27, 2009 – 10:00 pm

Thanks to forgiving server-configurations it usually doesn’t matter whether you access a website via the fully qualified URL including “www” or without it - requests to http://codemassacre.com and www.codemassacre.com are taking you to the same place. After all you want to make it as easy as possible for people to access your website, so being forgiving is important.

However, as soon as you try to secure your site with help of a secure certificate and the corresponding https protocol, you need to pay closer attention, and, ultimately, make a choice as to which domain name the certificate applies to, because it will be either with www, or without.

Specifically, requesting https://some-domain.com for a site that only has a certificate for https://www.some-domain.com will cause your browser to raise a warning about an invalid certificate and block the site - certainly not a trust-invoking impression to give a potential customer! (unfortunately I am speaking first hand here, hehehe ;)

So, long story short, here’s an .htaccess entry that forces all requests to your site to have a www at the beginning:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^some-domain.com
RewriteRule (.*) http://www.some-domain.com/$1 [R=301,L]

What’s happening here is that  we’re watching the HTTP_HOST server variable for anything that starts with “some-domain.com”. The ^ signifies the beginning of a string. If this condition is fulfilled, we rewrite any request (.*) to the fully qualified url, and we append whatever was matched by the (.*) part - which goes into the $1 variable. Lastly, we set the R=301 flag to tell the client browsers that this redirect is permanent. The L is for good taste to make sure this is the last rule applied to this request (no point in applying any other rules..).

Now requests to https://some-domain.com will be redirected to https://www.some-domain.com, the certificate matches, and the browser is happy - and so is whoever visited your site, because as we all know “ignorance is bliss” ;)


Filed under: Rnadom Sftuf — Tags: , , — by Richtermeister

Converting MS-SQL to MySql (from .bak file or otherwise)

March 17, 2009 – 12:52 pm

What to expect:

Using this methodology, you’ll be able to move table definitions and the data contained within those tables. However, you will NOT move Views, Stored Procedures, Triggers, etc.

Views arrive as simple tables, so be prepared to recreate them by hand. Procedures don’t arrive at all, so same “manuality” applies.

These are the steps to convert an MS-SQL database to MySql:

First, you will need access to a machine that runs MS-SQL, so if all you have is a .bak file, you will first need to restore a database from it within MS-SQL before you can covert it.

(Skip this step if your DB already up  and running).
If you don’t have MS-SQL running on your machine, download SQL Server Management Studio Express, a free tool from Microsoft that allows you to work with MS-SQL databases.
After installing, open the program and see if you can connect to your local machine. In my case I couldn’t becauseI didn’t have all the required services running.

To start the services, go to “Control Panel -> Administrative Tools -> Services” and look for 2 services: “SQL Server”, and “SQL Server Browser”. Start those. Now SQLSMSE should let you connect.

Restore DatabaseTo restore a database from a .bak file, simply right-click on the Databases item in the Object Explorer, select “Restore Database” and follow the directions. Voila, you should end up with a running MS-SQL database.

Now for the actual conversion to MySql:

You will need to have a MySql ODBC connector installed on your machine, which you can get from http://dev.mysql.com/downloads/connector/odbc/5.1.htm
(You don’t have to register to get the driver!)

Once installed, you need to add a new data source to your machine. Go to “Control Panel -> Administrative Tools -> Data Source” and under User DSN click “Add” and select the newly installed MySql ODBC driver. Then point this to the target MySql database (if it doesn’t yet exist, create it first).

Great! So now we have a source database, a target database, and both can transfer data via ODBC. All that’s left to do is fire up the Import / Export Data Wizard that comes with MS-SQL and select the appropriate input/output settings, and convert.


Filed under: Rnadom Sftuf — by Richtermeister

IE 7 rejects my session for domains with “_”

December 9, 2008 – 4:38 pm

For this problem I want to bill Microsoft for an hour!
Here’s the gist: watch out when you use “_” in sub-domains.

Today I copied a website to a different subdomain on our dev-server, and I thought I add a “_bak” to the domain to tell things apart. All worked well, I thought, until my coworker tried to test the site using Internet Explorer 7 and found that she was unable to log in. No error either, just the same blank login form after submitting valid credentials.

Since all was working fine in Firefox, my first hunch smelled of cookies ;). And yup, IE rejected domain cookies from this sub-domain, and consequently session_start() generated a new session id for every request, effectively clearing the $_SESSION array which I was using for authentication.

Great Microsoft. Thanks for watching out for me. ,,|,,


Filed under: Rnadom Sftuf — by Richtermeister

static variables and caching

August 24, 2008 – 1:11 pm

If you’re working with classes a lot, you’ve probably come across the “static” keyword for properties and methods, enabling you to call these members straight on the class itself, without instantiating an object.


class UserPeer
{
protected static $userlist;

public static function getUserList()
{
return self::$userlist;
}
}
What’s a little less frequently encountered is a static variable inside a function (or a method for that matter). Say you have a function that returns something rather computing-intensive, but that’s called multiple times from different areas and you don’t want to re-compute every time, and you also don’t want to drag a global variable around.. then static is for you. Check this out:


function get_world_formula()
{
static $result;
if(!$result)
{
$result = //compute complex stuff here;
}
return $result;
}

Voila! A self-contained caching function. Obviously $result persists only for the duration of one request, but that’ll do just fine if it cuts down some CPU cycles. (It does add to the memory usage though, so be selective where you use it.) If you want to take this one step further, you can make caching optional by adding an extra parameter.


function get_world_formula($use_cache = true)
{
static $result;
if(!$result || !$use_cache)
{
$result = //compute complex stuff here;
}
return $result;
}

That’s it. Small but cute.

A word of OOP caution! I would not recommend using this approach in objects-methods, because this one static variable would be the same accross all object instances, so all objects would return the same data the first method call yielded, and this may not be appropriate for other objects. Use object properties instead to store chached results.


Filed under: Rnadom Sftuf — by Richtermeister

Problems Hosting Symfony sites on Godaddy?

July 24, 2008 – 5:11 pm

Please Note: There is an updated version of this post for Symfony 1.2

For all who are having problems getting a Symfony app to play nice with GoDaddy, here’s what you (most likely) need to tweak. First of all, I had to make the following entry in my settings.yml file:

path_info_key:          REDIRECT_URL

Then, and this was the real tricky part, I also needed to place a file called php5.ini at my site root, that contains the following line:

cgi.fix_pathinfo = 1

This tells the godaddy server to actually respect this front.php/somepage syntax. Otherwise this will throw a 404 error.

Hope this helps.


Filed under: Rnadom Sftuf — by Richtermeister

301 redirects - the tricky ones

June 14, 2008 – 9:54 pm

Here’s a tip for all who inherited or upgraded an existing site, and have to retain some crucial URLs. I generally hate dealing with this stuff, because it feels like polluting a nice new shiny project with legacy code that fulfills no other purpose than make your site work with outdated systems.. such as, err… Google! ;)
But then again, at the end of the day sites are only useful if you can find the information you’re looking for, and if the whole world is linking to your site already, we have to use redirects to make sure visitors continue to find the content they expect.

So on with it! Needless to say, the place to put redirects is the .htaccess file at your web root.

Now, for static content, redirects are straightforward - you simply write:

redirect 301 -from -to -flags (pseudo code)

for example:

redirect 301 /register.php /customer/join

Now, where it gets tricky is dynamic urls. Say you have an existing site with a product catalog, and your URLs look something like this:

/products/detail.php?product_id=31

While it is possible to redirect this via the same 301 redirect we used above, you would have to keep working with the same query string format, as a normal redirect would only appends it to the new url, resulting in, say, /newscript.php?product_id=31. However, what we really want to do here, is turn this legacy url into a nice sexy new-school url, such as /products/detail/31. In order to do that, we can’t just use redirects, we gotta do some url rewriting by defining a rewrite rule. Rewrite rules - nomen est omen - take a url and reformat it according to certain rules.

The resulting code is:

RewriteCond %{QUERY_STRING} product_id=([0-9]+) [NC]
RewriteRule ^products/detail.php /products/detail/%1? [R=301, L]

What’s happening here? Basically, our rewrite condition checks a server variable for a pattern. Server variables that are made available by apache are accessed via %{VARIABLE_NAME_HERE}, and in this case the variable we want is QUERY_STRING, but different circumstances could call for others, such as HTTP_REFERER, REQUEST_METHOD, DOCUMENT_ROOT, etc.

For a great cheat sheet on all the variables and flags you can use, go to http://www.ilovejackdaniels.com/cheat-sheets/mod_rewrite-cheat-sheet. It’s one of the best cheat-sheets I’ve ever seen since college ;)

Next, the pattern we check QUERY_STRING for is product_id=([0-9]+), as product_id is the name of the GET parameter the site was using to determine which product to show. In this case we know that this id is numeric, so we’re using a pattern that matches 1 or more digits (+ for 1 or more). Only if this condition is met, the following rewrite rule applies. Also, the first matched pattern is stored in a temporary variable %1. For subsequent matches the variable name is %2, %3 etc..

The rewrite rule itself simply rewrites /products/details.php into /products/detail, but we still need to stick the id into the url, so we append the previously matched pattern from the rewrite condition %1 and get /products/detail/%1?

Note that a period (.) has a special meaning in patterns, so you need to escape it via to say you mean a literal period.

Also note the flags we set on the condition and rule. The [NC] stands for “no case,” or “caSeInseNsitiVe.” Not required in this case, but you never know..

The R=301 tells the requesting browser to interpret this redirect as a 301 permanent redirect, and the L states that this is the Last rule that should be applied to this url. This may or may not apply to your case.. nothing prevents you from applying further rules below, in which case you need to remove the L.

That’s it, good rewriting!


Filed under: Rnadom Sftuf — Tags: , , , , , , — by Richtermeister

Outputting Data in Excel .xls format

January 22, 2008 – 5:51 pm

All clients love data! The only thing they like more than data is data in a spreadsheet, and that’s where we would be in all sorts of trouble if we had to rely on special export libraries that in turn have to keep track with the dataformat - whims of Microsoft. Thankfully there’s a more durable solution, so your application will still be happily exporting in the future.

So here’s how you can quickly spit out a table for Excel:

(more…)


Filed under: Rnadom Sftuf — by Richtermeister

PHP Short Opening Tag not supported… null problemo!

January 20, 2008 – 9:28 pm

Here’s another config - particularity of PHP, right up there with my beloved register_globals run-ins and $_REQUEST access issues..
Recently I’ve had to deploy a site on a server that had short opening tags disabled, and no access to the php.ini. Wow, didn’t see that coming.

Now, for my scripts that didn’t really mean a thing, since I habitually start a multi-line php code block with <?php, however, I do like using <?= within my “templates” (or view files), rather than <?php echo, because, well it keeps things “template-ier” IMHO. Almost as short as the smarty tag, eh? ;)

So what to do avoid string replacing the hell out of your front-end?

(more…)


Filed under: Rnadom Sftuf — by Richtermeister
Older Posts »

Powered by WordPress