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

/framework/classes/nos.php

https://github.com/jay3/core
PHP | 252 lines | 174 code | 29 blank | 49 comment | 31 complexity | 9ada144d68b9cc057d3aa33d2efd0e50 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * NOVIUS OS - Web OS for digital communication
  4. *
  5. * @copyright 2011 Novius
  6. * @license GNU Affero General Public License v3 or (at your option) any later version
  7. * http://www.gnu.org/licenses/agpl-3.0.html
  8. * @link http://www.novius-os.org
  9. */
  10. namespace Nos;
  11. class Nos
  12. {
  13. /**
  14. * @var string constant used for when entry point is back-office
  15. */
  16. const ENTRY_POINT_ADMIN = 'admin';
  17. /**
  18. * @var string constant used for when entry point is front-office
  19. */
  20. const ENTRY_POINT_FRONT = 'front';
  21. /**
  22. * @var string constant used for when entry point is 404
  23. */
  24. const ENTRY_POINT_404 = '404';
  25. /**
  26. * @var string constant used for when entry point is install
  27. */
  28. const ENTRY_POINT_INSTALL = 'install';
  29. /**
  30. * @var string The Novius OS entry point
  31. */
  32. public static $entry_point = NOS_ENTRY_POINT;
  33. /**
  34. * Returns the controller instance from the main request
  35. *
  36. * @return Controller | Controller_Front
  37. */
  38. public static function main_controller()
  39. {
  40. return \Request::main()->controller_instance;
  41. }
  42. /**
  43. * Execute a HMVC request
  44. *
  45. * @param string $where Route for the request
  46. * @param array $args The method parameters
  47. * @throws \Exception|NotFoundException
  48. * @throws \Exception|FrontIgnoreTemplateException
  49. * @return string
  50. */
  51. public static function hmvc($where, $args = null)
  52. {
  53. \Fuel::$profiling && \Profiler::console("HMVC $where");
  54. if (empty($args['args'])) {
  55. $args['args'] = array();
  56. }
  57. ob_start();
  58. try {
  59. $request = \Request::forge($where);
  60. $response = $request->execute($args['args']);
  61. echo $response;
  62. } catch (\Nos\FrontIgnoreTemplateException $e) {
  63. throw $e;
  64. } catch (\Nos\NotFoundException $e) {
  65. throw $e;
  66. } catch (\Exception $e) {
  67. if (\Fuel::$env == \Fuel::DEVELOPMENT) {
  68. \Debug::dump('Error when executing HMVC request "'.$where.'"');
  69. $old_continue_on = \Config::get('errors.continue_on', array());
  70. $continue_on = $old_continue_on;
  71. $continue_on[] = $e->getCode();
  72. \Config::set('errors.continue_on', $continue_on);
  73. \Error::show_php_error($e);
  74. $backtrace = \Debug::local_backtrace(true);
  75. $profiled = array();
  76. for ($i = 0; $i < count($backtrace); $i++) {
  77. $profiled[] = $backtrace[$i]['file'].'@'.$backtrace[$i]['line'];
  78. }
  79. \Debug::dump($profiled);
  80. \Config::set('errors.continue_on', $old_continue_on);
  81. }
  82. \Log::exception($e, 'HMVC - ');
  83. \Fuel::$profiling && \Console::logError($e, "HMVC request '$where' failed.");
  84. }
  85. $content = ob_get_clean();
  86. return $content;
  87. }
  88. /**
  89. * Parse a wyiswyg
  90. *
  91. * @param string $content Wysiwyg content to parse
  92. * @return string
  93. */
  94. public static function parse_wysiwyg($content)
  95. {
  96. static::_parse_enhancers($content);
  97. static::_parse_medias($content);
  98. static::_parse_internals($content);
  99. if (NOS_ENTRY_POINT === Nos::ENTRY_POINT_FRONT) {
  100. $content = preg_replace(
  101. '`href="#([^#"])`iUu',
  102. 'href="'.\Nos\Tools_Url::encodePath(static::main_controller()->getUrl()).(!empty($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : '').'#\\1',
  103. $content
  104. );
  105. }
  106. $content = str_replace(
  107. 'href="##',
  108. 'href="#',
  109. $content
  110. );
  111. \Event::trigger_function('front.parse_wysiwyg', array(&$content));
  112. return $content;
  113. }
  114. protected static function _parse_enhancers(&$content)
  115. {
  116. // Fetch the available functions
  117. \Nos\Config_Data::load('enhancers');
  118. \Fuel::$profiling && \Profiler::mark('Recherche des fonctions dans la page');
  119. $callback = array(get_called_class(), 'get_enhancer_content');
  120. static::parse_enhancers(
  121. $content,
  122. function ($enhancer, $config, $tag) use (&$content, $callback) {
  123. $function_content = call_user_func($callback, $enhancer, $config);
  124. $content = str_replace($tag, $function_content, $content);
  125. }
  126. );
  127. }
  128. public static function parse_enhancers($content, $closure)
  129. {
  130. preg_match_all('`<(\w+)\s[^>]*data-enhancer=[^>]*>.*?</\\1>`u', $content, $matches);
  131. foreach ($matches[0] as $enhancer_content) {
  132. if (preg_match_all('`data-enhancer="([^"]+)"`u', $enhancer_content, $matches2)) {
  133. $enhancer = $matches2[1][0];
  134. } elseif (preg_match_all('`data-enhancer=\'([^\']+)\'`u', $enhancer_content, $matches2)) {
  135. $enhancer = $matches2[1][0];
  136. }
  137. if (preg_match_all('`data-config="([^"]+)"`u', $enhancer_content, $matches2)) {
  138. $config = $matches2[1][0];
  139. } elseif (preg_match_all('`data-config=\'([^\']+)\'`u', $enhancer_content, $matches2)) {
  140. $config = $matches2[1][0];
  141. }
  142. if (!empty($enhancer) && !empty($config)) {
  143. $closure($enhancer, $config, $enhancer_content);
  144. }
  145. }
  146. }
  147. public static function get_enhancer_content($enhancer, $args)
  148. {
  149. $args = json_decode(
  150. htmlspecialchars_decode(
  151. $args
  152. ),
  153. true
  154. );
  155. $config = \Nos\Config_Data::get('enhancers.'.$enhancer, false);
  156. $found = $config !== false;
  157. false && \Fuel::$profiling && \Profiler::console(
  158. array(
  159. 'enhancer' => $enhancer,
  160. )
  161. );
  162. if ($found) {
  163. $function_content = self::hmvc(
  164. (!empty($config['urlEnhancer']) ? $config['urlEnhancer'] : $config['enhancer']),
  165. array(
  166. 'args' => array($args),
  167. )
  168. );
  169. if (empty($function_content) && \Fuel::$env == \Fuel::DEVELOPMENT) {
  170. $function_content = 'Enhancer '.$enhancer.' ('.$config['enhancer'].') returned empty content.';
  171. }
  172. } else {
  173. $function_content = \Fuel::$env == \Fuel::DEVELOPMENT ? 'Enhancer '.$enhancer.' not found.' : '';
  174. \Fuel::$profiling && \Console::logError(new \Exception(), 'Enhancer'.$enhancer.' not found.');
  175. }
  176. return $function_content;
  177. }
  178. protected static function _parse_medias(&$content)
  179. {
  180. Tools_Wysiwyg::parse_medias(
  181. $content,
  182. function ($media, $params) use (&$content) {
  183. if (empty($media)) {
  184. if ($params['tag'] == 'img') {
  185. // Remove dead images
  186. $content = str_replace($params['content'], '', $content);
  187. } elseif ($params['tag'] == 'a') {
  188. // Remove href for links (they become anchor)?
  189. // http://stackoverflow.com/questions/11144653/a-script-links-without-href
  190. //$content = str_replace('href="'.$params['url'].'"', '', $content);
  191. }
  192. } else {
  193. if (!empty($params['height'])) {
  194. $media_url = $media->urlResized($params['width'], $params['height']);
  195. } else {
  196. $media_url = $media->url();
  197. }
  198. $new_content = preg_replace('`'.preg_quote($params['url'], '`').'(?!\d)`u', Tools_Url::encodePath($media_url), $params['content']);
  199. $content = str_replace($params['content'], $new_content, $content);
  200. }
  201. }
  202. );
  203. }
  204. protected static function _parse_internals(&$content)
  205. {
  206. // Replace internal links
  207. preg_match_all('`nos://page/(\d+)`u', $content, $matches);
  208. if (!empty($matches[0])) {
  209. $page_ids = array();
  210. foreach ($matches[1] as $match_id => $page_id) {
  211. $page_ids[] = $page_id;
  212. }
  213. $pages = \Nos\Page\Model_Page::find('all', array('where' => array(array('page_id', 'IN', $page_ids))));
  214. foreach ($matches[1] as $match_id => $page_id) {
  215. if (isset($pages[$page_id])) {
  216. $content = preg_replace('`'.preg_quote($matches[0][$match_id], '`').'(?!\d)`u', Tools_Url::encodePath($pages[$page_id]->url()), $content);
  217. } else {
  218. $content = str_replace('href="'.$matches[0][$match_id].'"', '', $content);
  219. }
  220. }
  221. }
  222. }
  223. }