Archive for Script

Dropbox CakePHP Plugin

For my new project (MameliCMS) I use a Dropbox plugin for CakePHP: Cakebox.

This plugin helps you to authenticate to Dropbox REST services and to obtain an authorization token to play with Dropbox REST API.

In this post I will (in far future) write a small tutorial to that plugin!

Continue reading »

Posted in CakePHP, Script | Leave a comment

Adminer for CakePHP

adminer

Managing your database should be a problem.

cPanel, phpMyAdmin are very difficult to install and you need to remember configurations or access you database.php file to read from.

Adminer is a one file db manager for MySQL and I wrote a simple webroot package to automatize loggin-in action reading from your App/Config/datavase.php file.

Continue reading »

Posted in CakePHP, Script | Leave a comment

PowerSet::def() API

Often my functions accepts an array of options to drive its behavior:

function foo( $data, $options = array() ) {
  if ( $options['filter'] = 'int' ) return intval($data);
  return $data;
}

The plus of accepting an array of options is your method can grow up without change it’s params API.

I often allow to accepts more than array type for my $options param:

function foo( $data, $options = array() ) {
  if ( is_string($options) ) $options = array( 'filter'=>$options );
  ...
}

This way my method API should be something like:

// full api
echo foo( $data, array('filter'=>'int'));

// compact api
echo foo( $data, 'int' );

I like DRY code so much!

Make it easy!

What PowerSet::def() does is to simplify the logic that convert different $options types into specific property names:

function foo( $data, $options = array() ) {
  $options = PowerSet::def($options, null, array(
    'string'=>'filter', 
    'integer'=>'length'
  ));
  ...
}

This code should be translated into:

"if $options type is -string- then put it value into -filter- key"
"if $options type is -integer- then put it value into -length- key"

In every cake $options will be converted into an array.

PowerSet::def('aaa', null, 'class');
-> array( 'class'=>'aaa' );

PowerSet::def(24, null, 'length');
-> array( 'length'=> (int) 24 );

PowerSet::def('aaa', null, array( 'string'=>'class', 'integer'=>'length'));
-> array( 'class'=>'aaa' );

PowerSet::def(24, null, array( 'string'=>'class', 'integer'=>'length'));
-> array( 'length'=> (int) 24 );

Why “null” as second param?

You can set up a default array to be extended with given values:

// useful to apply default values to an array
PowerSet::def('container', array(
  'id => '',
  'class => '',
  'style => '',
), 'class');
Posted in CakePOWER, Script | Leave a comment

Markdown CakePHP Plugin

Markdown Plugin for CakePHP provides a MarkdownHelper to parse Markdown source code plus an integration with views rendering engine to automagically render views as Markdown source files.

  • MarkdownHelper
  • Render views as Markdown

Continue reading »

Posted in CakePHP, Script | 4 Comments

PhpCompiler – PHP deployment utility

Today I wrote an utility class to quick deploy my PHP based projects: PhpCompiler.

It copy all files from a /source path to a /-compiled path with these utility:

  • follows symlinks to create a ready to FTP deployed path
  • allow to skip files and folders by rules
  • optionally removes all comments from PHP files (by rules)
  • optionally uglify PHP files (by rules)

Continue reading »

Posted in Script, Tips & Tricks | 1 Comment

Execute complex Javascript in LessCSS

Today I was playing with my public project LessMixin adding some cool features when I ran into a LessCSS limit: loops!

Loops – for, while – are not implemented yet (but the author said it will never be!).

Continue reading »

Posted in MovableApp.com, Script, Tips & Tricks | 2 Comments

Javascript Optimization: variable’s scope

Variables are places where you store values.
When doing some logics you always need to access variables.

Do you know accessing local variables is better than accessing globals?

Continue reading »

Posted in Script, Tips & Tricks | Leave a comment

CakePOWER :: PowerAuthComponent

PowerAuthComponent extends CakePHP’s AuthComponent adding a $accessDeniedRedirect property to allow define a static redirect when an access deny happen to an authenticated user.

Usage:

You can use this class by aliasing the standard AuthComponent inclusion in your AppController (or wherever you plan to use Authentication and Authorization):

public $components = array(
    'Auth' => array(
        'className' => 'CakePower.PowerAuth',
        'loginAction' => ...
    );
);

You don’t need anything else code to implement this CakePower component!

NOTICE: if you are extending CakePowerController in your AppController you do not need to set up the “className” property. CakePower sets up the alias for you!

Why do I need PowerAuth?

CakePHP’s AuthComponent redirects to the referral url (fallback to the root url) when access is denied to an authenticated user but this behavior may falls into infinite loop if booth referral url and root action are denied to the user.

The stage:

Figure you have a fully protected app where even the root url (/) belong to authentication and authorization.

If your app is a private blog (like in CakePHP tutorials) you may have these controllers:

  • posts – display and manage posts
  • users – manage users
  • info – display some analytics about the blog.

Authentication uses UsersController::login() and UsersController::logout() methods to handle users authentication so these actions are allowed in the AppController rules:

class AppController extends Controller {

    public $components = array(
        'Session',
        'Auth' => array(
            'loginAction' => array( 'controller'=>'users', 'action'=>'login' ),
            'authorize' => 'Controller'
    );

    public beforeFilter() {
        // All app actions are protected except login/logout.
        $this->Auth->allow( 'login', 'logout' );
    }

}

Users Access Policies:

  • User “Marco Sheeptaker” has an “admin” role so hes access is unlimitet.
  • User “Analytics Hero” can access only the AnalyticsController in the app.

These rules are coded as follow in AppController::isAuthorized() method:

public function isAuthorized( $user ) {

    if ( $user['role'] == 'admin' ) return true;

    if ( $this->request->params['controller'] == 'analytics' ) return true;

    return false;

}

When Authorization will cause a loop?

When “Marco Sheeptaker” login to the app no error happen: when authenticated his profile will know no restrictions in the app.

But what happen when an Analytics Hero user try to login?

  • access the root url (/) via browser’s url bar
  • authentication redirects to the login action
  • when login success Auth redirects to the url requested before the login: the root url
  • user is not allowed to view this url so he is redirected to the login another time!

Each login will redirect back… to the login! This is a loop!

Now the user understand he can’t go out this loop so he go to the root url by digit it into the browser’s url bar:

  • access the root url (/) via browser’s url bar – user is already authenticated
  • authentication return “true” so there is no login redirection rules
  • authorization return false so user is redirected to the referral
  • referral does not exists because of user has directly accessed the root url
  • AuthComponent fallback for the referral url is… the root url!!!
  • user is redirected to the root!

This will cause a “bad redirection error” in (modern) browser! In ancient browsers (aka IE<9) it will crash down the client because of an infinite loop!!!

What do PowerAuthComponent does for me?

In simplest configuration (as shown at the beginning of this page) PowerAuthComponent replace fallback url for the referral url when redirecting after an access denied event. In place of the root url (/) it will use the loginAction property.

This way every infinite loops are kicked out of the application! If an action is denied and even the root action is denied PowerAuthComponent will redirect to the login page!

$accessDeniedRedirect

You can customize this behavior by setting an $accessDeniedRedirect. This property will be used in place of the $loginAction when configuring the referral’s fallback url:

public $components = array(
    'Auth' => array(
        'className' => 'CakePower.PowerAuth',
        'loginAction' => ...
        'accessDeniedRedirect' => array( 'controller'=>'pages', 'action'=>'access_denied_error' )
    );
);

$accessDeniedHardRedirect

If you want to skip the referral url and force redirection to an error page you can set the $accessDeniedHardRedirect propety.

This way when an action is not allowed the PowerAuthComponent will always redirect to defined url:

public $components = array(
    'Auth' => array(
        'className' => 'CakePower.PowerAuth',
        'loginAction' => ...
        'accessDeniedHardRedirect' => array( 'controller'=>'pages', 'action'=>'access_denied_error' )
    );
);

Bibliography & Resources:

 

Posted in CakePHP, Script | Leave a comment

sqlDB.js – Access Webkit SQL databases

Today I’m pleased to share a little piece of code I wrote to access and query a webkit local sql database.

It allow you to:

  • open a connection
  • throw queries
  • handling callbacks (with context)
  • execute batch queries with a global callback when all queries are done

>>> Go Quickly to the GitHub Repo! <<<

… but i’d like to tell you WHY I wrote this code …

Continue reading »

Posted in Script | Leave a comment

Swedish Greys - a WordPress theme from Nordic Themepark.