Browse code

Fix another wrong attribute name in send file notation job

Benjamin Roth authored on09/05/2025 09:42:21
Showing1 changed files
... ...
@@ -63,7 +63,7 @@ class SecureDownloadsJob
63 63
                 $Members = $this->db->executeQuery("SELECT s.pid FROM tl_member_secureDownloads s INNER JOIN tl_files f ON f.uuid = s.uuid WHERE s.nc_sent != '1' GROUP BY s.pid");
64 64
 
65 65
                 // Load groups and notification models if we have news to share
66
-                if ($Members->rowCount() && ($Notification = Notification::findByPk($Configurations->sd_nc_notification)) !== null)
66
+                if ($Members->rowCount() && ($Notification = Notification::findByPk($Configurations->notification)) !== null)
67 67
                 {
68 68
                     foreach ($Members->iterateAssociative() as $member)
69 69
                     {
Browse code

Fix wrong attribute name in send file notation job

Benjamin Roth authored on09/05/2025 09:17:37
Showing1 changed files
... ...
@@ -53,7 +53,7 @@ class SecureDownloadsJob
53 53
     public function sendFileNotifications(string $scope)
54 54
     {
55 55
         // Get archives with notifications enabled
56
-        $Configurations = MemberfilesConfigModel::findBy(["hasNotifications = ?", "enabled = ?"], ['1','1']);
56
+        $Configurations = MemberfilesConfigModel::findBy(["hasNotification = ?", "enabled = ?"], ['1','1']);
57 57
 
58 58
         if ($Configurations !== null)
59 59
         {
Browse code

Update cronjob to use the newly introduced memberfiles configurations instead of the deprecated root page settings

Benjamin Roth authored on08/05/2025 11:02:03
Showing1 changed files
... ...
@@ -30,6 +30,7 @@ use Doctrine\DBAL\Connection;
30 30
 use NotificationCenter\Model\Notification;
31 31
 use Psr\Log\LoggerInterface;
32 32
 use Psr\Log\LogLevel;
33
+use vonRotenberg\MemberfilesBundle\Model\MemberfilesConfigModel;
33 34
 
34 35
 class SecureDownloadsJob
35 36
 {
... ...
@@ -52,17 +53,17 @@ class SecureDownloadsJob
52 53
     public function sendFileNotifications(string $scope)
53 54
     {
54 55
         // Get archives with notifications enabled
55
-        $Root = PageModel::findBy(array("sd_nc_enable = ?","type = 'root'"),'1');
56
+        $Configurations = MemberfilesConfigModel::findBy(["hasNotifications = ?", "enabled = ?"], ['1','1']);
56 57
 
57
-        if ($Root !== null)
58
+        if ($Configurations !== null)
58 59
         {
59
-            while ($Root->next())
60
+            while ($Configurations->next())
60 61
             {
61 62
                 // Do we have new items
62 63
                 $Members = $this->db->executeQuery("SELECT s.pid FROM tl_member_secureDownloads s INNER JOIN tl_files f ON f.uuid = s.uuid WHERE s.nc_sent != '1' GROUP BY s.pid");
63 64
 
64 65
                 // Load groups and notification models if we have news to share
65
-                if ($Members->rowCount() && ($Notification = Notification::findByPk($Root->sd_nc_notification)) !== null)
66
+                if ($Members->rowCount() && ($Notification = Notification::findByPk($Configurations->sd_nc_notification)) !== null)
66 67
                 {
67 68
                     foreach ($Members->iterateAssociative() as $member)
68 69
                     {
... ...
@@ -108,72 +109,73 @@ class SecureDownloadsJob
108 109
      */
109 110
     public function importFiles(string $scope)
110 111
     {
111
-        $objRoots = PageModel::findPublishedRootPages();
112
+        $projectDir = System::getContainer()->getParameter('kernel.project_dir');
113
+        $Configurations = MemberfilesConfigModel::findAll();
112 114
 
113 115
         $intFileCount = 0;
114 116
 
115 117
         // No root pages available
116
-        if ($objRoots === null)
118
+        if ($Configurations === null)
117 119
         {
118 120
             return;
119 121
         }
120 122
 
121 123
         // Iterate page roots
122
-        while ($objRoots->next())
124
+        while ($Configurations->next())
123 125
         {
124 126
             if (System::getContainer()->getParameter('kernel.debug'))
125 127
             {
126
-                $this->logger->log(LogLevel::DEBUG, sprintf('Starting secure downloads import for %s', $objRoots->title), array('contao' => new ContaoContext(__METHOD__, 'CRON')));
128
+                $this->logger->log(LogLevel::DEBUG, sprintf('Starting secure downloads import for %s', $Configurations->title), array('contao' => new ContaoContext(__METHOD__, 'CRON')));
127 129
             }
128 130
 
129 131
             // Continue if secure downloads is disabled
130
-            if (!$objRoots->secureDownloadsEnabled)
132
+            if (!$Configurations->enabled)
131 133
             {
132 134
                 continue;
133 135
             }
134 136
 
135 137
             // Get folder models
136
-            $objFolder = FilesModel::findByUuid($objRoots->secureDownloadsSRC);
137
-            $objTarget = FilesModel::findByUuid($objRoots->secureDownloadsTarget);
138
+            $Source = FilesModel::findByUuid($Configurations->source);
139
+            $Target = FilesModel::findByUuid($Configurations->target);
138 140
 
139 141
             // Continue if folder doesn't exist
140
-            if ($objFolder === null || !file_exists(TL_ROOT . "/".$objFolder->path) || $objTarget === null || !file_exists(TL_ROOT . "/".$objTarget->path))
142
+            if ($Source === null || !file_exists($projectDir . "/".$Source->path) || $Target === null || !file_exists($projectDir . "/".$Target->path))
141 143
             {
142
-                $this->logger->log(LogLevel::WARNING, sprintf('Source or target folder is missing for page root %s. %s %s', $objRoots->title,$objFolder->path,$objTarget->path), array('contao' => new ContaoContext(__METHOD__, 'ERROR')));
144
+                $this->logger->log(LogLevel::WARNING, sprintf('Source or target folder is missing for page root %s. %s %s', $Configurations->title,$Source->path,$Target->path), array('contao' => new ContaoContext(__METHOD__, 'ERROR')));
143 145
                 continue;
144 146
             }
145 147
 
146 148
             // Read files inside folder
147
-            $objFiles = FilesModel::findByPid($objFolder->uuid);
149
+            $Files = FilesModel::findByPid($Source->uuid);
148 150
 
149 151
             // Continue if folder is empty
150
-            if ($objFiles === null)
152
+            if ($Files === null)
151 153
             {
152 154
                 continue;
153 155
             }
154 156
 
155 157
             // Escape single quotes in fragment regexp
156
-            $objRoots->secureDownloadsRegExp = addcslashes($objRoots->secureDownloadsRegExp,'\'');
158
+            $Configurations->regexp = addcslashes($Configurations->regexp,'\'');
157 159
 
158 160
             // Iterate files
159
-            while ($objFiles->next())
161
+            while ($Files->next())
160 162
             {
161 163
                 // Skip subfolders and special files
162
-                if ($objFiles->type == 'folder' || strpos($objFiles->name,'.') === 0)
164
+                if ($Files->type == 'folder' || strpos($Files->name,'.') === 0)
163 165
                 {
164 166
                     continue;
165 167
                 }
166 168
 
167 169
                 // Continue if file doesn't exist
168
-                if (!file_exists(TL_ROOT . "/".$objFiles->path))
170
+                if (!file_exists($projectDir . "/".$Files->path))
169 171
                 {
170 172
                     continue;
171 173
                 }
172 174
 
173
-                $objFile = new File($objFiles->path);
175
+                $objFile = new File($Files->path);
174 176
 
175 177
                 // Check for fragments
176
-                if (!preg_match('/'.$objRoots->secureDownloadsRegExp.'/U',$objFile->filename,$fragments))
178
+                if (!preg_match('/'.$Configurations->regexp.'/U',$objFile->filename,$fragments))
177 179
                 {
178 180
                     $this->logger->log(LogLevel::WARNING, sprintf('File "%s" is missing a member fragment or has the wrong syntax.', $objFile->name), array('contao' => new ContaoContext(__METHOD__, 'ERROR')));
179 181
                     continue;
... ...
@@ -182,7 +184,7 @@ class SecureDownloadsJob
182 184
                 // Find member
183 185
                 $arrColumns = array();
184 186
                 $arrValues = array();
185
-                foreach (StringUtil::deserialize($objRoots->secureDownloadsFields, true) as $i => $field)
187
+                foreach (StringUtil::deserialize($Configurations->fields, true) as $i => $field)
186 188
                 {
187 189
                     $arrColumns[] = "$field = ?";
188 190
                     $arrValues[] = $fragments[$i+1];
... ...
@@ -196,19 +198,19 @@ class SecureDownloadsJob
196 198
                 }
197 199
 
198 200
                 // Remove fragments from file name
199
-                $baseFilename = preg_replace('/'.$objRoots->secureDownloadsRegExp.'/U','',$objFile->filename);
201
+                $baseFilename = preg_replace('/'.$Configurations->regexp.'/U','',$objFile->filename);
200 202
                 $sanitizedFilename = $baseFilename.'.'.$objFile->extension;
201 203
 
202 204
                 // Check or create folder structure
203 205
                 $strSubFolder = sprintf("%03d",ceil(($objMember->id+1)/100)-1).'/'.$objMember->id;
204
-                $strTargetFolder = $objTarget->path.'/'.$strSubFolder;
206
+                $strTargetFolder = $Target->path.'/'.$strSubFolder;
205 207
 
206 208
                 $arrFolders = explode('/',$strTargetFolder);
207 209
                 $strStartPath = '';
208 210
 
209 211
                 foreach ($arrFolders as $folder)
210 212
                 {
211
-                    if (!is_dir(TL_ROOT . '/' . $strStartPath . $folder))
213
+                    if (!is_dir($projectDir . '/' . $strStartPath . $folder))
212 214
                     {
213 215
                         Files::getInstance()->mkdir($strStartPath . $folder);
214 216
                     }
... ...
@@ -217,7 +219,7 @@ class SecureDownloadsJob
217 219
 
218 220
                 // Do not overwrite existing files
219 221
                 $copyId = 1;
220
-                while(file_exists(TL_ROOT . "/".$strTargetFolder.'/'.$sanitizedFilename))
222
+                while(file_exists($projectDir . "/".$strTargetFolder.'/'.$sanitizedFilename))
221 223
                 {
222 224
                     $sanitizedFilename = $baseFilename.'_'.$copyId++.'.'.$objFile->extension;
223 225
                 }
Browse code

Try to not overwrite existing files

Benjamin Roth authored on12/10/2023 15:11:12
Showing1 changed files
... ...
@@ -196,7 +196,8 @@ class SecureDownloadsJob
196 196
                 }
197 197
 
198 198
                 // Remove fragments from file name
199
-                $sanitizedFilename = preg_replace('/'.$objRoots->secureDownloadsRegExp.'/U','',$objFile->filename).'.'.$objFile->extension;
199
+                $baseFilename = preg_replace('/'.$objRoots->secureDownloadsRegExp.'/U','',$objFile->filename);
200
+                $sanitizedFilename = $baseFilename.'.'.$objFile->extension;
200 201
 
201 202
                 // Check or create folder structure
202 203
                 $strSubFolder = sprintf("%03d",ceil(($objMember->id+1)/100)-1).'/'.$objMember->id;
... ...
@@ -216,10 +217,9 @@ class SecureDownloadsJob
216 217
 
217 218
                 // Do not overwrite existing files
218 219
                 $copyId = 1;
219
-                $baseFilename = $sanitizedFilename;
220 220
                 while(file_exists(TL_ROOT . "/".$strTargetFolder.'/'.$sanitizedFilename))
221 221
                 {
222
-                    $sanitizedFilename = $baseFilename.'_'.$copyId++;
222
+                    $sanitizedFilename = $baseFilename.'_'.$copyId++.'.'.$objFile->extension;
223 223
                 }
224 224
 
225 225
                 // Try to copy file
Browse code

Try to not overwrite existing files

Benjamin Roth authored on12/10/2023 15:08:44
Showing1 changed files
... ...
@@ -214,6 +214,14 @@ class SecureDownloadsJob
214 214
                     $strStartPath .= $folder . '/';
215 215
                 }
216 216
 
217
+                // Do not overwrite existing files
218
+                $copyId = 1;
219
+                $baseFilename = $sanitizedFilename;
220
+                while(file_exists(TL_ROOT . "/".$strTargetFolder.'/'.$sanitizedFilename))
221
+                {
222
+                    $sanitizedFilename = $baseFilename.'_'.$copyId++;
223
+                }
224
+
217 225
                 // Try to copy file
218 226
                 if (!Files::getInstance()->copy($objFile->path,$strTargetFolder.'/'.$sanitizedFilename))
219 227
                 {
Browse code

Fix file notification job

Benjamin Roth authored on08/09/2023 16:29:35
Showing1 changed files
... ...
@@ -68,14 +68,19 @@ class SecureDownloadsJob
68 68
                     {
69 69
                         if (($Member = MemberModel::findOneBy(array("disable != '1'","login = '1'","email LIKE '%@%'","id = ?"),array($member['pid']))) !== null)
70 70
                         {
71
-                            $Files = $this->db->executeQuery("SELECT s.id, f.name, s.ctime FROM tl_member_secureDownloads s INNER JOIN tl_files f ON f.uuid = s.uuid WHERE s.pid = ? AND s.nc_sent != '1' ORDER BY s.ctime DESC, f.name",[$Member->id]);
71
+                            $Files = $this->db->executeQuery("SELECT s.id, f.uuid, s.ctime FROM tl_member_secureDownloads s INNER JOIN tl_files f ON f.uuid = s.uuid WHERE s.pid = ? AND s.nc_sent != '1' ORDER BY s.ctime DESC, f.name",[$Member->id]);
72
+                            $arrFileIds = [];
72 73
 
73 74
                             if ($Files->rowCount())
74 75
                             {
75 76
                                 $arrDownloads = array();
76
-                                foreach ($Files as $file)
77
+                                foreach ($Files->iterateAssociative() as $file)
77 78
                                 {
78
-                                    $arrDownloads[] = date('d.m.Y', $file['ctime']) . " - " . $file['name'];
79
+                                    if (($File = FilesModel::findByUuid($file['uuid'])) !== null)
80
+                                    {
81
+                                        $arrDownloads[] = date('d.m.Y', $file['ctime']) . " - " . $File->name;
82
+                                        $arrFileIds[] = $file['id'];
83
+                                    }
79 84
                                 }
80 85
 
81 86
                                 $Notification->send(array
... ...
@@ -89,7 +94,6 @@ class SecureDownloadsJob
89 94
                             }
90 95
 
91 96
                             // Flag news as sent
92
-                            $arrFileIds = $Files->fetchFirstColumn();
93 97
                             $this->db->executeStatement("UPDATE tl_member_secureDownloads SET nc_sent = '1' WHERE id IN (" . implode(',', $arrFileIds) . ")");
94 98
                         }
95 99
                     }
Browse code

Fix notification reminder job

Benjamin Roth authored on08/09/2023 16:13:43
Showing1 changed files
... ...
@@ -59,12 +59,12 @@ class SecureDownloadsJob
59 59
             while ($Root->next())
60 60
             {
61 61
                 // Do we have new items
62
-                $Members = $this->db->executeQuery("SELECT s.pid FROM tl_member_secureDownloads s INNER JOIN tl_files f ON f.uuid = s.uuid WHERE s.nc_sent != '1' GROUP BY s.pid ORDER BY s.ctime DESC, f.name");
62
+                $Members = $this->db->executeQuery("SELECT s.pid FROM tl_member_secureDownloads s INNER JOIN tl_files f ON f.uuid = s.uuid WHERE s.nc_sent != '1' GROUP BY s.pid");
63 63
 
64 64
                 // Load groups and notification models if we have news to share
65 65
                 if ($Members->rowCount() && ($Notification = Notification::findByPk($Root->sd_nc_notification)) !== null)
66 66
                 {
67
-                    foreach ($Members as $member)
67
+                    foreach ($Members->iterateAssociative() as $member)
68 68
                     {
69 69
                         if (($Member = MemberModel::findOneBy(array("disable != '1'","login = '1'","email LIKE '%@%'","id = ?"),array($member['pid']))) !== null)
70 70
                         {
Browse code

Update

Benjamin Roth authored on26/07/2023 17:00:15
Showing1 changed files
... ...
@@ -14,11 +14,22 @@ namespace vonRotenberg\MemberfilesBundle\Cron;
14 14
 
15 15
 use Contao\BackendUser;
16 16
 use Contao\Controller;
17
+use Contao\CoreBundle\Monolog\ContaoContext;
17 18
 use Contao\CoreBundle\ServiceAnnotation\Callback;
19
+use Contao\CoreBundle\ServiceAnnotation\CronJob;
18 20
 use Contao\DataContainer;
21
+use Contao\Dbafs;
22
+use Contao\File;
23
+use Contao\Files;
24
+use Contao\FilesModel;
25
+use Contao\MemberModel;
26
+use Contao\PageModel;
27
+use Contao\StringUtil;
19 28
 use Contao\System;
20 29
 use Doctrine\DBAL\Connection;
30
+use NotificationCenter\Model\Notification;
21 31
 use Psr\Log\LoggerInterface;
32
+use Psr\Log\LogLevel;
22 33
 
23 34
 class SecureDownloadsJob
24 35
 {
... ...
@@ -36,10 +47,200 @@ class SecureDownloadsJob
36 47
     }
37 48
 
38 49
     /**
39
-     * @CronJob("* * * * *")
50
+     * @CronJob("*\/10 * * * *")
40 51
      */
41 52
     public function sendFileNotifications(string $scope)
42 53
     {
54
+        // Get archives with notifications enabled
55
+        $Root = PageModel::findBy(array("sd_nc_enable = ?","type = 'root'"),'1');
56
+
57
+        if ($Root !== null)
58
+        {
59
+            while ($Root->next())
60
+            {
61
+                // Do we have new items
62
+                $Members = $this->db->executeQuery("SELECT s.pid FROM tl_member_secureDownloads s INNER JOIN tl_files f ON f.uuid = s.uuid WHERE s.nc_sent != '1' GROUP BY s.pid ORDER BY s.ctime DESC, f.name");
63
+
64
+                // Load groups and notification models if we have news to share
65
+                if ($Members->rowCount() && ($Notification = Notification::findByPk($Root->sd_nc_notification)) !== null)
66
+                {
67
+                    foreach ($Members as $member)
68
+                    {
69
+                        if (($Member = MemberModel::findOneBy(array("disable != '1'","login = '1'","email LIKE '%@%'","id = ?"),array($member['pid']))) !== null)
70
+                        {
71
+                            $Files = $this->db->executeQuery("SELECT s.id, f.name, s.ctime FROM tl_member_secureDownloads s INNER JOIN tl_files f ON f.uuid = s.uuid WHERE s.pid = ? AND s.nc_sent != '1' ORDER BY s.ctime DESC, f.name",[$Member->id]);
72
+
73
+                            if ($Files->rowCount())
74
+                            {
75
+                                $arrDownloads = array();
76
+                                foreach ($Files as $file)
77
+                                {
78
+                                    $arrDownloads[] = date('d.m.Y', $file['ctime']) . " - " . $file['name'];
79
+                                }
80
+
81
+                                $Notification->send(array
82
+                                (
83
+                                    'member_email' => $Member->email,
84
+                                    'member_firstname' => $Member->firstname,
85
+                                    'member_lastname' => $Member->lastname,
86
+                                    'downloads' => implode("\n",$arrDownloads),
87
+                                    'downloads_html' => "<ul>\n<li>".implode("</li>\n<li>",$arrDownloads)."</li>\n</ul>"
88
+                                ),$GLOBALS['TL_LANGUAGE']);
89
+                            }
90
+
91
+                            // Flag news as sent
92
+                            $arrFileIds = $Files->fetchFirstColumn();
93
+                            $this->db->executeStatement("UPDATE tl_member_secureDownloads SET nc_sent = '1' WHERE id IN (" . implode(',', $arrFileIds) . ")");
94
+                        }
95
+                    }
96
+                    $this->logger->log(LogLevel::INFO, 'Secure downloads notifications has been sent (' . $Members->rowCount() . ')', array('contao' => new ContaoContext(__METHOD__, 'CRON')));
97
+                }
98
+            }
99
+        }
100
+    }
101
+
102
+    /**
103
+     * @CronJob("* * * * *")
104
+     */
105
+    public function importFiles(string $scope)
106
+    {
107
+        $objRoots = PageModel::findPublishedRootPages();
108
+
109
+        $intFileCount = 0;
110
+
111
+        // No root pages available
112
+        if ($objRoots === null)
113
+        {
114
+            return;
115
+        }
116
+
117
+        // Iterate page roots
118
+        while ($objRoots->next())
119
+        {
120
+            if (System::getContainer()->getParameter('kernel.debug'))
121
+            {
122
+                $this->logger->log(LogLevel::DEBUG, sprintf('Starting secure downloads import for %s', $objRoots->title), array('contao' => new ContaoContext(__METHOD__, 'CRON')));
123
+            }
124
+
125
+            // Continue if secure downloads is disabled
126
+            if (!$objRoots->secureDownloadsEnabled)
127
+            {
128
+                continue;
129
+            }
130
+
131
+            // Get folder models
132
+            $objFolder = FilesModel::findByUuid($objRoots->secureDownloadsSRC);
133
+            $objTarget = FilesModel::findByUuid($objRoots->secureDownloadsTarget);
134
+
135
+            // Continue if folder doesn't exist
136
+            if ($objFolder === null || !file_exists(TL_ROOT . "/".$objFolder->path) || $objTarget === null || !file_exists(TL_ROOT . "/".$objTarget->path))
137
+            {
138
+                $this->logger->log(LogLevel::WARNING, sprintf('Source or target folder is missing for page root %s. %s %s', $objRoots->title,$objFolder->path,$objTarget->path), array('contao' => new ContaoContext(__METHOD__, 'ERROR')));
139
+                continue;
140
+            }
141
+
142
+            // Read files inside folder
143
+            $objFiles = FilesModel::findByPid($objFolder->uuid);
144
+
145
+            // Continue if folder is empty
146
+            if ($objFiles === null)
147
+            {
148
+                continue;
149
+            }
150
+
151
+            // Escape single quotes in fragment regexp
152
+            $objRoots->secureDownloadsRegExp = addcslashes($objRoots->secureDownloadsRegExp,'\'');
153
+
154
+            // Iterate files
155
+            while ($objFiles->next())
156
+            {
157
+                // Skip subfolders and special files
158
+                if ($objFiles->type == 'folder' || strpos($objFiles->name,'.') === 0)
159
+                {
160
+                    continue;
161
+                }
162
+
163
+                // Continue if file doesn't exist
164
+                if (!file_exists(TL_ROOT . "/".$objFiles->path))
165
+                {
166
+                    continue;
167
+                }
168
+
169
+                $objFile = new File($objFiles->path);
170
+
171
+                // Check for fragments
172
+                if (!preg_match('/'.$objRoots->secureDownloadsRegExp.'/U',$objFile->filename,$fragments))
173
+                {
174
+                    $this->logger->log(LogLevel::WARNING, sprintf('File "%s" is missing a member fragment or has the wrong syntax.', $objFile->name), array('contao' => new ContaoContext(__METHOD__, 'ERROR')));
175
+                    continue;
176
+                }
177
+
178
+                // Find member
179
+                $arrColumns = array();
180
+                $arrValues = array();
181
+                foreach (StringUtil::deserialize($objRoots->secureDownloadsFields, true) as $i => $field)
182
+                {
183
+                    $arrColumns[] = "$field = ?";
184
+                    $arrValues[] = $fragments[$i+1];
185
+                }
186
+                $objMember = MemberModel::findOneBy($arrColumns,$arrValues);
187
+
188
+                // Continue if no member found
189
+                if ($objMember === null) {
190
+                    $this->logger->log(LogLevel::WARNING, sprintf('Could not find member for file "%s". (%s) [%s]', $objFile->name, implode(' AND ',$arrColumns),implode(', ',$arrValues)), array('contao' => new ContaoContext(__METHOD__, 'ERROR')));
191
+                    continue;
192
+                }
193
+
194
+                // Remove fragments from file name
195
+                $sanitizedFilename = preg_replace('/'.$objRoots->secureDownloadsRegExp.'/U','',$objFile->filename).'.'.$objFile->extension;
196
+
197
+                // Check or create folder structure
198
+                $strSubFolder = sprintf("%03d",ceil(($objMember->id+1)/100)-1).'/'.$objMember->id;
199
+                $strTargetFolder = $objTarget->path.'/'.$strSubFolder;
200
+
201
+                $arrFolders = explode('/',$strTargetFolder);
202
+                $strStartPath = '';
203
+
204
+                foreach ($arrFolders as $folder)
205
+                {
206
+                    if (!is_dir(TL_ROOT . '/' . $strStartPath . $folder))
207
+                    {
208
+                        Files::getInstance()->mkdir($strStartPath . $folder);
209
+                    }
210
+                    $strStartPath .= $folder . '/';
211
+                }
212
+
213
+                // Try to copy file
214
+                if (!Files::getInstance()->copy($objFile->path,$strTargetFolder.'/'.$sanitizedFilename))
215
+                {
216
+                    $this->logger->log(LogLevel::WARNING, sprintf('Could not import file "%s" for member ID "%s".', $objFile->name, $objMember->id), array('contao' => new ContaoContext(__METHOD__, 'ERROR')));
217
+                    continue;
218
+                }
219
+
220
+                $File = Dbafs::addResource($strTargetFolder.'/'.$sanitizedFilename,false);
221
+
222
+                // Add file to DB and delete source
223
+                $arrData = array(
224
+                    'pid'     => $objMember->id,
225
+                    'uuid'    => ($File !== null ? $File->uuid : ''),
226
+                    'tstamp'  => time(),
227
+                    'ctime'   => time(),
228
+//          'name'    => $sanitizedFilename,
229
+//          'path'    => $strTargetFolder.'/'.$sanitizedFilename
230
+                );
231
+                $this->db->insert('tl_member_secureDownloads',$arrData);
232
+
233
+                if (!$objFile->delete()) {
234
+                    $this->logger->log(LogLevel::WARNING, sprintf('Could not delete source file "%s" for member ID "%s".', $objFile->name, $objMember->id), array('contao' => new ContaoContext(__METHOD__, 'ERROR')));
235
+                }
236
+
237
+                $intFileCount++;
238
+            }
239
+        }
43 240
 
241
+        if ($intFileCount)
242
+        {
243
+            $this->logger->log(LogLevel::INFO, sprintf("Imported %s files for secure member downloads",$intFileCount), array('contao' => new ContaoContext(__METHOD__, 'CRON')));
244
+        }
44 245
     }
45 246
 }
Browse code

Update

Benjamin Roth authored on26/07/2023 15:18:55
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,45 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of memberfiles bundle.
7
+ *
8
+ * (c) vonRotenberg
9
+ *
10
+ * @license commercial
11
+ */
12
+
13
+namespace vonRotenberg\MemberfilesBundle\Cron;
14
+
15
+use Contao\BackendUser;
16
+use Contao\Controller;
17
+use Contao\CoreBundle\ServiceAnnotation\Callback;
18
+use Contao\DataContainer;
19
+use Contao\System;
20
+use Doctrine\DBAL\Connection;
21
+use Psr\Log\LoggerInterface;
22
+
23
+class SecureDownloadsJob
24
+{
25
+
26
+    /** @var LoggerInterface */
27
+    private $logger;
28
+
29
+    /** @var Connection */
30
+    protected $db;
31
+
32
+    public function __construct(Connection $db, LoggerInterface $logger)
33
+    {
34
+        $this->logger = $logger;
35
+        $this->db = $db;
36
+    }
37
+
38
+    /**
39
+     * @CronJob("* * * * *")
40
+     */
41
+    public function sendFileNotifications(string $scope)
42
+    {
43
+
44
+    }
45
+}