/** * Moves a media directory * * @param string The old directory * @param string The new directory * @param string The type of what to remove (dir|file|filedir) * @param string An item id of a file * @param array Result of serendipity_fetchImageFromDatabase($id) * @return boolean * */ function serendipity_moveMediaDirectory($oldDir, $newDir, $type = 'dir', $item_id = null, $file = null) { global $serendipity; $real_oldDir = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $oldDir; $real_newDir = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $newDir; if ($type == 'dir') { if (!is_dir($real_oldDir)) { echo '<span class="msg_error"><span class="icon-attention-circled"></span> '; printf(ERROR_FILE_NOT_EXISTS, $oldDir); echo "</span>\n"; return false; } if (is_dir($real_newDir)) { echo '<span class="msg_error"><span class="icon-attention-circled"></span> '; printf(ERROR_FILE_EXISTS, $newDir); echo "</span>\n"; return false; } if (!rename($real_oldDir, $real_newDir)) { echo '<span class="msg_error"><span class="icon-attention-circled"></span> '; printf(MEDIA_DIRECTORY_MOVE_ERROR, $newDir); echo "</span>\n"; return false; } echo '<span class="msg_success"><span class="icon-ok-circled"></span> '; printf(MEDIA_DIRECTORY_MOVED, $newDir); echo "</span>\n"; $dirs = serendipity_db_query("SELECT id, path\n FROM {$serendipity['dbPrefix']}images\n WHERE path LIKE '" . serendipity_db_escape_string($oldDir) . "%'", false, 'assoc'); if (is_array($dirs)) { foreach ($dirs as $dir) { $old = $dir['path']; $new = preg_replace('@^(' . preg_quote($oldDir) . ')@i', $newDir, $old); serendipity_db_query("UPDATE {$serendipity['dbPrefix']}images\n SET path = '" . serendipity_db_escape_string($new) . "'\n WHERE id = {$dir['id']}"); } } $dirs = serendipity_db_query("SELECT groupid, artifact_id, artifact_type, artifact_mode, artifact_index\n FROM {$serendipity['dbPrefix']}access\n WHERE artifact_type = 'directory'\n AND artifact_index LIKE '" . serendipity_db_escape_string($oldDir) . "%'", false, 'assoc'); if (is_array($dirs)) { foreach ($dirs as $dir) { $old = $dir['artifact_index']; $new = preg_replace('@^(' . preg_quote($oldDir) . ')@i', $newDir, $old); serendipity_db_query("UPDATE {$serendipity['dbPrefix']}access\n SET artifact_index = '" . serendipity_db_escape_string($new) . "'\n WHERE groupid = '" . serendipity_db_escape_string($dir['groupid']) . "'\n AND artifact_id = '" . serendipity_db_escape_string($dir['artifact_id']) . "'\n AND artifact_type = '" . serendipity_db_escape_string($dir['artifact_type']) . "'\n AND artifact_mode = '" . serendipity_db_escape_string($dir['artifact_mode']) . "'\n AND artifact_index = '" . serendipity_db_escape_string($dir['artifact_index']) . "'"); } } } if ($type == 'file') { if (serendipity_isActiveFile(basename($newDir))) { echo '<span class="msg_error"><span class="icon-attention-circled"></span> '; printf(ERROR_FILE_FORBIDDEN, serendipity_specialchars($newDir)); echo "</span>\n"; return false; } if ($file['hotlink']) { serendipity_updateImageInDatabase(array('realname' => $newDir, 'name' => $newDir), $item_id); } else { $file_new = $newDir . $file['name'] . (empty($file['extension']) ? '' : '.'); $file_old = $file['path'] . $file['name'] . (empty($file['extension']) ? '' : '.'); $newfile = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file_new . $file['extension']; $oldfile = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file_old . $file['extension']; if ($newDir != '' && file_exists($oldfile) && !file_exists($newfile)) { $renameValues = array(array('from' => $oldfile, 'to' => $newfile, 'thumb' => $serendipity['thumbSuffix'], 'fthumb' => $file['thumbnail_name'], 'oldDir' => $oldDir, 'newDir' => $newDir, 'type' => $type, 'item_id' => $item_id, 'file' => $file)); serendipity_plugin_api::hook_event('backend_media_rename', $renameValues); // eg. for staticpage entries path regex replacements // Rename file rename($renameValues[0]['from'], $renameValues[0]['to']); foreach ($renameValues as $renameData) { // Rename thumbnail @rename($serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file['path'] . $file['name'] . (!empty($renameData['fthumb']) ? '.' . $renameData['fthumb'] : '') . (empty($file['extension']) ? '' : '.' . $file['extension']), $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $newDir . $file['name'] . (!empty($file['thumbnail_name']) ? '.' . $renameData['thumb'] : '') . (empty($file['extension']) ? '' : '.' . $file['extension'])); } serendipity_updateImageInDatabase(array('thumbnail_name' => $renameValues[0]['thumb'], 'realname' => $newDir, 'name' => $newDir), $item_id); $oldDir = $file_old; $newDir = $file_new; $real_oldDir = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $oldDir; $real_newDir = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $newDir; // Forward user to overview (we don't want the user's back button to rename things again) } else { if (!file_exists($oldfile)) { echo '<span class="msg_error"><span class="icon-attention-circled"></span> ' . ERROR_FILE_NOT_EXISTS . '</span>'; } elseif (file_exists($newfile)) { echo '<span class="msg_error"><span class="icon-attention-circled"></span> ' . ERROR_FILE_EXISTS . '</span>'; } else { echo '<span class="msg_error"><span class="icon-attention-circled"></span> ' . ERROR_SOMETHING . '</span>'; } return false; } } } elseif ($type == 'filedir') { serendipity_db_query("UPDATE {$serendipity['dbPrefix']}images\n SET path = '" . serendipity_db_escape_string($newDir) . "'\n WHERE id = " . (int) $item_id); $pick = serendipity_db_query("SELECT * FROM {$serendipity['dbPrefix']}images\n WHERE id = " . (int) $item_id, true, 'assoc'); // Move thumbs $oldfile = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $oldDir . $pick['name'] . (empty($pick['extension']) ? '' : '.' . $pick['extension']); $newfile = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $newDir . $pick['name'] . (empty($pick['extension']) ? '' : '.' . $pick['extension']); $renameValues = array(array('from' => $oldfile, 'to' => $newfile, 'thumb' => $serendipity['thumbSuffix'], 'fthumb' => $pick['thumbnail_name'], 'oldDir' => $oldDir, 'newDir' => $newDir, 'type' => $type, 'item_id' => $item_id, 'file' => $pick, 'name' => $pick['name'])); serendipity_plugin_api::hook_event('backend_media_rename', $renameValues); // Rename file rename($renameValues[0]['from'], $renameValues[0]['to']); foreach ($renameValues as $renameData) { // Rename thumbnail @rename($serendipity['serendipityPath'] . $serendipity['uploadPath'] . $oldDir . $pick['name'] . (!empty($renameData['fthumb']) ? '.' . $renameData['fthumb'] : '') . (empty($pick['extension']) ? '' : '.' . $pick['extension']), $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $newDir . $pick['name'] . (!empty($pick['thumbnail_name']) ? '.' . $pick['thumbnail_name'] : '') . (empty($pick['extension']) ? '' : '.' . $pick['extension'])); } $oldDir .= $pick['name']; $newDir .= $pick['name']; $hasExt = isset($pick['extension']) ? '.' . $pick['extension'] : ''; if (file_exists($newfile)) { echo '<span class="msg_success"><span class="icon-ok-circled"></span> '; printf(MEDIA_DIRECTORY_MOVED, $newDir . $hasExt); echo "</span>\n"; } } elseif ($type == 'dir') { $renameValues = array(array('from' => $oldfile, 'to' => $newfile, 'thumb' => $serendipity['thumbSuffix'], 'fthumb' => $file['thumbnail_name'], 'oldDir' => $oldDir, 'newDir' => $newDir, 'type' => $type, 'item_id' => $item_id, 'file' => $file)); serendipity_plugin_api::hook_event('backend_media_rename', $renameValues); } // Only MySQL supported, since I don't know how to use REGEXPs differently. if ($serendipity['dbType'] != 'mysql' && $serendipity['dbType'] != 'mysqli') { echo '<span class="msg_notice"><span class="icon-info-circled"></span> ' . MEDIA_DIRECTORY_MOVE_ENTRY . "</span>\n"; return true; } // Prepare the SELECT query for filetypes if ($type == 'filedir' || $type == 'file') { $_file = $type == 'filedir' ? $pick : $file; $oldDir = $type == 'file' ? str_replace($_file['name'] . '.', '', $oldDir) : $oldDir; // Path patterns to SELECT en detail to not pick path parts in a loop $oldDirThumb = $oldDir . $_file['name'] . '.' . $_file['thumbnail_name'] . ($_file['extension'] ? '.' . $_file['extension'] : ''); $oldDirFile = $oldDir . $_file['name'] . ($_file['extension'] ? '.' . $_file['extension'] : ''); $quickblogFilePath = $serendipity['serendipityPath'] . $serendipity['uploadHTTPPath'] . $oldDirFile; // REPLACE BY Path and Name only to also match Thumbs if (strpos($oldDir, $_file['name']) === FALSE) { $oldDir .= $_file['name']; } if (strpos($newDir, $_file['name']) === FALSE) { $newDir .= $_file['name']; } // imageselectorplus plugin quickblog is either quickblog:FullPath or quickblog:none|FullPath or quickblog:|(plugin|js|_blankl)|FullPath // For a possible future isp regex change, we search for 'none' between pipes too $q = "SELECT id, body, extended\n FROM {$serendipity['dbPrefix']}entries\n WHERE body REGEXP '(src=|href=|window.open.|<!--quickblog:)(\\'|\"|none\\\\||\\\\|(plugin|none|js|_blank)\\\\|)(" . serendipity_db_escape_String($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDirFile) . "|" . serendipity_db_escape_String($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDirFile) . "|" . serendipity_db_escape_String($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDirThumb) . "|" . serendipity_db_escape_String($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDirThumb) . "|" . serendipity_db_escape_String($quickblogFilePath) . ")'\n OR extended REGEXP '(src=|href=|window.open.)(\\'|\")(" . serendipity_db_escape_String($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDirFile) . "|" . serendipity_db_escape_String($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDirFile) . "|" . serendipity_db_escape_String($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDirThumb) . "|" . serendipity_db_escape_String($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDirThumb) . ")'\n "; } else { $q = "SELECT id, body, extended\n FROM {$serendipity['dbPrefix']}entries\n WHERE body REGEXP '(src=|href=|window.open.)(\\'|\")(" . serendipity_db_escape_String($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDir) . "|" . serendipity_db_escape_string($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDir) . ")'\n OR extended REGEXP '(src=|href=|window.open.)(\\'|\")(" . serendipity_db_escape_String($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDir) . "|" . serendipity_db_escape_string($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDir) . ")'\n "; } // strip, if last char is a period ".", which may happen with quickblog image path strings $newDir = rtrim($newDir, '.'); $dirs = serendipity_db_query($q); if (is_array($dirs)) { foreach ($dirs as $dir) { $dir['body'] = preg_replace('@(src=|href=|window.open.)(\'|")(' . preg_quote($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDir) . '|' . preg_quote($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDir) . ')@', '\\1\\2' . $serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $newDir, $dir['body']); $dir['body'] = preg_replace('@(<!--quickblog:)(none\\||\\|(plugin|none|js|_blank)\\|)(' . preg_quote($serendipity['serendipityPath'] . $serendipity['uploadHTTPPath'] . $oldDir) . ')@', '\\1\\2' . $serendipity['serendipityPath'] . $serendipity['uploadHTTPPath'] . $newDir, $dir['body']); $dir['extended'] = preg_replace('@(src=|href=|window.open.)(\'|")(' . preg_quote($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDir) . '|' . preg_quote($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDir) . ')@', '\\1\\2' . $serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $newDir, $dir['extended']); $uq = "UPDATE {$serendipity['dbPrefix']}entries\n SET body = '" . serendipity_db_escape_string($dir['body']) . "' ,\n extended = '" . serendipity_db_escape_string($dir['extended']) . "'\n WHERE id = " . serendipity_db_escape_string($dir['id']); serendipity_db_query($uq); } $imgmovedtodir = sprintf(MEDIA_DIRECTORY_MOVE_ENTRIES, count($dirs)); printf('<span class="msg_notice"><span class="icon-info-circled"></span> ' . $imgmovedtodir . '</span>'); } return true; }
/** * Moves a media directory * * @param string The old directory * @param string The new directory * @param string The type of what to remove (dir|file|filedir) * @param string An item id of a file * @return boolean * */ function serendipity_moveMediaDirectory($oldDir, $newDir, $type = 'dir', $item_id = null, $file = null) { global $serendipity; $real_oldDir = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $oldDir; $real_newDir = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $newDir; if ($type == 'dir') { if (!is_dir($real_oldDir)) { printf(ERROR_FILE_NOT_EXISTS . '<br />', $oldDir); return false; } if (is_dir($real_newDir)) { printf(ERROR_FILE_EXISTS . '<br />', $newDir); return false; } if (!rename($real_oldDir, $real_newDir)) { printf(MEDIA_DIRECTORY_MOVE_ERROR . '<br />', $newDir); return false; } printf(MEDIA_DIRECTORY_MOVED . '<br />', $newDir); $dirs = serendipity_db_query("SELECT id, path\n FROM {$serendipity['dbPrefix']}images\n WHERE path LIKE '" . serendipity_db_escape_string($oldDir) . "%'", false, 'assoc'); if (is_array($dirs)) { foreach ($dirs as $dir) { $old = $dir['path']; $new = preg_replace('@^(' . preg_quote($oldDir) . ')@i', $newDir, $old); serendipity_db_query("UPDATE {$serendipity['dbPrefix']}images\n SET path = '" . serendipity_db_escape_string($new) . "'\n WHERE id = {$dir['id']}"); } } $dirs = serendipity_db_query("SELECT groupid, artifact_id, artifact_type, artifact_mode, artifact_index\n FROM {$serendipity['dbPrefix']}access\n WHERE artifact_type = 'directory'\n AND artifact_index LIKE '" . serendipity_db_escape_string($oldDir) . "%'", false, 'assoc'); if (is_array($dirs)) { foreach ($dirs as $dir) { $old = $dir['artifact_index']; $new = preg_replace('@^(' . preg_quote($oldDir) . ')@i', $newDir, $old); serendipity_db_query("UPDATE {$serendipity['dbPrefix']}access\n SET artifact_index = '" . serendipity_db_escape_string($new) . "'\n WHERE groupid = '" . serendipity_db_escape_string($dir['groupid']) . "'\n AND artifact_id = '" . serendipity_db_escape_string($dir['artifact_id']) . "'\n AND artifact_type = '" . serendipity_db_escape_string($dir['artifact_type']) . "'\n AND artifact_mode = '" . serendipity_db_escape_string($dir['artifact_mode']) . "'\n AND artifact_index = '" . serendipity_db_escape_string($dir['artifact_index']) . "'"); } } } if ($type == 'file') { if (serendipity_isActiveFile(basename($newDir))) { printf(ERROR_FILE_FORBIDDEN, htmlspecialchars($newDir)); return false; } if ($file['hotlink']) { serendipity_updateImageInDatabase(array('realname' => $newDir, 'name' => $newDir), $item_id); } else { $file_new = $file['path'] . $newDir . '.'; $file_old = $file['path'] . $file['name'] . '.'; $newfile = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file_new . $file['extension']; $oldfile = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file_old . $file['extension']; if ($newDir != '' && file_exists($oldfile) && !file_exists($newfile)) { $renameValues = array(array('from' => $oldfile, 'to' => $newfile, 'thumb' => $serendipity['thumbSuffix'], 'fthumb' => $file['thumbnail_name'], 'oldDir' => $oldDir, 'newDir' => $newDir, 'type' => $type, 'item_id' => $item_id, 'file' => $file)); serendipity_plugin_api::hook_event('backend_media_rename', $renameValues); // Rename file rename($renameValues[0]['from'], $renameValues[0]['to']); foreach ($renameValues as $renameData) { // Rename thumbnail rename($serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file['path'] . $file['name'] . (!empty($renameData['fthumb']) ? '.' . $renameData['fthumb'] : '') . '.' . $file['extension'], $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file['path'] . $newDir . '.' . $renameData['thumb'] . '.' . $file['extension']); } serendipity_updateImageInDatabase(array('thumbnail_name' => $renameValues[0]['thumb'], 'realname' => $newDir, 'name' => $newDir), $item_id); $oldDir = $file_old; $newDir = $file_new; $real_oldDir = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $oldDir; $real_newDir = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $newDir; // Forward user to overview (we don't want the user's back button to rename things again) } else { if (!file_exists($oldfile)) { echo ERROR_FILE_NOT_EXISTS; } elseif (file_exists($newfile)) { echo ERROR_FILE_EXISTS; } else { echo ERROR_SOMETHING; } return false; } } } elseif ($type == 'filedir') { serendipity_db_query("UPDATE {$serendipity['dbPrefix']}images\n SET path = '" . serendipity_db_escape_string($newDir) . "'\n WHERE id = " . (int) $item_id); $pick = serendipity_db_query("SELECT * FROM {$serendipity['dbPrefix']}images\n WHERE id = " . (int) $item_id, true, 'assoc'); // Move thumbs $oldfile = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $oldDir . $pick['name'] . '.' . $pick['extension']; $newfile = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $newDir . $pick['name'] . '.' . $pick['extension']; $renameValues = array(array('from' => $oldfile, 'to' => $newfile, 'thumb' => $serendipity['thumbSuffix'], 'fthumb' => $pick['thumbnail_name'], 'oldDir' => $oldDir, 'newDir' => $newDir, 'type' => $type, 'item_id' => $item_id, 'file' => $file, 'name' => $pick['name'])); serendipity_plugin_api::hook_event('backend_media_rename', $renameValues); // Rename file rename($renameValues[0]['from'], $renameValues[0]['to']); foreach ($renameValues as $renameData) { // Rename thumbnail rename($serendipity['serendipityPath'] . $serendipity['uploadPath'] . $oldDir . $pick['name'] . (!empty($renameData['fthumb']) ? '.' . $renameData['fthumb'] : '') . '.' . $pick['extension'], $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $newDir . $pick['name'] . '.' . $renameData['thumb'] . '.' . $pick['extension']); } $oldDir .= $pick['name']; $newDir .= $pick['name']; } elseif ($type == 'dir') { $renameValues = array(array('from' => $oldfile, 'to' => $newfile, 'thumb' => $serendipity['thumbSuffix'], 'fthumb' => $file['thumbnail_name'], 'oldDir' => $oldDir, 'newDir' => $newDir, 'type' => $type, 'item_id' => $item_id, 'file' => $file)); serendipity_plugin_api::hook_event('backend_media_rename', $renameValues); } // Only MySQL supported, since I don't know how to use REGEXPs differently. if ($serendipity['dbType'] != 'mysql' && $serendipity['dbType'] != 'mysqli') { echo MEDIA_DIRECTORY_MOVE_ENTRY . '<br />'; return true; } $q = "SELECT id, body, extended\n FROM {$serendipity['dbPrefix']}entries\n WHERE body REGEXP '(src=|href=|window.open.)(\\'|\")(" . serendipity_db_escape_String($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDir) . "|" . serendipity_db_escape_string($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDir) . ")'\n OR extended REGEXP '(src=|href=|window.open.)(\\'|\")(" . serendipity_db_escape_String($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDir) . "|" . serendipity_db_escape_string($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDir) . ")'\n "; $dirs = serendipity_db_query($q); if (is_array($dirs)) { foreach ($dirs as $dir) { $dir['body'] = preg_replace('@(src=|href=|window.open.)(\'|")(' . preg_quote($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDir) . '|' . preg_quote($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDir) . ')@', '\\1\\2' . $serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $newDir, $dir['body']); $dir['extended'] = preg_replace('@(src=|href=|window.open.)(\'|")(' . preg_quote($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDir) . '|' . preg_quote($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDir) . ')@', '\\1\\2' . $serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $newDir, $dir['extended']); $uq = "UPDATE {$serendipity['dbPrefix']}entries\n SET body = '" . serendipity_db_escape_string($dir['body']) . "' ,\n extended = '" . serendipity_db_escape_string($dir['extended']) . "'\n WHERE id = " . serendipity_db_escape_string($dir['id']); serendipity_db_query($uq); } printf(MEDIA_DIRECTORY_MOVE_ENTRIES . '<br />', count($dirs)); } return true; }
/** * Moves a media directory * * @param string The old directory. * This can be NULL or (an empty / a) STRING for re-name/multiCheck move comparison events * @param string The new directory * @param string The type of what to remove (dir|file|filedir) * @param string An item id of a file * @param array Result of serendipity_fetchImageFromDatabase($id) * @return boolean * */ function serendipity_moveMediaDirectory($oldDir, $newDir, $type = 'dir', $item_id = null, $file = null) { global $serendipity; // paranoid case for updating an old image id entry - else we have a new entry incrementary if (is_null($item_id) && isset($file['id']) && $file['id'] > 0) { $item_id = $file['id']; } if (!$item_id || $item_id < 1) { // only print message if not posting a case_directoryEdit submit if (empty($serendipity['POST']['save'])) { echo '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span> '; printf(ERROR_FILE_NOT_EXISTS, $item_id); echo "</span>\n"; return false; } } // Prepare data for the database, any hooks and the real file move, by case AREA: // DIR = Media directory form edit, // FILE = File rename or File bulk move, // FILEDIR = Media properties form edit // images.inc case 'directoryEdit', which is ML Directories form, via ML case 'directorySelect' if ($type == 'dir') { $real_oldDir = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $oldDir; $real_newDir = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $newDir; if (!is_dir($real_oldDir)) { echo '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span> '; printf(ERROR_FILE_NOT_EXISTS, $oldDir); echo "</span>\n"; return false; } if (is_dir($real_newDir)) { echo '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span> '; printf(ERROR_FILE_EXISTS, $newDir); echo "</span>\n"; return false; } if (!rename($real_oldDir, $real_newDir)) { echo '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span> '; printf(MEDIA_DIRECTORY_MOVE_ERROR, $newDir); echo "</span>\n"; return false; } echo '<span class="msg_success"><span class="icon-ok-circled" aria-hidden="true"></span> '; printf(MEDIA_DIRECTORY_MOVED, $newDir); echo "</span>\n"; $dirs = serendipity_db_query("SELECT id, path\n FROM {$serendipity['dbPrefix']}images\n WHERE path LIKE '" . serendipity_db_escape_string($oldDir) . "%'", false, 'assoc'); if (is_array($dirs)) { foreach ($dirs as $dir) { $old = $dir['path']; $new = preg_replace('@^(' . preg_quote($oldDir) . ')@i', $newDir, $old); serendipity_db_query("UPDATE {$serendipity['dbPrefix']}images\n SET path = '" . serendipity_db_escape_string($new) . "'\n WHERE id = {$dir['id']}"); } } $dirs = serendipity_db_query("SELECT groupid, artifact_id, artifact_type, artifact_mode, artifact_index\n FROM {$serendipity['dbPrefix']}access\n WHERE artifact_type = 'directory'\n AND artifact_index LIKE '" . serendipity_db_escape_string($oldDir) . "%'", false, 'assoc'); if (is_array($dirs)) { foreach ($dirs as $dir) { $old = $dir['artifact_index']; $new = preg_replace('@^(' . preg_quote($oldDir) . ')@i', $newDir, $old); serendipity_db_query("UPDATE {$serendipity['dbPrefix']}access\n SET artifact_index = '" . serendipity_db_escape_string($new) . "'\n WHERE groupid = '" . serendipity_db_escape_string($dir['groupid']) . "'\n AND artifact_id = '" . serendipity_db_escape_string($dir['artifact_id']) . "'\n AND artifact_type = '" . serendipity_db_escape_string($dir['artifact_type']) . "'\n AND artifact_mode = '" . serendipity_db_escape_string($dir['artifact_mode']) . "'\n AND artifact_index = '" . serendipity_db_escape_string($dir['artifact_index']) . "'"); } } // hook into staticpage for the renaming regex replacements // first and last two are null - only differ by being set already by their default var for the last two $renameValues = array(array('from' => null, 'to' => null, 'thumb' => $serendipity['thumbSuffix'], 'fthumb' => null, 'oldDir' => $oldDir, 'newDir' => $newDir, 'type' => $type, 'item_id' => $item_id, 'file' => $file)); // Changing a ML directory via directoryEdit needs to run through entries too! serendipity_plugin_api::hook_event('backend_media_rename', $renameValues); // case 'rename' OR 'multidelete' (bulk multimove) } else { if ($type == 'file') { // active in mean of eval or executable if (serendipity_isActiveFile(basename($newDir))) { echo '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span> '; printf(ERROR_FILE_FORBIDDEN, serendipity_specialchars($newDir)); echo "</span>\n"; return false; } if (!empty($file['hotlink'])) { $newHotlinkFile = false === strpos($newDir, $file['extension']) ? $newDir . (empty($file['extension']) ? '' : '.' . $file['extension']) : $newDir; serendipity_updateImageInDatabase(array('realname' => $newHotlinkFile, 'name' => $newDir), $item_id); } else { $parts = pathinfo($newDir); // build new, thumb and old file names relative to Serendipity root path if ($oldDir === null && $newDir != 'uploadRoot') { // case single file re-name event (newDir = newName is passed without path!) $newName = $newDir; // for better readability // do we really need this? if ($parts['extension'] != $file['extension']) { $file_new = $file['path'] . $newName . (empty($file['extension']) ? '' : '.' . $file['extension']); $file_old = $file['path'] . $file['name'] . (empty($file['extension']) ? '' : '.' . $file['extension']); } else { $file_new = $file['path'] . $newName; $file_old = $file['path'] . $file['name']; } // build full thumb file names $file_newthumb = $file['path'] . $newName . (!empty($file['thumbnail_name']) ? '.' . $file['thumbnail_name'] : '') . (empty($file['extension']) ? '' : '.' . $file['extension']); $file_oldthumb = $file['path'] . $file['name'] . (!empty($file['thumbnail_name']) ? '.' . $file['thumbnail_name'] : '') . (empty($file['extension']) ? '' : '.' . $file['extension']); $newThumb = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file_newthumb; $oldThumb = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file_oldthumb; } else { // case bulkmove event (newDir is passed inclusive path! and normally w/o the filename, but we better check this though) $newDir = $newDir == 'uploadRoot' ? '' : $newDir; // Take care: remove temporary 'uploadRoot' string, in case of moving a subdir file into upload root by bulkmove $_newDir = str_replace($file['name'] . (empty($file['extension']) ? '' : '.' . $file['extension']), '', $newDir); // do we really need this? if ($parts['extension'] != $file['extension']) { $file_new = $_newDir . $file['name'] . (empty($file['extension']) ? '' : '.' . $file['extension']); $file_old = $file['path'] . $file['name'] . (empty($file['extension']) ? '' : '.' . $file['extension']); } else { $file_new = $_newDir . $file['name']; $file_old = $file['path'] . $file['name']; } } // build full origin and new file path names $newfile = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file_new; $oldfile = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file_old; // check files existence if (file_exists($oldfile) && !file_exists($newfile)) { // for the paranoid, securely check these build filenames again, since we really need a real file set to continue! $newparts = pathinfo($newfile); if ($newparts['dirname'] == '.' || !empty($file['extension']) && empty($newparts['extension']) || empty($newparts['filename'])) { // error new file build mismatch echo '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span> ' . $newfile . ' ' . ERROR_SOMETHING . "</span>\n"; return false; } // Case re-name event, keeping a possible moved directory name for a single file if ($oldDir === null) { // Move the origin file @rename($oldfile, $newfile); // do not re-name again, if item has no thumb name (eg zip object file case) and old thumb actually exists (possible missing pdf preview image on WinOS with IM) if ($newThumb != $newfile && file_exists($oldThumb)) { // the thumb file @rename($oldThumb, $newThumb); // Keep both rename() error disabled, since we have to avoid any output in renaiming cases } // hook into staticpage for the renaming regex replacements $renameValues = array(array('from' => $oldfile, 'to' => $newfile, 'thumb' => $serendipity['thumbSuffix'], 'fthumb' => $file['thumbnail_name'], 'oldDir' => $oldDir, 'newDir' => $newDir, 'type' => $type, 'item_id' => $item_id, 'file' => $file)); serendipity_plugin_api::hook_event('backend_media_rename', $renameValues); // renaming filenames has to update mediaproperties if set $q = "UPDATE {$serendipity['dbPrefix']}mediaproperties\n SET value = '" . serendipity_db_escape_string($newName . (empty($file['extension']) ? '' : '.' . $file['extension'])) . "'\n WHERE mediaid = " . (int) $item_id . ' AND property = "realname" AND value = "' . $file['realname'] . '"'; serendipity_db_query($q); $q = "UPDATE {$serendipity['dbPrefix']}mediaproperties\n SET value = '" . serendipity_db_escape_string($newName) . "'\n WHERE mediaid = " . (int) $item_id . ' AND property = "name" AND value = "' . $file['name'] . '"'; serendipity_db_query($q); $q = "UPDATE {$serendipity['dbPrefix']}mediaproperties\n SET value = '" . serendipity_db_escape_string($newName . (empty($file['extension']) ? '' : '.' . $file['extension'])) . "'\n WHERE mediaid = " . (int) $item_id . ' AND property = "TITLE" AND value = "' . $file['realname'] . '"'; serendipity_db_query($q); serendipity_updateImageInDatabase(array('thumbnail_name' => $renameValues[0]['thumb'], 'realname' => $newName . (empty($file['extension']) ? '' : '.' . $file['extension']), 'name' => $newName), $item_id); // Forward user to overview (we don't want the user's back button to rename things again) ?? What does this do? Check!!! } elseif (!empty($newfile)) { if ($newDir == 'uploadRoot') { $newDir = ''; } // now move back into root of /uploads dir // hook into staticpage for the renaming regex replacements $renameValues = array(array('from' => $oldfile, 'to' => $newfile, 'thumb' => $serendipity['thumbSuffix'], 'fthumb' => $file['thumbnail_name'], 'oldDir' => $oldDir, 'newDir' => $newDir, 'type' => $type, 'item_id' => $item_id, 'file' => $file)); serendipity_plugin_api::hook_event('backend_media_rename', $renameValues); // eg. for staticpage entries path regex replacements // Move the origin file try { rename($oldfile, $newfile); } catch (Exception $e) { echo '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span> ' . ERROR_SOMETHING . ': ' . $e->getMessage() . "</span>\n"; } // do still need this? YES, it is definitely false, so we would not need the ternary // Rename newDir + file name in case it is called by the Bulk Move and not by rename $newDirFile = false === strpos($newDir, $file['name']) ? $newDir . $file['name'] : $newDir; foreach ($renameValues as $renameData) { // build full thumb file names $thisOldThumb = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $renameData['oldDir'] . $file['name'] . (!empty($renameData['fthumb']) ? '.' . $renameData['fthumb'] : '.' . $serendipity['thumbSuffix']) . (empty($file['extension']) ? '' : '.' . $file['extension']); $thisNewThumb = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $newDirFile . (!empty($file['thumbnail_name']) ? '.' . $renameData['thumb'] : '.' . $serendipity['thumbSuffix']) . (empty($file['extension']) ? '' : '.' . $file['extension']); // Check for existent old thumb files first, to not need to disable rename by @rename(), then move the thumb file and catch any wrong renaming if ($thisNewThumb != $newfile && file_exists($thisOldThumb)) { // the thumb file and catch any wrong renaming try { rename($thisOldThumb, $thisNewThumb); } catch (Exception $e) { echo '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span> ' . ERROR_SOMETHING . ': ' . $e->getMessage() . "</span>\n"; } } } serendipity_updateImageInDatabase(array('thumbnail_name' => $renameValues[0]['thumb'], 'path' => $newDir, 'realname' => $file['realname'], 'name' => $file['name']), $item_id); // Forward user to overview (we don't want the user's back button to rename things again) } else { //void } } else { if (!file_exists($oldfile)) { echo '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span> ' . ERROR_FILE_NOT_EXISTS . "</span>\n"; } elseif (file_exists($newfile)) { echo '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span> ' . ERROR_FILE_EXISTS . "</span>\n"; } else { echo '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span> ' . ERROR_SOMETHING . "</span>\n"; } return false; } } // used solely by serendipity_parsePropertyForm base_properties when changing the file selected path within mediaproperties form } elseif ($type == 'filedir') { serendipity_db_query("UPDATE {$serendipity['dbPrefix']}images\n SET path = '" . serendipity_db_escape_string($newDir) . "'\n WHERE id = " . (int) $item_id); $pick = serendipity_db_query("SELECT * FROM {$serendipity['dbPrefix']}images\n WHERE id = " . (int) $item_id, true, 'assoc'); // Move thumbs - Rebuild full origin and new file path names by the new picked file array $oldfile = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $oldDir . $pick['name'] . (empty($pick['extension']) ? '' : '.' . $pick['extension']); $newfile = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $newDir . $pick['name'] . (empty($pick['extension']) ? '' : '.' . $pick['extension']); // hook into staticpage for the renaming regex replacements $renameValues = array(array('from' => $oldfile, 'to' => $newfile, 'thumb' => $serendipity['thumbSuffix'], 'fthumb' => $pick['thumbnail_name'], 'oldDir' => $oldDir, 'newDir' => $newDir, 'type' => $type, 'item_id' => $item_id, 'file' => $pick, 'name' => $pick['name'])); serendipity_plugin_api::hook_event('backend_media_rename', $renameValues); // Move the origin file try { rename($oldfile, $newfile); } catch (Exception $e) { echo '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span> ' . ERROR_SOMETHING . ': ' . $e->getMessage() . "</span>\n"; } foreach ($renameValues as $renameData) { $thisOldThumb = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $oldDir . $pick['name'] . (!empty($renameData['fthumb']) ? '.' . $renameData['fthumb'] : '') . (empty($pick['extension']) ? '' : '.' . $pick['extension']); $thisNewThumb = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $newDir . $pick['name'] . (!empty($pick['thumbnail_name']) ? '.' . $pick['thumbnail_name'] : '') . (empty($pick['extension']) ? '' : '.' . $pick['extension']); // Check for existent old thumb files first, to not need to disable rename by @rename(),then move the thumb file and catch any wrong renaming if ($thisNewThumb != $newfile && file_exists($thisOldThumb)) { // the thumb file and catch any wrong renaming try { rename($thisOldThumb, $thisNewThumb); } catch (Exception $e) { echo '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span> ' . ERROR_SOMETHING . ': ' . $e->getMessage() . "</span>\n"; } } } // no need to use serendipity_updateImageInDatabase() here since already done in this case start // ???? Forward user to overview (we don't want the user's back button to rename things again) // prepare for message $thisnew = (empty($newDir) ? $serendipity['uploadPath'] : '') . $newDir . $pick['name']; $thisExt = isset($pick['extension']) ? '.' . $pick['extension'] : ''; if (file_exists($newfile)) { echo '<span class="msg_success"><span class="icon-ok-circled" aria-hidden="true"></span> '; printf(MEDIA_DIRECTORY_MOVED, $thisnew . $thisExt); echo "</span>\n"; } } } // case dir, file, filedir end // Entry REPLACEMENT AREA // Only MySQL supported, since I don't know how to use REGEXPs differently. // Ian: we should improve this to all! if ($serendipity['dbType'] != 'mysql' && $serendipity['dbType'] != 'mysqli') { echo '<span class="msg_notice"><span class="icon-info-circled" aria-hidden="true"></span> ' . MEDIA_DIRECTORY_MOVE_ENTRY . "</span>\n"; return true; } // Prepare the SELECT query for filetypes if ($type == 'filedir' || $type == 'file') { // get the right $file, which is array or null, by type $_file = $type == 'filedir' ? $pick : $file; // check oldDir in bulkmove case $oldDir = $type == 'file' && !is_null($oldDir) ? str_replace($_file['name'] . '.' . $_file['extension'], '', $oldDir) : $oldDir; // Path patterns to SELECT en detail to not pick path parts in a loop if ($oldDir === null) { // care for file renaming with oldpath $oldDirFile = $_file['path'] . $_file['name'] . ($_file['extension'] ? '.' . $_file['extension'] : ''); $oldDirThumb = $_file['path'] . $_file['name'] . '.' . $_file['thumbnail_name'] . ($_file['extension'] ? '.' . $_file['extension'] : ''); } else { $oldDirFile = $oldDir . $_file['name'] . ($_file['extension'] ? '.' . $_file['extension'] : ''); $oldDirThumb = $oldDir . $_file['name'] . '.' . $_file['thumbnail_name'] . ($_file['extension'] ? '.' . $_file['extension'] : ''); } if ($type == 'filedir' && !isset($newDirFile)) { $newDirFile = strpos($newDir, $_file['name']) === FALSE ? $newDir . $_file['name'] : $newDir; } if ($type == 'file' && $oldDir === null) { $newDirFile = empty($newDirFile) ? $newDir : $newDirFile; // for file renamings $newDirFile has to be $newDir ( which is subdir and new NAME w/o ext) } $ispOldFile = $serendipity['serendipityPath'] . $serendipity['uploadHTTPPath'] . $oldDirFile; } elseif ($type == 'dir') { // since this is case 'dir', we do not have a filename and have to rename replacement File vars to oldDir and newDir values for the update preg_replace match $oldDirFile = $oldDir; $newDirFile = $newDir; $ispOldFile = $serendipity['serendipityPath'] . $serendipity['uploadHTTPPath'] . $oldDirFile . ($_file['extension'] ? '.' . $_file['extension'] : ''); } // Please note: imageselectorplus plugin quickblog is either quickblog:FullPath or quickblog:|?(none|plugin|js|_blank)|FullPath // SELECTing the entries uses a more detailled approach to be as precise as possible, thus we need to reset these vars for the preg_replace later on in some cases $q = "SELECT id, body, extended\n FROM {$serendipity['dbPrefix']}entries\n WHERE body REGEXP '(src=|href=|window.open.|<!--quickblog:)(\\'|\"|\\\\|?(plugin|none|js|_blank)?\\\\|?)(" . serendipity_db_escape_String($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDirFile) . "|" . serendipity_db_escape_String($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDirFile) . "|" . serendipity_db_escape_String($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDirThumb) . "|" . serendipity_db_escape_String($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDirThumb) . "|" . serendipity_db_escape_String($ispOldFile) . ")'\n OR extended REGEXP '(src=|href=|window.open.)(\\'|\")(" . serendipity_db_escape_String($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDirFile) . "|" . serendipity_db_escape_String($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDirFile) . "|" . serendipity_db_escape_String($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDirThumb) . "|" . serendipity_db_escape_String($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDirThumb) . ")'\n "; $entries = serendipity_db_query($q, false, 'assoc'); if (is_array($entries) && !empty($entries)) { // here we need to match thumbs too, so we do not want the extension, see detailled SELECT regex note if ($type == 'file' && $oldDir === null) { $_ispOldFile = $oldfile; // these vars are more exact in every case $_ispNewFile = $newfile; // dito $oldDirFile = $_file['path'] . $oldDirFile; // oldDirFile is missing a possible subdir path for the preg_replace $newDirFile = $_file['path'] . $newDirFile; // newDirFile - dito } else { $_ispOldFile = $ispOldFile; $_ispNewFile = $serendipity['serendipityPath'] . $serendipity['uploadHTTPPath'] . $newDirFile . ($_file['extension'] ? '.' . $_file['extension'] : ''); } // last paranoidal check $_oldDirFile = strpos($oldDirFile, $_file['extension']) === FALSE ? $oldDirFile : $oldDir . $_file['name']; // what we actually need here, is oldDirFile w/o EXT to newDirFile w/o EXT and full ispOldFile path to full ispNewFile path !!! foreach ($entries as $entry) { $entry['body'] = preg_replace('@(src=|href=|window.open.)(\'|")(' . preg_quote($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $_oldDirFile) . '|' . preg_quote($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $_oldDirFile) . ')@', '\\1\\2' . $serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $newDirFile, $entry['body']); $entry['body'] = preg_replace('@(<!--quickblog:)(\\|?(plugin|none|js|_blank)?\\|?)(' . preg_quote($_ispOldFile) . ')@', '\\1\\2' . $_ispNewFile, $entry['body']); $entry['extended'] = preg_replace('@(src=|href=|window.open.)(\'|")(' . preg_quote($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $_oldDirFile) . '|' . preg_quote($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $_oldDirFile) . ')@', '\\1\\2' . $serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $newDirFile, $entry['extended']); $uq = "UPDATE {$serendipity['dbPrefix']}entries\n SET body = '" . serendipity_db_escape_string($entry['body']) . "' ,\n extended = '" . serendipity_db_escape_string($entry['extended']) . "'\n WHERE id = " . serendipity_db_escape_string($entry['id']); serendipity_db_query($uq); } if ($oldDir !== null) { $imgmovedtodir = sprintf(MEDIA_DIRECTORY_MOVE_ENTRIES, count($entries)); echo '<span class="msg_notice"><span class="icon-info-circled" aria-hidden="true"></span> ' . $imgmovedtodir . "</span>\n"; } } return true; }