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