private function buildFileToSave($file) { if (!isset($file['path'])) { die('File path cannot be null.'); } if (!isset($file['fd_id'])) { die('Folder id cannot be null.'); } $file['path'] = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $file['path']); $fileInfo = getimagesize($file['path']) ? getimagesize($file['path']) : array(); $fileInfo[0] = empty($fileInfo[0]) ? 0 : $fileInfo[0]; $fileInfo[1] = empty($fileInfo[1]) ? 0 : $fileInfo[1]; $fileInfo[2] = empty($fileInfo[1]) ? 0 : $fileInfo[2]; $fileInfo[3] = empty($fileInfo[3]) ? '' : $fileInfo[3]; $fileInfo['bits'] = empty($fileInfo['bits']) ? 0 : $fileInfo['bits']; $fileInfo['channels'] = empty($fileInfo['channels']) ? 0 : $fileInfo['channels']; $fileInfo['mime'] = empty($fileInfo['mime']) ? 0 : $fileInfo['mime']; $fileToSave['path'] = "path='" . mysql_escape_string($file['path']) . "'"; $fileToSave['fd_id'] = "fd_id=" . $file['fd_id']; $fileToSave['name'] = !empty($file['name']) ? "name='" . mysql_escape_string($file['name']) . "'" : "name='" . mysql_escape_string(end(explode(DIRECTORY_SEPARATOR, $file['path']))) . "'"; $fileToSave['ext_name'] = !empty($file['ext_name']) ? "ext_name='" . mysql_escape_string($file['ext_name']) . "'" : "ext_name='" . mysql_escape_string(end(explode('.', end(explode(DIRECTORY_SEPARATOR, $file['path']))))) . "'"; $fileToSave['sys_created'] = !empty($file['sys_created']) ? "sys_created='" . mysql_escape_string($file['sys_created']) . "'" : "sys_created='" . my_filectime($file['path']) . "'"; $fileToSave['last_modified'] = !empty($file['last_modified']) ? "last_modified='" . mysql_escape_string($file['last_modified']) . "'" : "last_modified='" . my_filemtime($file['path']) . "'"; $fileToSave['page_title'] = !empty($file['page_title']) ? "page_title='" . mysql_escape_string($file['page_title']) . "'" : "page_title='" . mysql_escape_string(end(explode(DIRECTORY_SEPARATOR, $file['path']))) . "'"; $fileToSave['page_meta'] = !empty($file['page_meta']) ? "page_meta='" . mysql_escape_string($file['page_meta']) . "'" : "page_meta=''"; $fileToSave['file_size'] = !empty($file['file_size']) ? "file_size=" . $file['file_size'] : "file_size=" . filesize($file['path']); $fileToSave['width'] = !empty($file['width']) ? "width=" . $file['width'] : "width=" . $fileInfo[0]; $fileToSave['height'] = !empty($file['height']) ? "height=" . $file['height'] : "height=" . $fileInfo[1]; $fileToSave['image_type'] = !empty($file['image_type']) ? "image_type=" . $file['image_type'] : "image_type=" . $fileInfo[2]; $fileToSave['bits'] = !empty($file['bits']) ? "bits=" . $file['bits'] : "bits=" . $fileInfo['bits']; $fileToSave['channels'] = !empty($file['channels']) ? "channels=" . $file['channels'] : "channels=" . $fileInfo['channels']; $fileToSave['size_string'] = !empty($file['size_string']) ? "size_string='" . mysql_escape_string($file['size_string']) . "'" : "size_string='" . mysql_escape_string($fileInfo[3]) . "'"; $fileToSave['mime'] = !empty($file['mime']) ? "mime='" . mysql_escape_string($file['mime']) . "'" : "mime='" . $fileInfo['mime'] . "'"; return $fileToSave; }
private function buildFolderToSave($folder) { if (!isset($folder['path'])) { die('Folder name cannot be null.'); } if (!isset($folder['parent_id'])) { die('Folder parent id cannot be null.'); } $folderToSave['path'] = "path='" . mysql_escape_string($folder['path']) . "'"; $folderToSave['parent_id'] = "parent_id=" . $folder['parent_id']; $folderToSave['name'] = isset($folder['name']) && !empty($folder['name']) ? "name='" . mysql_escape_string($folder['name']) . "'" : "name='" . mysql_escape_string(end(explode(DIRECTORY_SEPARATOR, $folder['path']))) . "'"; $folderToSave['sys_created'] = isset($folder['sys_created']) && !empty($folder['sys_created']) ? "sys_created='" . mysql_escape_string($folder['sys_created']) . "'" : "sys_created='" . my_filectime($folder['path']) . "'"; $folderToSave['last_modified'] = isset($folder['last_modified']) && !empty($folder['last_modified']) ? "last_modified='" . mysql_escape_string($folder['last_modified']) . "'" : "last_modified='" . my_filemtime($folder['path']) . "'"; $folderToSave['page_title'] = isset($folder['page_title']) && !empty($folder['page_title']) ? "page_title='" . mysql_escape_string($folder['page_title']) . "'" : "page_title='" . mysql_escape_string(end(explode(DIRECTORY_SEPARATOR, $folder['path']))) . "'"; $folderToSave['page_meta'] = isset($folder['page_meta']) && !empty($folder['page_meta']) ? "page_meta='" . mysql_escape_string($folder['page_meta']) . "'" : "page_meta=''"; $folderToSave['locked'] = isset($folder['locked']) && !empty($folder['locked']) ? "locked=" . $folder['locked'] : "locked=0"; return $folderToSave; }
public static function syncFolders($folderToSync) { $debug = self::$debug; $mode = self::$mode; import('dao.Folder'); $folder = new Folder(); import('dao.File'); $file = new File(); $folderFullName = $folderToSync['path']; $folderId = $folderToSync['fd_id']; // If folder is locked, ignore. Unless set mode greater than 0. if ($folderToSync['locked'] && $mode == 0) { if ($debug) { ZDebug::my_echo('Ignore locked folder: ' . $folderFullName . '(' . $folderId . ')'); } return TRUE; } // Step 1: If folder is not physically exist, set to deleted in DB. if (!is_dir($folderFullName)) { $folder->deleteFolder($folderId); if ($debug) { ZDebug::my_echo('Delete folder in DB: ' . $folderFullName . '(' . $folderId . ')'); } return TRUE; } // Step 2: Get the result set of files under this folder $filesInFolder = $folder->getFilesInFolder($folderId); $fileNameArr = array(); foreach ($filesInFolder as $theFile) { // Step 3: If a file is not physically exist, delete it in table files. if (!file_exists($theFile['path'])) { $file->deleteFile($theFile['fid']); if ($debug) { ZDebug::my_echo('Delete file in DB: ' . $theFile['path'] . '(' . $theFile['fid'] . ')'); } } elseif ($theFile['last_modified'] != my_filemtime($theFile['path'])) { $theFile['last_modified'] = my_filemtime($theFile['path']); $file->saveFile($theFile); if ($debug) { ZDebug::my_echo('Update file in DB: ' . $theFile['path'] . '(' . $theFile['fid'] . ')'); } } elseif ($mode > 1) { $file->saveFile($theFile); if ($debug) { ZDebug::my_echo('Update file in DB: ' . $theFile['path'] . '(' . $theFile['fid'] . ')'); } } else { } $fileNameArr[] = $theFile['name']; } // Step 5: Get result set of sub-folders under this folder $subFoldera = $folder->getSubFolder($folderId); $folderNameArr = array(); foreach ($subFoldera as $theFolder) { // Step 6: If a folder is not physically exist, set deleted flag in table folders. if (!file_exists($theFolder['path'])) { $folder->deleteFolder($theFolder['fd_id']); if ($debug) { ZDebug::my_echo('Delete folder in DB: ' . $theFolder['path'] . '(' . $theFolder['fd_id'] . ')'); } } elseif ($theFolder['last_modified'] != my_filemtime($theFolder['path'])) { $theFolder['last_modified'] = my_filemtime($theFolder['path']); $folder->saveFolder($theFolder); if ($debug) { ZDebug::my_echo('Update folder in DB: ' . $theFolder['path'] . '(' . $theFolder['fd_id'] . ')'); } self::syncFolders($theFolder); } else { self::syncFolders($theFolder); } $folderNameArr[] = $theFolder['name']; } $hdl = opendir($folderFullName); while ($item = readdir($hdl)) { $itemFullName = $folderFullName . DIRECTORY_SEPARATOR . $item; // Step 8: If physical file is not in DB file result set, then add a file if ($item != "." && $item != ".." && is_file($itemFullName) && !in_array($item, $fileNameArr)) { $file->saveFile(array('path' => $itemFullName, 'fd_id' => $folderId)); if ($debug > 1) { ZDebug::my_echo('Adding new file in DB: ' . $itemFullName); } } // Step 9: if physical folder is not in DB folder result set, then add a folder if ($item != "." && $item != ".." && is_dir($itemFullName) && !in_array($item, $folderNameArr)) { self::addFolder($itemFullName, $folderId); } } closedir($hdl); }