<?php
  include('html_templates/functions.php');

  // get the query
  if (isset($_GET['q'])) { $q = strip_tags($_GET['q']); $q = stripslashes($q); }	// query
  if (isset($_GET['p'])) $p = (int)$_GET['p']; else $p = 0;	// page offset to view

  if (isset($q)) $add_title = $q.' 사진'; else $add_title = "Jinsuk";
  include('html_templates/__head.php');
?>


<table border=0 cellpadding=0 cellspacing=0 width="100%" class='borderTable'>
<tr><td bgcolor="#153e7e">
  <h4>&nbsp; 김진숙의 사진집 <?php if (isset($q)) echo ': '.stripslashes($q); ?></h4>
  </td>
</tr>
<tr><td>

  <?php
	 $page_size=16;
	 unset($outs);
	if (isset($q)) $kristal_qry='(BASIC:"'.ClearQuery($q).'") AND (User_ID:김진숙)';
	else $kristal_qry='(User_ID: 김진숙)';
	 exec('bin/jinsuk.search -f'.$p.' -n'.$page_size.' -q'.base64_encode($kristal_qry), $outs);
	 $item_count = 0;
	 $total_tags = "";
	 while (list($key, $var) = each($outs)) {
	 	if (!strncmp($var, "<TOTAL_DF>", 10)) {
			$pos = strpos($var, "</TOTAL_DF>");
			$_scrap_count = (int)substr($var, 10, $pos-10);
			if ($_scrap_count == 0) {
				echo '<p>&nbsp; <b>검색된 사진이 없습니다.</b>';
				break;
			}
			else {
				echo '<p>&nbsp;총 '.$_scrap_count." 개";
			}
		}
	 	if (!strncmp($var, "<FETCH_COUNT>", 13)) {
			$pos = strpos($var, "</FETCH_COUNT>");
			$pfetch_cnt = (int)substr($var, 13, $pos-13);
			if ($pfetch_cnt > 0) {
				echo ", 현재: ".($p+1).' ~ '.($p+$pfetch_cnt)."\n";
			}
		}
		else if (!strcmp($var, "<ENTRY>")) {
			$psbjt = "";
			$pdesc = "";
			$pdate = "";
			$pcmmt = "";
			$pcmmt_date = "";
			$ptags = "";
			$pimg_url = "";
			$psrc_url = "";
			$pimg_id = "";
			$pexif = "";
			$prating = "";
			$presolution = "";
			$precommand = "";
			$phit = "";
			$apos = $p + $item_count;	// position at the search
			$pviewable_to = "";
			$prating = "";
			$palbum_title = "";
		}
		else if (!strcmp($var, "</ENTRY>")) {
			if ($item_count%4 == 0) {
				if ($item_count == 0) {
					if ($pfetch_cnt > 0) echo "<p><CENTER><TABLE border=1>\n";
				}
				echo "<TR>\n";
			}
			$pViewURL='view.php?id='.$pdid;
			echo '<TD ALIGN="CENTER" VALIGN="TOP" WIDTH="20%">';

			if ( ($prating == 'N' && !strcmp($pviewable_to, 'PUBLIC') && strcmp($pviewable_to, 'COPYRIGH')) || GetLoginInfo($id) == true ) {
				echo '<a href="'.$pViewURL.'"><img border=0 width=150 src="Image/'.$pimg_id.'-s.jpg"></a><br>'."\n";
			}
			else {
				if (!strcmp($pviewable_to, 'COPYRIGH')) {
					// echo "COPY";
					include('private_replace.php');
				}
				else if ($prating == 'A' || $prating == 'X') {
					// echo "ADLT";
					include('private_replace.php');
				}
				else if (!strcmp($pviewable_to, 'PRIVATE')) {
					// echo "PRIV";
					include('private_replace.php');
				}
			}

			if (!strcmp($pviewable_to, 'PRIVATE')) {
				echo " [<font size=-1 color=gray><i>비공개</i></font>]\n";
			}
			if ($pviewable_to != "COPYRIGH") {
				if ($prating == 'A' || $prating == 'X') {
					// do nothing
				}
				else {
					echo ' <font size=-1>'.$presolution.'<br>'."\n";
					$_print_title = str_replace("_", " ", $psbjt);
					echo $_print_title."<br></font><p />\n";
					echo "꼬리표: ".str_replace(";", " ", $ptags);
				}
			}
			else if ($pviewable_to == "COPYRIGH") {
					// do nothing
			}
			echo "</TD>\n";

			++$item_count;
			if ($item_count == $pfetch_cnt) {
				if ($_scrap_count == $apos+1) {
					if (($item_count)%4 == 0) {
						echo "</TR><TR>";
					}
					else {
					}
				}
				echo "</TR></TABLE>\n";
			}
		}
		else if (!strncmp($var, "<TABLE_ID>", 10)) {
			$pos = strpos($var, "</TABLE_ID>");
			$ptid = substr($var, 10, $pos-10);
		}
		else if (!strncmp($var, "<DOCUMENT_ID>", 13)) {
			$pos = strpos($var, "</DOCUMENT_ID>");
			$pdid = substr($var, 13, $pos-13);
		}
		else if (!strncmp($var, "<Date>", 6)) {
			$pos = strpos($var, "</Date>");
			$pdate = substr($var, 6, $pos-6);
		}
		else if (!strncmp($var, "<Image_ID>", 10)) {
			$pos = strpos($var, "</Image_ID>");
			$pimg_id = substr($var, 10, $pos-10);
		}
		else if (!strncmp($var, "<Subject>", 9)) {
			$pos = strpos($var, "</Subject>");
			$psbjt = substr($var, 9, $pos-9);
		}
		else if (!strncmp($var, "<Image_URL>", 11)) {
			$pos = strpos($var, "</Image_URL>");
			$pimg_url = substr($var, 11, $pos-11);
		}
		else if (!strncmp($var, "<Source_URL>", 12)) {
			$pos = strpos($var, "</Source_URL>");
			$psrc_url = substr($var, 12, $pos-12);
		}
		else if (!strncmp($var, "<Resolution>", 12)) {
			$pos = strpos($var, "</Resolution>");
			$presolution = substr($var, 12, $pos-12);
		}
		else if (!strncmp($var, "<Hit>", 5)) {
			$pos = strpos($var, "</Hit>");
			$phit = (int)substr($var, 5, $pos-5);
		}
		else if (!strncmp($var, "<Recommand>", 11)) {
			$pos = strpos($var, "</Recommand>");
			$precommand = (int)substr($var, 11, $pos-11);
		}
		else if (!strncmp($var, "<Viewable_To>", 13)) {
			$pos = strpos($var, "</Viewable_To>");
			$pviewable_to = substr($var, 13, $pos-13);
			if (strlen($pviewable_to) == 0) $pviewable_to = 'PUBLIC';	// default
		}
		else if (!strncmp($var, "<Rating>", 8)) {
			$pos = strpos($var, "</Rating>");
			$prating = substr($var, 8, $pos-8);
			if (strlen($prating) == 0) $prating = 'N';	// default
		}
		else if (!strncmp($var, "<User_ID>", 9)) {
			$pos = strpos($var, "</User_ID>");
			$scrap_user_id = substr($var, 9, $pos-9);
		}
		else if (!strncmp($var, "<Album_Title>", 13)) {
			$pos = strpos($var, "</Album_Title>");
			$palbum_title = substr($var, 13, $pos-13);
		}
		else if (!strncmp($var, "<Tag>", 5)) {
			$pos = strpos($var, "</Tag>");
			$ptags = substr($var, 5, $pos-5);
			$total_tags .= $ptags;
		}
	 }

	 // make page navigation here...
	 if ($_scrap_count > 0) {
		if (isset($q)) $pageNavURL = 'list.php?q='.urlencode($q).'&';
		else $pageNavURL = 'list.php?';
		 $total_pages = (int)(($_scrap_count+$page_size-1)/$page_size);
		 $cur_page = (int)($p/$page_size)+1;
		 echo "<p><center><font size=+0>쪽이동: \n";
		 if ($cur_page > 1) {
			 echo '[<a href="'.$pageNavURL;
			 if ($cur_page > 2) {
				 echo 'p=';
				 echo $page_size * ($cur_page-2);
			 }
			 echo '"><b>이전</b></a>]'."&nbsp;\n";
		 }
		 echo $cur_page.' / '.$total_pages."쪽\n";
		 echo "(".$_scrap_count." 사진)\n";
		 if ($cur_page < $total_pages) {
			 echo '&nbsp;[<a href="'.$pageNavURL.'p=';
			 echo $page_size * $cur_page;
			 echo '"><b>다음</b></a>]';
		 }
		 echo "&nbsp;&nbsp;\n";
		 echo "</font>\n";

		 // first and last page
		 echo "<font size=-1>\n";
		 if ($cur_page > 1)
			 echo '[<a href="'.$pageNavURL.'">첫쪽</a>]';
		 echo "&nbsp;\n";
		 if ($cur_page < $total_pages)
			 echo '[<a href="'.$pageNavURL.'p='.(($total_pages-1)*$page_size).'">끝쪽</a>]';
		 echo "</font></center>\n";
	 }

/*
	$total_tag_arr = explode(";", $total_tags);
	$total_tag_arr = array_unique($total_tag_arr);
	sort($total_tag_arr);
	if (count($total_tag_arr) > 0) {
		echo '<table width="100%" cellpadding="0" cellspacing="0" class="noBorderTable"><tr><td height=3></td></tr></table>'."\n";
		echo '<table width=100% class=borderTable cellpadding=0 cellspacing=0><tr><td>';
		echo ' <font size=-1 color=green> page </font> <img src=img/tag.gif alt="tags in this page"> ';
		foreach($total_tag_arr as $value) {
			echo '<font size=-1><a href=list.php?q='.urlencode($value).'>'.$value.'</a></font> ';
			echo "&nbsp;\n";
		}
		echo '</td></tr></table>';
	}
*/
  ?>

</td></tr></table>

<?php
  include('html_templates/__tail.php');
?>
