Why Twitter was down for 5 hours. It’s all about calendars.


Twitter was down for many users this Monday, between midnight and 5 AM, CET. Android app users were logged out and the API was not working during a few hours. A bug in a line of code caused the service to think that it was 29 December, 2015.

Gregorian VS ISO calendars

The ISO week numbering system uses the YYYY format for the year instead of the Gregorian calendar’s yyyy. It then looks at which week of the year it is, and then uses a date digit with 1 starting on Monday. So, for example, Tuesday of the 50th week of 2014 would have been 2014-W50-2 in ISO week format. The problem comes in when January 1 of the new year ends up falling on a date that doesn’t get along well with the ISO week format. The first day of 2015 will start on a Thursday, whereas the ISO standard expects the first week of the year to start on “the Monday that contains the first Thursday in January.” In 2014-2015, that would be January 1st, 2015. That’s why Twitter believed that we were in 2015 on Monday, because “the first Thursday in January” is in fact this week.

Afterwards, some parts of the system believed we were almost a year later, while other were perfectly aware that we were still in 2014. Users were not able to log in (and were logged out) because tokens have a limited lifetime and therefore are not valid a year later.

Dates are hard to handle

Dates are really hard to handle when programming, and it’s very easy to make a mistake without breaking a thing until the day it will eventually explode everything because calendars or timezones are different. The same kind of mistake happened two years ago with the “Do Not Disturb” feature on iPhones, and it isn’t the last time it will happen for sure.

Using JavaScript / jQuery to detect if someone is using AdBlock


Let’s face it: ads are not cool. I know, but sometimes you have no other choice than to use them to pay your server / your coffee / whatever. And if you are using ads, you know that some people will not see your ads because they are using a plugin that blocks ads on your website. You want to reach these visitors and just say

“Hey, we know ads are not cool, but please make an exception for us or give us a small amount of money, just to support our work”

On Teen Quotes I am showing a friendly message in place of the only ad to say what I have to say. Here is what people with an ad blocker are seeing:

But let’s get back to the point: detect visitors that are using an ad blocker with JavaScript.

Some code

Let’s write some code. You’re going to write the most simple JS file you have ever written. Ready? Here it is:

var isNaughtyVisitor = false;

BOOM! That was fast, isn’t it? In fact, we don’t care about the variable name or its value, we just want to define a new variable. But this variable needs to be defined in a file called ads.js that you will include in your HTML. Why so? This is were it gets funny. Ad blockers are a little dumb, so when they see a file named something like ad.js or ads.js, they will block the request. And if the request was blocked… your variable will not be defined!

The second step is just to test in your regular JS file of your application if the variable was defined. This file needs to be include after the previous file, ads.js. You can add something like this:

$(document).ready(function() {
    // The div that will add to the DOM if the visitor is 
    // using an ad blocker
    var div = 'Your amazing HTML block here';
    // The script was never called, probably using an ad blocker
    if (typeof(isNaughtyVisitor) == "undefined") {
        // Insert the div wherever you want
        // Send the event to Google Analytics if you want to track
        ga('send', 'event', 'ads', 'hidden');
    // A friendly user!
    } else {
        // Send the event to Google Analytics if you want to track
        ga('send', 'event', 'ads', 'displayed');

Pretty darn simple, and really effective. The downside is that friendly users will have to make an extra HTTP request. Yep, I know, it’s not fair for them.

Using ImageMagick to crop images


ImageMagick is a PHP library for image processing and image generation. It is a requirement for a popular library, stojg/crop that offers various cropping techniques. The library can be installed easily via Composer, but it requires ImageMagick to work.

Installing ImageMagick on Ubuntu / Debian

You are lucky, there is a package for that. It is just a one line command:

sudo apt-get install php5-imagick

It will grab ImageMagick, build the PHP extension and enable it in your php.ini file. You may have to restart your web server or PHP-FPM to reflect the changes.

Using ImageMagick on Travis CI

If you are running a continuous integration test suite, you may be interested to test some methods that are using the ImageMagick library under the hood. In order to do that, the ImageMagick library needs to be installed on the Travis’ VM. At the time, the ImageMagick library is already pulled in for every VM, but the PHP extension is not enabled by default. We will still try to grab the library before enabling it, just to be cautious. You’ll have to edit your travis.yml file and add the following lines to your before_script:

  - sudo apt-get update -q
  - sudo apt-get install -y imagemagick
  - printf "\n" | pecl install imagick

The last line is a very dirty hack: when running the command pecl install imagick, you have to hit “Enter” to autodetect some settings. That’s why you need to pipe a new line to this command. Dirty, but it works, you’ll be able to test methods using ImageMagick afterwards.

Fighting against suicide on Teen Quotes


Teen Quotes lets teenagers share their daily thoughts and feelings. Since people speak their mind, we receive happy quotes about friendship and love, regular life inspiring quotes and sad quotes. And sometimes, unfortunately, really really sad quotes.

At Teen Quotes, we have a very strict publishing policy. Every quote gets reviewed by a human before being published. If we don’t like a quote, we can edit it a little bit to make it publishable or we can refuse it.

How to manage refused quotes

In the past, when we received a quote that we didn’t want to publish, here is what happened:

  • The submitted quote goes through moderation and gets refused. As an immediate result, it will never be published on Teen Quotes.
  • The author of the quote receives an email telling that its quote was refused with a few hints to improve: verify the English, be sure that a similar quote wasn’t published before and try to write an original quote.

This type of email is sent a lot because we refuse around 75 % of the submitted quotes. We would be super happy to explain in every email why this specific quote was refused but it would take too much time during moderation.

But I am convinced that sometimes we should contact the author of the quote, in specific situations. One of the possible situation is described in this post’s title: suicide promotion / suicide thoughts in a quote.

Submitting super sad quotes is a real alarm

As already said, users submit quotes on Teen Quotes to inspire others. Most of our visitors come just to get inspired, to relate to others, to put words on feelings. We deeply think that we shouldn’t publish quotes that promote suicide thoughts. Everyone has ups and downs, but suicide should not be an option. So we protect other members from Teen Quotes by refusing such quotes. And today, we are going one step further.

Trying to help authors that need assistance

In the upcoming major version of Teen Quotes, if a user submits a really sad quote, we will send him a personal email to make sure that everything is right and we will include some useful links if he needs to get help from non-profit organisation around the world. As a software engineer, it is usually difficult to have an impact in someone’s life. Moving a button 2 pixels to the left will not make a huge difference. We deeply hope that we will be able to help people with this feature. It really matters to us.

Laravel package for the recommendation system Easyrec


During a few days in August I have coded a Laravel wrapper for the recommendation system Easyrec. If you want to display most viewed items, best rated items, related items or something like that, a recommendation system is the perfect way to go. If you are familiar with Machine Learning techniques, you now that a recommendation system is something very difficult. If you remember, Netflix offered $ 1,000,000 to improve its collaborative filtering algorithm.

Easyrec provides a REST API that you can call for free. This is something very convenient if don’t have a lot of data and that you still want to use a recommender system for your web service.

Features overview

After registering the service provider and the alias in your app/config/app.php file, it will be super easy to use! Here is a little overview of the available functions:

Easyrec::view(42, 'Post 42', 'http://example.com/posts/42', 200, null, null, 'POST'); // User #200 has viewed post #42
Easyrec::rate(42, 8, 'Book 42', 'http://example.com/books/42', 200, null, null, 'BOOK'); // User #200 has rated 8/10 book #42
Easyrec::mostViewedItems(10, 'WEEK', 'BOOK'); // Retrieves the 10 most viewed books for the last week
Easyrec::bestRatedItems(10, 'MONTH', 'POST'); // Retrieves the 10 best rated posts for the last month

Documentation and download

Of course the package is available via Composer. The full documentation can be found on GitHub: github.com/AntoineAugusti/laravel-easyrec.

Do not hesitate to open up an issue if something is not working as expected. Pull-requests are very welcome also!

Error handlers order in Laravel


Error handlers are a neat feature of Laravel. They allow you to deal with custom exceptions so that you know what to do when something wrong happens. If you want to play with error handlers, you need to know two vital things:

  1. If an exception handler returns a response, that response will be sent to the browser and no other error handlers will be called ;
  2. Handlers are called based on the type-hint of the Exception they handle. If two handlers handle the same exception, the last one will be called first.

About error handlers order

An example:

App::error(function(MyCustomException $exception, $code)
	// This handler will not be called first

App::error(function(MyCustomException $exception, $code)
	// This handler will be called first

Error handlers and responses

If your last handler returns a response, your previous handler will not be called because of the first rule.

App::error(function(MyCustomException $exception, $code)
	// This handler will not be called AT ALL

App::error(function(MyCustomException $exception, $code)
	// This handler will be called first
	$data = ['title' => 'Error occured'];

	// We are returning a response: no other error handlers will be called
	return Response::view('errors.default', $data, $code);

Playing with error handlers order

Let’s say you want an error handler to be called at the bottom of the stack. This is very useful for example if you are using a package that registers an error handler and you want one of your error handlers to be called after. Laravel provides the App::pushError() function that registers an error handler at the bottom of the stack.

App::error(function(MyCustomException $exception, $code)
	// This handler will be called first
	// If we are returning a response here, no other error handlers will be called

App::pushError(function(MyCustomException $exception, $code)
	// This handler will be called at the end
	$data = ['title' => 'Error occured'];

	return Response::view('errors.default', $data, $code);

Laravel package to perform sentiment analysis


I have just released a Laravel package that allows you to perform a sentiment analysis over an English sentence. This is basically a Laravel wrapper for phpInsight, written by JWHennessey.

Features overview

After registering the service provider and the alias in your app/config/app.php file, it will be super easy to use! Here is a little overview of the available functions:

SentimentAnalysis::isNegative("Weather today is rubbish"); // Returns true
SentimentAnalysis::decision("Weather today is rubbish"); // Returns 'negative'
SentimentAnalysis::score("Weather today is rubbish"); // Returns 0.5. We are pretty confident that it is a negative sentence

SentimentAnalysis::isPositive("Marie was enthusiastic about the upcoming trip. Her brother was also passionate about her leaving - he would finally have the house for himself."); // Returns true

Documentation and download

Of course the package is available via Composer. The full documentation can be found on GitHub : github.com/AntoineAugusti/laravel-sentiment-analysis.

Do not hesitate to open up an issue if something is not working as expected. Pull-requests are very welcome also!

Inlining CSS when sending an email with Mailgun in Laravel


Since Laravel 4.2, it is possible to use external emails providers to send emails in your application: Mailgun and Mandrill. Before that I was using a nice plugin fedeisas/laravel-mail-css-inliner to inline CSS just before sending the email. Thanks to this, my views are very clean and my emails are still displayed properly in the various email clients and webmails. This plugin was taking advantage of SwiftMailer to inline the CSS when sending an email by registering a plugin. Unfortunately, it is not working with external providers because SwiftMailer is not used since an API call is made instead.

Extending some classes to fix this

I really wanted to inline my CSS before sending an email and I wanted a clean way to do this. A workaround that I have figured out is to extend two classes: Illuminate\Mail\MailServiceProvider and Illuminate\Mail\Transport\MailgunTransport.

I’ve created a new file located at app/lib/TeenQuotes/Mail/Transport/MailgunTransport.php. The goal was to edit the message before calling the Mailgun API.

namespace TeenQuotes\Mail\Transport;

use Swift_Transport;
use Swift_Mime_Message;
use GuzzleHttp\Post\PostFile;
use Swift_Events_EventListener;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;

class MailgunTransport extends \Illuminate\Mail\Transport\MailgunTransport {

	 * {@inheritdoc}
	public function send(Swift_Mime_Message $message, &$failedRecipients = null)
		$client = $this->getHttpClient();

		// Inline CSS here
		$converter = new CssToInlineStyles();

		if ($message->getContentType() === 'text/html' ||
			($message->getContentType() === 'multipart/alternative' && $message->getBody())
		) {

		foreach ($message->getChildren() as $part) {
			if (strpos($part->getContentType(), 'text/html') === 0) {

		// Call the API
		$client->post($this->url, ['auth' => ['api', $this->key],
			'body' => [
				'to' => $this->getTo($message),
				'message' => new PostFile('message', (string) $message),

Since we have a new MailgunTransport, we need to use our custom MailgunTransport when sending an email. I have created a new file at app/lib/TeenQuotes/Mail/MailServiceProvider.php.

namespace TeenQuotes\Mail;

use TeenQuotes\Mail\Transport\MailgunTransport;

class MailServiceProvider extends \Illuminate\Mail\MailServiceProvider {

	 * Register the Mailgun Swift Transport instance.
	 * @param  array  $config
	 * @return void
	protected function registerMailgunTransport($config)
		$mailgun = $this->app['config']->get('services.mailgun', array());

		$this->app->bindShared('swift.transport', function() use ($mailgun)
			return new MailgunTransport($mailgun['secret'], $mailgun['domain']);

Not so much work, I just use my custom MailgunTransport that I have just created.

Replacing the Mail Provider

You need to update providers in app/config/app.php to replace the MailServiceProvider with our custom provider.

	'providers' => array(

		// Some others providers...
		// Comment this 'Illuminate\Mail\MailServiceProvider', 
		// We add our new MailServiceProvider
		// Some more providers...

Updating composer.json

We need some new plugins

	"require": {
		// Your plugins
		"tijsverkoyen/css-to-inline-styles": "1.2.*",
		"guzzlehttp/guzzle": "~4.0"

And we need to update the autoload section to be able to load our custom library

	"autoload": {
		"classmap": [
		"psr-0": {
			"TeenQuotes": "app/lib"

A simple composer dump-autoload and you will be good! Do not forget to set your API key and your mail domain for Mailgun in app/config/services.php.

If you want to use a different namespace of course you are free!

Laravel: fulltext selection and ordering


Yesterday I was looking for a way to do a FULLTEXT select using Laravel. It was not so easy. In this article I’m going to explain how to a FULLTEXT select and to order by this selection.

The migration

If you want to do a FULLTEXT search, you will need a FULLTEXT index on at least one column of your table. Warning: if you are using InnoDB as your table’s engine, you will need MySQL >= 5.6. If you are using MyISAM as your table’s engine, you are good to go for the index but you can’t use foreign keys.

I’m using InnoDB with MySQL 5.6, here is my code for the migration of the table.

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateQuotesTable extends Migration {

     * Run the migrations.
     * @return void
    public function up()

        Schema::create('quotes', function(Blueprint $table) {
            $table->engine = "InnoDB";
            $table->string('content', 500);

        // Here we create the FULLTEXT index
        DB::statement('ALTER TABLE quotes ADD FULLTEXT search(content)');

     * Reverse the migrations.
     * @return void
    public function down()
        // Drop the index before dropping the table
        Schema::table('quotes', function($table) {


Nothing uncommon, note that you will have to use a DB::statement('ALTER TABLE quotes ADD FULLTEXT search(content)') to create the index.

Searching using the FULLTEXT index

Here it comes the fun part. Now that we have your index, let’s begin to use it. I want to get quotes based on a search on their content. I want pertinent results so I’ll advantage of the index.

My code is the following:

 * @brief Function used to search for quotes using the FULLTEXT index on content
 * @param  string $search Our search query
 * @return Collection Collection of Quote
public static function searchQuotes($search)
    return Quote::
    select('id', 'content', 'user_id', 'approved', 'created_at', 'updated_at', DB::raw("MATCH(content) AGAINST(?) AS `rank`"))
    // $search will NOT be bind here
    // it will be bind when calling setBindings
    ->whereRaw("MATCH(content) AGAINST(?)", array($search))
    // I want to keep only published quotes
    ->where('approved', '=', 1)
    // Order by the rank column we got with our FULLTEXT index
    ->orderBy('rank', 'DESC')
    // Bind variables here
    // We really need to bind ALL variables here
    // question marks will be replaced in the query
    ->setBindings([$search, $search, 1])

I haven’t found a convenient way to select all columns from my table plus an additional one: the rank given by the FULLTEXT search. The tricky part here is really the binding. You need to bind all variables at the end of your query to make it work.

I’m not using the FULLTEXT search in BOOLEAN MODE here. If you need to do so, take a look at the official documentation: http://dev.mysql.com/doc/refman/5.0/en/fulltext-boolean.html. You will only need to add two strings to make it work.

Paginate posts correctly when they are random ordered


The problem

This is a common problem: you have entities in a category, you want to display them by pages because you have a lot of entities and you don’t want to have entities from page 1 in your page 2.

If you are using the ORDER BY RAND() function from MySQL, you will have a problem. MySQL splits up the data into pages of X posts each (paginates) and fails to include a new set of X posts on page 2 and so forth. In other words, because it is listing things in a random order, it just goes out and gets another X random posts. As a result, you will have some repeated posts instead of a new set of X random posts on page 2, etc.

The solution

Fortunately, there is a solution for this problem. You will be able to “remember” which random 10 posts were included on page 1, and then have a new set of 10 posts to put on pages 2, 3, etc. until all posts are displayed.

The MySQL RAND() function accepts a seed as an optional argument. Using a seed, it will return the same randomized result set each time. For example, if you want your posts to be random ordered, paginated with no repetition, you can write a query like this: SELECT * FROM posts ORDER BY RAND(42) to select posts.

If you do not want to have the same results for every user viewing the list, do not give an arbitrary value to the RAND function: generate a random number, store it in session and pass it to the MySQL RAND function when selecting posts.