瀏覽代碼

add dev-sites supports and grant typo3 user rights on new site

Olivier Massot 5 年之前
父節點
當前提交
1c5d3b4b04
共有 1 個文件被更改,包括 29 次插入7 次删除
  1. 29 7
      ot_stats/Classes/Domain/Repository/MatomoWebsiteRepository.php

+ 29 - 7
ot_stats/Classes/Domain/Repository/MatomoWebsiteRepository.php

@@ -16,8 +16,9 @@ class MatomoWebsiteRepository
     const MATOMO_DB_USER = 'root';
     const MATOMO_DB_PWD = 'mysql2iopenservice369566';
 
-    const DOMAIN_VALIDATION = '/^(https?:\/\/)?([a-zA-Z0-9]+\.)+[a-zA-Z]{2,}$/';
-
+    const RELATIVE_PATH_VALIDATION = '/^\/[a-zA-Z0-9_\-]+\/?$/';
+    const DOMAIN_VALIDATION = '/^(https?:\/\/)?([a-zA-Z0-9]+\.)+[a-zA-Z]{2,}(:\d+)?(\/[a-zA-Z0-9_\-]+)*\/?$/';
+//    const DOMAIN_VALIDATION = '/^(https?:\/\/)?([a-zA-Z0-9]+\.)+[a-zA-Z]{2,}$/';
 
     /**
      * Connection to the matomo DB
@@ -132,16 +133,29 @@ class MatomoWebsiteRepository
         $rootUid = $rootPage['uid'];
 
         // Retrieve current site informations
+        $isDev = ($_SERVER['TYPO3_CONTEXT'] == 'Development');
+
         $site = $otPageRepository->getSiteFor($rootUid);
         $title = $site->getIdentifier();
-        $uri = $site->getConfiguration()['base'];
-        if (!preg_match(self::DOMAIN_VALIDATION, $uri)) {
-            throw new \RuntimeException("The domain's name is not valid: " . $uri);
+        if ($isDev) {
+            $title = $title . "_DEV";
         }
-        if (!preg_match('/https?:\/\//', $uri)) {
+
+        $uri = $site->getBase();
+        if ($isDev && preg_match(self::RELATIVE_PATH_VALIDATION, $uri)) {
+            $host = $_SERVER['HTTP_HOST'];
+            $uri = $host . $uri;
+        }
+
+//        $uri = $site->getConfiguration()['base'];
+        if (!preg_match('/https?:\/\/.*/', $uri)) {
             $uri = 'https://' . $uri;
         }
 
+        if (!preg_match(self::DOMAIN_VALIDATION, $uri)) {
+            throw new \RuntimeException("The domain's name is not valid: " . $uri);
+        }
+
         // Make sure the current typo3 website has no existing configured matomo website
         if ($rootPage['tx_opentalent_matomo_id'] != null) {
             // Check if the registered matomo site still exist. If it does, throw an error.
@@ -167,6 +181,7 @@ class MatomoWebsiteRepository
         $this->matomoCnn->beginTransaction();
         $cnnPool->getConnectionByName('Default')->beginTransaction();
         try {
+            $ignoreIps = $isDev ? '' : '10.8.0.*';
 
             if ($existingMatomoSite == null) {
                 // Create the new website
@@ -176,11 +191,18 @@ class MatomoWebsiteRepository
                            sitesearch_category_parameters, timezone, 
                            currency, excluded_ips, excluded_parameters, excluded_user_agents, `group`, `type`, creator_login)
                           VALUES ('" . $title . "', '" . $uri . "', NOW(), 0, 1, '', '', 'Europe/Paris', 
-                                  'EUR', '10.8.0.*', '', '', '', 'website', 'typo3');");
+                                  'EUR', '" . $ignoreIps . "', '', '', '', 'website', 'typo3');");
                 $stmt->execute();
 
                 // Get the new site's id
                 $matomoSiteId = $this->matomoCnn->lastInsertId();
+
+                # Give the right to the typo3 user on this new site
+                $stmt = $this->matomoCnn->prepare(
+                    "INSERT INTO matomo.matomo_access
+                          (login, idSite, access)
+                          VALUES ('typo3', $matomoSiteId, 'view');");
+                $stmt->execute();
             }
 
             // Update typo3 DB