ctrl-c ctrl-v

Self-Hosted Scripts for an URL Shortener: PHP-pastebin

In Part 4 of this series of short articles on setting up your own URL shortener we look into to a pastebin aptly named PHP-pastebin. It is using random short URLs for the pastes.

What is a Pastebin?

The concept of a pastebin is to provide a simple service turning code snippets or large blocks of computer input or output into a short reference (short URL). This reference only fills one line in a textual conversation. It can be used conveniently for quick exchange of the referenced content via IRC or IM. That way, the risk of being banned for “flooding” the chat can be avoided.

Requirements and Features of PHP-pastebin

Requirements: a server with mod_rewrite enabled, and PHP/MySQL. It was written in PHP and smarty. It includes a decent installer, user management, integrated PayPal payment for premium accounts, and a responsive design based on an older version of Bootstrap. PHP-pastebin was developed with the aim of facilitating collaborative coding, and features syntax highlighting of the pastes. There is also some support for plugins.

SeeHe.re, a Pastebin based on PHP-pastebin

BestNa.me has deployed a pastebin based on PHP-pastebin for the domain SeeHe.re. On the start page of SeeHe.re, specific syntax highlighting modes are available for more than fifty programming languages:

Seehe.re start page

It is possible to choose between public or private mode for pasting: public pastes are listed publicly, whereas private pastes remain hidden. Therefore they are only accessible for a person knowing the random short link. The time frame for expiry can also be selected. It ranges from 10 minutes to unlimited. The next screenshot shows a pasted snippet of Ruby code for tr.im with syntax highlighting (which is powered by GeSHi):

A paste of Ruby code for tr.im

A list of all public pastes can easily be retrieved. It shows all important information related to the paste, e.g. the user name of the author and simple access counts.

Last pastes of seehe.re

There is also a dedicated info page for each user. It lists all the pastes issued by this particular user, including the private ones. This page can be accessed by the admin and by the respective user. The user needs to be logged in to access it in his user panel via account settings. Unfortunately, it can also be accessed by everyone if at least one public persistent paste of this user exists: in this case the info page of this user is accessible via the list of all public pastes. More precisely: via the link in the column author in the corresponding row for this paste. So the behavior of PHP-pastebin (and, for the moment, also of SeeHe.re) is that only users without any public paste are able to stay entirely “behind the scenes”. On the info page of a user, also the Gravatar of the user is displayed along with a summary of information about the user.

User details in seehe.re

Logged in users can edit the details of their account and info page:

Edit user details in seehe.re

PHP-pastebin distinguishes between anonymous users, registered users, premium users, and the admin. To become a premium user, the PHP-pastebin software first requests a payment via PayPal. Payment then goes to the PayPal email address configured by the admin. After payment, the premium account is activated instantly. For this, Instant Payment Notification (IPN) is used:

Premium user account registration via Paypal

Premium users are given much larger limits for the length of their pastes. It should be noted that some corrections to the script were necessary to make IPN (and the instant activation of the premium user account) work correctly. There is a second point that needed correction: ATTOW, PHP-pastebin does not check the size limits for the pastes—so even anonymous users can insert pastes of (almost) arbitrary length. On SeeHe.re, this has been corrected by a few lines of code in function addPaste in libs/default.class.php.

In the last part of our series of short articles, several other self-hosted scripts for URL shorteners are briefly reviewed: polr, URL Shortener, and kissa.be.