[ Index ]

PHP Cross Reference of Mambo 4.6.5

[ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/mambots/content/ -> mosimage.php (source)

   1  <?php
   2  /**
   3  * @package Mambo
   4  * @author Mambo Foundation Inc see README.php
   5  * @copyright (C) 2000 - 2009 Mambo Foundation Inc.
   6  * See COPYRIGHT.php for copyright notices and details.
   7  * @license GNU/GPL Version 2, see LICENSE.php
   8  *
   9  * Redistributions of files must retain the above copyright notice.
  10  *
  11  * Mambo is free software; you can redistribute it and/or
  12  * modify it under the terms of the GNU General Public License
  13  * as published by the Free Software Foundation; version 2 of the License.
  14  */
  15  
  16  /** ensure this file is being included by a parent file */
  17  defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
  18  
  19  $_MAMBOTS->registerFunction( 'onPrepareContent', 'botMosImage' );
  20  
  21  /**
  22  */
  23  function botMosImage( $published, &$row, &$cparams, $page=0, $params ) {
  24      global $database;
  25      
  26       // expression to search for
  27      $regex = '/{mosimage\s*.*?}/i';
  28        
  29      // find all instances of mambot and put in $matches
  30      if (is_callable(array($row, 'getText'))) $localtext = $row->getText();
  31      else $localtext = $row->text;
  32      preg_match_all( $regex, $localtext, $matches );
  33      
  34       // Number of mambots
  35      $count = count( $matches[0] );
  36  
  37       // mambot only processes if there are any instances of the mambot in the text
  38       if ( $count ) {
  39       
  40          // load mambot params info
  41          
  42          /*$query = "SELECT id FROM #__mambots WHERE element = 'mosimage' AND folder = 'mosimage'";
  43          $database->setQuery( $query );
  44          $id     = $database->loadResult();
  45          $mambot = new mosMambot( $database );
  46          $mambot->load( $id );*/
  47          $mambots =& mosMambotHandler::getInstance();
  48          $mambot = $mambots->getBot('mosimage','content');
  49          $params =& new mosParameters( (isset($mambot->params)?$mambot->params:'')  );
  50      
  51           $params->def( 'padding' );
  52           $params->def( 'margin' );
  53           $params->def( 'link', 0 );
  54  
  55           $images     = processImages( $row, $params, $cparams );
  56          
  57          // store some vars in globals to access from the replacer
  58          $GLOBALS['botMosImageCount']     = 0;
  59          $GLOBALS['botMosImageParams']     =& $params;
  60          $GLOBALS['botMosImageArray']     =& $images;
  61          //$GLOBALS['botMosImageArray']     =& $combine;
  62      
  63          // perform the replacement
  64          $localtext = preg_replace_callback( $regex, 'botMosImage_replacer', $localtext );
  65          if (is_callable(array($row, 'saveText'))) $row->saveText($localtext);
  66          else $row->text = $localtext;
  67      
  68          // clean up globals
  69          unset( $GLOBALS['botMosImageCount'] );
  70          unset( $GLOBALS['botMosImageMask'] );
  71          unset( $GLOBALS['botMosImageArray'] );
  72      
  73          return true;
  74      }     
  75  }
  76  
  77  function processImages ( &$row, &$params, &$cparams ) {
  78      global $mosConfig_absolute_path, $mosConfig_live_site;
  79      
  80      $images         = array();
  81  
  82      // split on \n the images fields into an array
  83      if (is_callable(array($row, 'getImages'))) $localimages = $row->getImages();
  84      else $localimages = $row->images;
  85      $rimages = explode( "\n", $localimages);
  86      $total             = count( $rimages );
  87      if (is_callable(array($row, 'saveImages'))) $row->saveImages($rimages);
  88      else $row->images = $rimages;
  89          
  90      if ( (!$cparams->get('introtext')) && strstr( $row->introtext, '{mosimage}' ) ) {
  91          $search = explode( '{mosimage}', $row->introtext );
  92          $start = count( $search ) - 1;
  93      } else {
  94          $start = 0;
  95      }
  96      
  97      for ( $i = $start; $i < $total; $i++ ) {
  98          $img = trim( $rimages[$i] );
  99  
 100          // split on pipe the attributes of the image
 101          if ( $img ) {
 102              $attrib = explode( '|', trim( $img ) );
 103              // $attrib[0] image name and path from /images/stories
 104              
 105              // $attrib[1] alignment
 106              if ( !isset($attrib[1]) || !$attrib[1] ) {
 107                  $attrib[1] = '';
 108              }
 109              
 110              // $attrib[2] alt & title
 111              if ( !isset($attrib[2]) || !$attrib[2] ) {
 112                  $attrib[2] = 'Image';
 113              } else {
 114                  $attrib[2] = htmlspecialchars( $attrib[2] );
 115              }
 116              
 117              // $attrib[3] border
 118              if ( !isset($attrib[3]) || !$attrib[3] ) {
 119                  $attrib[3] = '0';
 120              }
 121              
 122              // $attrib[4] caption
 123              if ( !isset($attrib[4]) || !$attrib[4] ) {
 124                  $attrib[4]    = '';
 125                  $border     = $attrib[3];
 126              } else {
 127                  $border     = 0;
 128              }
 129              
 130              // $attrib[5] caption position
 131              if ( !isset($attrib[5]) || !$attrib[5] ) {
 132                  $attrib[5] = '';
 133              }
 134              
 135              // $attrib[6] caption alignment
 136              if ( !isset($attrib[6]) || !$attrib[6] ) {
 137                  $attrib[6] = '';
 138              }
 139              
 140              // $attrib[7] width
 141              if ( !isset($attrib[7]) || !$attrib[7] ) {
 142                  $attrib[7]     = '';
 143                  $width         = '';
 144              } else {
 145                  $width         = ' width: '. $attrib[7] .'px;';
 146              }
 147              
 148              // image size attibutes
 149              $size = '';
 150              if ( function_exists( 'getimagesize' ) ) {
 151                  $size     = @getimagesize( $mosConfig_absolute_path .'/images/stories/'. $attrib[0] );
 152                  if (is_array( $size )) {
 153                      $size = 'width="'. $size[0] .'" height="'. $size[1] .'"';
 154                  }
 155              }
 156              
 157              // assemble the <image> tag
 158              $image = '<img src="'. $mosConfig_live_site .'/images/stories/'. $attrib[0] .'" '. $size;
 159              // no aligment variable - if caption detected
 160              if ( !$attrib[4] ) {
 161                  $image .= $attrib[1] ? ' align="'. $attrib[1] .'"' : '';
 162              }
 163              $image .=' hspace="6" alt="'. $attrib[2] .'" title="'. $attrib[2] .'" border="'. $border .'" />';
 164              
 165              // assemble caption - if caption detected
 166              if ( $attrib[4] ) {
 167                  $caption = '<div class="mosimage_caption" style="width: '. $width .'; text-align: '. $attrib[6] .';" align="'. $attrib[6] .'">';
 168                  $caption .= $attrib[4];
 169                  $caption .='</div>';
 170              }        
 171              
 172              // final output
 173              if ( $attrib[4] ) {
 174                  $img = '<div class="mosimage" style="border-width: '. $attrib[3] .'px; float: '. $attrib[1] .'; margin: '. $params->def( 'margin' ) .'px; padding: '. $params->def( 'padding' ) .'px;'. $width .'" align="center">';
 175                  
 176                  // display caption in top position
 177                  if ( $attrib[5] == 'top' ) {
 178                      $img .= $caption;
 179                  }
 180                  
 181                  $img .= $image;                
 182                  
 183                  // display caption in bottom position
 184                  if ( $attrib[5] == 'bottom' ) {
 185                      $img .= $caption;
 186                  }
 187                  $img .='</div>';
 188              } else {
 189                  $img = $image;
 190              }
 191  
 192              
 193              $images[] = $img;
 194          }
 195      }
 196      
 197      return $images;
 198  }
 199  
 200  /**
 201  * Replaces the matched tags an image
 202  * @param array An array of matches (see preg_match_all)
 203  * @return string
 204  */
 205  function botMosImage_replacer( &$matches ) {
 206      $i = $GLOBALS['botMosImageCount']++;
 207      
 208      return @$GLOBALS['botMosImageArray'][$i];
 209  }
 210  ?>