PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/package_J16/admin/plugin/jumi.php

http://jumi.googlecode.com/
PHP | 129 lines | 94 code | 16 blank | 19 comment | 27 complexity | 0563351d531b8611338fac3f3ca0d8cd MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Jumi
  5. * @copyright (C) 2008 - 2010 Martin Hajek, 2011 Edvard Ananyan
  6. * @license GNU/GPL v3 http://www.gnu.org/licenses/gpl.html
  7. */
  8. defined('_JEXEC') or die('Restricted access');
  9. // Import library dependencies
  10. jimport('joomla.plugin.plugin');
  11. jimport('joomla.event.plugin');
  12. class plgSystemJumi extends JPlugin {
  13. function __construct( &$subject ) {
  14. parent::__construct( $subject );
  15. // load plugin parameters and language file
  16. $this->_plugin = JPluginHelper::getPlugin( 'system', 'jumi' );
  17. $this->_params = json_decode( $this->_plugin->params );
  18. JPlugin::loadLanguage('plg_system_jumi', JPATH_ADMINISTRATOR);
  19. }
  20. function onAfterRender() {
  21. $mainframe = &JFactory::getApplication();
  22. if($mainframe->isAdmin())
  23. return;
  24. $plugin =& JPluginHelper::getPlugin('content', 'jumi');
  25. $pluginParams = json_decode( $plugin->params );
  26. $content = JResponse::getBody();
  27. //print_r($pluginParams);exit;
  28. // expression to search for
  29. $regex = '/{(jumi)\s*(.*?)}/i'; //BUG: des not work with written codes containing
  30. // if hide_code then replace jumi syntax codes with an empty string
  31. if ( $pluginParams->hide_code == 1 ) {
  32. $content = preg_replace( $regex, '', $content );
  33. return true;
  34. }
  35. $continuesearching = true;
  36. while ($continuesearching){ //Nesting loop
  37. // find all instances of $regex (i.e. jumi) in an article and put them in $matches
  38. $matches = array();
  39. $matches_found = preg_match_all( $regex, $content, $matches, PREG_SET_ORDER );
  40. if ($matches_found) {
  41. // cycle through all jumi instancies. Put text into $dummy[2]
  42. foreach ($matches as $dummy) {
  43. //read arguments contained in [] from $dummy[2] and put them into the array $jumi
  44. $mms=array();
  45. $jumi="";
  46. preg_match_all('/\[.*?\]/', $dummy[2], $mms);
  47. if ($mms) { //at the least one argument found
  48. foreach ($mms as $i=>$mm) {
  49. $jumi = preg_replace("/\[|]/", "", $mm);
  50. }
  51. }
  52. //Following syntax {jumi [storage_source][arg1]...[argN]}
  53. $storage_source = $this->getStorageSource(trim(array_shift($jumi)), $pluginParams->default_absolute_path); //filepathname or record id or ""
  54. $output = ''; // Jumi output
  55. if($storage_source == '') { //if nothing to show
  56. $output = '<div style="color:#FF0000;background:#FFFF00;">'.JText::_('ERROR_CONTENT').'</div>';
  57. } else { // buffer output
  58. ob_start();
  59. if(is_int($storage_source)){ //it is record id
  60. $code_stored = $this->getCodeStored($storage_source);
  61. if($code_stored != null){ //include custom script written
  62. eval ('?>'.$code_stored);
  63. } else {
  64. $output = '<div style="color:#FF0000;background:#FFFF00;">'.JText::sprintf('ERROR_RECORD', $storage_source).'</div>';
  65. }
  66. } else { //it is file
  67. if(is_readable($storage_source)) {
  68. include($storage_source); //include file
  69. } else {
  70. $output = '<div style="color:#FF0000;background:#FFFF00;">'.JText::sprintf('ERROR_FILE', $storage_source).'</div>';
  71. }
  72. }
  73. if ($output == ''){ //if there are no errors
  74. //$output = str_replace( '$' , '\$' , ob_get_contents()); fixed joomla bug
  75. $output = ob_get_contents();
  76. }
  77. ob_end_clean();
  78. }
  79. // final replacement of $regex (i.e. {jumi [][]}) in $article->text by $output
  80. $content = preg_replace($regex, $output, $content, 1);
  81. }
  82. if ($pluginParams->nested_replace == 0) {
  83. $continuesearching = false;
  84. }
  85. } else {
  86. $continuesearching = false;
  87. }
  88. }
  89. JResponse::setBody($content);
  90. }
  91. function getCodeStored($source) { //returns code stored in the database or null.
  92. $database = &JFactory::getDBO();
  93. //$user = &JFactory::getUser();
  94. //$database->setQuery("select custom_script from #__jumi where id = '{$source}' and access <= {$user->gid} and published = 1");
  95. $database->setQuery("select custom_script from #__jumi where id = $source");
  96. return $database->loadResult();
  97. }
  98. function getStorageSource($source, $abspath) { //returns filepathname or a record id or ""
  99. $storage=trim($source);
  100. if ($storage!=""){
  101. if ($id = substr(strchr($storage,"*"),1)) { //if record id return it
  102. return (int)$id;
  103. } else { // else return filepathname
  104. if($abspath == '')
  105. return $storage;
  106. else
  107. return $abspath.DS.$storage;
  108. }
  109. } else { // else return ""
  110. return '';
  111. }
  112. }
  113. }