<?php
/***************************************************************************
 *            quote_smarts()
 *                            ----------------------------
 *   Author      :   © php.net
 *   Version      :   1.0.0
 *   Created      :   inconnu
 *   Revised      :   inconnu
 *
 *   Description   :   Protège les caractères spéciaux d'une commande SQL.
 *
 *   Parameters   :      n/a
 *
 *   Options Params   :   n/a
 *   
 ************************************************************************ ***/
function quote_smarts($value)
{
   // Stripslashes
   if (get_magic_quotes_gpc())
   {
      $value = stripslashes($value);
   }
   
   // Protection si ce n'est pas un entier
   if (!is_numeric($value))
   {
      $value = "'" . mysql_real_escape_string($value) . "'";
   }
   
   return $value;
}

/***************************************************************************
 *                                send_mp()
 *                            ----------------------------
 *   Author      :   Oyo (oyo@u-web.org) http://mods.u-web.org
 *   Version      :   1.3.7
 *   Created      :   01:40 13/05/2006
 *   Revised      :   16:55 01/10/2006
 *
 *   Description   :   Cette fonction est utilisée pour envoyer un message privé à un utilistateur.
 *
 *   Parameters   :   
 *            $to      - ID du membre qui reçoi
 *            $subject   - Sujet du mp
 *            $text      - Commentaire du prof
 *
 *   Options Params   :   $from      - ID du membre qui envoi
 *
 ************************************************************************ ***/
if ( !function_exists( 'send_pm' ) )
{
   function send_pm($to, $subject, $text, $from = '')
   {
      //  Valeur global pour la fonction
      global $board_config, $lang, $db, $phpbb_root_path, $phpEx;
      global $userdata, $user_ip;
      
      include_once($phpbb_root_path . 'includes/bbcode.'.$phpEx);
      include_once($phpbb_root_path . 'includes/functions_post.'.$phpEx);
   
      // On récupere
      $sqlaaa = 'SELECT user_id, user_notify_pm, user_email, user_lang, user_active, user_allowhtml, user_allowsmile, user_allowbbcode, user_attachsig FROM ' . USERS_TABLE . ' WHERE user_id = '.$to.' AND user_id <> ' . ANONYMOUS.';';
      if ( !($result = $db->sql_query($sqlaaa)) )
      {
            message_die(GENERAL_ERROR, 'Could not select users table', '', __LINE__, __FILE__, $sqlaaa);
      }
      $to_userdata = $db->sql_fetchrow($result);
   
      // Si $from n'est pas défini dans ce cas c'est l'id du membre qui utilise le script.
      if ( !$from )
      {
         $from = $userdata['user_id'];
      }
      
      $uid          = make_bbcode_uid();
      $date       = date('U');
      $html_on       = ($board_config['allow_html'] != 0) ? $board_config['allow_html'] : $to_userdata['user_allowhtml'];
      $smilies_on    = ($board_config['allow_smilies'] != 0) ? $board_config['allow_smilies'] : $to_userdata['user_allowsmile'];
      $bbcode_on    = ($board_config['allow_bbcode'] != 0) ? $board_config['allow_bbcode'] : $to_userdata['user_allowbbcode'];
      $attach_sig    = $to_userdata['user_attachsig'];
      $text       = prepare_message($text, $html_on, $bbcode_on, $smilies_on, $uid);      

      $sqlf = 'SELECT COUNT(privmsgs_id) AS inbox_items, MIN(privmsgs_date) AS oldest_post_time
      FROM ' . PRIVMSGS_TABLE . '
      WHERE ( privmsgs_type = ' . PRIVMSGS_NEW_MAIL . '
         OR privmsgs_type = ' . PRIVMSGS_READ_MAIL . '
         OR privmsgs_type = ' . PRIVMSGS_UNREAD_MAIL . ' )
      AND privmsgs_to_userid = ' . $to_userdata['user_id'].';';
      if ( !($result = $db->sql_query($sqlf)) )
      {
         message_die(GENERAL_ERROR, 'Could not find max privmsgs (inbox)', '', __LINE__, __FILE__, $sqlf);
      }

      $sql_priority = ( SQL_LAYER == 'mysql' ) ? 'LOW_PRIORITY' : '';

      if ( $inbox_info = $db->sql_fetchrow($result) )
      {
         if ( $inbox_info['inbox_items'] >= $board_config['max_inbox_privmsgs'] )
         {
            // selectionne les vieux messages
            $sqlk = 'SELECT privmsgs_id FROM ' . PRIVMSGS_TABLE . '
            WHERE ( privmsgs_type = ' . PRIVMSGS_NEW_MAIL . '
               OR privmsgs_type = ' . PRIVMSGS_READ_MAIL . '
               OR privmsgs_type = ' . PRIVMSGS_UNREAD_MAIL . '  )
            AND privmsgs_date = ' . $inbox_info['oldest_post_time'] . '
                 AND privmsgs_to_userid = ' . $to_userdata['user_id'].';';
            if ( !$result = $db->sql_query($sqlk) )
            {
               message_die(GENERAL_ERROR, 'Could not find oldest privmsgs (inbox)', '', __LINE__, __FILE__, $sqlk);
            }
            $old_privmsgs_id = $db->sql_fetchrow($result);
            $old_privmsgs_id = $old_privmsgs_id['privmsgs_id'];
            
            // supprime les vieux messages
            $sqll = 'DELETE '.$sql_priority.' FROM ' . PRIVMSGS_TABLE . ' WHERE privmsgs_id = '.$old_privmsgs_id.';';
            if ( !$db->sql_query($sqll) )
            {
               message_die(GENERAL_ERROR, 'Could not delete oldest privmsgs (inbox)'.$sql, '', __LINE__, __FILE__, $sqll);
            }
            
            // supprime les vieux messages
            $sqlm = 'DELETE '.$sql_priority.' FROM ' . PRIVMSGS_TEXT_TABLE . ' WHERE privmsgs_text_id = '.$old_privmsgs_id.';';
            if ( !$db->sql_query($sqlm) )
            {
               message_die(GENERAL_ERROR, 'Could not delete oldest privmsgs text (inbox)', '', __LINE__, __FILE__, $sqlm);
            }
         }
      }

      // Indique à phpBB que l'utilisateur à un nouveau message
      $sqly = 'UPDATE ' . USERS_TABLE . " SET user_new_privmsg = user_new_privmsg + 1, user_last_privmsg = $date WHERE user_id = $to;";
      if( !($result = $db->sql_query($sqly)) )
      {
            message_die(GENERAL_ERROR, 'Could not update users table', '', __LINE__, __FILE__, $sqly);
      }
         
      // Créer la donnée dans la 1er table
      $sqlq = sprintf('INSERT INTO ' . PRIVMSGS_TABLE . ' (privmsgs_type, privmsgs_subject, privmsgs_from_userid, privmsgs_to_userid, privmsgs_date, privmsgs_ip, privmsgs_enable_html, privmsgs_enable_bbcode, privmsgs_enable_smilies, privmsgs_attach_sig) VALUES (%s, %s, %s, %s, %s, "%s", %s, %s, %s, %s);',
      PRIVMSGS_NEW_MAIL , quote_smarts(trim(strip_tags(sprintf($subject, $board_config['sitename'])))), $from, $to, $date, $user_ip, $html_on, $bbcode_on, $smilies_on, $attach_sig);
      if( !($result = $db->sql_query($sqlq)) )
      {
         message_die(GENERAL_ERROR, 'Could not insert private message sent info', '', __LINE__, __FILE__, $sql);
      }
      $privmsg_sent_id = $db->sql_nextid();

      // Ajoute le texte dans la table
      $sqls = sprintf('INSERT INTO ' . PRIVMSGS_TEXT_TABLE . '(privmsgs_text_id, privmsgs_bbcode_uid, privmsgs_text) VALUES (%s, %s, %s);',
      $privmsg_sent_id, quote_smarts($uid), quote_smarts(sprintf($text, $board_config['sitename'])));
      if( !($result = $db->sql_query($sqls)) )
      {
            message_die(GENERAL_ERROR, 'Could not insert private message sent text', '', __LINE__, __FILE__, $sql);
      }

      return;
   }
}

?>