2011-12-13 12:29:05 +01:00
< ? php
if ( IN_serendipity !== true ) {
die ( " Don't hack! " );
}
2016-02-04 18:36:20 +01:00
// Load possible language files.
@ serendipity_plugin_api :: load_language ( dirname ( __FILE__ ));
2011-12-13 12:29:05 +01:00
// Subst Plugin for Serendipity
// 01/2006 by Thomas Nesges <thomas@tnt-computer.de>
2014-10-27 14:39:31 +01:00
// 12/2006 Andy Hopkins Added Greybox functionality. <andy.hopkins@gmail.com>
2016-02-04 18:36:20 +01:00
class serendipity_event_lightbox extends serendipity_event
{
2011-12-13 12:29:05 +01:00
var $title = PLUGIN_EVENT_LIGHTBOX_NAME ;
2014-10-27 14:39:31 +01:00
2011-12-13 12:29:05 +01:00
// Remembers, if an image link was found in the article. If not found, nor CSS nor JS will be added to the blog header.
var $foundImageLink = false ;
2016-02-04 18:36:20 +01:00
function introspect ( & $propbag )
{
2011-12-13 12:29:05 +01:00
global $serendipity ;
$propbag -> add ( 'name' , PLUGIN_EVENT_LIGHTBOX_NAME );
$propbag -> add ( 'description' , PLUGIN_EVENT_LIGHTBOX_DESC );
2014-10-27 14:39:31 +01:00
$propbag -> add ( 'author' , 'Thomas Nesges, Andy Hopkins, Lokesh Dhakar, Cody Lindley, Stephan Manske, Grischa Brockhaus, Ian' );
2016-02-06 16:11:16 +01:00
$propbag -> add ( 'version' , '2.4.0' );
2011-12-13 12:29:05 +01:00
$propbag -> add ( 'requirements' , array (
2014-10-27 14:39:31 +01:00
'serendipity' => '1.6' ,
'php' => '5.3.0'
2011-12-13 12:29:05 +01:00
));
2016-02-04 18:36:20 +01:00
$propbag -> add ( 'event_hooks' , array ( 'frontend_display' => true , 'css' => true , 'frontend_header' => true , 'frontend_footer' => true ));
2014-10-27 14:39:31 +01:00
$propbag -> add ( 'stackable' , false );
$propbag -> add ( 'groups' , array ( 'IMAGES' ));
2011-12-13 12:29:05 +01:00
$propbag -> add ( 'cachable_events' , array ( 'frontend_display' => true ));
$this -> markup_elements = array (
array (
'name' => 'ENTRY_BODY' ,
'element' => 'body' ,
),
array (
'name' => 'EXTENDED_BODY' ,
'element' => 'extended' ,
),
array (
'name' => 'COMMENT' ,
'element' => 'comment' ,
),
array (
'name' => 'HTML_NUGGET' ,
'element' => 'html_nugget' ,
)
);
2014-10-27 14:39:31 +01:00
$conf_array = array ();
2011-12-13 12:29:05 +01:00
$conf_array [] = 'type' ;
$conf_array [] = 'path' ;
$conf_array [] = 'header_optimization' ;
$conf_array [] = 'navigate_one_entry_only' ;
2012-03-28 12:05:21 +02:00
$conf_array [] = 'init_js' ;
2011-12-13 12:29:05 +01:00
foreach ( $this -> markup_elements as $element ) {
$conf_array [] = $element [ 'name' ];
}
$propbag -> add ( 'configuration' , $conf_array );
}
2016-02-04 18:36:20 +01:00
function generate_content ( & $title )
{
2011-12-13 12:29:05 +01:00
$title = PLUGIN_EVENT_LIGHTBOX_NAME ;
}
2016-02-04 18:36:20 +01:00
function introspect_config_item ( $name , & $propbag )
{
2011-12-13 12:29:05 +01:00
global $serendipity ;
switch ( $name ) {
case 'navigate_one_entry_only' :
$propbag -> add ( 'type' , 'select' );
$propbag -> add ( 'name' , PLUGIN_EVENT_LIGHTBOX_GALLERY );
$propbag -> add ( 'description' , '' );
$propbag -> add ( 'select_values' , array ( 'none' => PLUGIN_EVENT_LIGHTBOX_GALLERY_NONE , 'entry' => PLUGIN_EVENT_LIGHTBOX_GALLERY_ENTRY , 'page' => PLUGIN_EVENT_LIGHTBOX_GALLERY_PAGE ));
$propbag -> add ( 'default' , 'none' );
break ;
case 'type' :
$propbag -> add ( 'type' , 'select' );
$propbag -> add ( 'name' , PLUGIN_EVENT_LIGHTBOX_TYPE );
$propbag -> add ( 'description' , '' );
2014-10-27 14:39:31 +01:00
$propbag -> add ( 'select_values' , array ( 'colorbox' => 'ColorBox' , 'lightbox2jq' => 'Lightbox 2 jQuery' , 'magnific' => 'Magnific-Popup' , 'prettyPhoto' => 'prettyPhoto' ));
$propbag -> add ( 'default' , 'lightbox2jq' );
2011-12-13 12:29:05 +01:00
break ;
case 'path' :
$propbag -> add ( 'type' , 'string' );
$propbag -> add ( 'name' , PLUGIN_EVENT_LIGHTBOX_PATH );
$propbag -> add ( 'description' , PLUGIN_EVENT_LIGHTBOX_PATH_DESC );
2014-11-01 20:00:10 +01:00
$propbag -> add ( 'default' , $serendipity [ 'serendipityHTTPPath' ] . 'plugins/serendipity_event_lightbox' );
2011-12-13 12:29:05 +01:00
break ;
2012-03-28 12:05:21 +02:00
case 'init_js' :
$propbag -> add ( 'type' , 'text' );
$propbag -> add ( 'name' , PLUGIN_EVENT_LIGHTBOX_INIT_JS );
$propbag -> add ( 'description' , PLUGIN_EVENT_LIGHTBOX_INIT_JS_DESC );
$propbag -> add ( 'default' , '' );
break ;
2011-12-13 12:29:05 +01:00
case 'header_optimization' :
$propbag -> add ( 'type' , 'boolean' );
$propbag -> add ( 'name' , PLUGIN_EVENT_LIGHTBOX_OPTIMIZATION );
$propbag -> add ( 'description' , PLUGIN_EVENT_LIGHTBOX_OPTIMIZATION_DESC );
$propbag -> add ( 'default' , 'false' );
break ;
default :
$propbag -> add ( 'type' , 'boolean' );
$propbag -> add ( 'name' , defined ( $name ) ? constant ( $name ) : $name );
$propbag -> add ( 'description' , sprintf ( APPLY_MARKUP_TO , defined ( $name ) ? constant ( $name ) : $name ));
$propbag -> add ( 'default' , 'true' );
break ;
2016-02-04 19:47:13 +01:00
default :
return false ;
2011-12-13 12:29:05 +01:00
}
return true ;
}
2016-02-04 18:36:20 +01:00
function event_hook ( $event , & $bag , & $eventData , $addData = null )
{
2011-12-13 12:29:05 +01:00
global $serendipity ;
2014-10-27 14:39:31 +01:00
static $regex = null ;
static $sub = null ;
static $navigate = null ;
2011-12-13 12:29:05 +01:00
static $pluginDir = null ;
static $type = null ;
2014-10-27 14:39:31 +01:00
$hooks = & $bag -> get ( 'event_hooks' );
2011-12-13 12:29:05 +01:00
if ( isset ( $hooks [ $event ])) {
if ( $pluginDir === null ) {
$pluginDir = $this -> get_config ( 'path' );
}
2014-10-27 14:39:31 +01:00
2011-12-13 12:29:05 +01:00
if ( $type === null ) {
2014-10-27 14:39:31 +01:00
$type = $this -> get_config ( 'type' );
2015-08-31 17:49:37 +02:00
#$new = array('colorbox', 'lightbox2jq', 'magnific', 'prettyPhoto');
/* $removed = array ( 'lightbox2' , 'lightbox' , 'lightbox_plus' , 'thickbox' , 'greybox' );
if ( in_array ( $type , $removed )) {
$type = 'lightbox2jq' ; // force and set new default for upgraders
$this -> set_config ( 'type' , $type );
} */
2011-12-13 12:29:05 +01:00
}
if ( $navigate === null ) {
2014-10-27 14:39:31 +01:00
$navigate = $this -> get_config ( 'navigate_one_entry_only' );
2011-12-13 12:29:05 +01:00
}
2014-10-27 14:39:31 +01:00
2011-12-13 12:29:05 +01:00
if ( $regex == null ) {
2014-10-27 14:39:31 +01:00
if ( $type == 'lightbox2jq' ) {
2011-12-13 12:29:05 +01:00
$regex = '/<a([^>]+)(href=(["\'])[^"\']*\.(jpe?g|gif|png)["\'])/i' ;
$sub = '<a $1 rel=$3lightbox$3 $2' ;
} elseif ( $type == 'prettyPhoto' ) {
$regex = '/<a([^>]+)(href=(["\'])[^"\']*\.(jpe?g|gif|png)["\'])/i' ;
$sub = '<a rel=$3prettyPhoto$3 $1 $2' ;
2014-10-27 14:39:31 +01:00
} elseif ( $type == 'colorbox' ) {
2011-12-13 12:29:05 +01:00
$regex = '/<a([^>]+)(href=(["\'])[^"\']*\.(jpe?g|gif|png)["\'])/i' ;
2014-10-27 14:39:31 +01:00
$sub = '<a $1 rel=$3singlebox$3 $2' ;
} elseif ( $type == 'magnific' ) {
$regex = '/<a([^>]+)(href=(["\'])[^"\']*\.(jpe?g|gif|png)["\'])/i' ;
$sub = '<a rel=$3onemagnificPopup$3 $1 $2' ;
} // do not use 'class' here as identifier, whenever possible, since this conflicts/not validates with $1 'class'es
2014-12-29 17:03:45 +01:00
else { // force new lib to prevent empty regular expression errors in preg_replace()
2014-12-29 16:21:59 +01:00
$type = 'lightbox2jq' ;
2014-12-29 17:03:45 +01:00
$this -> set_config ( 'type' , $type );
2014-12-29 16:21:59 +01:00
$regex = '/<a([^>]+)(href=(["\'])[^"\']*\.(jpe?g|gif|png)["\'])/i' ;
$sub = '<a $1 rel=$3lightbox$3 $2' ;
}
2011-12-13 12:29:05 +01:00
}
switch ( $event ) {
2016-02-04 19:47:13 +01:00
2014-11-01 20:00:10 +01:00
case 'frontend_header' :
$headcss = true ;
2014-10-27 14:39:31 +01:00
case 'frontend_footer' :
2015-08-31 17:49:37 +02:00
// case plugin imagesidebar on eg staticpages. We need the libs then!
$check_imagesidebar = serendipity_plugin_api :: enum_plugins ( '*' , false , 'serendipity_plugin_imagesidebar' );
2016-02-06 16:11:16 +01:00
$cisb = ( is_array ( $check_imagesidebar ) && $check_imagesidebar [ 0 ][ 'placement' ] != 'hide' ) ? $check_imagesidebar : null ;
2011-12-13 12:29:05 +01:00
2016-02-06 16:14:41 +01:00
// If no imagelink was processed, don't add css or js files to the header or footer! (configurable plugin option)
2016-02-06 16:11:16 +01:00
if ( true === ( serendipity_db_bool ( $this -> get_config ( 'header_optimization' , 'false' )) && $this -> foundImageLink === false && empty ( $cisb ))) {
break ;
2011-12-13 12:29:05 +01:00
}
2014-10-27 14:39:31 +01:00
echo " \n " ;
// ColorBox code (https://github.com/jackmoore/colorbox) - init with :visible to ensure to not show hidden elements via hideafter function in imageselectorplus ranges
if ( $type == 'colorbox' ) {
2014-11-01 20:00:10 +01:00
if ( $headcss ) {
2015-08-31 17:49:37 +02:00
echo ' <link rel="stylesheet" type="text/css" href="' . $pluginDir . '/colorbox/colorboxScreens.css" />' . " \n " ;
2014-11-01 20:00:10 +01:00
echo ' <link rel="stylesheet" type="text/css" href="' . $pluginDir . '/colorbox/colorbox.css" />' . " \n " ;
} else {
if ( ! class_exists ( 'serendipity_event_jquery' ) && ! $serendipity [ 'capabilities' ][ 'jquery' ]) {
2015-08-31 17:49:37 +02:00
echo ' <script type="text/javascript" src="' . $pluginDir . '/jquery-1.11.3.min.js" charset="utf-8"></script>' . " \n " ;
2014-11-01 20:00:10 +01:00
}
echo ' <script type="text/javascript" src="' . $pluginDir . '/colorbox/jquery.colorbox-min.js" charset="utf-8"></script>' . " \n " ;
echo ' <script type="text/javascript" src="' . $pluginDir . '/colorbox/jquery.colorbox.init.js" charset="utf-8"></script>' . " \n " ;
2014-10-27 14:39:31 +01:00
}
}
// LightBox2 jQuery based - http://lokeshdhakar.com/projects/lightbox2/ - this lightbox does not allow to show :visible anchors only - it shows and counts all gallery images, if set to view galleries
elseif ( $type == 'lightbox2jq' ) {
2014-11-01 20:00:10 +01:00
if ( $headcss ) {
echo ' <link rel="stylesheet" type="text/css" href="' . $pluginDir . '/lightbox2-jquery/css/lightbox.css" />' . " \n " ;
} else {
if ( ! class_exists ( 'serendipity_event_jquery' ) && ! $serendipity [ 'capabilities' ][ 'jquery' ]) {
2015-08-31 17:49:37 +02:00
echo ' <script type="text/javascript" src="' . $pluginDir . '/jquery-1.11.3.min.js" charset="utf-8"></script>' . " \n " ;
2014-11-01 20:00:10 +01:00
}
// remove anchors possible onclick handler
echo ' <script type="text/javascript"> jQuery(document).ready(function(){ jQuery(\'a[rel^="lightbox"]\').removeAttr("onclick"); }); </script>' . " \n " ;
echo ' <script type="text/javascript" src="' . $pluginDir . '/lightbox2-jquery/js/lightbox.min.js" charset="utf-8"></script>' . " \n " ;
2014-10-27 14:39:31 +01:00
}
}
// Magnific-Popup code (https://github.com/dimsemenov/Magnific-Popup) - init with :visible to ensure to not show hidden elements via hideafter function in imageselectorplus ranges
elseif ( $type == 'magnific' ) {
2014-11-01 20:00:10 +01:00
if ( $headcss ) {
echo ' <link rel="stylesheet" type="text/css" href="' . $pluginDir . '/magnific-popup/magnific-popup.css" />' . " \n " ;
} else {
if ( ! class_exists ( 'serendipity_event_jquery' ) && ! $serendipity [ 'capabilities' ][ 'jquery' ]) {
2015-08-31 17:49:37 +02:00
echo ' <script type="text/javascript" src="' . $pluginDir . '/jquery-1.11.3.min.js" charset="utf-8"></script>' . " \n " ;
2014-11-01 20:00:10 +01:00
}
echo ' <script type="text/javascript" src="' . $pluginDir . '/magnific-popup/jquery.magnific-popup.min.js" charset="utf-8"></script>' . " \n " ;
echo ' <script type="text/javascript" src="' . $pluginDir . '/magnific-popup/jquery.magnific-popup.init.js" charset="utf-8"></script>' . " \n " ;
2014-10-27 14:39:31 +01:00
}
}
// PrettyPhoto code - http://www.no-margin-for-errors.com/projects/prettyPhoto/ - init with :visible to ensure to not show hidden elements via hideafter function in imageselectorplus ranges
elseif ( $type == 'prettyPhoto' ) {
2014-11-01 20:00:10 +01:00
if ( $headcss ) {
echo ' <link rel="stylesheet" type="text/css" href="' . $pluginDir . '/prettyphoto/css/prettyPhoto.css" />' . " \n " ;
echo ' <link rel="stylesheet" type="text/css" href="' . $pluginDir . '/prettyphoto/css/prettyPhotoScreens.css" />' . " \n " ;
} else {
if ( ! class_exists ( 'serendipity_event_jquery' ) && ! $serendipity [ 'capabilities' ][ 'jquery' ]) {
2015-08-31 17:49:37 +02:00
echo ' <script type="text/javascript" src="' . $pluginDir . '/jquery-1.11.3.min.js" charset="utf-8"></script>' . " \n " ;
2014-11-01 20:00:10 +01:00
}
// remove anchors possible onclick handler
echo ' <script type="text/javascript">jQuery(document).ready(function(){ jQuery(\'a[rel^="prettyPhoto"]\').removeAttr("onclick"); }); </script>' . " \n " ;
echo ' <script type="text/javascript" src="' . $pluginDir . '/prettyphoto/js/jquery.prettyPhoto.min.js" charset="utf-8"></script>' . " \n " ;
echo ' <script type="text/javascript">jQuery(document).ready(function(){ jQuery(\'a:visible[rel^="prettyPhoto"]\').prettyPhoto(' . $this -> get_config ( 'init_js' ) . '); }); </script>' . " \n " ;
2014-10-27 14:39:31 +01:00
}
2011-12-13 12:29:05 +01:00
}
break ;
2016-02-04 18:36:20 +01:00
case 'css' :
// prepend css in this case only! since the scripts rely on this... I think ... (that is why it was added with fixchrome.css before!)
echo '
2011-12-13 12:29:05 +01:00
2016-02-04 18:36:20 +01:00
/* serendipity_event_lightbox start */
/* Fix for Safari and Chrome */
. serendipity_image_link {
display : block ;
}
/* serendipity_event_lightbox end */
' ;
break ;
case 'frontend_display' :
2014-10-27 14:39:31 +01:00
if ( $type == 'lightbox2jq' ) {
2011-12-13 12:29:05 +01:00
if ( $navigate == 'entry' ) {
2014-10-27 14:39:31 +01:00
$sub = '<a $1 rel=$3lightbox[' . $eventData [ 'id' ] . ']$3 $2' ;
2011-12-13 12:29:05 +01:00
} elseif ( $navigate == 'page' ) {
2014-10-27 14:39:31 +01:00
$sub = '<a $1 rel=$3lightbox[]$3 $2' ;
2011-12-13 12:29:05 +01:00
}
} elseif ( $type == 'prettyPhoto' ) {
if ( $navigate == 'entry' ) {
$sub = '<a rel=$3prettyPhoto[' . $eventData [ 'id' ] . ']$3 $1 $2' ;
} elseif ( $navigate == 'page' ) {
$sub = '<a rel=$3prettyPhoto[]$3 $1 $2' ;
}
2014-10-27 14:39:31 +01:00
} elseif ( $type == 'colorbox' ) {
2011-12-13 12:29:05 +01:00
if ( $navigate == 'entry' ) {
2014-10-27 14:39:31 +01:00
$sub = '<a rel=$3colorbox[' . $eventData [ 'id' ] . ']$3 $1 $2' ;
2011-12-13 12:29:05 +01:00
} elseif ( $navigate == 'page' ) {
2014-10-27 14:39:31 +01:00
$sub = '<a rel=$3colorbox[]$3 $1 $2' ;
2011-12-13 12:29:05 +01:00
}
2014-10-27 14:39:31 +01:00
} elseif ( $type == 'magnific' ) {
2011-12-13 12:29:05 +01:00
if ( $navigate == 'entry' ) {
2014-10-27 14:39:31 +01:00
$sub = '<a rel=$3magnificPopup[' . $eventData [ 'id' ] . ']$3 $1 $2' ;
2011-12-13 12:29:05 +01:00
} elseif ( $navigate == 'page' ) {
2014-10-27 14:39:31 +01:00
$sub = '<a rel=$3magnificPopup[]$3 $1 $2' ;
2011-12-13 12:29:05 +01:00
}
}
2014-10-27 14:39:31 +01:00
2011-12-13 12:29:05 +01:00
foreach ( $this -> markup_elements as $temp ) {
2016-02-04 18:36:20 +01:00
if ( isset ( $eventData [ $temp [ 'element' ]]) && serendipity_db_bool ( $this -> get_config ( $temp [ 'name' ], 'true' )) &&
2011-12-13 12:29:05 +01:00
! $eventData [ 'properties' ][ 'ep_disable_markup_' . $this -> instance ] &&
! isset ( $serendipity [ 'POST' ][ 'properties' ][ 'disable_markup_' . $this -> instance ])) {
$element = $temp [ 'element' ];
2014-10-27 14:39:31 +01:00
$replacement_count = 0 ;
$eventData [ $element ] = preg_replace ( $regex , $sub , $eventData [ $element ], - 1 , $replacement_count );
// Remember if an image link was found
if ( $replacement_count > 0 ) {
$this -> foundImageLink = true ;
2011-12-13 12:29:05 +01:00
}
}
}
break ;
2016-02-04 19:47:13 +01:00
default :
return false ;
2011-12-13 12:29:05 +01:00
2016-02-04 19:47:13 +01:00
}
return true ;
2011-12-13 12:29:05 +01:00
} else {
return false ;
}
}
2016-02-04 18:36:20 +01:00
function install ()
{
2011-12-13 12:29:05 +01:00
serendipity_plugin_api :: hook_event ( 'backend_cache_entries' , $this -> title );
}
2016-02-04 18:36:20 +01:00
function uninstall ( & $propbag )
{
2011-12-13 12:29:05 +01:00
serendipity_plugin_api :: hook_event ( 'backend_cache_purge' , $this -> title );
serendipity_plugin_api :: hook_event ( 'backend_cache_entries' , $this -> title );
}
2014-10-27 14:39:31 +01:00
2016-02-04 18:36:20 +01:00
function example ()
{
2014-10-27 14:39:31 +01:00
// remove old lightbox script directory
if ( is_file ( dirname ( __FILE__ ) . '/lightbox/lightbox.js' )) {
$this -> empty_dir ( dirname ( __FILE__ ) . '/lightbox' );
@ rmdir ( dirname ( __FILE__ ) . '/lightbox' );
}
// remove old lightbox2 script directory
if ( is_file ( dirname ( __FILE__ ) . '/lightbox2/lightbox2.js' )) {
$this -> empty_dir ( dirname ( __FILE__ ) . '/lightbox2' );
@ rmdir ( dirname ( __FILE__ ) . '/lightbox2' );
}
// remove old lightbox plus script directory
if ( is_file ( dirname ( __FILE__ ) . '/lightbox_plus/lightbox_plus.js' )) {
$this -> empty_dir ( dirname ( __FILE__ ) . '/lightbox_plus' );
@ rmdir ( dirname ( __FILE__ ) . '/lightbox_plus' );
}
// remove old thickbox script directory
if ( is_file ( dirname ( __FILE__ ) . '/thickbox/thickbox.js' )) {
$this -> empty_dir ( dirname ( __FILE__ ) . '/thickbox' );
@ rmdir ( dirname ( __FILE__ ) . '/thickbox' );
}
// remove old graybox script directory
if ( is_file ( dirname ( __FILE__ ) . '/graybox/gb_scripts.js' )) {
$this -> empty_dir ( dirname ( __FILE__ ) . '/graybox' );
@ rmdir ( dirname ( __FILE__ ) . '/graybox' );
2014-10-27 17:11:05 +01:00
@ unlink ( dirname ( __FILE__ ) . '/graycode_samples.txt' );
2014-10-27 14:39:31 +01:00
}
// remove old greybox script directory
if ( is_file ( dirname ( __FILE__ ) . '/greybox/gb_scripts.js' )) {
$this -> empty_dir ( dirname ( __FILE__ ) . '/greybox' );
@ rmdir ( dirname ( __FILE__ ) . '/greybox' );
}
}
/**
* empty a directory using the Standard PHP Library ( SPL ) iterator
* @ access private
* @ param string directory
*/
2016-02-04 18:36:20 +01:00
private function empty_dir ( $dir )
{
2014-10-27 14:39:31 +01:00
if ( ! is_dir ( $dir )) return ;
try {
$_dir = new RecursiveDirectoryIterator ( $dir );
// NOTE: UnexpectedValueException thrown for PHP >= 5.3
} catch ( Exception $e ) {
return ;
}
$iterator = new RecursiveIteratorIterator ( $_dir , RecursiveIteratorIterator :: CHILD_FIRST );
//$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST);
foreach ( $iterator as $file ) {
if ( $file -> isFile ()) {
@ unlink ( $file -> __toString ());
} else {
@ rmdir ( $file -> __toString ());
}
}
@ rmdir ( dir );
}
2011-12-13 12:29:05 +01:00
}
2016-02-04 18:36:20 +01:00
?>