<?php
/*
Plugin Name: LCAS
Version: 2.1.0
Description: Allow to disable login/register name to be sensible to the case/accents
Plugin URI: http://fr.piwigo.org/forum/viewtopic.php?pid=158563
Author: Eric, LucMorizur, Whiler
Author URI: http://www.infernoweb.net, http://blogs.wittwer.fr/whiler/
*/

/* History:  LCAS_PATH.'Changelog.txt.php' */

/*
 ***** TODO List *****
See project bugtracker: http://piwigo.org/bugs/my_view_page.php
*/

if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
if (!defined('LCAS_DIR')) define('LCAS_DIR' , basename(dirname(__FILE__)));
if (!defined('LCAS_PATH')) define('LCAS_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');

include_once (LCAS_PATH.'include/constants.php');
include_once (LCAS_PATH.'include/functions.inc.php');

load_language('plugin.lang', LCAS_PATH);

$t = pwg_db_fetch_row(pwg_query('
  SELECT `value`
  FROM `'.CONFIG_TABLE.'`
  WHERE `param` = "LoginCaseAccentsSensitivity"
  LIMIT 1;
'));

$conf['LoginCaseAccentsSensitivity'] = $t[0];

/* Plugin admin */
add_event_handler('get_admin_plugin_menu_links', 'LCAS_admin_menu');

function LCAS_admin_menu($menu)
{
// +-----------------------------------------------------------------------+
// |                      Getting plugin name                              |
// +-----------------------------------------------------------------------+
  $plugin =  LCAS_PluginInfos(LCAS_PATH);
  $name = $plugin['name'];
  
  array_push($menu,
    array(
      'NAME' => $name,
      'URL'  => get_admin_plugin_menu_link(LCAS_PATH.'/admin/LCAS_admin.php')
    )
  );

  return $menu;
}

// Check users identification
add_event_handler('init', 'LCAS_InitPage');
 
function LCAS_InitPage()
{
  global $conf;

  $conf_LCAS= unserialize($conf['LoginCaseAccentsSensitivity']);
  
  // Set $conf['insensitive_case_logon'] to false, as LCAS takes completely
  // in charge everything around the case and the accents
  $conf['insensitive_case_logon'] = false;
 
/* User identification */
  if (script_basename() == 'identification')
  {
    if (isset($_POST['username']) and isset($conf_LCAS[1]))
    {
      $new_username = LCAS_SearchCaseUsername($_POST['username'],$conf_LCAS[1]);
      $_POST['username'] = $new_username == '' ? $_POST['username'] : $new_username;
    }
  }
}

// Check users registration
// Returns error
add_event_handler('register_user_check', 'LCAS_RegistrationCheck');

function LCAS_RegistrationCheck($errors)
{
  global $conf, $lang;
  
  $conf_LCAS= unserialize($conf['LoginCaseAccentsSensitivity']);
  
  load_language('plugin.lang', LCAS_PATH);

  if (isset($conf_LCAS[1]))
   $NewPostLogin = LCAS_SearchCaseUsername($_POST['login'], $conf_LCAS[1]);
  
  if (isset($NewPostLogin) and get_userid($NewPostLogin))
   $errors[] = $lang['LCAS_error'][intval($conf_LCAS[1])];
  
  return $errors;
}
?>