Newer
Older
pukiwikiplugin / sync.inc.php
<?php
// Original : Mase?
// Modified by Sosuke Moriguchi (2016/12/21) -- ignore pukiwiki-comments in source page
function plugin_sync_action() {
  // config
  $wikiname_dst = mb_convert_encoding('演習室の利用状況', 'UTF-8', 'EUC-jp');
  $wikiname_src_regex = '演習担当';
  $url       = 'https://ist.ksc.kwansei.ac.jp/junbi/index.php';
  $query_login= '?plugin=loginform&pcmd=login&page=FrontPage';
  $query_edit = '?cmd=edit&page=' . urlencode($wikiname_dst);
  $query_diff = '?cmd=diff&page=' . urlencode($wikiname_dst);

  // http://qiita.com/wanwanland/items/a5f9574fadd214d7b5c8
  // Cookie情報を保存する一時ファイルディレクトリにファイルを作成します
  $cookie_path =  tempnam(sys_get_temp_dir(), "CKI");

  // login
  $account_data = array('username' => "junbi" ,'password'=> "Grl12hsj");
  url_get_contents($url.$query_login, $cookie_path, $account_data);
  
  // load
  $wikiname_src = urldecode(end(preg_split('/\?/', $_SERVER['HTTP_REFERER'])));
  if (preg_match('/' . $wikiname_src_regex . '\d{4}\w/', $wikiname_src)) {
    $target = array();  
    $is_target = false;
    $header_index = 0;
    $column_cut_index = 0;
    $column_cut_length = 0;
    foreach(get_source($wikiname_src) as $line) {
      $line = rtrim($line, "\n");
      if (preg_match('/^\/\/\s*(start|end)/', $line)) {
	$is_target = !$is_target;
	array_push($target, rtrim($line, "\n"));
      }

      if ($is_target) {
	if (preg_match('/^\*/', $line)) {         // wiki title
	  array_push($target, $line);
	} else if (preg_match('/^\|~/', $line)) { // table header
	  $columns = preg_split('/\|/', $line);
	  $column_cut_index = array_search('~担当', $columns);
	  if ($header_index != 3) {
	    $column_cut_length = 1;
	  } else {
	    $column_cut_index -= 2;
	    $column_cut_length = 3;
	  }
	  array_splice($columns, $column_cut_index, $column_cut_length);
	  array_push($target, join('|', $columns));
	  $header_index++;
	} else if (preg_match('/^\|[^?~]/', $line)) { // table row (except header)
	  $columns = preg_split('/\|/', $line);
	  array_splice($columns, $column_cut_index, $column_cut_length);
	  array_push($target, join('|', $columns));
	} else if (preg_match('/^\/\//', $line)) {
	  // line is commented out (so do nothing)
	} else {
	  if ($target[count($target)-1] != '') array_push($target, '');
	}
      }
    }
    $target = mb_convert_encoding(join("\n", $target), 'UTF-8', 'EUC-JP');
  } else {
    $target = '';
  }

  if (preg_match('/[^\s]/', $target)) {
    // update
    $html = url_get_contents($url.$query_edit, $cookie_path);
    $data = array();
    preg_match_all('/<input\s+?type="hidden"\s+?name="(.*?)"\s+?value="(.*?)"/s', $html, $matches);
    for ($i = 0; $i < count($matches[1]); $i++) {
      $data[$matches[1][$i]] = $matches[2][$i];
    }
    preg_match('/<textarea name="msg".*?>(.*?)<\/textarea>/s', $html, $matches);
    
    $data['original'] = htmlspecialchars_decode($matches[1]);
    $data['msg'] = preg_replace('/\/\/ start.*?\/\/ end/s', $target, $data['original']);
    
    $updated = false;
    if (strcmp($data['msg'], $data['original']) != 0) {
      $data['notimestamp'] = strcmp($data['msg'], $data['original']) == 0 ? 'true' : '';
      $data['template_page'] = '';
      $data['write'] = mb_convert_encoding('ページの更新', 'UTF-8', 'EUC-JP');
      $data['target'] = $target;
      $contents = url_get_contents($url, $cookie_path, $data);
      $updated = true;
    }

    // diff
    $contents = mb_convert_encoding(url_get_contents($url.$query_diff, $cookie_path), 'EUC-JP', 'UTF-8');
    preg_match('/(<div id="body"><ul>.*?<\/ul>)(.*?<\/pre>\s<\/div>)/s', $contents, $matches);
    $navi = $matches[1];
    $body = $matches[2];
    // navi 
    $links = '<li>お知らせページの <a href="' . $url . '?' . urlencode($wikiname_dst) . '">' . mb_convert_encoding($wikiname_dst, 'EUC-JP', 'UTF-8')  . '</a>&nbsp;へ行く。</li>';
    $links .= '<li><a href="' . $_SERVER['HTTP_REFERER'] . '">' . $wikiname_src . '</a>&nbsp;へ戻る。</li>';
    $navi = preg_replace('/<li><a .*?<\/li>\s<\/ul>/s', $links . '</ul>', $navi);

    // body 
    $body = preg_replace('/<pre>.*(\/\/\sstart.*?\/\/\send).*<\/pre>/s', '<pre>\1</pre>', $body);
    if (!$updated) {
      $body = preg_replace('/<span class=".*"\s*>(.*?)<\/span>/', '\1', $body);
      // $body = preg_replace('/<span class="diff_removed">(.*?)<\/span>/', '\1', $body);
    }

    $contents = $navi . $body;
    
  } else {
    $contents = 'usage error';
  }

  unlink($cookie_path); //一時ファイル削除

  $msg  = mb_convert_encoding($wikiname_dst, 'EUC-JP', 'UTF-8') . '&nbsp;の変更点';

  return array('msg' => $msg, 'body' => $contents);
}

function url_get_contents($url, $cookie_path, $data_array=NULL) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if (!is_null($data_array)) {
        curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data_array));
    }
    curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie_path);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
    $html = curl_exec($ch);
    curl_close($ch);
    return $html;
}
?>