tinycm annotate index.cgi @ rev 121

Remove ashism ==
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Feb 26 12:26:25 2019 +0100 (2019-02-26)
parents 05ccbdd25ab5
children
rev   line source
pankso@5 1 #!/bin/sh
pankso@5 2 #
paul@64 3 # TinyCM - Small, fast and elegant CGI/SHell Content Manager
pankso@5 4 #
pankso@76 5 # Copyright (C) 2012-2017 SliTaz GNU/Linux - BSD License
pankso@5 6 #
pankso@84 7 . /usr/lib/slitaz/httphelper.sh
pankso@5 8
paul@8 9 # Let's have a peer site config file with a .cgi extension so content
pankso@5 10 # is secure even if left in a web server directory.
pankso@57 11 . ./config.cgi
pankso@5 12
pankso@5 13 tiny="$PWD"
pankso@5 14 content="content"
pankso@5 15 wiki="$content/wiki"
pankso@5 16 index="index"
pankso@5 17 cache="cache"
pankso@5 18 plugins="plugins"
pankso@5 19 tmp="/tmp/tinycm"
pankso@5 20 sessions="$tmp/sessions"
pankso@15 21 script="$SCRIPT_NAME"
pankso@28 22 activity="$cache/log/activity.log"
pankso@5 23
pankso@5 24 # Content negotiation for Gettext
pankso@5 25 IFS=","
pankso@5 26 for lang in $HTTP_ACCEPT_LANGUAGE
pankso@5 27 do
pankso@5 28 lang=${lang%;*} lang=${lang# } lang=${lang%-*}
pankso@5 29 case "$lang" in
pankso@57 30 en) lang="C" && break ;;
pankso@57 31 fr) lang="fr_FR" && break ;;
pankso@57 32 pt) lang="pt_BR" && break ;;
pankso@57 33 ru) lang="ru_RU" && break ;;
pankso@5 34 esac
pankso@5 35 done
pankso@5 36 unset IFS
pankso@5 37 export LANG=$lang LC_ALL=$lang
pankso@5 38
pankso@55 39 # Internationalization
pankso@55 40 . /usr/bin/gettext.sh
pankso@55 41 TEXTDOMAIN='tinycm'
pankso@55 42 export TEXTDOMAIN
pankso@55 43
pankso@5 44 #
pankso@5 45 # Functions
pankso@5 46 #
pankso@5 47
paul@8 48 # Used by edit to display language name and the language box. This is
pankso@5 49 # for CM content not gettext support.
pankso@5 50 get_lang() {
pankso@57 51 dlang=$(echo $d | cut -d "/" -f 1)
pankso@57 52 doc=${d#$dlang/}
pankso@5 53 echo '<div id="lang">'
pankso@5 54 for l in $LANGUAGES
pankso@5 55 do
pankso@57 56 case $dlang in
pankso@5 57 en) i18n="English" ;;
pankso@5 58 fr) i18n="Français" ;;
pankso@5 59 pt) i18n="Português" ;;
pankso@5 60 ru) i18n="Русский" ;;
pankso@5 61 *) i18n="*" ;;
pankso@5 62 esac
pankso@5 63 echo "<a href='?d=$l/$doc'>$l</a>"
pankso@5 64 done
pankso@5 65 echo '</div>'
pankso@5 66 }
pankso@5 67
pankso@5 68 # HTML 5 header.
pankso@5 69 html_header() {
pankso@5 70 if [ -f "$tiny/lib/header.html" ]; then
pankso@5 71 cat $tiny/lib/header.html | sed -e s!'%TITLE%'!"$TITLE - $d"!g
pankso@5 72 else
pankso@5 73 cat << EOT
pankso@5 74 <!DOCTYPE html>
pankso@5 75 <html xmlns="http://www.w3.org/1999/xhtml">
pankso@5 76 <head>
pankso@5 77 <title>$TITLE</title>
pankso@5 78 <meta charset="utf-8" />
pankso@5 79 <style type="text/css">body { margin: 40px 120px; }</style>
pankso@5 80 </head>
pankso@5 81 <body>
pankso@5 82 <!-- Content -->
pankso@5 83 <div id="content">
pankso@5 84 EOT
pankso@5 85 fi
pankso@5 86 }
pankso@5 87
pankso@5 88 # HTML 5 footer.
pankso@5 89 html_footer() {
pankso@5 90 if [ -f "$tiny/lib/footer.html" ]; then
pankso@5 91 cat $tiny/lib/footer.html
pankso@5 92 else
pankso@5 93 cat << EOT
pankso@5 94
pankso@5 95 <!-- End content -->
pankso@5 96 </div>
pankso@5 97
pankso@63 98 <div id="footer">
pankso@63 99 I &hearts; <a href="http://tinycm.slitaz.org/">TinyCM</a>
pankso@63 100 </div>
pankso@5 101
pankso@5 102 </body>
pankso@5 103 </html>
pankso@5 104 EOT
pankso@5 105 fi
pankso@5 106 }
pankso@5 107
pankso@5 108 # Default index if missing
pankso@5 109 default_index() {
pankso@11 110 mkdir -p "$wiki"
pankso@5 111 cat > $wiki/$index.txt << EOT
pankso@5 112 ==== Welcome ====
pankso@5 113
pankso@36 114 <p>
pankso@32 115 This is the default index page of your TinyCM, you can login then start to
paul@80 116 edit and add some content. You can read the help about text formating
pankso@48 117 and functions: [Help page|en/help]
pankso@36 118 </p>
pankso@5 119
pankso@5 120 EOT
pankso@5 121 }
pankso@5 122
pankso@28 123 # Log main activity.
pankso@28 124 log_activity() {
pankso@28 125 [ -d "$cache/log" ] || mkdir -p ${cache}/log
pankso@28 126 #gravatar="$(get_gravatar $MAIL 24)"
pankso@28 127 grep ^[A-Z] | \
pankso@28 128 sed s"#^[A-Z]\([^']*\)#$user|$(date '+%Y-%m-%d')|\0#" \
pankso@28 129 >> $cache/log/activity.log
pankso@28 130 }
pankso@28 131
pankso@5 132 # Log documents activity.
pankso@5 133 log() {
pankso@5 134 grep ^[A-Z] | \
pankso@5 135 sed s"#^[A-Z]\([^']*\)#$(date '+%Y-%m-%d %H:%M') : \0#" \
pankso@5 136 >> $cache/$d/activity.log
pankso@5 137 }
pankso@5 138
pankso@5 139 # Check if user is auth
pankso@5 140 check_auth() {
pankso@5 141 auth="$(COOKIE auth)"
pankso@5 142 user="$(echo $auth | cut -d ":" -f 1)"
pankso@5 143 md5cookie="$(echo $auth | cut -d ":" -f 2)"
pankso@5 144 [ -f "$sessions/$user" ] && md5session="$(cat $sessions/$user)"
pascal@121 145 if [ "$md5cookie" = "$md5session" ] && [ "$auth" ]; then
pankso@5 146 . $PEOPLE/$user/account.conf
pankso@5 147 return 0
pankso@5 148 else
pankso@5 149 return 1
pankso@5 150 fi
pankso@5 151 }
pankso@5 152
pankso@28 153 # Check if user is admin
pankso@28 154 admin_user() {
pankso@106 155 grep -w -q "$user" ${ADMIN_USERS}
pankso@28 156 }
pankso@28 157
paul@64 158 # Authenticated or not
pankso@5 159 user_box() {
pankso@5 160 if check_auth; then
pankso@5 161 cat << EOT
pankso@5 162
pankso@5 163 <div id="user">
pankso@5 164 <a href="$script?user=$user">$(get_gravatar $MAIL 20)</a>
pankso@5 165 <a href="$script?logout">Logout</a>
pankso@5 166 </div>
pankso@5 167
pankso@5 168 EOT
pankso@5 169 else
pankso@5 170 cat << EOT
pankso@5 171
pankso@5 172 <div id="user">
pankso@5 173 <a href="$script?login"><img src="images/avatar.png" alt="[ User ]" /></a>
pankso@5 174 <a href="$script?login">Login</a>
pankso@5 175 </div>
pankso@5 176
pankso@5 177 EOT
pankso@5 178 fi
pankso@5 179 cat << EOT
pankso@5 180 <!--
pankso@5 181 <div id="search">
pankso@5 182 <form method="get" action="$script">
pankso@5 183 <input type="text" name="search" placeholder="$(gettext "Search")" />
pankso@5 184 </form>
pankso@5 185 </div>
pankso@5 186 -->
pankso@5 187 EOT
pankso@5 188 }
pankso@5 189
pankso@5 190 # Link for online signup if enabled.
pankso@5 191 online_signup() {
pascal@121 192 if [ "$ONLINE_SIGNUP" = "yes" ]; then
pankso@5 193 echo -n "<p><a href='$script?signup'>"
pankso@5 194 gettext "Create a new account"
pankso@5 195 echo '</a></p>'
pankso@5 196 fi
pankso@5 197 }
pankso@5 198
pankso@5 199 # Login page
pankso@5 200 login_page() {
pankso@5 201 cat << EOT
pankso@5 202 <h2>$(gettext "Login")</h2>
pankso@5 203
pankso@5 204 <div id="account-info">
paul@61 205 $(gettext "No account yet or trouble with your account? Please send
pankso@5 206 a request to $ADMIN_MAIL with your real name, user name, mail and password.")
pankso@5 207 $(online_signup)
pankso@5 208 </div>
pankso@5 209
pankso@5 210 <div id="login">
pankso@5 211 <form method="post" action="$script">
pankso@5 212 <input type="text" name="auth" placeholder="$(gettext "User name")" />
pankso@5 213 <input type="password" name="pass" placeholder="$(gettext "Password")" />
pankso@5 214 <div>
pankso@5 215 <input type="submit" value="Login" /> $error
pankso@5 216 </div>
pankso@5 217 </form>
pankso@5 218 </div>
pankso@5 219
pankso@5 220 <div style="clear: both;"></div>
pankso@5 221 EOT
pankso@5 222 }
pankso@5 223
pankso@5 224 # Signup page
pankso@5 225 signup_page() {
pankso@5 226 cat << EOT
pankso@5 227
pankso@5 228 <div id="signup">
pankso@5 229 <form method="post" name="signup" action="$script" onsubmit="return checkSignup();">
pankso@5 230 <input type="hidden" name="signup" value="new" />
pankso@5 231 <input type="text" name="name" placeholder="$(gettext "Real name")" />
pankso@5 232 <input type="text" name="user" placeholder="$(gettext "User name")" />
pankso@5 233 <input type="text" name="mail" placeholder="$(gettext "Email")" />
pankso@5 234 <input type="password" name="pass" placeholder="$(gettext "Password")" />
pankso@5 235 <div>
pankso@5 236 <input type="submit" value="$(gettext "Create new account")" />
pankso@5 237 </div>
pankso@5 238 </form>
pankso@5 239 </div>
pankso@5 240
pankso@5 241 EOT
pankso@5 242 }
pankso@5 243
pankso@5 244 # Create a new user in AUTH_FILE and PEOPLE
pankso@5 245 new_user_config() {
pankso@106 246 if [ ! -f "$AUTH_FILE" ]; then
pankso@106 247 touch $AUTH_FILE && chmod 0600 $AUTH_FILE
pankso@20 248 fi
pankso@5 249 echo "$user:$pass" >> $AUTH_FILE
pankso@106 250 mkdir -pm0700 $PEOPLE/${user}
pankso@5 251 cat > $PEOPLE/$user/account.conf << EOT
pankso@104 252 # User configuration
pankso@5 253 NAME="$name"
pankso@5 254 USER="$user"
pankso@5 255 MAIL="$mail"
pankso@5 256 EOT
pankso@106 257 chmod 0600 $PEOPLE/$user/account.conf
pankso@28 258 # First created user is admin
pascal@121 259 if [ $(ls ${PEOPLE} | wc -l) = "1" ]; then
pankso@104 260 echo "$user" > ${ADMIN_USERS}
pankso@28 261 fi
pankso@5 262 }
pankso@5 263
paul@64 264 # The CM style parser. Just a title, simple text formatting and internal
paul@8 265 # links, as well as images and use HTML for other stuff. Keep it fast!
pankso@5 266 # To make TinyCM as easy as possible we have a small HTML editor/helper
pankso@5 267 # written in Javascript
pankso@5 268 wiki_parser() {
pankso@5 269 doc="[0-9a-zA-Z\.\#/~\_%=\?\&,\+\:@;!\(\)\*\$'\-]*"
pankso@5 270 sed \
pankso@5 271 -e s"#====\([^']*\)====#<h2>\1</h2>#"g \
pankso@5 272 -e s"#===\([^']*\)===#<h3>\1</h3>#"g \
pankso@5 273 -e s"#==\([^']*\)==#<h4>\1</h4>#"g \
pankso@13 274 -e s"#\*\*\([^']*\)\*\*#<b>\1</b>#"g \
pankso@5 275 -e s"#''\([^']*\)''#<em>\1</em>#"g \
pankso@5 276 -e s"#__\([^']*\)__#<u>\1</u>#"g \
pankso@5 277 -e s"#\[\([^]]*\)|\($doc\)\]#<a href='$script?d=\2'>\1</a>#"g \
pankso@17 278 -e s"#\[\([^]]*\)!\($doc\)\]#<a href='\2'>\1</a>#"g \
pankso@17 279 -e s"#\[\(http://*[^]]*.png\)\]#<img src='\1' />#"g \
pankso@91 280 -e s"#\[\([^]]*.png\)\]#<img src='content/cloud/\1' />#"g \
pankso@91 281 -e s"#@\([^']*\)@#<a href='$script?user=\1'>\1</a>#"g
pankso@5 282 }
pankso@5 283
pankso@5 284 link_user() {
pankso@15 285 echo "<a href='$(basename $script)?user=$user'>$user</a>"
pankso@5 286 }
pankso@5 287
pankso@5 288 # Save a document. Do we need more than 1 backup and diff ?
pankso@5 289 save_document() {
pankso@5 290 mkdir -p $cache/$d $(dirname $wiki/$d)
pankso@5 291 # May be a new page.
pankso@5 292 if [ ! -f "$wiki/$d.txt" ]; then
pankso@5 293 new=0
pankso@5 294 touch $wiki/$d.txt
pankso@5 295 fi
pankso@5 296 cp $wiki/$d.txt $cache/$d/last.bak
pankso@5 297 sed "s/$(echo -en '\r') /\n/g" > $wiki/$d.txt << EOT
pankso@5 298 $(GET content)
pankso@5 299 EOT
pankso@5 300 diff $cache/$d/last.bak $wiki/$d.txt > $cache/$d/last.diff
pankso@5 301 # Log
pankso@5 302 if [ "$new" ]; then
pankso@5 303 echo "Page created by: $(link_user)" | log
pankso@31 304 echo "New document: <a href='$script?d=$d'>$d</a>" | log_activity
pascal@121 305 if [ "$HG" = "yes" ]; then
pankso@5 306 cd $content && hg -q add
pankso@5 307 hg commit -q -u "$NAME <$MAIL>" -m "Created new document: $d"
pankso@5 308 cd $tiny
pankso@5 309 fi
pankso@5 310 else
pankso@28 311 # Here we may clean log: cat && tail -n 40
pankso@5 312 echo "Page edited by: $(link_user)" | log
pascal@121 313 if [ "$HG" = "yes" ]; then
pankso@5 314 cd $content && hg commit -q -u "$NAME <$MAIL>" \
pankso@5 315 -m "Edited document: $d"
pankso@5 316 cd $tiny
pankso@5 317 fi
pankso@5 318 fi
pankso@5 319 }
pankso@5 320
pankso@52 321 # CM tools (edit, diff, etc) for auth users
pankso@5 322 wiki_tools() {
pankso@52 323 if check_auth; then
pankso@52 324 cat << EOT
pankso@5 325 <div id="tools">
pankso@5 326 <a href="$script?edit=$d">$(gettext "Edit document")</a>
pankso@59 327 <a href="$script?log=$d">$(gettext "File log")</a>
pankso@5 328 <a href="$script?diff=$d">$(gettext "Last diff")</a>
pankso@59 329 $PLUGINS_TOOLS
pankso@52 330 EOT
pascal@121 331 [ "$HG" = "yes" ] && echo "<a href='$script?hg'>Hg Log</a>"
pankso@52 332 echo "</div>"
pankso@52 333 fi
pankso@52 334 }
pankso@52 335
pankso@59 336 # Built-in tools such as log/ls and PLUGINS_TOOLS
pankso@59 337 tiny_tools() {
pankso@52 338 if check_auth; then
pankso@52 339 cat << EOT
pankso@52 340 <div id='tools'>
pankso@52 341 <a href='$script?log'>Activity log</a>
pankso@52 342 <a href='$script?ls'>Pages list</a>
pankso@59 343 $PLUGINS_TOOLS
pankso@5 344 </div>
pankso@5 345 EOT
pankso@52 346 fi
pankso@5 347 }
pankso@5 348
pankso@5 349 # Get and display Gravatar image: get_gravatar email size
pankso@5 350 # Link to profile: <a href="http://www.gravatar.com/$md5">...</a>
pankso@5 351 get_gravatar() {
pankso@5 352 email=$1
pankso@5 353 size=$2
pankso@5 354 [ "$size" ] || size=48
pankso@5 355 url="http://www.gravatar.com/avatar"
pankso@5 356 md5=$(md5crypt $email)
pankso@5 357 echo "<img src='$url/$md5?d=identicon&s=$size' alt='&lowast;' />"
pankso@5 358 }
pankso@5 359
pankso@5 360 # List hg logs
pankso@5 361 hg_log() {
pankso@5 362 cd $content
pankso@5 363 cat << EOT
pankso@5 364 <table>
pankso@5 365 <thead>
pankso@5 366 <td>$(gettext "User")</td>
pankso@5 367 <td>$(gettext "Description")</td>
pankso@5 368 <td>$(gettext "Revision")</td>
pankso@5 369 </thead>
pankso@5 370 EOT
pankso@5 371 hg log --template "<tr><td>{author}</td><td>{desc}</td><td>{rev}</td></tr>\n"
pankso@5 372 echo '</table>'
pankso@5 373 }
pankso@5 374
pankso@5 375 #
pankso@5 376 # POST actions
pankso@5 377 #
pankso@5 378
pankso@5 379 case " $(POST) " in
pankso@5 380 *\ auth\ *)
pankso@5 381 # Authenticate user. Create a session file in $sessions to be used
pankso@5 382 # by check_auth. We have the user login name and a peer session
pankso@5 383 # md5 string in the COOKIE.
pankso@5 384 user="$(POST auth)"
pankso@5 385 pass="$(md5crypt "$(POST pass)")"
pankso@5 386 valid=$(fgrep "${user}:" $AUTH_FILE | cut -d ":" -f 2)
pascal@121 387 if [ "$pass" = "$valid" ] && [ "$pass" != "" ]; then
pankso@5 388 md5session=$(echo -n "$$:$user:$pass:$$" | md5sum | awk '{print $1}')
pankso@5 389 [ -d $sessions ] || mkdir -p $sessions
pankso@36 390 date '+%Y-%m-%d' > ${PEOPLE}/${user}/last
pankso@5 391 echo "$md5session" > $sessions/$user
pankso@5 392 header "Location: $script" \
pankso@5 393 "Set-Cookie: auth=$user:$md5session; HttpOnly"
pankso@5 394 else
pankso@5 395 header "Location: $script?login&error"
pankso@5 396 fi ;;
pankso@5 397 *\ signup\ *)
pankso@5 398 # POST action for signup
pankso@5 399 name="$(POST name)"
pankso@5 400 user="$(POST user)"
pankso@5 401 mail="$(POST mail)"
pankso@5 402 pass="$(md5crypt "$(POST pass)")"
pankso@5 403 if ! grep "^${user}:" $AUTH_FILE; then
pankso@5 404 new_user_config
pankso@5 405 header "Location: $script?login"
pankso@5 406 else
pankso@5 407 header
pankso@5 408 html_header
pankso@5 409 user_box
pankso@44 410 echo "<h2>$(gettext 'User already exists:') $user</h2>"
pankso@5 411 html_footer
pankso@5 412 fi ;;
pankso@5 413 esac
pankso@5 414
pankso@5 415 #
pankso@5 416 # Plugins
pankso@5 417 #
pankso@5 418 for p in $(ls -1 $plugins)
pankso@5 419 do
pankso@5 420 [ -f "$plugins/$p/$p.conf" ] && . $plugins/$p/$p.conf
pankso@5 421 [ -x "$plugins/$p/$p.cgi" ] && . $plugins/$p/$p.cgi
pankso@5 422 done
pankso@5 423
pankso@5 424 #
pankso@5 425 # GET actions
pankso@5 426 #
pankso@5 427
pankso@5 428 case " $(GET) " in
pankso@5 429 *\ edit\ *)
pankso@5 430 d="$(GET edit)"
pankso@5 431 header
pankso@5 432 html_header
pankso@5 433 user_box
pankso@5 434 get_lang
pankso@59 435 wiki_tools
pankso@5 436 if check_auth; then
pankso@5 437 cat << EOT
pankso@5 438 <h2>$(gettext "Edit $doc [ $i18n ]")</h2>
pankso@5 439
pankso@5 440 <div id="edit">
pankso@5 441
pankso@5 442 <form method="get" action="$script" name="editor">
pankso@5 443 <input type="hidden" name="save" value="$d" />
pankso@5 444 <textarea name="content">$(cat "$wiki/$d.txt")</textarea>
pankso@5 445 <input type="submit" value="$(gettext "Save document")" />
pankso@5 446 $(gettext "Code Helper:")
pankso@5 447 $(cat lib/jseditor.html)
pankso@5 448 </form>
pankso@5 449
pankso@5 450 </div>
pankso@5 451 EOT
pankso@5 452 else
paul@8 453 gettext "You must be logged in to edit pages"
pankso@5 454 fi
pankso@5 455 html_footer ;;
pankso@15 456
pankso@5 457 *\ save\ *)
pankso@5 458 d="$(GET save)"
pankso@5 459 if check_auth; then
pankso@76 460 save_document
pankso@5 461 fi
pankso@5 462 header "Location: $script?d=$d" ;;
pankso@15 463
pankso@5 464 *\ log\ *)
pankso@5 465 d="$(GET log)"
pankso@5 466 header
pankso@5 467 html_header
pankso@5 468 user_box
pankso@28 469 # Main activity
pascal@121 470 if [ "$d" = "log" ]; then
pankso@59 471 tiny_tools
pankso@52 472 echo "<h2>$(gettext "Activity log")</h2>"
pankso@28 473 echo '<pre>'
pankso@28 474 if [ -f "$cache/log/activity.log" ]; then
pankso@28 475 IFS="|"
pankso@28 476 tac $cache/log/activity.log | while read USER DATE LOG
pankso@28 477 do
pankso@28 478 . ${PEOPLE}/${USER}/account.conf
pankso@28 479 cat << EOT
pankso@28 480 <a href='$script?user=$USER'>$(get_gravatar $MAIL 24)</a>\
pankso@28 481 <span class='date'>$DATE -</span> $LOG
pankso@28 482 EOT
pankso@28 483 done
pankso@28 484 unset IFS
pankso@28 485 else
pankso@28 486 gettext "No activity log yet"; echo
pankso@28 487 fi
pankso@28 488 echo '</pre>'
pankso@28 489 html_footer && exit 0
pankso@28 490 fi
pankso@52 491 # Document activity
pankso@5 492 get_lang
pankso@52 493 wiki_tools
pankso@5 494 echo "<h2>$(gettext "Activity for:") <a href='$script?d=$d'>$d</a></h2>"
pankso@5 495 echo '<pre>'
pankso@5 496 if [ -f "$cache/$d/activity.log" ]; then
pankso@5 497 tac $cache/$d/activity.log
pankso@5 498 else
pankso@5 499 gettext "No log for: $d"; echo
pankso@5 500 fi
pankso@5 501 echo '</pre>'
pankso@5 502 html_footer ;;
pankso@28 503
pankso@28 504 *\ ls\ *)
pankso@28 505 d="Document list"
pankso@28 506 header
pankso@28 507 html_header
pankso@28 508 user_box
pankso@59 509 tiny_tools
pankso@59 510 [ ! check_auth ] && auth=0
pankso@119 511 cat << EOT
pankso@119 512 <h2>$(gettext "Pages list")</h2>
pankso@119 513 <div id="plugins">
pankso@119 514 <table>
pankso@119 515 <thead>
pankso@119 516 <td>$(gettext "Name")</td>
pankso@119 517 <td>$(gettext "Title")</td>
pankso@119 518 <td>$(gettext "Action")</td>
pankso@119 519 </thead>
pankso@119 520
pankso@119 521 EOT
pankso@28 522 cd ${wiki}
pankso@28 523 for d in $(find . -type f | sed s'/.\///')
pankso@28 524 do
pankso@119 525 d="${d%.txt}"
pankso@119 526 title=$(grep '^====' ${d}.txt | sed s'/====//'g)
pankso@119 527 echo "<tr><td><a href='$script?d=${d}'>${d}</a></td>"
pankso@119 528 echo "<td>$title</td>"
pankso@65 529 if [ "$auth" ]; then
pankso@65 530 cat << EOT
pankso@119 531 <td><a href="$script?edit=$d">$(gettext "Edit")</a> | \
pankso@119 532 <a href="$script?rm=$d">$(gettext "Remove")</a></td></tr>
pankso@48 533 EOT
pankso@65 534 else
pankso@119 535 echo "N/A"
pankso@65 536 fi
pankso@59 537 done && unset auth
pankso@119 538 echo '</table></div>'
pankso@28 539 html_footer ;;
pankso@48 540
pankso@48 541 *\ rm\ *)
pankso@59 542 [ ! check_auth ] && exit 1
pankso@48 543 d="$(GET rm)"
pankso@48 544 rm ${wiki}/"${d}"
pankso@48 545 rm -rf ${cache}/"${d%.txt}"
pankso@48 546 header "Location: $script?ls" ;;
pankso@15 547
pankso@5 548 *\ diff\ *)
pankso@5 549 d="$(GET diff)"
pankso@5 550 date="last"
pankso@5 551 header
pankso@5 552 html_header
pankso@5 553 user_box
pankso@5 554 get_lang
pankso@52 555 wiki_tools
pankso@5 556 echo "<h2>$(gettext "Diff for:") <a href='$script?d=$d'>$d</a></h2>"
pankso@5 557 echo '<pre>'
pankso@5 558 if [ -f "$cache/$d/$date.diff" ]; then
pankso@5 559 cat $cache/$d/$date.diff | sed \
pankso@5 560 -e 's|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|g' \
pankso@5 561 -e s"#^-\([^']*\).#<span style='color: red;'>\0</span>#"g \
pankso@5 562 -e s"#^+\([^']*\).#<span style='color: green;'>\0</span>#"g \
pankso@5 563 -e s"#@@\([^']*\)@@#<span style='color: blue;'>@@\1@@</span>#"g
pankso@5 564 else
pankso@59 565 gettext "No diff for:"; echo " $d"
pankso@5 566 fi
pankso@5 567 echo '</pre>'
pankso@5 568 html_footer ;;
pankso@15 569
pankso@5 570 *\ login\ *)
pankso@5 571 # The login page
pankso@5 572 d="Login"
pankso@5 573 [ "$(GET error)" ] && \
pankso@5 574 error="<p class="error">$(gettext "Bad login or pass")</p>"
pankso@5 575 header
pankso@5 576 html_header
pankso@5 577 user_box
pankso@5 578 login_page
pankso@5 579 html_footer ;;
pankso@15 580
pankso@5 581 *\ signup\ *)
pankso@5 582 # The login page
pankso@5 583 d="$(gettext "Sign Up")"
pankso@5 584 header
pankso@5 585 html_header
pankso@5 586 user_box
pankso@5 587 echo "<h2>$d</h2>"
pascal@121 588 if [ "$ONLINE_SIGNUP" = "yes" ]; then
pankso@5 589 signup_page
pankso@5 590 else
paul@8 591 gettext "Online registration is disabled"
pankso@5 592 fi
pankso@5 593 html_footer ;;
pankso@15 594
pankso@5 595 *\ logout\ *)
pankso@5 596 # Set a Cookie in the past to logout.
pankso@5 597 expires="Expires=Wed, 01-Jan-1980 00:00:00 GMT"
pankso@5 598 if check_auth; then
pankso@5 599 rm -f "$sessions/$user"
pankso@5 600 header "Location: $script" "Set-Cookie: auth=none; $expires; HttpOnly"
pankso@5 601 fi ;;
pankso@15 602
pankso@5 603 *\ user\ *)
pankso@76 604 # Basic user profile. Use the users plugin for more functions
pankso@15 605 d="$(GET user)"
pankso@36 606 last="$(cat $PEOPLE/"$(GET user)"/last)"
pankso@5 607 header
pankso@5 608 html_header
pankso@5 609 user_box
pankso@5 610 . $PEOPLE/"$(GET user)"/account.conf
pankso@36 611 cat << EOT
pankso@36 612 <h2>$(get_gravatar $MAIL) $NAME</h2>
pankso@36 613
pankso@36 614 <pre>
pankso@36 615 $(gettext "User name :") $USER
pankso@36 616 $(gettext "Last login :") $last
pankso@76 617 </pre>
pankso@36 618 EOT
pankso@5 619 html_footer ;;
pankso@15 620
pankso@59 621 *\ hg\ *)
pankso@59 622 d="Hg Log"
pankso@5 623 header
pankso@5 624 html_header
pankso@5 625 user_box
pankso@5 626 [ "$HG" != "yes" ] && gettext "Hg is disabled" && exit 0
pankso@5 627 [ ! -x /usr/bin/hg ] && gettext "Hg is not installed" && exit 0
pankso@5 628 echo "<h2>$d</h2>"
pankso@5 629 case " $(GET hg) " in
pankso@5 630 *\ init\ *)
pankso@5 631 if check_auth; then
pankso@5 632 [ -d "$content/.hg" ] && exit 0
pankso@5 633 echo '<pre>'
pankso@5 634 gettext "Executing: hg init"; echo
pankso@5 635 cd $content/ && hg init
pankso@5 636 echo '[hooks]' > .hg/hgrc
pankso@5 637 echo 'incoming = hg update' >> .hg/hgrc
paul@8 638 gettext "Adding current content and committing"; echo
pankso@59 639 [ ! -f "$wiki/index.txt" ] && default_index
pankso@5 640 hg add && hg commit -u "$NAME <$MAIL>" \
paul@22 641 -m "Initial commit with current content"
pankso@5 642 echo '</pre>' && cd ..
pankso@5 643 fi ;;
pankso@5 644 esac
pankso@5 645 hg_log
pankso@5 646 html_footer ;;
pankso@15 647
pankso@5 648 *)
pankso@5 649 # Display requested page
pankso@5 650 d="$(GET d)"
pankso@5 651 [ "$d" ] || d=$index
pankso@5 652 header
pankso@5 653 html_header
pankso@5 654 user_box
pankso@5 655 get_lang
pankso@52 656
pankso@34 657 # Generate a default index on first run
pankso@11 658 if [ ! -f "$wiki/$index.txt" ]; then
pankso@34 659 if ! default_index; then
paul@64 660 echo "<pre class='error'>Directory : content/ is not writeable</pre>"
pankso@32 661 html_footer && exit 0
pankso@32 662 fi
pankso@11 663 fi
pankso@52 664
pankso@34 665 # Check cache dir
pankso@34 666 if [ ! -w "$cache" ]; then
paul@64 667 echo "<pre class='error'>Directory : cache/ is not writeable"
pankso@34 668 echo "Command : install -m 0777 -d $tiny/cache</pre>"
pankso@34 669 html_footer && exit 0
pankso@34 670 fi
pankso@52 671
pankso@59 672 # Hg warning if enabled but not initiated
pascal@121 673 if [ "$HG" = "yes" ] && [ ! -d "$content/.hg" ]; then
pankso@52 674 echo '<p class="error box">'
pankso@52 675 gettext "Mercurial is enabled but no repository found"
pankso@59 676 echo ": <a href='$script?hg=init'>Hg init</a>"
pankso@52 677 echo '</p>'
pankso@52 678 fi
pankso@52 679
pankso@52 680 # Wiki tools
pankso@52 681 wiki_tools
pankso@52 682
pankso@34 683 # Wiki document
pankso@5 684 if [ ! -f "$wiki/$d.txt" ]; then
pankso@5 685 echo "<h2>$d</h2>"
pankso@5 686 gettext "The document does not exist. You can create it or read the"
pankso@59 687 echo " <a href='$script?d=en/help'>help</a>"
pankso@5 688 else
pankso@26 689 if fgrep -q [NOWIKI] $wiki/$d.txt; then
pankso@35 690 cat $wiki/$d.txt | sed '/\[NOWIKI\]/'d
pankso@5 691 else
pankso@5 692 cat $wiki/$d.txt | wiki_parser
pankso@5 693 fi
pankso@5 694 fi
pankso@5 695 html_footer ;;
pankso@5 696 esac
pankso@5 697
pankso@5 698 exit 0