webservice/application/controllers/ServiceController.php
2017-03-30 17:11:36 +02:00

203 lines
8.5 KiB
PHP

<?php
/**
* Distribute all SOAP based Web Services
*
*/
class ServiceController extends Zend_Controller_Action
{
public function indexAction()
{
/**
* Be sure we don't make any render
*/
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$request = $this->getRequest();
// --- Get the service name, make sure the string is in lower case
$serviceName = strtolower($request->getParam('service', 'Entreprise'));
// --- ClassName and Directory with first letter capitalized
$serviceClassName = ucfirst($serviceName);
// --- Customize service for customers
if ('clients' == $serviceName) {
$client = strtolower($request->getParam('client', ''));
$clientClassName = ucfirst($client);
// --- Get list of customers
$clients = array();
$listeClients = include APPLICATION_PATH . '/../library/WsScore/Clients/ClientsConfig.php';
foreach ($listeClients as $section => $params) {
if ($params['actif']) {
$clients[$section] = $params;
}
}
if (!array_key_exists($client, $clients)) {
echo 'Service clients introuvable !';
exit;
}
$configServiceVersions = $clients[$client]['versions'];
}
// --- Service versions
else {
$services = include APPLICATION_PATH . '/../library/WsScore/ServicesConfig.php';
$configServiceVersions = $services[$serviceName]['versions'];
}
// --- Check versions
foreach ($configServiceVersions as $section => $params) {
$serviceVersions[$section] = $params;
if ($params['defaut']) {
$defautVersion = $section;
}
}
$version = $request->getParam('version', 'v'.$defautVersion);
$version = substr($version, 1);
// --- Version inexistante
if (!array_key_exists($version, $serviceVersions)) {
echo "Version inexistante.";
exit;
}
// --- Version désactivé
if (!$serviceVersions[$version]['actif']) {
echo "Version désactivée.";
exit;
}
// --- Charger les classes et les types pour le service suivant la version
if ('clients' == $serviceName) {
$pathServiceClassIni = 'WsScore/Clients/'.$clientClassName.'/v'.$version.'/Config.php';
$pathServiceClassPhp = 'WsScore/Clients/'.$clientClassName.'/v'.$version.'/Service.php';
// --- Gestion du mode de génération du wsdl
if (APPLICATION_ENV == 'development'
|| array_key_exists('mode', $serviceVersions[$version])
&& $serviceVersions[$version]['mode']=='auto') {
$pathServiceUrl = 'clients/'.$client.'/v'.$version.'?wsdl-auto';
} else {
$pathServiceUrl = 'clients/'.$client.'/v'.$version.'?wsdl';
}
$pathServiceUri = 'clients/'.$client.'/v'.$version;
// --- On redéfini le nom du service
$serviceClassName = 'Entreprise';
$fichierWsdl = $clientClassName.'-'.$serviceClassName.'-'.$version.'.wsdl';
} else {
$pathServiceClassIni = 'WsScore/'.$serviceClassName.'/v'.$version.'/Config.php';
$pathServiceClassPhp = 'WsScore/'.$serviceClassName.'/v'.$version.'/Service.php';
// --- Gestion du mode de génération du wsdl
if (APPLICATION_ENV == 'development'
|| array_key_exists('mode', $serviceVersions[$version])
&& $serviceVersions[$version]['mode']=='auto') {
$pathServiceUrl = $serviceName.'/v'.$version.'?wsdl-auto';
} else {
$pathServiceUrl = $serviceName.'/v'.$version.'?wsdl';
}
$pathServiceUri = $serviceName.'/v'.$version;
$fichierWsdl = $serviceClassName.'-'.$version.'.wsdl';
}
// --- Get map of WSDL type to PHP Classes
$classmap = include $pathServiceClassIni;
// --- Inclusion des classes de données
require_once $pathServiceClassPhp;
// --- Get hostname - add compatibility with Reverse Proxy
$hostName = $request->getHttpHost();
$hostScheme = $request->getScheme();
$http = new Zend_Controller_Request_Http();
$proxyScheme = $http->getHeader('X-Forwarded-Proto');
if ($proxyScheme == 'https') {
$hostScheme = 'https';
}
$fichierWsdl = $hostName . '-' . $hostScheme . '-' . $fichierWsdl;
$c = Zend_registry::get('config');
$wsdlPath = $c->profil->path->shared . '/wsdl';
// --- Fourniture du wsdl
if (isset($_GET['wsdl']) && file_exists($wsdlPath . '/' . $fichierWsdl)) {
if (!headers_sent()) {
header('Content-Type: text/xml');
}
readfile($wsdlPath . '/' . $fichierWsdl);
} elseif (isset($_GET['wsdl']) && !file_exists($wsdlPath . '/' . $fichierWsdl)
|| isset($_GET['wsdl-generate'])
|| isset($_GET['wsdl-auto'])) {
// --- Définition du webservice
$wsdl = new Zend_Soap_AutoDiscover();
$wsdl->setComplexTypeStrategy('Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence');
$wsdl->setOperationBodyStyle(array('use' => 'literal'));
$wsdl->setBindingStyle(array('style' => 'document'));
$wsdl->setClass($serviceClassName);
if ($hostScheme == 'https') {
$wsdl->setUri($hostScheme.'://'.$hostName.'/'.$pathServiceUri);
}
// --- Enregistrement du WSDL dans un fichier
if (isset($_GET['wsdl-generate'])) {
if (file_exists($wsdlPath . '/' . $fichierWsdl)) {
unlink($wsdlPath . '/' . $fichierWsdl);
}
$wsdl->dump($wsdlPath . '/' . $fichierWsdl);
echo "Le fichier $fichierWsdl a été généré";
// --- Génération/Fourniture du wsdl
} elseif (isset($_GET['wsdl']) && !file_exists($wsdlPath . '/' . $fichierWsdl)) {
$wsdl->dump($wsdlPath . '/' . $fichierWsdl);
if (!headers_sent()) {
header('Content-Type: text/xml');
}
readfile($wsdlPath . '/' . $fichierWsdl);
}
// --- Envoi sur la sortie standard le wsdl sans enregistrement dans un fichier
elseif (isset($_GET['wsdl-auto'])) {
$wsdl->handle();
}
}
// --- Fourniture du service
else {
// --- Traitement
if (in_array(APPLICATION_ENV, array('production', 'staging'))
&& file_exists($wsdlPath . '/' . $fichierWsdl)) {
$server = new Zend_Soap_Server($wsdlPath . '/' . $fichierWsdl);
} else {
$server = new Zend_Soap_Server($hostScheme.'://'.$hostName.'/'.$pathServiceUrl);
}
// --- Sonde paramètres server
$debug = false;
$debugUser = '';
if ($debug && $_SERVER['PHP_AUTH_USER'] == $debugUser) {
file_put_contents(APPLICATION_PATH . '/../debugserver.log',
"FichierWSDL : ".$fichierWsdl."\n".
"Hostname : ".$hostName."\n"
);
}
// --- Options et traitement de la requete
$server->setSoapFeatures(SOAP_USE_XSI_ARRAY_TYPE + SOAP_SINGLE_ELEMENT_ARRAYS);
$server->setClassmap($classmap);
$server->setEncoding('UTF-8');
$server->registerFaultException(array('Scores_Ws_Exception'));
$server->setWsiCompliant(true);
// --- Création du service
$server->setObject(new $serviceClassName());
$server->handle();
// --- Pour débuggage ultime
$debug = false;
$debugUser = '';
if ($debug && $_SERVER['PHP_AUTH_USER'] == $debugUser) {
file_put_contents(APPLICATION_PATH . '/../debugcall.log',
"FichierWSDL : ".$fichierWsdl."\n"."Hostname : ".$hostName."\n");
$request = $server->getLastRequest();
file_put_contents(APPLICATION_PATH . '/../debugcall.log', $request . "\n", FILE_APPEND);
$response = $server->getLastResponse();
file_put_contents(APPLICATION_PATH . '/../debugcall.log', $response. "\n", FILE_APPEND);
}
}
}
}