1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
4 include '../inc/common.php';
6 MTrackACL::requireAllRights('Snippets', 'read');
8 if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submit'])) {
9 MTrackACL::requireAllRights('Snippets', 'create');
11 $snip = new MTrackSnippet;
12 $snip->snippet = $_POST['code'];
13 $snip->description = $_POST['description'];
14 $snip->lang = $_POST['lang'];
15 $cs = MTrackChangeset::begin("snippet:?", $snip->description);
17 $cs->setObject("snippet:$snip->snid");
19 header("Location: {$ABSWEB}snippet.php/$snip->snid");
23 $pi = mtrack_get_pathinfo();
25 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
26 $snip = new MTrackSnippet;
27 $snip->description = $_POST['description'];
28 $snip->lang = $_POST['lang'];
29 $snip->snippet = $_POST['code'];
30 } elseif (strlen($pi)) {
31 $snip = MTrackSnippet::loadById($pi);
33 throw new Exception("Invalid snippet ID");
41 $code = $snip->snippet;
42 $desc = $snip->description;
43 mtrack_head("Snippet $pi");
46 $code = 'Enter your snippet here';
47 $desc = 'Enter a descriptive message here; you may use wiki syntax';
48 mtrack_head("New Snippet");
54 /* collect recent snippets */
55 $recent = MTrackDB::q('select snid, description, who, changedate
57 left join changes on snippets.updated = changes.cid
58 order by changes.changedate desc
59 limit 10')->fetchAll(PDO::FETCH_OBJ);
62 <td id='recentsnippets'>
63 <em>Snippets are a way to share text or code fragments</em><br><br>
66 if (MTrackACL::hasAllRights('Snippets', 'create')) {
68 <button id='newsnippet'>New Snippet</button><br>
70 \$(document).ready(function () {
71 \$('#newsnippet').click(function () {
72 document.location.href = "{$ABSWEB}snippet.php";
80 <b>Recent Snippets</b>
83 foreach ($recent as $s) {
84 $url = "{$ABSWEB}snippet.php/$s->snid";
85 $sum = MTrackWiki::format_to_oneliner($s->description);
86 $who = mtrack_username($s->who, array('no_image' => true));
87 $when = mtrack_date($s->changedate);
89 <div class='snippetsummary'>
92 <a href='$url'>view snippet</a>
98 if (MTrackACL::hasAllRights('Snippets', 'create') &&
99 (!$snip || $_SERVER['REQUEST_METHOD'] == 'POST')) {
100 echo "<form method='post' class='snippetform' action='{$ABSWEB}snippet.php'>";
101 echo "<textarea name='description' class='wiki shortwiki'>$desc</textarea>\n";
102 echo MTrackSyntaxHighlight::getLangSelect('lang', $lang);
103 echo "<br><textarea name='code' class='code' rows='20' cols='78'>";
104 echo htmlentities($code, ENT_QUOTES, 'utf-8');
105 echo "</textarea><br>";
106 echo "<button type='submit' name='preview'>Preview</button>\n";
107 echo "<button type='submit' name='submit'>Submit</button>\n";
112 echo "<div class='snippetview'>";
113 echo "<h1>Snippet</h1>";
114 if ($snip->created) {
115 $created = MTrackChangeset::get($snip->created);
117 $created = new stdclass;
120 echo "<span id='snippetmug'>",
121 mtrack_username($created->who, array('no_name' => true, 'size' => 48)),
123 echo "<b>Created</b>: ",
124 mtrack_date($created->when),
126 mtrack_username($created->who, array('no_image' => true)),
128 echo "<a href='{$ABSWEB}snippet.php/$snip->snid'>Link to this snippet</a><br>";
130 echo MTrackWiki::format_to_html($snip->description);
132 echo MTrackSyntaxHighlight::getSchemeSelect();
133 echo MTrackSyntaxHighlight::highlightSource($code, $lang, null, true);
135 } else if (!MTrackACL::hasAllRights('Snippets', 'create')) {
136 echo "<p>You do not have rights to create snippets</p>";
139 echo "</td></tr></table>";