File: external_script.php

package info (click to toggle)
kalkun 0.8.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 7,340 kB
  • sloc: php: 30,659; javascript: 30,443; sql: 961; sh: 766; xml: 105; makefile: 40
file content (132 lines) | stat: -rw-r--r-- 2,652 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
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
* Plugin Name: External Script
* Plugin URI: http://azhari.harahap.us
* Version: 0.1
* Description: Execute external script to create your own logic application
* Author: Azhari Harahap
* Author URI: http://azhari.harahap.us
*/

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

class External_script_plugin extends CI3_plugin_system {

	use plugin_trait;

	public function __construct()
	{
		parent::__construct();
		// Add hook for incoming message
		add_filter('message.incoming.before', array($this, 'external_script'), 12);
	}

	function external_script($sms)
	{
		$scripts = Plugin_helper::get_plugin_config('external_script')['external_script'];
		$phone = $sms->SenderNumber;
		$content = $sms->TextDecoded;
		$id = $sms->ID;
		$time = $sms->ReceivingDateTime;
		$match = NULL; //The result of a preg_match capture
		// Load all rules
		foreach ($scripts as $rule)
		{
			$intepreter_path = $rule['intepreter_path'];
			$script_path = $rule['script_path'];
			$value = $parameter = '';

			// evaluate rule key
			switch ($rule['key'])
			{
				case 'sender':
					$value = $phone;
					break;

				case 'content':
					$value = $content;
					break;
			}

			// evaluate rule type
			switch ($rule['type'])
			{
				case 'match':
				case 'equal':
					$is_valid = is_equal($rule['value'], $value);
					break;

				case 'contain':
					$is_valid = is_contain($rule['value'], $value);
					break;

				case 'preg_match':
					$ret = is_preg_match($rule['value'], $value);
					$is_valid = $ret[0];
					$match = $ret[1][1];
					break;
			}

			// if we got valid rules
			if ($is_valid)
			{
				// build extra parameters
				if ( ! empty($rule['parameter']))
				{
					$valid_param = array('phone', 'content', 'id', 'time', 'match');
					$param = explode('|', $rule['parameter']);

					foreach ($param as $tmp)
					{
						if (in_array($tmp, $valid_param))
						{
							$parameter .= ' ' . escapeshellarg(${$tmp});
						}
					}
				}

				// execute it
				exec(escapeshellcmd($intepreter_path . ' ' . $script_path . ' ' . $parameter));
			}
		}
	}

	function is_equal($subject, $matched)
	{
		if ($subject === $matched)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

	function is_contain($subject, $matched)
	{
		if ( ! strstr($matched, $subject))
		{
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}

	function is_preg_match($pattern, $subject)
	{
		$ret = preg_match($pattern, $subject, $matches, PREG_UNMATCHED_AS_NULL);
		if ($ret === 1)
		{
			return array(TRUE, $matches);
		}
		else
		{
			return array(FALSE, NULL);
		}
	}
}