PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/system/rokcandy_system.php

https://bitbucket.org/ericrlarson/com_biblestudy
PHP | 228 lines | 157 code | 45 blank | 26 comment | 43 complexity | 9efbcd2d8c28ff747c41cd5483a3f712 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * RokCandy Macros RokCandy Macro System Plugin
  4. *
  5. * @package Joomla
  6. * @subpackage RokCandy Macros
  7. * @copyright Copyright (C) 2009 RocketTheme. All rights reserved.
  8. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  9. * @author RocketTheme, LLC
  10. */
  11. // no direct access
  12. defined( '_JEXEC' ) or die( 'Restricted access' );
  13. require_once (JPATH_ADMINISTRATOR.DS.'components'.DS.'com_rokcandy'.DS.'helpers'.DS.'rokcandyhelper.php' );
  14. class plgSystemRokCandy_System extends JPlugin {
  15. var $_initialized = false;
  16. var $_instanceId = 0;
  17. var $_library;
  18. var $_debug;
  19. function plgSystemRokCandy_System(& $subject, $config) {
  20. parent :: __construct($subject, $config);
  21. $this->_instanceId = rand(1000, 9999);
  22. $this->_debug = JRequest::getVar('debug_rokcandy') == true ? true : false;
  23. }
  24. function onAfterRoute() {
  25. $this->_initialize();
  26. }
  27. function _PHP4() {
  28. if (version_compare( phpversion(), '5.0' ) < 0) {
  29. if (!$this->_initialized) {
  30. //Something is wrong with PHP4... let's repeat the work...
  31. $this->_instanceId = rand(1000, 9999);
  32. $this->_initialize();
  33. }
  34. return true;
  35. } else {
  36. return false;
  37. }
  38. }
  39. function _initialize() {
  40. if ($this->_initialized) {
  41. JError::raiseWarning( '1' , 'RokCandy instanceId=' . $this->_instanceId . ' was initialized already');
  42. return true;
  43. }
  44. $document = & JFactory :: getDocument();
  45. $doctype = $document->getType();
  46. $this->_library = RokCandyHelper::getMacros();
  47. }
  48. // Do BBCode replacements on the whole page
  49. function onAfterRender() {
  50. // don't run if disabled overrides are true
  51. if ($this->_shouldProcess()) return;
  52. $this->_PHP4();
  53. $document = & JFactory::getDocument();
  54. $doctype = $document->getType();
  55. if ($doctype == 'html') {
  56. $body = JResponse::getBody();
  57. if ($this->_replaceCode($body)) {
  58. JResponse::setBody($body);
  59. }
  60. }
  61. }
  62. //process on content items first
  63. function onPrepareContent( &$article, &$params, $limitstart=0)
  64. {
  65. // don't execute if contentPlugin disabled in system config
  66. $candy_params =& JComponentHelper::getParams('com_rokcandy');
  67. if ($candy_params->get("contentPlugin",1)==0) return;
  68. // don't run if disabled overrides are true
  69. if ($this->_shouldProcess()) return;
  70. $this->_PHP4();
  71. if ($this->_replaceCode($article->text)) {
  72. return $article->text;
  73. }
  74. }
  75. function _shouldProcess() {
  76. global $mainframe;
  77. $params =& JComponentHelper::getParams('com_rokcandy');
  78. //don't run if in edit mode and flag enabled
  79. if (JRequest::getCmd('task') == 'edit' && $params->get('editenabled',0) == 0) return true;
  80. // don't process if in list view:
  81. if (JRequest::getCmd('task') == 'list' && JRequest::getCmd('option') == "com_rokcandy") return true;
  82. //don't run in admin
  83. if ($mainframe->isAdmin() && $params->get('adminenabled',0)==0) return true;
  84. // process manual overrides
  85. $flag = false;
  86. $is_disabled = $params->get('disabled');
  87. if ($is_disabled != "") {
  88. $disabled_entries = explode ("\n",$params->get('disabled'));
  89. foreach ($disabled_entries as $entries) {
  90. $checks = explode ("&",$entries);
  91. if (count($checks) > 0) {
  92. $flag = true;
  93. foreach ($checks as $check) {
  94. $bits = explode ("=",$check);
  95. if ((count($bits) == 2) && ($bits[1] != "") && (JRequest::getVar($bits[0]) == $bits[1])) {
  96. $flag = true;
  97. }
  98. else {
  99. $flag = false;
  100. break;
  101. }
  102. }
  103. }
  104. if ($flag == true)
  105. return true;
  106. }
  107. }
  108. return $flag;
  109. }
  110. function _replaceCode(&$body) {
  111. foreach ($this->_library as $key => $val) {
  112. $script_tag_matches = array();
  113. $search = array();
  114. $replace = array();
  115. $tokens = array();
  116. // create a working body
  117. $working_body = $body;
  118. // remove the script tag contents from the working body
  119. $find_scipt_tag = '#(<script.*type="text/javascript"[^>]*>(?!<script)(.*)</script>)#iUs';
  120. preg_match_all ( $find_scipt_tag , $working_body , $script_tag_matches);
  121. foreach($script_tag_matches[2] as $scripttagbody) {
  122. if(!empty($scripttagbody)){
  123. $working_body = str_replace($scripttagbody,'',$working_body);
  124. }
  125. }
  126. // build the regexp for the tag
  127. $opentag = substr($key,0,strpos($key,']')+1);
  128. $partial_open_tag = substr($opentag,0,(strpos($opentag,' '))?strpos($opentag,' '):strpos($opentag,']'));
  129. $tokened_opentag = preg_replace('/\{([a-zA-Z0-9_]+)\}/', '(?P<$1>.*?)',$opentag);
  130. if (strpos($opentag,"/]")){
  131. $escaped_key = $this->_addEscapes($tokened_opentag);
  132. }
  133. else {
  134. $tag_contents = substr($key, strpos($key,']')+1, strrpos($key,'[') - (strpos($key,']')+1));
  135. $tokened_tag_contens = preg_replace('/\{([a-zA-Z0-9_]+)\}/', '(?P<$1>(?s:(?!'.$partial_open_tag.').)*?)',$tag_contents);
  136. $closetag = substr($key,strrpos($key,'['),strrpos($key,']')-strrpos($key,'[')+1);
  137. $escaped_key = $this->_addEscapes($tokened_opentag.$tokened_tag_contens.$closetag);
  138. }
  139. $final_tag_patern = "%".$escaped_key."%";
  140. // run the matching for the tag on the working body
  141. if ($this->_debug) var_dump ($final_tag_patern);
  142. preg_match_all($final_tag_patern, $working_body, $results);
  143. if (!empty($results[0])) {
  144. if ($this->_debug) var_dump ($results);
  145. $search = array_merge($search, $results[0]);
  146. foreach ($results as $k => $v) {
  147. if (!is_numeric($k)) {
  148. $tokens[] = $k;
  149. }
  150. }
  151. for($i=0;$i< count($results[0]);$i++) {
  152. $tmpval = $val;
  153. foreach ($tokens as $token) {
  154. $tmpval = str_replace("{".$token."}",$results[$token][$i],$tmpval);
  155. }
  156. $replace[] = $tmpval;
  157. }
  158. }
  159. // do actual replacement on the real body
  160. $body = str_replace($search,$replace,$body);
  161. }
  162. return true;
  163. }
  164. function _addEscapes($fullstring) {
  165. $fullstring = str_replace("\\","\\\\",$fullstring);
  166. $fullstring = str_replace("[","\[",$fullstring);
  167. $fullstring = str_replace("]","\\]",$fullstring);
  168. return $fullstring;
  169. }
  170. function _readIniFile($path, $library) {
  171. jimport( 'joomla.filesystem.file' );
  172. $content = JFile::read($path);
  173. $data = explode("\n",$content);
  174. foreach ($data as $line) {
  175. //skip comments
  176. if (strpos($line,"#")!==0 and trim($line)!="" ) {
  177. $div = strpos($line,"]=");
  178. $library[substr($line,0,$div+1)] = substr($line,$div+2);
  179. }
  180. }
  181. return $library;
  182. }
  183. }