| ... | ... |
@@ -207,7 +207,10 @@ class SecureDownloadsJob |
| 207 | 207 |
|
| 208 | 208 |
// Continue if no member found |
| 209 | 209 |
if ($objMember === null) {
|
| 210 |
- $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')));
|
|
| 210 |
+ if (System::getContainer()->getParameter('kernel.debug'))
|
|
| 211 |
+ {
|
|
| 212 |
+ $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')));
|
|
| 213 |
+ } |
|
| 211 | 214 |
continue; |
| 212 | 215 |
} |
| 213 | 216 |
|
| ... | ... |
@@ -95,7 +95,7 @@ class SecureDownloadsJob |
| 95 | 95 |
'member_firstname' => $Member->firstname, |
| 96 | 96 |
'member_lastname' => $Member->lastname, |
| 97 | 97 |
'downloads' => implode("\n",$arrDownloads),
|
| 98 |
- 'downloads_html' => "<ul>\n<li>".implode("</li>\n<li>",$arrDownloads)."</li>\n</ul>"
|
|
| 98 |
+ 'downloads_html' => "<ul class='vault_list'>\n<li>".implode("</li>\n<li>",$arrDownloads)."</li>\n</ul>"
|
|
| 99 | 99 |
),$GLOBALS['TL_LANGUAGE']); |
| 100 | 100 |
} |
| 101 | 101 |
|
| ... | ... |
@@ -32,14 +32,13 @@ use Psr\Log\LoggerInterface; |
| 32 | 32 |
use Psr\Log\LogLevel; |
| 33 | 33 |
use vonRotenberg\MemberfilesBundle\Model\MemberfilesConfigModel; |
| 34 | 34 |
|
| 35 |
+/** |
|
| 36 |
+ * Cron job for importing member files and sending notifications |
|
| 37 |
+ */ |
|
| 35 | 38 |
class SecureDownloadsJob |
| 36 | 39 |
{
|
| 37 |
- |
|
| 38 |
- /** @var LoggerInterface */ |
|
| 39 |
- private $logger; |
|
| 40 |
- |
|
| 41 |
- /** @var Connection */ |
|
| 42 |
- protected $db; |
|
| 40 |
+ private LoggerInterface $logger; |
|
| 41 |
+ private Connection $db; |
|
| 43 | 42 |
|
| 44 | 43 |
public function __construct(Connection $db, LoggerInterface $logger) |
| 45 | 44 |
{
|
| ... | ... |
@@ -48,33 +47,39 @@ class SecureDownloadsJob |
| 48 | 47 |
} |
| 49 | 48 |
|
| 50 | 49 |
/** |
| 50 |
+ * Send email notifications about new files to members |
|
| 51 |
+ * Runs every 10 minutes |
|
| 52 |
+ * |
|
| 51 | 53 |
* @CronJob("*\/10 * * * *")
|
| 52 | 54 |
*/ |
| 53 |
- public function sendFileNotifications(string $scope) |
|
| 55 |
+ public function sendFileNotifications(string $scope): void |
|
| 54 | 56 |
{
|
| 55 |
- // Get archives with notifications enabled |
|
| 57 |
+ // Get configurations with notifications enabled |
|
| 56 | 58 |
$Configurations = MemberfilesConfigModel::findBy(["hasNotification = ?", "enabled = ?"], ['1','1']); |
| 57 | 59 |
|
| 58 | 60 |
if ($Configurations !== null) |
| 59 | 61 |
{
|
| 60 | 62 |
while ($Configurations->next()) |
| 61 | 63 |
{
|
| 62 |
- // Do we have new items |
|
| 64 |
+ // Check for members with unsent file notifications |
|
| 63 | 65 |
$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 | 66 |
|
| 65 |
- // Load groups and notification models if we have news to share |
|
| 67 |
+ // Send notifications if there are new files and notification is configured |
|
| 66 | 68 |
if ($Members->rowCount() && ($Notification = Notification::findByPk($Configurations->notification)) !== null) |
| 67 | 69 |
{
|
| 68 | 70 |
foreach ($Members->iterateAssociative() as $member) |
| 69 | 71 |
{
|
| 72 |
+ // Only process active members with valid email addresses |
|
| 70 | 73 |
if (($Member = MemberModel::findOneBy(array("disable != '1'","login = '1'","email LIKE '%@%'","id = ?"),array($member['pid']))) !== null)
|
| 71 | 74 |
{
|
| 75 |
+ // Get all unsent files for this member |
|
| 72 | 76 |
$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]);
|
| 73 | 77 |
$arrFileIds = []; |
| 74 | 78 |
|
| 75 | 79 |
if ($Files->rowCount()) |
| 76 | 80 |
{
|
| 77 | 81 |
$arrDownloads = array(); |
| 82 |
+ // Build list of files for notification email |
|
| 78 | 83 |
foreach ($Files->iterateAssociative() as $file) |
| 79 | 84 |
{
|
| 80 | 85 |
if (($File = FilesModel::findByUuid($file['uuid'])) !== null) |
| ... | ... |
@@ -94,8 +99,14 @@ class SecureDownloadsJob |
| 94 | 99 |
),$GLOBALS['TL_LANGUAGE']); |
| 95 | 100 |
} |
| 96 | 101 |
|
| 97 |
- // Flag news as sent |
|
| 98 |
- $this->db->executeStatement("UPDATE tl_member_secureDownloads SET nc_sent = '1' WHERE id IN (" . implode(',', $arrFileIds) . ")");
|
|
| 102 |
+ // Flag notifications as sent |
|
| 103 |
+ if (!empty($arrFileIds)) {
|
|
| 104 |
+ $placeholders = implode(',', array_fill(0, count($arrFileIds), '?'));
|
|
| 105 |
+ $this->db->executeStatement( |
|
| 106 |
+ "UPDATE tl_member_secureDownloads SET nc_sent = '1' WHERE id IN ($placeholders)", |
|
| 107 |
+ $arrFileIds |
|
| 108 |
+ ); |
|
| 109 |
+ } |
|
| 99 | 110 |
} |
| 100 | 111 |
} |
| 101 | 112 |
$this->logger->log(LogLevel::INFO, 'Secure downloads notifications has been sent (' . $Members->rowCount() . ')', array('contao' => new ContaoContext(__METHOD__, 'CRON')));
|
| ... | ... |
@@ -105,22 +116,25 @@ class SecureDownloadsJob |
| 105 | 116 |
} |
| 106 | 117 |
|
| 107 | 118 |
/** |
| 119 |
+ * Import files from source folders and assign them to members |
|
| 120 |
+ * Runs every minute |
|
| 121 |
+ * |
|
| 108 | 122 |
* @CronJob("* * * * *")
|
| 109 | 123 |
*/ |
| 110 |
- public function importFiles(string $scope) |
|
| 124 |
+ public function importFiles(string $scope): void |
|
| 111 | 125 |
{
|
| 112 | 126 |
$projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
| 113 | 127 |
$Configurations = MemberfilesConfigModel::findAll(); |
| 114 | 128 |
|
| 115 | 129 |
$intFileCount = 0; |
| 116 | 130 |
|
| 117 |
- // No root pages available |
|
| 131 |
+ // Exit if no configurations available |
|
| 118 | 132 |
if ($Configurations === null) |
| 119 | 133 |
{
|
| 120 | 134 |
return; |
| 121 | 135 |
} |
| 122 | 136 |
|
| 123 |
- // Iterate page roots |
|
| 137 |
+ // Process each configuration |
|
| 124 | 138 |
while ($Configurations->next()) |
| 125 | 139 |
{
|
| 126 | 140 |
if (System::getContainer()->getParameter('kernel.debug'))
|
| ... | ... |
@@ -138,14 +152,14 @@ class SecureDownloadsJob |
| 138 | 152 |
$Source = FilesModel::findByUuid($Configurations->source); |
| 139 | 153 |
$Target = FilesModel::findByUuid($Configurations->target); |
| 140 | 154 |
|
| 141 |
- // Continue if folder doesn't exist |
|
| 155 |
+ // Continue if source or target folder doesn't exist |
|
| 142 | 156 |
if ($Source === null || !file_exists($projectDir . "/".$Source->path) || $Target === null || !file_exists($projectDir . "/".$Target->path)) |
| 143 | 157 |
{
|
| 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')));
|
|
| 158 |
+ $this->logger->log(LogLevel::WARNING, sprintf('Source or target folder is missing for configuration %s. %s %s', $Configurations->title,$Source->path,$Target->path), array('contao' => new ContaoContext(__METHOD__, 'ERROR')));
|
|
| 145 | 159 |
continue; |
| 146 | 160 |
} |
| 147 | 161 |
|
| 148 |
- // Read files inside folder |
|
| 162 |
+ // Read files from source folder |
|
| 149 | 163 |
$Files = FilesModel::findByPid($Source->uuid); |
| 150 | 164 |
|
| 151 | 165 |
// Continue if folder is empty |
| ... | ... |
@@ -174,14 +188,14 @@ class SecureDownloadsJob |
| 174 | 188 |
|
| 175 | 189 |
$objFile = new File($Files->path); |
| 176 | 190 |
|
| 177 |
- // Check for fragments |
|
| 191 |
+ // Extract member identifier from filename using regex |
|
| 178 | 192 |
if (!preg_match('/'.$Configurations->regexp.'/U',$objFile->filename,$fragments))
|
| 179 | 193 |
{
|
| 180 | 194 |
$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')));
|
| 181 | 195 |
continue; |
| 182 | 196 |
} |
| 183 | 197 |
|
| 184 |
- // Find member |
|
| 198 |
+ // Find member based on extracted fragments |
|
| 185 | 199 |
$arrColumns = array(); |
| 186 | 200 |
$arrValues = array(); |
| 187 | 201 |
foreach (StringUtil::deserialize($Configurations->fields, true) as $i => $field) |
| ... | ... |
@@ -197,11 +211,11 @@ class SecureDownloadsJob |
| 197 | 211 |
continue; |
| 198 | 212 |
} |
| 199 | 213 |
|
| 200 |
- // Remove fragments from file name |
|
| 214 |
+ // Remove member identifier fragments from filename |
|
| 201 | 215 |
$baseFilename = preg_replace('/'.$Configurations->regexp.'/U','',$objFile->filename);
|
| 202 | 216 |
$sanitizedFilename = $baseFilename.'.'.$objFile->extension; |
| 203 | 217 |
|
| 204 |
- // Check or create folder structure |
|
| 218 |
+ // Create organized folder structure (groups of 100 members per folder) |
|
| 205 | 219 |
$strSubFolder = sprintf("%03d",ceil(($objMember->id+1)/100)-1).'/'.$objMember->id;
|
| 206 | 220 |
$strTargetFolder = $Target->path.'/'.$strSubFolder; |
| 207 | 221 |
|
| ... | ... |
@@ -233,17 +247,16 @@ class SecureDownloadsJob |
| 233 | 247 |
|
| 234 | 248 |
$File = Dbafs::addResource($strTargetFolder.'/'.$sanitizedFilename,false); |
| 235 | 249 |
|
| 236 |
- // Add file to DB and delete source |
|
| 250 |
+ // Register file in database for member |
|
| 237 | 251 |
$arrData = array( |
| 238 | 252 |
'pid' => $objMember->id, |
| 239 | 253 |
'uuid' => ($File !== null ? $File->uuid : ''), |
| 240 | 254 |
'tstamp' => time(), |
| 241 | 255 |
'ctime' => time(), |
| 242 |
-// 'name' => $sanitizedFilename, |
|
| 243 |
-// 'path' => $strTargetFolder.'/'.$sanitizedFilename |
|
| 244 | 256 |
); |
| 245 | 257 |
$this->db->insert('tl_member_secureDownloads',$arrData);
|
| 246 | 258 |
|
| 259 |
+ // Delete source file after successful import |
|
| 247 | 260 |
if (!$objFile->delete()) {
|
| 248 | 261 |
$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')));
|
| 249 | 262 |
} |
| ... | ... |
@@ -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 |
{
|
| ... | ... |
@@ -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 |
{
|
| ... | ... |
@@ -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 |
} |
| ... | ... |
@@ -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 |
| ... | ... |
@@ -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 |
{
|
| ... | ... |
@@ -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 |
} |
| ... | ... |
@@ -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 |
{
|
| ... | ... |
@@ -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 |
} |
| 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 |
+} |