<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[2304] 2013/codebykat/post-by-email/trunk: PIN-based authentication</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://gsoc.trac.wordpress.org/changeset/2304">2304</a></dd>
<dt>Author</dt> <dd>codebykat</dd>
<dt>Date</dt> <dd>2013-09-13 01:26:59 +0000 (Fri, 13 Sep 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>PIN-based authentication</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#2013codebykatpostbyemailtrunkclasspostbyemailadminphp">2013/codebykat/post-by-email/trunk/class-post-by-email-admin.php</a></li>
<li><a href="#2013codebykatpostbyemailtrunkclasspostbyemailphp">2013/codebykat/post-by-email/trunk/class-post-by-email.php</a></li>
<li><a href="#2013codebykatpostbyemailtrunkjsadminjs">2013/codebykat/post-by-email/trunk/js/admin.js</a></li>
<li><a href="#2013codebykatpostbyemailtrunkreadmemd">2013/codebykat/post-by-email/trunk/readme.md</a></li>
<li><a href="#2013codebykatpostbyemailtrunkreadmetxt">2013/codebykat/post-by-email/trunk/readme.txt</a></li>
<li><a href="#2013codebykatpostbyemailtrunkviewsadminphp">2013/codebykat/post-by-email/trunk/views/admin.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="2013codebykatpostbyemailtrunkclasspostbyemailadminphp"></a>
<div class="modfile"><h4>Modified: 2013/codebykat/post-by-email/trunk/class-post-by-email-admin.php (2303 => 2304)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/codebykat/post-by-email/trunk/class-post-by-email-admin.php 2013-09-13 01:26:50 UTC (rev 2303)
+++ 2013/codebykat/post-by-email/trunk/class-post-by-email-admin.php    2013-09-13 01:26:59 UTC (rev 2304)
</span><span class="lines">@@ -40,6 +40,8 @@
</span><span class="cx">          // AJAX hook to clear the log
</span><span class="cx">          add_action( 'wp_ajax_post_by_email_clear_log', array( $this, 'clear_log') );
</span><span class="cx"> 
</span><ins>+               add_action( 'wp_ajax_post_by_email_generate_pin', array( $this, 'generate_pin') );
+
</ins><span class="cx">           add_action( 'admin_notices', array( $this, 'admin_notices' ) );
</span><span class="cx">  
</span><span class="cx">          add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
</span><span class="lines">@@ -110,6 +112,9 @@
</span><span class="cx">                  $options['status'] = '';
</span><span class="cx">          }
</span><span class="cx"> 
</span><ins>+               $options['pin_required'] = isset( $input['pin_required'] );
+               $options['pin'] = trim( $input['pin'] );
+
</ins><span class="cx">           return $options;
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="lines">@@ -189,4 +194,18 @@
</span><span class="cx">          die();
</span><span class="cx">  }
</span><span class="cx"> 
</span><ins>+       /**
+        * Generate a good PIN.
+        *
+        * @since    1.0.2
+       */
+       public function generate_pin() {
+               check_ajax_referer( 'post-by-email-generate-pin', 'security' );
+               if ( current_user_can( 'manage_options' ) ) {
+                       echo wp_generate_password( 8, true, false );
+               }
+
+               die();
+       }
+
</ins><span class="cx"> }
</span><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="2013codebykatpostbyemailtrunkclasspostbyemailphp"></a>
<div class="modfile"><h4>Modified: 2013/codebykat/post-by-email/trunk/class-post-by-email.php (2303 => 2304)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/codebykat/post-by-email/trunk/class-post-by-email.php       2013-09-13 01:26:50 UTC (rev 2303)
+++ 2013/codebykat/post-by-email/trunk/class-post-by-email.php  2013-09-13 01:26:59 UTC (rev 2304)
</span><span class="lines">@@ -73,6 +73,8 @@
</span><span class="cx">          'default_email_category'    => '',
</span><span class="cx">          'delete_messages'           => true,
</span><span class="cx">          'status'                    => 'unconfigured',
</span><ins>+               'pin_required'              => false,
+               'pin'                       => '',
</ins><span class="cx">   );
</span><span class="cx"> 
</span><span class="cx">  /**
</span><span class="lines">@@ -315,7 +317,19 @@
</span><span class="cx">                  if ( '' == $post_title )
</span><span class="cx">                          $post_title = $subject;
</span><span class="cx"> 
</span><ins>+                       /* validate PIN */
+                       if ( $options['pin_required'] ) {
+                               $pin = $this->find_shortcode( 'pin', $post_content );
+                               $pin = implode( $pin );
</ins><span class="cx"> 
</span><ins>+                               if( $pin != $options['pin'] ) {
+                                       // security check failed - move on to the next message
+                                       $log_message .= '<br />"' . $post_title . '" ' . __( 'failed PIN authentication; discarding.', 'post-by-email' );
+                                       continue;
+                               }
+                       }
+
+
</ins><span class="cx">                   /* categories */
</span><span class="cx"> 
</span><span class="cx">                  $shortcode_categories = $this->find_shortcode( 'category', $post_content );
</span><span class="lines">@@ -656,7 +670,7 @@
</span><span class="cx">   * @return   string    $text         Filtered text
</span><span class="cx">   */
</span><span class="cx">  protected function filter_valid_shortcodes( $text ) {
</span><del>-               foreach ( array( 'tag', 'category' ) as $shortcode ) {
</del><ins>+                foreach ( array( 'tag', 'category', 'pin' ) as $shortcode ) {
</ins><span class="cx">                   $text = preg_replace( "/\[$shortcode\s(.*?)\]/i", '', $text );        
</span><span class="cx">          }
</span><span class="cx">          return $text;
</span></span></pre></div>
<a id="2013codebykatpostbyemailtrunkjsadminjs"></a>
<div class="modfile"><h4>Modified: 2013/codebykat/post-by-email/trunk/js/admin.js (2303 => 2304)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/codebykat/post-by-email/trunk/js/admin.js   2013-09-13 01:26:50 UTC (rev 2303)
+++ 2013/codebykat/post-by-email/trunk/js/admin.js      2013-09-13 01:26:59 UTC (rev 2304)
</span><span class="lines">@@ -6,7 +6,7 @@
</span><span class="cx"> 
</span><span class="cx">          var data = {
</span><span class="cx">                  action: 'post_by_email_clear_log',
</span><del>-                       security: '<?php echo wp_create_nonce( "post-by-email-clear-log" ); ?>'
</del><ins>+                        security: logNonce
</ins><span class="cx">           };
</span><span class="cx"> 
</span><span class="cx">          jQuery.post( ajaxurl, data, function( response ) {
</span><span class="lines">@@ -15,6 +15,20 @@
</span><span class="cx">          });
</span><span class="cx">  });
</span><span class="cx"> 
</span><ins>+       // AJAX request for a new PIN
+       jQuery( 'input#generatePIN' ).click( function( e ) {
+               e.preventDefault();
+
+               var data = {
+                       action: 'post_by_email_generate_pin',
+                       security: pinNonce
+               };
+
+               jQuery.post( ajaxurl, data, function( response ) {
+                       jQuery( 'input#post_by_email_options\\\[pin\\\]' ).val( response );
+               });
+       });
+
</ins><span class="cx">   // tab switching
</span><span class="cx">  jQuery( 'a.nav-tab' ).click( function( e ) {
</span><span class="cx">          e.preventDefault();
</span><span class="lines">@@ -34,19 +48,27 @@
</span><span class="cx">          jQuery( 'input#post_by_email_options\\[delete_messages\\]' ).attr( 'disabled', false );
</span><span class="cx">  });
</span><span class="cx"> 
</span><del>-       if( 'POP3' == jQuery( 'select#post_by_email_options\\[mailserver_protocol\\]' ).val() ) {
</del><ins>+        if ( 'POP3' == jQuery( 'select#post_by_email_options\\[mailserver_protocol\\]' ).val() ) {
</ins><span class="cx">           jQuery( 'input#post_by_email_options\\[delete_messages\\]' ).attr( 'checked', 'checked' );
</span><span class="cx">          jQuery( 'input#post_by_email_options\\[delete_messages\\]' ).attr( 'disabled', true);
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  jQuery( 'select#post_by_email_options\\[mailserver_protocol\\]' ).change( function( e ) {
</span><del>-               if( 'POP3' == jQuery( e.target ).val() ) {
</del><ins>+                if ( 'POP3' == jQuery( e.target ).val() ) {
</ins><span class="cx">                   jQuery( 'input#post_by_email_options\\[delete_messages\\]' ).attr( 'checked', 'checked' );
</span><span class="cx">                  jQuery( 'input#post_by_email_options\\[delete_messages\\]' ).attr( 'disabled', true);
</span><del>-               }
-               else {
</del><ins>+                } else {
</ins><span class="cx">                   jQuery( 'input#post_by_email_options\\[delete_messages\\]' ).attr( 'disabled', false );
</span><span class="cx">          }
</span><span class="cx">  });
</span><span class="cx"> 
</span><ins>+       // PIN tab
+       jQuery( 'input#post_by_email_options\\[pin_required\\]' ).click( function( e ) {
+               if ( jQuery( e.target ).attr('checked') ) {
+                       jQuery( 'tr.post-by-email-pin-settings' ).show();
+               } else {
+                       jQuery( 'tr.post-by-email-pin-settings' ).hide();
+               }
+       });
+
</ins><span class="cx"> });
</span><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="2013codebykatpostbyemailtrunkreadmemd"></a>
<div class="modfile"><h4>Modified: 2013/codebykat/post-by-email/trunk/readme.md (2303 => 2304)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/codebykat/post-by-email/trunk/readme.md     2013-09-13 01:26:50 UTC (rev 2303)
+++ 2013/codebykat/post-by-email/trunk/readme.md        2013-09-13 01:26:59 UTC (rev 2304)
</span><span class="lines">@@ -46,6 +46,7 @@
</span><span class="cx"> 
</span><span class="cx"> ### 1.0.2 ###
</span><span class="cx"> * Support shortcodes to specify categories and tags.
</span><ins>+* Added PIN-based authentication.
</ins><span class="cx"> 
</span><span class="cx"> ### 1.0.1 ###
</span><span class="cx"> * Added tabs and additional options to options panel.
</span></span></pre></div>
<a id="2013codebykatpostbyemailtrunkreadmetxt"></a>
<div class="modfile"><h4>Modified: 2013/codebykat/post-by-email/trunk/readme.txt (2303 => 2304)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/codebykat/post-by-email/trunk/readme.txt    2013-09-13 01:26:50 UTC (rev 2303)
+++ 2013/codebykat/post-by-email/trunk/readme.txt       2013-09-13 01:26:59 UTC (rev 2304)
</span><span class="lines">@@ -46,6 +46,7 @@
</span><span class="cx"> 
</span><span class="cx"> = 1.0.2 =
</span><span class="cx"> * Support shortcodes to specify categories and tags.
</span><ins>+* Added PIN-based authentication.
</ins><span class="cx"> 
</span><span class="cx"> = 1.0.1 =
</span><span class="cx"> * Added tabs and additional options to options panel.
</span></span></pre></div>
<a id="2013codebykatpostbyemailtrunkviewsadminphp"></a>
<div class="modfile"><h4>Modified: 2013/codebykat/post-by-email/trunk/views/admin.php (2303 => 2304)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/codebykat/post-by-email/trunk/views/admin.php       2013-09-13 01:26:50 UTC (rev 2303)
+++ 2013/codebykat/post-by-email/trunk/views/admin.php  2013-09-13 01:26:59 UTC (rev 2304)
</span><span class="lines">@@ -31,6 +31,9 @@
</span><span class="cx">          <a id="nav-connection" href="<?php echo admin_url('tools.php?page='.$_GET['page'].'&tab=connection'); ?>" class="nav-tab <?php if ( 'connection' == $tab ) { echo 'nav-tab-active'; } ?>">
</span><span class="cx">                  <?php _e( 'Mailbox Details', 'post-by-email' ); ?>
</span><span class="cx">          </a>
</span><ins>+               <a id="nav-security" href="<?php echo admin_url('tools.php?page='.$_GET['page'].'&tab=security'); ?>" class="nav-tab <?php if ( 'security' == $tab ) { echo 'nav-tab-active'; } ?>">
+                       <?php _e( 'Security', 'post-by-email' ); ?>
+               </a>
</ins><span class="cx">           <a id="nav-log" href="<?php echo admin_url('tools.php?page='.$_GET['page'].'&tab=log'); ?>" class="nav-tab <?php if ( 'log' == $tab ) { echo 'nav-tab-active'; } ?>">
</span><span class="cx">                  <?php _e( 'Activity Log', 'post-by-email' ); ?>
</span><span class="cx">          </a>
</span><span class="lines">@@ -44,14 +47,11 @@
</span><span class="cx">          <div class='tab-content' id='tab-main' <?php if ( 'main' != $tab ) { echo 'style="display:none;"'; } ?>>
</span><span class="cx">                  <p>
</span><span class="cx">                          <?php
</span><del>-                                       printf( __( 'To post to WordPress by e-mail you must set up a secret e-mail account
-                                                                with IMAP or POP3 access. Any mail received at this address will be posted, so
-                                                                it&#8217;s a good idea to keep this address very secret. Here are three
-                                                                random strings you could use: <kbd>%s</kbd>, <kbd>%s</kbd>, <kbd>%s</kbd>.',
-                                                                'post-by-email' ),
-                                                       wp_generate_password( 8, false ),
-                                                       wp_generate_password( 8, false ),
-                                                       wp_generate_password( 8, false ) )
</del><ins>+                                        _e( "To post to WordPress by e-mail you must set up a special-purpose e-mail account
+                                               with IMAP or POP3 access. Any mail received at this address will be posted, so it's
+                                               a good idea to keep this address very secret.  For an extra level of security, enable
+                                               PIN-based authentication under the Security tab.",
+                                               'post-by-email' );
</ins><span class="cx">                           ?>
</span><span class="cx">                  </p>
</span><span class="cx"> 
</span><span class="lines">@@ -170,6 +170,51 @@
</span><span class="cx">                  <input type="button" id="resetButton" class="button-secondary" value="<?php esc_attr_e( 'Reset to Defaults', 'post-by-email'); ?>" />
</span><span class="cx">                  <?php submit_button(); ?>
</span><span class="cx">          </div>
</span><ins>+
+               <div class='tab-content' id='tab-security' <?php if ( 'security' != $tab ) { echo 'style="display:none;"'; } ?>>
+                       <p>
+                               <?php
+                                       _e( 'If you do not require a PIN to create a new post, anyone who knows your email address and the address of your Post By Email inbox will be able to post to this blog.',
+                                               'post-by-email' );
+                               ?>
+                       </p>
+                       <p>
+                               <?php
+                                       _e( 'Once you have enabled PIN-based authentication, include the PIN in your email with a shortcode.  Emails that do not include the correct PIN will be discarded.', 'post-by-email' );
+                               ?>
+                       </p>
+                       <p>
+                               <?php _e( 'Example:', 'post-by-email' ); ?> <kbd>[pin 12345]</kbd>
+                       </p>
+
+                       <table class="form-table">
+                               <tr valign="top">
+                                       <th scope="row">
+                                               <label for="post_by_email_options[pin_required]">
+                                                       <?php _e( 'Require a PIN to post?', 'post-by-email' ); ?>
+                                               </label>
+                                       </th>
+                                       <td>
+                                               <input type="checkbox" name="post_by_email_options[pin_required]" id="post_by_email_options[pin_required]" <?php checked( $options['pin_required'] ); ?> />
+                                       </td>
+                               </tr>
+                               <tr class="post-by-email-pin-settings" <?php if ( ! $options['pin_required'] ) { echo 'style="display:none;";'; } ?>>
+                                       <th scope="row">
+                                               <label for="post_by_email_options[pin]">
+                                                       <?php _e( 'PIN', 'post-by-email' ); ?>
+                                               </label>
+                                       </th>
+                                       <td>
+                                               <input type="text" name="post_by_email_options[pin]" id="post_by_email_options[pin]" value="<?php echo $options['pin']; ?>" />
+                                               <input type="button" class="button-secondary" href='' id="generatePIN" value="<?php _e( 'Generate' ); ?>" />
+                                       </td>
+                               </tr>
+                       </table>
+
+                       <?php submit_button(); ?>
+
+               </div>
+
</ins><span class="cx">   </form>
</span><span class="cx"> 
</span><span class="cx">  <div class='tab-content' id='tab-log' <?php if ( 'log' != $tab ) { echo 'style="display:none;"'; } ?>>
</span><span class="lines">@@ -223,4 +268,9 @@
</span><span class="cx">                  </table>
</span><span class="cx">          <?php endif; ?>
</span><span class="cx">  </div>
</span><del>-</div>
</del><span class="cx">\ No newline at end of file
</span><ins>+</div>
+
+<script type="text/javascript">
+       var logNonce = "<?php echo wp_create_nonce( 'post-by-email-clear-log' ); ?>";
+       var pinNonce = "<?php echo wp_create_nonce( 'post-by-email-generate-pin' ); ?>";
+</script>
</ins><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>

</body>
</html>