function bi_HandleBlockIP($src, $auth = 'comment-approve')
{
    #action=bi_bip
    global $bi_Pages;
    $result = array('msg' => XL('Unable to block IP address.'), 'result' => 'error');
    if (PageTextVar($src, 'entrytype') == 'comment' && bi_Auth($auth . ' ' . $src)) {
        if ($_GET['bi_ip'] > '') {
            Lock(2);
            $old = RetrieveAuthPage($bi_Pages['blocklist'], 'edit', false);
            if ($old) {
                if (!preg_match('/\\nblock:' . preg_replace(array('/\\./', '/\\*/'), array('\\.', '\\*'), $_GET['bi_ip']) . '\\n/', $old['text'])) {
                    $new = $old;
                    if (substr($new['text'], -1, 1) != "\n") {
                        $new['text'] .= "\n";
                    }
                    $new['text'] .= 'block:' . $_GET['bi_ip'] . "\n";
                    PostPage($bi_Pages['blocklist'], $old, $new);
                    $result = array('msg' => XL('Blocked IP address: ') . $_GET['bi_ip'], 'result' => 'success', 'ip' => $_GET['bi_ip']);
                } else {
                    $result = array('result' => 'error', 'msg' => XL('IP address is already being blocked: ' . $_GET['bi_ip']));
                }
            } else {
                $result = array('result' => 'error', 'msg' => 'Cannot edit ' . $bi_Pages['blocklist']);
            }
        } else {
            #No IP passed in, so determine who created page
            $page = RetrieveAuthPage($src, 'read', false);
            if ($page) {
                foreach ($page as $k => $v) {
                    #find the last diff in the list, which is the create point
                    if (preg_match("/^diff:(\\d+):(\\d+):?([^:]*)/", $k, $match)) {
                        $ip = @$page['host:' . $match[1]];
                    }
                }
            }
            $result = array('result' => $ip > '' ? 'success' : 'error', 'ip' => $ip, 'msg' => $ip > '' ? '' : XL('Unable to determine IP address.'));
        }
    }
    bi_Redirect(bi_Clean('mode', $_GET['bi_Mode']), $result);
}
Example #2
0
function bi_HandleBlockIP($src, $auth = 'comment-approve')
{
    //action=bi_bip
    global $bi_Pages;
    $result = array('msg' => XL('Unable to block IP address.'), 'result' => 'error');
    if (PageTextVar($src, 'entrytype') == 'comment' && bi_Auth($auth . ' ' . $src)) {
        if ($_GET['bi_ip'] > '') {
            //either we have an IP, or need to find one
            Lock(2);
            $old = RetrieveAuthPage($bi_Pages['blocklist'], 'edit', false, READPAGE_CURRENT);
            if ($old) {
                $ip = explode(',', $_GET['bi_ip']);
                $blocked = array();
                $already = array();
                foreach ($ip as $key) {
                    //check to see if ip is already blocked
                    if (!preg_match('/\\nblock:' . preg_replace(array('/\\./', '/\\*/'), array('\\.', '\\*'), $key) . '\\n/', $old['text'])) {
                        $new = $old;
                        $blocked[] = $key;
                        $new['text'] .= (substr($new['text'], -1, 1) != "\n" ? "\n" : '') . 'block:' . $key . "\n";
                        //add newline to end of file, and then blocked ip
                    } else {
                        $already[] = $key;
                    }
                }
                if (!empty($blocked)) {
                    PostPage($bi_Pages['blocklist'], $old, $new);
                }
                $result = array('msg' => (!empty($blocked) ? XL('Blocked IP address: ') . implode(',', $blocked) : '') . (!empty($blocked) && !empty($already) ? '<br />' : '') . (!empty($already) ? XL('IP address is already being blocked: ') . implode(',', $already) : ''), 'result' => 'success');
                //removed , 'ip'=>$_GET['bi_ip'], not used
            } else {
                $result = array('result' => 'error', 'msg' => XL('Cannot edit ') . $bi_Pages['blocklist']);
            }
        } else {
            //No IP passed in, so determine who created page
            $ip = array();
            $pages = bi_GetPages($src);
            foreach ($pages as $p) {
                $page = RetrieveAuthPage($p, 'read', false);
                //read history
                if ($page) {
                    $x = preg_grep_keys('/^host:.*$/', $page, -1);
                    //find the last occurence of host: which stores creator IP
                    $ip[$x] = $x;
                    //store as key/value to ensure we don't store same IP multiple times
                }
                $result = array('result' => !$ip ? 'error' : 'success', 'ip' => implode($ip, "\n"), 'msg' => !$ip ? XL('Unable to determine IP address.') : '');
            }
        }
    }
    bi_Redirect(bi_Clean('mode', $_GET['bi_Mode']), $result);
}