From: William Desportes <williamdes@wdes.fr>
Date: Mon, 16 Dec 2024 00:14:17 +0100
Subject: Add the vendor_config.php file and use it

In bootstrap.php the autoload must load first to allow libs in testMinimumPhpVersion to load

Origin: vendor
Forwarded: not-needed
---
 console                   |  2 ++
 core/bootstrap.php        |  5 ++---
 index.php                 |  2 ++
 js/tracker.php            | 12 +++++++-----
 matomo.php                |  6 +-----
 misc/cron/archive.php     |  2 ++
 misc/cron/updatetoken.php |  2 ++
 piwik.php                 |  2 ++
 vendor_config.php         | 31 +++++++++++++++++++++++++++++++
 9 files changed, 51 insertions(+), 13 deletions(-)
 create mode 100644 vendor_config.php

diff --git a/console b/console
index 069c992..9bc177d 100755
--- a/console
+++ b/console
@@ -3,6 +3,8 @@
 
 use Piwik\FrontController;
 
+require_once __DIR__ . '/vendor_config.php';
+
 if (!defined('PIWIK_DOCUMENT_ROOT')) {
     define('PIWIK_DOCUMENT_ROOT', dirname(__FILE__) == '/' ? '' : dirname(__FILE__));
 }
diff --git a/core/bootstrap.php b/core/bootstrap.php
index 411fa19..6056d68 100644
--- a/core/bootstrap.php
+++ b/core/bootstrap.php
@@ -27,6 +27,8 @@ error_reporting($errorLevel);
 @ini_set('display_errors', defined('PIWIK_DISPLAY_ERRORS') ? PIWIK_DISPLAY_ERRORS : @ini_get('display_errors'));
 @ini_set('xdebug.show_exception_trace', 0);
 
+// Composer autoloader
+require_once PIWIK_VENDOR_PATH . '/autoload.php';
 
 // NOTE: the code above must be PHP 4 compatible
 require_once PIWIK_INCLUDE_PATH . '/core/testMinimumPhpVersion.php';
@@ -42,9 +44,6 @@ disableEaccelerator();
 
 require_once PIWIK_INCLUDE_PATH . '/libs/upgradephp/upgrade.php';
 
-// Composer autoloader
-require_once PIWIK_VENDOR_PATH . '/autoload.php';
-
 require_once PIWIK_INCLUDE_PATH . '/libs/upgradephp/dev.php';
 
 require_once PIWIK_INCLUDE_PATH . '/DIObject.php';
diff --git a/index.php b/index.php
index f8640c3..7a20d33 100644
--- a/index.php
+++ b/index.php
@@ -6,6 +6,8 @@
  * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
+require_once __DIR__ . '/../vendor_config.php';
+
 if (!defined('PIWIK_DOCUMENT_ROOT')) {
     define('PIWIK_DOCUMENT_ROOT', dirname(__FILE__) == '/' ? '' : dirname(__FILE__));
 }
diff --git a/js/tracker.php b/js/tracker.php
index f71ab0e..a39b59f 100644
--- a/js/tracker.php
+++ b/js/tracker.php
@@ -9,13 +9,15 @@
 
 use Piwik\ProxyHttp;
 
+require_once __DIR__ . '/../vendor_config.php';
+
 /**
  * Tracker proxy
  */
 if ($_SERVER['REQUEST_METHOD'] == 'POST'
     || !empty($_SERVER['QUERY_STRING'])
 ) {
-    include '../piwik.php';
+    include MATOMO_PUBLIC_PATH . '/piwik.php';
     exit;
 }
 
@@ -24,7 +26,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST'
  *
  * @see core/Piwik.php
  */
-define('PIWIK_DOCUMENT_ROOT', '..');
+//define('PIWIK_DOCUMENT_ROOT', '..');
 
 // ensure errors are not printed
 ini_set('display_errors', 0);
@@ -46,7 +48,7 @@ require_once PIWIK_INCLUDE_PATH . '/libs/upgradephp/upgrade.php';
 // Composer autoloader
 require PIWIK_VENDOR_PATH . '/autoload.php';
 
-$file = '../matomo.js';
+$file = MATOMO_PUBLIC_PATH . '/matomo.js';
 
 $daysExpireFarFuture = 10;
 
@@ -67,9 +69,9 @@ $environment = new \Piwik\Application\Environment(null, array(
 $environment->init();
 
 if (!\Piwik\Tracker\IgnoreCookie::isIgnoreCookieFound()) {
-    
+
     $request = new \Piwik\Tracker\Request(array());
-    
+
     if ($request->shouldUseThirdPartyCookie()) {
         $visitorId = $request->getVisitorIdForThirdPartyCookie();
         if (!$visitorId) {
diff --git a/matomo.php b/matomo.php
index 1c2988a..0cfc6f3 100644
--- a/matomo.php
+++ b/matomo.php
@@ -7,8 +7,4 @@
  * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
-if (!defined('PIWIK_DOCUMENT_ROOT')) {
-    define('PIWIK_DOCUMENT_ROOT', dirname(__FILE__) == '/' ? '' : dirname(__FILE__));
-}
-
-include PIWIK_DOCUMENT_ROOT . '/piwik.php';
+require_once __DIR__ . '/piwik.php';
diff --git a/misc/cron/archive.php b/misc/cron/archive.php
index bad2d24..efe94ba 100644
--- a/misc/cron/archive.php
+++ b/misc/cron/archive.php
@@ -6,6 +6,8 @@
  * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  */
 
+require_once __DIR__ . '/../../vendor_config.php';
+
 if (!defined('PIWIK_DOCUMENT_ROOT')) {
     define('PIWIK_DOCUMENT_ROOT', realpath(dirname(__FILE__) . "/../.."));
 }
diff --git a/misc/cron/updatetoken.php b/misc/cron/updatetoken.php
index a69e738..38aec82 100644
--- a/misc/cron/updatetoken.php
+++ b/misc/cron/updatetoken.php
@@ -12,6 +12,8 @@ use Piwik\Application\Environment;
 use Piwik\Tests\Framework\TestingEnvironmentManipulator;
 use Piwik\Tests\Framework\TestingEnvironmentVariables;
 
+require_once __DIR__ . '/../../vendor_config.php';
+
 if (!defined('PIWIK_DOCUMENT_ROOT')) {
     define('PIWIK_DOCUMENT_ROOT', realpath(dirname(__FILE__) . "/../.."));
 }
diff --git a/piwik.php b/piwik.php
index 576c7dd..f3294e9 100644
--- a/piwik.php
+++ b/piwik.php
@@ -12,6 +12,8 @@ use Piwik\Tracker;
 use Piwik\Tracker\Handler;
 use Piwik\API\CORSHandler;
 
+require_once __DIR__ . '/../vendor_config.php';
+
 @ignore_user_abort(true);
 
 // Note: if you wish to debug the Tracking API please see this documentation:
diff --git a/vendor_config.php b/vendor_config.php
new file mode 100644
index 0000000..ff548cb
--- /dev/null
+++ b/vendor_config.php
@@ -0,0 +1,31 @@
+<?php
+
+$thisDir = __DIR__;
+
+if (!defined('PIWIK_DOCUMENT_ROOT')) {
+    define('PIWIK_DOCUMENT_ROOT', $thisDir);
+}
+
+if (!defined('PIWIK_INCLUDE_PATH')) {
+    define('PIWIK_INCLUDE_PATH', PIWIK_DOCUMENT_ROOT);
+}
+
+if (!defined('PIWIK_VENDOR_PATH')) {
+    define('PIWIK_VENDOR_PATH', PIWIK_DOCUMENT_ROOT);
+}
+
+if (!defined('PIWIK_USER_PATH')) {
+    define('PIWIK_USER_PATH', PIWIK_INCLUDE_PATH);
+}
+
+if (!defined('MATOMO_TMP_PATH')) {
+    define('MATOMO_TMP_PATH', '/var/cache/matomo/tmp');
+}
+
+if (!defined('MATOMO_PUBLIC_PATH')) {
+    define('MATOMO_PUBLIC_PATH', PIWIK_DOCUMENT_ROOT);
+}
+
+if (!defined('MATOMO_PLUGINS_PATH')) {
+    define('MATOMO_PLUGINS_PATH', PIWIK_DOCUMENT_ROOT);
+}
