<?php
 
// table_edit2.inc.php, 1.1 2006/09/22 taru        plugin
 
define('PLUGIN_TABLE_EDIT_TEXT_SIZE',  68);
define('PLUGIN_TABLE_EDIT_TD_SHOW', TRUE);		// td_edit_and_add	06.09.21

function plugin_table_edit2_convert()
{
	global $script, $vars, $edit_auth, $edit_auth_pages;
	static $number = array();
	$line_count = 1;
//	$table_end = 0;
	$table_sub_num = 1;

	$page = isset($vars['page']) ? $vars['page'] : '';
	if (! isset($number[$page])) $number[$page] = 1;
	$count = $number[$page]++;
	$r_page = rawurlencode($page);
	$opt = array(
		'edit' => 'on',
		'auth' => '',
		'table_mod' => '',
//		'copy' => '',
	);

	$auth_chk = auth::check_auth();
	foreach($edit_auth_pages as $key=>$val){
		if (preg_match($key, $page) && $edit_auth){
			$opt['edit'] = 'off';
			if(! empty($auth_chk)){
				$opt['edit'] = 'on';
			}
		}
	}
//	if(is_editable($page)){	}

	$head_button = '';
	$body = '';
	$body_table = '';

	$args = func_get_args();
	$arg = array_pop($args);
	foreach ($args as $opt_key) {
		list($key, $val) = explode('=', strtolower($opt_key));
		$opt[$key] = htmlspecialchars($val);
	}

	// if (PKWK_READONLY) {
	if (auth::check_role('readonly') || is_freeze($page) ) {
		$opt['edit'] = 'off';
	}

	// open or close
	if ($opt['table_mod'] != 'off' && $opt['edit'] != 'off'){
		if ($opt['table_mod'] == 'open' || $opt['table_mod'] == ''){
			$image_png = 'close.png" width="11" height="11" alt="" title="close"';
			$table_mod = 'close';
		} else if ($opt['table_mod'] == 'close'){
			$image_png = 'paraedit.png" width="9" height="9" alt="" title="open"';
			$opt['edit'] = 'off';
			$table_mod = 'open';
		}
		$head_button = '<div align="right" class="table_edit"><a name="table_edit2_table_number' . $count . '"></a><a href="' . $script
			  . "?plugin=table_edit2&amp;refer=$r_page&amp;table_mod=" . $table_mod . "&amp;table_num=$count"
			  . '"><img src="' . IMAGE_URI . 'plus/' . $image_png . '/></a></div>';
	}

	$arg = preg_replace(array("[\\r|\\n]","[\\r]"), array("\n","\n"), $arg);
	$args = explode("\n", $arg);
//	$args_count = count($args);

	foreach ($args as $args_line) {
		if (preg_match('/^\|(.+)\|([hHfFcC]?)$/', $args_line, $matches) && $opt['edit'] != 'off'){
//			$cell_count = 1;		//move 06.09.21
			if ( PLUGIN_TABLE_EDIT_TD_SHOW === TRUE ){		//td 06.09.21
				$match_cells = explode("|", $matches[1]);
			}
			$body_table .= '|' . $matches[1] . "|";

			$body_table .= "&table_edit2(edit_mod=show,table_num=$count,line_count=$line_count,table_sub_num=$table_sub_num);"
					  . "&table_edit2(edit_mod=tr,table_num=$count,line_count=$line_count);";

			$body_table .= "|" . $matches[2] . "\n";

			$line_count++;
//			$table_end = 1;			//06.09.21
			$table_header = 1;		//td 06.09.21
		} else {
			if( $table_header === 1 && $opt['edit'] != 'off' && PLUGIN_TABLE_EDIT_TD_SHOW === TRUE){		//td edit and add 06.09.21
				$cell_count = 1;
				$table_line_count = $line_count- 1;
				$body .= '|';
				foreach ($match_cells as $cell){
					$body .= "~&table_edit2(edit_mod=tdshow,table_num=$count,table_sub_num=$table_sub_num,line_count=$table_line_count,cell_count=$cell_count);"
						  . "&table_edit2(edit_mod=td,table_num=$count,table_sub_num=$table_sub_num,line_count=$table_line_count,cell_count=$cell_count);|";
					$cell_count++;
				}
				$table_sub_num++;
				$body .= "|h\n" . $body_table;
				$body_table = '';
				$table_header = 0;
			} else {
				$body .= $body_table;
				$body_table = '';
			}
			$body .= $args_line . "\n";
		}
	}
 
	return $head_button . convert_html($body);
}

function plugin_table_edit2_inline()
{
	global $script, $vars, $edit_auth_pages;
	$page = isset($vars['page']) ? $vars['page'] : '';
	$r_page = rawurlencode($page);
	$body = '';
	$opt = array(
		'edit_mod' => '',
		'table_num' => '',
		'line_count' => '',
	);
	$args = func_get_args();
	foreach ($args as $opt_key) {
		list($key, $val) = explode('=', strtolower($opt_key));
		$opt[$key] = htmlspecialchars($val);
	}
	if ($opt['edit_mod'] == 'show' || $opt['edit_mod'] == 'tdshow'){
		$image_png = 'paraedit.png" width="9" height="9" alt="" title="edit"';
	} else if ($opt['edit_mod'] == 'tr' || $opt['edit_mod'] == 'td'){
		$image_png = 'mini_add.png" width="8" height="8" alt="" title="addition"';
	}
	$body .= '<a href="' . $script . "?plugin=table_edit2&amp;refer=$r_page&amp;edit_mod="
		 . $opt['edit_mod'] . "&amp;table_num=" . $opt['table_num'] . "&amp;table_sub_num=" . $opt['table_sub_num']
		 . "&amp;line_count=" . $opt['line_count'] . "&amp;cell_count=" . $opt['cell_count']
		 . '"><img src="' . IMAGE_URI . 'plus/' . $image_png . ' /></a>';
	return $body;
}
function plugin_table_edit2_action()
{
	global $vars, $post, $script;
	$line_count = 1;
	$table_sub_num = 1;			//td 06.09.21
	$table_sub_num_chk = 1;		//td 06.09.21
	$line_count_td = 1;			//td 06.09.21
	$table_header_chk =1;		//show header	06.09.22
	if ($vars['edit_mod'] == 't_edit_td' || $vars['edit_mod'] == 'td'){		//td 06.09.21
		$td_edit = 1;
	}
	$table_num = 	$vars['table_num'];
	$page = 		$vars['refer'];

	if (auth::check_role('readonly')) die_message('PKWK_READONLY prohibits editing');
	if (! path_check($script,$_SERVER['HTTP_REFERER'])) return;	//06.09.11 upk
	if ($vars['cancel'] == 'cancel') {		//06.09.21
		header('Location: ' . $script . '?' . rawurlencode($page));
		exit;
		return;
	}
	$args  = get_source($page);
	static $count = 0;
	$source_s = '';
	$body = '';
	$title = '';
	$notimestamp = (isset($vars['notimestamp'])) ? TRUE : FALSE;
	if ($vars['table_mod'] == 'close'){
		$search_r = array ('@table_mod=open@si');
		$replace_r = array ('table_mod=close');
	} else if ($vars['table_mod'] == 'open') {
		$search_r = array ('@table_mod=close@si');
		$replace_r = array ('table_mod=open');
	}

	foreach ($args as $args_line) {
//		if (preg_match('/^#table_edit2([^\(\{]+)(?:\(([^\r]*)\))?(\{*)/', $args_line, $matches)){ //2
		if(preg_match('/^#([^\(\{]+)(?:\(([^\r]*)\))?(\{*)/', $args_line, $matches) && $matches[1] == 'table_edit2') {
			$table_find = 1;
			$count++;
			if( $line_count === 1 && $count == $table_num ){
				if(! preg_match('/(auth_check_off)/i',$matches[2])){
					check_editable($page, true, true);
				}
				if ( isset($vars['table_mod']) ){		//table_mod
					$notimestamp = TRUE;
					if(preg_match('/(table_mod)/',$matches[2])){
						$args_line = preg_replace($search_r, $replace_r, $args_line);
					} else {
						$args_line = "#table_edit2(" . $matches[2] . ",table_mod=" . $vars['table_mod'] . ")" . $matches[3] . "\n";
					}
				}
			}
			$end_line = strlen($matches[3]);
		}
		if (preg_match('/^\}{' . $end_line . '}/', $args_line)) {
			$table_find = 0;
		}
		if($table_find && $table_num == $count){
			if (preg_match('/^\|(.+)\|([hHfFcC]?)$/', $args_line, $match_line)){
				if ($table_sub_num == $vars['table_sub_num'] && $table_sub_num_chk){		//td 06.09.21
					$chk_table_sub_first_line = $line_count;
					$table_sub_num_chk = 0;
				}
				if ($vars['line_count'] == $line_count || strtolower( $match_line[2] ) == 'h' || $vars['edit_mod'] == 'tdshow' || $td_edit){
					$match_t = explode("|", $match_line[1]);	//move 06.09.16
				}
				if($vars['edit_mod'] == 'show'){					//show				header
					if($match_line[2] == 'h' && $table_header_chk && $table_sub_num == $vars['table_sub_num']){
//						$match_t = explode("|", $match_line[1]);
						$cell_count = 1;
						foreach ($match_t as $cell){
							$table_header[$cell_count] = $cell;
							$cell_count++;
						}
						$table_header_chk =0;		//show header	06.09.22
					} 
				}
				if ( $td_edit && $table_sub_num_chk == 0 && $table_sub_num == $vars['table_sub_num']){			//t_edit_td 06.09.21
					$end_cell = count ($match_t);
					$source_s .= '|';
					for ($x = 0;$x < $end_cell;$x++){
						if ($x == ($vars['cell_count'] - 1)){
							if (isset($vars['delete'])){
								$source_s .= '';
							} else if ($vars['edit_mod'] == 'td'){
								$source_s .= $match_t[$x] . '||';
								$notimestamp = TRUE;
							} else {
								$source_s .= $vars['cell' . $line_count_td] . '|';
							}
						} else {
							$source_s .= $match_t[$x] . '|';
						}
					}
					$source_s .= $match_line[2] . "\n";
					$line_count_td++;
					$table_sub_num_count_chk = 1;
				} else if($vars['line_count'] == $line_count && ! $td_edit){
					if($vars['edit_mod'] == 't_edit'){				//t_edit
						if(isset($vars['write'])){
							$source_s .= '|';
							for ($i = 1;$i < $vars['cell_count'];$i++){
								$source_s .= $vars['cell' . $i] . '|';
							}
							$source_s .= $match_line[2] . "\n";
//						} else if(isset($vars['cancel'])){
//								return;
//						} else if(isset($vars['delete'])){
						}
					} else if($vars['edit_mod'] == 'tr'){				//tr	tr_add
						$notimestamp = TRUE;
						$source_s .= $args_line;
						$source_s .= '|';
						foreach ($match_t as $cell){
							$source_s .= '|';
						}
						$source_s .= "\n";
					} else if($vars['edit_mod'] == 'show'){				//show
//						$match_t = explode("|", $match_line[1]);
						$title = 'table_edit2 ' . $page;
						$body = table_edit_form($page, $vars['edit_mod'], $table_num, $line_count, $match_t,'',$table_header);
						break;
					} else if($vars['edit_mod'] == 'tdshow'){			//tdshow	06.09.21
						$cells[$line_count] = $match_t;
						$column_num = $vars['cell_count'] - 1;
						for ( $x = $chk_table_sub_first_line ;$x <= $line_count ; $x++){
							$column_cell[$x] = $cells[$x][$column_num];
						}
						$title = 'table_edit2 ' . $page;
						$body = table_edit_form($page, $vars['edit_mod'], $table_num, $vars['cell_count'], $column_cell, $vars['table_sub_num']);
					}
				} else {
					if ($vars['edit_mod'] == 'tdshow'){		//tdshow and edit_td	06.09.21
						$cells[$line_count] = $match_t;
					}
					$table_sub_num_count_chk = 1;			//td06.09.21
					$source_s .= $args_line;
				}
				$line_count++;
			} else {
				if ($table_sub_num_count_chk == 1){			//td 06.09.21
					$table_sub_num++;
					$table_sub_num_count_chk = 0;
				}
				$source_s .= $args_line;
			}
		} else {
			$source_s .= $args_line;
		}
	}

	if($vars['edit_mod'] == 't_edit' || $vars['edit_mod'] == 'tr' || $td_edit || isset($vars['table_mod'])){
		page_write($page, $source_s, $notimestamp);
	}

	$post['refer'] = $page;
	if ($vars['edit_mod'] == 'tdshow' || $vars['edit_mod'] == 'show'){
		return array('msg'=>$title, 'body'=>$body);
	}
	header('Location: ' . $script . '?' . rawurlencode($page) . '#' . "table_edit2_table_number$table_num");	//1.0	06.09.21
	exit;
}

//table edit form
function table_edit_form($page, $_edit_mod, $table_num, $edit_count, $edit_cell, $table_sub_num, $table_header)
{
	if ($_edit_mod == 'show'){
		$line_name = ' line';
		$edit_mod = 't_edit';
		$x_count = 'line_count';
		$y_count = 'cell_count';
	} else if ($_edit_mod == 'tdshow'){
		$line_name = " (sub number=" . $table_sub_num . ") column line";
		$edit_mod = 't_edit_td';
		$x_count = 'cell_count';
		$y_count = 'line_count';
	}
	$body = '<h3>' . 'table=' . $table_num . $line_name . '=' . $edit_count . '</h3>';
	$body .= '<form action="' . $script . '" method="post">';
	$body .= ' <table cellspacing="0" cellpadding="2" class="style_table">';
	$cell_count = 1;
	foreach ($edit_cell as $cell){
		$body .= '  <tr><td>' . $table_header[$cell_count] . '(' . $cell_count . ')</td><td class="style_td">';
		$body .= '   <input type=text name="cell' . $cell_count . '" value="' . $cell . '" size="'
					 . PLUGIN_TABLE_EDIT_TEXT_SIZE . '" /></td></tr>';
		$cell_count++;
	}
	$body .= <<<EOD
  <tr><td  colspan="2">
   <input type="hidden" name="plugin"        value="table_edit2" />
   <input type="hidden" name="refer"         value="$page" />
   <input type="hidden" name="table_num"     value="$table_num" />
   <input type="hidden" name="table_sub_num" value="$table_sub_num" />
   <input type="hidden" name="$x_count"      value="$edit_count" />
   <input type="hidden" name="$y_count"      value="$cell_count" />
   <input type="hidden" name="edit_mod"      value="$edit_mod" />
   <input type="submit" name="write"         value="edit" />
   <input type="submit" name="cancel"  value="cancel" />
   <input type="submit" name="delete"  value="delete" /> no time stamp
   <input type="checkbox" name="notimestamp" />
  </td></tr>
 </table>
</form>
EOD;
	return $body;
}

?>
