コード例 #1
0
ファイル: userinfo.php プロジェクト: sedrion/moniwiki
function macro_UserInfo($formatter, $value, $options = array())
{
    global $DBInfo;
    if ($options['id'] == 'Anonymous' && !empty($options['q']) && empty($DBInfo->use_anonymous_editcount)) {
        return sprintf(_("You are not allowed to use the \"%s\" macro."), "UserInfo");
    }
    $offset = $off = !empty($options['offset']) ? $options['offset'] : 0;
    $limit = !empty($options['limit']) ? $options['limit'] : 100;
    // page
    $pg = !empty($options['p']) ? $options['p'] : 1;
    $q = !empty($options['q']) ? trim($options['q']) : '';
    $uid = !empty($options['uid']) ? $options['uid'] : '';
    $type = !empty($options['type']) ? trim($options['type']) : 'wait';
    $act = !empty($options['act']) ? trim($options['act']) : '';
    $comment = !empty($options['comment']) ? trim($options['comment']) : '';
    if (empty($uid) and !empty($q)) {
        $uids = (array) $q;
    } else {
        $uids = $uid;
    }
    if (empty($q) and !empty($uid) and sizeof($uid) == 1) {
        $q = $uid;
    }
    if ($limit > 100) {
        $limit = 100;
    }
    if ($pg > 1) {
        $off += ($pg - 1) * $limit;
    }
    $params = array('offset' => $off, 'limit' => $limit);
    $retval = array();
    $params['retval'] =& $retval;
    $udb =& $DBInfo->udb;
    $user =& $DBInfo->user;
    $members = $DBInfo->members;
    $ismember = $user->is_member;
    // set default query string
    if (!$ismember and empty($q)) {
        $q = $user->id;
        if ($q == 'Anonymous') {
            $q = $_SERVER['REMOTE_ADDR'];
        }
    }
    if (!empty($q) and empty($options['type'])) {
        $type = 'all';
    }
    $act = strtolower($act);
    $type = strtolower($type);
    $strs = array('all' => _("Total %d users found."), 'wait' => _("Total %d Permanently Suspended users found."), 'del' => _("Total %d Deleted users found."));
    if (!in_array($type, array('wait', 'del', 'monitor'))) {
        $type = 'all';
    }
    if (!empty($q)) {
        $params['q'] = $q;
    }
    if (!empty($type)) {
        $params['type'] = $type;
    }
    if (empty($act) and !empty($q)) {
        if ($udb->_exists($q)) {
            $type = 'all';
        } else {
            if ($udb->_exists($q, true)) {
                $params['type'] = $type = 'wait';
            }
        }
    }
    if (!empty($q) || $type != 'monitor') {
        $users = $udb->getUserList($params);
        $sz = sizeof($users);
        // not found anonymous IP address
        if ($sz == 0 and preg_match('@^(\\d{1,3}\\.){3}\\d{1,3}$@', $q)) {
            $users = array();
            $users[$q] = time();
            $sz = 1;
        }
    }
    // HACK to make simple message board
    if (!empty($comment) and empty($q) and $type == 'monitor') {
        $q = '127.0.0.1';
        $sz = 1;
        $users[$q] = time();
    }
    if ($type != 'monitor') {
        $title = $strs[$type];
        $title = sprintf($title, $retval['count']);
    } else {
        $title = _("Contributors Monitor");
    }
    $userinfo = '';
    $anchor = '';
    $extra = '';
    $cur = time();
    $min_ttl = !empty($DBInfo->user_suspend_time_default) ? intval($DBInfo->user_suspend_time_default) : 60 * 30;
    $allowed = $DBInfo->security_class == 'acl' && $DBInfo->security->is_allowed($options['action'], $options);
    if (!$allowed) {
        $allowed = in_array($user->id, $DBInfo->owners);
    }
    if ($allowed && $type == 'monitor' && $ismember) {
        $suspend_btn = _("Temporary Suspend User");
        $formhead = "<form method='POST' action=''>";
        $formtail = '';
        if ($DBInfo->security->is_protected('userinfo', $options)) {
            $formtail = _("Password") . ": <input type='password' name='passwd' /> ";
        }
        $formtail .= "<input type='hidden' name='action' value='userinfo' />";
        $formtail .= "<input type='hidden' name='type' value='{$type}' />";
        $formtail .= "<input type='hidden' name='act' value='pause' />";
        $formtail .= _("Summary") . " : <input type='text' size='80' name='comment' />";
        $formtail .= "<span class='button'><input class='button' type='submit' name='suspend' value='{$suspend_btn}' /></span> ";
        $formtail .= "</form>";
        // abusefilter cache
        $ac = new Cache_Text('abusefilter');
        // prepare to return
        $ret = array();
        $retval = array();
        $ret['retval'] =& $retval;
        if (!empty($uids) && in_array($act, array('inc', 'dec', 'reset', 'suspend', 'block', 'pause', 'clear'))) {
            if ($act == 'reset') {
                // clear abusefilter cache
                $msgid = _("%s: Reset editting information.");
            } else {
                if ($act == 'inc') {
                    // increse TTL
                    $msgid = _("%s: Increse monitoring time period.");
                } else {
                    if ($act == 'dec') {
                        // reduce TTL
                        $msgid = _("%s: Decrese monitoring time period.");
                    } else {
                        if ($act == 'pause') {
                            // pause more
                            $msgid = _("%s: Temporary pause 30 minutes.");
                        } else {
                            if ($act == 'block') {
                                // block
                                $msgid = _("%s: Temporary Block IP address.");
                            } else {
                                // clear
                                $msgid = _("%s: Clear Suspended state");
                            }
                        }
                    }
                }
            }
            $change = array();
            foreach ($uids as $q) {
                // fetch monitor information
                $info = $ac->fetch($q, 0, $ret);
                $ttl = 0;
                if ($info === false) {
                    $suspended = false;
                    if ($udb->_exists($q, true)) {
                        $suspended = true;
                    }
                    $uinfo = $udb->getInfo($q, $suspended);
                    $new_info = array('create' => 0, 'delete' => 0, 'revert' => 0, 'save' => 0, 'edit' => 0, 'add_lines' => 0, 'del_lines' => 0, 'add_chars' => 0, 'del_chars' => 0);
                    $new_info['id'] = $q;
                    if (isset($uinfo['remote'])) {
                        $new_info['ip'] = $uinfo['remote'];
                    }
                    $ttl = $min_ttl;
                } else {
                    $new_info = $info;
                    $ttl = $retval['ttl'] - (time() - $retval['mtime']);
                    $new_info['id'] = $q;
                }
                if ($act == 'reset') {
                    // reset edit information
                    $new_info = array_merge($new_info, array('create' => 0, 'delete' => 0, 'revert' => 0, 'save' => 0, 'edit' => 0));
                    $new_info['suspended'] = false;
                } else {
                    if ($act == 'clear') {
                        // clear suspended state
                        $new_info['suspended'] = false;
                        $new_info['comment'] = '';
                    } else {
                        if ($act == 'inc' || $act == 'dec') {
                            if ($ttl < 60 * 30) {
                                $inc = 60 * 30;
                            } else {
                                if ($ttl < 60 * 60) {
                                    $inc = 60 * 60;
                                } else {
                                    if ($ttl < 60 * 60 * 6) {
                                        $inc = 60 * 60 * 6;
                                    } else {
                                        if ($ttl < 60 * 60 * 12) {
                                            $inc = 60 * 60 * 12;
                                        } else {
                                            if ($ttl < 60 * 60 * 24) {
                                                $inc = 60 * 60 * 24;
                                            } else {
                                                if ($ttl < 60 * 60 * 24 * 7) {
                                                    $inc = 60 * 60 * 24 * 7;
                                                } else {
                                                    if ($ttl < 60 * 60 * 24 * 14) {
                                                        $inc = 60 * 60 * 24 * 14;
                                                    } else {
                                                        if ($ttl < 60 * 60 * 24 * 30) {
                                                            $inc = 60 * 60 * 24 * 30;
                                                        } else {
                                                            if ($ttl < 60 * 60 * 24 * 30 * 2) {
                                                                $inc = 60 * 60 * 24 * 30 * 2;
                                                            } else {
                                                                $inc = 60 * 60 * 24 * 30 * 6;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            $inc = max($min_ttl, $inc);
                            $ttl += $act == 'inc' ? $inc : -intval($inc / 2);
                            if ($ttl < 60 * 10) {
                                $ttl = 60 * 10;
                            } else {
                                if ($ttl > 60 * 60 * 24 * 364) {
                                    $ttl = 60 * 60 * 24 * 364;
                                }
                            }
                        } else {
                            if ($act == 'pause' || $act == 'block') {
                                $ttl += $min_ttl;
                                // pause and add minimum suspend time (default: 60*30)
                                $new_info['suspended'] = true;
                                if (!empty($comment)) {
                                    // add comment
                                    $comments = array();
                                    if (!empty($new_info['comment'])) {
                                        $comments = explode("\n", $new_info['comment']);
                                    }
                                    $comments[] = date('Y-m-d H:i', time()) . "\t" . $user->id . "\t" . $comment;
                                    if ($q == '127.0.0.1' and sizeof($comments) > 10) {
                                        array_shift($comments);
                                    } else {
                                        if (sizeof($comments) > 5) {
                                            array_shift($comments);
                                        }
                                    }
                                    $new_info['comment'] = implode("\n", $comments);
                                }
                            }
                        }
                    }
                }
                $ac->update($q, $new_info, $ttl);
                $change[] = $q;
            }
            // make title
            $title = sprintf($msgid, implode(',', $change));
        }
        $files = array();
        $ac->_caches($files, array('prefix' => 1));
        $list = '<table class="wiki editinfo">';
        $list .= '<tr><th>' . _("ID") . '</th></th><th>' . _("IP") . '</th><th>' . _("Last updated") . '</th>' . '<th>' . _("State") . '</th>' . '<th colspan="2">' . _("TTL") . '</th><th>' . _("Edits") . '</th><th>' . _("actions") . '</th></tr>';
        foreach ($files as $f) {
            // low level _fetch(), _remove()
            $info = $ac->_fetch($f, 0, $ret);
            if ($info === false) {
                $ac->_remove($f);
                continue;
            }
            if (!isset($info['id'])) {
                continue;
            }
            $ttl = $retval['ttl'] - (time() - $retval['mtime']);
            $tmp = $ttl;
            $d = intval($tmp / 60 / 60 / 24);
            $tmp -= $d * 60 * 60 * 24;
            $h = intval($tmp / 60 / 60);
            $tmp -= $h * 60 * 60;
            $m = intval($tmp / 60);
            $tmp -= $m * 60;
            $s = $tmp % 60;
            $ttl_time = '';
            if (!empty($d)) {
                $ttl_time = $d . ' ' . _("days") . ' ';
            }
            $ttl_time .= sprintf("%02d:%02d:%02d", $h, $m, $s);
            $check = array('create' => 'C', 'edit' => 'E', 'save' => 'S', 'delete' => 'X', 'revert' => 'R', 'revoke' => 'V');
            $edit = array('add_lines' => 'L+', 'add_chars' => 'C+', 'del_lines' => 'L-', 'del_chars' => 'C-');
            $class = array('add_lines' => 'diff-added', 'add_chars' => 'diff-added', 'del_lines' => 'diff-removed', 'del_chars' => 'diff-removed');
            $edits = array();
            foreach ($check as $c => $k) {
                if (!empty($info[$c])) {
                    $edits[] = '<span class="' . $c . '"><span>' . $k . '</span>' . '<span class="num">' . $info[$c] . '</span></span>';
                }
            }
            $out = implode(',', $edits);
            $edits = array();
            foreach ($edit as $c => $k) {
                if (!empty($info[$c])) {
                    $edits[] = '<span class="' . $class[$c] . '">' . $k . '' . $info[$c] . '</span>';
                }
            }
            $out .= '<br />' . implode('', $edits);
            $tag = '';
            $permanently_suspended = $udb->_exists($info['id'], true);
            if ($permanently_suspended) {
                $tag = '<span style="color:magenta">P</span>';
            }
            $anchor = 'a-' . substr(md5($info['id']), 0, 7);
            $list .= '<tr><td>';
            $list .= '<a name="' . $anchor . '"></a><input type="checkbox" name="uid[]" value="' . $info['id'] . '" />';
            $list .= '<a href="?action=userinfo&amp;type=all&q=' . $info['id'] . '"><span>' . $info['id'] . '</span></a></td>';
            if (isset($info['ip']) and $info['id'] != $info['ip']) {
                $list .= '<td>' . $info['ip'] . '</td>';
            } else {
                $list .= '<td>&nbsp;</td>';
            }
            $list .= '<td>' . date('Y-m-d H:i:s', $retval['mtime']) . '</td>';
            $list .= '<th>' . $tag . ($info['suspended'] ? "<span style='color:red'>S</span>" : '') . '</th>';
            $list .= '<th>' . $ttl_time . '</th>';
            $list .= '<td><a href="?action=userinfo&amp;type=monitor' . '&amp;act=inc&amp;q=' . $info['id'] . '"><span>&#9650;</span></a><br />';
            $list .= '<a href="?action=userinfo&amp;type=monitor' . '&amp;act=dec&amp;q=' . $info['id'] . '"><span>&#9660;</span></a>';
            $list .= '</td>';
            $list .= '<td><span class="editinfo">' . $out . '</span></td>';
            $list .= '<td>';
            $list .= '<a class="button-small" href="?action=userinfo&amp;type=monitor' . '&amp;act=pause&amp;q=' . $info['id'] . '"><span>' . _("Suspend") . '</span></a> ';
            $list .= '<a class="button-small" href="?action=userinfo&amp;type=monitor' . '&amp;act=reset&amp;q=' . $info['id'] . '"><span>' . _("Reset") . '</span></a> ';
            $list .= '<a class="button-small" href="?action=userinfo&amp;type=monitor' . '&amp;act=clear&amp;q=' . $info['id'] . '"><span>' . _("Clear") . '</span></a> ';
            $list .= '<a class="button-small" href="?action=userinfo&amp;type=monitor' . '&amp;act=block&amp;q=' . $info['ip'] . '"><span>' . _("Block IP") . '</span></a> ';
            $list .= '</td>';
            $list .= '</tr>';
            if (!empty($info['comment'])) {
                $comments = explode("\n", $info['comment']);
                $comment = '<ul>';
                foreach ($comments as $c) {
                    list($date, $by, $log) = explode("\t", $c);
                    $comment .= '<li>[' . $date . '] ' . $log . ' --' . $by . '</li>' . "\n";
                }
                $comment .= '</ul>';
                $list .= '<tr><td>&nbsp;</td><td colspan="7"><div class="msgboard">' . $comment . '</div></td></tr>';
            }
        }
        $list .= '</table>';
        $extra = '<ul>';
        $extra .= '<li>' . '<strong style="color:magenta">P</strong>' . ':' . _("Permanently Suspended") . '</li>';
        $extra .= '<li>' . '<strong style="color:red">S</strong>' . ':' . _("Temporary Suspended") . '</li>';
        $extra .= '</ul>';
    } else {
        if ($sz == 1 && $allowed) {
            // abusefilter cache
            $ac = new Cache_Text('abusefilter');
            $actions = array();
            if (!empty($DBInfo->userinfo_actions) and is_array($DBInfo->userinfo_actions)) {
                $actions = $DBInfo->userinfo_actions;
            }
            $keys = array_keys($users);
            $hide_infos = array('bookmark', 'password', 'scrapped_pages', 'quicklinks', 'ticket', 'tz_offset');
            $inf = $udb->getInfo($keys[0], $type != 'all');
            unset($inf['eticket']);
            // hide eticket
            if ($ismember) {
                $allowed_infos = array_keys($inf);
            } else {
                $allowed_infos = array('nick', 'home', 'edit_count', 'edit_add_lines', 'edit_add_chars', 'edit_del_lines', 'edit_del_chars', 'strike_total', 'strikeout_total');
            }
            $addr = !empty($inf['remote']) ? $inf['remote'] : '';
            unset($inf['remote']);
            $anchor = '#a-' . substr(md5($keys[0]), 0, 7);
            $id_form = '';
            $ip_form = '';
            if (!empty($actions)) {
                $url = qualifiedUrl($formatter->link_url($formatter->page->urlname));
                $action_form = ' <form style="display:inline;margin:0" method="get" action="' . $url . '">';
                $action_form .= '<select name="action" onchange="if (this.selectedIndex != 0) this.form.submit();">';
                $action_form .= '<option value="">----</option>';
                foreach ($actions as $a) {
                    $action_form .= '<option value="' . $a . '">' . _($a) . "</option>\n";
                }
                $id_form = $action_form . '<input type="hidden" name="q" value="' . _html_escape($keys[0]) . '">' . "</select></form>\n";
                $ip_form = $action_form . '<input type="hidden" name="q" value="' . $addr . '">' . "</select></form>\n";
            }
            $list = '<table class="info">';
            $list .= '<tr><th>' . _("ID") . '/' . _("IP") . '</th></th><td>' . $keys[0] . $id_form . '</td></tr>';
            if (!empty($addr) and $keys[0] != $addr && !in_array($keys[0], $members)) {
                $list .= '<tr><th>' . _("IP") . '</th></th><td>' . $addr . $ip_form . '</td></tr>';
            }
            if (!empty($DBInfo->use_avatar) && !empty($addr) && !empty($DBInfo->use_uniq_avatar)) {
                $avatar_type = 'identicon';
                if (is_string($DBInfo->use_avatar)) {
                    $avatar_type = $DBInfo->use_avatar;
                }
                $avatarlink = qualifiedUrl($formatter->link_url('', '?action=' . $avatar_type . '&amp;seed='));
                $uniq_avatar = $DBInfo->use_uniq_avatar;
                if ($ismember) {
                    $uniq_avatar = 'Y';
                }
                // change avatar after year :>
                $key = $addr . $uniq_avatar;
                if (!$ismember) {
                    $key .= $q;
                }
                // not a member: show different avatar for login user
                $crypted = md5($key);
                $mylnk = preg_replace('/seed=/', 'seed=' . $crypted, $avatarlink);
                // for user defined avatar
                $mylnk .= '&amp;user='******'<tr><th>' . _("Avatar") . '</th></th><td><img src="' . $mylnk . '" /></td></tr>';
            }
            foreach ($allowed_infos as $k) {
                if (!in_array($k, $hide_infos) and !empty($inf[$k])) {
                    $list .= '<tr><th>' . $k . '</th><td>' . $inf[$k] . '</td></tr>';
                }
            }
            $info = $ac->fetch($keys[0]);
            if ($info !== false && isset($info['suspended']) and $info['suspended'] == 'true') {
                $list .= '<tr><th>' . _("Status") . '</th><th style="color:red">' . _("Temporary Suspended") . '</th></tr>';
            }
            $list .= '</table>';
            $userinfo = $list;
            $list = '';
            if ($type == 'all') {
                $btn = _("Delete User");
            } else {
                if ($type == 'del' or $type == 'wait') {
                    $btn = _("Activate User");
                }
            }
            $suspend_btn = _("Permanently Suspend User");
            if (!$ismember && $q == $user->id) {
                $pause_btn = _("Temporary Suspend Me!");
            } else {
                if ($ismember) {
                    $pause_btn = _("Temporary Suspend User");
                }
            }
            $comment_btn = _("Comment");
            $formhead = "<form method='POST' action=''>";
            $formtail = '';
            if ($DBInfo->security->is_protected('userinfo', $options)) {
                $formtail = _("Password") . ": <input type='password' name='passwd' /> ";
            }
            $formtail .= "<input type='hidden' name='action' value='userinfo' />";
            $formtail .= "<input type='hidden' name='type' value='{$type}' />";
            $formtail .= "<input type='hidden' name='uid' value='{$keys['0']}' />";
            // comments
            $mb = new Cache_Text('msgboard');
            if (($info = $mb->fetch($q, 0, $ret)) !== false) {
                if (!empty($info['comment'])) {
                    $comments = explode("\n", $info['comment']);
                    $comment = '<ul>';
                    foreach ($comments as $c) {
                        list($date, $by, $log) = explode("\t", $c);
                        $comment .= '<li>[' . $date . '] ' . $log . ' --' . $by . '</li>' . "\n";
                    }
                    $comment .= '</ul>';
                    $formtail .= '<div class="msgboard">' . $comment . '</div>';
                }
            }
            // send comment
            $formtail .= "<div>" . _("Message") . ": <input type='text' name='comment' size='80' /> </div>";
            if (($ismember or $q == $user->id) && $type == 'all') {
                $formtail .= "<span class='button'><input class='button' type='submit' name='pause' value='{$pause_btn}' /></span> ";
            }
            if ($ismember && $type != 'wait') {
                $formtail .= "<span class='button'><input class='button' type='submit' name='suspend' value='{$suspend_btn}' /></span> ";
            }
            if ($ismember) {
                $formtail .= "<span class='button'><input class='button' type='submit' value='{$btn}' /></span> ";
            }
            $formtail .= "<span class='button'><input class='button' type='submit' name='comment_btn' value='{$comment_btn}' /></span> ";
            $formtail .= "</form>";
            // do not show form for non members
            //if (!$ismember)
            //    $formtail = $formhead = '';
        } else {
            if ($allowed && $ismember) {
                $names = array_keys($users);
                $pages = intval($retval['count'] / $limit);
                $query = '?action=userinfo';
                if ($limit != 100) {
                    $query .= '&amp;limit=' . $limit;
                }
                if (!empty($offset)) {
                    $query .= '&amp;offset=' . $offset;
                }
                // paginate
                $pnut = '';
                if ($pages > 0) {
                    $pnut = get_pagelist($formatter, $pages, $query . '&amp;p=', $pg);
                }
                for ($i = 0; $i < $limit && $i < $sz; $i++) {
                    $u = $names[$i];
                    $mtime = $users[$u];
                    $test = $cur - $mtime;
                    if ($test > 60 * 60 * 24 * 365 * 2) {
                        $color = '#c0c0c0';
                    } else {
                        if ($test > 60 * 60 * 24 * 365) {
                            $color = 'blue';
                        } else {
                            if ($test > 60 * 60 * 24 * 30 * 6) {
                                $color = 'green';
                            } else {
                                if ($test > 60 * 60 * 24 * 30) {
                                    $color = '#ff00ff';
                                } else {
                                    $color = '#ff0000';
                                }
                            }
                        }
                    }
                    $date = date("Y-m-d H:i:s", $mtime);
                    $list .= '<li><input type="checkbox" name="uid[]" value="' . $u . '"/>' . '<a href="?action=userinfo&amp;type=' . $type . '&amp;q=' . $u . '">' . $u . "</a> (<span style='color:" . $color . "'>" . $date . "</span>)</li>\n";
                }
                $list = "<ul>\n" . $list . "</ul>\n";
                $formhead = "<form method='POST' action=''>";
                $formtail = '';
                if ($type == 'all') {
                    $btn = _("Delete Users");
                } else {
                    if ($type == 'del' or $type == 'wait') {
                        $btn = _("Activate Users");
                    }
                }
                if ($type != 'wait') {
                    $btn2 = _("Permanently Suspend Users");
                }
                if ($DBInfo->security->is_protected('userinfo', $options)) {
                    $formtail = _("Password") . ": <input type='password' name='passwd' /> ";
                }
                $formtail .= "<input type='hidden' name='action' value='userinfo' />" . "<input type='hidden' name='type' value='{$type}' />" . "<span class='button'><input class='button' type='submit' value='{$btn}' /></span> ";
                if ($type != 'wait') {
                    $formtail .= "<span class='button'><input class='button' type='submit' name='suspend' value='{$btn2}' /></span> ";
                }
                $formtail .= "</form>";
                $select = "<select name='type'>\n";
                foreach (array('ALL' => 'all', 'WAIT' => 'wait', 'DELETED' => 'del') as $k => $v) {
                    if ($type == $v) {
                        $checked = ' selected="selected"';
                    } else {
                        $checked = '';
                    }
                    $select .= "<option value='{$v}'{$checked}>{$k}</option>";
                }
                $select .= "</select>";
                $formtail .= "<form method='GET'>" . $select . "<input type='hidden' name='action' value='userinfo' />" . "<input type='text' name='q' value='' placeholder='Search' />";
                $formtail .= "</form>";
                $formtail .= $pnut;
            } else {
                if (!empty($DBInfo->use_userinfo)) {
                    foreach ($users as $u => $v) {
                        $list .= '<li>' . $u . "</li>\n";
                    }
                } else {
                    $list .= '<li>' . _("User infomation is restricted by wikimaster") . "</li>\n";
                }
                $list = '<ul>' . "\n" . $list . '</ul>' . "\n";
            }
        }
    }
    if ($allowed && $ismember) {
        if ($type != 'monitor') {
            $extra .= '<a href="?action=userinfo&amp;type=monitor' . $anchor . '" class="button"><span>' . _("Contributors Monitor") . "</span></a>";
        } else {
            $extra .= '<a href="?action=userinfo" class="button"><span>' . _("Permanently Suspended Users") . "</span></a> " . '<a href="?action=userinfo&amp;type=monitor" class="button"><span>' . _("Refresh") . "</span></a>";
        }
    }
    return "<h2>" . $title . "</h2>\n" . $userinfo . $formhead . $list . $formtail . $extra;
}
コード例 #2
0
ファイル: wikilib.php プロジェクト: reviforks/moniwiki
function macro_TitleIndex($formatter, $value, $options = array())
{
    global $DBInfo;
    $pc = !empty($DBInfo->titleindex_pagecount) ? intval($DBInfo->titleindex_pagecount) : 100;
    if ($pc < 1) {
        $pc = 100;
    }
    $pg = empty($options['p']) ? 1 : intval($options['p']);
    if ($pg < 1) {
        $pg = 1;
    }
    $group = $formatter->group;
    $key = -1;
    $keys = array();
    if ($value == '' or $value == 'all') {
        $sel = '';
    } else {
        $sel = ucfirst($value);
    }
    // get all keys
    $all_keys = get_keys();
    if (isset($sel[0])) {
        if (!isset($all_keys[$sel])) {
            $sel = key($all_keys);
        }
        // default
    }
    if (@preg_match('/' . $sel . '/i', '') === false) {
        $sel = '';
    }
    $titleindex = array();
    // cache titleindex
    $kc = new Cache_text('titleindex');
    $delay = !empty($DBInfo->default_delaytime) ? $DBInfo->default_delaytime : 0;
    $uid = '';
    if (function_exists('posix_getuid')) {
        $uid = '.' . posix_getuid();
    }
    $lock_file = _fake_lock_file($DBInfo->vartmp_dir, 'titleindex' . $uid);
    $locked = _fake_locked($lock_file, $DBInfo->mtime());
    if ($locked or $kc->exists('key') and $DBInfo->checkUpdated($kc->mtime('key'), $delay)) {
        if (!empty($formatter->use_group) and $formatter->group) {
            $keys = $kc->fetch('key.' . $formatter->group);
            $titleindex = $kc->fetch('titleindex.' . $formatter->group);
        } else {
            $keys = $kc->fetch('key');
            $titleindex = $kc->fetch('titleindex' . $sel);
        }
        if (isset($sel[0]) and isset($titleindex[$sel])) {
            $all_pages = $titleindex[$sel];
        }
        if (empty($titleindex) and $locked) {
            // no cache found
            return _("Please wait...");
        }
    }
    if (empty($all_pages)) {
        $all_pages = array();
        $indexer = $DBInfo->lazyLoad('titleindexer');
        if (!empty($formatter->use_group) and $formatter->group) {
            $group_pages = $indexer->getLikePages('^' . $formatter->group);
            foreach ($group_pages as $page) {
                $all_pages[] = str_replace($formatter->group, '', $page);
            }
        } else {
            $all_pages = $indexer->getLikePages('^' . $all_keys[$sel], 0);
        }
        #natcasesort($all_pages);
        #sort($all_pages,SORT_STRING);
        //usort($all_pages, 'strcasecmp');
        $pages = array_flip($all_pages);
        if (!empty($formatter->use_group)) {
            array_walk($pages, '_setpagekey');
        } else {
            array_walk($pages, create_function('&$p, $k', '$p = $k;'));
        }
        $all_pages = array_flip($pages);
        uksort($all_pages, 'strcasecmp');
    }
    if (empty($keys) or empty($titleindex)) {
        _fake_lock($lock_file);
        foreach ($all_pages as $page => $rpage) {
            $p = ltrim($page);
            $pkey = get_key("{$p}");
            if ($key != $pkey) {
                $key = $pkey;
                //$keys[] = $pkey;
                if (!isset($titleindex[$pkey])) {
                    $titleindex[$pkey] = array();
                }
            }
            $titleindex[$pkey][$page] = $rpage;
        }
        $keys = array_keys($all_keys);
        if (!empty($tlink)) {
            $keys[] = 'all';
        }
        if (!empty($formatter->use_group) and $formatter->group) {
            $kc->update('key.' . $formatter->group, $keys);
            $kc->update('titleindex.' . $formatter->group, $titleindex);
        } else {
            $kc->update('key', $keys);
            $kc->update('titleindex' . $sel, $titleindex);
        }
        if (isset($sel[0]) and isset($titleindex[$sel])) {
            $all_pages = $titleindex[$sel];
        }
        _fake_lock($lock_file, LOCK_UN);
    }
    $pnut = null;
    if (isset($sel[0]) and count($all_pages) > $pc) {
        $pages_number = intval(count($all_pages) / $pc);
        if (count($all_pages) % $pc) {
            $pages_number++;
        }
        $pages = array_keys($all_pages);
        $pages = array_splice($pages, ($pg - 1) * $pc, $pc);
        $selected = array();
        foreach ($pages as $p) {
            $selected[$p] = $all_pages[$p];
        }
        $pages = $selected;
        $pnut = get_pagelist($formatter, $pages_number, '?action=titleindex&amp;sec=' . $sel . '&amp;p=', !empty($pg) ? $pg : 1);
    } else {
        $pages =& $all_pages;
    }
    //print count($all_pages);
    //exit;
    $out = '';
    #  if ($DBInfo->use_titlecache)
    #    $cache=new Cache_text('title');
    $key = '';
    foreach ($pages as $page => $rpage) {
        $p = ltrim($page);
        $pkey = get_key("{$p}");
        if ($key != $pkey) {
            $key = $pkey;
            if (isset($sel[0]) and !preg_match('/^' . $sel . '/i', $pkey)) {
                continue;
            }
            if (!empty($out)) {
                $out .= "</ul>";
            }
            $out .= "<a name='{$key}'></a><h3><a href='#top'>{$key}</a></h3>\n";
            $out .= "<ul>";
        }
        if (isset($sel[0]) and !preg_match('/^' . $sel . '/i', $pkey)) {
            continue;
        }
        #
        #    if ($DBInfo->use_titlecache and $cache->exists($page))
        #      $title=$cache->fetch($page);
        #    else
        $title = get_title($rpage, $page);
        #$out.= '<li>' . $formatter->word_repl('"'.$page.'"',$title,'',0,0);
        $urlname = _urlencode($group . $rpage);
        $out .= '<li>' . $formatter->link_tag($urlname, '', _html_escape($title));
        $keyname = $DBInfo->pageToKeyname(urldecode($rpage));
        if (is_dir($DBInfo->upload_dir . "/{$keyname}") or !empty($DBInfo->use_hashed_upload_dir) and is_dir($DBInfo->upload_dir . '/' . get_hashed_prefix($keyname) . $keyname)) {
            $out .= ' ' . $formatter->link_tag($urlname, "?action=uploadedfiles", $formatter->icon['attach']);
        }
        $out .= "</li>\n";
    }
    if (!empty($pnut)) {
        $out .= '<li style="list-style:none">' . $pnut . '</li>' . "\n";
    }
    $out .= "</ul>\n";
    $index = '';
    $tlink = '';
    if (isset($sel[0])) {
        $tlink = $formatter->link_url($formatter->page->urlname, '?action=titleindex&amp;sec=');
    }
    $index = array();
    foreach ($keys as $key) {
        $name = strval($key);
        $tag = '#' . $key;
        $link = !empty($tlink) ? preg_replace('/sec=/', 'sec=' . _urlencode($key), $tlink) : '';
        if ($name == 'Others') {
            $name = _("Others");
        } else {
            if ($name == 'all') {
                $name = _("Show all");
            }
        }
        $index[] = "<a href='{$link}{$tag}'>{$name}</a>";
    }
    $str = implode(' | ', $index);
    return "<center><a name='top'></a>{$str}</center>\n{$out}";
}
コード例 #3
0
ファイル: Gallery.php プロジェクト: ahastudio/moniwiki
function macro_Gallery($formatter, $value, &$options)
{
    global $DBInfo;
    # add some actions at the bottom of the page
    if (!$value and !in_array('UploadFile', $formatter->actions)) {
        $formatter->actions[] = 'UploadFile';
        $formatter->actions[] = 'UploadedFiles';
    }
    $default_column = 3;
    $default_row = 4;
    $col = (!empty($options['col']) and $options['col'] > 0) ? (int) $options['col'] : 0;
    $row = (!empty($options['row']) and $options['row'] > 0) ? (int) $options['row'] : 0;
    $sort = !empty($options['sort']) ? $options['sort'] : '';
    $nocomment = !empty($options['nocomment']) ? $options['nocomment'] : '';
    $href_attr = '';
    if (!empty($DBInfo->gallery_use_lightbox) and !empty($DBInfo->use_lightbox)) {
        $use_lightbox = 1;
        if (is_string($DBInfo->gallery_use_lightbox)) {
            $href_attr = ' rel="' . $DBInfo->gallery_use_lightbox . '[gallery]" ';
        } else {
            $href_attr = ' rel="lightbox[gallery]" ';
        }
    }
    // parse args
    preg_match("/^(('|\")([^\\2]+)\\2)?,?(\\s*,?\\s*.*)?\$/", $value, $match);
    $opts = explode(',', $match[4]);
    foreach ($opts as $opt) {
        if ($opt == 'showall') {
            $show_all = 1;
        } else {
            if ($opt == 'nocomment') {
                $nocomment = 1;
            } else {
                if (($p = strpos($opt, '=')) !== false) {
                    $k = substr($opt, 0, $p);
                    $v = substr($opt, $p + 1);
                    if ($k == 'col') {
                        $col = $v;
                    } else {
                        if ($k == 'row') {
                            $row = $v;
                        } else {
                            if ($k == 'sort') {
                                $sort = $v;
                            }
                        }
                    }
                } else {
                    if ($opt == 'sort') {
                        $sort = 1;
                    }
                }
            }
        }
    }
    if (!in_array($sort, array(0, 1, 'name', 'date'))) {
        $sort = 0;
    }
    $img_default_width = 150;
    $col_td_width = '';
    if ($col > 1) {
        $col_td_width = (int) (100 / $col);
        $col_td_width = ' width="' . $col_td_width . '%"';
        $img_default_width = (int) (100 / $col) * 5;
        // XXX assume 500px
    }
    $default_width = !empty($DBInfo->gallery_img_width) ? $DBInfo->gallery_img_width : 600;
    $img_class = "gallery-img";
    $col = ($col <= 0 or $col > 10) ? $default_column : $col;
    $row = ($row <= 0 or $row > 7) ? $default_row : $row;
    $perpage = $col * $row;
    $img_style = '';
    if ($col == 1) {
        $img_style = ' style="float:left"';
    }
    if (!empty($match[3])) {
        # arg has a pagename
        $value = $match[3];
    } else {
        $value = $formatter->page->name;
    }
    $key = $DBInfo->pageToKeyname($value);
    if ($key != $value) {
        $prefix = $formatter->link_url(_rawurlencode($value), "?action=download&amp;value=");
    }
    $dir = $DBInfo->upload_dir . "/{$key}";
    if (empty($prefix)) {
        $prefix = $DBInfo->url_prefix . "/" . $dir . "/";
    }
    if (!file_exists($dir)) {
        umask(00);
        mkdir($dir, 0777);
    }
    $top_link = '';
    $bot_link = '';
    $upfiles = array();
    $comments = array();
    if (file_exists($dir . "/list.txt")) {
        $cache = file($dir . "/list.txt");
        foreach ($cache as $line) {
            #list($name,$mtime,$comment)=explode("\t",rtrim($line),3);
            $tmp = explode("\t", rtrim($line), 3);
            $name = $tmp[0];
            $upfiles[$name] = $tmp[1];
            if (isset($tmp[2])) {
                $comments[$name] = $tmp[2];
            }
        }
    }
    if ($sort) {
        if ($sort == 1) {
            arsort($upfiles);
        } elseif ($sort == 'name') {
            ksort($upfiles);
        }
    } else {
        asort($upfiles);
    }
    if (!empty($options['value'])) {
        $file = urldecode($options['value']);
    }
    if (!empty($file) and !empty($upfiles[$file]) and !empty($options['comments'])) {
        // admin: edit all comments
        $comment = _stripslashes($options['comments']);
        $comment = str_replace("<", "&lt;", $comment);
        $comment = str_replace("\r", "", $comment);
        $comment = preg_replace("/\n----\n/", "\t", $comment);
        $comment = str_replace("\n", "\\n", $comment);
        $comments[$file] = $comment;
        $update = 1;
    } else {
        if (!empty($file) and !empty($upfiles[$file]) and !empty($options['comment'])) {
            // add new comment
            $comment = $text = _stripslashes($options['comment']);
            // spam filtering
            $fts = preg_split('/(\\||,)/', $DBInfo->spam_filter);
            foreach ($fts as $ft) {
                $text = $formatter->filter_repl($ft, $text, $options);
            }
            if ($text != $comment) {
                $options['err'] = _("Sorry, can not save page because some messages are blocked in this wiki.");
            } else {
                if ($options['id'] == 'Anonymous') {
                    $name = $_SERVER['REMOTE_ADDR'];
                } else {
                    $name = $options['id'];
                }
                if ($options['name']) {
                    $name = $options['name'];
                }
                $date = date("(Y-m-d H:i:s) ");
                $comment = str_replace("\r", "", $comment);
                $comment = str_replace("\n", "\\n", $comment);
                $comment = str_replace("\t", " ", $comment);
                $comment = str_replace("<", "&lt;", $comment);
                $comment .= " -- {$name} {$date}";
                $comments[$file] = $comment . "\t" . $comments[$file];
                $update = 1;
            }
        } else {
            if (!empty($file) and !empty($upfiles[$file])) {
                // show comments of the selected item
                $mtime = $upfiles[$file];
                $comment = !empty($comments[$file]) ? $comments[$file] : '';
                $values = array_keys($upfiles);
                $prev_value = $values[array_search($file, $values) - 1];
                $next_value = $values[array_search($file, $values) + 1];
                unset($values);
                $upfiles = array();
                $comments = array();
                $upfiles[$file] = $mtime;
                $comments[$file] = $comment;
                $selected = 1;
                $img_class = "gallery-sel";
                if (!empty($prev_value)) {
                    $prev_link = "<div class='gallery-prev-link'><a href='" . $formatter->link_url($formatter->page->urlname, "?action=gallery&amp;value={$prev_value}") . "'><span class='gallery-prev-text'>&#171;Prev</span></a></div>";
                } else {
                    $prev_link = '';
                }
                if (!empty($next_value)) {
                    $next_link = "<div class='gallery-next-link'><a href='" . $formatter->link_url($formatter->page->urlname, "?action=gallery&amp;value={$next_value}") . "'><span class='gallery-next-text'>Next&#187;</span></a></div>";
                } else {
                    $next_link = '';
                }
                if (!empty($next_link) or !empty($prev_link)) {
                    $top_link = "<div class='gallery-top-link'>{$prev_link}{$next_link}</div>";
                    $bot_link = "<div class='gallery-bottom-link'>{$prev_link}{$next_link}</div>";
                }
            }
        }
    }
    $width = !empty($selected) ? $default_width : $img_default_width;
    $thumb_width = !empty($DBInfo->thumb_width) ? $DBInfo->thumb_width : '250';
    $mtime = file_exists($dir . "/list.txt") ? filemtime($dir . "/list.txt") : 0;
    if (filemtime($dir) > $mtime or !empty($update)) {
        unset($upfiles);
        $handle = opendir($dir);
        $cache = '';
        $cr = '';
        while ($file = readdir($handle)) {
            if ($file[0] == '.' or $file == 'list.txt' or is_dir($dir . "/{$file}")) {
                continue;
            }
            $mtime = filemtime($dir . "/" . $file);
            $cache .= $cr . $file . "\t" . $mtime;
            $upfiles[$file] = $mtime;
            if (!empty($comments[$file])) {
                $cache .= "\t" . $comments[$file];
            }
            $cr = "\n";
        }
        closedir($handle);
        $fp = @fopen($dir . "/list.txt", 'w');
        if ($fp) {
            fwrite($fp, $cache);
            fclose($fp);
        }
    }
    if (empty($upfiles)) {
        return "<h3>" . _("No files found") . "</h3>\n";
    }
    $out = "<table width='100%' border='0' cellpadding='2'>\n<tr>\n";
    $idx = 1;
    $pages = intval(sizeof($upfiles) / $perpage);
    if (sizeof($upfiles) % $perpage) {
        $pages++;
    }
    if (!empty($options['p']) and $options['p'] > 1) {
        $slice_index = $perpage * intval($options['p'] - 1);
        $upfiles = array_slice($upfiles, $slice_index);
    }
    $extra = $sort ? "&amp;sort=" . $sort : '';
    $extra .= $nocomment ? "&amp;nocomment=1" : '';
    $pnut = '';
    if ($pages > 1) {
        $pnut = get_pagelist($formatter, $pages, '?action=gallery&amp;col=' . $col . '&amp;row=' . $row . $extra . '&amp;p=', !empty($options['p']) ? $options['p'] : '', $perpage);
    }
    if (!file_exists($dir . "/thumbnails")) {
        @mkdir($dir . "/thumbnails", 0777);
    }
    while (list($file, $mtime) = each($upfiles)) {
        $size = filesize($dir . "/" . $file);
        $id = rawurlencode($file);
        $linksrc = $key == $value ? $prefix . $id : str_replace('value=', 'value=' . $id, $prefix);
        $link = (!empty($selected) or !empty($use_lightbox)) ? $linksrc : $formatter->link_url($formatter->page->urlname, "?action=gallery{$extra}&amp;value={$id}");
        $date = date("Y-m-d", $mtime);
        if (preg_match("/\\.(jpg|jpeg|gif|png)\$/i", $file)) {
            if (!empty($DBInfo->use_convert_thumbs) and !file_exists($dir . "/thumbnails/" . $file)) {
                if (function_exists('gd_info')) {
                    $fname = $dir . '/' . $file;
                    list($w, $h) = getimagesize($fname);
                    if ($w > $thumb_width) {
                        $nh = $thumb_width * $h / $w;
                        $thumb = imagecreatetruecolor($thumb_width, $nh);
                        // XXX only jpeg for testing now.
                        if (preg_match("/\\.(jpg|jpeg)\$/i", $file)) {
                            $imgtype = 'jpeg';
                        } else {
                            if (preg_match("/\\.png\$/i", $file)) {
                                $imgtype = 'png';
                            } else {
                                if (preg_match("/\\.gif\$/i", $file)) {
                                    $imgtype = 'gif';
                                }
                            }
                        }
                        $myfunc = 'imagecreatefrom' . $imgtype;
                        $source = $myfunc($fname);
                        imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $nh, $w, $h);
                        #imagecopyresized($thumb, $source, 0,0,0,0, $thumb_width, $nh, $w, $h);
                        $myfunc = 'image' . $imgtype;
                        $myfunc($thumb, $dir . '/thumbnails/' . $file);
                    }
                } else {
                    $fp = popen("convert -scale " . $thumb_width . " " . $dir . "/" . $file . " " . $dir . "/thumbnails/" . $file . $formatter->NULL, 'r');
                    @pclose($fp);
                }
            }
            if (empty($selected) and file_exists($dir . "/thumbnails/" . $file)) {
                $thumb = $key == $value ? $prefix . 'thumbnails/' . $id : str_replace('value=', 'value=thumbnails/' . $id, $prefix);
                if ($thumb_width > $width) {
                    $mywidth = " width='" . $width . "' ";
                } else {
                    $mywidth = '';
                }
                $object = "<img class='imgGallery' src='{$thumb}' {$mywidth} alt='{$file}' />";
            } else {
                $nwidth = $width;
                if (function_exists('getimagesize')) {
                    list($nwidth, $height, $type, $attr) = getimagesize($dir . '/' . $file);
                    $nwidth = $nwidth > $width ? $width : $nwidth;
                }
                $object = "<img class='imgGallery' src='{$linksrc}' width='{$nwidth}' alt='{$file}' />";
            }
        } else {
            $object = $file;
        }
        $unit = array('Bytes', 'KB', 'MB', 'GB', 'TB');
        $i = 0;
        for (; $i < 4; $i++) {
            if ($size <= 1024) {
                $size = round($size, 2) . ' ' . $unit[$i];
                break;
            }
            $size = $size / 1024;
        }
        #    $size=round($size,2).' '.$unit[$i];
        $comment = '';
        if ($width > 100) {
            $comment_btn = '';
            $comment_btn = $nocomment ? '' : _("add comment");
            $imginfo = (!$nocomment or $selected) ? "{$date} ({$size}) " : '';
            if (!empty($comments[$file]) and !empty($options['value'])) {
                $comment = $comments[$file];
                $comment = str_replace("\\n", "\n", $comment);
                $options['comments'] = str_replace("\t", "\n----\n", $comment);
                $comment = str_replace("\t", "<div class='separator'><hr /></div>", $comment);
                $comment = str_replace("\n", "<br/>\n", $comment);
            } else {
                if ((empty($nocomment) or !empty($selected)) and !empty($comments[$file])) {
                    if (empty($show_all)) {
                        $comment_btn = _("show comments");
                        //list($comment,$dum)=explode("\t",$comments[$file],2);
                        $tmp = explode("\t", $comments[$file], 2);
                        $comment = $tmp[0];
                    } else {
                        $comment_btn = _("add comment");
                        $comment = str_replace("\t", "<div class='separator'><hr /></div>\n", $comments[$file]);
                    }
                    $comment = str_replace("\\n", "<br/>\n", $comment);
                }
            }
        }
        $out .= "<td {$col_td_width} align='center' valign='top'>{$top_link}<div class='{$img_class}' {$img_style}><a href='{$link}'{$href_attr}>{$object}</a>";
        if (!empty($imginfo)) {
            $out .= "<br />" . $imginfo;
        }
        if (!empty($comment_btn)) {
            $out .= '[' . $formatter->link_tag($formatter->page->urlname, "?action=gallery&amp;value={$id}", $comment_btn) . "]<br />\n";
        }
        $out .= '</div>' . $bot_link;
        if (!empty($comment)) {
            $out .= "<div class='gallery-comments'>{$comment}</div>";
        }
        $out .= "</td>\n";
        if ($idx % $col == 0) {
            $out .= "</tr>\n<tr>\n";
        }
        $idx++;
        if ($idx > $perpage) {
            break;
        }
    }
    $idx--;
    $out .= "</tr></table>\n";
    if (!in_array('UploadFile', $formatter->actions)) {
        $formatter->actions[] = 'UploadFile';
    }
    return $pnut . '<div class="gallery">' . $out . '</div>' . $pnut;
}