/cms113/cms/rename-file.php
PHP | 144 lines | 101 code | 7 blank | 36 comment | 27 complexity | d21922df9c1d11095d5013bce220057a MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
- <?
- /*
- * ***********************************************************************
- * Copyright Ben Hunt 2007, 2008
- *
- * This file is part of cmsfromscratch.
-
- Cmsfromscratch is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- Cmsfromscratch is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Cmsfromscratch. If not, see <http://www.gnu.org/licenses/>.
- ***********************************************************************
- */
- /*
- Expects:
- $_GET['filePath'] ;
- $_GET['newFileName'] ;
- */
- require '../cmsfns.php' ;
-
- if (!isSet($_GET['filePath'])) {
- echo 'error:No original file name sent' ;
- exit ;
- }
- else $filePath = pathFromID($_GET['filePath']) ;
- if (!file_exists($filePath)) {
- echo 'error:Original file not found.' ;
- exit ;
- }
-
- if (!isSet($_GET['newFileName'])) {
- echo 'error:No new file name sent' ;
- exit ;
- }
- else $newFileName = $_GET['newFileName'] ;
-
- $dir = dirname($filePath) ;
- $ext = getFileExtension($filePath) ;
-
- /*
- If it's a SET TEMPLATE ???
- */
-
- if (isPageTemplate($filePath)) {
- // PAGE TEMPLATE --- rename it and its LCI folder
- $newFilePath = $dir . '/' . $newFileName . '.' . $ext ;
- if (False === @rename($filePath, $newFilePath)) {
- echo 'error:Failed to rename Page Template: ' . $filePath . ' >>> ' . $newFilePath ;
- exit ;
- }
- if ($ext == 'php') {
- $originalLCIFolder = stripFileExtension($filePath) ;
- $newLCIFolder = stripFileExtension($newFilePath) ;
- if (False === @rename($originalLCIFolder, $newLCIFolder)) {
- echo 'error:Failed to rename Page Template includes folder: ' . $originalLCIFolder . ' >>> ' . $newLCIFolder ;
- exit ;
- }
- }
- echo 'pt' ;
- exit ;
- }
- else if ($ext == 'php') {
- // PAGE --- rename the page and its LCI folder, and its partner in Includes
- $newFilePath = $dir . '/' . $newFileName . '.' . $ext ;
- if (False === @rename($filePath, $newFilePath)) {
- echo 'error:Failed to rename page: ' . $filePath . ' >>> ' . $newFilePath ;
- exit ;
- }
- if (False === @rename(getPreviewFileFromLive($filePath), getPreviewFileFromLive($newFilePath))) {
- echo 'error:Failed to rename includes version of page: ' . getPreviewFileFromLive($filePath) . ' >>> ' . getPreviewFileFromLive($newFilePath) ;
- exit ;
- }
- $lciRoot = getLCIRootFolderFromPagePath($filePath) ;
- $newLciRoot = getLCIRootFolderFromPagePath($newFilePath) ;
- if (False === @rename($lciRoot, $newLciRoot)) {
- echo 'error:Failed to rename page LCI folder: ' . $lciRoot . ' >>> ' . $newLciRoot ;
- exit ;
- }
- echo 'regular' ;
- exit ;
- }
- else if (isLCI($filePath)) {
- // CHILD INCLUDE --- rename it and its sibling
- // e.g. includes/page_cms_files/cms_preview/lci.ext
- $newFilePath = $dir . '/' . $newFileName . '.' . $ext ;
- if (False === @rename($filePath, $newFilePath)) {
- echo 'error:Failed to rename preview include: ' . $filePath ;
- exit ;
- }
- if (False === @rename(getLiveLCIFromPreview($filePath), getLiveLCIFromPreview($newFilePath))) {
- echo 'error:Failed to rename live include: ' . getLiveLCIFromPreview($filePath) . ' >>> ' . getLiveLCIFromPreview($newFilePath) ;
- exit ;
- }
- /* ALSO TRY TO RENAME ANY REFERENCES TO THE LCI IN THE PARENT PAGE */
- $parentPagePath = getParentPageFromLCI($filePath) ;
- $parentPageSource = file_get_contents($parentPagePath) ;
- $newParentPageSource = str_replace(basename($filePath), $newFileName . '.' . $ext, $parentPageSource) ;
- @file_put_contents($parentPagePath, $newParentPageSource) ;
-
- // Success
- echo 'regular' ;
- exit ;
- }
- else if (is_dir($filePath)) {
- // DIRECTORY --- rename it and its partner in Includes
- $newDirPath = dirname($filePath) . '/' . $newFileName ;
- if (False === @rename($filePath, $newDirPath)) {
- echo 'error:Failed to rename folder: ' . $filePath . ' >>> ' . $newDirPath ;
- exit ;
- }
- $includesDirPath = getPreviewFileFromLive($filePath) ;
- $newIncludesDirPath = getPreviewFileFromLive($newDirPath) ;
- if (False === @rename($includesDirPath, $newIncludesDirPath)) {
- echo 'error:Failed to rename includes folder: ' . $includesDirPath . ' >>> ' . $newIncludesDirPath ;
- exit ;
- }
- echo 'regular' ;
- exit ;
- }
- else {
- // FREE INCLUDE --- rename it and its partner in Includes
- $newFilePath = $dir . '/' . $newFileName . '.' . $ext ;
- if (False === @rename($filePath, $newFilePath)) {
- echo 'error:Failed to rename free include: ' . $filePath . ' >>> ' . $newFilePath ;
- exit ;
- }
-
- if (False === @rename(getPreviewFileFromLive($filePath), getPreviewFileFromLive($newFilePath))) {
- echo 'error:Failed to rename includes version of free include: ' . getPreviewFileFromLive($filePath) . ' >>> ' . getPreviewFileFromLive($newFilePath) ;
- exit ;
- }
- echo 'regular' ;
- exit ;
- }
- echo 'Not handled: ' . $filePath ;
- ?>