Skip to content
Extraits de code Groupes Projets
Valider b8b4e063 rédigé par Matthieu Aubry's avatar Matthieu Aubry
Parcourir les fichiers

Merge pull request #9226 from piwik/9208

New API to create custom SMS providers in Piwik plugins
parents bf5bd787 f1b97e2d
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -7,7 +7,9 @@ This is a changelog for Piwik platform developers. All changes for our HTTP API' ...@@ -7,7 +7,9 @@ This is a changelog for Piwik platform developers. All changes for our HTTP API'
### Internal change ### Internal change
* When generating a new plugin skeleton via `generate:plugin` command, plugin name must now contain only letters and numbers. * When generating a new plugin skeleton via `generate:plugin` command, plugin name must now contain only letters and numbers.
* JavaScript Tracker tests no longer require `SQLite`. The existing MySQL configuration for tests is used now. In order to run the tests make sure Piwik is installed and `[database_tests]` is configured in `config/config.ini.php`. * JavaScript Tracker tests no longer require `SQLite`. The existing MySQL configuration for tests is used now. In order to run the tests make sure Piwik is installed and `[database_tests]` is configured in `config/config.ini.php`.
### New APIs
* Add your own SMS/Text provider by creating a new class in the `SMSProvider` directory of your plugin. The class has to extend `Piwik\Plugins\MobileMessaging\SMSProvider` and implement the required methods.
## Piwik 2.15.0 ## Piwik 2.15.0
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
*/ */
namespace Piwik\Plugins\MobileMessaging; namespace Piwik\Plugins\MobileMessaging;
use Piwik\Common;
use Piwik\Option; use Piwik\Option;
use Piwik\Piwik; use Piwik\Piwik;
use Piwik\Plugins\MobileMessaging\SMSProvider; use Piwik\Plugins\MobileMessaging\SMSProvider;
...@@ -26,15 +25,6 @@ class API extends \Piwik\Plugin\API ...@@ -26,15 +25,6 @@ class API extends \Piwik\Plugin\API
const VERIFICATION_CODE_LENGTH = 5; const VERIFICATION_CODE_LENGTH = 5;
const SMS_FROM = 'Piwik'; const SMS_FROM = 'Piwik';
/**
* @param string $provider
* @return SMSProvider
*/
private static function getSMSProviderInstance($provider)
{
return SMSProvider::factory($provider);
}
/** /**
* determine if SMS API credential are available for the current user * determine if SMS API credential are available for the current user
* *
...@@ -83,7 +73,7 @@ class API extends \Piwik\Plugin\API ...@@ -83,7 +73,7 @@ class API extends \Piwik\Plugin\API
{ {
$this->checkCredentialManagementRights(); $this->checkCredentialManagementRights();
$smsProviderInstance = self::getSMSProviderInstance($provider); $smsProviderInstance = SMSProvider::factory($provider);
$smsProviderInstance->verifyCredential($apiKey); $smsProviderInstance->verifyCredential($apiKey);
$settings = $this->getCredentialManagerSettings(); $settings = $this->getCredentialManagerSettings();
...@@ -160,7 +150,7 @@ class API extends \Piwik\Plugin\API ...@@ -160,7 +150,7 @@ class API extends \Piwik\Plugin\API
Piwik::checkUserIsNotAnonymous(); Piwik::checkUserIsNotAnonymous();
$credential = $this->getSMSAPICredential(); $credential = $this->getSMSAPICredential();
$SMSProvider = self::getSMSProviderInstance($credential[MobileMessaging::PROVIDER_OPTION]); $SMSProvider = SMSProvider::factory($credential[MobileMessaging::PROVIDER_OPTION]);
$SMSProvider->sendSMS( $SMSProvider->sendSMS(
$credential[MobileMessaging::API_KEY_OPTION], $credential[MobileMessaging::API_KEY_OPTION],
$content, $content,
...@@ -183,7 +173,7 @@ class API extends \Piwik\Plugin\API ...@@ -183,7 +173,7 @@ class API extends \Piwik\Plugin\API
$this->checkCredentialManagementRights(); $this->checkCredentialManagementRights();
$credential = $this->getSMSAPICredential(); $credential = $this->getSMSAPICredential();
$SMSProvider = self::getSMSProviderInstance($credential[MobileMessaging::PROVIDER_OPTION]); $SMSProvider = SMSProvider::factory($credential[MobileMessaging::PROVIDER_OPTION]);
return $SMSProvider->getCreditLeft( return $SMSProvider->getCreditLeft(
$credential[MobileMessaging::API_KEY_OPTION] $credential[MobileMessaging::API_KEY_OPTION]
); );
......
...@@ -94,7 +94,12 @@ class Controller extends ControllerAdmin ...@@ -94,7 +94,12 @@ class Controller extends ControllerAdmin
$view->creditLeft = $mobileMessagingAPI->getCreditLeft(); $view->creditLeft = $mobileMessagingAPI->getCreditLeft();
} }
$view->smsProviders = SMSProvider::getAvailableSMSProviders(); $providers = array();
foreach (SMSProvider::findAvailableSmsProviders() as $provider) {
$providers[$provider->getId()] = $provider->getDescription();
}
$view->smsProviders = $providers;
// construct the list of countries from the lang files // construct the list of countries from the lang files
$countries = array(); $countries = array();
......
...@@ -8,63 +8,120 @@ ...@@ -8,63 +8,120 @@
*/ */
namespace Piwik\Plugins\MobileMessaging; namespace Piwik\Plugins\MobileMessaging;
use Exception; use Piwik\Container\StaticContainer;
use Piwik\Development; use Piwik\Plugin;
use Piwik\Piwik; use Piwik\Piwik;
use Piwik\BaseFactory;
/** /**
* The SMSProvider abstract class is used as a base class for SMS provider implementations. * The SMSProvider abstract class is used as a base class for SMS provider implementations. To create your own custom
* SMSProvider extend this class and implement the methods to send text messages. The class needs to be placed in a
* `SMSProvider` directory of your plugin.
* *
* @api
*/ */
abstract class SMSProvider extends BaseFactory abstract class SMSProvider
{ {
const MAX_GSM_CHARS_IN_ONE_UNIQUE_SMS = 160; const MAX_GSM_CHARS_IN_ONE_UNIQUE_SMS = 160;
const MAX_GSM_CHARS_IN_ONE_CONCATENATED_SMS = 153; const MAX_GSM_CHARS_IN_ONE_CONCATENATED_SMS = 153;
const MAX_UCS2_CHARS_IN_ONE_UNIQUE_SMS = 70; const MAX_UCS2_CHARS_IN_ONE_UNIQUE_SMS = 70;
const MAX_UCS2_CHARS_IN_ONE_CONCATENATED_SMS = 67; const MAX_UCS2_CHARS_IN_ONE_CONCATENATED_SMS = 67;
protected static $availableSMSProviders = array( /**
'Clockwork' => '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/> * Get the ID of the SMS Provider. Eg 'Clockwork' or 'FreeMobile'
<ul> * @return string
<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> abstract public function getId();
</ul>
<br/><em>About Clockwork: </em><ul> /**
<li>Clockwork gives you fast, reliable high quality worldwide SMS delivery, over 450 networks in every corner of the globe. * Get a description about the SMS Provider. For example who the SMS Provider is, instructions how the API Key
</li><li>Cost per SMS message is around ~0.08USD (0.06EUR). * needs to be set, and more. You may return HTML here for better formatting.
</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> * @return string
</ul> */
', abstract public function getDescription();
);
/**
protected static function getClassNameFromClassId($id) * Verify the SMS API credential.
*
* @param string $apiKey API Key
* @return bool true if SMS API Key is valid, false otherwise
*/
abstract public function verifyCredential($apiKey);
/**
* Get the amount of remaining credits.
*
* @param string $apiKey API Key
* @return string remaining credits
*/
abstract public function getCreditLeft($apiKey);
/**
* Actually send the given text message. This method should only send the text message, it should not trigger
* any notifications etc.
*
* @param string $apiKey
* @param string $smsText
* @param string $phoneNumber
* @param string $from
* @return bool true
*/
abstract public function sendSMS($apiKey, $smsText, $phoneNumber, $from);
/**
* Defines whether the SMS Provider is available. If a certain provider should be used only be a limited
* range of users you can restrict the provider here. For example there is a Development SMS Provider that is only
* available when the development is actually enabled. You could also create a SMS Provider that is only available
* to Super Users etc. Usually this method does not have to be implemented by a SMS Provider.
*
* @return bool
*/
public function isAvailable()
{ {
return __NAMESPACE__ . '\\SMSProvider\\' . $id; return true;
} }
protected static function getInvalidClassIdExceptionMessage($id) /**
* @param string $provider The name of the string
* @return SMSProvider
* @throws \Exception
* @ignore
*/
public static function factory($provider)
{ {
return Piwik::translate('MobileMessaging_Exception_UnknownProvider', $providers = self::findAvailableSmsProviders();
array($id, implode(', ', array_keys(self::getAvailableSMSProviders())))
); if (!array_key_exists($provider, $providers)) {
throw new \Exception(Piwik::translate('MobileMessaging_Exception_UnknownProvider',
array($provider, implode(', ', array_keys($providers)))
));
}
return $providers[$provider];
} }
/** /**
* Returns all available SMS Providers * Returns all available SMS Providers
* *
* @return array * @return SMSProvider[]
* @ignore
*/ */
public static function getAvailableSMSProviders() public static function findAvailableSmsProviders()
{ {
$smsProviders = self::$availableSMSProviders; /** @var SMSProvider[] $smsProviders */
$smsProviders = Plugin\Manager::getInstance()->findMultipleComponents('SMSProvider', 'Piwik\Plugins\MobileMessaging\SMSProvider');
$providers = array();
if (Development::isEnabled()) { foreach ($smsProviders as $provider) {
$smsProviders['Development'] = 'Development SMS Provider<br />All sent SMS will be displayed as Notification'; /** @var SMSProvider $provider */
$provider = StaticContainer::get($provider);
if ($provider->isAvailable()) {
$providers[$provider->getId()] = $provider;
}
} }
return $smsProviders; return $providers;
} }
/** /**
...@@ -72,6 +129,7 @@ abstract class SMSProvider extends BaseFactory ...@@ -72,6 +129,7 @@ abstract class SMSProvider extends BaseFactory
* *
* @param string $string * @param string $string
* @return bool true if $string contains UCS2 characters * @return bool true if $string contains UCS2 characters
* @ignore
*/ */
public static function containsUCS2Characters($string) public static function containsUCS2Characters($string)
{ {
...@@ -94,6 +152,7 @@ abstract class SMSProvider extends BaseFactory ...@@ -94,6 +152,7 @@ abstract class SMSProvider extends BaseFactory
* @param int $maximumNumberOfConcatenatedSMS * @param int $maximumNumberOfConcatenatedSMS
* @param string $appendedString * @param string $appendedString
* @return string original $string or truncated $string appended with $appendedString * @return string original $string or truncated $string appended with $appendedString
* @ignore
*/ */
public static function truncate($string, $maximumNumberOfConcatenatedSMS, $appendedString = 'MobileMessaging_SMS_Content_Too_Long') public static function truncate($string, $maximumNumberOfConcatenatedSMS, $appendedString = 'MobileMessaging_SMS_Content_Too_Long')
{ {
...@@ -150,30 +209,4 @@ abstract class SMSProvider extends BaseFactory ...@@ -150,30 +209,4 @@ abstract class SMSProvider extends BaseFactory
$maxCharsInOneConcatenatedSMS * $maximumNumberOfConcatenatedSMS; $maxCharsInOneConcatenatedSMS * $maximumNumberOfConcatenatedSMS;
} }
/**
* verify the SMS API credential
*
* @param string $apiKey API Key
* @return bool true if SMS API credential are valid, false otherwise
*/
abstract public function verifyCredential($apiKey);
/**
* get remaining credits
*
* @param string $apiKey API Key
* @return string remaining credits
*/
abstract public function getCreditLeft($apiKey);
/**
* send SMS
*
* @param string $apiKey
* @param string $smsText
* @param string $phoneNumber
* @param string $from
* @return bool true
*/
abstract public function sendSMS($apiKey, $smsText, $phoneNumber, $from);
} }
...@@ -15,8 +15,9 @@ use Piwik\Plugins\MobileMessaging\APIException; ...@@ -15,8 +15,9 @@ use Piwik\Plugins\MobileMessaging\APIException;
use Piwik\Plugins\MobileMessaging\SMSProvider; use Piwik\Plugins\MobileMessaging\SMSProvider;
require_once PIWIK_INCLUDE_PATH . "/plugins/MobileMessaging/APIException.php"; require_once PIWIK_INCLUDE_PATH . "/plugins/MobileMessaging/APIException.php";
/** /**
* * @ignore
*/ */
class Clockwork extends SMSProvider class Clockwork extends SMSProvider
{ {
...@@ -31,6 +32,27 @@ class Clockwork extends SMSProvider ...@@ -31,6 +32,27 @@ class Clockwork extends SMSProvider
const MAXIMUM_FROM_LENGTH = 11; const MAXIMUM_FROM_LENGTH = 11;
const MAXIMUM_CONCATENATED_SMS = 3; 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/><em>About Clockwork: </em><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) public function verifyCredential($apiKey)
{ {
$this->getCreditLeft($apiKey); $this->getCreditLeft($apiKey);
......
...@@ -10,14 +10,31 @@ namespace Piwik\Plugins\MobileMessaging\SMSProvider; ...@@ -10,14 +10,31 @@ namespace Piwik\Plugins\MobileMessaging\SMSProvider;
use Piwik\Notification; use Piwik\Notification;
use Piwik\Plugins\MobileMessaging\SMSProvider; use Piwik\Plugins\MobileMessaging\SMSProvider;
use Piwik\Development as PiwikDevelopment;
/** /**
* Used for development only * Used for development only
* *
* @ignore
*/ */
class Development extends SMSProvider class Development extends SMSProvider
{ {
public function getId()
{
return 'Development';
}
public function getDescription()
{
return 'Development SMS Provider<br />All sent SMS will be displayed as Notification';
}
public function isAvailable()
{
return PiwikDevelopment::isEnabled();
}
public function verifyCredential($apiKey) public function verifyCredential($apiKey)
{ {
return true; return true;
......
...@@ -13,10 +13,26 @@ use Piwik\Plugins\MobileMessaging\SMSProvider; ...@@ -13,10 +13,26 @@ use Piwik\Plugins\MobileMessaging\SMSProvider;
/** /**
* Used for testing * Used for testing
* *
* @ignore
*/ */
class StubbedProvider extends SMSProvider class StubbedProvider extends SMSProvider
{ {
public function getId()
{
return 'StubbedProvider';
}
public function getDescription()
{
return 'Only during testing available';
}
public function isAvailable()
{
return defined('PIWIK_TEST_MODE') && PIWIK_TEST_MODE;
}
public function verifyCredential($apiKey) public function verifyCredential($apiKey)
{ {
return true; return true;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter