File: Api.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 (230 lines) | stat: -rw-r--r-- 6,398 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
 *	@Author: bullshit "oskar@biglan.at"
 *	@Copyright: bullshit, 2010
 *	@License: GNU General Public License
*/
//include_once(APPPATH.'plugins/Plugin_controller.php');

class Api extends MY_Controller {

	public static $VERSION = '0.0.8';

	private static $NAMESPACE = 'urn:KalkunRemoteAccess';
	private $ENDPOINT = '/plugin/soap/api';

	private $server;

	function __construct()
	{
		parent::__construct(FALSE);

		// Hide/Forbid access if soap plugin is not enabled
		$this->load->library('Plugins_lib_kalkun');
		if ( ! isset($this->plugins_lib_kalkun->get_enabled_plugins()['soap']))
		{
			$this->session->set_flashdata('notif', 'Plugin '.strtolower(get_class($this)).' is not installed');
			show_404();
		}

		$this->load->model('Api_Model', 'api_model');
		$this->load->library('session');
		$this->load->model(array('Kalkun_model', 'Message_model'));
		log_message('info', 'init remote access api');

		$this->ENDPOINT = site_url($this->ENDPOINT);

		// FIXME - WORKAROUND FOR NUSOAPLIB
		$_SERVER['PHP_SELF'] = $this->ENDPOINT;

		$this->_initialze_soap_server();
	}

	function index()
	{
		log_message('debug', 'index');
		function version()
		{
			$CI = &get_instance();
			return $CI->getApiVersion();
		}

		function login($token)
		{
			$CI = &get_instance();
			$account = $CI->api_model->getAccount($token);

			if ($account['status'] === FALSE)
			{
				return 0;
			}

			if ($account['ip'] === $_SERVER['REMOTE_ADDR'])
			{
				$CI->session->set_userdata('loggedin', 'TRUE');
				$CI->session->set_userdata('access_id', $account['id']);
				return $CI->session->session_id;
			}
			else
			{
				$CI->session->set_userdata('loggedin', NULL);
			}

			return 0;
		}

		function sendMessage($destinationNumber = '', $message = '')
		{
			$CI = &get_instance();
			if ($CI->session->userdata('loggedin') === NULL OR $CI->session->userdata('loggedin') !== 'TRUE')
			{
				return 128; // Unauthorized
			}
			$message = trim($message);

			$CI->load->helper('i18n');
			$CI->load->helper('kalkun');
			if (is_phone_number_valid($destinationNumber) === TRUE)
			{
				$CI->_sendMessage($destinationNumber, $message, 1);
				return 1;
			}

			return 0;
		}

		function sendFlashMessage($destinationNumber = '', $message = '')
		{
			$CI = &get_instance();
			$CI = &get_instance();
			if ($CI->session->userdata('loggedin') === NULL OR $CI->session->userdata('loggedin') !== 'TRUE')
			{
				return 128; // Unauthorized
			}
			$message = trim($message);

			$CI->load->helper('i18n');
			$CI->load->helper('kalkun');
			if (is_phone_number_valid($destinationNumber) === TRUE)
			{
				$CI->_sendMessage($destinationNumber, $message, 0);
				return 1;
			}

			return 0;
		}

		function logout()
		{
			$CI = &get_instance();
			$CI->session->sess_destroy();
			return 1;
		}

		$this->server->service($this->input->raw_input_stream);
	}

	function wsdl()
	{
		log_message('debug', 'wsdl');
		$_SERVER['QUERY_STRING'] = 'wsdl';
		$this->server->service($this->input->raw_input_stream);
	}

	private function remoteAccessEnabled()
	{
		return TRUE;
	}

	// phpcs:disable CodeIgniter.Commenting.InlineComment.LongCommentWithoutSpacing
	function _initialze_soap_server()
	{
		log_message('debug', 'init');
		$this->server = new soap_server();
		$this->server->configureWSDL('KalkunRemoteAccess', Api::$NAMESPACE, $this->ENDPOINT);

		$this->server->register(
			'version',
			array(),                            // input parameters
			array('result' => 'xsd:string'),    // output parameter
			'urn:Api',                          // namespace
			$this->ENDPOINT.'/getApiVersion',   // soapaction
			'rpc',                              // style
			'encoded',                          // use
			'API Version'                       // documentation
		);

		if ($this->remoteAccessEnabled())
		{
			$this->server->register(
				'login',
				array('token' => 'xsd:string'),     // input parameters
				array('result' => 'xsd:string'),    // output parameter
				'urn:Api',                          // namespace
				$this->ENDPOINT.'/login',           // soapaction
				'rpc',                              // style
				'encoded',                          // use
				'User login'                        // documentation
			);

			$this->server->register(
				'sendMessage',
				array('destinationNumber' => 'xsd:string',      // input parameters
					'message' => 'xsd:string'),
				array('result' => 'xsd:integer'),              // output parameter
				'urn:Api',                                      // namespace
				$this->ENDPOINT.'/sendMessage',                 // soapaction
				'rpc',                                          // style
				'encoded',                                      // use
				'Send SMS Message'                              // documentation
			);

			$this->server->register(
				'sendFlashMessage',
				array('destinationNumber' => 'xsd:string',      // input parameters
					'message' => 'xsd:string'),
				array('result' => 'xsd:integer'),              // output parameter
				'urn:Api',                                      // namespace
				$this->ENDPOINT.'/sendFlashMessage',            // soapaction
				'rpc',                                          // style
				'encoded',                                      // use
				'Send Flash SMS Message'                        // documentation
			);

			$this->server->register(
				'logout',
				array(),                                        // input parameters
				array('result' => 'xsd:integer'),               // output parameter
				'urn:Api',                                      // namespace
				$this->ENDPOINT.'/logout',                      // soapaction
				'rpc',                                          // style
				'encoded',                                      // use
				'User logout'                                   // documentation
			);
		}
	}
	// phpcs:enable

	public function _sendMessage($dest = '', $message = '', $class = 1)
	{
		//TODO - NOTIFICATIONS

		$this->_send($dest, $message, $class);
	}

	private function _send($dest = '', $message = '', $class = 1)
	{
		$data['dest'] = $dest;
		$data['date'] = date('Y-m-d H:i:s');
		$data['message'] = $message;
		$data['uid'] = 1;
		$data['class'] = $class;
		$data['delivery_report'] = 'default';
		return $this->Message_model->send_messages($data);
	}

	public function getApiVersion()
	{
		return Api::$VERSION;
	}
}