コード例 #1
0
/**
 * Check if client is in configured IPs / IP ranges.
 * 
 * @return boolean
 */
function ipr_client_ip_is_allowed()
{
    $client_ip = ipr_get_client_ip();
    $allowed_ips = ipr_get_allowed_ips();
    $pattern_to_regex = function ($p) {
        $p = preg_replace_callback("|\\[(\\d+)-(\\d+)\\]|", function ($matches) {
            return "(" . implode("|", range($matches[1], $matches[2])) . ")";
        }, $p);
        // allow ranges like [0-1] or [33-60]
        $p = str_replace(".", "\\.", $p);
        // turn dots into real dots
        $p = str_replace("*", "\\d{1,3}", $p);
        // wildcard to "1 to 3 digit number"
        $p = '/^' . $p . '$/';
        // make sure it's a complete match
        return $p;
    };
    foreach ($allowed_ips as $ip) {
        if ($client_ip == $ip) {
            return true;
        }
        if (preg_match($pattern_to_regex($ip), $client_ip)) {
            return true;
        }
    }
    return false;
}
コード例 #2
0
/**
 * Displays the options page. The big difference here is where you post the data
 * because, unlike for normal option pages, there is nowhere to process it by
 * default so we have to create our own hook to process the saving of our options.
 */
function ipr_network_options_page_callback()
{
    if (isset($_GET['updated'])) {
        ?>
<div id="message" class="updated notice is-dismissible"><p><?php 
        _e('Options saved.');
        ?>
</p></div>
  <?php 
    }
    ?>
<div class="wrap">
  <h1><?php 
    _e('IP Post Restrict Options', 'wp-ip-post-restrict');
    ?>
</h1>
  
  <div class="card">
    <form method="POST" action="edit.php?action=ipr_update_network_options"><?php 
    settings_fields('ipr_network_options_page');
    do_settings_sections('ipr_network_options_page');
    submit_button();
    ?>
    </form>
  </div>

  <div class="card">
    Your current IP <code><?php 
    echo ipr_get_client_ip();
    ?>
</code> <em>is <?php 
    if (!ipr_client_ip_is_allowed()) {
        ?>
<strong>not</strong><?php 
    }
    ?>
 part of the configured ranges</em>.
  </div>
</div>
<?php 
}