5 basic principles of SOLID object-oriented programming and design

18/02/2013

The principles when applied together intend to make it more likely that aprogrammer will create a system that is easy to maintain and extend over time

S. single responsibility principle states that every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility. A responsibility is a reason to change, and a class or module should have one, and only one, reason to change.

O. open/closed principle states “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification“;[1] that is, such an entity can allow its behaviour to be modified without altering its source code.

L. Liskov substitution principle “objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program”. In Design by contract (DbC), software designers should define formal, precise and verifiable interface specifications for software components, which extend the ordinary definition of abstract data types with preconditions,postconditions and invariants.

I. The interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use.

D. Dependency inversion principle one should “Depend upon Abstractions. Do not depend upon concretions. Dependency injection is a software design pattern that allows removing hard-coded dependencies and making it possible to change them, whether at run-time or compile-time.

Anuncis

Reusable ZF2 Modules

18/02/2013

 Autoloading

<?php
namespace foo;

use ZendModuleConsumerAutoloaderProvider;

class Module implements AutoloaderProvider
{
  public function getConfig()
  {
    return include __DIR__.’/config/module.config.php’;
  }
  public function getAutoloaderConfig
  {
    return array(
    ‘ZendLoaderClassMapAutoloader’ => array(include __DIR__.’/autoload_classmap.php’),
    ‘ZendLoaderStandardAutoloader’ => array(‘namespaces’=>array(‘Foo’=>__DIR__.’/src/Foo’)),
    );
  }
}

Typical module configuration

 <?php
return array(‘di’=>array(
  ‘instance’=>array(‘ZendMvcRouterRouteStack’=>
    array(‘parameters’=>array(
      ‘routes’=>array(
        ‘foo’=>array(
          ‘type’=>’Literal’,
          ‘options’=>array(
            ‘route’=>’/foo’
            ‘defaults’=>array(
               ‘controller’=>’FooControllerIndexController’
            ),
          ),
          ‘may_terminate’=>true,
          ‘child_routes’=>array( /**/ )
        )
      )
    ))
  )
)); 

Typical event registration

<?php
namespace foo;

use ZendEventManagerStaticEventManager;
        ZendModuleManager as ModuleManager;

class Module
{
  public function init(ModuleManager $manager)
  {
    $events = StaticEventManager::getInstance();
    $events->attach(
      ‘bootstrap’,’bootstrap’,
      array($this, ‘bootstrap’)
    );
  }
  public function bootstrap($e)
  {
    $app = $e->getParam(‘application’);
    //access to service locator to be able to register other listeners and do some stuff
  }
}

Module specific event listeners

<?php

/* ..*/
  public function bootstrap($e)
  {
    $app = $e->getParam(‘application’);
    $app->events()->attach(‘route’ array($this, ‘moduleCheck’), -100); //priority
  }
  public function moduleCheck($e)
  {
    $matches = $e->getRouteMatch();
    $controller = $matches->getParam(‘controller’, ”);
    if(0 !== strpos($controller, __NAMESPACE__)){
      //Not our module
      return;
    }
    //do stuff
  }
}

Example contact form

  • Configure routes in modulePhlyContactconfigmodule.config.php
  • Define the form in modulePhlyContactsrcPhlyContactContactForm.php
  • Code the controller and their actions
  • Add view scripts
  • Definition classes of the module in modulePhlyContactconfigmodule.config.php
  • Inject configurations for used classes like ZendMailMessage and preferences for ZendMailTransport
  • View scripts injection
  • Create autoloader map
  • Create the module class
  • Remember to register your module in config/application.config.php

Why is reusable?

Via configuration…

  • You can change base route
  • You can change preferences of sending method
  • You can change the captcha adapter

More info

    Complete code: https://github.com/weierophinney/PhlyContact

    Presentation: http://www.zend.com/en/webinar/Framework/70170000000bkF8-reusablezf2modules.flv

    Official languages of the United Nations and languages by number of native speakers

    16/02/2013

    The official languages of the United Nations are the six languages that are used in UN meetings, and in which all official UN documents are written. In alphabetical order they are:

    S’ha proposat que el Hindi, el de l’Índia que estat més poblat del món i també el portuguès, parlat a Portugal, Brasil, Angola, Mozambique, Guinea Equatorial, Cap Verd, Guinea-bissau, São Tomé and Príncipe, Timor-Leste, Macao.
    L’alemany o el japonés no hi son ni s’han proposat mai.

     

    Language Family Native speakers
    (Ethnologue 16)[2]
    Total speakers
    (Ethnologue 16)[2]
    Other estimates Rank
    Mandarin Sino-Tibetan,
    Chinese
    845 million (2000) 1025 million One of the six official languages of the United Nations.
    All varieties of Chinese: 1,200 million (2000)
    1
    Spanish
    (Castilian)
    Indo-European,
    Romance
    329 million (1986–2000) 390 million 400 million native.[3] 500 million total (2009)[4]

    One of the six official languages of the United Nations.

    2
    English Indo-European,
    Germanic
    328 million (2000–2006) Approximately 375 million L1 speakers, 375 million L2 speakers, and 750 million EFL speakers. Totaling about 1.5 billion speakers.[5]

    One of the six official languages of the United Nations.

    3
    Hindustani
    (Hindi-Urdu)
    Indo-European,
    Indo-Aryan
    240 million (1991–1997) 405 million (1999) 490 million total speakers.[6] 4
    Arabic Afro-Asiatic,
    Semitic
    206 million (1999), 221 million, 232 million
    (206M is “all Arabic varieties”; 221M is Arabic “macrolanguage”, not counting Hassaniya; 232M is sum of counts for all dialects)
    452 million (1999) 280 million native.[7]

    One of the six official languages of the United Nations.

    5
    Bengali
    (Bangla)
    Indo-European,
    Indo-Aryan
    181 million (1997–2001) 250 million   6–7
    Portuguese Indo-European,
    Romance
    178 million (1998) 193 million 220 million native, 240 million total.[8] 6–7
    Russian Indo-European,
    Slavic
    144 million (2002) 250 million One of the six official languages of the United Nations.[9] 8
    Japanese Japonic 122 million (1985) 123 million   9
    Punjabi Indo-European,
    Indo-Aryan
    109 million (2000)
    All varieties: Lahnda, Seraiki, Hindko, Mirpur
      10

     

     

    El top 10 segons ethnologue.org és el següent:

    Rank Language Primary Country Total
    Countries
    Speakers
    (millions)
    1 Chinese [zho] China 31 1,213
      Chinese, Gan [gan] China 1 20.6
      Chinese, Hakka [hak] China 17 30.0
      Chinese, Huizhou [czh] China 1 4.6
      Chinese, Jinyu [cjy] China 1 45.0
      Chinese, Mandarin [cmn] China 20 845
      Chinese, Min Bei [mnp] China 2 10.3
      Chinese, Min Dong [cdo] China 7 9.1
      Chinese, Min Nan [nan] China 10 47.3
      Chinese, Min Zhong [czo] China 1 3.1
      Chinese, Wu [wuu] China 2 77.2
      Chinese, Xiang [hsn] China 2 36.0
      Chinese, Yue [yue] China 20 55.5
    2 Spanish [spa] Spain 44 329
    3 English [eng] United Kingdom 112 328
    4 Arabic [ara] Saudi Arabia 57 221
      Arabic, Algerian Spoken [arq] Algeria 6 22.4
      Arabic, Egyptian Spoken [arz] Egypt 10 54.0
      Arabic, Gulf Spoken [afb] Iraq 10 3.6
      Arabic, Hijazi Spoken [acw] Saudi Arabia 2 6.0
      Arabic, Libyan Spoken [ayl] Libya 3 4.3
      Arabic, Mesopotamian Spoken [acm] Iraq 5 15.1
      Arabic, Moroccan Spoken [ary] Morocco 10 21.0
      Arabic, Najdi Spoken [ars] Saudi Arabia 7 10.0
      Arabic, North Levantine Spoken [apc] Syria 16 14.4
      Arabic, North Mesopotamian Spoken [ayp] Iraq 4 6.3
      Arabic, Sa’idi Spoken [aec] Egypt 1 19.0
      Arabic, Sanaani Spoken [ayn] Yemen 1 7.6
      Arabic, South Levantine Spoken [ajp] Jordan 9 6.2
      Arabic, Sudanese Spoken [apd] Sudan 6 16.8
      Arabic, Ta’izzi-Adeni Spoken [acq] Yemen 8 7.1
      Arabic, Tunisian Spoken [aeb] Tunisia 5 9.4
    5 Hindi [hin] India 20 182
    6 Bengali [ben] Bangladesh 10 181
    7 Portuguese [por] Portugal 37 178
    8 Russian [rus] Russian Federation 33 144
    9 Japanese [jpn] Japan 25 122
    10 German, Standard [deu] Germany 43 90.3

    MVC in ZF2

    16/02/2013

    ZF2 file structure

    config
      autoload
      application.config.app 

    data
      cache

    module
      Application
       Module.php 
       config
          module.config.pgp
        src
          Application
            Controller
              IndexController.php
         view
           application
             index
               index.phtml 
            error
            layout 

    public
      images
      js
      css
      .htaccess
      index.php 

    vendor
      zendframework

    application.config.app 

    array(
      ‘modules’ =>array( //list of modules you want to use
        ‘Application’
      ) ,
      ‘module_listener_options’ =>array( //modules options as autoloading and paths
        ‘config_glob_paths’ => array(
          ‘config/autoload/{,*.}{global,local}.php’,
        ),
        ‘module_paths’=>array(
          ‘./module’,
          ‘./vendor’,
        )
      );
    ); 

    ZendServiceManager

    ServiceManager is a Service Locator where you register services and later retrive them by configuration.

    Types of services: explicit services, invokables to instatiate, factory returing object, aliases, unknown services, scoped containers, shared or not.

    module/Application/Module.php

    namespace Application;

    use ZendMvcModuleRouteListener;
    use ZendMvcMvcEvent;

    class Module
    {
      public function getConfig(){
        return include __DIR__ . ‘/config/module.config.php’;
      }

    module/Application/config/module.config.php 

    return array(
      ‘router’=>array(
        ‘routes’=>array(
          ‘home=>array(
            ‘type’=>’ZendMvcRouterHttpLiteral’, //You can use Segment type
            ‘options’=>array(
              ‘route’=>’/’,
              ‘defaults’=>array(
                ‘controller’=>’ApplicationControllerIndex’,
                ‘action’=>’index’,
              ),
            ),
          ),
        ),
      ),
      ‘controllers’=>array(
        ‘invocables’=>array(
          ‘ApplicationControllerIndex’=>’ApplicationControllerIndexController’
        )
      ),
      ‘view_manaer’=>array(
       ‘display_not_found_reason’=>true,
       ‘display_exceptions’=>true,
        ‘doctype’=>’HTML5’,
        ‘not_found_template’=>’error/404’,
        ‘exception_template’=>’error/index’,
        ‘template_map’=>array(
          ‘layout/layout’=>__DIR__.’/../view/layout/layout.phtml’,
          ‘application/index/index’=>__DIR__.’/../view/application/index/index.phtml’,
          ‘error/404’=>__DIR__.’/../view/error/404.phtml’,
          ‘error/index’=>__DIR__.’/../view/error/index.phtml’,
        ),
        ‘template_stack_path’=>array(
          __DIR__.’/../view’,
        ),
      ),
    /* … */
    ); 

    module/Application/src/Application/Controller/IndexController.php

    namespace ApplicationController;

    use ZendMvcControllerAbstractActionController;
    use ZendViewModelViewModel;

    class IndexController extends AbstractActionController
    {
      public function indexAction(){
        return ViewModel();
      }

    The ViewModel will return view/application/index/index.phtml contents.

    Source Zend webinar.

    Get started to zf2 and why to switch from zf1

    15/02/2013
    • ZF1 way: Singletons, registers, soft and hard dependencies
    • ZF2 way: Even-Driven Services flexibility, better secure defaults, contexts of escaping, dependencies injection (inversion of control).

    Instead of using hard dependencies and dificult to test code:

    public function someAction(){  $front = Zend_Controller_Front::getInstance();  $bootstrap = $front->getParam('bootstrap');  $db = $bootstrap->getResource('db');  $service = new SomeService($db);  $this->view->results = $service->doSomething();}

     

    With ZF2 you can achieve this:

    public function setService($service){  $this->service = $service;}public function someAction(){  return array(    'results' => $this->service->doSomething();  ); }

    You need to use Dependency Injection when getting the controller:

    function ($controllers){  $services = $controllers->getServiceLocator();  $service = $services->get('SomeService');  $controller = new SomeController();  $controller->setService($service);  return $controller;}

    Benefits:

    • Easy replacements which means easier to test
    • Code for construcion is located in one place, easy reviewed place.
    • You can do things for separate services more easily

     

    Zend/ServiceManager

    No discovery, you code injections. Explicit writing means less debuging

    //Explicit services
    array(‘services’, array(
      ‘foo’=>new SomeComponentFoo,
      ‘bar’=>$someObjectInstance,
    ));
    //Instantiable class
    array(‘invokables’, array(
      ‘foo’=>’SomeComponentFoo’,
    ));
    //Callable that returns and object
    array(‘factories’, array(
      ‘foo’=>function($services){
        return new SomeComponentFoo(
          $services->get(‘bar’)
        )
      },
      ‘bar’=>’SomeStatic::method’,
      ‘baz’=>’SomeClassImplementingFactoryInterface’,
      ‘bat’=>’SomeFunctionClass’,
    ));
    //Callable that handles multiple services
    array(‘abstract_factories’, array(
      ‘
    SomeClassImplementingFactoryInterface‘,
      $
    someAbstractFactoryInstance
    ));
    //Example of abstract factory
    class SampleAbstractFactory implements AbstractFactoryInterface {
      public funtion canCreateServiceWithName(
       ServiceLocatorInterface $services, $name, $requestedName
      ) { /* code.. */ }
      public funtion createServiceWithName  /* code.. */

    //(un)Share a service
    array(‘shared’, array(
      ‘EventManager’=>false, //default is true
    ));
     
    //Operations on new instances, for example to automate injections like this
    array(‘initializers’, array(
      function($instance, $services) {
        if($instance instanceof EventManagerAwareInterface) {
          $instance->setEventManager(
            $services->get(‘EventManager’)
          )
        }
      }
    ));
    //Plugin example. $heplers is view helper plugin manager instance
    array(‘factories’, array(
      ‘something’=> function($helpers) {
        $services = $helpers->getServiceLocator();
        $model = $services->get(‘SomeModel’);
        $helper = new SomethingHelper($model);
        return $helper;
      }
    ));

    Event Manager

    Listen and react to trigger events in simple way

    $events = new EventManager();
    $events->attach(‘do’, function($e){
      $event = $e->getName();
      $params= $e->getParams();
      printf(‘Handled event %s with parameters %s, 
      $event, json_enccode($params));
    });
    $params=array(‘foo’ => ‘bar’, ‘baz’ => ‘bat’);
    $events->trigger(‘do, null, $params);
     

    Listen and react to trigger events to a group of objects or objects that don’t exist yet.

    $shared = $events->getSharedEventManager();
    $shared =  $services->get(‘SharedEventManager’);

    $shared->attach(‘ZendStdlibDispatchableInterface’, ‘dispatch’, $callback, $priority);

    Everything is an event

    Boostrap listeners run modules, route listeners run modules, dispatch listeners run modules.

    Controllers are services

    Use ServiceManager to wire default workflow and event listeners.

    Plug and play modules

    They should do one thing and do it well. They provide services for MVC and wire event listeners.

     Modules are implemented as a namespace with a single Module.php class file.

     

     

    Module example

    Edit module/MyMarkdown/Module.php

    namespace  MyMarkdown;

    use ZendViewHelperAbstractHelper;

    class Module extends AbstractHelper
    {
      public function getViewHelperConfig(){
        return array(‘services’, array(‘markdown’ => $this));
      }
      public function __invoke($string = null){
        require_once ‘php-markdown/markdown.php’;
        return Markdown($string);
      }
    }

    Add the MyMarkdown module to application in config/application.config.php

    return array(
      ‘modules’ => array(
        ‘Application’,
        ‘MyMarkdown’,
      ),
      /* … */
    ); 

    Test it in view script

    <?= $this->markdown($this->someMarkdowntext) ?> 

    What you can do if the module you want exist? Use ‘git submodule add’, add module name to your config/application.config.php file and you are done.

     

    Controller example

    They are very similar than in ZF1. In ZF2 you return something.

    namespace ApplicationController;

    use ZendMvcControllerAbstractActionController;
    use ZendViewModelViewModel;

    class IndexController extends AbstractActionController
    {
      public function indexAction()
      {
        return new ViewModel();
      }

     

    Key points to remember for using ZF2

    • Know to configure services.
    • Know to wire event listeners.
    • Controllers return informa
      tion as they are now services.
    • Modules inform the MVC of services (controllers!) and wire events.

     

    Secure Zend Framework programing in ZF2

    15/02/2013

    Comments on this webinar:

    What are the most common security risks?

    OWASP Top 10 Application Security Risks – 2010

    A1-Injection Injection flaws, such as SQL, OS, and LDAP injection, occur when untrusted data is sent to an interpreter as part of a command or query. The attacker’s hostile data can trick the interpreter into executing unintended commands or accessing unauthorized data.
    A2-Cross Site Scripting (XSS) XSS flaws occur whenever an application takes untrusted data and sends it to a web browser without proper validation and escaping. XSS allows attackers to execute scripts in the victim’s browser which can hijack user sessions, deface web sites, or redirect the user to malicious sites.
    A3-Broken Authentication and Session Management Application functions related to authentication and session management are often not implemented correctly, allowing attackers to compromise passwords, keys, session tokens, or exploit other implementation flaws to assume other users’ identities.
    A4-Insecure Direct Object References A direct object reference occurs when a developer exposes a reference to an internal implementation object, such as a file, directory, or database key. Without an access control check or other protection, attackers can manipulate these references to access unauthorized data.
    A5-Cross Site Request Forgery (CSRF) A CSRF attack forces a logged-on victim’s browser to send a forged HTTP request, including the victim’s session cookie and any other automatically included authentication information, to a vulnerable web application. This allows the attacker to force the victim’s browser to generate requests the vulnerable application thinks are legitimate requests from the victim.
    A6-Security Misconfiguration Good security requires having a secure configuration defined and deployed for the application, frameworks, application server, web server, database server, and platform. All these settings should be defined, implemented, and maintained as many are not shipped with secure defaults. This includes keeping all software up to date, including all code libraries used by the application.
    A7-Insecure Cryptographic Storage Many web applications do not properly protect sensitive data, such as credit cards, SSNs, and authentication credentials, with appropriate encryption or hashing. Attackers may steal or modify such weakly protected data to conduct identity theft, credit card fraud, or other crimes.
    A8-Failure to Restrict URL Access Many web applications check URL access rights before rendering protected links and buttons. However, applications need to perform similar access control checks each time these pages are accessed, or attackers will be able to forge URLs to access these hidden pages anyway.
    A9-Insufficient Transport Layer Protection Applications frequently fail to authenticate, encrypt, and protect the confidentiality and integrity of sensitive network traffic. When they do, they sometimes support weak algorithms, use expired or invalid certificates, or do not use them correctly.
    A10-Unvalidated Redirects and Forwards Web applications frequently redirect and forward users to other pages and websites, and use untrusted data to determine the destination pages. Without proper validation, attackers can redirect victims to phishing or malware sites, or use forwards to access unauthorized pages.

     

    “Filter input, escape output”
    Yest, but it’s not enough.

    Security tools in ZF2

    • ZendAuthentication
    • ZendPermissions
    • ZendFilter
    • ZendValidator
    • ZendInputFilter
    • ZendEscaper
    • ZendCaptcha
    • ZendCrypt
    • ZendMath

     

      ZendAuthentication

      API for authentication and includes concrete authentication adapters for common use case scenarios:

      • Database table
      • Digest
      • HTTP
      • LDAP
      • Or your custom authentication adapter…

      Example of ZendAuthentication use:

        use ZendAuthenticationAuthenticationService;$auth = new AuthenticationService();$authAdapter = new MyAuthAdapter($username, $password);if(!$result->isValid()){//auth failedvar_dump($result->getMessages());}else{//auth success, $username is stored in the session}

        ZendPermissions

        Proviedes access control list (ACL) for privileges management

        • Resource: An object which access is controlled
        • Role: An object that may request access to a resource.

        Example of ZendPermissions use:

        use ZendPermissionsAclAcl;use ZendPermissionsAclRoleGenericRole as Role;use ZendPermissionsAclResourceGenericResource as Resource;$acl = new Acl();$acl->addRole(new Role('guest'));    ->addRole(new Role('member'));    ->addRole(new Role('admin'));$parents = array('guest', 'member', 'admin');$acl->addRole(new('someUser'), $parents);$acl->addResource(new Resource('someResource'));$acl->deny('guest', 'someResource');$acl->deny('member', 'someResource');echo $acl->isAllowed('someUser', 'someResource')? 'allowed' : 'denied';

        ZendPermissionsRbac adapter in ZF2.1 is a new Role-Based Access Control based in PHP 5.3 SPL RecursiveIterator. It put emphasis on roles and their permissions rather than resource objects:

        • identity: one or more roles
        • role: requests acces to a permission
        • permission is givet to a role.

         

        ZendFilter

        Provides a set of commonly needed data filters and a chainging mechanism by which multiple filters may be applied.

        Standard Filter Classes…

        • Alnum
        • Alpha
        • BaseName
        • Boolean
        • Compress/Decompress
        • Digits
        • Dir
        • Encrypt/Decrypt
        • HtmlEntities
        • Int
        • Null
        • NumberFormat
        • PregReplace
        • RealPath
        • StringToLower/ToUpper
        • StringTrim
        • StripNewLines/Tags
        • Callback (custom filter)

         

        ZendValidator

        Provides a set of commonly needed validators and a validator chainging mechanism by which multiple validators may be applied.

        It examines its input with respecto to requirements and
        productes a boolean result whether the input successfully validates against the requirements.

        $validator = new ZendValidatorEmailAddress;
        if(!$validator->isValid($email)){//valid email}else{//print reasons email is invalidvar_dump($validator->getMessages());}

         

        Standard Validator Classes…

        • Alnum
        • Alpha
        • Barcode
        • Between
        • CreditCard
        • Date
        • DbRecordExists and NoRecordExists
        • Digits
        • EmailAddress
        • GreaterThan/LessThan
        • Hex
        • Hostname
        • Iban
        • Identical
        • InArray
        • Ip
        • Isbn
        • NotEmpty
        • PostCode
        • Regex
        • Sitemap
        • Step
        • StringLength
        • Callback (custom filter)

         

        ZendInputFilter

        Can be used to filter and validate generic sets of input data. For instance $_GET, $_POST, CLI, etc

        use ZendInputFilterInputFilter;use ZendInputFilterInput;use ZendValidator;$email = new Input('email');$email->getValidatorChain()  ->addValidator(new ValidatorEmailAddress());$password = new Input('password');$password ->getValidatorChain()  ->addValidator(new ValidatorStringLength(8));$inputFilter = new InputFilter();$inputFilter->add($email)            ->add($password)            ->setData($_POST);if(!$inputFilter->isValid()){//form is valid}else{//print reasons form is invalidvar_dump($inputFilter->getInvalidInput());}

         

         

        ZendEscaper

        Always escape the output, multiple formats:

        • escapeHtml()
        • escapeHtmlAttr()
        • escapeJs()
        • escapeUrl()
        • EscapeCss()

        ZendCaptcha

        Prevent submision from automated tools (Figlet, AbstractWord, Dumb, Image, ReCaptcha)

        $captcha = new ZendCaptchaFiglet(array( 'name=>'foo', 'wordLen'=>6, 'timeout'=>300,));$id = $captcha->generate();echo $captcha->getFiglet()->render($captcha->getWord());//On subsequent requestif($captcha->isValid($_POST['foo'], $_POST)){  //validated}

         

        ZendCrypt

        Use cryptography in a convenient way (instead of php built-in functions and extensions crypt, mcrypt, openssl, hash, mhash):

        • ZendCryptPassword
        • ZendCryptKeyDerivation
        • ZendCryptSymmetric
        • ZendCryptPublicKey
        • ZendCryptHash
        • ZendCryptHmac
        • ZendCryptBlockCipher

        Example of how to encrypt sensitive data:

        • ZendCryptBlockCipher symmetric encryption and authenication (HMAC)
        • Simplified API: setKey($key), encrypt($data), decrupt($data) uses Mcrypt adapter (Zend CryptSymmetricMcrypy) for us.
        • Defult values used by BlockCipher:
          • AES (256 bits key)
          • CBC mode + HMAC (SHA-256)
          • PKCS6 padding mode (RFC 5652)
          • PBKDF2 to generate encryption key + auth key for HMAC
          • Random IV for each encryption

        BlockCipher using AES encryption

        use ZendCryptBlockCipher;$cipher = BlockCipher::factory(  'mcrypt',   array('algorithm' => 'aes'));$cipher->setKey('this is the encryption key');$plaintext = 'This is the sensitive message to encrypt';//encoded base64, get binary using setBinaryOutput(true)$encrypted = $cipher->encrypt($plaintext);// You will see three parts: HMAC, IV and ciphertextprintf("encrypted text: %sn", $encrypted); file_put_contents('test.crypt',  $encrypted);

         

        BlockCipher decryption

        use ZendCryptBlockCipher;$cipher = BlockCipher::factory(  'mcrypt',   array('algorithm' => 'aes'));$cipher->setKey('this is the encryption key');$encrypted = file_get_contents('test.crypt');$plaintext = $cipher->decrypt($encrypted );printf("Dencrypted text: %sn", $plaintext );

         

        Example of how to store a password:

        Insecure, old school: MD5 password or/and salt random string (In a 4 core CPU you can break md5 8 char passwords in less than 2 hours)

        New way: bcrypt (Blowfish cipher is secure way now)

        bcrypt is secure because is very slow and attacks need huge amount of time to be completed (cost value determine how expensive is bcrypt, by default 14 that is equivalent to 1 second of computation using Core i5 CPU at 3.3Ghz.. Check taht your system use at least 1 second)

        Example of how to encrypt the password with bcrypt:

        use ZendCryptPasswordBcrypt;$bcrypt = new Bcrypt();$start = microtime(true);$hash =$bcrypt->create('password');$end = microtime(true);printf("Hash      : %s, %hash); //string of 60 bytesprintf("Exec. time: %.2fn", $end-$start);

         

        To check if password the user is valid, you verify them using the hash:

        $is_valid = bcrypt::verify($password, $hash)

         

        ZendMath

        PHP generate pseudo-random values but they are not good for cryptography purposes.

        You can get secure random numbers in PHP if you have openssl PHP extension:

        openssl_random_pseudo_bytes()

        Zend use most secure available function at the moment, never PHP primitives if $strong=true.

        ZendMathMath:randBytes($lenggth, $strong=false)

        ZendMathMath:rand($min, $max, $strong=false)

         

        ??? php encoding problems by mysql collation

        14/02/2013

        The problem in php encoding with latin1 in mysql collation:

        Unicode-ruso-ru-php-mysql

        The solution is to change mysql collation in table and column fields to utf8_unicode_ci:

        Unicode-ruso-ru-phpmyadmin

        The conclusion is that you should use utf8_unicode_ci collation in mysql for your projects so as to they can be internationalized.

        Magento Spanish Email Templates

        12/02/2013

        Spanish translation for magento does'nt include that files.??

        Here you have the spanish email templates that i use for magento in spanish.
        Unzip this file on your Magento DocumentRoot directory.

        Changelog:??MARIA SALUD MOLINA BELDA is out.

        Security PHP functions for secure PHP programming

        05/02/2013

        PHP validation functions

        is_numeric()

        is_array()

        strlen()

        ctype_alnum()

        ctype_alpha()

        ctype_cntrl()

        ctype_digit()

        ctype_xdigit()

        ctype_lower()

        ctype_upper()

        ctype_print()

        ctype_punct()

        ctype_space()

         

        PHP filter functions

        filter_input()

        filter_var()

         

        PHP validation filters

        FILTER_VALIDATE_INT

        FILTER_VALIDATE_BOOLEAN

        FILTER_VALIDATE_FLOAT

        FILTER_VALIDATE_REGEXP

        FILTER_VALIDATE_URL

        FILTER_VALIDATE_EMAIL

        FILTER_VALIDATE_IP

         

        PHP sanitizing filters

        FILTER_SANITIZE_STRIPPED

        FILTER_SANITIZE_ENCODED

        FILTER_SANITIZE_SPECIAL_CHARS

        FILTER_SANITIZE_EMAIL

        FILTER_SANITIZE_URL

        FILTER_SANITIZE_NUMBER_INT

        FILTER_SANITIZE_NUMBER_FLOAT

        FILTER_SANITIZE_MAGIC_QUOTES

        FILTER_CALLBACK

         

        PHP escaping and encoding functions

        htmlspecialchars(); //echo htmlspecialchars($_GET[‘name’], ‘utf-8’, ENT_QUOTES);

        htmlentities(); //echo htmlentities($_GET[‘name’], ‘utf-8’, ENT_QUOTES);

        urlencode(); //$url = ‘http://www.domain.com/?param=&#8217;.urlencode($_GET[‘param’]);

        addslashes()

        addcslashes()

        mysql_real_escape_string()  deprecated as of PHP 5.5.0 

         mysqli_real_escape_string() or use PDO::prepare

        preg_quote()

        escapeshellarg()

        escapeshellcmd()

        How to see what fails when you get internal server error in PHP (500 HTTP Error)

        31/01/2013

        Add this lines as first lines when debugging in maintenance mode:

        error_reporting(E_ALL | E_STRICT);

        ini_set(‘display_errors’, 1);

         

        In Magento you can set developer mode:

        Mage::setIsDeveloperMode(true);