$basedir = $serendipity['serendipityPath'] . $serendipity['uploadPath'];
$images = array();
if ($dir = @opendir($basedir . $odir)) {
- while(false !== ($f = readdir($dir))) {
+ $aTempArray = array();
+ while (($file = @readdir($dir)) !== false) {
+ array_push($aTempArray, $file);
+ }
+ @closedir($dir);
+ sort($aTempArray);
+ foreach($aTempArray as $f) {
if ($f != '.' && $f != '..' && strpos($f, $serendipity['thumbSuffix']) === false) {
$cdir = ($odir != '' ? $odir . '/' : '');
if (is_dir($basedir . $odir . '/' . $f)) {
function serendipity_insertImageInDatabase($filename, $directory, $authorid = 0, $time = NULL) {
global $serendipity;
- if ( is_null($time) ) {
+ if (is_null($time)) {
$time = time();
}
$perPage = (!empty($serendipity['GET']['sortorder']['perpage']) ? $serendipity['GET']['sortorder']['perpage'] : $sort_row_interval[0]);
$start = ($page-1) * $perPage;
+ ## SYNCH START ##
+ $aExclude = array("CVS" => true, ".svn" => true);
+ serendipity_plugin_api::hook_event('backend_media_path_exclude_directories', $aExclude);
+ $paths = array();
+ $aFilesOnDisk = array();
+
+ $aResultSet = serendipity_traversePath(
+ $serendipity['serendipityPath'] . $serendipity['uploadPath']. $limit_path,
+ '',
+ false,
+ NULL,
+ 1,
+ NULL,
+ FALSE,
+ $aExclude
+ );
+
+ foreach ($aResultSet AS $sKey => $sFile) {
+ if ($sFile['directory']) {
+ echo "{$sFile['relpath']} is a directory.<br />"; #DEBUG
+ array_push($paths, $sFile);
+ } else {
+ echo "{$sFile['relpath']} is a file.<br />"; #DEBUG
+ // Store the file in our array, remove any ending slashes
+ $aFilesOnDisk[$sFile['relpath']] = 1;
+ }
+ unset($aResultSet[$sKey]);
+ }
+ sort($paths);
+ echo "<p>Got files: <pre>" . print_r($aFilesOnDisk, true) . "</pre></p>"; #DEBUG
+ $serendipity['current_image_hash'] = md5(serialize($aFilesOnDisk));
+ $serendipity['current_image_hash'] = md5(time()); #DEBUGGING
+
+ $nTimeStart = microtime_float();
+ // MTG 21/01/06: request all images from the database, delete any which don't exist
+ // on the filesystem, and mark off files from the file list which are already
+ // in the database
+
+ $nCount = 0;
+ if (serendipity_checkPermission('adminImagesSync') && $serendipity['current_image_hash'] != $serendipity['last_image_hash']) {
+ $aResultSet = serendipity_db_query("SELECT path, name, extension, thumbnail_name, id
+ FROM {$serendipity['dbPrefix']}images", false, 'assoc');
+ echo "<p>Got images: <pre>" . print_r($aResultSet, true) . "</pre></p>"; #DEBUG
+ if (is_array($aResultSet)) {
+ foreach ($aResultSet AS $sKey => $sFile) {
+ serendipity_plugin_api::hook_event('backend_thumbnail_filename_select', $sFile);
+ $sThumbNailFile = '';
+ if (isset($sFile['thumbnail_filename'])) {
+ $sThumbNailFile = $sFile['thumbnail_filename'];
+ } else {
+ $sThumbNailFile = $sFile['path'] . $sFile['name'] . '.' . $sFile['thumbnail_name'] . '.' . $sFile['extension'];
+ }
+
+ $sFileName = $sFile['path'] . $sFile['name'] . '.' . $sFile['extension'];
+ echo "<p>File name is $sFileName,<br />thumbnail is $sThumbNailFile</p>"; #DEBUG
+ unset($aResultSet[$sKey]);
+
+ if (isset($aFilesOnDisk[$sFileName])){
+ unset($aFilesOnDisk[$sFileName]);
+ } else {
+ echo "Deleting Image {$sFile['id']}<br />\n"; #DEBUG
+ #serendipity_deleteImage($sFile['id']); #DEBUGGING
+ ++$nCount;
+ }
+
+ unset($aFilesOnDisk[$sThumbNailFile]);
+ }
+ }
+
+ if ($nCount > 0){
+ echo "<p>Cleaned up ".$nCount." database entries</p>"; #DEBUG
+ }
+
+ serendipity_set_config_var('last_image_hash', $serendipity['current_image_hash'], 0);
+ $aUnmatchedOnDisk = array_keys($aFilesOnDisk);
+ echo "<p>Got unmatched files: <pre>" . print_r($aUnmatchedOnDisk, true) . "</pre></p>"; #DEBUG
+ $nCount = 0;
+ foreach ($aUnmatchedOnDisk AS $sFile) {
+ if (preg_match('@\.' . $serendipity['thumbSuffix'] . '\.@', $sFile)) {
+ echo "<p>Skipping thumbnailed file $sFile</p>"; #DEBUG
+ continue;
+ } else {
+ echo "<p>Checking $sFile</p>"; #DEBUG
+ }
+
+ // MTG: 21/01/06: put files which have just 'turned up' into the database
+ $aImageData = serendipity_getImageData($sFile);
+ if (serendipity_isImage($aImageData)) {
+ $nPos = strrpos($sFile, "/");
+ if (is_bool($nPos) && !$nPos) {
+ $sFileName = $sFile;
+ $sDirectory = "";
+ } else {
+ ++$nPos;
+ $sFileName = substr($sFile, $nPos);
+ $sDirectory = substr($sFile, 0, $nPos);
+ }
+ echo "<p>Inserting image $sFileName from $sDirectory <pre>" . print_r($aImageData, true) . "</pre> into database</p>"; #DEBUG
+ # TODO: Check if the thumbnail generation goes fine with Marty's code
+ serendipity_makeThumbnail($sFileName, $sDirectory);
+ serendipity_insertImageInDatabase($sFileName, $sDirectory);
+ ++$nCount;
+ }
+ }
+
+ if ($nCount > 0) {
+ echo "<p>Inserted ".$nCount." images into the database</p>"; #DEBUG
+ }
+ } else {
+ echo "<p>Media Gallery database is up to date</p>"; #DEBUG
+ }
+
+ /*
+ $nTimeEnd = microtime_float ( );
+ $nDifference = $nTimeEnd - $nTimeStart;
+ echo "<p> total time taken was " . $nDifference . "</p>";
+ */
+ ## SYNCH FINISHED ##
+
+ ## Aply ACL afterwards:
+ serendipity_directoryACL($paths, 'read');
+
$serendipity['imageList'] = serendipity_fetchImagesFromDatabase(
$start,
$perPage,
$linkPrevious = '?' . $extraParems . 'serendipity[page]=' . ($page-1);
$linkNext = '?' . $extraParems . 'serendipity[page]=' . ($page+1);
$sort_order = serendipity_getImageFields();
- $paths = serendipity_traversePath(
- $serendipity['serendipityPath'] . $serendipity['uploadPath']. $limit_path,
- '',
- true,
- NULL,
- 1,
- NULL,
- 'read'
- );
-
if (is_null($lineBreak)) {
$lineBreak = floor(750 / ($serendipity['thumbSize'] + 20));
}
</tr>
</table>
</form>
-<?php if ( sizeof($serendipity['imageList']) == 0 ) { ?>
+<?php if (sizeof($serendipity['imageList']) == 0) { ?>
<div align="center">- <?php echo NO_IMAGES_FOUND ?> -</div>
<?php } else { ?>
<table border="0" width="100%">
<table width="100%">
<tr>
<td>
- <?php if ( $page != 1 && $page <= $pages ) { ?>
+ <?php if ($page != 1 && $page <= $pages) { ?>
<a href="<?php echo $linkPrevious ?>" class="serendipityIconLink"><img src="<?php echo serendipity_getTemplateFile('admin/img/previous.png') ?>" /><?php echo PREVIOUS ?></a>
<?php } ?></td>
<td align="right">
- <?php if ($page != $pages ) { ?>
+ <?php if ($page != $pages) { ?>
<a href="<?php echo $linkNext ?>" class="serendipityIconLinkRight"><?php echo NEXT ?><img src="<?php echo serendipity_getTemplateFile('admin/img/next.png') ?>" /></a>
<?php } ?></td>
</tr>
$x = 0;
foreach ($serendipity['imageList'] as $k => $file) {
++$x; $preview = '';
- $img = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file['path'] . $file['name'] . (!empty($file['thumbnail_name']) ? '.' . $file['thumbnail_name'] : '') . '.' . $file['extension'];
- $i = @getimagesize($img);
- $file['imgsrc'] = $serendipity['uploadHTTPPath'] . $file['path'] . $file['name'] . (!empty($file['thumbnail_name']) ? '.' . $file['thumbnail_name'] : '') . '.' . $file['extension'];
+ $sThumbSource = serendipity_getThumbNailPath($file['path'], $file['name'], $file['extension'], $file['thumbnail_name']);
+ $img = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $sThumbSource;
+ if (!isset($file['imgsrc'])) {
+ $file['imgsrc'] = $serendipity['uploadHTTPPath'] . $file['path'] . $file['name'] . (!empty($file['thumbnail_name']) ? '.' . $file['thumbnail_name'] : '') . '.' . $file['extension'];
+ }
+ $i = @getimagesize($img);
$is_image = serendipity_isImage($file);
if (!($serendipity['authorid'] == $file['authorid'] || $file['authorid'] == '0' || serendipity_checkPermission('adminImagesViewOthers'))) {
/* If it is an image, and the thumbnail exists */
if ($is_image && file_exists($img)) {
- $preview .= '<img src="' . $serendipity['serendipityHTTPPath'] . $file['imgsrc'] . '" border="0" title="' . $file['path'] . $file['name'] . '" alt="'. $file['name'] . '" />';
+ $preview .= '<img src="' . $serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $sThumbSource . '" border="0" title="' . $file['path'] . $file['name'] . '" alt="'. $file['name'] . '" />';
if ($url) {
$preview = '<a href="'. $url .'&serendipity[image]='. $file['id'] .'">'. $preview .'</a>';
}
* @param int Level of nesting (recursive use)
* @param int The maximum level of nesting (recursive use)
* @param mixed Toggle whether to apply serendipity_directoryACL (false / 'read' / 'write')
+ * @param array An array of directories to skip [passed by plugins, for example]
* @return array Array of files/directories
*/
-function serendipity_traversePath($basedir, $dir='', $onlyDirs = true, $pattern = NULL, $depth = 1, $max_depth = null, $apply_ACL = false) {
+function serendipity_traversePath($basedir, $dir='', $onlyDirs = true, $pattern = NULL, $depth = 1, $max_depth = NULL, $apply_ACL = false, $aExcludeDirs = NULL) {
$odir = serendipity_dirSlash('end', $basedir) . serendipity_dirSlash('end', $dir);
$dh = @opendir($odir);
$files = array();
while (($file = @readdir($dh)) !== false) {
- if ( $file != '.' && $file != '..' ) {
- if ( $onlyDirs === false || ($onlyDirs === true && is_dir($odir . $file)) ) {
- if ( is_null($pattern) || preg_match($pattern, $file) ) {
+ if ($file != '.' && $file != '..') {
+ $bPatternMatch = (is_null($pattern) || preg_match($pattern, $file));
+ $sFullPath = $odir . $file;
+ $bIsDir = is_dir($sFullPath);
+ if ($onlyDirs === false || $bIsDir) {
+ if ($bPatternMatch &&
+ (!$bIsDir || $aExcludeDirs == null || !isset($aExcludeDirs[$file]))) {
$files[] = array(
- 'name' => $file,
- 'depth' => $depth,
- 'relpath' => ltrim(str_replace('\\', '/', serendipity_dirSlash('end', $dir)) . basename($file) . '/', '/')
+ 'name' => $file,
+ 'depth' => $depth,
+ 'relpath' => ltrim(str_replace('\\', '/', serendipity_dirSlash('end', $dir)) . basename($file) . ($bIsDir ? '/' : ''), '/'),
+ 'directory' => $bIsDir
);
}
}
- if (is_dir($odir . $file) && ($max_depth === null || $depth < $max_depth)) {
+ if ($bIsDir &&
+ ($max_depth === null || $depth < $max_depth) &&
+ ($aExcludeDirs == null || !isset($aExcludeDirs[$file]))) {
$next_dir = serendipity_dirSlash('end', $dir) . basename($file);
- $files = array_merge($files, serendipity_traversePath($basedir, $next_dir, $onlyDirs, $pattern, ($depth+1), $max_depth));
+ $files = array_merge($files, serendipity_traversePath($basedir, $next_dir, $onlyDirs, $pattern, ($depth+1), $max_depth, $apply_ACL, $aExcludeDirs));
}
}
}
}
return true;
-}
\ No newline at end of file
+}
+
+ /**
+ * Build the name of a thumbnail image file.
+ *
+ * @author MTG
+ * @param string Relative Path
+ * @param string File name
+ * @param string File extension
+ * @param string Thumbnail suffix
+ * @return array Thumbnail path
+ *
+ */
+function serendipity_getThumbNailPath($sRelativePath, $sName, $sExtension, $sThumbName) {
+ $aTempArray = array('path' => $sRelativePath,
+ 'name' => $sName,
+ 'extension' => $sExtension);
+ serendipity_plugin_api::hook_event('backend_thumbnail_filename_select', $aTempArray);
+
+ if (isset($aTempArray['thumbnail_filename'])) {
+ $sThumbNailPath = $aTempArray['thumbnail_filename'];
+ } else {
+ $sThumbNailPath = $sRelativePath . $sName . (!empty($sThumbName) ? '.' . $sThumbName : '') . '.' . $sExtension;
+ }
+
+ return $sThumbNailPath;
+}
+
+ /**
+ * Given a relative path to an image, construct an array containing all
+ * relevant information about that image in the file structure.
+ *
+ * @author MTG
+ * @param string Relative Path
+ * @return array Data about image
+ *
+ */
+function serendipity_getImageData($sRelativePath) {
+ global $serendipity;
+
+ // First, peel off the file name from the path
+ $nPos = strrpos($sRelativePath, '/');
+ if (is_bool($nPos) && !$nPos) {
+ $sFileName = $sRelativePath;
+ $sDirectory = '';
+ } else {
+ $nLastSlashPos = 1 + $nPos;
+ $sFileName = substr($sRelativePath, $nLastSlashPos);
+ $sDirectory = substr($sRelativePath, 0, $nLastSlashPos);
+ }
+
+ list($sName, $sExtension) = serendipity_parseFileName($sFileName);
+
+ $sImagePath = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $sRelativePath;
+
+ $aSizeData = @serendipity_getimagesize($sImagePath , '', $sExtension);
+ $nWidth = $aSizeData[0];
+ $nHeight = $aSizeData[1];
+ $sMime = $aSizeData['mime'];
+ $nFileSize = @filesize($sImagePath);
+
+ $array = array(
+ 'name' => $sName,
+ 'extension' => $sExtension,
+ 'mime' => $sMime,
+ 'size' => $nFileSize,
+ 'dimensions_width' => $nWidth,
+ 'dimensions_height' => $nHeight,
+ 'path' => $sDirectory,
+ 'authorid' => 0,
+ 'hotlink' => 0,
+ 'id' => $sRelativePath
+ );
+
+ return $array;
+}
+