Thursday 8 March 2018

Monday 3 July 2017

Rajendra Thakur: HybridAuth : Steps to use this library for social ...

Rajendra Thakur: HybridAuth : Steps to use this library for social ...: Example of  HybridAuth Library for Social Login i.e. Facebook, Google, LinkedIn and Twiter Requirements :  CakePHP 3.x, If you don&#3...

HybridAuth : Steps to use this library for social login in CakePHP 3.x

Example of HybridAuth Library for Social Login i.e. Facebook, Google, LinkedIn and Twiter

Requirements:

  •  CakePHP 3.x, If you don't have CakePHP3.x click here to install 
  •  Composer, If you don't have composer click here to install.
  •  Should have User login/Authentication - CakePHP 3.x


Installation:
 Run: composer require --prefer-dist admad/cakephp-hybridauth

Setup:
 Load HybridAuth Plugin into your CakePHP application run below command
 Run: bin/cake plugin load ADmad/HybridAuth -b -r

Or you can add it manually, add below line into your app's config/bootstrap.php file:
 Plugin::load('ADmad/HybridAuth', ['bootstrap' => true, 'routes' => true]);

Configuration:
 Create a file: config/hybridauth.php inside of application folder, add similar to this script:

    use Cake\Core\Configure;

    return [
       'HybridAuth' => [
          'providers' => [
             'Google' => [
                'enabled' => true,
                'keys' => [
                    'id' => '<google-client-id>',
                    'secret' => '<secret-key>'
                ],
                "scope" => 'https://www.googleapis.com/auth/plus.login
                https://www.googleapis.com/auth/plus.profile.emails.read', // These two URL's required for google login
                "approval_prompt" => "force" // Force required here
           ],
           'Facebook' => [
              'enabled' => true,
              'keys' => [
                 'id' => '<facebook-application-id>',
                 'secret' => '<secret-key>'
              ],
              'scope' => 'email, user_about_me, user_hometown' //whatever you want from facebook you need to add it here
           ],
           'LinkedIn' => [
               'enabled' => true,
               'keys' => [
                  'id' => '<linkedin-key>',
                  'secret' => '<secret-key>'
               ],
               "scope"   => "r_basicprofile", // optional if you want to get email Id as well add this in scope:  r_emailaddress
            ],
            'Twitter' => [
                 'enabled' => true,
                 'keys' => [
                    'key' => '<twitter-key>',
                    'secret' => '<twitter-secret>'
                 ],
                 'includeEmail' => true // Only if your app is whitelisted by Twitter Support
             ]
        ],
        'debug_mode' => Configure::read('debug'),
        'debug_file' => LOGS . 'hybridauth.log',
     ]
 ];


Click here for more information about the HybridAuth.

Database:

 Create a table “social_profiles” run below command:
    $ bin/cake migrations migrate -p ADmad/HybridAuth 

Usage:

In AppController’s initialize() method add below script for allowing social login:

# FOR ALLOWING FACEBOOK LOGIN ALSO

parent::initialize();
$this->loadComponent('Auth', [
  'authenticate' => [
    'Form',
    'ADmad/HybridAuth.HybridAuth' => [
      // All keys shown below are defaults
      'fields' => [
        'provider' => 'provider',
        'openid_identifier' => 'openid_identifier',
        'email' => 'email'
      ],

      'profileModel' => 'ADmad/HybridAuth.SocialProfiles',
      'profileModelFkField' => 'user_id',
      'userModel' => 'Users',

      // The URL Hybridauth lib should redirect to after authentication.
      // If no value is specified you are redirect to this plugin's
      // HybridAuthController::authenticated() which handles persisting
      // user info to AuthComponent and redirection.
      'hauth_return_to' => [
        'controller'  => 'MyController',
        'action'      => 'update_status',
        'plugin' => false
      ]
    ]
  ],
  'loginRedirect' => array('controller' => '<controller Name>', 'action' => '<Method name>'),
  'logoutRedirect' => array('controller' => '<controller Name>', 'action' => '<Method name'),
]);

Your UserController’s login method should be similar to this:
    public function login() {
     if ($this->request->is('post') || $this->request->query('provider')) {
       $user = $this->Auth->identify();
       if ($user) {
         $this->Auth->setUser($user);
         return $this->redirect($this->Auth->redirectUrl());
       }
       $this->Flash->error(__('Invalid username or password, try again'));
    }
 }

On your login page you can create links to initiate authentication using required providers. Specify the provider name using variable named provider in query string.

     echo $this->Form->postLink(
         'Login with Google',
          ['controller' => 'Users', 'action' => 'login', '?' => ['provider' => 'Google']]
     );

Setup a method of your UsersTable as callback for the event:
public function initialize(array $config)
{
  parent::initialize($config);

  $this->setTable('users');
  $this->setDisplayField('id');
  $this->setPrimaryKey('id');
  $this->addBehavior('Timestamp');

  $this->hasMany('ADmad/HybridAuth.SocialProfiles');
  \Cake\Event\EventManager::instance()->on('HybridAuth.newUser', [$this, 'createUser']);
}


public function createUser(\Cake\Event\Event $event) {
    // Entity representing record in social_profiles table
    $profile = $event->data()['profile'];

    // Make sure here that all the required fields are actually present

    $user = $this->newEntity(['email' => $profile->email]);
    $user = $this->save($user);

    if (!$user) {
        throw new \RuntimeException('Unable to save new user');
    }

    return $user;
}

Create a Model src/Model/SocialProfilesTable.php and add similar to this:
    namespace ADmad\HybridAuth\Model\Table;
    use Cake\ORM\Table;

   /**
   * HybridAuth Authenticate
   *
   * Licensed under The MIT License
   * For full copyright and license information, please see the LICENSE.txt
   */
   class SocialProfilesTable extends Table
   {
      /**
       * Initialize table.
       *
       * @param array $config Configuration
       * @return void
       */
       public function initialize(array $config)
      {
          parent::initialize($config);
          $this->addBehavior('Timestamp');
          $this->belongsTo('Users');
      }
   }

Any one have questions or doubt please ✋ your hands.

Friday 7 December 2012

get vocabulary list dropdown programmatically

/**
 * programmatically show taxonomy dropdown
 */
$taxonomy_source = taxonomy_get_tree(2);
$options_arr = array('0' => '--none--');
foreach($taxonomy_source as $taxonomy_items) {
    $tid = $taxonomy_items->tid;
    $t_name = $item->name;
    $options_arr[$tid] = $t_name;
}

/**
 * Category Dropdown
 */
$form['filterset']['category'] = array(
  '#weight' => '1',
  '#key_type' => 'associative',
  '#multiple_toggle' => '1',
  '#type' => 'select',
  '#options' => $options_arr,
  '#title' => 'Categories',
);

unset($options_arr);

Tuesday 4 December 2012

Exposed form show as Block

Go to Advance section and click on Exposed form Exposed in block: No and set it to Yes. Then go to block and manually enable it to any Region.

Tuesday 2 October 2012

About Drupal 7 Hook, Form API, Token, Triggers, Cache, Performance optimization

  • About Drupal Hook.
    In programming, a hook is a place and usually an interface provided in packaged code that allows a programmer to insert customized programming. Allow modules to interact with the Drupal core.

    Drupal’s module system is based on the concept of “hooks”. A hook is a PHP function that is named foo_bar(), where “foo” is the name of the module (whose filename is thus foo.module) and “bar” is the name of the hook. Each hook has a defined set of parameters and a specified result type.

    For example, if the module file is called example.module, then hook_help() as implemented by that module would be defined as example_help().
  • About Drupal Form API.
    Drupal's Form API is a powerful tool allowing developers to create forms and handle form submissions quickly and easily. This is done by defining arrays of form elements and creating validation and submit callbacks for the form.

    function form_example_tutorial_1($form, &$form_state) {
      $form['description'] = array(
        '#type' => 'item',
        '#title' => t('A form with nothing but a textfield'),
      );
      // This is the first form element. It's a textfield with a label, "Name"
      $form['name'] = array(
        '#type' => 'textfield',
        '#title' => t('Name'),
      );
      return $form;
    }
  • About Drupal Token.
    A token is a small piece of text that can be placed into a piece of text via the use of a placeholder. When the text is passed through the function token_replace(), then the tokens are replaced with the appropriate information. Tokens allow users to include data that could change in text blocks, without having to go back and change it everywhere they're referenced.

    A sample token is [site:name]. When text containing this token is passed through token_replace(), it is replaced with your site's name as defined in Home | Administer | Configuration | Site information.
  • About Drupal Triggers.
    The Trigger module provides the ability to take a particular action when a trigger occurs.

    Actions include functional tasks that are defined by Drupal core modules, and advanced actions that can be defined by the system administrator. Contributed modules can introduce new actions. Some examples of actions are sending an email, publishing a node, banning a user or promoting a node to the front page of a website.

    The Trigger module allows you to perform useful tasks by associating actions with specific triggers, such as e-mailing an administrator (the action) when a user account is deleted (the trigger), or automatically unpublishing a comment (the action) if the added comment (the trigger) contains certain words. The Trigger module is included in the core Drupal installation. The module must be enabled to use it.
  • About Drupal Cache.
    A collection of data duplicating original values stored elsewhere on a computer Drupal's APIs provide three key functions you'll need to be familiar with: cache_get(), cache_set(), and cache_clear_all().

    cache_get($cid, $bin = 'cache') Data may be stored as either plain text or as serialized data. cache_get will automatically return unserialized objects and arrays.

    Parameters
    $cid: The cache ID of the data to retrieve.

    $bin: The cache bin to store the data in. Valid core values are 'cache_block', 'cache_bootstrap', 'cache_field', 'cache_filter', 'cache_form', 'cache_menu', 'cache_page', 'cache_path', 'cache_update' or 'cache' for the default cache.

    cache_set($cid, $data, $bin = 'cache', $expire = CACHE_PERMANENT)
    Stores data in the persistent cache.

    The persistent cache is split up into several cache bins. In the default cache implementation, each cache bin corresponds to a database table by the same name. Other implementations might want to store several bins in data structures that get flushed together. While it is not a problem for most cache bins if the entries in them are flushed before their expire time, some might break functionality or are extremely expensive to recalculate. These will be marked with a (*). The other bins expired automatically by core. Contributed modules can add additional bins and get them expired automatically by implementing hook_flush_caches().

    Cache: Generic cache storage bin (used for variables, theme registry,
    locale date, list of simpletest tests etc).

    cache_block: Stores the content of various blocks.
    cache field: Stores the field data belonging to a given object.
    cache_filter: Stores filtered pieces of content.
    cache_form(*): Stores multistep forms. Flushing this bin means that some
    forms displayed to users lose their state and the data already submitted to them.

    cache_menu: Stores the structure of visible navigation menus per page.
    cache_page: Stores generated pages for anonymous users. It is flushed
    very often, whenever a page changes, at least for every ode and comment submission. This is the only bin affected by the page cache setting on the administrator panel.

    cache path: Stores the system paths that have an alias.
    cache update(*): Stores available releases. The update server (for
    example, drupal.org) needs to produce the relevant XML for every project installed on the current site. As this is different for (almost) every site, it's very expensive to recalculate for the update server.

    The reasons for having several bins are as follows:
    Smaller bins mean smaller database tables and allow for faster selects and inserts we try to put fast changing cache items and rather static ones into different bins. The effect is that only the fast changing bins will need a lot of writes to disk. The more static bins will also be better cacheable with MySQL's query cache.
    Parameters

    $cid: The cache ID of the data to store.

    $data: The data to store in the cache. Complex data types will be automatically serialized before insertion. Strings will be stored as plain text and not serialized.

    $bin: The cache bin to store the data in. Valid core values are 'cache_block', 'cache_bootstrap', 'cache_field', 'cache_filter', 'cache_form', 'cache_menu', 'cache_page', 'cache_update' or 'cache' for the default cache.

    $expire: One of the following values:

    CACHE_PERMANENT: Indicates that the item should never be removed unless explicitly told to using cache_clear_all() with a cache ID.
    CACHE_TEMPORARY: Indicates that the item should be removed at the next general cache wipe.
    A Unix timestamp: Indicates that the item should be kept at least until the given time, after which it behaves like CACHE_TEMPORARY.

    cache_clear_all($cid = NULL, $bin = NULL, $wildcard = FALSE)
    Expires data from the cache.

    If called without arguments, expirable entries will be cleared from the cache_page and cache_block bins.

    Parameters

    $cid: If set, the cache ID to delete. Otherwise, all cache entries that can expire are deleted.

    $bin: If set, the cache bin to delete from. Mandatory argument if $cid is set.

    $wildcard: If TRUE, cache IDs starting with $cid are deleted in addition to the exact cache ID specified by $cid. If $wildcard is TRUE and $cid is '*', the entire cache bin is emptied.
  • About Performance optimization of Drupal Website.
    The first step is to consider what part of your site is experiencing problems. A particular page or View.
    Another issue to consider is whether it's the backend that's slow, or the frontend.

    Secondly, Take a look at the load on your CPU when you notice poor performance as serving complex PHP applications can be quite taxing.

    On every production Drupal site I'll take the following steps:
    (1)   Enable CSS/JS compression
    (2)   Make sure APC is enabled and properly configured
    (3)   Disable unnecessary modules
    (4)   Clean up as many PHP errors as possible, especially if we're logging with the dblog or syslog modules
    (5)   Make sure the MySQL query cache is enabled and properly configured
    (6)   Sprite images where possible for frontend performance benefits

    We can go further to attempt to address outstanding issues:

    (1)   Cache expensive or time consuming operations, for example, requesting data from a web service. Drupal makes this simple with cache_set() / cache_get().
    (2)   Turn on additional caching, such as Views or Block, if possible
    (3)   Use the MySQL slow query log and run EXPLAIN on the logged queries. Look for queries which have joins using filesort or temporary tables and determine whether          these could be rewritten or sped up with an index.
    (4)   Analyze the traffic pattern. A site with a largely anonymous user base can see a significant benefits from implementing the Boost module or Varnish in to the         server stack.
    (5)   Change cache backends. Drupal reads and writes a lot of data from its caches (as it should) so you can see some great speed improvements storing this data                in memory (fast!) rather than the database (on the disk, slow!).


    While many things can contribute to a slowed-down site, there are some common issues that affect many customized Drupal setups. These common issues are :-

    Server Configuration
    User Types, Browsing Habits and Connections
    Using External Resources
    Database Configuration
    Image Handling
    Know Your Cache

    Helpful Drupal Modules
    Once you've completed the simple "do it yourself" steps, there are some modules offered by Drupal's community to help boost performance. Be sure to read through each module's documentation carefully, though - these are not simple plug-n-play modules. They require some configuration to get installed correctly.

    1. Memcache API and Integration

    The Memcache API and Integration module serves as, you guessed it, an integration between Drupal and Memcached.

    Memcached works to improve dynamic web applications by alleviating database load. It helps to optimize your site's use of available memory for caching. The installation looks scary, so be sure to read through every thing carefully to make sure that you've got it set correctly. However, once set, you'll see that it's totally worth the effort. Make sure your webhost supports memcache before trying this module.

    2. APC - Alternative PHP Cache

    The APC - Alternative PHP Cache module works to provide a reliable framework for caching and optimizing PHP intermediate code. It is best suited for caches which are not expected to change often or grow beyond a certain size.

    3. Boost

    The Boost module is another module that requires a little extra effort to get installed and working correctly. But, once it's up and running, you'll be glad you put in that extra effort.

    Unlike some other caching modules, Boost has been tested and works well on all sorts of server types - Shared, VPS, Dedicated. This module provides static page caching, which will have significant impact on your site's performance if your site's visitors are primarily anonymous visitors. If your site's traffic is mostly logged in / authenticated users, then Boost won't help you all that much.

    4. Varnish HTTP Accelerator Integration

    The Varnsh HTTP Accelerator Integration module works as a stack above Apache, helping to serve up cached files and page views faster for anonymous visitors.

    And My Personal Favorite Is...

    Personally, I use either Boost or Memcache depending on a particular site's needs. Boost if the traffic is mostly anonymous, and Memcache if the traffic is mostly registered members. Seems fairly cut and dry, right?

    But, in recent years, I have been working with more and more clients where it hasn't been quite so cut and dry. Their traffic has been almost equal between anonymous and logged in / authenticated users. So, you may like to know that these two modules play nicely together when installed on the same site - without any more work or configuration beyond their normal installation.

Saturday 11 August 2012

domdocument::domdocument() expects at least 1 parameter, filter.module

Using xampp (windows machine ) it seems that the package is wrong. 
You need to  Commenting out "php_domxml.dll" in php.ini file error has been solved solves it.