<?php
/*======================================================================*\
|| #################################################################### ||
|| #              الإهداءات  ehdaa v 3.4                              # ||
|| #              for vBulletin Version 3.5.x                         # ||
|| #              http://7beebi.com    موقع حبيبي                     # ||
|| #              webmaster@7beebi.com                                # ||
|| #################################################################### ||
\*======================================================================*/

// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);
@set_time_limit(0);
define('NO_REGISTER_GLOBALS', 1);
define('THIS_SCRIPT', 'ehdaa');

// ################### PRE-CACHE TEMPLATES AND DATA ######################
$phrasegroups = array();
$specialtemplates = array();
$globaltemplates = array();
$actiontemplates = array();

// ######################### REQUIRE BACK-END ############################
require_once('./global.php');
require_once(DIR . '/includes/adminfunctions.php'); // required for can_administer

// #######################################################################
// ######################## START MAIN SCRIPT ############################
// #######################################################################

$banduser = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "ehdaa_user_band where userid = " . $vbulletin->userinfo['userid'] . "");
$bandgroup = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "ehdaa_group_band where usergroupid = " . $vbulletin->userinfo['usergroupid'] . "");
$smiles = $db->query_read("SELECT * FROM " . TABLE_PREFIX . "ehdaa_smilies LIMIT " . $vbulletin->options['ehdaa_smile_x'] . "");
while ($smilepath = $db->fetch_array($smiles))
{
        $count++;

        if ($count == 1)
        $smile .= "<tr>";

        $smile .= "<td><img onclick=ehdaa_smilies('" . $smilepath['shortcut_s'] . "') src='images/ehdaa_smilies/" . $smilepath['path_smilie'] . "'></td>";

        if ($count == 3)
        {
              $smile .= "</tr>";
              $count = 0;
        }
}

if ($vbulletin->options['ehdaa_many'])
{
        $add_in_day = $db->query_first("SELECT COUNT(*) AS total FROM " . TABLE_PREFIX . "ehdaa WHERE userid = " . $vbulletin->userinfo['userid'] . " AND dateline >= " . (TIMENOW - 86400) . "");
}

if ($vbulletin->options['ehdaa_minute'])
{
        $add_in_minute = $db->query_first("SELECT COUNT(*) AS total FROM " . TABLE_PREFIX . "ehdaa WHERE userid = " . $vbulletin->userinfo['userid'] . " AND dateline >= " . (TIMENOW - (60 * $vbulletin->options['ehdaa_minute'])) . "");
}

if($bandgroup['usergroupid'])
{
        $msg = $bandgroup['msg'];
        eval('print_output("' . fetch_template('ehdaa_msg') . '");');
}
elseif($banduser['userid'])
{
        $msg = $banduser['msg'];
        eval('print_output("' . fetch_template('ehdaa_msg') . '");');
}
elseif($vbulletin->options['ehdaa_many'] AND $add_in_day['total'] >= $vbulletin->options['ehdaa_many']  AND !can_administer())
{
        $msg = construct_phrase($vbphrase['ehdaa_many_x'], $vbulletin->options['ehdaa_many']);
        eval('print_output("' . fetch_template('ehdaa_msg') . '");');
}
elseif($vbulletin->options['ehdaa_minute'] AND $add_in_minute['total']  AND !can_administer())
{
        $msg = construct_phrase($vbphrase['ehdaa_minute_x'], $vbulletin->options['ehdaa_minute']);
        eval('print_output("' . fetch_template('ehdaa_msg') . '");');
}


if ($_REQUEST['do'] == "add")
{
      if($vbulletin->userinfo['posts'] < $vbulletin->options['ehdaa_user_post'])
      {
              $msg = construct_phrase($vbphrase['ehdaa_post_x'], $vbulletin->options['ehdaa_user_post']);
              eval('print_output("' . fetch_template('ehdaa_msg') . '");');
      }
      else
      {
              $userehdaa = $vbulletin->userinfo['username'];
              eval('print_output("' . fetch_template('ehdaa_add') . '");');
      }
}

elseif ($_REQUEST['do'] == "save")
{
      $vbulletin->input->clean_array_gpc('p', array(
              'home'   => TYPE_STR,
              'msg'    => TYPE_STR
      ));

      if (!$vbulletin->GPC['home'] AND $vbulletin->options['ehdaa_home_enable'])
      {
              $msg = $vbphrase['ehdaa_no_home'];
      }
      elseif (!$vbulletin->GPC['msg'])
      {
              $msg = $vbphrase['ehdaa_no_msg'];
      }
      elseif (strlen($vbulletin->GPC['msg']) > $vbulletin->options['ehdaa_characters'])
      {
              $msg = construct_phrase($vbphrase['ehdaa_characters_x'], $vbulletin->options['ehdaa_characters']);
      }
      else
      {
            $words = $db->query_read("SELECT * FROM " . TABLE_PREFIX . "ehdaa_words");
            while ($word = $db->fetch_array($words))
            {
                    $vbulletin->GPC['msg'] = str_replace($word['findw'], $word['replacew'], $vbulletin->GPC['msg']);
            }

            $tableehdaa = iif($vbulletin->options['ehdaa_add_setting'] AND !can_moderate(), 'ehdaa_w', 'ehdaa');
            $db->query_write("INSERT INTO " . TABLE_PREFIX . "" . $tableehdaa . "(userid, name, home, msg, dateline) VALUES (" . $vbulletin->userinfo['userid'] . ", '" . $vbulletin->userinfo['username'] . "', '" . $vbulletin->GPC['home'] . "', '" . $vbulletin->GPC['msg'] . "', " . TIMENOW . ")");
            $msg = $vbphrase['done_add_' . $tableehdaa];
            $selfclose = iif(!$vbulletin->options['ehdaa_add_setting'] AND can_moderate(), 'countre()');
      }

      eval('print_output("' . fetch_template('ehdaa_msg') . '");');
}

if ($_REQUEST['do'] == "smilies")
{
     $ehdaa_smilies = $db->query("SELECT * FROM " . TABLE_PREFIX . "ehdaa_smilies ORDER BY id");
     while ($ehdaa_smilie = $db->fetch_array($ehdaa_smilies))
     {
         $smilies .= " ، <img onclick=ehdaa_smilies('" . $ehdaa_smilie['shortcut_s'] . "') src='images/ehdaa_smilies/" . $ehdaa_smilie['path_smilie']. "'>";
     }

     eval('print_output("' . fetch_template('ehdaa_smile') . '");');
}

?>