Files
luos/workflow/engine/src/ProcessMaker/Util/System.php

164 lines
5.7 KiB
PHP
Raw Normal View History

<?php
2017-05-25 13:38:22 -04:00
namespace ProcessMaker\Util;
2017-05-18 11:57:56 -04:00
use \ProcessMaker\Services\OAuth2\PmPdo;
use \ProcessMaker\Services\OAuth2\Server;
use \OAuth2\Request;
class System
{
2017-05-18 11:57:56 -04:00
const CLIENT_ID = 'x-pm-local-client';
/**
* Get Time Zone
*
* @return string Return Time Zone
2017-05-18 11:57:56 -04:00
* @throws \Exception
*/
public static function getTimeZone()
{
try {
$arraySystemConfiguration = \PmSystem::getSystemConfiguration('', '', SYS_SYS);
//Return
return $arraySystemConfiguration['time_zone'];
} catch (\Exception $e) {
throw $e;
}
}
2017-05-24 11:26:13 -04:00
/**
2017-05-24 15:29:26 -04:00
* Flush the cache files for the specified workspace.
2017-05-25 13:38:22 -04:00
*
2017-05-24 15:29:26 -04:00
* @param object $workspace
2017-05-24 11:26:13 -04:00
*/
2017-05-24 15:29:26 -04:00
public static function flushCache($workspace)
2017-05-24 11:26:13 -04:00
{
2017-05-24 15:29:26 -04:00
try {
//Update singleton file by workspace
2017-05-24 11:26:13 -04:00
\Bootstrap::setConstantsRelatedWs($workspace->name);
$pathSingleton = PATH_DATA . "sites" . PATH_SEP . $workspace->name . PATH_SEP . "plugin.singleton";
$oPluginRegistry = \PMPluginRegistry::loadSingleton($pathSingleton);
$items = \PMPlugin::getListAllPlugins($workspace->name);
foreach ($items as $item) {
if ($item->enabled === true) {
require_once($item->sFilename);
$details = $oPluginRegistry->getPluginDetails(basename($item->sFilename));
//Only if the API directory structure is defined
$pathApiDirectory = PATH_PLUGINS . $details->sPluginFolder . PATH_SEP . "src" . PATH_SEP . "Services" . PATH_SEP . "Api";
if (is_dir($pathApiDirectory)) {
$pluginSrcDir = PATH_PLUGINS . $details->sNamespace . PATH_SEP . 'src';
$loader = \Maveriks\Util\ClassLoader::getInstance();
$loader->add($pluginSrcDir);
$oPluginRegistry->registerRestService($details->sNamespace);
if (class_exists($details->sClassName)) {
$oPlugin = new $details->sClassName($details->sNamespace, $details->sFilename);
$oPlugin->setup();
}
2017-05-25 09:09:09 -04:00
file_put_contents($pathSingleton, $oPluginRegistry->serializeInstance());
2017-05-24 11:26:13 -04:00
}
}
}
2017-05-24 15:29:26 -04:00
//flush the cache files
\G::rm_dir(PATH_C);
\G::mk_dir(PATH_C, 0777);
2017-05-24 11:26:13 -04:00
\G::rm_dir($workspace->path . "/cache");
\G::mk_dir($workspace->path . "/cache", 0777);
\G::rm_dir($workspace->path . "/cachefiles");
\G::mk_dir($workspace->path . "/cachefiles", 0777);
if (file_exists($workspace->path . '/routes.php')) {
unlink($workspace->path . '/routes.php');
}
2017-05-24 15:29:26 -04:00
} catch (\Exception $e) {
throw new \Exception("Error: cannot perform this task. " . $e->getMessage());
2017-05-24 11:26:13 -04:00
}
}
2017-05-18 11:57:56 -04:00
/**
* Get Token with USER_LOGGED saved in $_SESSION
*
* @return array
*/
2017-05-22 09:52:17 -04:00
public static function tokenUserLogged()
2017-05-18 11:57:56 -04:00
{
$client = self::getClientCredentials();
2017-05-22 09:52:17 -04:00
$authCode = self::getAuthorizationCodeUserLogged($client);
2017-05-18 11:57:56 -04:00
$loader = \Maveriks\Util\ClassLoader::getInstance();
$loader->add(PATH_TRUNK . 'vendor/bshaffer/oauth2-server-php/src/', "OAuth2");
$request = array(
'grant_type' => 'authorization_code',
'code' => $authCode
);
$server = array(
'REQUEST_METHOD' => 'POST'
);
$headers = array(
"PHP_AUTH_USER" => $client['CLIENT_ID'],
"PHP_AUTH_PW" => $client['CLIENT_SECRET'],
"Content-Type" => "multipart/form-data;",
"Authorization" => "Basic " . base64_encode($client['CLIENT_ID'] . ":" . $client['CLIENT_SECRET'])
);
$request = new Request(array(), $request, array(), array(), array(), $server, null, $headers);
$oauthServer = new Server();
$response = $oauthServer->postToken($request, true);
$clientToken = $response->getParameters();
$clientToken["client_id"] = $client['CLIENT_ID'];
$clientToken["client_secret"] = $client['CLIENT_SECRET'];
return $clientToken;
}
2017-05-22 09:52:17 -04:00
/**
* Get client credentials
* @return array
*/
2017-05-18 11:57:56 -04:00
protected function getClientCredentials()
{
$oauthQuery = new PmPdo(self::getDsn());
return $oauthQuery->getClientDetails(self::CLIENT_ID);
}
2017-05-22 09:52:17 -04:00
/**
* Get DNS of workspace
* @return array
*/
2017-05-18 11:57:56 -04:00
protected function getDsn()
{
list($host, $port) = strpos(DB_HOST, ':') !== false ? explode(':', DB_HOST) : array(DB_HOST, '');
$port = empty($port) ? '' : ";port=$port";
$dsn = DB_ADAPTER . ':host=' . $host . ';dbname=' . DB_NAME . $port;
return array('dsn' => $dsn, 'username' => DB_USER, 'password' => DB_PASS);
}
2017-05-22 09:52:17 -04:00
/**
* Get authorization code for user logged in session
* @param $client
* @return bool|string
*/
protected function getAuthorizationCodeUserLogged($client)
2017-05-18 11:57:56 -04:00
{
Server::setDatabaseSource(self::getDsn());
Server::setPmClientId($client['CLIENT_ID']);
$oauthServer = new Server();
$userId = $_SESSION['USER_LOGGED'];
$authorize = true;
$_GET = array_merge($_GET, array(
'response_type' => 'code',
'client_id' => $client['CLIENT_ID'],
'scope' => implode(' ', $oauthServer->getScope())
));
$response = $oauthServer->postAuthorize($authorize, $userId, true);
$code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=') + 5, 40);
return $code;
}
}