Completed
Push — develop ( 61ee3a...f2bd12 )
by Seth
08:02
created

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 9 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
require_once 'common.inc.php';
4
5
use smtech\StMarksReflexiveCanvasLTIExample\Toolbox;
6
use smtech\ReflexiveCanvasLTI\LTI\ToolProvider;
7
use smtech\ReflexiveCanvasLTI\Exception\ConfigurationException;
8
9
define('ACTION_CONFIG', 'config');
10
define('ACTION_INSTALL', 'install');
11
define('ACTION_CONSUMERS', 'consumers');
12
define('ACTION_UNSPECIFIED', false);
13
14
/* store any requested actions for future handling */
15
$action = (empty($_REQUEST['action']) ?
16
    ACTION_UNSPECIFIED :
17
    strtolower($_REQUEST['action'])
18
);
19
20
/* action requests only come from outside the LTI! */
21
if ($action) {
22
    unset($_SESSION[ToolProvider::class]);
23
}
24
25
/* authenticate LTI launch request, if present */
26
if ($toolbox->lti_isLaunching()) {
27
    $toolbox->resetSession();
28
    $toolbox->lti_authenticate();
29
    exit;
30
}
31
32
/* if authenticated LTI launch, get the current user profile */
33
if (!empty($_SESSION[ToolProvider::class]['canvas'])) {
34
    try {
35
        $profile = $toolbox->api_get('users/' . $_SESSION[ToolProvider::class]['canvas']['user_id'] . '/profile');
36
    } catch (Exception $e) {
37
        $toolbox->smarty_addMessage(
38
            'Error',
39
            json_decode($e->getMessage(), true),
40
            NotificationMessage::DANGER
41
        );
42
    }
43
    $toolbox->smarty_assign([
44
        'profile' => $profile
45
    ]);
46
    $toolbox->smarty_display('home.tpl');
47
    exit;
48
49
/* if not authenticated, default to showing credentials */
50
} else {
51
    $action = (empty($action) ?
52
        ACTION_CONFIG :
53
        $action
54
    );
55
}
56
57
/* process any actions */
58
switch ($action) {
59
    /* reset cached install data from config file */
60
    case ACTION_INSTALL:
61
        $_SESSION['toolbox'] = Toolbox::fromConfiguration(CONFIG_FILE, true);
62
        $toolbox =& $_SESSION['toolbox'];
63
64
        /* test to see if we can connect to the API */
65
        try {
66
            $toolbox->getAPI();
67
        } catch (ConfigurationException $e) {
68
            /* if there isn't an API token in config.xml, are there OAuth credentials? */
69
            if ($e->getCode() === ConfigurationException::CANVAS_API_INCORRECT) {
70
                $toolbox->interactiveGetAccessToken();
71
                exit;
72
            } else { /* no (understandable) API credentials available -- doh! */
73
                throw $e;
74
            }
75
        }
76
77
        /* finish by opening consumers control panel */
78
        header('Location: consumers.php');
79
        exit;
80
81
    /* show LTI configuration XML file */
82
    case ACTION_CONFIG:
83
        header('Content-type: application/xml');
84
        echo $toolbox->saveConfigurationXML();
85
        exit;
86
}
87