Privacy

Development

Sysadmin

Gaming

Random stuff

PHP

PHP Learning

Twitter Bootstrap

Random stuff

Memory Upgrade!

This week I've upgraded my Macbook Pro's RAM. I bought two Kingston 8GB HyperX 1600MHz RAM Memory and tested all with memtest for OS X.

PHP

GNU/Linux & C.

Random stuff

Web Skills Profiles

Ever tried explaining what do you do for living? Finally some standard answers (in Italian):

Apple Mac OS X

Need to concatenate some PDF files? Thanks to otofriz.net I now know that Mac OS X provides a great Python command line tool:

$# "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" -o final.pdf directory/containing/*.pdf
$# "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" -o final.pdf 1.pdf 2.pdf 3.pdf
$# "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" -o final.pdf 1.pdf 2.pdf directory/*.pdf

Who to follow (PHP community related)?

Brandon Savage provided a list to some good blogs of people involved in the PHP community. I thought I might list my feed subscriptions:

and...

Thinking about becoming a parent?

Please, don't ask me why!

Last but not least

Silhouette

This is an AppleScript script useful to easily upload files (one or more at the same time) to a remote server using Transmit. The idea is based on this http://www.macdrifter.com/2011/09/upload-file-selection-via-transmit.html article.

Comments and code available on GitHub.

Scripts

Automated Transmit Uploader is divided into two scripts. One (One Click Transmit Uploader) to be used as a standalone application, the other (add - Transmit Uploader) as a folder action.

Configuration

Both scripts must be configured. The variables to be configured are pretty self explanatory:

set pathRemote to "/var/www/vhosts/com.example/uploads/"
set urlBasePath to "http://www.example.com/uploads/"
set favoriteTransmitConnection to "My Favorite Upload Server"
set notificationEnabled to true
set logEnabled to true
set appName to "One Click Transmit Uploader"
  • pathRemote is the upload path on the remote server. Must end with a slash
  • urlBasePath is the first part of the url pointing to the location of the uploaded files. Must end with a slash
  • favoriteTransmitConnection is the name of the connection added to Transmit. It must be in the Favorites group!
  • notificationEnabled can be true or false. Set it true only if you have OS X 10.8 and terminal-notifier installed
  • logEnabled can be true or false. If set to true writes a log file in your Documents directory
  • appName is a string that identifies your upload connection. It will be used also to identify the log filename.

Installation

If you have OS X 10.8 I recommend to install terminal-notifier.

To install One Click Transmit Uploader open the script with AppleScript Editor and export it as an application using Export from the File menu (give the application a name recalling the server you are using). Once you have the application exported, move it to a place you prefer and drag it to the dock. At this point choose a file from the Finder and click on the application available in the dock. Once the upload is completed you will have the url pointing to the upload in the clipboard.

To install add - Transmit Uploader open the script with AppleScript Editor and export it as an Only Executable Script using Export from the File menu (give the script a name recalling the server you are using). Once you have the script exported, move it to the ~/Library/Scripts/Folder Action Scripts/ directory. Choose the directory to be used as the "upload dropbox", Ctrl+click on it and choose Enable Folder Actions. You should be able to see your script. If you drag a file in the "upload dropbox" directory you should be able to find in the clipboard the url pointing to the uploaded file.

Notes

  • If you need to upload to more servers, you must configure end export both scripts for all the servers.
  • To open the ~/Library/Scripts/Folder Action Scripts/ directory, open a Finder window, press Command+Shift+G (or Go to Folder from the Go menu) and open ~/Library/. The Scripts/Folder Action Scripts/ directories, if not available, should be created.
  • Sometimes an error is generated using One Click Transmit Uploader with files located on the desktop

San Lorenzo Maggiore, Benevento, Italy

How many times I had to (re)write code to use Facebook's login feature? Too many! So, here is LfjOpauth for Zend Framework 2.

Comments and code available on GitHub.

Introduction

LfjOpauth is a Zend Framework 2 module that enables support for many authentication providers through the Opauth framework.

Installation

To use the module you need to install Opauth and one of it's strategies:

If you install LfjOpauth using composer, the Opauth dependecy is automatically resolved, but you still must provide at least one strategy.

Configuration

Once LfjOpauth is installed you must create a file named lfjopauth.global.php in your config/autoload directory. This is the configuration file where you specify the LfjOpauth options.

An example of the lfjopauth.global.php file:

$settings = array(
    'security_salt' => 'Some random text',
    'Strategy' => array(
        'Facebook' => array(
            'app_id' => 'facebook application id',
            'app_secret' => 'facebook application secret',
            'scope' => 'email,user_relationships',
        ),
        'second_facebook_app' => array(
            'app_id' => 'another facebook application id',
            'app_secret' => 'another facebook application secret',
            'scope' => 'email,user_relationships',
            'strategy_class' => 'Facebook',
            'strategy_url_name' => 'second_facebook_app'
        )
    ),
    'check_controller_enabled' => false
);

return array('lfjopauth' => $settings);

The configuration is pretty much the same as the Opauth configuration, without the path and callback_url options, which are handled by the module.

The check_controller_enabled flag enables or disables access to CheckController.

Login and callback urls

Given the above configuration (and the corresponding Facebook applications), you will be able to login using:

  • http://example.com/user/opauth/login/facebook
  • http://example.com/user/opauth/login/second_facebook_app

and to logout using:

  • http://example.com/user/opauth/logout

For the two demo Facebook application described in the example configuration, you should use

  • http://example.com/usa2012/login/facebook
  • http://example.com/usa2012/login/second_facebook_app

as value of the Website with Facebook Login, Site URL option.

Custom callback urls

If you need custom login and/or callback urls (for example containing more parameters), you can code custom routes and controller.

This is the code that defines the custom_lfjopauth_login and custom_lfjopauth_callback routes (custom-auth is the controller alias):

return array(
    'router' => array(
        'routes' => array(
            'custom_lfjopauth_login' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/custom/login/[:provider[/:oauth_callback]]',
                    'constraints' => array(
                        'provider'       => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'oauth_callback' => '[a-zA-Z][a-zA-Z0-9_-]*'
                    ),
                    'defaults' => array(
                        'controller'    => 'custom-auth',
                        'action'        => 'redirectAndReturn'
                    )
                )
            ),
            'custom_lfjopauth_callback' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/custom/callback/[:provider]',
                    'constraints' => array(
                        'provider'  => '[a-zA-Z][a-zA-Z0-9_-]*'
                    ),
                    'defaults' => array(
                        'controller'    => 'custom-auth',
                        'action'        => 'callback'
                    )
                )
            ),
            // more code
        )
    )
);

This is the code of the hypothetical controller that manages login and callback actions:

// [...]
class AuthController extends AbstractActionController
{
    public function redirectAndReturnAction()
    {
        // if user is not logged in
        if (!$this->auth()->hasIdentity())
        {
            $provider = $this->params()->fromRoute('provider');
            $oauth_callback = $this->params()->fromRoute('oauth_callback');
            $opauth_service = $this->getServiceLocator()->get('opauth_service');

            // set custom login and callback routes
            $opauth_service->setLoginUrlName('custom_lfjopauth_login');
            $opauth_service->setCallbackUrlName('custom_lfjopauth_callback');

            return $opauth_service->redirect($provider, $oauth_callback);
        }

        return $this->redirect()->toRoute('somewhere_over_the_rainbow');
    }

    public function callbackAction()
    {
        // if user is not logged in
        if (!$this->auth()->hasIdentity())
        {
            $provider = $this->params()->fromRoute('provider');
            $opauth_service = $this->getServiceLocator()->get('opauth_service');

            // set custom login and callback routes
            $opauth_service->setLoginUrlName('custom_lfjopauth_login');
            $opauth_service->setCallbackUrlName('custom_lfjopauth_callback');

            $opauth_service->callback($provider);
        }

        return $this->redirect()->toRoute('somewhere_else_over_the_rainbow');
    }
}

Checking login status

If the check_controller_enabled flag is enabled, you will be able to print current session info at this url:

  • http://example.com/user/opauth/check

The default value of check_controller_enabled is false.

Other info

LfjOpauth uses Zend\Authentication\AuthenticationService (alias lfjopauth_auth_service) to manage authentication.

The LfjOpauth\Service\OpauthService (alias: opauth_service) class exposes the redirect and callback methods which can be used in any controller. An example can be found in the LfjOpauth\Controller\LoginController class.

Quick note on how to startup a Zend Framework 2 application with service manager.

< ?php
use Zend\ServiceManager\ServiceManager;
use Zend\Mvc\Application

/* some configuration code */

$services = new ServiceManager($servicesConfig);
$application = new Application($applicationConfig, $services);
$app->bootstrap();
$response = $app->run();
$response->send();

Six by nine. Forty two.

That's it. That's all there is.

I always thought something was fundamentally wrong with the universe

Douglas Adams, The Restaurant at the End of the Universe