CmisConnectionApi.php in CMIS API 3.0.x
File
src/CmisConnectionApi.php
View source
<?php
declare (strict_types=1);
namespace Drupal\cmis;
use Dkd\PhpCmis\Enum\BindingType;
use Dkd\PhpCmis\SessionFactory;
use Dkd\PhpCmis\SessionParameter;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Message;
use Symfony\Component\HttpFoundation\RedirectResponse;
class CmisConnectionApi {
use StringTranslationTrait;
private $config;
private $httpInvoker;
private $parameters;
private $sessionFactory;
private $session;
private $rootFolder;
public function __construct($config = '') {
$this
->setConfig($config);
}
private function setConfig($config_id) {
$storage = \Drupal::entityTypeManager()
->getStorage('cmis_connection_entity');
if ($this->config = $storage
->load($config_id)) {
$this
->setHttpInvoker();
}
}
public function getConfig() {
return $this->config;
}
public function getConfigFromId($config_id) {
$storage = \Drupal::entityTypeManager()
->getStorage('cmis_connection_entity');
return $storage
->load($config_id);
}
private function setHttpInvoker() {
if (\Drupal::currentUser()
->isAuthenticated()) {
if (!empty($this->config
->getCmisUser()) && !empty($this->config
->getCmisPassword())) {
$auth = [
'auth' => [
$this->config
->getCmisUser(),
$this->config
->getCmisPassword(),
],
];
}
else {
$tempstore = \Drupal::service('tempstore.private')
->get('cmis_alfresco_auth_user');
$auth = [
'headers' => [
'Authorization' => 'Basic ' . $tempstore
->get('ticket'),
],
];
}
$this->httpInvoker = new Client($auth);
}
}
public function getHttpInvoker() {
return $this->httpInvoker;
}
public function setDefaultParameters() {
$parameters = [
SessionParameter::BINDING_TYPE => BindingType::BROWSER,
SessionParameter::BROWSER_URL => $this
->getConfig()
->getCmisUrl(),
SessionParameter::BROWSER_SUCCINCT => FALSE,
SessionParameter::HTTP_INVOKER_OBJECT => $this
->getHttpInvoker(),
];
$this
->setParameters($parameters);
}
public function setParameters(array $parameters) {
$this->parameters = $parameters;
$this
->setSessionFactory();
}
public function getParameters() {
return $this->parameters;
}
private function setSessionFactory() {
$this->sessionFactory = new SessionFactory();
$this
->setRepository();
}
public function getSessionFactory() {
return $this->sessionFactory;
}
private function setRepository() {
$repository_id = $this->config
->getCmisRepository();
if ($repository_id === NULL || $repository_id == '') {
$repositories = $this->sessionFactory
->getRepositories($this->parameters);
$this->parameters[SessionParameter::REPOSITORY_ID] = $repositories[0]
->getId();
}
else {
$this->parameters[SessionParameter::REPOSITORY_ID] = $repository_id;
}
$this->session = $this->sessionFactory
->createSession($this->parameters);
$this
->setRootFolder();
}
public function getSession() {
return $this->session;
}
private function setRootFolder() {
$this->rootFolder = $this->session
->getRootFolder();
}
public function getRootFolder() {
return $this->rootFolder;
}
public function getObjectById($id = '') {
if (empty($id)) {
return NULL;
}
if (!empty($this
->validObjectId($id) || !empty($this
->validObjectId($id, 'cmis:document')))) {
$cid = $this->session
->createObjectId($id);
$object = $this->session
->getObject($cid);
return $object;
}
return NULL;
}
public function validObjectId($id, $type = 'cmis:folder', $parentId = '') {
$where = "cmis:objectId='{$id}'";
if (!empty($parentId)) {
$where .= " AND IN_FOLDER('{$parentId}')";
}
$result = $this->session
->queryObjects($type, $where);
return $result;
}
public function validObjectName($name, $type = 'cmis:folder', $parentId = '') {
$query = "SELECT * FROM {$type} WHERE cmis:name='{$name}'";
if (!empty($parentId)) {
$query .= " and IN_FOLDER('{$parentId}')";
}
$result = $this->session
->query($query);
return $result;
}
public function checkConnectionIsAlive($config_id, $is_ajax = FALSE) {
$tempstore = \Drupal::service('tempstore.private')
->get('cmis_alfresco_auth_user');
if ($tempstore
->get('ticket')) {
$this
->setConfig($config_id);
$this
->setHttpInvoker();
try {
$this->httpInvoker
->request('GET', $this
->getConfig()
->getCmisUrl());
return TRUE;
} catch (RequestException $e) {
$guzzle_request = Message::toString($e
->getRequest());
\Drupal::logger('cmis')
->notice($guzzle_request);
if ($e
->getCode() === 401) {
$guzzle_response = Message::toString($e
->getResponse());
\Drupal::logger('cmis')
->notice($guzzle_response);
if (\Drupal::currentUser()
->isAuthenticated()) {
user_logout();
if ($is_ajax) {
$command = new RedirectCommand(Url::fromRoute('user.login')
->toString());
$response = new AjaxResponse();
$response
->addCommand($command);
}
else {
$response = new RedirectResponse(Url::fromRoute('user.login')
->toString());
$response
->send();
}
}
}
} catch (\Exception $e) {
\Drupal::logger('cmis')
->error($e
->getMessage());
}
return FALSE;
}
}
public function hasAllowableActionById($id, $action) {
if (empty($id)) {
return FALSE;
}
$cid = $this->session
->createObjectId($id);
$object = $this->session
->getObject($cid);
$get_actions = $object
->getAllowableActions()
->getAllowableActions();
foreach ($get_actions as $get_action) {
if ($get_action
->equals($action)) {
return TRUE;
}
}
return FALSE;
}
}