/**
 * Search for internal wiki links in page $file
 */
function orph_Check_InternalLinks(&$data, $base, $file, $type, $lvl, $opts)
{
    $dbg = false;
    define('LINK_PATTERN', '%\\[\\[([^\\]|#]*)(#[^\\]|]*)?\\|?([^\\]]*)]]%');
    if (!preg_match("/.*\\.txt\$/", $file)) {
        return;
    }
    if ($dbg) {
        echo '<p><b>' . $file . '</b></p>';
    }
    global $conf;
    // echo "  <!-- checking file: $file -->\n";
    $body = @file_get_contents($conf['datadir'] . $file);
    // ignores entries in <nowiki>, %%, <code> and emails with @
    foreach (array('/<nowiki>[\\W\\w]*<\\/nowiki>/', '/%%.*%%/', '/<code>[\\W\\w]*<\\/code>/', '/\\[\\[\\ *\\\\.*\\]\\]/', '/\\[\\[\\ *[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\\..*\\ *\\]\\]/') as $ignored) {
        $body = preg_replace($ignored, '', $body);
    }
    $links = array();
    preg_match_all(LINK_PATTERN, $body, $links);
    foreach ($links[1] as $link) {
        if ($dbg) {
            echo $link;
        }
        if (0 < strlen(ltrim($link)) and $link[0] != '/' and !preg_match('/^\\ *(https?|mailto|ftp|file):/', $link) and !preg_match('/^(.*)>/', $link) and !strpos('@', $link)) {
            // Try fixing the link...
            //$link = preg_replace("![ ]!", "_", strtolower($link));
            // need to fix the namespace?
            if ($link[0] == ":") {
                // forced root namespace
                $link = substr($link, 1);
                //echo "\t\t<!--  !! (2) $link -->\n";
            } else {
                if ($link[0] == ".") {
                    // forced relative namespace
                    // $link = preg_replace("!::!", ":",orph_fileNS($file) . ":" . substr($link, 1));
                    resolve_pageid(orph_fileNS($file), $link, $exists);
                    // echo "\t\t<!--  !! (2) $link -->\n";
                } else {
                    if (strpos($link, ':') === false) {
                        $link = preg_replace("!::!", ":", orph_fileNS($file) . ":" . $link);
                        // echo "\t\t<!--  !! (3) $link -->\n";
                    }
                }
            }
            // namespace fix
            if ($link[strlen($link) - 1] == ":") {
                $link .= $conf["start"];
            }
            // looks like an ID?
            $link = cleanID($link);
            if (strlen(ltrim($link)) > 0 and !auth_quickaclcheck($link) < AUTH_READ) {
                // should be visible to user
                // and (!preg_match("/^(http|mailto):/", $link))  // URL
                // and (!preg_match("/^(.*)>/", $link))) {        // interwiki
                //check ACL
                //echo "      <!-- adding $link -->\n";
                //dae mod
                //orph_handle_link(&$data, $link);
                orph_handle_link($data, $link);
            }
        }
        // link is not empty?
    }
    // end of foreach link
}
Ejemplo n.º 2
0
/**
 * Search for internal wiki links in page $file
 */
function orph_Check_InternalLinks(&$data, $base, $file, $type, $lvl, $opts)
{
    global $conf;
    define('LINK_PATTERN', '%\\[\\[([^\\]|#]*)(#[^\\]|]*)?\\|?([^\\]]*)]]%');
    if (!preg_match("/.*\\.txt\$/", $file)) {
        return;
    }
    $currentID = pathID($file);
    $currentNS = getNS($currentID);
    if (DEBUG) {
        echo sprintf("<p><b>%s</b>: %s</p>\n", $file, $currentID);
    }
    // echo "  <!-- checking file: $file -->\n";
    $body = @file_get_contents($conf['datadir'] . $file);
    // ignores entries in <nowiki>, %%, <code> and emails with @
    foreach (array('/<nowiki>.*?<\\/nowiki>/', '/%%.*?%%/', '/<code .*?>.*?<\\/code>/') as $ignored) {
        $body = preg_replace($ignored, '', $body);
    }
    $links = array();
    preg_match_all(LINK_PATTERN, $body, $links);
    foreach ($links[1] as $link) {
        if (DEBUG) {
            echo sprintf("--- Checking %s<br />\n", $link);
        }
        if (0 < strlen(ltrim($link)) and !preg_match('/^[a-zA-Z0-9\\.]+>{1}.*$/u', $link) and !preg_match('/^\\\\\\\\[\\w.:?\\-;,]+?\\\\/u', $link) and !preg_match('#^([a-z0-9\\-\\.+]+?)://#i', $link) and !preg_match('<' . PREG_PATTERN_VALID_EMAIL . '>', $link) and !preg_match('!^#.+!', $link)) {
            $pageExists = false;
            resolve_pageid($currentNS, $link, $pageExists);
            if (DEBUG) {
                echo sprintf("---- link='%s' %s ", $link, $pageExists ? 'EXISTS' : 'MISS');
            }
            if (strlen(ltrim($link)) > 0 and !auth_quickaclcheck($link) < AUTH_READ) {
                // should be visible to user
                //echo "      <!-- adding $link -->\n";
                if (DEBUG) {
                    echo ' A_LINK';
                }
                $link = utf8_strtolower($link);
                orph_handle_link($data, $link);
            } else {
                if (DEBUG) {
                    echo ' EMPTY_OR_FORBIDDEN';
                }
            }
        } else {
            if (DEBUG) {
                echo ' NOT_INTERNAL';
            }
        }
        if (DEBUG) {
            echo "<br />\n";
        }
    }
    // end of foreach link
}