Files
prestashop/classes/ConnectionsSource.php
2026-04-09 18:31:51 +02:00

108 lines
4.0 KiB
PHP

<?php
/**
* For the full copyright and license information, please view the
* docs/licenses/LICENSE.txt file that was distributed with this source code.
*/
/**
* Class ConnectionsSourceCore.
*/
class ConnectionsSourceCore extends ObjectModel
{
public $id_connections;
public $http_referer;
public $request_uri;
public $keywords;
public $date_add;
public static $uri_max_size = 255;
/**
* @see ObjectModel::$definition
*/
public static $definition = [
'table' => 'connections_source',
'primary' => 'id_connections_source',
'fields' => [
'id_connections' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true],
'http_referer' => ['type' => self::TYPE_STRING, 'validate' => 'isAbsoluteUrl', 'size' => 255],
'request_uri' => ['type' => self::TYPE_STRING, 'validate' => 'isUrl', 'size' => 255],
'keywords' => ['type' => self::TYPE_STRING, 'validate' => 'isMessage', 'size' => 255],
'date_add' => ['type' => self::TYPE_DATE, 'validate' => 'isDate', 'required' => true],
],
];
public static function logHttpReferer(?Cookie $cookie = null)
{
if (!$cookie) {
$cookie = Context::getContext()->cookie;
}
if (!isset($cookie->id_connections) || !Validate::isUnsignedInt($cookie->id_connections)) {
return false;
}
// If the referrer is not correct, we drop the connection
if (isset($_SERVER['HTTP_REFERER']) && !Validate::isAbsoluteUrl($_SERVER['HTTP_REFERER'])) {
return false;
}
$source = new ConnectionsSource();
$source->request_uri = Tools::getHttpHost();
// There are a few more operations if there is a referrer
if (!empty($_SERVER['HTTP_REFERER'])) {
// If the referrer is internal (i.e. from your own website), then we drop the connection
$parsed = parse_url($_SERVER['HTTP_REFERER']);
$parsedHost = parse_url(Tools::getProtocol() . $source->request_uri . __PS_BASE_URI__);
if (!isset($parsed['host']) || !isset($parsed['path']) || !isset($parsedHost['path'])) {
return false;
}
if (
preg_replace('/^www./', '', $parsed['host']) == preg_replace('/^www./', '', $source->request_uri)
&& !strncmp($parsed['path'], $parsedHost['path'], strlen(__PS_BASE_URI__))
) {
return false;
}
$source->http_referer = substr($_SERVER['HTTP_REFERER'], 0, ConnectionsSource::$uri_max_size);
$source->keywords = substr(trim(SearchEngine::getKeywords($_SERVER['HTTP_REFERER'])), 0, ConnectionsSource::$uri_max_size);
}
$source->id_connections = (int) $cookie->id_connections;
if (isset($_SERVER['REQUEST_URI'])) {
$source->request_uri .= $_SERVER['REQUEST_URI'];
} elseif (isset($_SERVER['REDIRECT_URL'])) {
$source->request_uri .= $_SERVER['REDIRECT_URL'];
}
if (!Validate::isUrl($source->request_uri)) {
$source->request_uri = '';
} else {
$source->request_uri = substr($source->request_uri, 0, ConnectionsSource::$uri_max_size);
}
return $source->add();
}
/**
* Get Order sources.
*
* @param int $idOrder Order ID
*
* @return array|false|mysqli_result|PDOStatement|resource|null
*/
public static function getOrderSources($idOrder)
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT cos.`http_referer`, cos.`request_uri`, cos.`keywords`, cos.`date_add`
FROM `' . _DB_PREFIX_ . 'orders` o
INNER JOIN `' . _DB_PREFIX_ . 'guest` g ON g.`id_customer` = o.`id_customer`
INNER JOIN `' . _DB_PREFIX_ . 'connections` co ON co.`id_guest` = g.`id_guest`
INNER JOIN `' . _DB_PREFIX_ . 'connections_source` cos ON cos.`id_connections` = co.`id_connections`
WHERE `id_order` = ' . (int) $idOrder . '
ORDER BY cos.`date_add` DESC');
}
}