SocialApi.php in Social API 8.2
File
src/Entity/SocialApi.php
View source
<?php
namespace Drupal\social_api\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Site\Settings;
class SocialApi extends ContentEntityBase implements ContentEntityInterface {
public static function create(array $values = []) {
if (isset($values['token'])) {
$values['token'] = static::encryptToken($values['token']);
}
return parent::create($values);
}
public static function preCreate(EntityStorageInterface $storage, array &$values) {
if (isset($values['token'])) {
$values['token'] = static::encryptToken($values['token']);
}
return parent::preCreate($storage, $values);
}
public function getUserId() {
return (int) $this
->get('user_id')->target_id;
}
public function setToken($token) {
$this
->set('token', $this
->encryptToken($token));
return $this;
}
public function getToken() {
$token = $this
->get('token')->value;
return $this
->decryptToken($token);
}
protected static function encryptToken($token) {
$key = static::getEncryptionKey();
$encryption_key = base64_decode($key);
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$encrypted = openssl_encrypt($token, 'aes-256-cbc', $encryption_key, 0, $iv);
return base64_encode($encrypted . '::' . $iv);
}
protected function decryptToken($token) {
$key = $this
->getEncryptionKey();
$encryption_key = base64_decode($key);
list($encrypted_data, $iv) = explode('::', base64_decode($token), 2);
return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);
}
protected static function getEncryptionKey() {
return Settings::getHashSalt();
}
}
Classes
Name |
Description |
SocialApi |
Defines a base class for Social API content entities. |