Skip to content

Commit

Permalink
Fixes #378
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Vincent committed Jun 15, 2019
1 parent 14d0d01 commit b715cac
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 41 deletions.
1 change: 1 addition & 0 deletions Upload/inc/languages/english/admin/asb.lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@
$l['asb_overwrite_confirmation'] = 'Overwrite Confirmation';
$l['asb_overwrite_confirmation_desc'] = 'confirm that you want to overwrite an existing script definition';
$l['asb_import_overwrite_warning'] = 'Importing this script definition will overwrite the existing definition. Would you like to proceed?';
$l['asb_save_overwrite_warning'] = 'A script definition with these paramaters already exists. Saving it will overwrite the existing definition. Would you like to proceed?';

$l['asb_add_new_script'] = 'Add a new script definition';
$l['asb_no_scripts'] = 'no script info to show';
Expand Down
78 changes: 74 additions & 4 deletions Upload/inc/plugins/asb/acp.php
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ function asb_admin_view_scripts()
if ($mybb->request_method == 'post') {
if ($mybb->input['mode'] == 'edit') {
$id = (int) $mybb->input['id'];
$redirectUrl = $html->url(array('action' => 'view_scripts', 'mode' => 'edit', 'tid' => $tid, 'id' => $id));
$redirectUrl = $html->url(array('action' => 'view_scripts', 'mode' => 'edit', 'tid' => $tid));

$mybb->input['action'] = $mybb->input['script_action'];
$script = new ScriptInfo($mybb->input);
Expand All @@ -1071,6 +1071,34 @@ function asb_admin_view_scripts()
admin_redirect($redirectUrl);
}

$keys = $script->get(array('filename', 'action', 'page'));
$dupeId = asbFindDuplicateScriptByFilename($keys, $tid);
if ($dupeId) {
asbConfirmScriptOverwrite($dupeId, $tid);
}

if (!$script->save()) {
flash_message($lang->asb_script_save_fail, 'error');
admin_redirect($redirectUrl);
}

asbCacheHasChanged();

flash_message($lang->asb_script_save_success, 'success');
admin_redirect($html->url(array('action' => 'view_scripts', 'tid' => $tid)));
} elseif ($mybb->input['mode'] == 'overwrite') {
$id = (int) $mybb->input['id'];
$redirectUrl = $html->url(array('action' => 'view_scripts', 'mode' => 'edit', 'tid' => $tid, 'id' => $id));

$script = new ScriptInfo($id);

if (!$script->isValid()) {
flash_message($lang->asb_script_save_fail, 'error');
admin_redirect($redirectUrl);
}

$script->set($mybb->input['data']);

if (!$script->save()) {
flash_message($lang->asb_script_save_fail, 'error');
admin_redirect($redirectUrl);
Expand Down Expand Up @@ -1289,11 +1317,11 @@ function asb_admin_view_scripts()
'find_bottom' => '{$footer}',
'replace_all' => 0,
'eval' => 0,
'width_left' => '15',
'width_left' => '20',
'left_margin' => '0.5',
'width_middle' => '69',
'width_middle' => '59',
'right_margin' => '0.5',
'width_right' => '15',
'width_right' => '20',
);

$action = $lang->asb_edit_script;
Expand Down Expand Up @@ -1861,6 +1889,48 @@ function asb_admin_delete_addon()
admin_redirect($html->url(array('action' => 'manage_modules')));
}

/**
* confirm overwriting a script definition on save
*
* @param string
* @param int
* @return void
*/
function asbConfirmScriptOverwrite($id, $tid)
{
global $html, $mybb, $db, $page, $lang, $html, $min, $cp_style;;

$page->extra_header .= <<<EOF
<link rel="stylesheet" type="text/css" href="styles/{$cp_style}/asb/global.css" media="screen" />
EOF;

$page->add_breadcrumb_item($lang->asb, $html->url());
$page->add_breadcrumb_item($lang->asb_overwrite_confirmation);
$page->output_header("{$lang->asb} - {$lang->asb_overwrite_confirmation}");
asbOutputTabs('asb_overwrite_confirmation');

$form = new Form($html->url(array('action' => 'view_scripts', 'mode' => 'overwrite')), 'post', '', 1);

$dataElements = '';
foreach ($mybb->input as $key => $val) {
$dataElements .= $form->generate_hidden_field("data[{$key}]", $val);
}

$formContainer = new FormContainer($lang->asb_overwrite_confirmation);
$formContainer->output_row($lang->asb_overwrite_confirmation, '', $lang->asb_save_overwrite_warning.$form->generate_hidden_field('overwrite', true).$dataElements.$form->generate_hidden_field('id', $id).$form->generate_hidden_field('tid', $tid));
$formContainer->end();

$confirmationButtons[] = $form->generate_submit_button($lang->asb_confirm, array('name' => 'confirm'));
$confirmationButtons[] = $form->generate_submit_button($lang->asb_cancel, array('name' => 'cancel'));
$form->output_submit_wrapper($confirmationButtons);
$form->end();

// output the link menu and MyBB footer
asbOutputFooter('manage_scripts');
exit;
}

/**
* confirm overwriting a script definition on import
*
Expand Down
37 changes: 0 additions & 37 deletions Upload/inc/plugins/asb/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,43 +118,6 @@ function asbBuildScriptFilename($script='')
return $filename;
}

/**
* checks if a script definition is already represented, relative to the filename and query parameters
*
* @param array
* @param int theme id
* @return string filename marked up for asb
*/
function asbFindDuplicateScriptByFilename($keys, $tid=0)
{
global $db;

if (!is_array($keys) ||
empty($keys) ||
!isset($keys['filename']) ||
empty($keys['filename'])) {
return false;
}

$tid = (int) $tid;

$where = "tid='{$tid}'";
foreach ($keys as $key => $val) {
$val = $db->escape_string($val);

if ($key && $val) {
$where .= " AND {$key}='{$val}'";
}
}

$query = $db->simple_select('asb_script_info', 'id', $where);
if ($db->num_rows($query) == 0) {
return false;
}

return true;
}

/**
* get the correct cached script info using the script parameters
*
Expand Down
37 changes: 37 additions & 0 deletions Upload/inc/plugins/asb/functions_acp.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,4 +557,41 @@ function asbBuildFilterSelector($filter)
return $form->end();
}

/**
* checks if a script definition is already represented, relative to the filename and query parameters
*
* @param array
* @param int theme id
* @return string filename marked up for asb
*/
function asbFindDuplicateScriptByFilename($keys, $tid=0)
{
global $db;

if (!is_array($keys) ||
empty($keys) ||
!isset($keys['filename']) ||
empty($keys['filename'])) {
return false;
}

$tid = (int) $tid;

$where = "tid='{$tid}'";
foreach ($keys as $key => $val) {
$val = $db->escape_string($val);

if ($key && $val) {
$where .= " AND {$key}='{$val}'";
}
}

$query = $db->simple_select('asb_script_info', 'id', $where);
if ($db->num_rows($query) == 0) {
return false;
}

return (int) $db->fetch_field($query, 'id');
}

?>

0 comments on commit b715cac

Please sign in to comment.