# REMOTE HACK START
define('REMOTE_PIC_IFRAME',1);
define('REMOTE_PIC_MAXW',1);
# REMOTE HACK END

// Displays a picture
function theme_html_picture()
{
    global $CONFIG, $CURRENT_PIC_DATA, $CURRENT_ALBUM_DATA, $USER;
    global $album, $comment_date_fmt, $template_display_media;
    global $lang_display_image_php, $lang_picinfo;

    $pid = $CURRENT_PIC_DATA['pid'];
    $pic_title = '';

    if (!isset($USER['liv']) || !is_array($USER['liv'])) {
        $USER['liv'] = array();
    }
    // Add 1 to hit counter
    if (!USER_IS_ADMIN && !in_array($pid, $USER['liv']) && isset($_COOKIE[$CONFIG['cookie_name'] . '_data'])) {
        add_hit($pid);
        if (count($USER['liv']) > 4) array_shift($USER['liv']);
        array_push($USER['liv'], $pid);
    }

    if($CONFIG['thumb_use']=='ht' && $CURRENT_PIC_DATA['pheight'] > $CONFIG['picture_width'] ){ // The wierd comparision is because only picture_width is stored
      $condition = true;
    }elseif($CONFIG['thumb_use']=='wd' && $CURRENT_PIC_DATA['pwidth'] > $CONFIG['picture_width']){
      $condition = true;
    }elseif($CONFIG['thumb_use']=='any' && max($CURRENT_PIC_DATA['pwidth'], $CURRENT_PIC_DATA['pheight']) > $CONFIG['picture_width']){
      $condition = true;
    }else{
     $condition = false;
    }

    if ($CURRENT_PIC_DATA['title'] != '') {
        $pic_title .= $CURRENT_PIC_DATA['title'] . "\n";
    }
    if ($CURRENT_PIC_DATA['caption'] != '') {
        $pic_title .= $CURRENT_PIC_DATA['caption'] . "\n";
    }
    if ($CURRENT_PIC_DATA['keywords'] != '') {
        $pic_title .= $lang_picinfo['Keywords'] . ": " . $CURRENT_PIC_DATA['keywords'];
    }

    if (!$CURRENT_PIC_DATA['title'] && !$CURRENT_PIC_DATA['caption']) {
        template_extract_block($template_display_media, 'img_desc');
    } else {
        if (!$CURRENT_PIC_DATA['title']) {
            template_extract_block($template_display_media, 'title');
        }
        if (!$CURRENT_PIC_DATA['caption']) {
            template_extract_block($template_display_media, 'caption');
        }
    }

    $CURRENT_PIC_DATA['menu'] = html_picture_menu(); //((USER_ADMIN_MODE && $CURRENT_ALBUM_DATA['category'] == FIRST_USER_CAT + USER_ID) || ($CONFIG['users_can_edit_pics'] && $CURRENT_PIC_DATA['owner_id'] == USER_ID && USER_ID != 0) || GALLERY_ADMIN_MODE) ? html_picture_menu($pid) : '';

	$image_size = compute_img_size($CURRENT_PIC_DATA['pwidth'], $CURRENT_PIC_DATA['pheight'], $CONFIG['picture_width']);

	if ($CONFIG['make_intermediate'] && $condition) {
        $picture_url = get_pic_url($CURRENT_PIC_DATA, 'normal');
		# REMOTE HACK START
		if ( preg_match("/(?:http\:\/\/)?(?:[^\.]+\.)?([^\.\/]+\.[^\.\/]+)\/.*/", $CURRENT_PIC_DATA['user4'], $matches) ) $pic_hosting = $matches[1];
		# REMOTE HACK END
    } else {
		# REMOTE HACK START
		// use Remote Page URL
		if ($CONFIG['user_field3_name'] == "Remote Page URL" && $CURRENT_PIC_DATA['user3'] != "" && REMOTE_PIC_IFRAME) {
			$page_url = $CURRENT_PIC_DATA['user3'];
			if (REMOTE_PIC_MAXW) {
				$page_geom = ' width="' . $image_size['width'] . '" height="' . $image_size['height'] . '"';
			} else {
				$page_geom = ' width="' . $CURRENT_PIC_DATA['pwidth'] . '" height="' . $CURRENT_PIC_DATA['pheight'] . '"';	
			}
		}

		// Use Remote URL instead of local fullsize picture
		if ($CONFIG['user_field4_name'] == "Remote URL" && $CURRENT_PIC_DATA['user4'] != "" ) {
			$picture_url = $CURRENT_PIC_DATA['user4'];
			if ( preg_match("/(?:http\:\/\/)?(?:[^\.]+\.)?([^\.\/]+\.[^\.\/]+)\/.*/", $CURRENT_PIC_DATA['user4'], $matches) ) $pic_hosting = $matches[1];
		} else {
			$picture_url = get_pic_url($CURRENT_PIC_DATA, 'fullsize');
		}
		# REMOTE HACK END
    }


    $pic_title = '';
    $mime_content = cpg_get_type($CURRENT_PIC_DATA['filename']);


    if ($mime_content['content']=='movie' || $mime_content['content']=='audio') {

        if ($CURRENT_PIC_DATA['pwidth']==0 || $CURRENT_PIC_DATA['pheight']==0) {
            $CURRENT_PIC_DATA['pwidth']  = 320; // Default width

            // Set default height; if file is a movie
            if ($mime_content['content']=='movie') {
                $CURRENT_PIC_DATA['pheight'] = 240; // Default height
            }
        }

        $ctrl_offset['mov']=15;
        $ctrl_offset['wmv']=45;
        $ctrl_offset['swf']=0;
        $ctrl_offset['rm']=0;
        $ctrl_offset_default=45;
        $ctrl_height = (isset($ctrl_offset[$mime_content['extension']]))?($ctrl_offset[$mime_content['extension']]):$ctrl_offset_default;
        $image_size['whole']='width="'.$CURRENT_PIC_DATA['pwidth'].'" height="'.($CURRENT_PIC_DATA['pheight']+$ctrl_height).'"';
    }

    if ($mime_content['content']=='image') {
		# REMOTE HACK START
		$pic_html = (isset($pic_hosting)) ? "Picture Hosted By : <a href='http://" . $pic_hosting . "' target='_blank'><em>" . $pic_hosting . "</em></a>" : "";
		//if (isset($image_size['reduced'])) {
		if ($CONFIG['make_intermediate'] && $conditions) {
            $winsizeX = $CURRENT_PIC_DATA['pwidth']+5;  //the +'s are the mysterious FF and IE paddings
            $winsizeY = $CURRENT_PIC_DATA['pheight']+3; //the +'s are the mysterious FF and IE paddings		
            $pic_html .= "<br /><a href=\"javascript:;\" onclick=\"MM_openBrWindow('displayimage.php?pid=$pid&amp;fullsize=1','" . uniqid(rand()) . "','scrollbars=yes,toolbar=yes,status=yes,resizable=yes,width=$winsizeX,height=$winsizeY')\">";
            $pic_title = $lang_display_image_php['view_fs'] . "\n==============\n" . $pic_title;
            $pic_html .= "<img src=\"" . $picture_url . "\" class=\"image\" border=\"0\" alt=\"{$lang_display_image_php['view_fs']}\" /><br />";
            $pic_html .= "</a>\n";
        } else {
			// use remote page in iframe
			if ( REMOTE_PIC_IFRAME && isset($page_url) ) {
				$pic_html .= ' - <a href="javascript:;" onclick="MM_openBrWindow(\'displayimage.php?pid=' . $pid . '&amp;fullsize=1\',\'' . uniqid(rand()) . '\',\'scrollbars=yes,toolbar=yes,status=yes,resizable=yes,width=' . $winsizeX . ',height=' . $winsizeY . '\');">Open in New Window</a><br />';
				$pic_html .= '<iframe src="' . $page_url . '" ' . $page_geom . ' ALIGN="middle" MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="YES">' . $picture_url . '</iframe>';
			// use remote picture
			} else {
				$pic_html .= '<br /><a href="javascript:;" onclick="MM_openBrWindow(\'displayimage.php?pid=' . $pid . '&amp;fullsize=1\',\'' . uniqid(rand()) . '\',\'scrollbars=yes,toolbar=yes,status=yes,resizable=yes,width=' . $winsizeX . ',height=' . $winsizeY . '\');"><img src="' . $picture_url . '" ';
	            $pic_html .= REMOTE_PIC_MAXW ? $image_size['geom'] : "";
				$pic_html .= " class=\"image\" border=\"0\" alt=\"\" /></a><br />\n";
			}
        }
		# REMOTE HACK END
    } elseif ($mime_content['content']=='document') {
        $pic_thumb_url = get_pic_url($CURRENT_PIC_DATA,'thumb');
        $pic_html = "<a href=\"{$picture_url}\" target=\"_blank\" class=\"document_link\"><img src=\"".$pic_thumb_url."\" border=\"0\" class=\"image\" /></a>\n<br />";
    } else {
        $autostart = ($CONFIG['media_autostart']) ? ('true'):('false');

        $players['WMP'] = array('id' => 'MediaPlayer',
                                'clsid' => 'classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" ',
                                'codebase' => 'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" ',
                                'mime' => 'type="application/x-mplayer2" ',
                               );
        $players['RMP'] = array('id' => 'RealPlayer',
                                'clsid' => 'classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" ',
                                'codebase' => '',
                                'mime' => 'type="audio/x-pn-realaudio-plugin" '
                               );
        $players['QT']  = array('id' => 'QuickTime',
                                'clsid' => 'classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" ',
                                'codebase' => 'codebase="http://www.apple.com/qtactivex/qtplugin.cab" ',
                                'mime' => 'type="video/x-quicktime" '
                               );
        $players['SWF'] = array('id' => 'SWFlash',
                                'clsid' => ' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ',
                                'codebase' => 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" ',
                                'mime' => 'type="application/x-shockwave-flash" '
                               );
        $players['UNK'] = array('id' => 'DefaultPlayer',
                                'clsid' => '',
                                'codebase' => '',
                                'mime' => ''
                               );

        if (isset($_COOKIE[$CONFIG['cookie_name'].'_'.$mime_content['extension'].'player'])) {
            $user_player = $_COOKIE[$CONFIG['cookie_name'].'_'.$mime_content['extension'].'player'];
        } else {
            $user_player = $mime_content['player'];
        }

                // There isn't a player selected or user wants client-side control
        if (!$user_player) {
            $user_player = 'UNK';
        }

        $player = $players[$user_player];

        $pic_html  = '<object id="'.$player['id'].'" '.$player['classid'].$player['codebase'].$player['mime'].$image_size['whole'].'>';
        $pic_html .= "<param name=\"autostart\" value=\"$autostart\"><param name=\"src\" value=\"". $picture_url . "\">";
        $pic_html .= "<embed {$image_size['whole']} src=\"". $picture_url . "\" autostart=\"$autostart\" type=\"".$player['mime']."\"></embed>";
        $pic_html .= "</object><br />\n";
    }

    $CURRENT_PIC_DATA['html'] = $pic_html;
    $CURRENT_PIC_DATA['header'] = '';
    $CURRENT_PIC_DATA['footer'] = '';

    $CURRENT_PIC_DATA = CPGPluginAPI::filter('file_data',$CURRENT_PIC_DATA);

    $params = array('{CELL_HEIGHT}' => '100',
        '{IMAGE}' => $CURRENT_PIC_DATA['header'].$CURRENT_PIC_DATA['html'].$CURRENT_PIC_DATA['footer'],
        '{ADMIN_MENU}' => $CURRENT_PIC_DATA['menu'],
        '{TITLE}' => bb_decode($CURRENT_PIC_DATA['title']),
        '{CAPTION}' => bb_decode($CURRENT_PIC_DATA['caption']),
        );

    return template_eval($template_display_media, $params);
}


function theme_slideshow()
{
    global $CONFIG, $lang_display_image_php, $template_display_media;

    pageheader($lang_display_image_php['slideshow']);

    include "include/slideshow.inc.php";

	# REMOTE HACK START
    $start_slideshow =	"<script language=\"JavaScript\" type=\"text/JavaScript\">runSlideShow(true)</script>";
	# REMOTE HACK END
    template_extract_block($template_display_media, 'img_desc', $start_slideshow);

	$pic_html = ($html_pic_hosting != "") ? "Picture Hosted By : <a href='http://" . $html_pic_hosting . "' target='_blank'>" . $html_pic_hosting . "</a><br />\n" : "";
	# REMOTE HACK START
	if ($CONFIG['make_intermediate'] && $conditions) {
		$pic_html .= '<img src="' . $start_img . '" name="SlideShow" class="image" /><br />\n';
	} else {
		// use remote page in iframe, force iframe 
		if ( REMOTE_PIC_IFRAME && !$CONFIG['make_intermediate'] ) {
			// no page URL
			if ( $html_page_url != "") {
				$pic_html .= '<iframe id="SlideShow" name="SlideShow" src="' . $start_img . '" ' . $html_page_geom . ' ALIGN="middle" MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="YES"><img src="' . $start_img . '" name=\"SlideShow\" class=\"image\" border=\"0\" alt=\"\" /><br /></iframe><br />';
			} else {
				$pic_html .= '<iframe id="SlideShow" name="SlideShow" src="' . $html_page_url . '" ' . $html_page_geom . ' ALIGN="middle" MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="YES"><img src="' . $start_img . '" name=\"SlideShow\" class=\"image\" border=\"0\" alt=\"\" /><br /></iframe><br />';
			}
		// use remote picture
		} else {
			$pic_html .= '<img src="' . $start_img . '" ';
			$pic_html .= REMOTE_PIC_MAXW ? $html_image_size['geom'] : "";
			$pic_html .= " name=\"SlideShow\" class=\"image\" border=\"0\" alt=\"\" /><br />\n";
		}
	}

	$params = array('{CELL_HEIGHT}' => $CONFIG['picture_width'] + 100,
        '{IMAGE}' => $pic_html,
        '{ADMIN_MENU}' => '',
        );	
	# REMOTE HACK END

    starttable();
    echo template_eval($template_display_media, $params);
    endtable();
    starttable();
    echo <<<EOT
        <tr>
                <td align="center" class="navmenu" style="white-space: nowrap;">
                        <a href="javascript:endSlideShow()" class="navmenu">{$lang_display_image_php['stop_slideshow']}</a>
                </td>
        </tr>

EOT;
    endtable();
    pagefooter();
}


// Display the full size image
function theme_display_fullsize_pic()
{
    global $CONFIG, $THEME_DIR, $ALBUM_SET;
    global $lang_errors, $lang_fullsize_popup, $lang_charset;

    if (isset($_GET['picfile']))
    {
        if (!GALLERY_ADMIN_MODE) cpg_die(ERROR, $lang_errors['access_denied'], __FILE__, __LINE__);

    $picfile = $_GET['picfile'];
    $picname = $CONFIG['fullpath'] . $picfile;
    $imagesize = @getimagesize($picname);
    $imagedata = array('name' => $picfile, 'path' => path2url($picname), 'geometry' => $imagesize[3]);
    }
    elseif (isset($_GET['pid']))
    {
    $pid = (int)$_GET['pid'];
    $sql = "SELECT * " . "FROM {$CONFIG['TABLE_PICTURES']} " . "WHERE pid='$pid' $ALBUM_SET";
    $result = cpg_db_query($sql);

    if (!mysql_num_rows($result)) cpg_die(ERROR, $lang_errors['non_exist_ap'], __FILE__, __LINE__);

    $row = mysql_fetch_array($result);
	# REMOTE HACK START
	if ( ($CONFIG['user_field3_name'] == "Remote Page URL" && $row['user3'] != "" && REMOTE_PIC_IFRAME ) || ( $CONFIG['user_field4_name'] == "Remote URL" && $row['user4'] != "" ) ) {
		// use Remote Page URL
		if ($CONFIG['user_field3_name'] == "Remote Page URL" && $row['user3'] != "" && REMOTE_PIC_IFRAME) {
			$page_url = $row['user3'];
			$page_geom = $row['pwidth']+100;
			$page_geom = 'width="' . $page_geom . '" height="' . $row['pheight'] . '"';
		}

		// use Remote URL
		if ($CONFIG['user_field4_name'] == "Remote URL" && $row['user4'] != "" ) {
			$pic_url = $row['user4'];		
			if ( preg_match("/(?:http\:\/\/)?(?:[^\.]+\.)?([^\.\/]+\.[^\.\/]+)\/.*/", $row['user4'], $matches) ) $pic_hosting = $matches[1];
		}
	} else {
        $pic_url = get_pic_url($row, 'fullsize');
	}
	# REMOTE HACK END

	$geom = 'width="' . $row['pwidth'] . '" height="' . $row['pheight'] . '"';
    $imagedata = array('name' => $row['filename'], 'path' => $pic_url, 'geometry' => $geom, 'page_geometry' => $page_geom);
    }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
  <title><?php echo $CONFIG['gallery_name'] ?>: <?php echo $lang_fullsize_popup['click_to_close'];
      ?></title>
  <meta http-equiv="content-type" content="text/html; charset=<?php echo $CONFIG['charset'] == 'language file' ? $lang_charset : $CONFIG['charset'] ?>" />
  <script type="text/javascript" src="scripts.js"></script>
  <style type="text/css">
  body { margin: 0; padding: 0; background-color: gray; }
  img { margin:0; padding:0; border:0; }
  #content { margin:0 auto; padding:0; border:0; }
  table { border:0; height:100%; width:100%; border-collapse:collapse}
  td {         vertical-align: middle; text-align:center; }
  </style>
  </head>
  <body>
    <script language="JavaScript" type="text/JavaScript">
      adjust_popup();
    </script>
    <table>
      <tr>
            <td>
          <div id="content">
              <?php     
				# REMOTE HACK START
				// Host Credits
				if (isset($pic_hosting)) echo "Picture Hosted By : <a href='http://" . $pic_hosting . "' target='_blank'>" . $pic_hosting . "</a><br /><br />";
				
				// Picture HTML
				$picstr = '<a href="javascript: window.close()"><img src="'
					. htmlspecialchars($imagedata['path']) . '" '
					. $imagedata['geometry']
					. 'alt="'
					. htmlspecialchars($imagedata['name'])
					. '" title="'
					. htmlspecialchars($imagedata['name'])
					. "\n" . $lang_fullsize_popup['click_to_close']
					. '" /></a><br />' ."\n";

				// Use iframes for the Remote Page URL
				if (isset($page_url) && REMOTE_PIC_IFRAME) {
					echo '<iframe src="' . $page_url . '"' . $imagedata['page_geometry'] . ' ALIGN="middle" MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="YES">' . $picstr . '</iframe>';

				} else {
					echo $picstr;
				}
               ?>
          </div>
        </td>
      </tr>
    </table>
  </body>
</html>
<?php
}
