Exemplo n.º 1
0
 public function testGetCurl()
 {
     $stuff = Net::getCurl("http://www.dr-chuck.com/page1.htm");
     $this->assertContains("The First Page", $stuff);
 }
Exemplo n.º 2
0
use Tsugi\Util\LTI;
use Tsugi\Util\Net;
$sanity = array('re.findall' => 'You should use re.findall() to extract the numbers');
// Compute the stuff for the output
$code = $USER->id + $LINK->id + $CONTEXT->id;
$sample_url = dataUrl('regex_sum_42.txt');
$actual_url = dataUrl('regex_sum_' . $code . '.txt');
$sample_data = Net::doGet($sample_url);
$sample_count = strlen($sample_data);
$response = Net::getLastHttpResponse();
if ($response != 200) {
    die("Response={$response} url={$sample_url}");
}
$actual_data = Net::doGet($actual_url);
$actual_count = strlen($actual_data);
$response = Net::getLastHttpResponse();
if ($response != 200) {
    die("Response={$response} url={$actual_url}");
}
$actual_matches = array();
preg_match_all('/[0-9]+/', $actual_data, $actual_matches);
$actual_count = count($actual_matches[0]);
$actual_sum = 0;
foreach ($actual_matches[0] as $match) {
    $actual_sum = $actual_sum + $match;
}
$sample_matches = array();
preg_match_all('/[0-9]+/', $sample_data, $sample_matches);
$sample_count = count($sample_matches[0]);
$sample_sum = 0;
foreach ($sample_matches[0] as $match) {
Exemplo n.º 3
0
 public static function sendOAuthBody($method, $endpoint, $oauth_consumer_key, $oauth_consumer_secret, $content_type, $body, $more_headers = false, $signature = false)
 {
     $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
     $hash = base64_encode(sha1($body, TRUE));
     if ($signature == "HMAC-SHA256") {
         $hmac_method = new OAuthSignatureMethod_HMAC_SHA256();
         $hash = base64_encode(hash('sha256', $body, TRUE));
     }
     $parms = array('oauth_body_hash' => $hash);
     $test_token = '';
     $test_consumer = new OAuthConsumer($oauth_consumer_key, $oauth_consumer_secret, NULL);
     $acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, $method, $endpoint, $parms);
     $acc_req->sign_request($hmac_method, $test_consumer, $test_token);
     // Pass this back up "out of band" for debugging
     global $LastOAuthBodyBaseString;
     $LastOAuthBodyBaseString = $acc_req->get_signature_base_string();
     $header = $acc_req->to_header();
     $header = $header . "\r\nContent-Type: " . $content_type . "\r\n";
     if ($more_headers === false) {
         $more_headers = array();
     }
     foreach ($more_headers as $more) {
         $header = $header . $more . "\r\n";
     }
     return Net::doBody($endpoint, $method, $body, $header);
 }
Exemplo n.º 4
0
// Get the ack value
$ack = false;
if ($re_register) {
    $ack = bin2hex(openssl_random_pseudo_bytes(10));
}
// Lets register!
$OUTPUT->togglePre("Registration Request", htmlent_utf8($body));
$more_headers = array();
if ($ack !== false) {
    $more_headers[] = 'VND-IMS-CONFIRM-URL: ' . $CFG->wwwroot . '/lti/tp_commit.php?commit=' . urlencode($ack);
}
$response = LTI::sendOAuthBody("POST", $register_url, $reg_key, $reg_password, "application/vnd.ims.lti.v2.toolproxy+json", $body, $more_headers, $hmac256);
$OUTPUT->togglePre("Registration Request Headers", htmlent_utf8(Net::getBodySentDebug()));
global $LastOAuthBodyBaseString;
$OUTPUT->togglePre("Registration Request Base String", $LastOAuthBodyBaseString);
$OUTPUT->togglePre("Registration Response Headers", htmlent_utf8(Net::getBodyReceivedDebug()));
$OUTPUT->togglePre("Registration Response", htmlent_utf8(jsonIndent($response)));
// Parse the response object and update the shared_secret if needed
$responseObject = json_decode($response);
if ($responseObject != null) {
    $tc_tool_proxy_guid = $responseObject->tool_proxy_guid;
    if ($tc_tool_proxy_guid) {
        $oauth_consumer_key = $tc_tool_proxy_guid;
        echo '<p>Tool consumer returned tool_proxy_guid=' . $tc_tool_proxy_guid . " (using as oauth_consumer_key)</p>\n";
        if ($tool_proxy_guid && $tool_proxy_guid != $tc_tool_proxy_guid) {
            echo '<p style="color: yellow;">Note: Returned tool_proxy_guid did not match launch oauth_consumer_key/tool_proxy_guid=' . $tool_proxy_guid . "</p>\n";
        }
    } else {
        echo '<p style="color: red;">Error: Tool Consumer did not include tool_proxy_guid in its response.</p>' . "\n";
    }
    if ($oauth_splitsecret && $shared_secret) {
Exemplo n.º 5
0
function getJsonOrDie($sample_url)
{
    $sample_data = Net::doGet($sample_url);
    $sample_count = strlen($sample_data);
    $response = Net::getLastHttpResponse();
    $sample_json = json_decode($sample_data);
    if ($response != 200 || $sample_json == null) {
        error_log("DIE: Sample response={$response} url={$sample_url} json_error=" . json_last_error_msg());
        die("Sample response={$response} url={$sample_url} json_error=" . json_last_error_msg());
    }
    return $sample_json;
}
Exemplo n.º 6
0
</p>
<?php 
if (!isset($_SESSION['html_data'])) {
    return;
}
$data = $_SESSION['html_data'];
unset($_SESSION['html_data']);
echo "<pre>\n";
echo "Input HTML\n";
echo htmlentities($data);
echo "\n";
// First validate using
// https://github.com/validator/validator/wiki/Service:-Input:-POST-body
$validator = 'https://validator.w3.org/nu/?out=json&parser=html5';
echo "Calling the validator {$validator} ... \n";
$return = Net::doBody($validator, "POST", $data, 'Content-type: text/html; charset=utf-8');
echo "Validator Output:\n";
echo htmlentities(jsonIndent($return));
if ($return . length > 0) {
    echo "Your code did not validate.  Please return to the W3 validator at validator.w3.org to check your code.";
} else {
    echo $data;
    $dom = new DOMDocument();
    @$dom->loadHTML($data);
    print "First check that major components are there.\n" . '<br>';
    try {
        $nodes = $dom->getElementsByTagName('html');
        if ($nodes->length == 1) {
            print "Found html tag!\n" . '<br>';
            print "...making sure English language is specified!" . '<br>';
            foreach ($nodes as $p) {
Exemplo n.º 7
0
use Tsugi\Core\LTIX;
use Tsugi\Util\LTI;
use Tsugi\Util\Net;
function mapdown($input)
{
    return preg_replace('/[^a-z0-9]/i', '', strtolower($input));
}
// Compute the stuff for the output
$getUrl = 'http://www.pythonlearn.com/code/intro-short.txt';
$data = Net::doGet($getUrl);
$response = Net::getLastHttpResponse();
if ($response != 200) {
    die("Response={$response} url={$getUrl}");
}
$headers = Net::parseHeaders();
$fields = array('Last-Modified', 'ETag', 'Content-Length', 'Cache-Control', 'Content-Type');
$oldgrade = $RESULT->grade;
// If we have a POST, pass to the GET
if (count($_POST) > 0) {
    $_SESSION['postdata'] = $_POST;
    $count = 0;
    $good = 0;
    foreach ($headers as $key => $val) {
        if (!in_array($key, $fields)) {
            continue;
        }
        $postkey = mapdown($key);
        $count++;
        if (isset($_POST[$postkey]) && mapdown($_POST[$postkey]) == mapdown($val)) {
            $good++;