/
index.php
239 lines (202 loc) · 6.96 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php // -*- mode:php -*-
//---------------------------------------------------------------------------
// 共通関数
const LWIKI_PHP_BEGIN_TAG="<\x3Fphp ";
const LWIKI_PHP_END_TAG="\x3F>";
function lwiki_include_string($html){
// $html の中に含まれている php ディレクティブを処理したい
echo eval(LWIKI_PHP_END_TAG.$html.LWIKI_PHP_BEGIN_TAG);
}
function lwiki_hash($key,$salt='4fa52acaf207'){
return substr(base64_encode(md5('salt+'.$salt.'+'.$key,true)),0,12);
}
function lwiki_split_lines($text){
return preg_split('/\r?\n|\r/u',$text);
}
function lwiki_canonicalize_linebreaks($text){
return preg_replace('/[ \t ]*(?:\r?\n|\r)/u',"\n",$text);
}
function lwiki_util_getTail($haystack,$head){
$h = strlen($head);
if(substr($haystack,0,$h)===$head)
return substr($haystack,$h);
else
return false;
}
//---------------------------------------------------------------------------
// 認証
function lwiki_auth_securimage_check(&$errorMessage){
$scode=$_POST['lwiki_simi'];
if(!$scode||$scode==''){
$errorMessage='画像認証コードが入力されていません。';
return 2;
}
require_once 'securimage.php';
$simg=new Securimage();
if($simg->check($scode)!==true){
$errorMessage='画像認証に失敗。';
return 1;
}
}
function lwiki_auth_securimage_generate(){
require_once 'securimage.php';
$opts=array(
'securimage_path' => '/~murase/php/',
'image_id' => 'lwiki_simg',
'image_alt_text' => 'letters',
'input_id' => 'lwiki_simi',
'show_audio_button' => false,
'refresh_alt_text' => '別画像',
'refresh_title_text' => '別画像',
'input_text' => '上の文字:');
return '<div class="securimage-captcha">'.Securimage::getCaptchaHtml($opts).'</div>';
}
//---------------------------------------------------------------------------
// common global variables
$lwiki_global_userIdentifier=$_SERVER['REMOTE_ADDR'].':'.$_SERVER['HTTP_USER_AGENT'];
$lwiki_base_php=$_SERVER['SCRIPT_NAME'];
$lwiki_base_baseDirectoryUrl=preg_replace('/\/index\.php$/','',$lwiki_base_php);
$lwiki_base_resourceDirectoryUrl=$lwiki_base_baseDirectoryUrl.'/res';
//---------------------------------------------------------------------------
// config
$lwiki_config_rewrite=false;
$lwiki_config_timezone=new DateTimeZone('Asia/Tokyo');
function lwiki_datetime($utime = null){
// http://qiita.com/Popphron/items/19c5bc6646db99bd3acb
// - new DateTime() を使用しようとすると timezone が php.ini で指定されていない場合にエラーになる。
// - '@' 形式で初期化すると $lwiki_config_timezone は使用されない。
global $lwiki_config_timezone;
$time=new DateTime(NULL,$lwiki_config_timezone);
if($utime!==null){
$time->setTimestamp($utime);
$time->setTimezone($lwiki_config_timezone);
}
return $time->format('Y-m-d H:i:s T');
}
$lwiki_config_commonHeadContent=<<<EOS
EOS;
include_once './lwiki_config.php';
$comment_authcode=lwiki_hash($lwiki_global_userIdentifier,$lwiki_config_fingerPrint);
$comment_authcode_cookie=@$_COOKIE['comment-authcode'];
//---------------------------------------------------------------------------
function lwiki_link_page($pageid=null,$get=null){
global $lwiki_config_rewrite;
if($lwiki_config_rewrite){
global $lwiki_base_baseDirectoryUrl;
$url=$lwiki_base_baseDirectoryUrl.'/';
if($pageid!==null){
# 1. Apache - AllowEncodedSlashes Off 対策で %2F は / に変換する。
$pageid=str_replace('%2F','/',$pageid);
# 2. query string にする時は urlencode (' ' → '+') で良いが、
# URI の一部にするときは RFC 3986 (' ' → '%20') にしないと駄目。
# 特に Apache の mod_rewrite で実行される "URI正規化" で '+' と ' ' が衝突する。
# また RFC 3986 では + → %2B にする必要もない。
$pageid=str_replace('+','%20',$pageid);
$pageid=str_replace('%2B','+',$pageid);
$url.=$pageid;
}
}else{
global $lwiki_base_php;
$url=$lwiki_base_php;
if($pageid!==null)
$get='id='.$pageid.($get!==null?'&'.$get:'');
}
if($get!==null)$url=$url.'?'.$get;
return $url;
}
//---------------------------------------------------------------------------
function lwiki_determine_pageid(){
global $page_title,$pageid;
# if accessed as http://example.com/wiki/index.php?id=title
$page_title=@$_GET['id'];
if($page_title!=''){
$pageid=urlencode($page_title);
return;
}
# if accessed as http://example.com/wiki/index.php/title
$page_title=lwiki_util_getTail($_SERVER['PHP_SELF'],$_SERVER['SCRIPT_NAME'].'/');
if($page_title!==false){
$pageid=urlencode($page_title);
return;
}
# if accessed as http://example.com/wiki/index.php
global $lwiki_config_default_pageid;
$page_title=$lwiki_config_default_pageid;
$pageid=urlencode($page_title);
}
lwiki_determine_pageid();
$ht_page_title=htmlspecialchars($page_title);
$pageinfo=@file('.lwiki/data/page.'.$pageid.'.info');
function page_modified_date(){
global $pageid,$pageinfo;
if($pageinfo!==false){
$f=explode('/',$pageinfo[0]);
$ipaddr=$f[0];
$date=$f[1];
}else{
$ipaddr='';
$date=@lwiki_datetime(@filemtime('.lwiki/data/page.'.$pageid.'.htm'));
}
$line=$date;
if($ipaddr!=null&&$ipaddr!='')
$line.=' by '.lwiki_hash($ipaddr);
return $line;
}
$comment_id='comment.'.$pageid;
$comment_error='';
switch(@$_GET['mode']){
case 'edit':
$partlength=@$_POST['partlength'];
$page_updated=false;
if(@$_POST['page_update']){
require_once ".lwiki/lib/lib.page-edit.php";
$page_updated=lwiki\edit\page_update();
}
if(!$page_updated){
include ".lwiki/lib/page.edit.php";
}else{
header('Location: '.lwiki_link_page($pageid)); // flush post data
}
exit;
case 'list':
include '.lwiki/lib/page.list.php';
exit;
case 'hist':
include '.lwiki/lib/page.hist.php';
exit;
case 'convert': // (preview 等の実装用に content を変換する)
require_once '.lwiki/lib/lib.lwiki.php';
require_once '.lwiki/lib/lib.page.php'; /* for \lwiki\page\generate_dynamic_link() */
lwiki_include_string(lwiki\convert\convert($_POST['content']));
exit;
default:
if(@$_GET['hist']!=''){
include ".lwiki/lib/page.diff.php";
exit;
}
if(@$_GET['command']=='comment-regenerate'){
require_once ".lwiki/lib/mod_comment.php";
\lwiki\comment\comment_regenerate();
}else if(@$_GET['command']=='page-convert'){
require_once ".lwiki/lib/lib.page-edit.php";
\lwiki\edit\page_convert();
}
// コメント追加
$comment_name=@$_COOKIE['comment-name'];
$comment_body="";
if(@$_POST['type']=='comment_add'){
require_once ".lwiki/lib/mod_comment.php";
$comment_added=\lwiki\comment\comment_add();
if($comment_added){
$comment_name=$_POST['name'];
setcookie('comment-name',$comment_name);
header('Location: '.lwiki_link_page($pageid)); // flush post data
exit;
}else{
$comment_body=$_POST['body'];
}
}
include ".lwiki/lib/page.read.php";
break;
}
?>