Skip to content

Commit 07096b2

Browse files
committed
Add editing pastes
1 parent 68d43ab commit 07096b2

File tree

5 files changed

+287
-1
lines changed

5 files changed

+287
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
use Illuminate\Http\Request;
5+
use App\Http\Requests;
6+
use App\Paste;
7+
use Auth;
8+
use App\User;
9+
use \Input;
10+
use \Hash;
11+
use Session;
12+
use Cookie;
13+
use DB;
14+
use \Carbon;
15+
16+
class EditPasteController extends Controller
17+
{
18+
public function index($link, Request $request){
19+
$paste = Paste::where('link', $link)->firstOrFail();
20+
21+
// Est-ce que l'utilisateur connecté est celui qui a écrit la paste ?
22+
if (Auth::user() != $paste->user || $paste->userId == 0) {
23+
return abort('404');
24+
}
25+
26+
// Renvoi de la view
27+
return view('paste/edit', [
28+
'username' => ($paste->userId != 0) ? $paste->user->name : 'Guest',
29+
'link' => $link,
30+
'title' => ($paste->title != 'Untitled') ? $paste->title : '',
31+
'content' => $paste->content,
32+
'expiration' => $paste->expiration,
33+
'privacy' => $paste->privacy,
34+
'date' => $paste->created_at->format('M j, Y'),
35+
'fulldate' => $paste->created_at->format('d/m/Y - H:i:s'),
36+
'noSyntax' => $paste->noSyntax,
37+
]);
38+
}
39+
40+
public function edit($link, Requests\EditPaste $request){
41+
$paste = Paste::where('link', $link)->firstOrFail();
42+
43+
// Est-ce que l'utilisateur connecté est celui qui a écrit la paste ?
44+
if (Auth::user() != $paste->user || $paste->userId == 0) {
45+
return abort('404');
46+
}
47+
48+
$title = (empty(trim(Input::get('pasteTitle')))) ? 'Untitled' : Input::get('pasteTitle');
49+
50+
$expiration = Input::get('expire');
51+
$privacy = Input::get('privacy');
52+
53+
// Ici on vérifie que l'user a pas foutu le bronx dans les dropdown list
54+
$possibleValuesPrivacy = array("link", "password", "private");
55+
if (in_array($privacy, $possibleValuesPrivacy) == false) return view('paste/edit');
56+
57+
// Si l'user a choisi password-protected, on hash son pass, sinon on met 'disabled' dans la variable
58+
if ($privacy == 'password') $password = bcrypt(Input::get('pastePassword'));
59+
else $password = 'disabled';
60+
61+
$burnAfter = 0;
62+
// Ici on génère le timestamp d'expiration
63+
switch ($expiration) {
64+
case 'never':
65+
$timestampExp = 0;
66+
break;
67+
case 'burn':
68+
$timestampExp = date('Y-m-d H:i:s', time());
69+
$burnAfter = 1;
70+
break;
71+
case '10m':
72+
$timestampExp = date('Y-m-d H:i:s', time()+600);
73+
break;
74+
case '1h':
75+
$timestampExp = date('Y-m-d H:i:s', time()+3600);
76+
break;
77+
case '1d':
78+
$timestampExp = date('Y-m-d H:i:s', time()+86400);
79+
break;
80+
case '1w':
81+
$timestampExp = date('Y-m-d H:i:s', time()+604800);
82+
break;
83+
default:
84+
die("User input error.");
85+
break;
86+
}
87+
88+
$paste->title = $title;
89+
$paste->content = Input::get('pasteContent');
90+
$paste->expiration = $timestampExp;
91+
$paste->privacy = $privacy;
92+
$paste->password = $password;
93+
$paste->noSyntax = Input::has('noSyntax');
94+
$paste->burnAfter = $burnAfter;
95+
96+
$paste->save();
97+
98+
return redirect('/'.$link);
99+
}
100+
101+
public function password($link, Request $request){
102+
$paste = Paste::where('link', $link)->firstOrFail();
103+
$messages = array(
104+
'pastePassword.required' => 'Please enter a password',
105+
);
106+
$this->validate($request, [
107+
'pastePassword' => 'required',
108+
], $messages);
109+
110+
if (Hash::check(Input::get('pastePassword'), $paste->password)) {
111+
Cookie::queue($paste->link, Input::get('pastePassword'), 15);
112+
return redirect('/'.$link);
113+
}
114+
else {
115+
return view('paste/password', ['link' => $paste->link, 'wrongPassword' => true]);
116+
}
117+
}
118+
}

app/Http/Requests/EditPaste.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class EditPaste extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
'pasteTitle' => 'max:70',
28+
'pasteContent' => 'required',
29+
'pastePassword' => 'required_if:privacy,password',
30+
'expire' => 'required',
31+
];
32+
}
33+
34+
public function messages()
35+
{
36+
return [
37+
'pasteContent.required' => 'Your paste cannot be empty.',
38+
'pastePassword.required_if' => 'Please enter a password.',
39+
'pasteTitle.max' => 'Title must not exceed 70 characters.',
40+
'expire.required' => 'Paste expiration is required.',
41+
];
42+
}
43+
}

resources/views/paste/edit.blade.php

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
@extends('default')
2+
3+
@section('pagetitle') Edit {{ $title }} - EdPaste @endsection
4+
5+
@section('navbar')
6+
<li class="nav-item active"><a href="#" class="nav-link">Home</a></li>
7+
@if (Auth::check())
8+
<li class="nav-item"><a href="/users/dashboard" class="nav-link">Dashboard</a></li>
9+
<li class="nav-item"><a href="/users/account" class="nav-link">My Account</a></li>
10+
<li class="nav-item"><a href=" /logout" class="nav-link">Logout <i>({{ Auth::user()->name }})</i></a></li>
11+
@else
12+
<li class="nav-item"><a href="/login" class="nav-link">Login</a></li>
13+
<li class="nav-item"><a href="/register" class="nav-link">Register</a></li>
14+
@endif
15+
@endsection
16+
17+
@section('script')
18+
<script src="../jquery.autogrowtextarea.min.js"></script>
19+
<script>
20+
function checkvalue(value)
21+
{
22+
if(value==="password")
23+
document.getElementById('passwordInput').style.display='block';
24+
else
25+
document.getElementById('passwordInput').style.display='none';
26+
}
27+
</script>
28+
@endsection
29+
30+
@section('content')
31+
<div class="container">
32+
<form action="/edit/{{ $link }}" method="post" accept-charset="utf-8">
33+
{{ csrf_field() }}
34+
{{-- Ca c'est pour éviter que les navigateurs préremplissent les champs --}}
35+
<input style="display:none" type="text" name="fakeusernameremembered"/>
36+
<input style="display:none" type="password" name="fakepasswordremembered"/>
37+
38+
<div class="row">
39+
<div class="form-group col-xs-12 @if ($errors->has('pasteTitle')) has-error @endif">
40+
<label for="pasteTitle">Title</label>
41+
<input type="text" class="form-control" name="pasteTitle" id="pasteTitle" placeholder="Title (optional)" maxlength="70" value="{{ old('pasteTitle') ? old('pasteTitle') : $title }}">
42+
@if ($errors->has('pasteTitle'))
43+
<span class="help-block">
44+
<strong>{{ $errors->first('pasteTitle') }}</strong>
45+
</span>
46+
@endif
47+
</div>
48+
</div>
49+
<div class="row">
50+
<div class="form-group col-xs-12 @if ($errors->has('pasteContent')) has-error @endif">
51+
<label for="pasteContent">Content</label>
52+
<script type="text/javascript">
53+
$(document).ready(function(){
54+
$("#pasteContent").autoGrow();
55+
});
56+
</script>
57+
<textarea class="form-control input-sm" name="pasteContent" id="pasteContent" rows="15" placeholder="Paste your text here..." style="font-family: monospace;">{{ old('pasteContent') ? old('pasteContent') : $content }}</textarea>
58+
@if ($errors->has('pasteContent'))
59+
<span class="help-block">
60+
<strong>{{ $errors->first('pasteContent') }}</strong>
61+
</span>
62+
@endif
63+
</div>
64+
</div>
65+
<div class="row">
66+
<div class="form-group col-sm-3 @if ($errors->has('expire')) has-error @endif">
67+
<label for="expire">Paste expiration</label>
68+
<select class="form-control" name="expire" id="expire">
69+
<option disabled selected></option>
70+
<option value="never">Never</option>
71+
<option value="burn">Burn after reading</option>
72+
<option value="10m">10 minutes</option>
73+
<option value="1h">1 hour</option>
74+
<option value="1d">1 day</option>
75+
<option value="1w">1 week</option>
76+
</select>
77+
@if ($errors->has('expire'))
78+
<span class="help-block">
79+
<strong>{{ $errors->first('expire') }}</strong>
80+
</span>
81+
@endif
82+
</div>
83+
<div class="form-group col-sm-3 @if ($errors->has('pastePassword')) has-error @endif">
84+
<label for="privacy">Privacy</label>
85+
<select class="form-control" name="privacy" id="privacy" onchange='checkvalue(this.value)'>
86+
<option value="link" {{ $privacy == "link" ? 'selected' : '' }}>Unlisted, access with link</option>
87+
<option value="password" {{ $privacy == "password" || $errors->has('pastePassword') ? 'selected' : '' }}>Password-protected</option>
88+
<option value="private" {{ $privacy == "private" ? 'selected' : '' }}>Private, only me</option>
89+
</select>
90+
</div>
91+
{{-- Ce truc n'apparait que si "Password-protected" est séléctionné plus haut --}}
92+
<div class="form-group col-sm-2 @if ($errors->has('pastePassword')) has-error @endif" id="passwordInput" @if (!$errors->has('pastePassword')) style="display:none;" @endif>
93+
<label for="pastePassword">Password</label>
94+
<input type="password" class="form-control" name="pastePassword" id="pastePassword" placeholder="Enter a password..." maxlength="40">
95+
@if ($errors->has('pastePassword'))
96+
<span class="help-block">
97+
<strong>{{ $errors->first('pastePassword') }}</strong>
98+
</span>
99+
@endif
100+
</div>
101+
</div>
102+
<div class="row">
103+
<div class="form-group text-center">
104+
<script>
105+
$(function () {
106+
$('[data-toggle="tooltip"]').tooltip()
107+
})
108+
</script>
109+
{{-- La tooltip n'apparaît que pour les users non-id et le btn devient danger si y'a des erreurs --}}
110+
<div class="checkbox">
111+
<label><input type="checkbox" name="noSyntax" @if ($noSyntax) checked @endif>Disable syntax highlighting</label>
112+
</div>
113+
<button type="submit" id="submit" class="btn @if (count($errors) > 0) btn-danger @else btn-outline-success @endif btn-lg" @if (!Auth::check()) data-toggle="tooltip" data-placement="top" title="Registered users have access to other privacy tools" @endif>Submit</button>
114+
</div>
115+
</div>
116+
117+
</div>
118+
</div>
119+
</form>
120+
</div>
121+
@endsection

resources/views/paste/view.blade.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@
130130
{{-- N'est formaté que si le SH est activé --}}
131131
<div class="row" @if ($noSyntax == true) style="margin-bottom:20px;" @endif>
132132
<div class="col-sm-12">
133-
<label for="paste"><i>@if ($noSyntax == false) Syntax-highlighted @else Plain-text @endif</i></label>@if ($privacy != "Password-protected") <i class="pull-right"><a href="/raw/{{ $link }}">Raw paste</a> @endif </i>
133+
<label for="paste"><i>@if ($noSyntax == false) Syntax-highlighted @else Plain-text @endif</i></label>
134+
@if ($privacy != "Password-protected") <i class="pull-right"><a href="/raw/{{ $link }}">Raw paste</a> @endif </i>
135+
@if ($sameUser) <i class="pull-right" style="margin-right: 10px;"><a href="/edit/{{ $link }}">Edit paste</a> @endif </i>
134136
<pre id="paste"><code>@if ($noSyntax == true)<i>@endif{{ $content }} @if ($noSyntax == true)</i>@endif</code></pre>
135137
</div>
136138
</div>

routes/web.php

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
Route::post('/', 'PasteController@submit');
2020
Route::get('/{link}', 'PasteController@view')->where('link', '[a-zA-Z0-9]+');
2121
Route::post('/{link}', 'PasteController@view')->where('link', '[a-zA-Z0-9]+');
22+
Route::get('/edit/{link}', 'EditPasteController@index')->where('link', '[a-zA-Z0-9]+');
23+
Route::post('/edit/{link}', 'EditPasteController@edit')->where('link', '[a-zA-Z0-9]+');
2224
Route::get('/raw/{link}', 'PasteController@raw')->where('link', '[a-zA-Z0-9]+');
2325
// Route::post('/{link}', 'PasteController@password')->where('link', '[a-zA-Z0-9]+');
2426
Route::get('users/dashboard', 'UserController@dashboard');

0 commit comments

Comments
 (0)