Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Clockwork.php « SMSProvider « MobileMessaging « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: afda1fed763d8d930a889a9379e7353e8c37a798 (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
123
124
125
126
127
128
129
130
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace Piwik\Plugins\MobileMessaging\SMSProvider;

use Exception;
use Piwik\Http;
use Piwik\Plugins\MobileMessaging\APIException;
use Piwik\Plugins\MobileMessaging\SMSProvider;

require_once PIWIK_INCLUDE_PATH . "/plugins/MobileMessaging/APIException.php";

/**
 * @ignore
 */
class Clockwork extends SMSProvider
{
    const SOCKET_TIMEOUT = 15;

    const BASE_API_URL = 'https://api.mediaburst.co.uk/http';
    const CHECK_CREDIT_RESOURCE = '/credit.aspx';
    const SEND_SMS_RESOURCE = '/send.aspx';

    const ERROR_STRING = 'Error';

    const MAXIMUM_FROM_LENGTH = 11;
    const MAXIMUM_CONCATENATED_SMS = 3;

    public function getId()
    {
        return 'Clockwork';
    }

    public function getDescription()
    {
        return 'You can use <a target="_blank" href="?module=Proxy&action=redirect&url=http://www.clockworksms.com/platforms/piwik/"><img src="plugins/MobileMessaging/images/Clockwork.png"/></a> to send SMS Reports from Piwik.<br/>
			<ul>
			<li> First, <a target="_blank" href="?module=Proxy&action=redirect&url=http://www.clockworksms.com/platforms/piwik/">get an API Key from Clockwork</a> (Signup is free!)
			</li><li> Enter your Clockwork API Key on this page. </li>
			</ul>
			<br/>About Clockwork: <ul>
			<li>Clockwork gives you fast, reliable high quality worldwide SMS delivery, over 450 networks in every corner of the globe.
			</li><li>Cost per SMS message is around ~0.08USD (0.06EUR).
			</li><li>Most countries and networks are supported but we suggest you check the latest position on their coverage map <a target="_blank" href="?module=Proxy&action=redirect&url=http://www.clockworksms.com/sms-coverage/">here</a>.
			</li>
			</ul>
			';
    }

    public function verifyCredential($apiKey)
    {
        $this->getCreditLeft($apiKey);

        return true;
    }

    public function sendSMS($apiKey, $smsText, $phoneNumber, $from)
    {
        $from = substr($from, 0, self::MAXIMUM_FROM_LENGTH);

        $smsText = self::truncate($smsText, self::MAXIMUM_CONCATENATED_SMS);

        $additionalParameters = array(
            'To'      => str_replace('+', '', $phoneNumber),
            'Content' => $smsText,
            'From'    => $from,
            'Long'    => 1,
            'MsgType' => self::containsUCS2Characters($smsText) ? 'UCS2' : 'TEXT',
        );

        $this->issueApiCall(
            $apiKey,
            self::SEND_SMS_RESOURCE,
            $additionalParameters
        );
    }

    private function issueApiCall($apiKey, $resource, $additionalParameters = array())
    {
        $accountParameters = array(
            'Key' => $apiKey,
        );

        $parameters = array_merge($accountParameters, $additionalParameters);

        $url = self::BASE_API_URL
            . $resource
            . '?' . http_build_query($parameters, '', '&');

        $timeout = self::SOCKET_TIMEOUT;

        try {
            $result = Http::sendHttpRequestBy(
                Http::getTransportMethod(),
                $url,
                $timeout,
                $userAgent = null,
                $destinationPath = null,
                $file = null,
                $followDepth = 0,
                $acceptLanguage = false,
                $acceptInvalidSslCertificate = true
            );
        } catch (Exception $e) {
            $result = self::ERROR_STRING . " " . $e->getMessage();
        }

        if (strpos($result, self::ERROR_STRING) !== false) {
            throw new APIException(
                'Clockwork API returned the following error message : ' . $result
            );
        }

        return $result;
    }

    public function getCreditLeft($apiKey)
    {
        return $this->issueApiCall(
            $apiKey,
            self::CHECK_CREDIT_RESOURCE
        );
    }
}