Facebook PHP Source Code from August 2007
gist.github.com
gist.github.com
In 2008 studiVZ was sued by Facebook. They said we theft their PHP, CSS/JS "code". (http://techcrunch.com/2008/07/18/facebook-sues-german-social...).
Indeed, the first version of studiVZ was inspired by Facebook. Our founder had seen FB in 2005 the first time in the US, but it wasn't possible to use the service without a .edu eMail account. That was the decision to make a local clone.
But the real story behind the lawsuit is a bit longer. In december 2006 Facebook tried to acquire studiVZ the very first time.
This picture with two of the studiVZ founder and some people from FB's management team is from this time:
http://img-a4.pe.imagevz.net/photo3/f3/9a/ce37499d84437cb744...
But FB hadn't the amount of cash to aquire the company. The Holtzbrinck Publishing Group aquired studiVZ later for 85 mio. €
In 2008 (before the lawsuit) FB tried the second time to acquire studiVZ - this time for 4,8% of shares! But this deal didn't happend - unfortunately.
I didnt really like it, they basically did nothing in terms of innovation that FB did, it had the same features for years. Later they had a half-assed app platform that never really took off. It was just a blatant rip-off like so many german startups, sadly.
Some numbers from 2010:
- 60.000 req/s (without static content)
- 2.5 mio. memcache ops/s
- 300k queries to the database tier
We've built that platform with a team of around 20-30 engineers and we had to solve the scaling stuff first - before we could add new features to the platform.
Today it's easy to store 3 billion files (photos). You could easily use S3, but 2007 these services didn't exist or most of the cloud services just started with their services.
studiVZ wasn't cool anymore. We missed the point for a rebrand to open the plattform for other people. "studi" is a abbreviation for student. Our answer was a new brand called meinVZ (just another UI, same database). Together with the ability to provide new cool features (e.g. Apps, Activity stream, i18n) it was just a question of time until FB was getting the market leadership.
One time I clicked it and got the source code to the file instead of the graph. It's somewhere on one of my hard drives, but it seems wrong to leak, especially since it has database credentials hard-coded into it.
Could you perhaps censor the credentials and release it?
Any Facebook employees want to get me a green light? :)
// NOTE: ok, at this point we know we are going to display the full // page, so it is time to do a PHATTY PHATTY MULTIGET of all the shit that // we are going to need to make this page, or at least the most common things
// Clear fire if desired
// make sure big tunas haven't moved aroundThere are situations that require using somewhat surprising language features occasionally, but quality code will limit them to small areas, and document the hell out of what's going on there.
There is a whole world out there where people want to rely on software to do what it says it does. I know Facebook can live in its own bubble and get away with every possible stupid bug a messy PHP spaghetti causes.
Personally, I can't bring myself to not care like that, but it seems to have worked pretty well in the early days of many now-popular sites. Especially in 2007, when Facebook was still in real competition with MySpace, moving as quickly as possible was probably much more important than a few messages dropping through the cracks.
Also, when we say "important" in this context, is almost like asking "are you willing to pay for it?". Most people wouldn't pay a dime for ensuring the consistency of all their Facebook messages. So Facebook chose the right option.
My opinion is that you can have both performance and consistency, and work your way toward scalability as required. I recall that Facebook has a very high server-per-engineer ratio (though I acknowledge that the user-per-engineer ratio is even higher in comparison to other startups).
I also realize it is not cost-efficient to write bugfree software, but saying they did it the other way on purpose is forgiving them too much. You never write bad software intentionally.
They didn't care, and the world should have relied and should continue to rely on better software.
Many programming methodologies are proposed these days, but the entire "field" smells of pseudo-science. Unless studies are done that can show statistically significant differences in relevant metrics (defect density, time required to add a feature, etc.), it's just a matter of opinion.
That's a dangerous argument. I mean, if startups stopped pursuing the most elegant code, they wouldn't need, nor would they be able to hire, the "best" developers. And then what?
1. Lots of startups would be able to get off the ground and grow with less cash because they wouldn't have to pay six figures to every developer on staff.
2. It would be a lot easier to find acceptable candidates (no more "you have five minutes to implement sort on a whiteboard...to prove that you can build a web form" challenges during interviews).
3. There would be a lot less premature optimization and use of the framework or NoSQL database du jour.
4. Egos would be hurt as engineers with the most impressive educational backgrounds and deepest technical expertise would be forced to come to grips with the fact that, while they are valuable and have a big role to play at some companies, people with less formal training and knowledge can build working, commercially-viable CRUD applications.
This would be the end of the Silicon Valley startup world as it exists today, I tell you!
* Reaction A: code is ugly, what a bunch of jerks!
* Reaction B strikes me a bit as hero worship. Since we already know the outcomes, already some consider them geniuses; we must conclude every decision they made was a good one, and there is no room for criticism.
Perhaps neither are great, but I think reaction B especially is a little dangerous. One must acknowledge that there are more factors to your success than coding style, but that doesn't mean if you start neglecting it then it will also lead to your success.
How about the possibility that a1a is one of the people who just automatically equates "large volume of code" with "spaghetti"? In my experience there is a fair number of HN users (and devs in general) with this knee-jerk perspective.
In my very humble opinion any reasonable person who takes a few minutes to actually read through this code would never call it spaghetti. _Especially_ considering 1) the feature set of PHP at the time the code was leaked, 2) the immense scale of Facebook even at that time.
Like I posted - just below - a day before your post: "My post was not meant to trash the code, but rather to point out that this code shows us that the primary objective shouldn't necessarily be writing the perfect code."
Shipped code > Well architected incomplete features.
Your user does not care in the slightest if you're using a design pattern, or if you are using dependancy injection, or if there is 100% code coverage. Just make it work! Then make it faster! Then make it more readable! In that order.
Shipping is a feature.
I'm all for tests but tests aren't the ONLY way to write software that you can modify without "breaking things." It does require more time to develop and test, but then again, you're saving some time by not writing and maintaing tests.
Again, I believe in automated testing because I think it hedges my risk but I'd caution against believing your own hype that there is only one way to do something..
What are some other methodologies that can let me change code with the confidence well-written tests give me? Would love to be able to employ them when automated tests aren't feasible.
I always took this as an argument for smaller, composable components that value simplicity over complexity, dumb over smart and composable over monolithic. I personally (not quite there yet!) try to write code in this manner. I find that the more straight forward and simple the code, the less need there is for tests because I can hold the whole of the code base in my head.
Actually there is more wisdom hidden in this dogma. I'd argue you can't really well architect anything without iterating running code. So, it's more of a cycle. At least in my case. Proto, use, profile, refactor, proto new stuff into it, use, profile, refactor...
Sometimes (not always), it's best to release when ready. Shipping is important for a company, but for a dev team/individual it should be further down on the list and more importantly, development is not done after the first release and features don't maintain themselves.
I don't like the whole 'ship as soon as possible'-thing.
Shipping quickly is important but it's also important to write quality code. Small bugs that can easily be fixed are fine but security problems or bugs related to payments, for example, are not.
These are not figures you want to see on your bottom line.
I think the unspoken secret here is the probability*loss for security issues is far less than the cost of missing features / delays.
We seem to be doing all right.
// TODO:
The best I've seen are (when reading DNA translated in all frames):
EVQLVE
LAMARCK
ELVISISGAY
SALTYSATAN
Someone recently mocked a full-stack-developer on HN as "I can javascript and servers too..". Oh man, how it made me cringe...In another 10 years, may be I won't have to. Off to dreaming...and coding...where is google....and stackoverflow of course...
Anyhow, not sure if this makes any difference or not but i'm curious as to why true PHP constants are not used, and instead regular variables in all caps like $PARAM_INT are used. Anyone know why this might be?
I ask because one of you PHP-gurus might inform me that there are certain use-cases where a true constant is not wise.
An example below:
param_get_slashed(array(
'feeduser' = > $PARAM_INT, //debug: gets feed for user here
'err' = > $PARAM_STRING, // returning from a failed entry on an orientation form
'error' = > $PARAM_STRING, // an error can also be here because the profile photo upload code is crazy
'ret' = > $PARAM_INT, 'success' = > $PARAM_INT, // successful profile picture upload
'jn' = > $PARAM_INT, // joined a network for orientation
'np' = > $PARAM_INT, // network pending (for work/address network)
'me' = > $PARAM_STRING, // mobile error
'mr' = > $PARAM_EXISTS, // force mobile reg view
'mobile' = > $PARAM_EXISTS, // mobile confirmation code sent
'jif' = > $PARAM_EXISTS, // just imported friends
'ied' = > $PARAM_STRING, // import email domain
'o' = > $PARAM_EXISTS, // first time orientation, passed on confirm
'verified' = > $PARAM_EXISTS)); // verified mobile phoneI guess this is an implementation of a semi-defined variable. It should probably be noted that there may be a tiny bit of performance improvement when calling isset instead of defined when checking for variable set. I'm not sure if that's one of the considerations since it would definitely be a case of premature (and ultimately pointless) optimization anywhere else, but since it's Facebook, they may have gone that far to squeeze every bit out of it they can.
But without more context it's not that easy to tell.
[1]: http://us.php.net/manual/en/language.constants.syntax.php (search for "undefined constant")
// Holy shit, is this the cleanest fucking frontend file you've ever seen?!
ubersearch($_GET, $embedded = false, $template = true);
In all seriousness though, I wonder how much of this was written by Mark Zuckerberg?It's now an asp.net mvc app on Azure. There's hardly any code in it now and a the page response times are down at 50ms rather than 1500ms! One query per page vs 400 as well.
An entire intranet functionality with one SQL query per page. Right, okay...
I haven't touched PHP since about 2007 and it seems like it would be pretty easy to jump in and start making changes and edits. It is well templated, seems to use variables well and not have too many (if any, didn't look all that closely) hard-coded values. I've seen much, much worse in "cleaner" languages than this.
As a side note. I learned a lesson in my early 20s that has served me well to today.
Shipped code always wins.
I was a huge proponent (and still am) of good code, well architected blah blah blah. But, in the end, if your code never sees the light of day, it doesn't matter. I saw this when I took over a team responsible for care and feeding of a product originally written by the founder of a company. This company had just secured a series B on the original code (still). The product was ugly, the code was terrible, it was slow, it wouldn't scale, couldn't be configured and really couldn't be maintained. However, it got the company moving, got a series A & B, secured the first customer, and a second. It basically created a runway to build the thing right that wouldn't have been available had it not gone live.
Beginning developers could learn a thing or two looking over code like these (even though it's now clearly outdated and likely defunct). I've been programming for half a decade and my naming conventions are still horrid.
Also, my style of comments seem so tame and lame now :)
if($user 0 && !is_unregistered($user)) { return $user; }$ php -r '$user = null; if ($user 0) { echo $user; }' PHP Parse error: syntax error, unexpected '0' (T_LNUMBER) in Command line code on line 1
$user <> 0
Which, of course, would not display if someone had originally pasted this code straight into an HTML page without escaping it.EDIT: Looks like it would show up. I wonder, then, if the <>s has been stripped. Some poorly written websites do that.
It isn't good code. It is cleanly written, especially for the time. But all the noise about how bad it is isn't "hipster" talk - this is 400+ lines of completely unmaintainable code; which is why it isn't part of the current code base.
As soon as they got some money & started trying to make it do more, they wised up and dumped it
Does anyone know what he is referring to when he says this can happen via high load?
I guess the story about apache serving php source under high load came from the idea tha Facebook is a high load site (true) and they couldn't just have made as obvious screwup as misconfiguring a production server (false) so it must be apache/php bug that happens only in high load sites (false)
https://bugs.php.net/bug.php?id=26810
there were many related bugs as well. it didn't get fixed for a while, and when it was fixed it was over a number of revisions and wasn't tagged.
The real way this code was leaked was via a plain text version of the file being stored on the server that the hacker found by trying a bunch of URLs
I was speculating at the time, and had recent experience with the bug where source code would be exposed. I found out a little later how it was actually leaked, but then forgot all about it.
It's one of the things that make one-off .php scripts easy to deploy (just chuck it into the webroot). With a little effort, this can be mostly prevented: let the webroot .php file just do an ' include("../outside-webroot/actual-script.php"); '
It may have been more subtle, but of a similar nature.
I am a bit surprised about the lack of OO though...
Look at how much I/O is happening one after the other. The latency could be greatly reduced by doing things in paralel and waiting until all the promises resolve.
Space between "=>"...
>ini_set('memory_limit', '100M'); // to be safe we are increasing the memory limit for search
>tpl_set('simple_orientation_first_login', $get_o); // unused right now
>// We special case the network not recognized error here, because affil_retval_msg is retarded.
>all those undocumented(?) random error codes
>mix between ternary operators and regular if/else statements with no logical choice between one or the other
>no auto loading whatsoever
Seeing as this is from 2007, there is hope.
Reminds me of some production PHP I saw once, returned the entire database (`SELECT * from 'db'`) and manually walked through it with a for() loop. It wasn't very efficient at scale.
If you're building SomeSimpleBasicSite.com and you do this, you're probably a bad programmer. If you're truly facing scalability problems, it's table stakes.
Based on your comments in this thread you're out of your depth, man.