Commit 7d850f41 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Refactorisation.

Ajout génération image informative si pas de droit d'utilisation de la photo.
parent 1009342e
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -2,6 +2,11 @@

namespace UnicaenLeocarte;

use UnicaenLeocarte\Controller\IndexControllerFactory;
use UnicaenLeocarte\Options\ModuleOptionsFactory;
use UnicaenLeocarte\Service\LeocarteServiceFactory;
use UnicaenLeocarte\Service\Soap\LeocarteSoapClientFactory;

return [
    'bjyauthorize'    => [
        'guards' => [
@@ -12,7 +17,7 @@ return [
                        'photo',
                    ],
//                    'privileges' => \UnicaenLeocarte\Provider\Privilege\ThesePrivileges::THESE_RECHERCHE,
                    'roles' => ['guest'],
                    'roles' => ['user'],
                ],
            ],
        ],
@@ -47,14 +52,15 @@ return [
    ],
    'service_manager' => [
        'factories' => [
            'UnicaenLeocarte\Options'      => Options\ModuleOptionsFactory::class,
            'UnicaenLeocarte\Service\Soap' => Service\Soap\LeocarteSoapServiceFactory::class,
            'UnicaenLeocarte\Service'     => LeocarteServiceFactory::class,
            'UnicaenLeocarte\Options'     => ModuleOptionsFactory::class,
            'UnicaenLeocarte\Soap\Client' => LeocarteSoapClientFactory::class,
        ],
    ],

    'controllers'  => [
        'factories' => [
            'UnicaenLeocarte\Controller\Index' => Controller\IndexControllerFactory::class,
            'UnicaenLeocarte\Controller\Index' => IndexControllerFactory::class,
        ],
    ],
    'view_manager' => [
+38 −17
Original line number Diff line number Diff line
@@ -2,31 +2,45 @@

namespace UnicaenLeocarte\Controller;

use UnicaenLeocarte\Service\Soap\LeocarteSoapServiceAwareInterface;
use UnicaenLeocarte\Service\Soap\LeocarteSoapServiceAwareTrait;
use UnicaenLeocarte\Service\LeocarteServiceAwareInterface;
use UnicaenLeocarte\Service\LeocarteServiceAwareTrait;
use Zend\Http\PhpEnvironment\Response;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Stdlib\Exception\ExtensionNotLoadedException;

class IndexController extends AbstractActionController implements LeocarteSoapServiceAwareInterface
class IndexController extends AbstractActionController implements LeocarteServiceAwareInterface
{
    use LeocarteSoapServiceAwareTrait;
    use LeocarteServiceAwareTrait;

    /**
     * Récupère et affiche la photo d'un étudiant.
     * Récupère la photo d'un étudiant.
     *
     * @returns string|response
     * NB: Si le droit d'utilisation de celle-ci n'est pas accordé, une image informative générée est retournée
     * (si l'extension PHP 'gd' est activée).
     *
     * @returns Response Réponse HTTP (par défaut, l'autorisation de mise en cache par le client est activée)
     */
    public function photoAction()
    {
        if (!($id = $this->params()->fromRoute('id'))) {
            return '[pas de photo]';
        }
        $id = $this->params()->fromRoute('id');
        $nocache = (bool) (int) $this->params()->fromQuery('nocache', '0');

        $results = $this->leocarteSoapService->getPhoto($id);
        $autorise = $this->leocarteService->getDroitUtilisationPhoto($id);
        if ($autorise) {
            $content = $this->leocarteService->getPhoto($id);
        }
        else {
            try {
                $content = $this->leocarteService->createPhotoNonAutorisee();
            }
            catch (ExtensionNotLoadedException $e) {
                $content = '';
            }
        }

        /** @var Response $response */
        $response = $this->getResponse();
        $response->setContent($content = $results->return->persons->data->value->imageValue);
        $response->setContent($content);

        $headers = $response->getHeaders();
        $headers
@@ -34,12 +48,19 @@ class IndexController extends AbstractActionController implements LeocarteSoapSe
            ->addHeaderLine('Content-Type', "image/jpeg")
            ->addHeaderLine('Content-length', strlen($content));

        // autorisation de la mise en cache
        if ($nocache) {
            $headers
                ->addHeaderLine('Cache-Control', "no-cache")
                ->addHeaderLine('Pragma', 'no-cache');
        }
        else {
            // autorisation de la mise en cache de l'image par le client
            $maxAge = 60 * 60 * 24; // 86400 secondes = 1 jour
            $headers
                ->addHeaderLine('Cache-Control', "private, max-age=$maxAge")
                ->addHeaderLine('Pragma', 'private')// tout sauf 'no-cache'
                ->addHeaderLine('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + $maxAge));
        }

        return $response;
    }
+4 −4
Original line number Diff line number Diff line
@@ -2,18 +2,18 @@

namespace UnicaenLeocarte\Controller;

use UnicaenLeocarte\Service\Soap\LeocarteSoapService;
use UnicaenLeocarte\Service\LeocarteService;
use Zend\Mvc\Controller\ControllerManager;

class IndexControllerFactory
{
    function __invoke(ControllerManager $controllerManager)
    {
        /** @var LeocarteSoapService $leocarteService */
        $service = $controllerManager->getServiceLocator()->get('UnicaenLeocarte\Service\Soap');
        /** @var LeocarteService $leocarteService */
        $service = $controllerManager->getServiceLocator()->get('UnicaenLeocarte\Service');

        $controller = new IndexController();
        $controller->setLeocarteSoapService($service);
        $controller->setLeocarteService($service);

        return $controller;
    }
+91 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenLeocarte\Service;

use UnicaenLeocarte\Service\Soap\LeocarteSoapClientAwareInterface;
use UnicaenLeocarte\Service\Soap\LeocarteSoapClientAwareTrait;
use Zend\Stdlib\Exception\ExtensionNotLoadedException;

/**
 * Web Service Léocarte.
 *
 * @author Unicaen
 */
class LeocarteService implements LeocarteSoapClientAwareInterface
{
    use LeocarteSoapClientAwareTrait;

    const UTILISATION_PHOTO_AUTORISEE = 'Utilisation autorisée';

    /**
     * Retourne le témoin de droit d'utilisation de la photo.
     *
     * @param string $identifiant Identifiant, ex: numéro étudiant
     * @return boolean
     */
    public function getDroitUtilisationPhoto($identifiant)
    {
        $result = $this->leocarteSoapClient->getDroitUtilisationPhoto($identifiant);

        $string = $result->return->persons->data->value->stringValue;

        return $string === self::UTILISATION_PHOTO_AUTORISEE;
    }

    /**
     *
     * @param string $identifiant Identifiant, ex: numéro étudiant
     * @return string Contenu "binaire" de la photo
     */
    public function getPhoto($identifiant)
    {
        /** @var \stdClass $result */
        $result =  $this->leocarteSoapClient->getPhoto($identifiant);

        // c'est le contenu "binaire" de la photo qui est retourné par le WS
        $imgData = $result->return->persons->data->value->imageValue;

        return $imgData;
    }

    /**
     * Fabrique une image informant que l'utilisation de la photo n'est pas autorisée.
     *
     * @param int $width
     * @param int $height
     * @return string Contenu binaire de l'image
     * @throws ExtensionNotLoadedException Si l'extension gd n'est pas activée
     */
    public function createPhotoNonAutorisee($width = 100, $height = 120)
    {
        if (! extension_loaded('gd')) {
            throw new ExtensionNotLoadedException("L'extension PHP 'gd' doit être activée pour fabriquer l'image");
        }

        $tmpImageFilePath = sys_get_temp_dir() . '/' . uniqid('unicaen-leocarte-photo-non-autorisee-') . '.png';

        // Create the image
        $img = imagecreatetruecolor($width, $height);

        // Create some colors
        $white = imagecolorallocate($img, 255, 255, 255);
        $grey = imagecolorallocate($img, 222, 222, 222);
        $black = imagecolorallocate($img, 0, 0, 0);
        imagefilledrectangle($img, 0, 0, $width - 1, $height - 1, $grey);

        // Add the text
        $text = utf8_decode("Utilisation | de la photo | non autorisée");
        $x = 10;
        $y = 20;
        foreach (explode('|', $text) as $part) {
            imagestring($img, 2, $x, $y, trim($part), $black);
            $y += 15;
        }

        // Using imagepng() results in clearer text compared with imagejpeg()
        imagepng($img, $tmpImageFilePath);
        imagedestroy($img);

        return file_get_contents($tmpImageFilePath);
    }
}
 No newline at end of file
+8 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenLeocarte\Service;

interface LeocarteServiceAwareInterface
{
    public function setLeocarteService(LeocarteService $service);
}
 No newline at end of file
Loading