Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.11% covered (warning)
86.11%
31 / 36
75.00% covered (warning)
75.00%
6 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Clickatell
86.11% covered (warning)
86.11%
31 / 36
75.00% covered (warning)
75.00%
6 / 8
15.60
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 text
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 getCarriers
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getApiUsername
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getApiPassword
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getApiId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getApiUrl
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
2.01
 formatMessage
55.56% covered (warning)
55.56%
5 / 9
0.00% covered (danger)
0.00%
0 / 1
3.79
1<?php
2
3/**
4 * Class for text messaging via Clickatell's HTTP API
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2009.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  SMS
25 * @author   Ronan McHugh <vufind-tech@lists.sourceforge.net>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\SMS;
31
32use VuFind\Exception\SMS as SMSException;
33
34use function function_exists;
35
36/**
37 * Class for text messaging via Clickatell's HTTP API
38 *
39 * @category VuFind
40 * @package  SMS
41 * @author   Ronan McHugh <vufind-tech@lists.sourceforge.net>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/development Wiki
44 */
45class Clickatell extends AbstractBase
46{
47    /**
48     * HTTP client
49     *
50     * @var \Laminas\Http\Client
51     */
52    protected $client;
53
54    /**
55     * Constructor
56     *
57     * @param \Laminas\Config\Config $config  SMS configuration
58     * @param array                  $options Additional options (client may be an
59     * HTTP client object)
60     */
61    public function __construct(\Laminas\Config\Config $config, $options = [])
62    {
63        parent::__construct($config);
64        $this->client = $options['client'] ?? new \Laminas\Http\Client();
65    }
66
67    /**
68     * Send a text message to the specified provider.
69     *
70     * @param string $provider The provider ID to send to
71     * @param string $to       The phone number at the provider
72     * @param string $from     The email address to use as sender
73     * @param string $message  The message to send
74     *
75     * @throws \VuFind\Exception\Mail
76     * @return void
77     */
78    public function text($provider, $to, $from, $message)
79    {
80        $url = $this->getApiUrl($to, $message);
81        try {
82            $result = $this->client->setMethod('GET')->setUri($url)->send();
83        } catch (\Exception $e) {
84            throw new SMSException($e->getMessage(), SMSException::ERROR_UNKNOWN);
85        }
86        $response = $result->isSuccess() ? trim($result->getBody()) : '';
87        if (empty($response)) {
88            throw new SMSException('Problem sending text.', SMSException::ERROR_UNKNOWN);
89        }
90        if (!str_starts_with($response, 'ID:')) {
91            throw new SMSException($response, SMSException::ERROR_UNKNOWN);
92        }
93        return true;
94    }
95
96    /**
97     * Get a list of carriers supported by the module. Returned as an array of
98     * associative arrays indexed by carrier ID and containing "name" and "domain"
99     * keys.
100     *
101     * @return array
102     */
103    public function getCarriers()
104    {
105        return [
106            'Clickatell' => ['name' => 'Clickatell', 'domain' => null],
107        ];
108    }
109
110    /**
111     * Get API username.
112     *
113     * @return string
114     */
115    protected function getApiUsername()
116    {
117        return $this->smsConfig->Clickatell->user ?? null;
118    }
119
120    /**
121     * Get API password.
122     *
123     * @return string
124     */
125    protected function getApiPassword()
126    {
127        return $this->smsConfig->Clickatell->password ?? null;
128    }
129
130    /**
131     * Get API ID.
132     *
133     * @return string
134     */
135    protected function getApiId()
136    {
137        return $this->smsConfig->Clickatell->api_id ?? null;
138    }
139
140    /**
141     * Get API URL.
142     *
143     * @param string $to      The phone number at the provider
144     * @param string $message The message to send
145     *
146     * @return string
147     */
148    protected function getApiUrl($to, $message)
149    {
150        // Get base URL:
151        $url = isset($this->smsConfig->Clickatell->url)
152            ? trim($this->smsConfig->Clickatell->url, '?')
153            : 'https://api.clickatell.com/http/sendmsg';
154
155        // Add parameters to URL:
156        $url .= '?api_id=' . urlencode($this->getApiId());
157        $url .= '&user=' . urlencode($this->getApiUsername());
158        $url .= '&password=' . urlencode($this->getApiPassword());
159        $url .= '&to=' . urlencode($this->filterPhoneNumber($to));
160        $url .= '&text=' . urlencode($this->formatMessage($message));
161
162        return $url;
163    }
164
165    /**
166     * Format message for texting.
167     *
168     * @param string $message Message to format
169     *
170     * @return string
171     */
172    protected function formatMessage($message)
173    {
174        // Clickatell expects UCS-2 encoding:
175        if (!function_exists('iconv')) {
176            throw new SMSException(
177                'Clickatell requires iconv PHP extension.',
178                SMSException::ERROR_UNKNOWN
179            );
180        }
181        // Normalize UTF-8 if intl extension is installed:
182        if (class_exists('Normalizer')) {
183            $message = \Normalizer::normalize($message);
184        }
185        $message = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $message);
186
187        // We need to trim to 160 bytes (note that we need to use substr and not
188        // mb_substr, because the limit is BYTES not CHARACTERS; this may result
189        // in broken multi-byte characters but it seems unavoidable):
190        return substr($message, 0, 160);
191    }
192}