function checkRedirect($link) { // 1 - $http_code = getStatusCode($link); if ($http_code === 301) { echo "301 redirect"; return true; } // 2 - html redirect $redirectString = "meta http-equiv=\"refresh\""; $source = getSourceCode($link); $pos = strpos($source, $redirectString); if ($pos !== false) { echo "http redirect"; return true; } // JS redirect // google.js redirect fail // $redirectString = ".location"; // $JSLinks = getJSLinks($source); // foreach ($JSLinks as $link) { // $jsCode = getSourceCode($link); // $pos = strpos($jsCode, $redirectString); // if ($pos !== false) // { // echo "js ".$link." redirect"; // return true; // } // } return false; }
function getLists($site, $counter) { echo "start working with " . $site . "\n"; $links[] = $site; $intraLinks[] = $site; $globalCSS = array(); $globalJS = array(); $i = 0; while ($i <= $counter && count($intraLinks) > $i) { $code = getSourceCode($intraLinks[$i]); // echo "working with ".$intraLinks[$i]."\n"; $urls = getUrls($code); $localCSS = getCSSLinks($code); $localJS = getJSLinks($code); if (count($urls) > 0) { foreach ($urls as $url) { // если начинается на два слэша - внешний линк, дописываем http: if ($url[0] == "/" && $url[1] == "/") { $url = "http:" . $url; if (!in_array($url, $links)) { $links[] = $url; } // если начинается на один слэш - внутренний линк, добавляем основную часть (домен) } elseif ($url[0] == "/") { $url = $site . $url; if (!in_array($url, $intraLinks)) { $intraLinks[] = $url; } // в остальных случаях - обычная внешняя ссылка - добавляем в список } elseif (!in_array($url, $links)) { $links[] = $url; } } } if (count($localCSS) > 0) { foreach ($localCSS as $url) { // если начинается на два слэша - внешний линк, дописываем http: if ($url[0] == "/" && $url[1] == "/") { $url = "http:" . $url; if (!in_array($url, $globalCSS)) { $globalCSS[] = $url; } // если начинается на один слэш - внутренний линк, добавляем основную часть (домен) } elseif ($url[0] == "/") { $url = $site . $url; if (!in_array($url, $globalCSS)) { $globalCSS[] = $url; } } elseif (!in_array($url, $globalCSS)) { $globalCSS[] = $url; } } } if (count($localJS) > 0) { foreach ($localJS as $url) { // если начинается на два слэша - внешний линк, дописываем http: if ($url[0] == "/" && $url[1] == "/") { $url = "http:" . $url; if (!in_array($url, $globalJS)) { $globalJS[] = $url; } // если начинается на один слэш - внутренний линк, добавляем основную часть (домен) } elseif ($url[0] == "/") { $url = $site . $url; if (!in_array($url, $globalJS)) { $globalJS[] = $url; } } elseif (!in_array($url, $globalJS)) { $globalJS[] = $url; } } } $i += 1; } return array($intraLinks, $globalCSS, $globalJS); }
done(); // If it reaches this point then all the tests were successful echo "All the tests completed successfully.\n"; // The test suite completed; that's all for now. exit; } elseif ($argc == 2 && $argv[1] == 'source') { // If there is only one argument and it is 'source' then display the code // You can put the output into a file or run it directly by piping it to php: // $ php recursive-ascii-spirals.php source | php -d error_reporting=0 4 r c 3 echo "<?php\n"; echo getSourceCode('keys_golfed'); echo "\n"; } elseif ($argc == 2 && 1 <= (int) $argv[1]) { // If the program receives exactly 1 positive integer argument then it runs // Read the code of the golfed function from the script file and evaluate it eval(getSourceCode('keys_golfed')); } else { // Invalid arguments usage(); exit(2); } exit(0); // That's all the code; below are just functions /////////////////////////////////////////////////////////////////////////// /** * This is the plain code * * @param int $argc * @param array $argv */ function keys_plain($argc, array $argv)
function showQuery($type, $backtrace, $host_info, $database, $sql, $rows, $affected_rows, $msc, $Result) { switch ($type) { case 'FETCH': $class = "qFetch"; $CreditTitle = "<b>Result " . $rows . " records:</b>"; break; case 'AFFECT': $class = "qAffect"; $CreditTitle = "<b>Affected rows: " . $affected_rows . ", Auto Increment " . $rows . ":</b>"; break; case 'FAILURE': $class = "qFailure"; $CreditTitle = "<b>NOT executed because:</b>"; break; default: $class = "qUnknown"; $CreditTitle = "<b>Result:</b>"; break; } $Queries = isset($_SESSION['Queries']) ? $_SESSION['Queries'] : 0; ?> <div class="<?php echo $class; ?> "> <div id="Qid<?php echo $Queries; ?> " class="close" onclick="hide( $(this).next() )"> <b>#<?php echo $Queries; ?> |T: <?php echo $msc; ?> ''| <span style="color:red">R:<?php echo $rows ? $rows : $affected_rows; ?> </span> |F:<?php echo $backtrace[0]['function']; ?> </b> </div> <div class="qContent" style="display: none;"> <div class="close" onclick="hide($(this).next())"> <b>Function info:</b> </div> <div style='display: none;'> <pre><?php echo getSourceCode($backtrace[0]['function']); ?> </pre> <hr style='border:1px solid black'> Called from file: <?php echo $backtrace[0]['file']; ?> <br /> At line: <?php $backtrace[0]['line']; ?> <br /> With args: <?php echo getFunctionArgs($backtrace); ?> </div> Database: <?php echo $database; ?> | Host: <?php echo $host_info; ?> <?php echo SqlFormatter::format($sql); //Error at upload (Insert) queries ?> <div class="close" onclick="hide( $(this).next() )" > <?php echo $CreditTitle; ?> </div> <pre class='qResult'><?php print_r($Result); ?> </pre> </div> </div> <?php }
// // Test the score computing function validates the output rules (no duplicate chars, no missing chars, no neighbours) it(sprintf('detects missing characters in <output>%s</output>', $output = '!"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'), "missing in output: [[[ ']]]" === computeScore($output, 'not used here')); it(sprintf('detects duplicate characters in <output>%s</output>', $output = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\'abc'), "duplicates in output: [[['abc]]]" === computeScore($output, 'not used here')); it(sprintf('detects neighbour characters in <output>%s</output>', $output = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'), "neighbours in output: [[[ ]]] and [[[!]]]" === computeScore($output, 'not used here')); ///////////////////////////////////////////////////////////////////////// // My attempts // it(sprintf('computes score for my answer r2364 (PHP) to %d', $score = 2364), $score === computeScore(getOutput('r2364'), getSourceCode('r2364'))); it(sprintf('computes score for my answer r1628 (PHP) to %d', $score = 1628), $score === computeScore(getOutput('r1628'), getSourceCode('r1628'))); it(sprintf('computes score for my answer r1362 (PHP) to %d', $score = 1362), $score === computeScore(getOutput('r1362'), getSourceCode('r1362'))); it(sprintf('computes score for my answer r1359 (PHP) to %d', $score = 1359), $score === computeScore(getOutput('r1359'), getSourceCode('r1359'))); it(sprintf('computes score for my answer r1256 (PHP) to %d', $score = 1256), $score === computeScore(getOutput('r1256'), getSourceCode('r1256'))); it(sprintf('computes score for my answer r1231 (PHP) to %d', $score = 1231), $score === computeScore(getOutput('r1231'), getSourceCode('r1231'))); it(sprintf('computes score for my answer r1217 (PHP) to %d', $score = 1217), $score === computeScore(getOutput('r1217'), getSourceCode('r1217'))); it(sprintf('computes score for my answer r1081 (PHP) to %d', $score = 1081), $score === computeScore(getOutput('r1081'), getSourceCode('r1081'))); // Exit with status code 1 if any test failed done(); // That's all the code in the main program; everything below are functions exit; ///////////////////////////////////////////////////////////////////////// // Testing functions // /** * Helper function to compute the score of a solution. * It is not required or needed by it helps me evaluate my solution faster * * @param string $output * @param string $sourceCode * @return integer|string */
<?php include "getLinks.php"; $site = "http://sibsr.ru"; $counter = 3; $links = getLists($site, $counter); // 0 - intralinks // 1 - css links // 2 - js links foreach ($links[0] as $link) { $source = getSourceCode($link); $crcLink[$link] = crc32($source); } // foreach ($links[1] as $link) { // $source = getSourceCode($link); // $crcCSS[$link] = crc32($source); // } // foreach ($links[1] as $link) { // $source = getSourceCode($link); // $crcJS[$link] = crc32($source); // } foreach ($crcJS as $site => $crc) { echo "crc:\t" . $crc . "\t for site: {$site}\n"; }
/** * Generate and return the text to put in a .php file as the standalone program * * @return string */ function getStandaloneProgramSourceCode() { $filename = basename(__FILE__); $code = getSourceCode('s'); $size = strlen($code); // Parse the source of this script, get all its docblock comments $comments = array_filter(token_get_all(file_get_contents(__FILE__)), function ($token) { return is_array($token) && $token[0] == T_DOC_COMMENT; }); // Keep only the first docblock $comment = reset($comments)[1]; return <<<E <?php {$comment} // // This is the standalone program ({$size} bytes) // Run it using: // \$ php -d error_reporting=0 {$filename} 8 r '' 3 // or // \$ php -d error_reporting=0 {$filename} 4 r c 3 {$code} // That's all, folks! E; }