ReferersComponent.php 3.53 KB
Newer Older
1 2 3
<?php

/**
4
 * ReferersComponent
5
 *
6 7
 * web-DPO : Outil de gestion de vos traitements dans le cadre de la
 * réglementation relative à la protection des données personnelles (RGPD)
8
 *
9 10 11 12 13 14 15 16 17 18 19 20
 * Copyright (c) Libriciel SCOP (https://www.libriciel.fr/)
 *
 * Licensed under the GNU Affero General Public License version 3 License - AGPL v3
 * For full copyright and license information, please see the "LICENSE" file.
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright   Copyright (c) Libriciel SCOP (https://www.libriciel.fr/)
 * @link        https://www.libriciel.fr/web-dpo/
 * @since       web-DPO v1.0.0
 * @license     [GNU Affero General Public License version 3](http://www.gnu.org/licenses/agpl-3.0.html) - AGPL v3
 * @version     v1.0.0
 * @package     Component
21
 */
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
App::uses('Component', 'Controller');

class ReferersComponent extends Component {

    /**
     * Paramètres de ce component
     *
     * @todo defaultSettings
     *
     * @var array
     */
    public $defaultSettings = array(
        'sessionKeyPrefix' => 'Referers',
        //@todo clearOn...
        'clearOnBeforeRedirect' => '/users/logout'
    );

    /**
     * Components utilisés par ce component.
     *
     * @var array
     */
    public $components = array('Session');

    /**
     *
     * @param ComponentCollection $collection
     * @param array $settings
     */
    public function __construct(ComponentCollection $collection, $settings = array()) {
        parent::__construct($collection, $settings + $this->defaultSettings);

        $this->settings['clearOnBeforeRedirect'] = (array)$this->settings['clearOnBeforeRedirect'];
    }

    public function startup(Controller $controller) {
        $here = url_to_string($controller->request->here(false));
        $referer = url_to_string($controller->request->referer(true));
        $sessionKey = "{$this->settings['sessionKeyPrefix']}.{$here}";
        $stored = $this->Session->read($sessionKey);

64
        if (('/' !== $referer || null === $stored) && $stored !== $referer && $here !== $referer && $controller->request->param('requested') === false) {
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
            $this->Session->write($sessionKey, $referer);
        }

        // @fixme ?
        $controller->set('referer', $this->Session->read($sessionKey));
    }

    /**
     *
     * @param string|array $url
     * @return type
     */
    public function get($url = null) {//@todo: param defaults = /
        $controller = $this->_Collection->getController();

        if (null === $url) {
            $url = $controller->request->here(false);
        }
        $url = url_to_string($url);

        $sessionKey = "{$this->settings['sessionKeyPrefix']}.{$url}";
        return $this->Session->read($sessionKey);
    }

    /**
     *
     * @return type
     */
    public function clear() {
        return $this->Session->delete($this->settings['sessionKeyPrefix']);
    }

    protected function _clearOnCallback($method) {
        $controller = $this->_Collection->getController();
        $key = Inflector::variable('clear_on_' . $method);

        if (true === isset($this->settings[$key]) && false === empty($this->settings[$key])) {
            $here = url_to_string($controller->request->here(false));
            if (true === in_array($here, $this->settings[$key])) {
                $this->clear();
            }
        }
    }

    public function beforeRedirect(Controller $controller, $url, $status = null, $exit = true) {
        $this->_clearOnCallback(__FUNCTION__);
        return parent::beforeRedirect($controller, $url, $status, $exit);
    }

}