Source for file gmail.php

Documentation is available at gmail.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Authentication.gmail
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC') or die;
  11.  
  12. /**
  13.  * GMail Authentication Plugin
  14.  *
  15.  * @package     Joomla.Plugin
  16.  * @subpackage  Authentication.gmail
  17.  * @since       1.5
  18.  */
  19. {
  20.     /**
  21.      * This method should handle any authentication and report back to the subject
  22.      *
  23.      * @param   array   $credentials  Array holding the user credentials
  24.      * @param   array   $options      Array of extra options
  25.      * @param   object  &$response    Authentication response object
  26.      *
  27.      * @return  boolean 
  28.      *
  29.      * @since   1.5
  30.      */
  31.     public function onUserAuthenticate($credentials, $options, &$response)
  32.     {
  33.         $success = 0;
  34.  
  35.         // Check if we have curl or not
  36.         if (function_exists('curl_init'))
  37.         {
  38.             // Check if we have a username and password
  39.             if (strlen($credentials['username']) && strlen($credentials['password']))
  40.             {
  41.                 $blacklist = explode(',', $this->params->get('user_blacklist', ''));
  42.  
  43.                 // Check if the username isn't blacklisted
  44.                 if (!in_array($credentials['username'], $blacklist))
  45.                 {
  46.                     $suffix = $this->params->get('suffix', '');
  47.                     $applysuffix = $this->params->get('applysuffix', 0);
  48.                     $offset = strpos($credentials['username'], '@');
  49.  
  50.                     // Check if we want to do suffix stuff, typically for Google Apps for Your Domain
  51.                     if ($suffix && $applysuffix)
  52.                     {
  53.                         if ($applysuffix == 1 && $offset === false)
  54.                         {
  55.                             // Apply suffix if missing
  56.                             $credentials['username'] .= '@' . $suffix;
  57.                         }
  58.                         elseif ($applysuffix == 2)
  59.                         {
  60.                             // Always use suffix
  61.                             if ($offset)
  62.                             {
  63.                                 // If we already have an @, get rid of it and replace it
  64.                                 $credentials['username'] = substr($credentials['username'], 0, $offset);
  65.                             }
  66.  
  67.                             $credentials['username'] .= '@' . $suffix;
  68.                         }
  69.                     }
  70.  
  71.                     $curl = curl_init('https://mail.google.com/mail/feed/atom');
  72.                     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  73.                     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->params->get('verifypeer', 1));
  74.                     //curl_setopt($curl, CURLOPT_HEADER, 1);
  75.                     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  76.                     curl_setopt($curl, CURLOPT_USERPWD, $credentials['username'] . ':' . $credentials['password']);
  77.                     curl_exec($curl);
  78.                     $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  79.  
  80.                     switch ($code)
  81.                     {
  82.                         case 200:
  83.                             $message = JText::_('JGLOBAL_AUTH_ACCESS_GRANTED');
  84.                             $success = 1;
  85.                             break;
  86.  
  87.                         case 401:
  88.                             $message = JText::_('JGLOBAL_AUTH_ACCESS_DENIED');
  89.                             break;
  90.  
  91.                         default:
  92.                             $message = JText::_('JGLOBAL_AUTH_UNKNOWN_ACCESS_DENIED');
  93.                             break;
  94.                     }
  95.                 }
  96.                 else
  97.                 {
  98.                     // The username is black listed
  99.                     $message = JText::_('JGLOBAL_AUTH_USER_BLACKLISTED');
  100.                 }
  101.             }
  102.             else
  103.             {
  104.                 $message = JText::_('JGLOBAL_AUTH_USER_BLACKLISTED');
  105.             }
  106.         }
  107.         else
  108.         {
  109.             $message = JText::_('JGLOBAL_AUTH_CURL_NOT_INSTALLED');
  110.         }
  111.  
  112.         $response->type = 'GMail';
  113.  
  114.         if ($success)
  115.         {
  116.             $response->status        = JAuthentication::STATUS_SUCCESS;
  117.             $response->error_message = '';
  118.  
  119.             if (strpos($credentials['username'], '@') === false)
  120.             {
  121.                 if ($suffix)
  122.                 {
  123.                     // If there is a suffix then we want to apply it
  124.                     $response->email = $credentials['username'] . '@' . $suffix;
  125.                 }
  126.                 else
  127.                 {
  128.                     // If there isn't a suffix just use the default gmail one
  129.                     $response->email = $credentials['username'] . '@gmail.com';
  130.                 }
  131.             }
  132.             else
  133.             {
  134.                 // The username looks like an email address (probably is) so use that
  135.                 $response->email = $credentials['username'];
  136.             }
  137.  
  138.             // Reset the username to what we ended up using
  139.             $response->username = $credentials['username'];
  140.             $response->fullname = $credentials['username'];
  141.         }
  142.         else
  143.         {
  144.             $response->status        = JAuthentication::STATUS_FAILURE;
  145.             $response->error_message = JText::sprintf('JGLOBAL_AUTH_FAILED', $message);
  146.         }
  147.     }
  148. }

Documentation generated on Tue, 19 Nov 2013 15:04:01 +0100 by phpDocumentor 1.4.3