forked from TheCodeDeli/stagebloc-local-theme-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
202 lines (182 loc) · 7.2 KB
/
index.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!doctype html>
<html>
<head>
<title>StageBloc Local Theme Development</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script src="assets/js/jquery.cookie.min.js"></script>
<script src="assets/js/main.js"></script>
<script src="assets/bender/js/bender.js"></script>
<link rel="stylesheet" href="assets/bender/css/bender.css" />
<link rel="stylesheet" href="assets/css/main.css" />
</head>
<body>
<?php
$loginRequired = $error = false;
// Check to see if they've configured their application
if ( file_exists('config.php') )
{
require_once 'config.php';
if ( $accessToken === null )
{
$loginRequired = true;
if ( ! empty($_POST) ) // The user is attempting to login
{
if ( isset($_POST['email']) && isset($_POST['password']) )
{
$params = array(
'client_id' => $clientId,
'client_secret' => $clientSecret,
'grant_type' => 'password',
'email' => $_POST['email'],
'password' => $_POST['password']
);
try
{
// Attempt to login with the given email and password
$response = $stagebloc->post('oauth2/token/index', $params);
$response = json_decode($response, true);
$accessToken = $response['access_token']; // Override the config var so that we can use it below right away
$stagebloc->setAccessToken($accessToken);
// Put this access token in our config file to make requests with
$code = file_get_contents('config.php');
$code = str_replace('$accessToken = null;', '$accessToken = \'' . $accessToken . '\';', $code);
file_put_contents('config.php', $code);
$loginRequired = false;
}
catch ( Services_StageBloc_Invalid_Http_Response_Code_Exception $e )
{
$response = json_decode($e->getHttpBody(), true);
if ( $response && isset($response['response']['errors']) )
{
$errors = $response['response']['errors'];
$error = $errors[0];
}
else
{
die($e->getHttpBody());
}
}
}
}
}
}
else
{
// Include the StageBloc library if config.php isn't loaded (since it loads it as well)
require_once 'php-stagebloc-api/StageBloc.php';
}
if ( ! $loginRequired )
{
// If the data var is null, we haven't gotten any acounts yet
// To force reloading of your accounts, simply reset this var to null
if ( $accountData === null )
{
try
{
$accountData = $stagebloc->post('accounts/list', array());
// Set the original authenticated account to the one authenticated by the API endpoint
$accounts = json_decode($accountData);
$accounts = $accounts->response->items;
foreach ( $accounts as $account )
{
if ( $account->authenticated )
{
$_COOKIE['account'] = $account->id; // Since COOKIEs aren't actually set until the next page load, manually set the data temporarily
setcookie('account', $account->id, time() + 60 * 60 * 24 * 30); // Expire in 30 days
}
}
// Write this data to our config file so that we can read it later instead of retrieving it every time
$code = file_get_contents('config.php');
$accountData = str_replace('\'', '\\\'', $accountData);
$code = str_replace('$accountData = null', '$accountData = \'' . $accountData . '\'', $code);
if ( false === file_put_contents('config.php', $code) )
{
die('Login data could not be written to config file, please make sure it has the right permissions set');
}
}
catch ( Services_StageBloc_Invalid_Http_Response_Code_Exception $e )
{
die($e->getHttpBody());
}
}
// We should always have accounts by now since we populated them if the data file was empty
$accounts = json_decode($accountData);
$accountOptionsHTML = '<select name="account" id="account"><optgroup label="Accounts">';
if ( $accounts )
{
$accounts = $accounts->response->items;
$accountUrl = '';
$accountToUse = ( isset($_COOKIE['account']) ? $_COOKIE['account'] : $accounts[0]->id ); // Default to use the first account
foreach ( $accounts as $account ) // Build a <select> drop down to use to switch between accounts quickly
{
$accountOptionsHTML .= '<option value="' . $account->id . '" ' . ( $accountToUse == $account->id ? 'selected' : '' ) . '>' . $account->name . '</option>';
if ( $accountToUse == $account->id )
{
$accountUrl = $account->stagebloc_url;
}
}
}
else
{
$accountOptionsHTML .= '<option>Please refresh your browser</option>';
}
$accountOptionsHTML .= '</optgroup></select>';
// Find all of the available themes we have
$themes = array_values(preg_grep('/^([^.])/', scandir($themePath))); // Ignore hidden files
$themeOptionsHTML = '<select name="theme" id="theme"><optgroup label="Themes">';
$themeToUse = ( isset($_COOKIE['theme']) ? $_COOKIE['theme'] : $themes[0] ); // Default to use the first theme
foreach ( $themes as $theme ) // Build a <select> drop down to use to switch between themes quickly
{
if ( is_dir($themePath . $theme) )
{
$themeOptionsHTML .= '<option value="' . $theme . '" ' . ( $themeToUse == $theme ? 'selected' : '' ) . '>' . $theme . '</option>';
}
}
$themeOptionsHTML .= '</optgroup></select>';
} ?>
<?php if ( ! file_exists('config.php') ): ?>
<div class="content">
<article>
<h2>Error!</h2>
<p>No config file found. Please rename config-sample.php to config.php.</p>
</article>
</div>
<?php elseif ( $loginRequired ): ?>
<div class="content">
<header><h1><a href="" id="stagebloc-logo">StageBloc</a></h1></header>
<form method="post" autocomplete="off">
<?php if ( $error !== false ): ?>
<div class="message"><?php echo $error; ?></div>
<?php endif; ?>
<fieldset>
<input class="input" type="text" id="email" name="email" placeholder="Email" required />
<input class="input" type="password" id="password" name="password" placeholder="Password" required />
<button type="submit" name="submit">Connect with StageBloc</button>
</fieldset>
</form>
</div>
<?php else: ?>
<div id="console">
<a href="http://stagebloc.com/developers/theming" target="_blank" class="docs"><i></i>Documentation</a>
<a href="https://stagebloc.com/<?php echo $accountUrl; ?>/admin/settings/site/assets" target="_blank">Upload Assets</a>
<form method="post" action="submit_theme.php" id="updateTheme">
<?php echo $themeOptionsHTML; ?>
<?php echo $accountOptionsHTML; ?>
<select id="actions" name="actions">
<option value="null">(Actions)</option>
<option value="reset">Reset Cookies</option>
<option value="generate">Generate Theme</option>
<option value="logout">Logout</option>
</select>
<select id="mobile" name="mobile">
<option value="false">Desktop</option>
<option value="true" <?php echo ( isset($_COOKIE['mobile']) && filter_var($_COOKIE['mobile'], FILTER_VALIDATE_BOOLEAN) ? 'selected' : '' ); ?>>Mobile</option>
</select>
<input class="button" type="submit" value="Submit Theme" />
</form>
</div>
<a href="theme_view.php" id="close" title="Close this frame"></a>
<iframe id="renderedTheme" src="theme_view.php<?php echo ( isset($_GET['url']) ? '?url=' . $_GET['url'] : '' ); ?>" frameborder="0"></iframe>
<?php endif; ?>
</body>
</html>