-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
165 lines (113 loc) · 2.98 KB
/
index.php
File metadata and controls
165 lines (113 loc) · 2.98 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$format = 'html';
$error = false;
$host = $_SERVER['HTTP_HOST'];
$base_url = $host;
$base_slug = '';
if ($host === 'madebyfieldwork.co') {
$base_url .= '/lab/things-we-find';
$base_slug = '/lab/things-we-find';
}
$build = 27;
$tag = false;
$item_id = false;
$item = false;
$item_data = false;
$items = false;
$slug = (isset($_GET['url'])) ? $_GET['url'] : false;
if ($slug) {
$slug = explode('/', $slug);
if ($slug[0] !== 'item' && $slug[0] !== 'feed.rss') {
$tag = ucfirst($slug[0]);
}
if ($slug[0] === 'item' && isset($slug[1])) {
$item_id = $slug[1];
}
else if (isset($slug[1]) && $slug[1] === 'item' && isset($slug[2])) {
$item_id = $slug[2];
}
if (!$item_id && array_pop($slug) === 'feed.rss') {
$format = 'rss';
$api_url = 'http://' . $base_url . '/api.php?q=becausestudio/things-we-have-found&tag=' . $tag;
$items = file_get_contents($api_url);
$items = json_decode($items);
}
}
$tag_title = ($tag) ?: 'Everything';
$page_title = ($tag) ?: '';
$page_title .= ($tag) ? ' – ' : '';
$page_title .= 'Things We Find';
$collection_url = 'http://' . $base_url;
if ($tag) {
$collection_url .= '/' . $tag;
}
$feed_url = false;
if (!$item_id) {
$feed_url = $collection_url . '/feed.rss';
}
// Access the API if the URL refers to an item
if ($item_id) {
$item = file_get_contents('http://' . $base_url . '/api.php?asset=' . $item_id);
$item_data = json_decode($item);
if ($item) {
$page_title = $item_data->title . ' – Things We Find';
}
}
// Prepare data for view
$view = new SimpleView();
$view->page_title = $page_title;
$view->tag = $tag;
$view->item_id = $item_id;
$view->item = $item;
$view->item_data = $item_data;
$view->base_slug = $base_slug;
$view->build = $build;
$view->tag_title = $tag_title;
$view->page_title = $page_title;
$view->host = $host;
$view->items = $items;
$view->collection_url = $collection_url;
$view->feed_url = $feed_url;
// Render view in requested format
try {
if ($error) {
$view->render($error . '.' . $format . '.php');
}
else {
if ($format === 'rss') {
header('Content-Type: application/rss+xml; charset=UTF-8');
}
$view->render('itemlist.' . $format . '.php');
}
}
catch (Exception $e) {
$view->render('404.html.php');
}
exit();
/**
* Simple templating engine
*/
class SimpleView {
protected $views_dir;
protected $vars = array();
public function __construct($views_dir = 'views/') {
$this->views_dir = $views_dir;
}
public function render($view_file) {
if (file_exists($this->views_dir . $view_file)) {
extract($this->vars);
include($this->views_dir . $view_file);
} else {
throw new Exception('View file ' . $view_file . ' not found in ' . $this->views_dir);
}
}
public function __set($name, $value) {
$this->vars[$name] = $value;
}
public function __get($name) {
return $this->vars[$name];
}
}
?>