Rev 6849 | AutorÃa | Comparar con el anterior | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Controller;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Log\LoggerInterface;
use Laminas\View\Model\JsonModel;
use LeadersLinked\Mapper\FeedMapper;
use LeadersLinked\Mapper\ShorterMapper;
use LeadersLinked\Model\Shorter;
use LeadersLinked\Mapper\PostMapper;
class ShorterController extends AbstractActionController
{
/**
*
* @var \Laminas\Db\Adapter\AdapterInterface
*/
private $adapter;
/**
*
* @var \LeadersLinked\Cache\CacheInterface
*/
private $cache;
/**
*
* @var \Laminas\Log\LoggerInterface
*/
private $logger;
/**
*
* @var array
*/
private $config;
/**
*
* @var \Laminas\Mvc\I18n\Translator
*/
private $translator;
/**
*
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
* @param \LeadersLinked\Cache\CacheInterface $cache
* @param \Laminas\Log\LoggerInterface LoggerInterface $logger
* @param array $config
* @param \Laminas\Mvc\I18n\Translator $translator
*/
public function __construct($adapter, $cache, $logger, $config, $translator)
{
$this->adapter = $adapter;
$this->cache = $cache;
$this->logger = $logger;
$this->config = $config;
$this->translator = $translator;
}
public function indexAction()
{
$code = $this->params()->fromRoute('code');
$shorterMapper = ShorterMapper::getInstance($this->adapter);
$shorter = $shorterMapper->fetchOneByUuid($code);
if(!$shorter || $shorter->status == Shorter::STATUS_INACTIVE) {
$response = $this->getResponse();
$response->setStatusCode(404);
return $response;
}
return $this->redirect()->toUrl($shorter->url);
}
public function generateAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$code = $this->params()->fromRoute('code');
$type = $this->params()->fromRoute('type');
$timestamp = time();
list($usec, $sec) = explode(' ', microtime());
$seed = intval($sec + ((float) $usec * 100000));
mt_srand($seed, MT_RAND_MT19937);
$rand = mt_rand();
if($type == 'feed') {
$feedMapper = FeedMapper::getInstance($this->adapter);
$feed = $feedMapper->fetchOneByUuidAnyStatus($code);
if(!$feed) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_FEED_NOT_FOUND'
]);
}
$target = $currentUser->uuid . '-feed-' . $feed->uuid;
$password = md5('user-' . $currentUser->uuid . '-feed-' . $feed->uuid . '-timestamp-' . $timestamp . '-rand-' . $rand . '-share-key-' . $currentUser->share_key) ;
} else {
$postMapper = PostMapper::getInstance($this->adapter);
$post = $postMapper->fetchOneByUuidAnyStatus($code);
if(!$post) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_POST_NOT_FOUND'
]);
}
$target = $currentUser->uuid . '-post-' . $post->uuid;
$password = md5('user-' . $currentUser->uuid . '-post-' . $post->uuid . '-timestamp-' . $timestamp . '-rand-' . $rand . '-share-key-' . $currentUser->share_key) ;
}
$shorterMapper = ShorterMapper::getInstance($this->adapter);
$shorter = $shorterMapper->fetchOneByTarget($target);
if(!$shorter) {
if($type == 'feed') {
$share_params = [
'type' => 'feed',
'code' => $feed->uuid,
'user' => $currentUser->uuid,
'timestamp' => $timestamp,
'rand' => $rand,
'password' => $password,
];
} else {
$share_params = [
'type' => 'post',
'code' => $post->uuid,
'user' => $currentUser->uuid,
'timestamp' => $timestamp,
'rand' => $rand,
'password' => $password,
];
}
$url = $this->url()->fromRoute('share', $share_params , ['force_canonical' => true]);
$shorter = new Shorter();
$shorter->status = Shorter::STATUS_ACTIVE;
$shorter->target = $target;
$shorter->url = $url;
if(!$shorterMapper->insert($shorter)) {
return new JsonModel([
'success' => false,
'data' => $shorterMapper->getError()
]);
}
$shorter = $shorterMapper->fetchOne($shorter->id);
}
return new JsonModel([
'success' => true,
'data' => $url = $this->url()->fromRoute('shorter', ['code' => $shorter->uuid ] , ['force_canonical' => true]),
]);
}
}