PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/mambots/content/mosimage.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 254 lines | 158 code | 43 blank | 53 comment | 50 complexity | 0563ab97c87b8eb5d2e731dcaeec6047 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: mosimage.php 5939 2006-12-06 12:43:29Z predator $
  4. * @package Joomla
  5. * @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  7. * Joomla! is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13. // no direct access
  14. defined( '_VALID_MOS' ) or die( 'Restricted access' );
  15. $_MAMBOTS->registerFunction( 'onPrepareContent', 'botMosImage' );
  16. /**
  17. */
  18. function botMosImage( $published, &$row, &$params, $page=0 ) {
  19. global $database, $_MAMBOTS;
  20. // simple performance check to determine whether bot should process further
  21. if ( strpos( $row->text, 'mosimage' ) === false ) {
  22. return true;
  23. }
  24. // expression to search for
  25. $regex = '/{mosimage\s*.*?}/i';
  26. // check whether mosimage has been disabled for page
  27. // check whether mambot has been unpublished
  28. if (!$published || !$params->get( 'image' )) {
  29. $row->text = preg_replace( $regex, '', $row->text );
  30. return true;
  31. }
  32. //count how many {mosimage} are in introtext if it is set to hidden.
  33. $introCount=0;
  34. if ( ! $params->get( 'introtext' ) & ! $params->get( 'intro_only') )
  35. {
  36. preg_match_all( $regex, $row->introtext, $matches );
  37. $introCount = count ( $matches[0] );
  38. }
  39. // find all instances of mambot and put in $matches
  40. preg_match_all( $regex, $row->text, $matches );
  41. // Number of mambots
  42. $count = count( $matches[0] );
  43. // mambot only processes if there are any instances of the mambot in the text
  44. if ( $count ) {
  45. // check if param query has previously been processed
  46. if ( !isset($_MAMBOTS->_content_mambot_params['mosimage']) ) {
  47. // load mambot params info
  48. $query = "SELECT params"
  49. . "\n FROM #__mambots"
  50. . "\n WHERE element = 'mosimage'"
  51. . "\n AND folder = 'content'"
  52. ;
  53. $database->setQuery( $query );
  54. $database->loadObject($mambot);
  55. // save query to class variable
  56. $_MAMBOTS->_content_mambot_params['mosimage'] = $mambot;
  57. }
  58. // pull query data from class variable
  59. $mambot = $_MAMBOTS->_content_mambot_params['mosimage'];
  60. $botParams = new mosParameters( $mambot->params );
  61. $botParams->def( 'padding' );
  62. $botParams->def( 'margin' );
  63. $botParams->def( 'link', 0 );
  64. $images = processImages( $row, $botParams, $introCount );
  65. // store some vars in globals to access from the replacer
  66. $GLOBALS['botMosImageCount'] = 0;
  67. $GLOBALS['botMosImageParams'] =& $botParams;
  68. $GLOBALS['botMosImageArray'] =& $images;
  69. //$GLOBALS['botMosImageArray'] =& $combine;
  70. // perform the replacement
  71. $row->text = preg_replace_callback( $regex, 'botMosImage_replacer', $row->text );
  72. // clean up globals
  73. unset( $GLOBALS['botMosImageCount'] );
  74. unset( $GLOBALS['botMosImageMask'] );
  75. unset( $GLOBALS['botMosImageArray'] );
  76. unset( $GLOBALS['botJosIntroCount'] );
  77. return true;
  78. }
  79. }
  80. function processImages ( &$row, &$params, &$introCount ) {
  81. global $mosConfig_absolute_path, $mosConfig_live_site;
  82. $images = array();
  83. // split on \n the images fields into an array
  84. $row->images = explode( "\n", $row->images );
  85. $total = count( $row->images );
  86. $start = $introCount;
  87. for ( $i = $start; $i < $total; $i++ ) {
  88. $img = trim( $row->images[$i] );
  89. // split on pipe the attributes of the image
  90. if ( $img ) {
  91. $attrib = explode( '|', trim( $img ) );
  92. // $attrib[0] image name and path from /images/stories
  93. // $attrib[1] alignment
  94. if ( !isset($attrib[1]) || !$attrib[1] ) {
  95. $attrib[1] = '';
  96. }
  97. // $attrib[2] alt & title
  98. if ( !isset($attrib[2]) || !$attrib[2] ) {
  99. $attrib[2] = 'Image';
  100. } else {
  101. $attrib[2] = htmlspecialchars( $attrib[2] );
  102. }
  103. // $attrib[3] border
  104. if ( !isset($attrib[3]) || !$attrib[3] ) {
  105. $attrib[3] = 0;
  106. }
  107. // $attrib[4] caption
  108. if ( !isset($attrib[4]) || !$attrib[4] ) {
  109. $attrib[4] = '';
  110. $border = $attrib[3];
  111. } else {
  112. $border = 0;
  113. }
  114. // $attrib[5] caption position
  115. if ( !isset($attrib[5]) || !$attrib[5] ) {
  116. $attrib[5] = '';
  117. }
  118. // $attrib[6] caption alignment
  119. if ( !isset($attrib[6]) || !$attrib[6] ) {
  120. $attrib[6] = '';
  121. }
  122. // $attrib[7] width
  123. if ( !isset($attrib[7]) || !$attrib[7] ) {
  124. $attrib[7] = '';
  125. $width = '';
  126. } else {
  127. $width = ' width: '. $attrib[7] .'px;';
  128. }
  129. // image size attibutes
  130. $size = '';
  131. if ( function_exists( 'getimagesize' ) ) {
  132. $size = @getimagesize( $mosConfig_absolute_path .'/images/stories/'. $attrib[0] );
  133. if (is_array( $size )) {
  134. $size = ' width="'. $size[0] .'" height="'. $size[1] .'"';
  135. }
  136. }
  137. // assemble the <image> tag
  138. $image = '<img src="'. $mosConfig_live_site .'/images/stories/'. $attrib[0] .'"'. $size;
  139. // no aligment variable - if caption detected
  140. if ( !$attrib[4] ) {
  141. if ($attrib[1] == 'left' OR $attrib[1] == 'right') {
  142. $image .= ' style="float: '. $attrib[1] .';"';
  143. } else {
  144. $image .= $attrib[1] ? ' align="middle"' : '';
  145. }
  146. }
  147. $image .=' hspace="6" alt="'. $attrib[2] .'" title="'. $attrib[2] .'" border="'. $border .'" />';
  148. // assemble caption - if caption detected
  149. $caption = '';
  150. if ( $attrib[4] ) {
  151. $caption = '<div class="mosimage_caption"';
  152. if ( $attrib[6] ) {
  153. $caption .= ' style="text-align: '. $attrib[6] .';"';
  154. $caption .= ' align="'. $attrib[6] .'"';
  155. }
  156. $caption .= '>';
  157. $caption .= $attrib[4];
  158. $caption .= '</div>';
  159. }
  160. // final output
  161. if ( $attrib[4] ) {
  162. // initialize variables
  163. $margin = '';
  164. $padding = '';
  165. $float = '';
  166. $border_width = '';
  167. $style = '';
  168. if ( $params->def( 'margin' ) ) {
  169. $margin = ' margin: '. $params->def( 'margin' ).'px;';
  170. }
  171. if ( $params->def( 'padding' ) ) {
  172. $padding = ' padding: '. $params->def( 'padding' ).'px;';
  173. }
  174. if ( $attrib[1] ) {
  175. $float = ' float: '. $attrib[1] .';';
  176. }
  177. if ( $attrib[3] ) {
  178. $border_width = ' border-width: '. $attrib[3] .'px;';
  179. }
  180. if ( $params->def( 'margin' ) || $params->def( 'padding' ) || $attrib[1] || $attrib[3] ) {
  181. $style = ' style="'. $border_width . $float . $margin . $padding . $width .'"';
  182. }
  183. $img = '<div class="mosimage" '. $style .' align="center">';
  184. // display caption in top position
  185. if ( $attrib[5] == 'top' && $caption ) {
  186. $img .= $caption;
  187. }
  188. $img .= $image;
  189. // display caption in bottom position
  190. if ( $attrib[5] == 'bottom' && $caption ) {
  191. $img .= $caption;
  192. }
  193. $img .='</div>';
  194. } else {
  195. $img = $image;
  196. }
  197. $images[] = $img;
  198. }
  199. }
  200. return $images;
  201. }
  202. /**
  203. * Replaces the matched tags an image
  204. * @param array An array of matches (see preg_match_all)
  205. * @return string
  206. */
  207. function botMosImage_replacer( &$matches ) {
  208. $i = $GLOBALS['botMosImageCount']++;
  209. return @$GLOBALS['botMosImageArray'][$i];
  210. }
  211. ?>