2007-05-07 15:22:44 +00:00
< ? php
2007-05-09 18:14:25 +00:00
/*
* This file may not be distributed without its readme . txt
**/
2007-05-07 15:22:44 +00:00
class LinkImage extends Extension {
//event handler
public function receive_event ( $event ) {
if ( is_a ( $event , 'DisplayingImageEvent' )) {
global $page ;
2007-05-09 02:23:00 +00:00
global $config ;
$data_href = $config -> get_string ( " data_href " );
2007-05-09 18:14:25 +00:00
$page -> add_header ( " <link rel='stylesheet' href=' $data_href /ext/link_image/_style.css' type='text/css'> " , 0 );
2007-06-30 01:19:11 +00:00
$page -> add_block ( new Block ( " Link to Image " , $this -> get_html ( $event -> image )));
2007-05-07 15:22:44 +00:00
}
if ( is_a ( $event , 'SetupBuildingEvent' )) {
2007-05-07 15:28:59 +00:00
$sb = new SetupBlock ( " Link to Image " );
2007-05-13 07:59:39 +00:00
$sb -> add_text_option ( " ext_link-img_text-link_format " , " Text Link Format: " );
2007-06-30 01:19:11 +00:00
$event -> panel -> add_block ( $sb );
2007-05-07 15:22:44 +00:00
}
if ( is_a ( $event , 'InitExtEvent' )) {
global $config ;
//just set default if empty.
if ( $config -> get_string ( " ext_link-img_text-link_format " ) == " " ) {
2007-05-07 15:28:59 +00:00
$config -> set_string ( " ext_link-img_text-link_format " , '$title - $id ($ext $size $filesize)' );
2007-05-07 15:22:44 +00:00
}
}
}
2007-05-29 20:02:11 +00:00
private function get_html ( $image ) {
2007-05-07 15:22:44 +00:00
global $config ;
$thumb_src = $image -> get_thumb_link ();
$image_src = $image -> get_image_link ();
$post_link = $image -> get_short_link ();
$text_link = $this -> parse_link_template ( $config -> get_string ( " ext_link-img_text-link_format " ), $image );
2007-05-29 20:02:11 +00:00
$html = " " ;
2007-05-07 15:22:44 +00:00
2007-05-29 20:02:11 +00:00
if ( $this -> get_HTML_PHP ()) {
$html_gen = new LinkImageHTML ( $post_link , $image_src , $thumb_src , $text_link );
$html = $html_gen -> getHTML ();
}
2007-05-07 15:22:44 +00:00
return $html ;
}
2007-05-29 20:02:11 +00:00
/* This function would do better generalized in the Extension class instead *
* of repeated in every extension . And probaly renamed , too ... *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
private function get_HTML_PHP () {
global $config ;
$theme = $config -> get_string ( " theme " );
2007-05-07 15:22:44 +00:00
2007-05-29 20:02:11 +00:00
if ( file_exists ( " themes/ $theme /link_image.html.php " )) {
//$html .= "Using theme version";
include " themes/ $theme /link_image.html.php " ;
} else if ( file_exists ( " ext/link_image/link_image.html.php " )) {
include " ext/link_image/link_image.html.php " ;
//$html .= "Using default generation in absense of themed generation.";
} else {
echo " <b>[Link to Image]<b> Error: <b>link_image.html.php</b> not found at either <b>ext/link_image/link_image.html.php</b> nor <b>themes/ $theme /link_image.html.php</b>.<br/> " .
" Please restore the default file to the former location, and copy it over to the latter if you wish to edit the html output of this extension. " ;
return false ;
}
return true ;
2007-05-07 15:22:44 +00:00
}
2007-05-29 20:02:11 +00:00
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-05-07 15:22:44 +00:00
private function parse_link_template ( $tmpl , $img ) { //shamelessly copied from image.class.php
global $config ;
// don't bother hitting the database if it won't be used...
$safe_tags = " " ;
if ( strpos ( $tmpl , '$tags' ) !== false ) { // * stabs dynamically typed languages with a rusty spoon *
$safe_tags = preg_replace (
" /[^a-zA-Z0-9_ \ - ]/ " ,
" " , $img -> get_tag_list ());
}
$base_href = $config -> get_string ( 'base_href' );
$fname = $img -> get_filename ();
$base_fname = strpos ( $fname , '.' ) ? substr ( $fname , 0 , strrpos ( $fname , '.' )) : $fname ;
$tmpl = str_replace ( '$id' , $img -> id , $tmpl );
$tmpl = str_replace ( '$hash' , $img -> hash , $tmpl );
$tmpl = str_replace ( '$tags' , $safe_tags , $tmpl );
$tmpl = str_replace ( '$base' , $base_href , $tmpl );
$tmpl = str_replace ( '$ext' , $img -> ext , $tmpl );
$tmpl = str_replace ( '$size' , " { $img -> width } x { $img -> height } " , $tmpl );
$tmpl = str_replace ( '$filesize' , to_shorthand_int ( $img -> filesize ), $tmpl );
$tmpl = str_replace ( '$filename' , $base_fname , $tmpl );
$tmpl = str_replace ( '$title' , $config -> get_string ( " title " ), $tmpl );
return $tmpl ;
}
}
add_event_listener ( new LinkImage ());
2007-05-07 15:28:59 +00:00
?>