PageRenderTime 31ms CodeModel.GetById 16ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

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