forked from reidab/osbp_wordpress
-
Notifications
You must be signed in to change notification settings - Fork 2
/
shared_fragments.php
79 lines (69 loc) · 2.75 KB
/
shared_fragments.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
# shared_fragments
# ================
#
# This library provides a way of including shared fragments, chunks of
# HTML that can be shared between applications.
#
# The fragments are stored in the SHARED_FRAGMENTS_DIRECTORY.
#
# Get a fragment's content as a string with the "read_shared_fragment"
# function, or raise a SharedFragmentNotFoundException if one isn't
# found.
#
# Require a fragment's contents with "require_shared_fragment", which
# will display a helpful debugging message if something went wrong.
# Directory with the shared fragments.
define(SHARED_FRAGMENTS_DIRECTORY, dirname(__FILE__) . "/shared_fragments");
# HTML to show if a shared fragment can't be found. This is a sprintf
# string that accepts a single argument: the name of the missing file.
define(SHARED_FRAGMENT_EXCEPTION_TEMPLATE, <<<EOB
<p style="background: #F2D6D6; border: 2px solid #C37575; padding: 0.5em; margin: 1em;">
<strong>ERROR:</strong> Couldn't find shared fragment file "<code>%s</code>". This theme expects the admin to symlink the OpenConferenceWare "RAILS_ROOT/<code>public/system/shared_fragments</code>" directory to this theme's "<code>shared_fragments</code>" directory or create this directory and its static files so they can be included here. Please report this error to <a href="mailto:[email protected]">[email protected]</a>.
</p>
EOB
);
# Exception thrown when a shared fragment wasn't found, contains a
# special getFriendlyMessage method that returns fancy HTML.
class SharedFragmentNotFoundException extends Exception {
public $filename;
public function __construct($message, $filename) {
$this->filename = $filename;
parent::__construct($message);
}
public function getFriendlyMessage() {
return sprintf(SHARED_FRAGMENT_EXCEPTION_TEMPLATE, $this->filename);
}
}
# Display the shared fragment for $name or a helpful error message.
function require_shared_fragment($name) {
try {
echo read_shared_fragment($name);
} catch (SharedFragmentNotFoundException $e) {
echo $e->getFriendlyMessage();
}
}
# Return a string for the shared fragment for $name, else raise a
# SharedFragmentNotFoundException.
function read_shared_fragment($name) {
$filename = SHARED_FRAGMENTS_DIRECTORY . "/$name";
$failure = FALSE;
$content = NULL;
if (file_exists($filename)) {
$handle = fopen($filename, 'r');
if ($handle) {
$content = fread($handle, filesize($filename));
fclose($handle);
} else {
$failure = TRUE;
}
} else {
$failure = TRUE;
}
if ($failure) {
throw new SharedFragmentNotFoundException("Couldn't find shared fragment: $name", $filename);
} else {
return $content;
}
}
?>