PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/cms113/general.php

http://cmsfromscratch.googlecode.com/
PHP | 318 lines | 211 code | 35 blank | 72 comment | 67 complexity | 81e0fb67daa1588397584965e8d0ed86 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. require ('cmsfns.php') ;
  20. global $mode ;
  21. global $whereWeAre ;
  22. global $pathToRoot ;
  23. $pathToRoot2 = (!strlen($pathToRoot)) ? '.' : $pathToRoot ;
  24. if (isSet($_GET['mode'])) {
  25. $mode = $_GET['mode'] ;
  26. }
  27. else {
  28. $mode = 'live' ;
  29. }
  30. $rootDir = realpath($pathToRoot2) ;
  31. $thisDir = realpath('.') ;
  32. if ($rootDir != $thisDir) {
  33. $whereWeAre = substr($thisDir, strlen($rootDir)+1) . '/' ;
  34. }
  35. else {
  36. $whereWeAre = '' ;
  37. }
  38. function put($putWhat, $justReturnValue = False, $historyInc = array()) {
  39. global $mode, $pathToRoot, $pathToRoot2, $whereWeAre ;
  40. $pageName = basename($_SERVER["SCRIPT_NAME"]) ;
  41. if (strpos($putWhat, '/')) {
  42. $putWhat = str_replace('SITEROOT/', '', $putWhat) ;
  43. $includeFileName = str_replace(array('<<','>>'), '', $putWhat) ;
  44. // FREE INCLUDE
  45. if ($mode == "preview") {
  46. $includeFilePath = $pathToRoot . CMSDIRNAME . '/includes/' . $includeFileName ;
  47. }
  48. else {
  49. $includeFilePath = $pathToRoot . $includeFileName ;
  50. }
  51. }
  52. else {
  53. // CHILD INCLUDE
  54. if ($mode == 'preview') {
  55. $includeFilePath = $pathToRoot . CMSDIRNAME . '/includes/' . $whereWeAre . stripFileExtension(basename($_SERVER["SCRIPT_NAME"])) . '_cms_files/cms_preview/' . $putWhat ;
  56. }
  57. else {
  58. $includeFilePath = $pathToRoot . CMSDIRNAME . '/includes/' . $whereWeAre . stripFileExtension(basename($_SERVER["SCRIPT_NAME"])) . '_cms_files/cms_live/' . $putWhat ;
  59. }
  60. }
  61. // Prevent circular includes!
  62. if (in_array($includeFilePath, $historyInc)) {
  63. echo '<!-- Warning: Loop caused in includes! -->' ;
  64. return false ;
  65. }
  66. else {
  67. array_push($historyInc, $includeFilePath) ;
  68. }
  69. /*
  70. If the file doesn't exist, try looking in the page's own directory (i.e. remove the /page_cms_files/cms_live|preview/ bit...
  71. */
  72. if (!file_exists($includeFilePath)) {
  73. $alternativePath = $pathToRoot . CMSDIRNAME . '/includes/' . $whereWeAre . basename($includeFilePath) ;
  74. if (file_exists($alternativePath)) {
  75. $includeFilePath = $alternativePath ;
  76. }
  77. }
  78. $fileContents = @file_get_contents($includeFilePath) ;
  79. if ($fileContents !== False) {
  80. switch (getFileExtension($includeFilePath)) {
  81. case "text" :
  82. $nestResult = nestIncludes($fileContents, $historyInc) ;
  83. $fileContents = $nestResult[0] ;
  84. $historyInc = $nestResult[1] ;
  85. if (!$justReturnValue) echo $fileContents ;
  86. else return $fileContents ;
  87. break ;
  88. case "html" :
  89. $nestResult = nestIncludes($fileContents, $historyInc) ;
  90. $fileContents = $nestResult[0] ;
  91. $historyInc = $nestResult[1] ;
  92. $fileContents = str_replace('"cmsimages/', '"' . CMSDIRNAME . '/cmsimages/', $fileContents) ; // May be redundant now?
  93. // $fileContents = str_replace('href="/' , 'href="' . $pathToRoot2 . '/', $fileContents) ; REDUNDANT
  94. $fileContents = convertSmartQuotes($fileContents) ;
  95. if (!$justReturnValue) echo $fileContents ;
  96. else return $fileContents ;
  97. break ;
  98. case "set" :
  99. if (!$justReturnValue) {
  100. renderSet($fileContents, False, $historyInc) ;
  101. }
  102. else {
  103. return renderSet($fileContents, True, $historyInc) ;
  104. }
  105. break ;
  106. case "phpx" :
  107. ob_start();
  108. include $includeFilePath ;
  109. $contents = ob_get_contents() ;
  110. ob_end_clean() ;
  111. echo $contents ;
  112. break ;
  113. }
  114. }
  115. else if ($mode == 'preview') {
  116. echo '<!-- Not found: ' . $includeFilePath . ' -->' ;
  117. }
  118. }
  119. function nestIncludes($fileContents, $historyInc) {
  120. // May be able to tidy up the code around including...
  121. $repeatIt = True ;
  122. while ($repeatIt == True) {
  123. $nextPutItem = getNextPut($fileContents) ;
  124. if (isSet($nextPutItem) && sizeOf($nextPutItem)>1) {
  125. // Replace the result of putting the new thing in place of the put() call
  126. $newPutResult = put("$nextPutItem[3].$nextPutItem[4]", True, $historyInc) ;
  127. // echo $newPutResult ;
  128. $fileContents = str_replace($nextPutItem[0], $newPutResult, $fileContents) ;
  129. array_push($historyInc, $nextPutItem[0]) ;
  130. }
  131. else {
  132. $repeatIt = False ;
  133. }
  134. }
  135. return array($fileContents, $historyInc) ;
  136. }
  137. function getNextPut($someHTML) {
  138. if (eregi("(<<|&lt;&lt;)([[:space:]]*)([-a-zA-Z0-9_\/]+).([a-zA-Z]{3,4})([[:space:]]*)(>>|&gt;&gt;)", $someHTML, $regs))
  139. return $regs ;
  140. }
  141. function convertSmartQuotes($string) {
  142. $search = array(chr(145),
  143. chr(146),
  144. chr(147),
  145. chr(148),
  146. chr(151));
  147. $replace = array("'",
  148. "'",
  149. '"',
  150. '"',
  151. '-');
  152. return str_replace($search, $replace, $string);
  153. }
  154. function renderSet($setContentsFileContents, $justReturnValue = False, $historyInc) {
  155. global $pathToRoot ;
  156. // Turn the string back into an array
  157. $setContents = unserialize($setContentsFileContents) ;
  158. $STPath = $pathToRoot . CMSDIRNAME . '/' . STDIR . $setContents[1] ;
  159. if (!file_exists($STPath)) return ;
  160. $templateContents = unserialize(file_get_contents($STPath)) ;
  161. // Item [2] now lists the column names and data types.. We use this to map the data to the right names.
  162. $columns = $setContents[2] ;
  163. $numCols = sizeOf($columns) ;
  164. $setContentsOutput = '' ;
  165. $contentsToShow = False ;
  166. // For each line in the Set
  167. reset($setContents) ;
  168. // var_dump($setContents) ;
  169. for ($item=3; $item<sizeOf($setContents); $item++) {
  170. // For each major repeated block
  171. for ($repeatedBlock=0; $repeatedBlock<sizeOf($templateContents['repeated']); $repeatedBlock++) {
  172. // Then for each of the 3 alternatives in the block
  173. for ($alternativeOption=0; $alternativeOption<sizeOf($templateContents['repeated'][$repeatedBlock]); $alternativeOption++) {
  174. $thisBlockStillOK = True ;
  175. $thisRepeatBlock = $templateContents['repeated'][$repeatedBlock][$alternativeOption] ;
  176. // Parse out the required column names in each repeat block
  177. $thisBlockRequiredCols = array() ;
  178. // !!! BUG here: Messes up adjacent includes!!
  179. // Need to be more specific about permitted characters, as currently needs a space between or runs all chars together
  180. // Note added regex assertions to exclude [[ ... ]]
  181. // Added - to allow dashes, and \s to include spaces in column names
  182. preg_match_all('/(?<!\[)\[\s*?([\w-\s]+)\s*?\](?!\])/', $thisRepeatBlock, $requiredColumnsFound, PREG_SET_ORDER) ;
  183. for ($rptItem=0; $rptItem<sizeOf($requiredColumnsFound); $rptItem++) {
  184. // $matches[$rptItem][1] returns the name of the required colum
  185. array_push($thisBlockRequiredCols, trim($requiredColumnsFound[$rptItem][1]) ) ;
  186. }
  187. /* var_dump($thisBlockRequiredCols) ;*/
  188. // For each required item
  189. for ($rqCol=0; $rqCol<sizeOf($thisBlockRequiredCols); $rqCol++) {
  190. $rqdColName = $thisBlockRequiredCols[$rqCol] ;
  191. // Get the position of the relevant column
  192. for ($iCol=0; $iCol<$numCols; $iCol++) {
  193. if ($columns[$iCol][0] == $rqdColName) {
  194. $rqdColPos = $iCol ;
  195. $isImage = ($columns[$iCol][1] == 'image') ? True : False ;
  196. $isLink = ($columns[$iCol][1] == 'link') ? True : False ;
  197. $isFile = ($columns[$iCol][1] == 'file') ? True : False ;
  198. $isLongtext = ($columns[$iCol][1] == 'longtext') ? True : False ;
  199. }
  200. }
  201. if (!isSet($isImage)) continue ;
  202. // Get value from corresponding position in Set line
  203. // If value exists, we're good
  204. $setContentsValue = @$setContents[$item][$rqdColPos] ;
  205. if ($isImage) {
  206. $setContentsValue = $pathToRoot . $setContentsValue ;
  207. }
  208. else if ($isLink && strpos($setContentsValue, '/') !== 0 && strpos($setContentsValue, 'http') !== 0) {
  209. $setContentsValue = $pathToRoot.$setContentsValue ;
  210. }
  211. else if ($isFile) {
  212. $setContentsValue = $pathToRoot . '/cmsfiles/' . $setContentsValue ;
  213. }
  214. if (!$setContentsValue || !strlen($setContentsValue)) {
  215. $thisBlockStillOK = False ;
  216. }
  217. else {
  218. /*
  219. Where the parameter fields are in the relevant array!!!
  220. The values we want are in $templateContents[n][2] and $templateContents[n][3] ;
  221. */
  222. /*
  223. NO THUMBNAILS YET...
  224. if ($columns[$rqCol][1] == 'thumbnail') {
  225. $param1 = False ;
  226. $param2 = False ;
  227. for ($tcItems=0; $tcItems<sizeOf($templateContents["cols"]); $tcItems++) {
  228. if ($templateContents["cols"][$tcItems][0] == $rqdColName) {
  229. $param1 = $templateContents["cols"][$tcItems][2] ;
  230. $param2 = $templateContents["cols"][$tcItems][3] ;
  231. }
  232. }
  233. $setContentsValue = 'cms/phpthumb.php?src=' . REALIMGDIR . '/' . $setContents[$item][$rqdColPos] ;
  234. if ($param1) {
  235. $setContentsValue .= '&w=' . $param1 ;
  236. }
  237. if ($param1) {
  238. $setContentsValue .= '&h=' . $param2 ;
  239. }
  240. }
  241. */
  242. // Change to regex?? Need to accommodate any number of spaces
  243. $thisRepeatBlock = str_replace('['. $rqdColName.']', $setContentsValue, stripslashes($thisRepeatBlock)) ;
  244. $thisRepeatBlock = str_replace('[ '. $rqdColName.' ]', $setContentsValue, $thisRepeatBlock) ;
  245. $thisRepeatBlock = str_replace('[[', '[', $thisRepeatBlock) ;
  246. $thisRepeatBlock = str_replace(']]', ']', $thisRepeatBlock) ;
  247. if ($isLongtext) $thisRepeatBlock = autop($thisRepeatBlock) ;
  248. }
  249. }
  250. if ($thisBlockStillOK) {
  251. $thisRepeatBlockArray = nestIncludes($thisRepeatBlock, $historyInc) ;
  252. $setContentsOutput .= $thisRepeatBlockArray[0] . "\n" ;
  253. $contentsToShow = True ;
  254. break ;
  255. }
  256. }
  257. }
  258. }
  259. if ($contentsToShow) {
  260. $beforeValArray = nestIncludes(stripslashes($templateContents['before']), $historyInc) ;
  261. $outputHTML = $beforeValArray[0] . "\n" ;
  262. $outputHTML .= stripslashes(stripslashes($setContentsOutput)) ;
  263. $afterValArray = nestIncludes(stripslashes($templateContents['after']), $historyInc) ;
  264. $outputHTML .= $afterValArray[0] . "\n" ;
  265. }
  266. else {
  267. $outputHTML = stripslashes($templateContents['else']) ;
  268. }
  269. if ($justReturnValue) return $outputHTML ;
  270. else echo $outputHTML ;
  271. }
  272. if ($mode == "preview") {
  273. if (!file_exists($pathToRoot . CMSDIRNAME . '/includes/' . $whereWeAre . basename($_SERVER["SCRIPT_NAME"]))) {
  274. echo "<strong>Error. No such file:</strong> " . $pathToRoot . CMSDIRNAME . '/includes/' . $whereWeAre . basename($_SERVER["SCRIPT_NAME"]) ;
  275. exit ;
  276. }
  277. include($pathToRoot . CMSDIRNAME . '/includes/' . $whereWeAre . basename($_SERVER["SCRIPT_NAME"])) ;
  278. exit ;
  279. }
  280. ?>