summaryrefslogtreecommitdiff
path: root/frontend/index.php
blob: 21d8a8a959964979552b01a89c19819274ef0028 (plain) (blame)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
require_once('conf.php');

if (isset($_GET['logout'])) {
	session_destroy();
	header('Location: /');
}

require_once('head.php');

if (isset($_POST['create'])) {
	$pass = Author::randomPass();
	try {
		$author = Author::create(
			$_pdo, [$_POST['create_name'], $_POST['create_email'], $pass]);
		?>
		Your account has been created.
		Your name is <tt><?=$author->name?></tt> and your password is <tt><?=$pass?></tt>.
		<b>Store this password in a secure place.</b>
		<?php
	} catch (Exception $e) {
		echo '<p class="error">' . $e->getMessage() . '.</p>';
	}
}

if (isset($_POST['login'])) {
	$authors = Author::search($_pdo, ['`name` like ?'], [$_POST['login_name']]);
	if (count($authors) == 0) {
		echo '<p class="error">No such user.</p>';
	} else {
		$author = $authors[0];
		if ($author->verifyPassword($_POST['login_pass'])) {
			$_SESSION['logged_in_id'] = $author->id;
		} else {
			echo '<p class="error">Invalid password.</p>';
		}
	}
}

if (isset($_SESSION['logged_in_id'])) {
	$_author = new Author($_pdo, $_SESSION['logged_in_id']);
	echo "You are logged in as {$_author->name}. <a href='?logout'>Logout</a>.<hr/>";

	if (isset($_POST['create_pkg'])) {
		try {
			$pkg = Package::create($_pdo,
				[ $_author->id
				, $_POST['create_pkg_name']
				, $_POST['create_pkg_url']
				, $_POST['create_pkg_git_url']
				, $_POST['create_pkg_desc']]);
			echo 'Your package has been created.';
		} catch (Exception $e) {
			echo '<p class="error">' . $e->getMessage() . '.</p>';
		}
	}
}
?>

<h2>Packages</h2>
<?php
$pkgs = Package::search($_pdo);

if (count($pkgs) == 0) :
	echo '<p class="error">No packages found.</p>';
else :
?>
	<table>
		<tr>
			<th>Name</th>
			<th>Author</th>
			<th>Description</th>
			<th>Website</th>
			<th>Versions</th>
		</tr>
		<?php
		foreach ($pkgs as $pkg) {
			echo 
				"<tr>
					<td><a href='/repo/{$pkg->name}'>{$pkg->name}</a></td>
					<td>" . $pkg->getAuthor()->name . "</td>
					<td>{$pkg->desc}</td>";
		
			if ($pkg->url != '')
				echo "<td><a href='{$pkg->url}' target='_blank'>Go</a></td>";
			else
				echo "<td>&endash;</td>";
		
			echo "<td>";
			foreach ($pkg->getVersions() as $v) {
				$vs = $v->major . '.' . $v->minor . '.' . $v->revision;
				echo "<a href='/repo/{$pkg->name}/$vs'>$vs</a> ({$v->time_added})";
			}
			echo "</td>";
		
			echo "</tr>";
		}
		?>
	</table>
<?php
endif;
?>

<hr/>

<?php
if (isset($_SESSION['logged_in_id'])) :
?>
	<h2>Your packages</h2>
	<?php
	$pkgs = $_author->getPackages();
	if (count($pkgs) == 0) :
		echo '<p class="error">No packages found.</p>';
	else :
	?>
		<p>To change details, please contact the CLPM maintainer.</p>
		<table>
			<tr>
				<th>Name</th>
				<th>URL</th>
				<th>Git URL</th>
				<th>Description</th>
			</tr>
			<?php
			foreach ($pkgs as $pkg) {
				echo "
					<tr>
						<td>{$pkg->name}</td>";
				if ($pkg->url != '')
					echo "<td><a href='{$pkg->url}'>{$pkg->url}</a></td>";
				else
					echo "<td>&endash;</td>";
				echo "
						<td><a href='{$pkg->git_url}'>{$pkg->git_url}</a></td>
						<td>{$pkg->desc}</td>
					</tr>";
			}
			?>
		</table>
	<?php
	endif;
	?>

	<h3>Add new</h3>
	<form method="post">
		<table>
			<tr>
				<th>Name</th>
				<td><input name="create_pkg_name"/></td>
				<td>Shown publicly.</td>
			</tr>
			<tr>
				<th>URL</th>
				<td><input type="url" name="create_pkg_url"/></td>
				<td>Shown publicly.</td>
			</tr>
			<tr>
				<th>Git URL</th>
				<td><input type="url" name="create_pkg_git_url"/></td>
				<td>
					<p>Your git repository. It should not require authorisation.</p>
					<p>On scheduled intervals, we will clone the repository and
						search for tags that look like a version number, e.g. <tt>v1.0.2</tt>.
					For those versions that are not yet found in the database,
						we will checkout the tag and run <tt>make TARGET.tar.gz</tt> for all targets
						(currently <tt>linux32</tt>, <tt>linux64</tt>, <tt>mac</tt>, <tt>win32</tt> and <tt>win64</tt>).
					We will then copy tose files to <tt>/repo/PACKAGE/VERSION/PLATFORM.tar.gz</tt>.</p>
				</td>
			</tr>
			<tr>
				<th>Description</th>
				<td colspan="2">
					<textarea name="create_pkg_desc" cols="60" rows="6"></textarea>
				</td>
			</tr>
			<tr>
				<th></th>
				<td><input type="submit" name="create_pkg" value="Add"/></td>
			</tr>
		</table>
	</form>
<?php
else : // Not logged in
?>
	<h2>Create an account</h2>
	You only need an account to add new packages.
	<form method="post">
		<table>
			<tr>
				<th>Name</th>
				<td><input name="create_name"/></td>
				<td>Posted publicly.</td>
			</tr>
			<tr>
				<th>Email</th>
				<td><input type="email" name="create_email"/></td>
				<td>Kept private.</td>
			</tr>
			<tr>
				<th></th>
				<td><input type="submit" name="create" value="Create"/></td>
			</tr>
		</table>
	</form>

	<hr/>

	<h2>Login</h2>
	<form method="post">
		<table>
			<tr>
				<th>Name</th>
				<td><input name="login_name"/></td>
			</tr>
			<tr>
				<th>Password</th>
				<td><input type="password" name="login_pass"/></td>
			</tr>
			<tr>
				<th></th>
				<td><input type="submit" name="login" value="Login"/></td>
			</tr>
		</table>
		<p>If you lost your password, please contact the CLPM maintainer.</p>
	</form>
<?php
endif;

require_once('foot.php');