PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/4.8/mambots/content/moscode.php

http://miacms.googlecode.com/
PHP | 80 lines | 38 code | 15 blank | 27 comment | 7 complexity | 9b592e823a269d815178109c4f4e4bd9 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-2.0
  1. <?php
  2. /**
  3. * @package MiaCMS
  4. * @author MiaCMS see README.php
  5. * @copyright see README.php
  6. * See COPYRIGHT.php for copyright notices and details.
  7. * @license GNU/GPL Version 2, see LICENSE.php
  8. * MiaCMS is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; version 2 of the License.
  11. */
  12. /** ensure this file is being included by a parent file */
  13. defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
  14. $_MAMBOTS->registerFunction( 'onPrepareContent', 'botMosCode' );
  15. /**
  16. * Code Highlighting Mambot
  17. *
  18. * <strong>Usage:</strong>
  19. * <code>{moscode}...some code...{/moscode}</code>
  20. */
  21. function botMosCode( $published, &$row, &$params, $page=0 ) {
  22. // define the regular expression for the bot
  23. $regex = "#{moscode}(.*?){/moscode}#s";
  24. if (is_callable(array($row, 'getText'))) $localtext = $row->getText();
  25. else $localtext = $row->text;
  26. if (!$published) {
  27. $localtext = preg_replace( $regex, '', $localtext );
  28. if (is_callable(array($row, 'saveText'))) $row->saveText($localtext);
  29. else $row->text = $localtext;
  30. return;
  31. }
  32. // perform the replacement
  33. $localtext = preg_replace_callback( $regex, 'botMosCode_replacer', $localtext );
  34. if (is_callable(array($row, 'saveText'))) $row->saveText($localtext);
  35. else $row->text = $localtext;
  36. return true;
  37. }
  38. /**
  39. * Replaces the matched tags an image
  40. * @param array An array of matches (see preg_match_all)
  41. * @return string
  42. */
  43. function botMosCode_replacer( &$matches ) {
  44. $html_entities_match = array("#<#", "#>#");
  45. $html_entities_replace = array("&lt;", "&gt;");
  46. $text = $matches[1];
  47. $text = preg_replace($html_entities_match, $html_entities_replace, $text );
  48. // Replace 2 spaces with "&nbsp; " so non-tabbed code indents without making huge long lines.
  49. $text = str_replace(" ", "&nbsp; ", $text);
  50. // now Replace 2 spaces with " &nbsp;" to catch odd #s of spaces.
  51. $text = str_replace(" ", " &nbsp;", $text);
  52. // Replace tabs with "&nbsp; &nbsp;" so tabbed code indents sorta right without making huge long lines.
  53. $text = str_replace("\t", "&nbsp; &nbsp;", $text);
  54. $text = str_replace('&lt;', '<', $text);
  55. $text = str_replace('&gt;', '>', $text);
  56. $text = highlight_string( $text, 1 );
  57. $text = str_replace('&amp;nbsp;', '&nbsp;', $text);
  58. $text = str_replace('&lt;br/&gt;', '<br />', $text);
  59. $text = str_replace('<font color="#007700">&lt;</font><font color="#0000BB">br</font><font color="#007700">/&gt;','<br />', $text);
  60. $text = str_replace('&amp;</font><font color="#0000CC">nbsp</font><font color="#006600">;', '&nbsp;', $text);
  61. $text = str_replace('&amp;</font><font color="#0000BB">nbsp</font><font color="#007700">;', '&nbsp;', $text);
  62. $text = str_replace('<font color="#007700">;&lt;</font><font color="#0000BB">br</font><font color="#007700">/&gt;','<br />', $text);
  63. return $text;
  64. }
  65. ?>