Example #1
0
function scan($path)
{
    global $aScanItems, $db, $id, $exts;
    if (is_dir($path)) {
        if ($dh = opendir($path)) {
            while (($item = readdir($dh)) !== false) {
                if (in_array($item, array(".", ".."))) {
                    continue;
                }
                $newPath = "{$path}/{$item}";
                scan($newPath);
            }
            closedir($dh);
        }
    } else {
        if (!in_array(substr($path, strrpos($path, ".") + 1), $exts)) {
            return;
        }
        $code = $line = array();
        $source = file_get_contents($path);
        $tokens = token_get_all($source);
        $oTokens = new Tokens();
        $totalItems = count($aScanItems);
        while ($token = array_shift($tokens)) {
            for ($i = 0; $i < $totalItems; $i++) {
                $itemId = $aScanItems[$i]['id'];
                $itemName = $aScanItems[$i]['name'];
                $totalTokens = count($aScanItems[$i]['scanTokens']);
                if ($oTokens->compareValues($aScanItems[$i]['scanTokens'][0], $token) && !(key($aScanItems[$i]['scanTokens']) == $totalTokens - 1)) {
                    reset($aScanItems[$i]['scanTokens']);
                    unset($code[$itemId], $line[$itemId]);
                }
                $aToken = current($aScanItems[$i]['scanTokens']);
                if (isset($code[$itemId])) {
                    $t = $oTokens->parseToken($token);
                    $code[$itemId] .= $t['value'];
                }
                $result = $oTokens->compareValues($aToken, $token);
                if ($result) {
                    if (!isset($line[$itemId]) && isset($token[2])) {
                        $line[$itemId] = $token[2];
                    }
                    if (!isset($code[$itemId])) {
                        $t = $oTokens->parseToken($token);
                        $code[$itemId] = $t['value'];
                    }
                    if (key($aScanItems[$i]['scanTokens']) == $totalTokens - 1) {
                        if (!isset($line[$itemId])) {
                            if (!isset($offset)) {
                                $offset = 0;
                            }
                            $pos = strpos($source, $code[$itemId], $offset);
                            $line[$itemId] = substr_count($source, "\n", null, $pos) + 1;
                            $offset = $pos + strlen($code[$itemId]);
                        }
                        $db->query('INSERT INTO scanresults (parentid, file, item, line, code) VALUES(?d, ?, ?, ?d, ?)', $id, $path, $itemName, $line[$itemId], $code[$itemId]);
                        reset($aScanItems[$i]['scanTokens']);
                        unset($code[$itemId], $line[$itemId]);
                        continue;
                    }
                    next($aScanItems[$i]['scanTokens']);
                } else {
                    if ($oTokens->compareValues($aScanItems[$i]['scanTokens'][$totalTokens - 1], $token)) {
                        reset($aScanItems[$i]['scanTokens']);
                        unset($code[$itemId], $line[$itemId]);
                    }
                    if ($aToken[2] == TOKEN_JUSTAFTER && !$oTokens->compareCodes($token, T_TERMINAL)) {
                        reset($aScanItems[$i]['scanTokens']);
                        unset($code[$itemId], $line[$itemId]);
                    }
                }
            }
        }
    }
}