Skip to content

Commit 21ba24c

Browse files
authored
Merge pull request #30 from Crizz0/issue/29
Improve code quality (validation result)
2 parents 5c86640 + 5d0f911 commit 21ba24c

File tree

13 files changed

+156
-216
lines changed

13 files changed

+156
-216
lines changed

config/routing.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ phpbbde_pastebin_main_controller:
22
path: /pastebin/
33
defaults:
44
_controller: 'phpbbde.pastebin.controller.main:handle'
5-
name: ""

config/services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ services:
99
class: phpbbde\pastebin\functions\utility
1010
arguments:
1111
- '%phpbbde.pastebin.geshilangs%'
12+
- '%core.php_ext%'
1213
phpbbde.pastebin.functions.pastebin:
1314
class: phpbbde\pastebin\functions\pastebin
1415
arguments:
@@ -33,7 +34,6 @@ services:
3334
- '@phpbbde.pastebin.functions.pastebin'
3435
- '%core.root_path%'
3536
- '%core.php_ext%'
36-
- '%phpbbde.pastebin.geshi%'
3737
- '%phpbbde.pastebin.geshilangs%'
3838
- '%tables.phpbbde.pastebin.pastebin%'
3939
phpbbde.pastebin.base_listener:

controller/main.php

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ class main
6666
/** @var \phpbb\captcha\factory */
6767
protected $captcha_factory;
6868

69-
/** @var string */
70-
protected $geshi_path;
71-
7269
/** @var string */
7370
protected $geshi_lang;
7471

@@ -108,7 +105,6 @@ public function __construct(
108105
\phpbbde\pastebin\functions\pastebin $pastebin,
109106
$root_path,
110107
$php_ext,
111-
$geshi_path,
112108
$geshi_lang,
113109
$pastebin_table)
114110
{
@@ -128,16 +124,11 @@ public function __construct(
128124
$this->util = $util;
129125
$this->captcha_factory = $captcha_factory;
130126

131-
$this->geshi_path = $geshi_path;
132127
$this->pastebin_table = $pastebin_table;
133128
$this->geshi_lang = $geshi_lang;
134129
}
135130

136-
/**
137-
* Handle all calls
138-
* @param string $name
139-
*/
140-
public function handle($name = '')
131+
public function handle()
141132
{
142133
$this->language->add_lang('pastebin', 'phpbbde/pastebin');
143134

@@ -152,20 +143,7 @@ public function handle($name = '')
152143
return $response;
153144
}
154145

155-
return $this->helper->render('pastebin_body.html', $this->language->lang('PASTEBIN'));
156-
}
157-
158-
/**
159-
* Adjust table naming correctly
160-
* @param string $name
161-
* @return string
162-
*/
163-
private function table($name)
164-
{
165-
if ($name == 'pastebin')
166-
{
167-
return $this->pastebin_table;
168-
}
146+
return $this->helper->render('@phpbbde_pastebin/pastebin_body.html', $this->language->lang('PASTEBIN'));
169147
}
170148

171149
/**
@@ -175,8 +153,6 @@ private function display_pb()
175153
{
176154
// Request variables
177155
$mode = $this->request->variable('mode', '');
178-
$confirm_id = $this->request->variable('confirm_id', '');
179-
$confirm_code = $this->request->variable('confirm_code', '');
180156
$snippet_id = $this->request->variable('s', 0);
181157
$submit = $this->request->is_set_post('submit');
182158

@@ -187,7 +163,7 @@ private function display_pb()
187163
$sql = $this->db->sql_build_query('SELECT', array(
188164
'SELECT' => 'pb.*, u.user_id, u.username, u.user_colour',
189165
'FROM' => array(
190-
$this->table('pastebin') => 'pb',
166+
$this->pastebin_table => 'pb',
191167
USERS_TABLE => 'u',
192168
),
193169
'WHERE' => "pb.snippet_author = u.user_id AND pb.snippet_id = $snippet_id",
@@ -198,9 +174,9 @@ private function display_pb()
198174

199175
if (!$data)
200176
{
201-
$message = $this->language->lang('NO_VALID_SNIPPET');
177+
$message = $this->language->lang('PASTEBIN_NO_VALID_SNIPPET');
202178
$message .= '<br /><br />';
203-
$message .= sprintf($this->language->lang('RETURN_PASTEBIN'), '<a href="' . $this->helper->route('phpbbde_pastebin_main_controller') . '">', '</a>');
179+
$message .= $this->language->lang('PASTEBIN_RETURN_PASTEBIN', '<a href="' . $this->helper->route('phpbbde_pastebin_main_controller') . '">', '</a>');
204180

205181
trigger_error($message);
206182
}
@@ -221,7 +197,7 @@ private function display_pb()
221197
$sql = $this->db->sql_build_query('SELECT', array(
222198
'SELECT' => 'pb.snippet_id, pb.snippet_time, pb.snippet_title, pb.snippet_desc, u.user_id, u.username, u.user_colour',
223199
'FROM' => array(
224-
$this->table('pastebin') => 'pb',
200+
$this->pastebin_table => 'pb',
225201
USERS_TABLE => 'u',
226202
),
227203
'WHERE' => 'pb.snippet_author = u.user_id',
@@ -236,7 +212,7 @@ private function display_pb()
236212
'DESC' => $row['snippet_desc'],
237213
'TITLE' => $row['snippet_title'],
238214
'SNIPPET_TIME' => $this->user->format_date($row['snippet_time']),
239-
'TITLE_SHORT' => (utf8_strlen($row['snippet_title']) > 50) ? utf8_substr($row['snippet_title'], 0, 50) . '...' : $row['snippet_title'],
215+
'TITLE_SHORT' => (utf8_strlen($row['snippet_title']) > 25) ? utf8_substr($row['snippet_title'], 0, 25) . $this->language->lang('PASTEBIN_ELLIPSIS') : $row['snippet_title'],
240216
'AUTHOR_FULL' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']),
241217
));
242218
}
@@ -269,9 +245,9 @@ private function display_pb()
269245
}
270246

271247
$data = array(
272-
'snippet_title' => utf8_normalize_nfc(str_replace("\n", '', $this->request->variable('snippet_title', '', true))),
273-
'snippet_desc' => utf8_normalize_nfc(str_replace("\n", '', $this->request->variable('snippet_desc', '', true))),
274-
'snippet_text' => utf8_normalize_nfc($this->request->variable('snippet_text', '', true)),
248+
'snippet_title' => str_replace("\n", '', $this->request->variable('snippet_title', '', true)),
249+
'snippet_desc' => str_replace("\n", '', $this->request->variable('snippet_desc', '', true)),
250+
'snippet_text' => $this->request->variable('snippet_text', '', true),
275251
'snippet_prunable' => 1,
276252
'snippet_highlight' => $this->request->variable('snippet_highlight', ''),
277253
'snippet_prune_on' => max(1, min(6, $this->request->variable('pruning_months', 0))),
@@ -287,7 +263,7 @@ private function display_pb()
287263

288264
if (empty($data['snippet_title']))
289265
{
290-
$error[] = $this->language->lang('ERR_NO_TITLE');
266+
$error[] = $this->language->lang('PASTEBIN_ERR_NO_TITLE');
291267
}
292268

293269
if (!$this->util->geshi_check($data['snippet_highlight']))
@@ -332,12 +308,12 @@ private function display_pb()
332308

333309
if (empty($snippet_contents))
334310
{
335-
$error[] = $this->language->lang('ERR_NO_BODY');
311+
$error[] = $this->language->lang('PASTEBIN_ERR_NO_BODY');
336312
}
337313

338314
if (!check_form_key('pastebinform'))
339315
{
340-
$error[] = $this->language->lang('FORM_INVALID');
316+
$error[] = $this->language->lang('PASTEBIN_FORM_INVALID');
341317
}
342318

343319
// Visual Confirmation handling (borrowed from includes/ucp/ucp_register.php)
@@ -350,7 +326,7 @@ private function display_pb()
350326

351327
if (!$captcha->is_solved())
352328
{
353-
$error[] = $this->language->lang('CONFIRM_CODE_WRONG');
329+
$error[] = $this->language->lang('PASTEBIN_CONFIRM_CODE_WRONG');
354330
}
355331
else if (!empty($error))
356332
{
@@ -383,15 +359,15 @@ private function display_pb()
383359
'snippet_prune_on' => time() + $this::SECONDS_MONTH * $data['snippet_prune_on'],
384360
);
385361

386-
$sql = 'INSERT INTO ' . $this->table('pastebin') . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
362+
$sql = 'INSERT INTO ' . $this->pastebin_table . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
387363
$this->db->sql_query($sql);
388364

389365
$snippet_id = $this->db->sql_nextid();
390366

391367
$redirect_url = $this->helper->route('phpbbde_pastebin_main_controller', array('mode' => "view", 's' => $snippet_id));
392368

393369
meta_refresh(3, $redirect_url);
394-
trigger_error($this->language->lang('SNIPPET_SUBMITTED') . '<br /><br />' . sprintf($this->language->lang('RETURN_SNIPPET'), '<a href="' . $redirect_url . '">', '</a>'));
370+
trigger_error($this->language->lang('PASTEBIN_SNIPPET_SUBMITTED') . '<br /><br />' . $this->language->lang('PASTEBIN_RETURN_SNIPPET', '<a href="' . $redirect_url . '">', '</a>'));
395371
}
396372

397373
break;
@@ -406,7 +382,7 @@ private function display_pb()
406382
trigger_error('PASTEBIN_AUTH_NO_VIEW');
407383
}
408384

409-
page_header(sprintf($this->language->lang('PASTEBIN_VIEW'), $data['snippet_title']));
385+
page_header($this->language->lang('PASTEBIN_VIEW', $data['snippet_title']));
410386

411387
$snippet_text = $data['snippet_text'];
412388

@@ -417,9 +393,6 @@ private function display_pb()
417393
$highlight = 'php';
418394
}
419395

420-
// highlight using geshi (http://qbnz.com/highlighter/)
421-
require($this->geshi_path . 'geshi.' . $this->php_ext);
422-
423396
$code = htmlspecialchars_decode($snippet_text);
424397

425398
$geshi = new \GeSHi($code, $highlight, $this->util->geshi_dir);
@@ -441,7 +414,7 @@ private function display_pb()
441414
'SNIPPET_TEXT_DISPLAY' => $snippet_text_display,
442415

443416
'SNIPPET_TIME' => $this->user->format_date($data['snippet_time']),
444-
'SNIPPET_PRUNE_ON' => $data['snippet_prunable'] ? $this->user->format_date($data['snippet_prune_on']) : $this->language->lang('NEVER'),
417+
'SNIPPET_PRUNE_ON' => $data['snippet_prunable'] ? $this->user->format_date($data['snippet_prune_on']) : $this->language->lang('PASTEBIN_NEVER'),
445418
'SNIPPET_DESC_V' => $data['snippet_desc'],
446419
'SNIPPET_TITLE_V' => $data['snippet_title'],
447420
'SNIPPET_AUTHOR' => $data['username'],
@@ -451,7 +424,7 @@ private function display_pb()
451424
'SNIPPET_DATE' => $this->user->format_date($data['snippet_time']),
452425

453426
'HIGHLIGHT_SELECT_MOD' => $this->util->highlight_select($data['snippet_highlight']),
454-
'DOWNLOAD_SNIPPET_EXPLAIN' => sprintf($this->language->lang('DOWNLOAD_SNIPPET_EXPLAIN'), '<a href="' . $snippet_download_url . '">', '</a>'),
427+
'DOWNLOAD_SNIPPET_EXPLAIN' => $this->language->lang('PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN', '<a href="' . $snippet_download_url . '">', '</a>'),
455428

456429
'U_SNIPPET' => $this->helper->route('phpbbde_pastebin_main_controller', array("mode" => "view", "s" => $data['snippet_id'])),
457430
'U_SNIPPET_DOWNLOAD' => $snippet_download_url,
@@ -518,7 +491,6 @@ private function display_pb()
518491

519492
if ($this->request->is_set_post('cancel'))
520493
{
521-
//redirect(append_sid("{$root_path}support/pastebin.$phpEx", "mode=view&amp;s=$snippet_id"));
522494
redirect($this->helper->route('phpbbde_pastebin_main_controller', array("mode"=>"view","s"=>$snippet_id)));
523495
}
524496

@@ -528,7 +500,7 @@ private function display_pb()
528500
if (!confirm_box(true))
529501
{
530502
$hidden = build_hidden_fields(array('mode' => 'moderate', 's' => $snippet_id, 'delete_snippet' => 1));
531-
confirm_box(false, sprintf($this->language->lang('DELETE_SNIPPET_CONFIRM'), $data['snippet_title']), $hidden);
503+
confirm_box(false, $this->language->lang('PASTEBIN_DELETE_SNIPPET_CONFIRM', $data['snippet_title']), $hidden);
532504
}
533505
else
534506
{
@@ -554,9 +526,9 @@ private function display_pb()
554526

555527
$redirect_url = $this->helper->route('phpbbde_pastebin_main_controller', $redirect_append);
556528

557-
$message = $this->language->lang('SNIPPET_MODERATED');
529+
$message = $this->language->lang('PASTEBIN_SNIPPET_MODERATED');
558530
$message .= '<br /><br />';
559-
$message .= sprintf($this->language->lang('RETURN_' . ((!$delete) ? 'SNIPPET' : 'PASTEBIN')), '<a href="' . $redirect_url . '">', '</a>');
531+
$message .= $this->language->lang('PASTEBIN_RETURN_' . ((!$delete) ? 'SNIPPET' : 'PASTEBIN'), '<a href="' . $redirect_url . '">', '</a>');
560532

561533
meta_refresh(3, $redirect_url);
562534
trigger_error($message);
@@ -614,7 +586,7 @@ private function display_pb()
614586
{
615587
$selected = '';
616588
}
617-
$pruning_months_select .= '<option' . $selected . ' value="-1">' . $this->language->lang('INFINITE') . '</option>';
589+
$pruning_months_select .= '<option' . $selected . ' value="-1">' . $this->language->lang('PASTEBIN_INFINITE') . '</option>';
618590
}
619591

620592
if (!isset($highlight))

cron/main.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class main extends \phpbb\cron\task\base
2929

3030
/** @var \phpbb\log\log_interface */
3131
protected $log;
32-
32+
protected $pastebin_path;
3333
protected $prune_interval;
3434
protected $pastebin_table;
3535

@@ -80,14 +80,4 @@ public function should_run()
8080

8181
return $now > $this->config['phpbbde_pastebin_prune_last_run'] + $this->prune_interval;
8282
}
83-
84-
/**
85-
* Adjust table naming correctly
86-
* @param string $name
87-
* @return string
88-
*/
89-
private function table($name)
90-
{
91-
return $this->pastebin_table;
92-
}
9383
}

event/acp_events.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ static public function getSubscribedEvents()
2626
);
2727
}
2828

29-
/**
30-
* Constructor
31-
*/
32-
public function __construct()
33-
{
34-
35-
}
36-
3729
/**
3830
* Add permissions for setting topic based posts per page settings.
3931
*

functions/pastebin.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, $
7272
'cfm' => 'cfm',
7373
'cpp-qt' => 'cpp',
7474
'css-gen.cfg' => 'cfg',
75-
'c_mac' => 'c',
7675
'd' => 'd',
7776
'delphi' => 'dpr',
7877
'div' => 'div',

functions/utility.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ class utility
3131
*/
3232
var $geshi_list = array();
3333

34+
/** @var string */
35+
protected $php_ext;
36+
3437
/**
3538
* Constructor
39+
* @param string $php_ext
3640
*/
37-
function __construct($geshi_dir)
41+
function __construct($geshi_dir, $php_ext)
3842
{
3943
$this->geshi_dir = $geshi_dir;
4044
$this->geshi_list = $this->geshi_list();
45+
$this->php_ext = $php_ext;
4146
}
4247

4348

@@ -54,8 +59,6 @@ function geshi_check($needle)
5459
*/
5560
function geshi_list()
5661
{
57-
global $phpEx;
58-
5962
$geshi_list = array();
6063

6164
$d = dir($this->geshi_dir);
@@ -66,13 +69,15 @@ function geshi_list()
6669
continue;
6770
}
6871

69-
if (($substr_end = strpos($file, ".$phpEx")) !== false)
72+
if (($substr_end = strpos($file, ".$this->php_ext")) !== false)
7073
{
71-
@sort($geshi_list[] = substr($file, 0, $substr_end));
74+
$geshi_list[] = substr($file, 0, $substr_end);
7275
}
7376
}
7477
$d->close();
7578

79+
sort($geshi_list);
80+
7681
return $geshi_list;
7782
}
7883

0 commit comments

Comments
 (0)