File: Spam_model.php

package info (click to toggle)
kalkun 0.8.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,340 kB
  • sloc: php: 30,659; javascript: 30,443; sql: 961; sh: 766; xml: 105; makefile: 41
file content (151 lines) | stat: -rw-r--r-- 3,234 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
 * Kalkun
 * An open source web based SMS Management
 *
 * @package		Kalkun
 * @author		Kalkun Dev Team
 * @license		https://spdx.org/licenses/GPL-2.0-or-later.html
 * @link		https://kalkun.sourceforge.io/
 */

// ------------------------------------------------------------------------
require_once(dirname(__FILE__) .'/../libraries/b8/b8.php');


/**
 * Spam_Check Class
 *
 * Handle all user database activity
 *
 * @package		Kalkun
 * @subpackage	Spam_model
 * @category	Models
 */
class Spam_model extends CI_Model {

	public $classifier;
	public $ratingcutoff = 0.7;
	public $b8;

	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();

		// get database engine
		$this->load->database();
		$this->load->helper('kalkun');
		$db_engine = $this->db->platform();
		$db_driver = get_database_property($db_engine)['name'];

		switch ($db_driver)
		{
			case 'mysqli':
				// value of storage should be then name of the backend file in libraries/b8/storage
				$config_b8 = ['storage' => 'mysql'];
				break;
			case 'postgre':
				$config_b8 = ['storage' => 'pgsql'];
				break;
			case 'sqlite':
				$config_b8 = ['storage' => 'sqlite3'];
				break;
			default:
		}

		$config_storage = [
			'resource' => $this->db->conn_id,
			'table' => 'b8_wordlist'];

		// We use the default lexer settings
		$config_lexer = [];

		// We use the default degenerator configuration
		$config_degenerator = [];

		try
		{
			$this->b8 = new b8\b8($config_b8, $config_storage, $config_lexer, $config_degenerator);
		}
		catch (Exception $e)
		{
			log_message('error', 'Could not initialize b8 library. '.$e->getMessage());
			show_error('Could not initialize b8 library. ' . $e->getMessage(), 500, '500 Internal Server Error');
		}
	}

	// --------------------------------------------------------------------

	/**
	 * Spam_model::_check_spam()
	 * Check if a a text is spam
	 * @param text $text
	 * @return
	 */
	function _check_spam($text)
	{
		$level = $this->b8->classify($text);
		$ret['class'] = ($level > $this->ratingcutoff) ? 'spam' : 'ham' ;
		$ret['level'] = $level;
		return (object)$ret;
	}

	/**
	 * Spam_model::apply_spam_filter()
	 *
	 * @param mixed $ID
	 * @param mixed $Text
	 * @return
	 */
	function apply_spam_filter($ID, $Text)
	{
		$is_spam = $this->_check_spam($Text);
		if ($is_spam->class === 'spam')
		{
			if ($is_spam->level > $this->ratingcutoff)
			{
				$this->report_spam(array('ID' => $ID, 'Text' => $Text));
			}
			//move to spam folder
			$this->db->where('ID', $ID)->update('inbox', array('id_folder' => '6'));

			return TRUE;
		}
		return FALSE;
	}

	/**
	 * Spam_model::report_spam()
	 *
	 * @param mixed $params
	 * @return
	 */
	function report_spam($params)
	{
		$this->b8->learn($params['Text'], b8\b8::SPAM);

		//move message to spam folder
		$this->db->where('ID', $params['ID']);
		$this->db->update('inbox', array('id_folder' => '6'));
	}

	/**
	 * Spam_model::report_ham()
	 *
	 * @param mixed $params
	 * @return
	 */
	function report_ham($params)
	{
		$this->b8->learn($params['Text'], b8\b8::HAM);

		//move message to spam folder
		$this->db->where('ID', $params['ID']);
		$this->db->update('inbox', array('id_folder' => '1'));
	}
}