summaryrefslogtreecommitdiff
path: root/sms/sms.class
blob: 6483035bb5c9d5ad3a2ff9fbf8fa0f3d81e4c341 (plain)
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
<?php
/*
**	$Id$
**
**	ITools - the Internet Tools Library
**
**	Copyright (C) 1995-2003 by the ITools Authors.
**	This program is free software; you can redistribute it and/or
**	modify it under the terms of either the GNU General Public License
**	or the GNU Lesser General Public License, as published by the Free
**	Software Foundation. See http://www.gnu.org/licenses/ for details.
**
**	sms.class - SMS gateway interface (both ways) using the Minick SMS gateway
*/

class it_sms
{
	/* Public readonly */
	var $service;	/* Service name ("soltime", "superweb", etc.) */
	var $keyword;	/* Short number service keyword ("SOLTIME", "SW", etc.) */

	var $direction;	/* "IN": Handy -> it_sms; "OUT": it_sms -> Handy */
	var $date;	/* Date of message sent / received */

	var $number;	/* FQPhone number of other party z. B. 0041763901391 */
	var $message;	/* Message text */

	var $result;	/* Result of transaction or empty */

	/* Private */
	var $logfile;	/* Log file name or empty for no log */


	/* Constructor */
	function it_sms($service, $keyword, $logfile="XX")
	{
		$this->service = $service;
		$this->keyword = $keyword;
		$this->logfile = $logfile;

		if ($logfile == "XX")
			$this->logfile = $_SERVER['DOCUMENT_ROOT']."/../log/sms.log";
	}


	/* Write a log file entry */
	function log()
	{
		if ($this->logfile == "")
			return;

		$log = date("Y-m-d H:i:s") . " DIR=$this->direction NUMBER=$this->number RESULT=$this->result TEXT=\"$this->message\"";
		if ($_COOKIE['UID'])
			$log .= " UID=\"" . $_COOKIE['UID'] . "\"";

		if ($file = fopen($this->logfile, "a"))
		{
			fwrite($file, $log . "\n");
			fclose($file);
		}
	}


	/* Send an SMS. Returns 1 on success, 0 otherwise. */
	function send($number, $message)
	{
		$this->direction = "OUT";
		$this->date = date("Y-m-d H:i:s");

		/* Remove all non-digits */
		$number = ereg_replace('[^+0-9]', '', $number);

		/* Change 076 to 004176 etc. */
		$this->number = ereg_replace('^0([1-9])', '0041\\1', $number);

		$this->message = $message;

		$url = "http://panther.minick.ch/cgi-bin/$this->service/send?number=$this->number&message=" . urlencode($this->keyword." ".$this->message);

		/* debug("Sending SMS: $url<br>\n"); */

		$result = file($url);
		$this->result = trim($result[5]); /* "OKAY" or "NOTOK" w/o newline */
		$this->log();

		if ($this->result == "OKAY")
			return 1;	/* Success */

		return 0;	/* Failure */
	}


	/*
	**  Receive an SMS from HTTP GET request
	**  Format: number=Absendertelefonnummer, message=SMS-Text, id=xxxxx
	**  Returns: 1=got a valid message, 0=got invalid garbage, ignore.
	*/
	function receive($secret="")
	{
		$this->direction = "IN";
		$this->date = date("Y-m-d H:i:s");

		$this->number = $_GET['number'];
		$this->message = $_GET['message'];

		if ($_GET['id'] == $secret)
		{
			$this->result = "OKAY";
			$this->log();
			return 1;
		}
		else
		{
			$this->result = "NOTOK: Bad authentication \"".$_GET['id']."\"";
			$this->log();
			$this->number = $this->message = "";
			return 0;
		}
	}

} /* End class it_sms */
?>