Skip to content

Commit

Permalink
lsp: migrate lsp to use twig templates
Browse files Browse the repository at this point in the history
  • Loading branch information
liushuyu committed Jan 3, 2020
1 parent 6a15083 commit b7a9ba0
Show file tree
Hide file tree
Showing 25 changed files with 888 additions and 505 deletions.
122 changes: 122 additions & 0 deletions public/js/lsp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
$(function () {
$('[data-toggle=\'tooltip\']').tooltip();
});

$(window).bind('resize load', function () {
if ($(this).width() < 962) {
collapse_in('#caret');
} else {
collapse_out('#caret');
}
});

function collapse_out(item_id) {
$('.collapse')
.not('.navbar-collapse')
.addClass('in');
$(item_id).removeClass('fa-caret-right');
$(item_id).addClass('fa-caret-down');
}

function collapse_in(item_id) {
if ($('#login').is(':focus') || $('#password').is(':focus')) {
return;
}
$('.collapse')
.not('.navbar-collapse')
.removeClass('in');
$(item_id).removeClass('fa-caret-down');
$(item_id).addClass('fa-caret-right');
}

$('.collapse')
.not('.navbar-collapse')
.on('shown.bs.collapse', function () {
collapse_out('#caret');
});
$('.collapse')
.not('.navbar-collapse')
.on('hidden.bs.collapse', function () {
collapse_in('#caret');
});

$(document).ready(function () {
/*
* Redirect page if alert contains a data-redirect tag
*/
$('.alert').each(function () {
var o = $(this);
var counter = $('.redirect-counter:first');
var timeout = counter.length ? parseInt(counter.text()) * 1000 : 5000;
if (o.data('redirect').length) {
window.setTimeout(function () {
window.location = o.data('redirect');
}, timeout);
countDown(false);
}
});

/*
* Focus to comment text-area if it is on the screen
*/
commentFocus();
});

function countDown(decrement) {
var counter = $('.redirect-counter:first');
if (counter.length) {
if (decrement) {
counter.text(parseInt(counter.text()) - 1);
}
if (counter.text() == '-1') {
counter.remove();
return;
} else {
window.setTimeout(function () {
countDown(true);
}, 1000);
return;
}
}
}

function commentFocus() {
var comment = $('#comment');
if (comment.length) {
comment.focus();
}
}

function loginFocus() {
javascript: $('#login-collapse').addClass('in');
$('#login').focus();
blink('#login');
}

function blink(item_id) {
$(item_id)
.fadeIn(100)
.fadeOut(100)
.fadeIn(100)
.fadeOut(100)
.fadeIn(100)
.fadeOut(100)
.fadeIn(100);
}

/*
* Bootstrap file button listener
*/
$(document).on('change', '.btn-file :file', function () {
var input = $(this),
numFiles = input.get(0).files ? input.get(0).files.length : 1,
label = input
.val()
.replace(/\\/g, '/')
.replace(/.*\//, '');
$('#file-selected').html(label ? label : 'No file selected');
$('#file-selected')
.removeClass()
.addClass(label ? 'text-primary' : 'text-danger');
blink('#file-selected');
});
55 changes: 13 additions & 42 deletions public/lsp/add_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,7 @@

if (!SESSION_EMPTY()) {
if (POST_EMPTY('ok') && POST_EMPTY('addfinalok')) {
display_warning('Do not submit offending, pornographic, racist or violent content.', array('<a href="">Add File</a>'));
echo '<div class="col-md-9">';
$form = new form($LSP_URL . '?content=add', 'Add File', 'fa-upload'); ?>
<label for="filename">File to add</label>
<div class="form-group">
<span class="pull-left btn btn-default btn-file">
<span class="fas fa-folder-open"></span>&nbsp;Select file<input type="file" name="filename" />
</span><strong><span class="text-center"><pre class="text-warning" id="file-selected">No file selected</pre></span></strong>
<small>Maximum file size: 2 MB</small>
</div>
<div class="form-group">
<input type="checkbox" id="nocopyright" name="nocopyright" />
<label for="nocopyright">Does not violate any existing copyright, law or trademark</label>
</div>
<button type="submit" name="ok" value="OK" class="btn btn-primary"><span class="fas fa-upload"></span>&nbsp;Upload</button>
<a href="<?php echo $LSP_URL; ?>" class="btn btn-warning"><span class="fas fa-times"></span>&nbsp;Cancel</a>
<?php $form->close(); echo '</div>';
echo twig_render('lsp/add_file.twig', []);
} else if(GET('content') == "add" ) {
if (POST_EMPTY('tmpname')) $tmp_path = $_FILES["filename"]["tmp_name"]; else $tmp_path = POST('tmpname');
if (POST_EMPTY('fn')) $file_path = $_FILES["filename"]["name"]; else $file_path = POST('fn');
Expand All @@ -41,34 +25,21 @@
$categories = get_categories_for_ext($file_extension);
if ($categories != false) {
if (isset($_FILES["filename"]["tmp_name"])) {
echo '<div class="col-md-9">';
create_title(array('<a href="">Add File</a>', $file_path));
$tmp_path = $_FILES["filename"]["tmp_name"];
$tmp_ext = trim(pathinfo($tmp_path, PATHINFO_EXTENSION));
$tmp_name_only = pathinfo($tmp_path, PATHINFO_FILENAME) . ($tmp_ext == "" ? '' : '.' . $tmp_ext);
move_uploaded_file($tmp_path, $TMP_DIR . $tmp_name_only);
//echo "<code>moving $tmp_path to $TMP_DIR$tmp_name_only</code>";?>
<?php $form = new form($LSP_URL . '?content=add', 'File Details', 'fa-upload'); ?>
<div class="form-group">
<label for="category">Category</label>
<select name="category" class="form-control"><?php echo $categories;?></select>
</div>
<div class="form-group">
<label for="license">License</label>
<select name="license" class="form-control"><?php echo get_licenses();?></select>
</div>

<div class="form-group">
<label for="description">Description</label>
<textarea id="description" name="description" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary" name="addfinalok" value="Add File"><span class="fas fa-check"></span>&nbsp;Add File</button>&nbsp;
<a href="" class="btn btn-warning"></span><span class="fas fa-times"></span>&nbsp;Cancel</a>
<input type="hidden" name="fn" value="<?php echo $file_path; ?>" />
<input type="hidden" name="tmpname" value="<?php echo "$TMP_DIR$tmp_name_only"; ?>" />
<input type="hidden" name="fsize" value="<?php echo $file_size; ?>" />
<input type="hidden" name="nocopyright" value="<?php echo $no_copyright; ?>" />
<?php $form->close(); echo '</div>';
//echo "<code>moving $tmp_path to $TMP_DIR$tmp_name_only</code>";
echo twig_render('lsp/edit_file.twig', [
'titles' => array('<a href="">Add File</a>', $file_path),
'categories' => $categories,
'file_id' => GET('file'),
'licenses' => get_licenses(),
'fn' => $file_path,
'tmpname' => "$TMP_DIR$tmp_name_only",
'fsize' => $file_size,
'nocopyright' => $no_copyright
]);
} else {
display_error("No file specified", array('<a href="">Add File</a>', 'Error'), $LSP_URL . '?content=add');
}
Expand Down Expand Up @@ -104,7 +75,7 @@
if ($file_id > 0) {
//echo "<code>rename " . $tmp_path . " to " . $DATA_DIR . $file_id . '</code>';
rename($tmp_path, $DATA_DIR . $file_id);
show_file($file_id, SESSION(), "File added successfully");
redirect($LSP_URL . '?action=show&file=' . $file_id);
} else {
display_error("Failed to commit file <strong>$file_extension</strong>", array('<a href="">Add File</a>', 'Error'), $LSP_URL . '?content=add');
}
Expand Down
24 changes: 10 additions & 14 deletions public/lsp/comment_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,24 @@
require_once('xhtml.php');

global $LSP_URL;
$error = null;
if (!SESSION_EMPTY()) {
if (!POST('addcomment', false) || POST_EMPTY('text')) {
if (POST_EMPTY('text') && POST('addcomment', false)) {
display_error('Please type a message', array('Comment', get_file_url()));
} else {
display_warning('Do not submit offending, pornographic, racist or violent content.', array('Comment', get_file_url()));
$error = 'Please type a message';
}
echo '<div class="col-md-9">';
$form = new form($LSP_URL . '?comment=add&' . file_show_query_string(), 'Comment', 'fa-comment'); ?>
<div class="form-group">
<label for="text">Add comment to "<?php echo get_file_name(GET('file')); ?>"</label>
<textarea id="comment" name="text" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary" name="addcomment" value="Comment"><span class="fas fa-check"></span>&nbsp;Comment</button>&nbsp;
<a href="<?php echo $LSP_URL . '?action=show&file=' . GET('file'); ?>" class="btn btn-warning"></span><span class="fas fa-times"></span>&nbsp;Cancel</a>
<?php $form->close(); echo '</div>';
} else {
add_visitor_comment(GET('file'), POST('text'), SESSION());
display_success('Comment posted successfully', array('Comment', get_file_url()), $LSP_URL . '?action=show&file=' . GET('file') . '#footer');
redirect($LSP_URL . '?action=show&file=' . GET('file') . '#footer');
}
} else {
display_error('Not logged in', array('Comment', get_file_url()));
$error = 'Not logged in';
}
echo twig_render('lsp/add_comment.twig', [
'file_id' => GET('file'),
'file_name' => get_file_name(GET('file')),
'titles' => array('Comment', get_file_url()),
'error' => $error
]);

?>
Loading

0 comments on commit b7a9ba0

Please sign in to comment.