1
+ <?php
2
+
3
+ // Simple gateway, using the default shared secret
4
+ // This simple demo gateway is available here : https://1-2-3-4-5-6.net/smsgateway/
5
+
6
+ //Import SMSGateway classes into the global namespace
7
+ //These must be at the top of your script, not inside a function
8
+ use multiOTP \SMSGateway \SMSGateway ;
9
+
10
+ // Some tricks to load the SMSGateway class in different situations
11
+ if (!class_exists ('multiOTP\SMSGateway\SMSGateway ' )) {
12
+ if (file_exists ('../src/SMSGateway.php ' )) {
13
+ // Quick load of SMSGateway without using composer
14
+ require_once '../src/SMSGateway.php ' ;
15
+ } else {
16
+ // Composer autoload
17
+ require '../vendor/autoload.php ' ;
18
+ }
19
+ }
20
+
21
+ // Create an SMSGateway instance if not done yet, and define the flat-file data folder
22
+ if (!isset ($ smsgateway )) {
23
+ $ smsgateway = new SMSGateway ();
24
+ $ smsgateway ->setDataPath (__DIR__ . DIRECTORY_SEPARATOR . 'data ' . DIRECTORY_SEPARATOR );
25
+ }
26
+
27
+ // Detect the URL
28
+ $ url = (isset ($ _SERVER ['HTTPS ' ]) && $ _SERVER ['HTTPS ' ] === 'on ' ? "https " : "http " ) . ":// " .$ _SERVER ['HTTP_HOST ' ].dirname ($ _SERVER ['PHP_SELF ' ]);
29
+
30
+ // Retrieve some parameters
31
+ $ command = isset ($ _GET ["m " ]) ? "m " : "" ;
32
+ $ h = isset ($ _GET ["h " ]) ? $ _GET ["h " ] : "" ;
33
+
34
+ // Correct the international format of the phone number if needed
35
+ $ to = isset ($ _GET ["to " ]) ? $ _GET ["to " ] : "" ;
36
+ if ("00 " == substr ($ to , 0 , 2 )) {
37
+ $ to = "+ " . substr ($ to , 2 , strlen ($ to ) -2 );
38
+ }
39
+
40
+ // Define a default message if needed
41
+ $ message = isset ($ _GET ["message " ]) ? $ _GET ["message " ] : "Hello World 😉 " ;
42
+
43
+ // Retrieve the device id
44
+ $ id = isset ($ _GET ["id " ]) ? $ _GET ["id " ] : "" ;
45
+ $ device_id = $ id ;
46
+ if ((!empty ($ to )) && empty ($ device_id )) {
47
+ $ device_id = substr (md5 (uniqid ("" , true )), 0 , 16 );
48
+ } elseif ((empty ($ to )) && (!empty ($ device_id )) && (!file_exists ($ smsgateway ->getDataPath () .$ device_id ))) {
49
+ $ device_id = "" ;
50
+ }
51
+
52
+ // Calculate the device hash based on the secret
53
+ $ device_h = $ smsgateway ->calculateAuthenticationHash ($ device_id );
54
+
55
+ // Check if device hash is valid for an existing device, otherwise flush the device id
56
+ if ((!empty ($ id )) && ($ h != $ device_h )) {
57
+ $ device_id = "" ;
58
+ }
59
+
60
+ if (("m " == $ command ) && (!empty ($ device_id ))) {
61
+
62
+ // Display messages resume for the "m" command
63
+ echo "<h1>SMSGateway " .$ smsgateway ->getVersion ()."</h1> " ;
64
+ echo "<h2>New SMS messages received</h2> " ;
65
+ $ new_messages_count = 0 ;
66
+ foreach ($ smsgateway ->readNewMessages ($ id ) as $ message ) {
67
+ echo date ("Y-m-d H:i:s " , $ message ['sms_received ' ] / 1000 )." " .$ message ['from ' ].": " .$ message ['content ' ]. "<br /> " ;
68
+ $ new_messages_count ++;
69
+ }
70
+ if (0 == $ new_messages_count ) {
71
+ echo "none<br /> " ;
72
+ }
73
+ echo "<h2>All SMS messages received</h2> " ;
74
+ $ messages_count = 0 ;
75
+ foreach ($ smsgateway ->readAllMessages ($ id ) as $ message ) {
76
+ echo date ("Y-m-d H:i:s " , $ message ['sms_received ' ] / 1000 )." " .$ message ['from ' ].": " .$ message ['content ' ]. "<br /> " ;
77
+ $ messages_count ++;
78
+ }
79
+ if (0 == $ messages_count ) {
80
+ echo "none<br /> " ;
81
+ }
82
+ echo "<h2>All SMS messages sent</h2> " ;
83
+ $ sent_messages_count = 0 ;
84
+ foreach ($ smsgateway ->readAllSentStatus ($ id ) as $ message ) {
85
+ if ("DELIVERED " == $ message ['status ' ]) {
86
+ $ pre_status = "<b> " ;
87
+ $ post_status = "</b> " ;
88
+ }
89
+ echo date ("Y-m-d H:i:s " , $ message ['last_update ' ] / 1000 )." " .$ message ['to ' ]." [ " . $ pre_status . $ message ['status ' ]. $ post_status . "]: " .$ message ['content ' ]. "<br /> " ;
90
+ $ sent_messages_count ++;
91
+ }
92
+ if (0 == $ sent_messages_count ) {
93
+ echo "none<br /> " ;
94
+ }
95
+
96
+ } elseif (empty ($ device_id )) {
97
+
98
+ // Display basic usage info
99
+ echo "<h1>SMSGateway " .$ smsgateway ->getVersion ()."</h1> " ;
100
+ echo "Please send a first message like this: <b> $ url/?to=001234567890&message=Hello+world</b> " ;
101
+
102
+ } elseif (!empty ($ to )) {
103
+
104
+ // Push the message on the server
105
+ $ message_id = $ smsgateway ->sendMessage ($ device_id , $ to , $ message );
106
+
107
+ // Display usage information
108
+ echo "<h1>SMSGateway " .$ smsgateway ->getVersion ()."</h1> " ;
109
+ echo "Message '<i> $ message</i>' for ' $ to' with id '<b> $ message_id</b>' pushed on the server. " ;
110
+ echo "<br /><br /> " ;
111
+ echo "If not done yet, please install the Android SMSGatewayApp available here : <a href= \"https://github.com/medic/cht-gateway/releases/latest \" target= \"blank \">medic-gateway-X.X.X-generic-release.apk</a> " ;
112
+ echo "<br /><br /> " ;
113
+ echo "URL to set in the Settings of the Android App: <b> $ url/?id= $ device_id&h= $ device_h</b> " ;
114
+ echo "<br /> " ;
115
+ echo "<i>(don't forget to enable polling)</i> " ;
116
+ echo "<br /><br /> " ;
117
+ echo "URL to check SMS messages: <b> $ url/?id= $ device_id&h= $ device_h&m</b> " ;
118
+ echo "<br /><br /> " ;
119
+ echo "URL to send more SMS messages: <b> $ url/?id= $ device_id&h= $ device_h&to=001234567890&message=Hello+world</b> " ;
120
+ } else {
121
+
122
+ // Run the API server
123
+ $ smsgateway ->apiServer ("secret " );
124
+ }
0 commit comments