logger = $logger; $this->db = $db; } /** * Send email notifications about new files to members * Runs every 10 minutes * * @CronJob("*\/10 * * * *") */ public function sendFileNotifications(string $scope): void { // Get configurations with notifications enabled $Configurations = MemberfilesConfigModel::findBy(["hasNotification = ?", "enabled = ?"], ['1','1']); if ($Configurations !== null) { while ($Configurations->next()) { // Check for members with unsent file notifications $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"); // Send notifications if there are new files and notification is configured if ($Members->rowCount() && ($Notification = Notification::findByPk($Configurations->notification)) !== null) { foreach ($Members->iterateAssociative() as $member) { // Only process active members with valid email addresses if (($Member = MemberModel::findOneBy(array("disable != '1'","login = '1'","email LIKE '%@%'","id = ?"),array($member['pid']))) !== null) { // Get all unsent files for this member $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]); $arrFileIds = []; if ($Files->rowCount()) { $arrDownloads = array(); // Build list of files for notification email foreach ($Files->iterateAssociative() as $file) { if (($File = FilesModel::findByUuid($file['uuid'])) !== null) { $arrDownloads[] = date('d.m.Y', $file['ctime']) . " - " . $File->name; $arrFileIds[] = $file['id']; } } $Notification->send(array ( 'member_email' => $Member->email, 'member_firstname' => $Member->firstname, 'member_lastname' => $Member->lastname, 'downloads' => implode("\n",$arrDownloads), 'downloads_html' => "" ),$GLOBALS['TL_LANGUAGE']); } // Flag notifications as sent if (!empty($arrFileIds)) { $placeholders = implode(',', array_fill(0, count($arrFileIds), '?')); $this->db->executeStatement( "UPDATE tl_member_secureDownloads SET nc_sent = '1' WHERE id IN ($placeholders)", $arrFileIds ); } } } $this->logger->log(LogLevel::INFO, 'Secure downloads notifications has been sent (' . $Members->rowCount() . ')', array('contao' => new ContaoContext(__METHOD__, 'CRON'))); } } } } /** * Import files from source folders and assign them to members * Runs every minute * * @CronJob("* * * * *") */ public function importFiles(string $scope): void { $projectDir = System::getContainer()->getParameter('kernel.project_dir'); $Configurations = MemberfilesConfigModel::findAll(); $intFileCount = 0; // Exit if no configurations available if ($Configurations === null) { return; } // Process each configuration while ($Configurations->next()) { if (System::getContainer()->getParameter('kernel.debug')) { $this->logger->log(LogLevel::DEBUG, sprintf('Starting secure downloads import for %s', $Configurations->title), array('contao' => new ContaoContext(__METHOD__, 'CRON'))); } // Continue if secure downloads is disabled if (!$Configurations->enabled) { continue; } // Get folder models $Source = FilesModel::findByUuid($Configurations->source); $Target = FilesModel::findByUuid($Configurations->target); // Continue if source or target folder doesn't exist if ($Source === null || !file_exists($projectDir . "/".$Source->path) || $Target === null || !file_exists($projectDir . "/".$Target->path)) { $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'))); continue; } // Read files from source folder $Files = FilesModel::findByPid($Source->uuid); // Continue if folder is empty if ($Files === null) { continue; } // Escape single quotes in fragment regexp $Configurations->regexp = addcslashes($Configurations->regexp,'\''); // Iterate files while ($Files->next()) { // Skip subfolders and special files if ($Files->type == 'folder' || strpos($Files->name,'.') === 0) { continue; } // Continue if file doesn't exist if (!file_exists($projectDir . "/".$Files->path)) { continue; } $objFile = new File($Files->path); // Extract member identifier from filename using regex if (!preg_match('/'.$Configurations->regexp.'/U',$objFile->filename,$fragments)) { $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'))); continue; } // Find member based on extracted fragments $arrColumns = array(); $arrValues = array(); foreach (StringUtil::deserialize($Configurations->fields, true) as $i => $field) { $arrColumns[] = "$field = ?"; $arrValues[] = $fragments[$i+1]; } $objMember = MemberModel::findOneBy($arrColumns,$arrValues); // Continue if no member found if ($objMember === null) { if (System::getContainer()->getParameter('kernel.debug')) { $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'))); } continue; } // Remove member identifier fragments from filename $baseFilename = preg_replace('/'.$Configurations->regexp.'/U','',$objFile->filename); $sanitizedFilename = $baseFilename.'.'.$objFile->extension; // Create organized folder structure (groups of 100 members per folder) $strSubFolder = sprintf("%03d",ceil(($objMember->id+1)/100)-1).'/'.$objMember->id; $strTargetFolder = $Target->path.'/'.$strSubFolder; $arrFolders = explode('/',$strTargetFolder); $strStartPath = ''; foreach ($arrFolders as $folder) { if (!is_dir($projectDir . '/' . $strStartPath . $folder)) { Files::getInstance()->mkdir($strStartPath . $folder); } $strStartPath .= $folder . '/'; } // Do not overwrite existing files $copyId = 1; while(file_exists($projectDir . "/".$strTargetFolder.'/'.$sanitizedFilename)) { $sanitizedFilename = $baseFilename.'_'.$copyId++.'.'.$objFile->extension; } // Try to copy file if (!Files::getInstance()->copy($objFile->path,$strTargetFolder.'/'.$sanitizedFilename)) { $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'))); continue; } $File = Dbafs::addResource($strTargetFolder.'/'.$sanitizedFilename,false); // Register file in database for member $arrData = array( 'pid' => $objMember->id, 'uuid' => ($File !== null ? $File->uuid : ''), 'tstamp' => time(), 'ctime' => time(), ); $this->db->insert('tl_member_secureDownloads',$arrData); // Delete source file after successful import if (!$objFile->delete()) { $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'))); } $intFileCount++; } } if ($intFileCount) { $this->logger->log(LogLevel::INFO, sprintf("Imported %s files for secure member downloads",$intFileCount), array('contao' => new ContaoContext(__METHOD__, 'CRON'))); } } }