<?php
/*
Plugin Name: LCAS
Version: 0.0.0RC1
Description: Allow to disable login/register name to be sensible to the case/accents
Plugin URI: http://fr.piwigo.org/ext/extension_view.php?eid=513
Author: Eric, Whiler, LucMorizur
Author URI: http://www.infernoweb.net, http://blogs.wittwer.fr/whiler, http://myr.luc.free.fr
*/

/* 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_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);
load_language('messages.lang', LCAS_PATH);


/* 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_root_url().'admin.php?page=plugin-'.basename(LCAS_PATH)
    )
  );

  return $menu;
}

// Check users identification
// Define prefilter to add tooltips on register page
add_event_handler('init', 'LCAS_InitPage');
 
function LCAS_InitPage()
{
  global $template, $conf, $lang;

  $conf_LCAS= unserialize($conf['LoginCaseAccentsSensitivity']);
  
  // Set $conf['insensitive_case_logon'] to false, as LCAS takes completely
  // in charge everything concerning the case and the accents in the login
  if ($conf_LCAS[0] != '0') $conf['insensitive_case_logon'] = false;
 
  /* User identification */
  if (script_basename() == 'identification')
  {
    if (isset($_POST['username']) and isset($conf_LCAS[0]))
    {
      $new_username = LCAS_SearchCaseUsername($_POST['username'],$conf_LCAS[0]);
      $_POST['username'] = $new_username == '' ? $_POST['username'] : $new_username;
    }
  }
  
  // Add tooltips on register page
  if (script_basename() == 'register') {
    $template->assign(array(
      'LCAS_username_tooltip' => $lang['LCAS_tooltip_username_register'][intval($conf_LCAS[0])],
      'LCAS_password_tooltip' => $lang['LCAS_tooltip_password_register'],
    ));
    $template->set_prefilter('register', 'LCAS_add_tooltips_prefilter_register');
  }
}

function LCAS_add_tooltips_prefilter_register($content, &$smarty) {
  $search = 'for="login"';
  $replacement = 'for="login" title="{$LCAS_username_tooltip}"';
  $content = str_replace($search, $replacement, $content);
  $search = 'name="login"';
  $replacement = 'name="login" title="{$LCAS_username_tooltip}"';
  $content = str_replace($search, $replacement, $content);
  $search = 'for="password"';
  $replacement = 'for="password" title="{$LCAS_password_tooltip}"';
  $content = str_replace($search, $replacement, $content);
  $search = 'name="password"';
  $replacement = 'name="password" title="{$LCAS_password_tooltip}"';
  $content = str_replace($search, $replacement, $content);
  $search = 'for="password_conf"';
  $replacement = 'for="password_conf" title="{$LCAS_password_tooltip}"';
  $content = str_replace($search, $replacement, $content);
  $search = 'name="password_conf"';
  $replacement = 'name="password_conf" title="{$LCAS_password_tooltip}"';
  $content = str_replace($search, $replacement, $content);
  return $content;
}

// 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[0]))
   $NewPostLogin = LCAS_SearchCaseUsername($_POST['login'], $conf_LCAS[0]);
  
  if (isset($NewPostLogin) and get_userid($NewPostLogin))
   $errors[] = $lang['LCAS_error'][intval($conf_LCAS[0])];
  
  return $errors;
}

/**
 *
 * LCAS_add_tooltips()
 * add tooltips on username and password fields
 *
 * @param no parameter
 * @return no return value
 */

add_event_handler('blockmanager_apply', 'LCAS_add_tooltips_index');

function LCAS_add_tooltips_index() {
  global $template, $conf, $lang;
  
  $conf_LCAS= unserialize($conf['LoginCaseAccentsSensitivity']);
  
  $template->assign(array(
    'LCAS_username_tooltip' =>
     $lang['LCAS_tooltip_username_index'][intval($conf_LCAS[0])],
    'LCAS_password_tooltip' =>
     $lang['LCAS_tooltip_password_index'],
  ));
  $template->set_prefilter('menubar', 'LCAS_add_tooltips_prefilter_index');
}

function LCAS_add_tooltips_prefilter_index($content, &$smarty) {
  $search = 'for="username"';
  $replacement = 'for="username" title="{$LCAS_username_tooltip}"';
  $content = str_replace($search, $replacement, $content);
  $search = 'name="username"';
  $replacement = 'name="username" title="{$LCAS_username_tooltip}"';
  $content = str_replace($search, $replacement, $content);
  $search = 'for="password"';
  $replacement = 'for="password" title="{$LCAS_password_tooltip}"';
  $content = str_replace($search, $replacement, $content);
  $search = 'name="password"';
  $replacement = 'name="password" title="{$LCAS_password_tooltip}"';
  $content = str_replace($search, $replacement, $content);
  return $content;
}

// LCAS_admin_user_filter
//
// Allow to use LCAS for searching usernames, in admin user_list page
//
add_event_handler('loc_end_admin', 'LCAS_admin_user_filter');

function LCAS_admin_user_filter() {
  if (
    strpos($_SERVER['REQUEST_URI'], 'user_list') !== false and
    isset($_GET['username']) and !empty($_GET['username'])
  ) include(LCAS_PATH.'include/admin_search.inc.php');
}

// LCAS_add_tooltips_user_list
//
// Also set prefilter to add tooltip
//
add_event_handler('loc_begin_admin', 'LCAS_add_tooltips_user_list');

function LCAS_add_tooltips_user_list() {
global $template, $conf, $lang;
  
  $conf_LCAS= unserialize($conf['LoginCaseAccentsSensitivity']);
  
  if (strpos($_SERVER['REQUEST_URI'], 'user_list') !== false) {
    $template->assign(
      'LCAS_username_tooltip_admin',
      $lang['LCAS_tooltip_username_admin'][intval($conf_LCAS[0])]
    );
    $template->set_prefilter('user_list','LCAS_add_tooltips_prefilter_admin');
  }
}

function LCAS_add_tooltips_prefilter_admin($content, &$smarty) {
  $search = 'name="username"';
  $replacement = 'name="username" title="{$LCAS_username_tooltip_admin}"';
  $content = str_replace($search, $replacement, $content);
  return $content;
}

?>