<?php
/*
Plugin Name: SpamKiller
Plugin URI: http://www.osclass.org/
Description: Marking the ad as spam automatically based on the filter words.
Version: 1.0
Author: RajaSekar
Author URI: http://www.osclass.org/
Short Name: SpamKiller
*/
function spamkiller_install() {
  $conn = getConnection();
  $conn->autocommit(false);
  try {
    $path = osc_plugin_resource('spamkiller/struct.sql');
    $sql = file_get_contents($path);
    $conn->osc_dbImportSQL($sql);
    $conn->commit();
  }
  catch (Exception $e) {
    $conn->rollback();
    echo $e->getMessage();
  }
  $conn->autocommit(true);
}

function spamkiller_uninstall() {
    // Insert here the code you want to execute after the plugin's uninstall
    // for example you might want to drop/remove a table or modify some values

    // In this case we'll remove the table we created to store Example attributes
    $conn = getConnection();
    $conn->autocommit(false);
    try {
        $conn->osc_dbExec('DROP TABLE %st_spamkiller', DB_TABLE_PREFIX);
        $conn->commit();
    } catch (Exception $e) {
        $conn->rollback();
        echo $e->getMessage();
    }
    $conn->autocommit(true);
}


function spamkiller_admin_menu() {
  echo '<h3><a href="#">SpamKiller</a></h3>
    <ul>
        <li><a href="' . osc_admin_render_plugin_url("spamkiller/settings.php") . '?section=types">&raquo; ' . __('Настройки', 'SpamKiller') . '</a></li>
    </ul>';
}
function get_bad_words() {
  $conn = getConnection();
  $badwords = $conn->osc_dbFetchResults("SELECT * FROM %st_spamkiller WHERE id=1", DB_TABLE_PREFIX);
  foreach ($badwords as $badword) {
    echo $badword[bad_words];
  }
}
function after_item_posted($item) {
  $conn = getConnection();
  $badwords = $conn->osc_dbFetchResults("SELECT * FROM %st_spamkiller WHERE id=1", DB_TABLE_PREFIX);
  foreach ($badwords as $badword) {
    $arrbadword = $badword[bad_words];
  }
  $exparrbadword = explode(",", $arrbadword);
  foreach ($exparrbadword as $expbadword) {
    if (preg_match("/" . $expbadword . "/i", $item[s_title]) || preg_match("/" . $expbadword . "/i", $item[s_description])) {
      $conn->osc_dbExec("UPDATE %st_item SET b_spam = 1 WHERE pk_i_id = '%s'", DB_TABLE_PREFIX, $item[pk_i_id]);
      $conn->osc_dbExec("INSERT INTO %st_spamkiller_ad_id (id) VALUES (%s)", DB_TABLE_PREFIX, $item[pk_i_id]);
      osc_add_flash_error_message(__('Ваше объявление заблокировано спам-фильтром', 'SpamKiller'));
    }
  }
}

function after_item_validate($id) {
$conn = getConnection();
$result = $conn->osc_dbFetchResults("SELECT * FROM %st_spamkiller_ad_id WHERE id='%s'",DB_TABLE_PREFIX,$id);
$number_of_rows = count($result);
echo $number_of_rows;
if ($number_of_rows > 0)
{
$conn->osc_dbExec("UPDATE %st_item SET b_spam = 1 WHERE pk_i_id = '%s'", DB_TABLE_PREFIX,$id);
osc_add_flash_error_message(__('Ваше объявление заблокировано спам-фильтром', 'SpamKiller'));
}
}

osc_register_plugin(osc_plugin_path(__FILE__), 'spamkiller_install');
osc_add_hook(osc_plugin_path(__FILE__) . '_uninstall','spamkiller_uninstall');
osc_add_hook('posted_item', 'after_item_posted');
osc_add_hook('activate_item', 'after_item_validate');
osc_add_hook('admin_menu', 'spamkiller_admin_menu');
?>