PageRenderTime 3808ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/sources/contentcodes.class.php

https://github.com/pijulius/jcore
PHP | 316 lines | 225 code | 83 blank | 8 comment | 57 complexity | 43cb8430bb9d9f2c45d471b2723cfd55 MD5 | raw file
  1. <?php
  2. /***************************************************************************
  3. * contentcodes.class.php
  4. *
  5. * Jul 05, 07:00:00 2009
  6. * Copyright 2009 Istvan Petres (aka P.I.Julius)
  7. * me@pijulius.com
  8. * For licensing, see LICENSE or http://jcore.net/license
  9. ****************************************************************************/
  10. define('NOW', date('Y-m-d H:i:s'));
  11. define('NOW_DATE', date('Y-m-d'));
  12. define('NOW_TIME', date('H:i:s'));
  13. define('NOW_YEAR', date('Y'));
  14. define('NOW_MONTH', date('m'));
  15. define('NOW_DAY', date('d'));
  16. define('CURRENT_URL', url::get());
  17. define('REMOTE_ADDR', (string)$_SERVER['REMOTE_ADDR']);
  18. include_once('lib/calendar.class.php');
  19. class _contentCodes {
  20. var $fixParagraph = false;
  21. var $contentLimit = 0;
  22. var $ignoreCodes = null;
  23. function run($code, $arguments) {
  24. $handled = api::callHooks(API_HOOK_BEFORE,
  25. 'contentCodes::run', $this, $code, $arguments);
  26. if (isset($handled)) {
  27. api::callHooks(API_HOOK_AFTER,
  28. 'contentCodes::run', $this, $code, $arguments, $handled);
  29. return $handled;
  30. }
  31. switch($code) {
  32. case 'translate':
  33. if ($this->ignoreCodes && in_array('translate', $this->ignoreCodes))
  34. break;
  35. echo __($arguments);
  36. break;
  37. case 'random':
  38. if ($this->ignoreCodes && in_array('random', $this->ignoreCodes))
  39. break;
  40. echo contentCodes::random($arguments);
  41. break;
  42. case 'variables':
  43. if ($this->ignoreCodes && in_array('variables', $this->ignoreCodes))
  44. break;
  45. echo contentCodes::variables($arguments);
  46. break;
  47. case 'calendar':
  48. if ($this->ignoreCodes && in_array('calendar', $this->ignoreCodes))
  49. break;
  50. $calendar = new calendar();
  51. $calendar->arguments = $arguments;
  52. $calendar->display();
  53. unset($calendar);
  54. break;
  55. case 'url':
  56. if ($this->ignoreCodes && in_array('url', $this->ignoreCodes))
  57. break;
  58. $url = new url();
  59. $url->arguments = $arguments;
  60. $url->display();
  61. unset($url);
  62. break;
  63. case 'languages':
  64. if ($this->ignoreCodes && in_array('languages', $this->ignoreCodes))
  65. break;
  66. $languages = new languages();
  67. $languages->arguments = $arguments;
  68. $languages->display();
  69. unset($languages);
  70. break;
  71. case 'menus':
  72. if ($this->ignoreCodes && in_array('menus', $this->ignoreCodes))
  73. break;
  74. $menus = new menus();
  75. $menus->arguments = $arguments;
  76. $menus->display();
  77. unset($menus);
  78. break;
  79. case 'pages':
  80. if ($this->ignoreCodes && in_array('pages', $this->ignoreCodes))
  81. break;
  82. $pages = new pages();
  83. $pages->arguments = $arguments;
  84. $pages->display();
  85. unset($pages);
  86. break;
  87. case 'posts':
  88. if ($this->ignoreCodes && in_array('posts', $this->ignoreCodes))
  89. break;
  90. $posts = new posts();
  91. $posts->arguments = $arguments;
  92. if ($this->contentLimit)
  93. $posts->limit = $this->contentLimit;
  94. $posts->display();
  95. unset($posts);
  96. break;
  97. case 'blocks':
  98. if ($this->ignoreCodes && in_array('blocks', $this->ignoreCodes))
  99. break;
  100. $blocks = new blocks();
  101. $blocks->arguments = $arguments;
  102. $blocks->display();
  103. unset($blocks);
  104. break;
  105. case 'modules':
  106. if ($this->ignoreCodes && in_array('modules', $this->ignoreCodes))
  107. break;
  108. preg_match('/(.*?)(\/|$)(.*)/', $arguments, $matches);
  109. $modules = new modules();
  110. if (!isset(modules::$loaded[$matches[1]]) || !modules::$loaded[$matches[1]])
  111. break;
  112. $modulename = new $matches[1]();
  113. $modulename->arguments = $matches[3];
  114. if ($this->contentLimit)
  115. $modulename->limit = $this->contentLimit;
  116. $modulename->display();
  117. unset($modulename);
  118. unset($modules);
  119. break;
  120. case 'forms':
  121. if ($this->ignoreCodes && in_array('forms', $this->ignoreCodes))
  122. break;
  123. $form = new dynamicForms($arguments);
  124. $form->load();
  125. $form->verify();
  126. $form->display();
  127. unset($form);
  128. break;
  129. default:
  130. break;
  131. }
  132. api::callHooks(API_HOOK_AFTER,
  133. 'contentCodes::run', $this, $code, $arguments);
  134. }
  135. static function random($arguments) {
  136. preg_match('/(.*?)\/(.*)/', $arguments, $matches);
  137. $start = 0;
  138. $end = 1000;
  139. if (isset($matches[1]))
  140. $start = (int)$matches[1];
  141. if (isset($matches[2]))
  142. $end = (int)$matches[2];
  143. return rand($start, $end);
  144. }
  145. static function variables($arguments) {
  146. $expargs = explode('/', $arguments);
  147. if (!isset($expargs[0]))
  148. return null;
  149. $postget = strtoupper($expargs[0]);
  150. if (!in_array($postget, array('POST', 'GET')))
  151. return null;
  152. $variable = $GLOBALS['_'.$postget];
  153. foreach($expargs as $key => $exparg) {
  154. if ($key < 1)
  155. continue;
  156. if (!isset($variable[$exparg])) {
  157. $variable = null;
  158. break;
  159. }
  160. $variable = strip_tags((string)$variable[$exparg]);
  161. }
  162. return (string)$variable;
  163. }
  164. static function replaceDefinitions(&$content) {
  165. $content = preg_replace_callback('/%([A-Z0-9-_]+?)%/',
  166. array('contentCodes', 'displayDefinitions'),
  167. $content);
  168. }
  169. static function displayDefinitions($constant) {
  170. if (!$constant)
  171. return null;
  172. $handled = api::callHooks(API_HOOK_BEFORE,
  173. 'contentCodes::displayDefinitions', $_ENV, $constant);
  174. if (isset($handled)) {
  175. api::callHooks(API_HOOK_AFTER,
  176. 'contentCodes::displayDefinitions', $_ENV, $constant, $handled);
  177. return $handled;
  178. }
  179. if (is_array($constant))
  180. $constant = $constant[1];
  181. if ($constant == 'SITE_URL')
  182. $result = url::site();
  183. else if ($constant == 'JCORE_URL')
  184. $result = url::jCore();
  185. else if ($constant == 'SECURITY_TOKEN')
  186. $result = security::genToken();
  187. else if (!defined($constant))
  188. $result = null;
  189. else if (in_array($constant, array(
  190. 'SQL_HOST',
  191. 'SQL_DATABASE',
  192. 'SQL_USER',
  193. 'SQL_PASS',
  194. 'SITE_PATH',
  195. 'JCORE_PATH')))
  196. $result = null;
  197. else
  198. $result = constant($constant);
  199. api::callHooks(API_HOOK_AFTER,
  200. 'contentCodes::displayDefinitions', $_ENV, $constant, $result);
  201. return $result;
  202. }
  203. function display($content) {
  204. $handled = api::callHooks(API_HOOK_BEFORE,
  205. 'contentCodes::display', $this, $content);
  206. if (isset($handled)) {
  207. api::callHooks(API_HOOK_AFTER,
  208. 'contentCodes::display', $this, $content, $handled);
  209. return $handled;
  210. }
  211. contentCodes::replaceDefinitions($content);
  212. preg_match_all('/(<p>[^>]+)?\{([a-zA-Z0-9\-\_]+?)\}(.*?)\{\/\2\}/', $content, $matches);
  213. if (!isset($matches[2]) || !count($matches[2])) {
  214. echo $content;
  215. api::callHooks(API_HOOK_AFTER,
  216. 'contentCodes::display', $this, $content);
  217. return;
  218. }
  219. $contents = preg_split('/\{([a-zA-Z0-9\-\_]+?)\}(.*?)\{\/\1\}/is', $content);
  220. foreach($matches[2] as $key => $code) {
  221. echo $contents[$key];
  222. if ($this->fixParagraph && $matches[1][$key])
  223. echo "</p>";
  224. $this->run(strtolower($code), $matches[3][$key]);
  225. if ($this->fixParagraph && $matches[1][$key])
  226. echo "<p>";
  227. }
  228. echo $contents[count($contents)-1];
  229. api::callHooks(API_HOOK_AFTER,
  230. 'contentCodes::display', $this, $content);
  231. }
  232. }
  233. ?>