<?php
	require("__include-common.php");
	require("../account/__logincheck.php");

	set_time_limit(0);		//実行時間制御なし　max_execution_time
	ob_implicit_flush();		//自動フラッシュをオン(出力をバッファリングしない)

	define('PRIVATE_DIR_1', str_replace('/', '', photo_RESIZE_DIR_SMALL));
	define('PRIVATE_DIR_2', str_replace('/', '', photo_RESIZE_DIR_THUMB));
	define('PRIVATE_DIR_3', str_replace('/', '', photo_DELETE_TRASH_DIR));
	define('PRIVATE_DIR_4', str_replace('/', '', VIDEO_PREVIEW_CONVERT_FLV_DIR));
	_account_change($_GET['uid']);
	if (isset($_POST["path"]) && $_POST["path"] <> '') {
		$open_path = form_str_filepath($_POST["path"]);
	} elseif ($_GET['path'] <> '') {
		$open_path = my_GET('path');
	}
	$open_path = str_replace(DIRECTORY_SEPARATOR, '/', right_slash_strip($open_path));
	if ($open_path == '') {
		if ($_SESSION['photo_open_path'] <> '') {
			$open_path = $_SESSION['photo_open_path'];
		} else {
			$open_path = $_SESSION['login_friends_album_folder_'.$_SESSION['current_id']];
			if ($open_path == '') {
				$open_path = DEFAULT_IMAGES_FOLDER;
			}
		}
	}
	if (defined("photo_LIMITED_IMAGES_FOLDER") and photo_LIMITED_IMAGES_FOLDER <> '') {
		if ($_SESSION['システム管理者'] <> "YES" or photo_LIMITED_ADMIN_NOLIMIT <> 'YES') {
			$len = strlen(photo_LIMITED_IMAGES_FOLDER);
			if (substr($open_path.'/', 0, $len+1) <> photo_LIMITED_IMAGES_FOLDER.'/') {
				$open_path = photo_LIMITED_IMAGES_FOLDER;
			}
		}
	}
	$open_path = right_slash_strip($open_path);
	if ($_POST['search_str'] <> '') {
		$_SESSION['photo_folder_search_str'] = $_POST['search_str'];
	}
	if ($_POST['search_folder_reset']) {
		$_SESSION['photo_folder_search_str'] = '';
	}
	if ($_POST['search_deep']) {
		setcookie("photo_folder_search_deep", $_POST['search_deep'], time() + LOGIN_COOKIE_EXPIRE, MY_SESSION_PATH);
		$_SESSION['photo_folder_search_deep'] = $_POST['search_deep'];
	} elseif (isset($_COOKIE['photo_folder_search_deep'])) {
		$_SESSION['photo_folder_search_deep'] = $_COOKIE['photo_folder_search_deep'];
	}
	if (empty($_SESSION['photo_folder_search_deep'])) {
		$_SESSION['photo_folder_search_deep'] = 1;
	}
	$pv_str = array();
	$pv_len = array();
	$pv_str[] = PRIVATE_DIR_1;
	$pv_len[] = strlen(PRIVATE_DIR_1);
	$pv_str[] = PRIVATE_DIR_2;
	$pv_len[] = strlen(PRIVATE_DIR_2);
	$pv_str[] = PRIVATE_DIR_3;
	$pv_len[] = strlen(PRIVATE_DIR_3);
	$pv_str[] = PRIVATE_DIR_4;
	$pv_len[] = strlen(PRIVATE_DIR_4);

	html_header(HTML_TITLE);
	page_header(False);		//ログイン表示なし
	contents_header();
	photo_body($open_path, $page);
//	page_footer();
	html_footer();
	exit();
function photo_body($open_path, $page) {
?>
<div id="serach_body">
	<div id="photo_header">
	<p>「<span id="photo_search_path"><?= my_htmlspecialchars($open_path) ?></span>」以下のフォルダ名を検索します。<span class="alarm_text">ファイルやフォルダが多いと時間がかかります。</span></p>
	<form name="frm0" action="<?= $_SERVER['SCRIPT_NAME'] ?>" method="post">
		フォルダ名：<input class="text" type="text" name="search_str" value="<?= $_SESSION['photo_folder_search_str'] ?>" style="width:160px">
		<input type="submit" name="search_folder" value="フォルダ検索">
		<input type="submit" name="search_folder_reset" value="リセット">
		&nbsp;検索するフォルダ階層：<select name="search_deep">
		<?php
			for ($ix=1; $ix<=5; $ix++) {
		?>
			<option value="<?= $ix ?>"<?= $_SESSION['photo_folder_search_deep'] == $ix ? " selected" : "" ?>><?= $ix ?>
		<?php
			}
		?>
			<option value="1000"<?= $_SESSION['photo_folder_search_deep'] == 1000 ? " selected" : "" ?>>無制限
			</select>
	</form>
	</div>
	<div id="found_folder">
<?php
	if ($_POST['search_folder'] and $open_path <> '' and $_SESSION['photo_folder_search_str'] <> '') {
?>
	<p class="alarm_text">検索開始.....</p>
<?php
		if ($open_path <> $_SESSION['photo_search_open_path'] or $_SESSION['photo_folder_search_str'] <> $_SESSION['photo_folder_search_str_old'] or $_SESSION['photo_folder_search_deep_old'] <> $_SESSION['photo_folder_search_deep']) {
			$_SESSION['photo_search_open_path'] = $open_path;
			$_SESSION['photo_folder_search_str_old'] = $_SESSION['photo_folder_search_str'];
			$_SESSION['photo_folder_search_deep_old'] = $_SESSION['photo_folder_search_deep'];
			$dir = myfile_ENCODE($open_path);
			$_SESSION['photo_found_dirs'] = array();
			$deep = 1;
			while($deep <= $_SESSION['photo_folder_search_deep'] && ($dirs = glob($dir.'/*', GLOB_ONLYDIR))) {
				found_dir_push($dirs, $_SESSION['photo_found_dirs']);
				$dir .= '/*';
				$deep++;
			}
		} else {
			foreach ($_SESSION['photo_found_dirs'] as $dir) {
				found_dir_print($dir);
			}
		}
?>
	<br><p class="alarm_text">検索終了</p>
<?php
	} else {
		if ($open_path == $_SESSION['photo_search_open_path'] and $_SESSION['photo_folder_search_str'] == $_SESSION['photo_folder_search_str_old'] and $_SESSION['photo_folder_search_deep_old'] == $_SESSION['photo_folder_search_deep']) {
?>
	<p class="alarm_text">検索開始.....</p>
<?php
			foreach ($_SESSION['photo_found_dirs'] as $dir) {
				found_dir_print($dir);
			}
?>
	<br><p class="alarm_text">検索終了</p>
<?php
		}
	}
?>
	</div>
</div>
<?php
}
function found_dir_push($dirs, &$dirtbl) {
	global $pv_str;
	global $pv_len;
	foreach ($dirs as $d) {
		$dir = myfile_DECODE($d);
		if ( mb_substr($dir,-$pv_len[0]) != $pv_str[0] &&
		     mb_substr($dir,-$pv_len[1]) != $pv_str[1] &&
		     mb_substr($dir,-$pv_len[2]) != $pv_str[2] &&
		     mb_substr($dir,-$pv_len[3]) != $pv_str[3] ) {
			if (stristr($dir, $_SESSION['photo_folder_search_str'])) {
				$dirtbl[] = $dir;
				found_dir_print($dir);
			}
		}
	}
}
function found_dir_print($dir) {
?>
	<a href="index.php?path=<?= urlencode($dir) ?>"><?= $dir ?></a><br>
<?php
}
