File: Kalkun_API.php

package info (click to toggle)
kalkun 0.8.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,340 kB
  • sloc: php: 30,659; javascript: 30,443; sql: 961; sh: 766; xml: 105; makefile: 40
file content (203 lines) | stat: -rw-r--r-- 5,065 bytes parent folder | download
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
<?php
/*
* @author Azhari Harahap (azhari.harahap@yahoo.com)
* @date Agustus 08, 2010
* @version 0.1 alpha
* @requirement cURL
*/

class Kalkun_API {

	protected $base_url = '';
	protected $login_url = 'login/index';
	protected $sms_url = 'messages/compose_process';
	protected $csrf_hash_url = 'kalkun/get_csrf_hash';
	protected $session_file = '/tmp/cookies.txt'; // must be writable
	protected $username = '';
	protected $password = '';
	protected $phone_number = '';
	protected $message = '';
	protected $sms_mode = '0'; // 1 = flash, 0 = normal
	//protected $send_date = date('Y-m-d H:i:s');
	protected $curl_id = '';

	function __construct($params = array())
	{
		if (count($params) > 0)
		{
			$this->curl_id = curl_init();
			$this->login_url = $params['base_url'].''.$this->login_url;
			$this->sms_url = $params['base_url'].''.$this->sms_url;
			$this->csrf_hash_url = $params['base_url'].''.$this->csrf_hash_url;
			$this->initialize($params);
		}
	}

	function initialize($params = array())
	{
		if (count($params) > 0)
		{
			foreach ($params as $key => $val)
			{
				if (isset($this->$key))
				{
					$this->$key = $val;
				}
			}
		}
	}

	function run()
	{
		if ($this->login())
		{
			$http_code = $this->send_sms();
			if ($http_code < 400)
			{
				$this->show_message("Message queued successfully.\n");
			}
			else
			{
				$this->show_message("Error queuing the message (HTTP_CODE: ${http_code}).\n");
			}
		}
		else
		{
			$this->show_message("Error during login.\n");
		}

		$this->finish();
	}

	function finish()
	{
		$ch = $this->curl_id;
		curl_close($ch);

		if (file_exists($this->session_file))
		{
			unlink($this->session_file);
		}
	}

	function login()
	{
		$csrf_hash = $this->get_csrf_hash_from_login_form();

		$ch = $this->curl_id;
		curl_setopt($ch, CURLOPT_URL, $this->login_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $this->session_file);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $this->session_file);

		$fields = array(
			'username' => urlencode($this->username),
			'password' => urlencode($this->password),
			'kalkun_csrf_tkn' => $csrf_hash,
		);

		$fields_string = $this->urlify($fields);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
		$output = curl_exec($ch);

		// Check HTTP Response code
		$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		if ($http_code >= 400)
		{
			$this->show_message('ERROR: HTTP_CODE: '.$http_code."\n");
			return FALSE;
		}

		if (strpos($output, 'Please enter your username and password') !== FALSE)
		{
			$this->show_message("Login failed.\n");
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}

	function send_sms()
	{
		$csrf_hash = $this->get_csrf_hash();
		$ch = $this->curl_id;
		curl_setopt($ch, CURLOPT_URL, $this->sms_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $this->session_file);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $this->session_file);

		$sms = array(
			'sendoption' => urlencode('sendoption3'),
			'manualvalue' => urlencode($this->phone_number),
			'senddateoption' => urlencode('option1'),
			'sms_mode' => urlencode($this->sms_mode),
			'sms_loop' => urlencode('1'),
			'validity' => urlencode('-1'),
			'message' => urlencode($this->message),
			'kalkun_csrf_tkn' => $csrf_hash,
		);
		$sms_field = $this->urlify($sms);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $sms_field);
		$output = curl_exec($ch);

		return curl_getinfo($ch, CURLINFO_HTTP_CODE);
	}

	function get_csrf_hash()
	{
		$ch = $this->curl_id;
		curl_setopt($ch, CURLOPT_URL, $this->csrf_hash_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
		curl_setopt($ch, CURLOPT_POST, FALSE);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $this->session_file);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $this->session_file);
		$output = curl_exec($ch);
		return json_decode($output);
	}

	function get_csrf_hash_from_login_form()
	{
		$ch = $this->curl_id;
		curl_setopt($ch, CURLOPT_URL, $this->login_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
		curl_setopt($ch, CURLOPT_POST, FALSE);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $this->session_file);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $this->session_file);

		$output = curl_exec($ch);

		// Extract the CSRF Hash from the <input> tag of the HTML
		$dom = new DOMDocument();
		$dom->loadHTML($output);
		$xp = new DOMXpath($dom);
		$nodes = $xp->query('//input[@name="kalkun_csrf_tkn"]');
		$node = $nodes->item(0);

		return $node->getAttribute('value');
	}

	//url-ify the data for the POST
	function urlify($param)
	{
		$param_string = '';
		foreach ($param as $key => $value)
		{
			$param_string .= $key.'='.$value.'&';
		}
		rtrim($param_string, '&');
		return $param_string;
	}

	function show_message($message)
	{
		echo $message;
	}
}