PageRenderTime 44ms CodeModel.GetById 22ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/package_J10/admin/plugin/plugin_jumi.php

http://jumi.googlecode.com/
PHP | 109 lines | 82 code | 10 blank | 17 comment | 27 complexity | 62271dd97b0ca47aa1fffaa62530971a MD5 | raw file
  1<?php
  2/**
  3* @version $Id: plugin_jumi.php 87 2009-02-12 11:22:33Z edo888 $
  4* @package Joomla! 1.0.x, Jumi plugin
  5* @copyright (C) 2006 - 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*/
 10defined( '_VALID_MOS' ) or die( 'Restricted access' );
 11$_MAMBOTS->registerFunction( 'onPrepareContent', 'plugJumi' );
 12
 13function plugJumi( $published, &$row, &$params, $page=0  ) {
 14    global $mainframe, $mosConfig_absolute_path, $database;
 15
 16    //get plugin parameters
 17    $query = "SELECT id FROM #__mambots WHERE element = 'plugin_jumi' AND folder = 'content'";
 18    $database->setQuery( $query );
 19    $id = $database->loadResult();
 20    $mambot = new mosMambot( $database );
 21    $mambot->load( $id );
 22    $pluginParams =& new mosParameters( $mambot->params );
 23
 24    // expression to search for
 25    $regex = '/{(jumi)\s*(.*?)}/i';
 26
 27    // if clear_code then replace jumi syntax codes with an empty string
 28    if ($pluginParams->get( 'clear_code') == 1 ) {
 29        $row->text = preg_replace( $regex, '', $row->text );
 30        return;
 31    }
 32
 33    // find all instances of mambot and put in $matches
 34    $matches = array();
 35    preg_match_all( $regex, $row->text, $matches, PREG_SET_ORDER );
 36
 37    $continuesearching = true;
 38  while ($continuesearching){  //Nesting loop
 39
 40        // find all instances of $regex (i.e. jumi) in an article and put them in $matches
 41        $matches = array();
 42        $matches_found = preg_match_all( $regex, $row->text, $matches, PREG_SET_ORDER );
 43        if ($matches_found) {
 44            // cycle through all jumi instancies. Put text into $dummy[2]
 45            foreach ($matches as $dummy) {
 46                //read arguments contained in [] from $dummy[2] and put them into the array $jumi
 47                $mms=array();
 48                $jumi="";
 49                preg_match_all('/\[.*?\]/', $dummy[2], $mms);
 50                if ($mms) { //at the least one argument found
 51                    foreach ($mms as $i=>$mm) {
 52                        $jumi = preg_replace("/\[|]/", "", $mm);
 53                    }
 54                }
 55
 56            //Following syntax {jumi [storage_source][arg1]...[argN]}
 57            $storage_source = trim(array_shift($jumi)); //filepathname or record id or ""
 58                if ($storage_source!=""){
 59                    if ($id = substr(strchr($storage_source,"*"),1)) { //if record id return it
 60                        $storage_source = (int)$id;
 61                    } else { // else return filepathname
 62                        $storage_source = $pluginParams->def('default_absolute_path',$mosConfig_absolute_path).'/'.$storage_source;
 63                    }
 64                }
 65                $output = ''; // Jumi output
 66                $debug  = $pluginParams->def('debug_mode', '0');
 67
 68                if($storage_source == '') { //if nothing to show
 69                    $output = ($debug == 0) ? 'dbgerr' : '<div style="color:#FF0000;background:#FFFF00;">Jumi is working but there is <b>nothing to be shown</b>.<br />Specify the source of the code (first square brackets)</div>';
 70                } else { // buffer output
 71                    ob_start();
 72                    if(is_int($storage_source)){ //it is record id
 73                        $user = $mainframe->getUser();
 74                        $database->setQuery("select custom_script from #__jumi where id = '{$storage_source}' and access <= {$user->gid} and published = 1");
 75                        $code_stored = $database->loadResult(); //returns code stored in the database or null.
 76                if($code_stored != null){ //include custom script written
 77                            eval ('?>'.$code_stored);
 78                } else {
 79                            $output = ($debug == 0) ? 'dbgerr' : '<div style="color:#FF0000;background:#FFFF00;">ERR: Record ID:<b>'.$storage_source.'</b> does not exist, or is not published, or you have got insufficient rights to read it!</div>';
 80                }
 81            } else { //it is file
 82                if(is_readable($storage_source)) {
 83                            include($storage_source); //include file
 84                } else {
 85                            $output = ($debug == 0) ? 'dbgerr' : '<div style="color:#FF0000;background:#FFFF00;">ERR: The file<br /><b>'.$storage_source.'</b><br />does not exist or is not readable!</div>';
 86                }
 87                    }
 88            if ($output == ''){ //if there are no errors
 89                $output = str_replace( '$' , '\$' , ob_get_contents()); //fixed joomla bug
 90                $output = ob_get_contents();
 91            } elseif ($output == 'dbgerr'){
 92                    $output = '';
 93              }
 94                ob_end_clean();
 95            }
 96
 97            // final replacement of $regex (i.e. {jumi [][]}) in $row->text by $output
 98                $row->text = preg_replace($regex, $output, $row->text, 1);
 99            }
100            if ($pluginParams->get('nested_replace') == 0) {
101                $continuesearching = false;
102            }
103        } else {
104          $continuesearching = false;
105        }
106    }
107    return true;
108}
109?>