File: stop_manager.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 (173 lines) | stat: -rw-r--r-- 4,724 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* Plugin Name: Stop Manager
* Plugin URI: https://github.com/kalkun-sms/Kalkun/wiki/Plugin%3A-Stop-manager
* Version: 1.0
* Description: Manage incoming SMS containing STOP
* Author: tenzap
* Author URI: https://github.com/tenzap
*/

require_once (APPPATH . 'plugins/Plugin_helper.php');
Plugin_helper::autoloader();

use Kalkun\Plugins\StopManager\Config;
use Kalkun\Plugins\StopManager\MsgIncoming;
use Kalkun\Plugins\StopManager\MsgOutgoing;

class Stop_manager_plugin extends CI3_plugin_system {

	use plugin_trait;

	public function __construct()
	{
		parent::__construct();
		// Add hook for outgoing message
		//add_action('message.outgoing_dest_data', 'stop_manager_cleanup_outgoing', 1);
		add_filter('message.outgoing_dest_data', array($this, 'cleanup_outgoing'), 1);
		//add_action('message.incoming.before', 'stop_manager_incoming', 1);
		add_action('message.incoming.before', array($this, 'incoming'), 1);
	}

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

	/**
	 * Install Plugin
	 *
	 * Anything that needs to happen when this plugin gets installed
	 *
	 * @access public
	 * @since   0.1.0
	 * @return bool    TRUE by default
	 */
	public static function install($data = NULL)
	{
		$CI = &get_instance();
		$CI->load->helper('kalkun');
		// check if table already exists
		if ( ! $CI->db->table_exists('plugin_stop_manager'))
		{
			$db_driver = $CI->db->platform();
			$db_prop = get_database_property($db_driver);
			execute_sql(APPPATH . 'plugins/stop_manager/media/' . $db_prop['file'] . '_stop_manager.sql');
		}
		return TRUE;
	}

	/**
	 * Cleanup the outgoing message
	 *  - the list of recipient (remove those who have opted out)
	 *  - the content (remove the type)
	 *
	 * @param array $all (an array containing $dest & $data)
	 * @return array (an array containing $dest & $data)
	 */
	public function cleanup_outgoing($all)
	{
		$stopCfg = Config::getInstance();
		$stopMsgOutgoing = new MsgOutgoing($all);

		$dest = $all[0];
		$data = $all[1];

		$CI = &get_instance();
		// Get the list of numbers having "STOP" for this type of SMS
		$CI->load->model('stop_manager/Stop_manager_model', 'Stop_manager_model');
		$type = $stopCfg->isTypeEnabled() ? $stopMsgOutgoing->getType() : NULL;
		$db_result = $CI->Stop_manager_model->get_num_for_type($type)->result_array();
		$blocked_numbers = array();

		foreach ($db_result as $row)
		{
			$blocked_numbers[] = $row['destination_number'];
		}

		// Remove the phone no. if the recipient is in the STOP table for this type of sms
		foreach ($dest as $key => $number)
		{
			foreach ($blocked_numbers as $n)
			{
				if ($n === $number)
				{
					unset($dest[$key]);
				}
			}
		}

		$data['message'] = $stopMsgOutgoing->getCleanedMsg();

		return array($dest, $data);
	}

	/**
	 * Analyse an incoming message and store/remove in the Stop_manager database
	 *
	 * @param
	 * @return void
	 */
	public function incoming($sms)
	{
		// On message reception, if it is a STOP message (eg STOP rappel)
		// Put it to the STOP table

		$stopCfg = Config::getInstance();
		$stopMsgIncoming = new MsgIncoming($sms);

		if ($stopMsgIncoming->isValidStopMessage())
		{
			$CI = &get_instance();
			$CI->load->model('stop_manager/Stop_manager_model', 'Stop_manager_model');

			// Add to DB in case of OptOut
			if ($stopMsgIncoming->isOptOut())
			{
				$ret = $CI->Stop_manager_model->add($stopMsgIncoming->getParty(), $stopMsgIncoming->getType(), $stopMsgIncoming->getOrigMsg());
			}

			// Delete From DB in case of OptIn
			if ($stopMsgIncoming->isOptIn())
			{
				$ret = $CI->Stop_manager_model->delete($stopMsgIncoming->getParty(), $stopMsgIncoming->getType());
			}

			// Send auto reply
			if ($stopCfg->isAutoreplyInfoEnabled())
			{
				$this->autoreply($stopMsgIncoming->getParty(), $stopMsgIncoming->getAutoReplyMsg());
			}
		}
		else
		{
			if ($stopCfg->isAutoreplyErrorEnabled())
			{
				$this->autoreply($stopMsgIncoming->getParty(), $stopMsgIncoming->getAutoReplyMsg());
			}
		}
	}

	private function autoreply($tel, $reply_msg)
	{
		$config = Config::getInstance()->getConfig();

		$ret = NULL;
		// Filter rule for outgoing SMS
		if ($config['enable_autoreply_outnumber_filter'])
		{
			$ret = preg_match($config['autoreply_outnumber_match_rule'], $tel, $matches);
			//var_dump($ret);
			//var_dump($matches);
		}
		if ( ! $config['enable_autoreply_outnumber_filter'] || $ret === 1)
		{
			$CI = &get_instance();
			$CI->load->model('Message_model');
			$data['class'] = '1';
			$data['dest'] = $tel;
			$data['date'] = date('Y-m-d H:i:s');
			$data['message'] = $reply_msg;
			$data['delivery_report'] = 'default';
			$data['uid'] = '1';
			$CI->Message_model->send_messages($data);
		}
	}
}