PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/cms113/cms/rename-file.php

http://cmsfromscratch.googlecode.com/
PHP | 144 lines | 101 code | 7 blank | 36 comment | 27 complexity | d21922df9c1d11095d5013bce220057a MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?
  2. /*
  3. * ***********************************************************************
  4. * Copyright Š Ben Hunt 2007, 2008
  5. *
  6. * This file is part of cmsfromscratch.
  7. Cmsfromscratch is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11. Cmsfromscratch is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with Cmsfromscratch. If not, see <http://www.gnu.org/licenses/>.
  17. ***********************************************************************
  18. */
  19. /*
  20. Expects:
  21. $_GET['filePath'] ;
  22. $_GET['newFileName'] ;
  23. */
  24. require '../cmsfns.php' ;
  25. if (!isSet($_GET['filePath'])) {
  26. echo 'error:No original file name sent' ;
  27. exit ;
  28. }
  29. else $filePath = pathFromID($_GET['filePath']) ;
  30. if (!file_exists($filePath)) {
  31. echo 'error:Original file not found.' ;
  32. exit ;
  33. }
  34. if (!isSet($_GET['newFileName'])) {
  35. echo 'error:No new file name sent' ;
  36. exit ;
  37. }
  38. else $newFileName = $_GET['newFileName'] ;
  39. $dir = dirname($filePath) ;
  40. $ext = getFileExtension($filePath) ;
  41. /*
  42. If it's a SET TEMPLATE ???
  43. */
  44. if (isPageTemplate($filePath)) {
  45. // PAGE TEMPLATE --- rename it and its LCI folder
  46. $newFilePath = $dir . '/' . $newFileName . '.' . $ext ;
  47. if (False === @rename($filePath, $newFilePath)) {
  48. echo 'error:Failed to rename Page Template: ' . $filePath . ' >>> ' . $newFilePath ;
  49. exit ;
  50. }
  51. if ($ext == 'php') {
  52. $originalLCIFolder = stripFileExtension($filePath) ;
  53. $newLCIFolder = stripFileExtension($newFilePath) ;
  54. if (False === @rename($originalLCIFolder, $newLCIFolder)) {
  55. echo 'error:Failed to rename Page Template includes folder: ' . $originalLCIFolder . ' >>> ' . $newLCIFolder ;
  56. exit ;
  57. }
  58. }
  59. echo 'pt' ;
  60. exit ;
  61. }
  62. else if ($ext == 'php') {
  63. // PAGE --- rename the page and its LCI folder, and its partner in Includes
  64. $newFilePath = $dir . '/' . $newFileName . '.' . $ext ;
  65. if (False === @rename($filePath, $newFilePath)) {
  66. echo 'error:Failed to rename page: ' . $filePath . ' >>> ' . $newFilePath ;
  67. exit ;
  68. }
  69. if (False === @rename(getPreviewFileFromLive($filePath), getPreviewFileFromLive($newFilePath))) {
  70. echo 'error:Failed to rename includes version of page: ' . getPreviewFileFromLive($filePath) . ' >>> ' . getPreviewFileFromLive($newFilePath) ;
  71. exit ;
  72. }
  73. $lciRoot = getLCIRootFolderFromPagePath($filePath) ;
  74. $newLciRoot = getLCIRootFolderFromPagePath($newFilePath) ;
  75. if (False === @rename($lciRoot, $newLciRoot)) {
  76. echo 'error:Failed to rename page LCI folder: ' . $lciRoot . ' >>> ' . $newLciRoot ;
  77. exit ;
  78. }
  79. echo 'regular' ;
  80. exit ;
  81. }
  82. else if (isLCI($filePath)) {
  83. // CHILD INCLUDE --- rename it and its sibling
  84. // e.g. includes/page_cms_files/cms_preview/lci.ext
  85. $newFilePath = $dir . '/' . $newFileName . '.' . $ext ;
  86. if (False === @rename($filePath, $newFilePath)) {
  87. echo 'error:Failed to rename preview include: ' . $filePath ;
  88. exit ;
  89. }
  90. if (False === @rename(getLiveLCIFromPreview($filePath), getLiveLCIFromPreview($newFilePath))) {
  91. echo 'error:Failed to rename live include: ' . getLiveLCIFromPreview($filePath) . ' >>> ' . getLiveLCIFromPreview($newFilePath) ;
  92. exit ;
  93. }
  94. /* ALSO TRY TO RENAME ANY REFERENCES TO THE LCI IN THE PARENT PAGE */
  95. $parentPagePath = getParentPageFromLCI($filePath) ;
  96. $parentPageSource = file_get_contents($parentPagePath) ;
  97. $newParentPageSource = str_replace(basename($filePath), $newFileName . '.' . $ext, $parentPageSource) ;
  98. @file_put_contents($parentPagePath, $newParentPageSource) ;
  99. // Success
  100. echo 'regular' ;
  101. exit ;
  102. }
  103. else if (is_dir($filePath)) {
  104. // DIRECTORY --- rename it and its partner in Includes
  105. $newDirPath = dirname($filePath) . '/' . $newFileName ;
  106. if (False === @rename($filePath, $newDirPath)) {
  107. echo 'error:Failed to rename folder: ' . $filePath . ' >>> ' . $newDirPath ;
  108. exit ;
  109. }
  110. $includesDirPath = getPreviewFileFromLive($filePath) ;
  111. $newIncludesDirPath = getPreviewFileFromLive($newDirPath) ;
  112. if (False === @rename($includesDirPath, $newIncludesDirPath)) {
  113. echo 'error:Failed to rename includes folder: ' . $includesDirPath . ' >>> ' . $newIncludesDirPath ;
  114. exit ;
  115. }
  116. echo 'regular' ;
  117. exit ;
  118. }
  119. else {
  120. // FREE INCLUDE --- rename it and its partner in Includes
  121. $newFilePath = $dir . '/' . $newFileName . '.' . $ext ;
  122. if (False === @rename($filePath, $newFilePath)) {
  123. echo 'error:Failed to rename free include: ' . $filePath . ' >>> ' . $newFilePath ;
  124. exit ;
  125. }
  126. if (False === @rename(getPreviewFileFromLive($filePath), getPreviewFileFromLive($newFilePath))) {
  127. echo 'error:Failed to rename includes version of free include: ' . getPreviewFileFromLive($filePath) . ' >>> ' . getPreviewFileFromLive($newFilePath) ;
  128. exit ;
  129. }
  130. echo 'regular' ;
  131. exit ;
  132. }
  133. echo 'Not handled: ' . $filePath ;
  134. ?>