public static function findMultiplePublishedMultiSRCContentElements($arrIds, $arrExtensions, array $arrOptions = array())
 {
     if (!is_array($arrIds) || empty($arrIds) || !is_array($arrExtensions) || empty($arrExtensions)) {
         return null;
     }
     foreach ($arrExtensions as $k => $v) {
         if (!preg_match('/^[a-z0-9]{2,5}$/i', $v)) {
             unset($arrExtensions[$k]);
         }
     }
     $t = static::$strTable;
     $objDatabase = \Database::getInstance();
     // get names of parent tables (tl_news, tl_article, tl_calendar_events)
     $objTable = $objDatabase->prepare("SELECT DISTINCT ptable FROM tl_content")->execute();
     if ($objTable->numRows < 1) {
         return null;
     }
     $arrReturn = null;
     while ($objTable->next()) {
         $strQuery = "\n                    SELECT c.id AS cid, c.ptable as ptable, c.pid as parent, c.multiSRC\n                    FROM tl_content c\n                    LEFT JOIN {$objTable->ptable} p ON p.id = c.pid\n                    WHERE c.multiSRC IS NOT NULL AND c.invisible = ''";
         $objStatement = $objDatabase->prepare($strQuery);
         if (\Database::getInstance()->fieldExists('published', $objTable->ptable)) {
             $strQuery . " AND p.published = 1";
         }
         $objResult = $objStatement->execute();
         if ($objResult->numRows < 1) {
             return null;
         }
         while ($objResult->next()) {
             $arrUuids = deserialize($objResult->multiSRC, true);
             $objFiles = \FilesModel::findMultipleByUuids($arrUuids);
             if ($objFiles === null) {
                 continue;
             }
             if (!$objFiles->copyright) {
                 continue;
             }
             if ($objFiles->type == 'folder') {
                 $objSubfiles = \FilesModel::findByPid($objFiles->uuid);
                 if ($objSubfiles === null) {
                     continue;
                 }
                 while ($objSubfiles->next()) {
                     // Skip subfolders
                     if ($objSubfiles->type == 'folder') {
                         $objFolderFiles = \FilesModel::findMultipleFilesByFolder($objSubfiles->path);
                         if ($objFolderFiles === null) {
                             continue;
                         }
                         while ($objFolderFiles->next()) {
                             if (!in_array($objFolderFiles->extension, $arrExtensions)) {
                                 continue;
                             }
                             if (!$objFolderFiles->copyright) {
                                 continue;
                             }
                             $arrReturn[] = (object) array_merge($objResult->row(), $objFolderFiles->row());
                         }
                     }
                     if (!in_array($objSubfiles->extension, $arrExtensions)) {
                         continue;
                     }
                     if (!$objSubfiles->copyright) {
                         continue;
                     }
                     $arrReturn[] = (object) array_merge($objResult->row(), $objSubfiles->row());
                 }
             } else {
                 $arrReturn[] = (object) array_merge($objResult->row(), $objFiles->row());
             }
         }
     }
     return empty($arrReturn) ? null : $arrReturn;
 }
Exemplo n.º 2
0
 /**
  * Synchronize the file system with the database
  *
  * @return string The path to the synchronization log file
  *
  * @throws \Exception If a parent ID entry is missing
  */
 public static function syncFiles()
 {
     // Try to raise the limits (see #7035)
     @ini_set('memory_limit', -1);
     @ini_set('max_execution_time', 0);
     $objDatabase = \Database::getInstance();
     // Lock the files table
     $objDatabase->lockTables(array('tl_files'));
     // Reset the "found" flag
     $objDatabase->query("UPDATE tl_files SET found=''");
     // Get a filtered list of all files
     $objFiles = new \RecursiveIteratorIterator(new \Dbafs\Filter(new \RecursiveDirectoryIterator(TL_ROOT . '/' . \Config::get('uploadPath'), \FilesystemIterator::UNIX_PATHS | \FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::SKIP_DOTS)), \RecursiveIteratorIterator::SELF_FIRST);
     $strLog = 'system/tmp/' . md5(uniqid(mt_rand(), true));
     // Open the log file
     $objLog = new \File($strLog, true);
     $objLog->truncate();
     $arrModels = array();
     // Create or update the database entries
     foreach ($objFiles as $objFile) {
         $strRelpath = str_replace(TL_ROOT . '/', '', $objFile->getPathname());
         // Get all subfiles in a single query
         if ($objFile->isDir()) {
             $objSubfiles = \FilesModel::findMultipleFilesByFolder($strRelpath);
             if ($objSubfiles !== null) {
                 while ($objSubfiles->next()) {
                     $arrModels[$objSubfiles->path] = $objSubfiles->current();
                 }
             }
         }
         // Get the model
         if (isset($arrModels[$strRelpath])) {
             $objModel = $arrModels[$strRelpath];
         } else {
             $objModel = \FilesModel::findByPath($strRelpath);
         }
         if ($objModel === null) {
             // Add a log entry
             $objLog->append("[Added] {$strRelpath}");
             // Get the parent folder
             $strParent = dirname($strRelpath);
             // Get the parent ID
             if ($strParent == \Config::get('uploadPath')) {
                 $strPid = null;
             } else {
                 $objParent = \FilesModel::findByPath($strParent);
                 if ($objParent === null) {
                     throw new \Exception("No parent entry for {$strParent}");
                 }
                 $strPid = $objParent->uuid;
             }
             // Create the file or folder
             if (is_file(TL_ROOT . '/' . $strRelpath)) {
                 $objFile = new \File($strRelpath, true);
                 $objModel = new \FilesModel();
                 $objModel->pid = $strPid;
                 $objModel->tstamp = time();
                 $objModel->name = $objFile->name;
                 $objModel->type = 'file';
                 $objModel->path = $objFile->path;
                 $objModel->extension = $objFile->extension;
                 $objModel->found = 2;
                 $objModel->hash = $objFile->hash;
                 $objModel->uuid = $objDatabase->getUuid();
                 $objModel->save();
             } else {
                 $objFolder = new \Folder($strRelpath);
                 $objModel = new \FilesModel();
                 $objModel->pid = $strPid;
                 $objModel->tstamp = time();
                 $objModel->name = $objFolder->name;
                 $objModel->type = 'folder';
                 $objModel->path = $objFolder->path;
                 $objModel->extension = '';
                 $objModel->found = 2;
                 $objModel->hash = $objFolder->hash;
                 $objModel->uuid = $objDatabase->getUuid();
                 $objModel->save();
             }
         } else {
             // Check whether the MD5 hash has changed
             $objResource = $objFile->isDir() ? new \Folder($strRelpath) : new \File($strRelpath);
             $strType = $objModel->hash != $objResource->hash ? 'Changed' : 'Unchanged';
             // Add a log entry
             $objLog->append("[{$strType}] {$strRelpath}");
             // Update the record
             $objModel->found = 1;
             $objModel->hash = $objResource->hash;
             $objModel->save();
         }
     }
     // Check for left-over entries in the DB
     $objFiles = \FilesModel::findByFound('');
     if ($objFiles !== null) {
         $arrMapped = array();
         $arrPidUpdate = array();
         while ($objFiles->next()) {
             $objFound = \FilesModel::findBy(array('hash=?', 'found=2'), $objFiles->hash);
             if ($objFound !== null) {
                 // Check for matching file names if the result is ambiguous (see #5644)
                 if ($objFound->count() > 1) {
                     while ($objFound->next()) {
                         if ($objFound->name == $objFiles->name) {
                             $objFound = $objFound->current();
                             break;
                         }
                     }
                 }
                 // If another file has been mapped already, delete the entry (see #6008)
                 if (in_array($objFound->path, $arrMapped)) {
                     $objLog->append("[Deleted] {$objFiles->path}");
                     $objFiles->delete();
                     continue;
                 }
                 $arrMapped[] = $objFound->path;
                 // Store the PID change
                 if ($objFiles->type == 'folder') {
                     $arrPidUpdate[$objFound->uuid] = $objFiles->uuid;
                 }
                 // Add a log entry BEFORE changing the object
                 $objLog->append("[Moved] {$objFiles->path} to {$objFound->path}");
                 // Update the original entry
                 $objFiles->pid = $objFound->pid;
                 $objFiles->tstamp = $objFound->tstamp;
                 $objFiles->name = $objFound->name;
                 $objFiles->type = $objFound->type;
                 $objFiles->path = $objFound->path;
                 $objFiles->found = 1;
                 // Delete the newer (duplicate) entry
                 $objFound->delete();
                 // Then save the modified original entry (prevents duplicate key errors)
                 $objFiles->save();
             } else {
                 // Add a log entry BEFORE changing the object
                 $objLog->append("[Deleted] {$objFiles->path}");
                 // Delete the entry if the resource has gone
                 $objFiles->delete();
             }
         }
         // Update the PID of the child records
         if (!empty($arrPidUpdate)) {
             foreach ($arrPidUpdate as $from => $to) {
                 $objChildren = \FilesModel::findByPid($from);
                 if ($objChildren !== null) {
                     while ($objChildren->next()) {
                         $objChildren->pid = $to;
                         $objChildren->save();
                     }
                 }
             }
         }
     }
     // Close the log file
     $objLog->close();
     // Reset the found flag
     $objDatabase->query("UPDATE tl_files SET found=1 WHERE found=2");
     // Unlock the tables
     $objDatabase->unlockTables();
     // Return the path to the log file
     return $strLog;
 }
Exemplo n.º 3
0
 /**
  * @param integer
  * @param string
  * Bilder aus Verzeichnis auf dem Server in Album einlesen
  */
 public static function importFromFilesystem($intAlbumId, $strMultiSRC)
 {
     $images = array();
     $objFilesModel = \FilesModel::findMultipleByUuids(explode(',', $strMultiSRC));
     if ($objFilesModel === null) {
         return;
     }
     while ($objFilesModel->next()) {
         // Continue if the file has been processed or does not exist
         if (isset($images[$objFilesModel->path]) || !file_exists(TL_ROOT . '/' . $objFilesModel->path)) {
             continue;
         }
         // If item is a file, then store it in the array
         if ($objFilesModel->type == 'file') {
             $objFile = new \File($objFilesModel->path);
             if ($objFile->isGdImage) {
                 $images[$objFile->path] = array('uuid' => $objFilesModel->uuid, 'basename' => $objFile->basename, 'path' => $objFile->path);
             }
         } else {
             // If it is a directory, then store its files in the array
             $objSubfilesModel = \FilesModel::findMultipleFilesByFolder($objFilesModel->path);
             if ($objSubfilesModel === null) {
                 continue;
             }
             while ($objSubfilesModel->next()) {
                 // Skip subfolders
                 if ($objSubfilesModel->type == 'folder' || !is_file(TL_ROOT . '/' . $objSubfilesModel->path)) {
                     continue;
                 }
                 $objFile = new \File($objSubfilesModel->path);
                 if ($objFile->isGdImage) {
                     $images[$objFile->path] = array('uuid' => $objSubfilesModel->uuid, 'basename' => $objFile->basename, 'path' => $objFile->path);
                 }
             }
         }
     }
     if (count($images)) {
         $uploadPath = GALLERY_CREATOR_UPLOAD_PATH;
         $objPictures = \Database::getInstance()->prepare('SELECT * FROM tl_gallery_creator_pictures WHERE pid=?')->execute($intAlbumId);
         $arrPictures['uuid'] = $objPictures->fetchEach('uuid');
         $arrPictures['path'] = $objPictures->fetchEach('path');
         foreach ($arrPictures['path'] as $path) {
             $arrPictures['basename'][] = basename($path);
         }
         $objAlb = \MCupic\GalleryCreatorAlbumsModel::findById($intAlbumId);
         foreach ($images as $image) {
             // Prevent duplicate entries
             if (in_array($image['uuid'], $arrPictures['uuid'])) {
                 continue;
             }
             // Prevent duplicate entries
             if (in_array($image['basename'], $arrPictures['basename'])) {
                 continue;
             }
             \Input::setGet('importFromFilesystem', 'true');
             if ($GLOBALS['TL_CONFIG']['gc_album_import_copy_files']) {
                 $strSource = $image['path'];
                 // Get the album upload directory
                 $objFolderModel = \FilesModel::findByUuid($objAlb->assignedDir);
                 $errMsg = 'Aborted import process, because there is no upload folder assigned to the album with ID ' . $objAlb->id . '.';
                 if ($objFolderModel === null) {
                     die($errMsg);
                 }
                 if ($objFolderModel->type != 'folder') {
                     die($errMsg);
                 }
                 if (!is_dir(TL_ROOT . '/' . $objFolderModel->path)) {
                     die($errMsg);
                 }
                 $strDestination = self::generateUniqueFilename($objFolderModel->path . '/' . basename($strSource));
                 if (is_file(TL_ROOT . '/' . $strSource)) {
                     //copy Image to the upload folder
                     $objFile = new \File($strSource);
                     $objFile->copyTo($strDestination);
                     \Dbafs::addResource($strSource);
                 }
                 self::createNewImage($objAlb->id, $strDestination);
             } else {
                 self::createNewImage($objAlb->id, $image['path']);
             }
         }
     }
 }