<?php
/*
Infobar Plugin for MyBB
Copyright (C) 2010 Sebastian Wunderlich
Traduit par: Rémi
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if(!defined('IN_MYBB'))
{
    die();
}
$plugins->add_hook('pre_output_page','infobar');
function infobar_info()
{
    return array
    (
        'name'=>'Infobar',
        'description'=>'Affiche une Infobar en haut du forum aux invités et membres qui n\'ont pas encore activé leur compte.',
        'website'=>'http://mods.mybboard.net/view/infobar',
        'author'=>'Sebastian Wunderlich',
        'version'=>'1.4',
        'guid'=>'6615441c6f918c53c3d378e1044ec448',
        'compatibility'=>'14*,16*',
        'codename'=>'infobar'
    );
}
function infobar_activate()
{
    global $db;
    $info=infobar_info();
    $setting_group_array=array
    (
        'name'=>$info['codename'],
        'title'=>$info['name'],
        'description'=>'Ici vous pouvez éditer les paramètres d\''.$info['name'].'.',
        'disporder'=>1,
        'isdefault'=>0
    );
    $db->insert_query('settinggroups',$setting_group_array);
    $group=$db->insert_id();
    $settings=array
    (
        'infobar_guests'=>array
        (
            'Infobar pour invités',
            'Voulez-vous afficher une Inforbar à vos invités ?',
            'yesno',
            1
        ),
        'infobar_activate'=>array
        (
            'Infobar pour utilisateurs non activés',
            'Voulez-vous afficher une Infobar à vos membres non activés ?',
            'yesno',
            1
        )
    );
    $i=1;
    foreach($settings as $name=>$sinfo)
    {
        $insert_array=array
        (
            'name'=>$name,
            'title'=>$db->escape_string($sinfo[0]),
            'description'=>$db->escape_string($sinfo[1]),
            'optionscode'=>$db->escape_string($sinfo[2]),
            'value'=>$db->escape_string($sinfo[3]),
            'gid'=>$group,
            'disporder'=>$i,
            'isdefault'=>0
        );
        $db->insert_query('settings',$insert_array);
        $i++;
    }
    rebuild_settings();
}
function infobar_deactivate()
{
    global $db;
    $info=infobar_info();
    $result=$db->simple_select('settinggroups','gid','name="'.$info['codename'].'"',array('limit'=>1));
    $group=$db->fetch_array($result);
    if(!empty($group['gid']))
    {
        $db->delete_query('settinggroups','gid="'.$group['gid'].'"');
        $db->delete_query('settings','gid="'.$group['gid'].'"');
        rebuild_settings();
    }
}
function infobar_lang()
{
    global $lang;
    $lang->load('infobar',false,true);
    $l['infobar_guests_message']='Vous n\'êtes pas connecté ou inscrit. Merci de <strong><a href="{1}/member.php?action=login">vous connecter</a></strong> ou de <strong><a href="{1}/member.php?action=register">vous enregistrer</a></strong> pour profiter de toutes les fonctionnalités de ce forum...';
    $l['infobar_activate_message']='Votre compte est toujours en attente d\'activation. Veuillez activer votre compte pour utiliser toutes les fonctionnalités de ce forum (<strong><a href="{1}/member.php?action=resendactivation">renvoyer le code d\'activation</a></strong> ou <strong><a href="{1}/usercp.php?action=email">changer votre adresse de courrier électronique</a></strong> d\'abord)...';
    foreach($l as $key=>$val)
    {
        if(!$lang->$key)
        {
            $lang->$key=$val;
        }
    }
}
function infobar($page)
{
    global $mybb,$session;
    if(($mybb->user['usergroup']==1||$mybb->user['usergroup']==5)&&empty($session->is_spider))
    {
        global $lang;
        infobar_lang();
        if($mybb->user['usergroup']==1&&$mybb->settings['infobar_guests'])
        {
            $infobar_message=$lang->sprintf($lang->infobar_guests_message,$mybb->settings['bburl']);
        }
        elseif($mybb->user['usergroup']==5&&$mybb->settings['infobar_activate'])
        {
            $infobar_message=$lang->sprintf($lang->infobar_activate_message,$mybb->settings['bburl']);
        }
        if($infobar_message)
        {
            $page=str_replace('</head>','<link rel="stylesheet" type="text/css" href="'.$mybb->settings['bburl'].'/themes/infobar.css" /></head>',$page);
            $page=preg_replace('#<body(.*)>#Usi','<body$1><div id="infobar"><span>'.$infobar_message.'</span></div>',$page);
            return $page;
        }
    }
}
?>