Skip to content

Commit

Permalink
Merge pull request #1343 from CaerCam/lunadev
Browse files Browse the repository at this point in the history
Implement nonces on edit/post forms (#1342 #1248) + fix JS onbeforeunload overuse
  • Loading branch information
Yannick committed Aug 19, 2015
2 parents 17c6ffe + 2bb0667 commit 37665d8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
3 changes: 2 additions & 1 deletion edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@

if (isset($_POST['form_sent'])) {
// Make sure they got here from the site
confirm_referrer('edit.php');
if (!isset($_POST['_luna_nonce_edit_post']) || !LunaNonces::verify($_POST['_luna_nonce_edit_post'],'edit-post'))
message(__('Are you sure you want to do this?', 'luna'));

// If it's a topic it must contain a subject
if ($can_edit_subject) {
Expand Down
41 changes: 41 additions & 0 deletions include/class/luna_nonces.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,29 @@ private function _create() {
return $this->nonce;
}

/**
* Output a nonce field.
*
* Create a HTML <INPUT> field to store the nonce. If no name is set for
* the field, generate a default one based on the action.
*
* @since 1.1
*
* @param string $action Nonce action
* @param string $name Name of the field
*
* @return void
*/
private function _field($name = null) {

$nonce = $this->_create();
if ( is_null( $name ) ) {
$name = '_luna_nonce_' . str_replace( '-', '_', strtolower( $this->action ) );
}

echo '<input type="hidden" name="' . $name . '" value="' . $nonce . '"/>';
}

/**
* Check a nonce validity.
*
Expand Down Expand Up @@ -231,6 +254,24 @@ public static function verify($nonce, $action = -1) {
return $check;
}

/**
* Output a nonce field.
*
* This method is static and can be called publicly.
*
* @since 1.1
*
* @param string $action Nonce action
* @param string $name Name of the field
*
* @return void
*/
public static function field($action = -1, $name = null) {

$nonce = new LunaNonces($action);
$nonce = $nonce->_field($name);
}

/**
* Match to hash againts each other to determine if they're identical.
*
Expand Down
13 changes: 11 additions & 2 deletions include/draw_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,19 @@ function draw_editor($height) {
elseif (FORUM_ACTIVE_PAGE == 'new-inbox')
echo luna_htmlspecialchars(isset($p_message) ? $p_message : '');
?></textarea>
<?php
if (FORUM_ACTIVE_PAGE == 'edit')
$action = 'edit-post';
elseif (FORUM_ACTIVE_PAGE == 'new-inbox')
$action = 'post-message';
else
$action = ($fid ? 'post-topic' : 'post-reply');
LunaNonces::field($action);
?>
<div class="btn-toolbar textarea-toolbar textarea-bottom">
<div class="btn-group pull-right">
<button class="btn btn-with-text btn-default" type="submit" name="preview" accesskey="p" tabindex="<?php echo $cur_index++ ?>"><span class="fa fa-fw fa-eye"></span> <?php _e('Preview', 'luna') ?></button>
<button class="btn btn-with-text btn-primary" type="submit" name="submit" accesskey="s" tabindex="<?php echo $cur_index++ ?>"><span class="fa fa-fw fa-plus"></span> <?php _e('Submit', 'luna') ?></button>
<button class="btn btn-with-text btn-default" type="submit" name="preview" accesskey="p" tabindex="<?php echo $cur_index++ ?>" onclick="window.onbeforeunload=null"><span class="fa fa-fw fa-eye"></span> <?php _e('Preview', 'luna') ?></button>
<button class="btn btn-with-text btn-primary" type="submit" name="submit" accesskey="s" tabindex="<?php echo $cur_index++ ?>" onclick="window.onbeforeunload=null"><span class="fa fa-fw fa-plus"></span> <?php _e('Submit', 'luna') ?></button>
</div>
</div>
</fieldset>
Expand Down
4 changes: 3 additions & 1 deletion post.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
$errors[] = sprintf(__('At least %s seconds have to pass between posts. Please wait %s seconds and try posting again.', 'luna'), $luna_user['g_post_flood'], $luna_user['g_post_flood'] - (time() - $luna_user['last_post']));

// Make sure they got here from the site
confirm_referrer(array('post.php', 'viewtopic.php'));
if (($fid && (!isset($_POST['_luna_nonce_post_topic']) || !LunaNonces::verify($_POST['_luna_nonce_post_topic'],'post-reply'))) ||
(!$fid && (!isset($_POST['_luna_nonce_post_reply']) || !LunaNonces::verify($_POST['_luna_nonce_post_reply'],'post-reply'))))
message(__('Are you sure you want to do this?', 'luna'));

// If it's a new topic
if ($fid) {
Expand Down

0 comments on commit 37665d8

Please sign in to comment.