Completed
Push — develop ( 5bc2b0...db625e )
by Seth
04:17
created

common-app.inc.php (8 issues)

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 19 and the first side effect is on line 7.

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
use smtech\CanvasPest\CanvasPest;
4
use Battis\BootstrapSmarty\NotificationMessage;
5
6
if (isset($_SESSION['toolProvider']->user)) {
7
	$_SESSION['canvasInstanceUrl'] = 'https://' . $_SESSION['toolProvider']->user->getResourceLink()->settings['custom_canvas_api_domain'];
8
} else {
9
	$_SESSION['canvasInstanceUrl'] = $metadata['CANVAS_INSTANCE_URL'];
10
}
11
12
if (isset($_SESSION['apiUrl']) && isset($_SESSION['apiToken'])) {
13
	$api = new CanvasPest($_SESSION['apiUrl'], $_SESSION['apiToken']);
14
} else {
15
	$api = new CanvasPest($metadata['CANVAS_API_URL'], $metadata['CANVAS_API_TOKEN']);
16
}
17
18
/* argument values for sync */
19
define('SCHEDULE_ONCE', 'once');
20
define('SCHEDULE_WEEKLY', 'weekly');
21
define('SCHEDULE_DAILY', 'daily');
22
define('SCHEDULE_HOURLY', 'hourly');
23
define('SCHEDULE_CUSTOM', 'custom');
24
25
define('LOCAL_TIMEZONE', 'US/Eastern'); // TODO:0 Can we detect the timezone for the Canvas instance and use it? issue:18
26
define('SEPARATOR', '_'); // used when concatenating information in the cache database
27
define('CANVAS_TIMESTAMP_FORMAT', 'Y-m-d\TH:iP');
28
define('SYNC_TIMESTAMP_FORMAT', 'Y-m-d\TH:iP'); // same as CANVAS_TIMESTAMP_FORMAT, FWIW
29
30
define('VALUE_OVERWRITE_CANVAS_CALENDAR', 'overwrite');
31
define('VALUE_ENABLE_REGEXP_FILTER', 'enable_filter');
32
33
/* cache database tables */
34
35
/* calendars
36
	id				hash of ICS and Canvas pairing, generated by getPairingHash()
37
	ics_url			URL of ICS feed
38
	canvas_url		canonical URL for Canvas object
39
	synced			sync identification, generated by getSyncTimestamp()
40
	modified		timestamp of last modificiation of the record
41
*/
42
43
/* events
44
	id					auto-incremented cache record id
45
	calendar			pair hash for cached calendar, generated by getPairingHash()
46
	calendar_event[id]	Canvas ID of calendar event
47
	event_hash			hash of cached event data from previous sync
48
	synced				sync identification, generated by getSyncTimestamp()
49
	modified			timestamp of last modification of the record
50
*/
51
52
/* schedules
53
	id			auto-incremented cache record id
54
	calendar	pair hash for cached calendar, generated by getPairingHash()
55
	crontab		crontab data for scheduled synchronization
56
	synced		sync identification, generated by getSyncTimestamp()
57
	modified	timestamp of last modification of the record
58
*/
59
60
$log = null;
61
function postMessage($subject, $body, $flag = NotificationMessage::INFO) {
62
	global $log;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
63
	global $smarty;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
64
	if (php_sapi_name() != 'cli') {
65
		$smarty->addMessage($subject, $body, $flag);
66
	} else {
67
		$logEntry = "[$flag] $subject: $body";
68
		if (is_a($log, Log::class)) {
69
			$log->log($logEntry);
70
		} else {
71
			echo "$logEntry\n";
72
		}
73
	}
74
}
75
76
/**
77
 * Generate a unique ID to identify this particular pairing of ICS feed and
78
 * Canvas calendar
79
 **/
80
function getPairingHash($icsUrl, $canvasContext) {
81
	global $metadata;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
82
	return md5($icsUrl . $canvasContext . $metadata['CANVAS_INSTANCE_URL']);
83
}
84
85
$FIELD_MAP = array(
86
	'calendar_event[title]' => 'SUMMARY',
87
	'calendar_event[description]' => 'DESCRIPTION',
88
	'calendar_event[start_at]' => array (
89
		0 => 'X-CURRENT-DTSTART',
90
		1 => 'DTSTART'
91
	),
92
	'calendar_event[end_at]' => array(
93
		0 => 'X-CURRENT-DTEND',
94
		1 => 'DTEND'
95
	),
96
	'calendar_event[location_name]' => 'LOCATION'
97
);
98
99
/**
100
 * Generate a hash of this version of an event to cache in the database
101
 **/
102
function getEventHash($event) {
103
	global $FIELD_MAP;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
104
	$blob = '';
105
	foreach ($FIELD_MAP as $field) {
106
		if (is_array($field)) {
107
			foreach ($field as $option) {
108
				if (!empty($property = $event->getProperty($option))) {
109
					$blob .= serialize($property);
110
					break;
111
				}
112
			}
113
		} else {
114
			if (!empty($property = $event->getProperty($field))) {
115
				$blob .= serialize($property);
116
			}
117
		}
118
	}
119
	return md5($blob);
120
}
121
122
/**
123
 * Generate a unique identifier for this synchronization pass
124
 **/
125
$SYNC_TIMESTAMP = null;
126
function getSyncTimestamp() {
0 ignored issues
show
getSyncTimestamp uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
127
	global $SYNC_TIMESTAMP;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
128
	if ($SYNC_TIMESTAMP) {
129
		return $SYNC_TIMESTAMP;
130
	} else {
131
		$timestamp = new DateTime();
132
		$SYNC_TIMESTAMP = $timestamp->format(SYNC_TIMESTAMP_FORMAT) . SEPARATOR . md5((php_sapi_name() == 'cli' ? 'cli' : $_SERVER['REMOTE_ADDR']) . time());
133
		return $SYNC_TIMESTAMP;
134
	}
135
}
136
137
?>
0 ignored issues
show
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
138