Completed
Push — master ( 8ade76...33fdc2 )
by Matthieu
23:07 queued 16:01
created

ASPSMS::issueApiCall()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 46
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 32
nc 3
nop 3
dl 0
loc 46
rs 8.6315
c 1
b 0
f 0
1
<?php
2
/**
3
 * Piwik - free/libre analytics platform
4
 *
5
 * @link http://piwik.org
6
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7
 *
8
 */
9
10
namespace Piwik\Plugins\MobileMessaging\SMSProvider;
11
12
use Exception;
13
use Piwik\Http;
14
use Piwik\Piwik;
15
use Piwik\Plugins\MobileMessaging\APIException;
16
use Piwik\Plugins\MobileMessaging\SMSProvider;
17
18
require_once PIWIK_INCLUDE_PATH . "/plugins/MobileMessaging/APIException.php";
19
20
/**
21
 * @ignore
22
 */
23
class ASPSMS extends SMSProvider
24
{
25
    const SOCKET_TIMEOUT = 15;
26
27
    const BASE_API_URL          = 'https://json.aspsms.com/';
28
    const CHECK_CREDIT_RESOURCE = 'CheckCredits';
29
    const SEND_SMS_RESOURCE     = 'SendTextSMS';
30
31
    const MAXIMUM_FROM_LENGTH      = 11;
32
    const MAXIMUM_CONCATENATED_SMS = 9;
33
34
    public function getId()
35
    {
36
        return 'ASPSMS';
37
    }
38
39
    public function getDescription()
40
    {
41
        return 'You can use <a target="_blank" href="?module=Proxy&action=redirect&url=http://www.aspsms.com/en/?REF=227830"><img src="plugins/MobileMessaging/images/ASPSMS.png"/></a> to send SMS Reports from Piwik.<br/>
42
			<ul>
43
			<li> First, <a target="_blank" href="?module=Proxy&action=redirect&url=http://www.aspsms.com/en/registration/?REF=227830">get an Account at ASP SMS</a> (Signup is free!)
44
			</li><li> Enter your ASPSMS credentials on this page. </li>
45
			</ul>
46
			<br/>About ASPSMS.com: <ul>
47
			<li>ASPSMS provides fast and reliable high quality worldwide SMS delivery, over 600 networks in every corner of the globe.
48
			</li><li>Cost per SMS message depends on the target country and starts from ~0.09USD (0.06EUR).
49
			</li><li>Most countries and networks are supported but we suggest you check the latest position on their supported networks list <a target="_blank" href="?module=Proxy&action=redirect&url=http://www.aspsms.com/en/networks/?REF=227830">here</a>.
50
			</li><li>For sending an SMS, you need so-called ASPSMS credits, which are purchased in advance. The ASPSMS credits do not expire. 
51
			</li><li><a href="?module=Proxy&action=redirect&url=https://www.aspsms.com/instruction/payment.asp?REF=227830">Payment</a> by bank transfer, various credit cards such as Eurocard/Mastercard, Visa, American Express or Diners Club, PayPal or Swiss Postcard.
52
			</li>
53
			</ul>
54
			';
55
    }
56
57
    public function getCredentialFields()
58
    {
59
        return array(
60
            array(
61
                'type'  => 'text',
62
                'name'  => 'username',
63
                'title' => 'General_Username'
64
            ),
65
            array(
66
                'type'  => 'text',
67
                'name'  => 'password',
68
                'title' => 'General_Password'
69
            ),
70
        );
71
    }
72
73
    public function verifyCredential($credentials)
74
    {
75
        $this->getCreditLeft($credentials);
76
77
        return true;
78
    }
79
80
    public function sendSMS($credentials, $smsText, $phoneNumber, $from)
81
    {
82
        $from = substr($from, 0, self::MAXIMUM_FROM_LENGTH);
83
84
        $smsText = self::truncate($smsText, self::MAXIMUM_CONCATENATED_SMS);
85
86
        $additionalParameters = array(
87
            'Recipients'  => array($phoneNumber),
88
            'MessageText' => $smsText,
89
            'Originator'  => $from,
90
            'AffiliateID' => '227830',
91
        );
92
93
        $this->issueApiCall(
94
            $credentials,
95
            self::SEND_SMS_RESOURCE,
96
            $additionalParameters
97
        );
98
    }
99
100
    private function issueApiCall($credentials, $resource, $additionalParameters = array())
101
    {
102
        $accountParameters = array(
103
            'UserName' => $credentials['username'],
104
            'Password' => $credentials['password'],
105
        );
106
107
        $parameters = array_merge($accountParameters, $additionalParameters);
108
109
        $url = self::BASE_API_URL
110
            . $resource;
111
112
        $timeout = self::SOCKET_TIMEOUT;
113
114
        try {
115
            $result = Http::sendHttpRequestBy(
116
                Http::getTransportMethod(),
117
                $url,
118
                $timeout,
119
                $userAgent = null,
120
                $destinationPath = null,
121
                $file = null,
122
                $followDepth = 0,
123
                $acceptLanguage = false,
124
                $acceptInvalidSslCertificate = true,
125
                $byteRange = false,
126
                $getExtendedInfo = false,
127
                $httpMethod = 'POST',
128
                $httpUserName = null,
129
                $httpPassword = null,
130
                $requestBody = json_encode($parameters)
131
            );
132
        } catch (Exception $e) {
133
            throw new APIException($e->getMessage());
134
        }
135
136
        $result = @json_decode($result, true);
137
138
        if (!$result || $result['StatusCode'] != 1) {
139
            throw new APIException(
140
                'ASPSMS API returned the following error message : ' . $result['StatusInfo']
141
            );
142
        }
143
144
        return $result;
145
    }
146
147
    public function getCreditLeft($credentials)
148
    {
149
        $credits = $this->issueApiCall(
150
            $credentials,
151
            self::CHECK_CREDIT_RESOURCE
152
        );
153
154
        return Piwik::translate('MobileMessaging_Available_Credits', array($credits['Credits']));
155
    }
156
}
157