/package_J15/admin/plugin_system/jumi.php

http://jumi.googlecode.com/ · PHP · 187 lines · 156 code · 12 blank · 19 comment · 46 complexity · 6c83a78b38c4e6a0a4cab43ffbbf7a99 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: jumi.php 92 2009-02-15 17:08:02Z martin2hajek $
  4. * @package Joomla! 1.5.x, Jumi plugin
  5. * @copyright (c) 2009 Martin Hajek
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  7. *
  8. * Usage: {jumi stored_code_source}code_written{/jumi}
  9. */
  10. defined('_JEXEC') or die( "Direct Access Is Not Allowed" );
  11. // Import library dependencies
  12. jimport( 'joomla.plugin.plugin' );
  13. require_once( dirname( __FILE__ ).DS.'jumi'.DS.'class.jumicoder.php' );
  14. class plgSystemJumi extends JPlugin
  15. {
  16. var $regex = '%\{jumi\b[^}]?(\S*?)\}([\S\s]*?)\{/jumi\}%';
  17. var $debug;
  18. var $pluginParams;
  19. function plgSystemJumi( &$subject, $config ) //constuctor
  20. {
  21. global $mainframe, $_JUMI_ROOT;
  22. parent::__construct($subject, $config);
  23. //parent::__construct( $subject );
  24. $this->loadLanguage( );
  25. //$option = JRequest::getCmd( 'option' );
  26. JPlugin::loadLanguage('plg_system_jumi', JPATH_ADMINISTRATOR);
  27. JPlugin::loadLanguage('plg_system_jumi');
  28. $this->_plugin = JPluginHelper::getPlugin( 'system', 'jumi' );
  29. $this->pluginParams = new JParameter( $this->_plugin->params );
  30. $this->debug = $this->pluginParams->get( 'debug_mode');
  31. //Jumi root for files inclusion is GLOBAL for all Jumi extensions
  32. $_JUMI_ROOT = $this->pluginParams->def('jumi_root', JPATH_ROOT);
  33. }
  34. function onPrepareContent( &$article ) //Articles, Sections desc., Categories desc.
  35. {
  36. global $mainframe, $_JUMI_ROOT;
  37. $nested = $this->pluginParams->get('nested_replace');
  38. //Clear the Jumi code and syntax from the article in the frontend? If yes then clear and end
  39. $aagid = (!isset($article->usertype)) ? $this->getGroupIdFromType('Administrator') : $this->getGroupIdFromType($article->usertype); //for Sections and Categories desc article author is set to Admin by default
  40. if ($this->getClearing( $this->pluginParams->get( 'clear_code'), $aagid )) {
  41. $article->text = preg_replace( $this->regex, '', $article->text );
  42. return true;
  43. }
  44. $continuesearching = true; //Nesting loop. NO {jumi}{/jumi} in code_written please!
  45. while ($continuesearching){
  46. // find all instances of $regex (i.e. jumi syntax) in an article and put them in $result
  47. $result = array();
  48. $matches_found = preg_match_all( $this->regex, $article->text, $result, PREG_SET_ORDER );
  49. if ($matches_found) {
  50. for ($matchi = 0; $matchi < count($result); $matchi++) { //cycle through all jumi instancies.
  51. //Sewing code written and code stored together to output
  52. $storage_source = $this->getStorageSource(trim($result[$matchi][1])); //filepathname or record id or ""
  53. $code_written = $result[$matchi][2]; //raw code written or ""
  54. $output = $this->getOutput($code_written, $storage_source, $this->debug);
  55. //Final replacement of $regex (i.e. {jumi ...}...{/jumi}) in $article->text by eval $output
  56. ob_start();
  57. eval("?>".$output);
  58. $output = str_replace( '$' , '\$' , ob_get_contents()); //fixed joomla bug
  59. $output = str_replace( '\0' , '\\\\0' , ob_get_contents()); //fixed php bug. Not sure if there is no side effect of the fix.
  60. ob_end_clean();
  61. $article->text = preg_replace($this->regex, $output, $article->text, 1);
  62. }
  63. if ($nested == 0) {
  64. $continuesearching = false;
  65. }
  66. } else {
  67. $continuesearching = false;
  68. }
  69. }
  70. return true;
  71. }
  72. function onAfterDispatch() //Feeds
  73. {
  74. global $mainframe;
  75. $docu =& JFactory::getDocument();
  76. $docuType = $docu->getType(); //feed, html, pdf
  77. if ( $docuType == 'feed' && isset( $docu->items ) ) { // if feed then replace it with empty string
  78. for ( $i = 0; $i <= count( $docu->items ); $i++ ) {
  79. if ( isset( $docu->items[$i]->description ) ) {
  80. $docu->items[$i]->description = preg_replace( $this->regex, '', $docu->items[$i]->description, 1 );
  81. }
  82. }
  83. }
  84. }
  85. /////////////////////custom methods //////////////////////////////
  86. function getCodeStored($source)
  87. { //returns code stored in the database or null.
  88. $database = &JFactory::getDBO();
  89. $user = &JFactory::getUser();
  90. $database->setQuery("select custom_script from #__jumi where id = '{$source}' and access <= {$user->gid} and published = 1");
  91. //$database->setQuery("select custom_script from #__jumi where id = $source"); //all records, all users
  92. return $database->loadResult();
  93. }
  94. function getStorageSource($source)
  95. { //returns filepathname or a record id or ""
  96. global $_JUMI_ROOT;
  97. $storage=trim($source);
  98. if ($storage!=""){
  99. if ($id = substr(strchr($storage,"*"),1)) { //if record id return it
  100. return (int)$id;
  101. } else { // else return filepathname
  102. return $GLOBALS['_JUMI_ROOT'].DS.$storage;
  103. }
  104. } else { // else return ""
  105. return '';
  106. }
  107. }
  108. function getGroupIdFromType($type)
  109. { //returns user group id from its type or null
  110. $database =& JFactory::getDBO();
  111. $database->setQuery( 'SELECT id FROM #__core_acl_aro_groups WHERE name = "'.$type.'"' );
  112. return $database->loadResult();
  113. }
  114. function getOutput($code_written, $storage_source, $debug)
  115. { //returns Jumi $output
  116. $output = ''; // Jumi output
  117. if($code_written == '' && $storage_source == '') { //if nothing to show
  118. $output = ($debug == 0) ? '' : '<div style="color:#FF0000;background:#FFFF00;">'.JText::_('ERROR_CONTENT').'</div>';
  119. } else { // buffer code to $output
  120. if($code_written != ''){ //if code written
  121. $code_written = JumiCoder::cleanRubbish($code_written);
  122. $code_written = JumiCoder::decode($code_written, 0);
  123. $output .= $code_written; //include code written
  124. }
  125. if($storage_source != ''){ //if record id or filepathname
  126. if(is_int($storage_source)){ //if record id
  127. $code_stored = $this->getCodeStored($storage_source);
  128. if($code_stored != null){
  129. $output .= $code_stored; //include record
  130. } else {
  131. $output = ($debug == 0) ? '' : '<div style="color:#FF0000;background:#FFFF00;">'.JText::sprintf('ERROR_RECORD', $storage_source).'</div>';
  132. }
  133. } else { //if file
  134. if(is_readable($storage_source)) {
  135. $output .= file_get_contents($storage_source); //include file
  136. } else {
  137. $output = ($debug == 0) ? '' : '<div style="color:#FF0000;background:#FFFF00;">'.JText::sprintf('ERROR_FILE', $storage_source).'</div>';
  138. }
  139. }
  140. }
  141. }
  142. return $output;
  143. }
  144. function getClearing($clear_switch, $aagid)
  145. { //decides wheather clear (filter out) Jumi syntax from the article or not
  146. //aagid: article autor group id
  147. switch ($clear_switch) {
  148. case '0':
  149. $clearing = true;
  150. $config = JComponentHelper::getParams( 'com_content' );
  151. $filterGroups = $config->get( 'filter_groups' ); //$params->_registry[_default][data]->filter_groups;
  152. $filterType = $config->get( 'filter_type' ); //$params->_registry[_default][data]->filter_type;
  153. if ((is_array($filterGroups) && in_array( $aagid, $filterGroups )) || (!is_array($filterGroups) && $aagid == $filterGroups)) {
  154. if ($filterType == 'WL') {
  155. $clearing = false;
  156. }
  157. } else {
  158. if ($filterType != 'WL') {
  159. $clearing = false;
  160. }
  161. }
  162. break;
  163. case '1':
  164. $clearing = true;
  165. break;
  166. case '2':
  167. $clearing = false;
  168. break;
  169. default:
  170. $clearing = false;
  171. }
  172. return $clearing;
  173. }
  174. }
  175. ?>