Skip to content
This repository was archived by the owner on Jun 27, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
composer.lock
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"require":{
"php": ">=5.6.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.2"
},
"autoload":{
"psr-4": {
"IMSGlobal\\LTI\\": "src/"
Expand Down
17 changes: 17 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<file>./src</file>
<arg value="p" />
<arg name="colors" />
<rule ref="PSR2">
<exclude name="Generic.Files.LineLength.TooLong" />
</rule>
<rule ref="Squiz.Classes.ValidClassName.NotCamelCaps">
<exclude-pattern>./src/OAuth/OAuthSignatureMethod_*</exclude-pattern>
<exclude-pattern>./src/ToolProvider/DataConnector/DataConnector_*</exclude-pattern>
</rule>
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
<exclude-pattern>./src/OAuth/*</exclude-pattern>
<exclude-pattern>./src/Toolprovider/OAuthDataStore.php</exclude-pattern>
</rule>
</ruleset>
132 changes: 54 additions & 78 deletions src/HTTPMessage.php
Original file line number Diff line number Diff line change
@@ -1,168 +1,146 @@
<?php

namespace IMSGlobal\LTI;

/**
* Class to represent an HTTP message
* Class to represent an HTTP message.
*
* @author Stephen P Vickers <[email protected]>
* @copyright IMS Global Learning Consortium Inc
* @date 2016
* @author Stephen P Vickers <[email protected]>
* @copyright 2016 IMS Global Learning Consortium Inc
* @version 3.0.0
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license Apache-2.0
*/
class HTTPMessage
{

/**
* True if message was sent successfully.
*
* @var boolean $ok
*/
/** @var bool True if message was sent successfully. */
public $ok = false;

/**
* Request body.
*
* @var request $request
*/
/** @var request Request body. */
public $request = null;

/**
* Request headers.
*
* @var request_headers $requestHeaders
*/
/** @var request_headers Request headers. */
public $requestHeaders = '';

/**
* Response body.
*
* @var response $response
*/
/** @var response Response body. */
public $response = null;

/**
* Response headers.
*
* @var response_headers $responseHeaders
*/
/** @var response_headers Response headers. */
public $responseHeaders = '';

/**
* Status of response (0 if undetermined).
*
* @var status $status
*/
/** @var status Status of response (0 if undetermined). */
public $status = 0;

/**
* Error message
*
* @var error $error
*/
/** @var error Error message. */
public $error = '';

/**
* Request URL.
*
* @var url $url
*/
/** @var url Request URL. */
private $url = null;

/**
* Request method.
*
* @var method $method
*/
/** @var method Request method. */
private $method = null;

/**
* Class constructor.
*
* @param string $url URL to send request to
* @param string $method Request method to use (optional, default is GET)
* @param mixed $params Associative array of parameter values to be passed or message body (optional, default is none)
* @param string $header Values to include in the request header (optional, default is none)
*/
function __construct($url, $method = 'GET', $params = null, $header = null)
/**
* Class constructor.
*
* @param string $url URL to send request to.
* @param string $method Request method to use (optional, default is GET).
* @param mixed $params Associative array of parameter values to be passed or message body (optional, default is none).
* @param string $header Values to include in the request header (optional, default is none).
*/
public function __construct($url, $method = 'GET', $params = null, $header = null)
{

$this->url = $url;
$this->method = strtoupper($method);

if (is_array($params)) {
$this->request = http_build_query($params);
} else {
$this->request = $params;
}

if (!empty($header)) {
$this->requestHeaders = explode("\n", $header);
}

}

/**
* Send the request to the target URL.
*
* @return boolean True if the request was successful
*/
/**
* Send the request to the target URL.
*
* @return bool TRUE if the request was successful
*/
public function send()
{

$this->ok = false;
// Try using curl if available

// Try using curl if available
if (function_exists('curl_init')) {
$resp = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);

if (!empty($this->requestHeaders)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->requestHeaders);
} else {
curl_setopt($ch, CURLOPT_HEADER, 0);
}

if ($this->method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request);
} else if ($this->method !== 'GET') {
} elseif ($this->method !== 'GET') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);

if (!is_null($this->request)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request);
}
}

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);

//curl_setopt($ch, CURLOPT_SSLVERSION,3);
$chResp = curl_exec($ch);
$this->ok = $chResp !== false;

if ($this->ok) {
$chResp = str_replace("\r\n", "\n", $chResp);
$chRespSplit = explode("\n\n", $chResp, 2);

if ((count($chRespSplit) > 1) && (substr($chRespSplit[1], 0, 5) === 'HTTP/')) {
$chRespSplit = explode("\n\n", $chRespSplit[1], 2);
}

$this->responseHeaders = $chRespSplit[0];
$resp = $chRespSplit[1];
$this->status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->ok = $this->status < 400;

if (!$this->ok) {
$this->error = curl_error($ch);
}
}

$this->requestHeaders = str_replace("\r\n", "\n", curl_getinfo($ch, CURLINFO_HEADER_OUT));
curl_close($ch);
$this->response = $resp;
} else {
// Try using fopen if curl was not available
$opts = array('method' => $this->method,
'content' => $this->request
);
// Try using fopen if curl was not available
$opts = array(
'method' => $this->method,
'content' => $this->request
);

if (!empty($this->requestHeaders)) {
$opts['header'] = $this->requestHeaders;
}

try {
$ctx = stream_context_create(array('http' => $opts));
$ctx = stream_context_create(array(
'http' => $opts
));
$fp = @fopen($this->url, 'rb', false, $ctx);

if ($fp) {
$resp = @stream_get_contents($fp);
$this->ok = $resp !== false;
Expand All @@ -171,9 +149,7 @@ public function send()
$this->ok = false;
}
}

return $this->ok;

}

}
16 changes: 9 additions & 7 deletions src/OAuth/OAuthConsumer.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
<?php

namespace IMSGlobal\LTI\OAuth;

/**
* Class to represent an %OAuth Consumer
* Class to represent an %OAuth Consumer.
*
* @copyright Andy Smith
* @copyright Andy Smith
* @version 2008-08-04
* @license https://opensource.org/licenses/MIT The MIT License
*/
class OAuthConsumer {
class OAuthConsumer
{

public $key;

public $secret;

function __construct($key, $secret, $callback_url=NULL) {
public function __construct($key, $secret, $callback_url = null)
{
$this->key = $key;
$this->secret = $secret;
$this->callback_url = $callback_url;
}

function __toString() {
public function __toString()
{
return "OAuthConsumer[key=$this->key,secret=$this->secret]";
}

}
25 changes: 15 additions & 10 deletions src/OAuth/OAuthDataStore.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
<?php

namespace IMSGlobal\LTI\OAuth;

/**
* Class to represent an %OAuth Data Store
* Class to represent an %OAuth Data Store.
*
* @copyright Andy Smith
* @copyright Andy Smith
* @version 2008-08-04
* @license https://opensource.org/licenses/MIT The MIT License
*/
class OAuthDataStore {
function lookup_consumer($consumer_key) {
class OAuthDataStore
{

public function lookup_consumer($consumer_key)
{
// implement me
}

function lookup_token($consumer, $token_type, $token) {
public function lookup_token($consumer, $token_type, $token)
{
// implement me
}

function lookup_nonce($consumer, $token, $nonce, $timestamp) {
public function lookup_nonce($consumer, $token, $nonce, $timestamp)
{
// implement me
}

function new_request_token($consumer, $callback = null) {
public function new_request_token($consumer, $callback = null)
{
// return a new token attached to this consumer
}

function new_access_token($token, $consumer, $verifier = null) {
public function new_access_token($token, $consumer, $verifier = null)
{
// return a new access token attached to this consumer
// for the user associated with this token if the request token
// is authorized
// should also invalidate the request token
}

}
10 changes: 5 additions & 5 deletions src/OAuth/OAuthException.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace IMSGlobal\LTI\OAuth;

/**
* Class to represent an %OAuth Exception
* Class to represent an %OAuth Exception.
*
* @copyright Andy Smith
* @copyright Andy Smith
* @version 2008-08-04
* @license https://opensource.org/licenses/MIT The MIT License
*/
class OAuthException extends \Exception {
// pass
class OAuthException extends \Exception
{
// pass
}
Loading