/**
* Add a device to a user.
*
* @return void
* @param string $token The token of the device.
* @param string $email The emailaddress for the user to link the device to.
*/
public static function appleAdddevice($token, $email)
{
// authorized?
if (API::authorize()) {
// redefine
$token = str_replace(' ', '', (string) $token);
// validate
if ($token == '') {
API::output(API::BAD_REQUEST, array('message' => 'No token-parameter provided.'));
}
if ($email == '') {
API::output(API::BAD_REQUEST, array('message' => 'No email-parameter provided.'));
}
// we should tell the ForkAPI that we registered a device
$publicKey = BackendModel::getModuleSetting('core', 'fork_api_public_key', '');
$privateKey = FrontendModel::getModuleSetting('core', 'fork_api_private_key', '');
// validate keys
if ($publicKey == '' || $privateKey == '') {
API::output(API::BAD_REQUEST, array('message' => 'Invalid key for the Fork API, configer them in the backend.'));
}
try {
// load user
$user = new BackendUser(null, $email);
// get current tokens
$tokens = (array) $user->getSetting('apple_device_token');
// not already in array?
if (!in_array($token, $tokens)) {
$tokens[] = $token;
}
// require the class
require_once PATH_LIBRARY . '/external/fork_api.php';
// create instance
$forkAPI = new ForkAPI($publicKey, $privateKey);
// make the call
$forkAPI->appleRegisterDevice($token);
// store
if (!empty($tokens)) {
$user->setSetting('apple_device_token', $tokens);
}
} catch (Exception $e) {
API::output(API::FORBIDDEN, array('message' => 'Can\'t authenticate you.'));
}
}
}