Plugin Directory

Changeset 3168966


Ignore:
Timestamp:
10/15/2024 02:34:08 AM (18 months ago)
Author:
jamesdlow
Message:

1.4.3

  • Add optional API key authentication for WP JSON API
Location:
pageapp/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • pageapp/trunk/inc/jsonlib.php

    r2480315 r3168966  
    33class JsonLib {
    44    static $json = false;
    5     static $apikey = null;
     5    static $apikeys = null;
    66    static $path = null;
    77
    88    function __construct($path, $apikey = null) {
    9         self::$apikey = $apikey;
     9        self::$apikeys = $apikey ? (is_array($apikey) ? $apikey : array($apikey)) : array();
    1010        self::add_hooks($path);
    1111    }
     
    4242                    }
    4343                } elseif ($function->isProtected()) {
    44                     $apikey = self::assert_param('apikey');
    45                     if ($apikey == self::$apikey) {
     44                    if (in_array(self::assert_param('apikey'), self::$apikeys)) {
    4645                        return $json->$method();
    4746                    } else {
  • pageapp/trunk/pageapp-json.php

    r2982921 r3168966  
    33class PageAppJson extends JsonLib {
    44    function __construct() {
    5         parent::__construct('pageapp/v1', get_option('pageapp_apikey'));
     5        parent::__construct('pageapp/v1', PageApp::api_keys());
    66    }
    77   
  • pageapp/trunk/pageapp.php

    r3150723 r3168966  
    44Plugin URI: https://wordpress.org/plugins/pageapp/
    55Description: Extensions to Wordpress wp-json for the PageApp API and mobile framework
    6 Version: 1.4.2
     6Version: 1.4.3
    77Author: PageApp
    88Author URI: https://www.pageapp.com
     
    5959        add_filter('login_url', array(static::class, 'redirect_to'), 10, 3);
    6060        add_filter('lostpassword_redirect', array(static::class, 'lostpassword_redirect'));
     61        add_filter('rest_pre_dispatch', array(static::class, 'rest_pre_dispatch'), 10, 3);
    6162    }
    6263    public static function init() {
     
    6667       
    6768        $main = new SettingsLib(array(
     69            array('id'=>'pageapp_apioptions', 'type'=>'title', 'title'=>'WP JSON Meta'),
    6870            array('id'=>'pageapp_relevanssi', 'type'=>'boolean', 'title'=>'Enable Relevanssi'),
    6971            array('id'=>'pageapp_whitelist', 'type'=>'boolean', 'title'=>'Whitelist Post Meta'),
     
    7173            array('id'=>'pageapp_categories', 'type'=>'boolean', 'title'=>'Include Category Details'),
    7274            array('id'=>'pageapp_customposts', 'type'=>'boolean', 'title'=>'Include Custom Post Types'),
    73             array('id'=>'pageapp_authentication', 'type'=>'boolean', 'title'=>'Enable Authentication API (Depricated)'),
    74             array('id'=>'pageapp_maxresults', 'type'=>'integer', 'title'=>'Max Results', 'default'=>100, 'description'=>'Maximum results returned over wp-json API'),
    75             array('id'=>'pageapp_apikey', 'type'=>'string', 'title'=>'API Key', 'default'=>md5(wp_salt().time()), 'description'=>'API key for public PageApp functions'),
     75            array('id'=>'pageapp_apisettings', 'type'=>'title', 'title'=>'API Settings'),
     76            array('id'=>'pageapp_restkey', 'type'=>'boolean', 'title'=>'WP JSON Key', 'description'=>'Require apikey on WP JSON API'),
     77            array('id'=>'pageapp_apikey', 'type'=>'text', 'title'=>'API Keys', 'default'=>md5(wp_salt().time()), 'description'=>'One per line'),
     78            array('id'=>'pageapp_maxresults', 'type'=>'integer', 'title'=>'Max Results', 'default'=>100, 'description'=>'Maximum results returned over WP JSON API'),
     79            array('id'=>'pageapp_authentication', 'type'=>'boolean', 'title'=>'Enable Authentication API', 'description'=>'(Deprecated in favour of WP OAuth Server plugin)'),
     80            array('id'=>'pageapp_registration', 'type'=>'title', 'title'=>'User Registration'),
    7681            array('id'=>'pageapp_username', 'type'=>'boolean', 'title'=>'Hide username field in registration form'),
    7782            array('id'=>'pageapp_password', 'type'=>'boolean', 'title'=>'Enable password field in registration form'),
     
    127132        self::$ValueCache = new ValueCache(__FILE__, 'pa');
    128133    }
     134    public static function admin_init() {
     135        self::register_cssjs();
     136    }
     137    public static function register_cssjs() {
     138        wp_register_style('pageapp-admin-style', self::plugin().'/css/admin.css');
     139        wp_register_script('pageapp-admin-script', self::plugin().'/js/admin.js');
     140    }
     141    public static function include_cssjs() {
     142        if (isset($_GET['page']) && strpos($_GET['page'], self::$prefix) === 0) {
     143            wp_enqueue_script('jquery');
     144            wp_enqueue_script('jquery-ui-dialog');
     145            wp_enqueue_script('jquery-ui-sortable');
     146            //wp_enqueue_style('jquery-style', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
     147            wp_enqueue_style('pageapp-admin-style');
     148            wp_enqueue_script('pageapp-admin-script');
     149        }
     150    }
     151
     152    /* Helper Functions */
     153    public static function sanitize_options($input) {
     154        return $input;
     155    }
     156    public static function api_keys() {
     157        $keys = get_option('pageapp_apikey');
     158        $parts = preg_split('/[\s,]+/', $keys);
     159        return array_filter($parts);
     160    }
    129161
    130162    /* Registration hooks */
     
    234266    }
    235267
    236     /* Other Functions */
     268    /* Rest/WP-JSON Functions */
    237269    public static function rest_collection_params($params, $post_type) {
    238270        if (isset($params['per_page'])) {
     
    393425        }
    394426    }
    395     public static function admin_init() {
    396         self::register_cssjs();
    397     }
    398427    public static function register_options() {
    399428        //Whitelist Meta
     
    404433        return $value > 0 ? $value : self::$maxdefault;
    405434    }
    406     public static function sanitize_options($input) {
    407         return $input;
    408     }
    409     public static function register_cssjs() {
    410         wp_register_style('pageapp-admin-style', self::plugin().'/css/admin.css');
    411         wp_register_script('pageapp-admin-script', self::plugin().'/js/admin.js');
    412     }
    413     public static function include_cssjs() {
    414         if (isset($_GET['page']) && strpos($_GET['page'], self::$prefix) === 0) {
    415             wp_enqueue_script('jquery');
    416             wp_enqueue_script('jquery-ui-dialog');
    417             wp_enqueue_script('jquery-ui-sortable');
    418             //wp_enqueue_style('jquery-style', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
    419             wp_enqueue_style('pageapp-admin-style');
    420             wp_enqueue_script('pageapp-admin-script');
    421         }
    422     }
     435    public static function rest_pre_dispatch($result, $server, $request) {
     436        if (strpos($request->get_route(), '/wp/v2/') === 0 && get_option('pageapp_restkey') == '1') {
     437            if (!isset($_REQUEST['apikey']) || empty($_REQUEST['apikey'])) {
     438                return new WP_Error('missing_api_key', 'The apikey is missing from the request.', array('status' => 403));
     439            } else if (!in_array($_REQUEST['apikey'], self::api_keys())) {
     440                return new WP_Error('invalid_api_key', 'The apikey is invalid.', array('status' => 403));
     441            }
     442        }
     443        return $result;
     444    }
     445
     446    /* Post Meta Admin Functions */
    423447    public static function admin_menu() {
    424448        add_submenu_page(self::$prefix, 'Post Meta', 'Post Meta', 'manage_options', self::$prefix.'-meta', array(self::class, 'post_meta'));
     
    512536        <?php
    513537    }
     538
     539    /* Relevanssi Functions */
    514540    public static function relevanssi_installed() {
    515541        return function_exists('relevanssi_do_query');
     
    570596        return $resp;
    571597    }
     598
     599    /* Cache Functions */
    572600    public static function cache_xml($url) {
    573601        self::require_http();
  • pageapp/trunk/readme.txt

    r3150723 r3168966  
    44Requires at least: 3.0
    55Tested up to: 6.5.4
    6 Stable tag: 1.4.2
     6Stable tag: 1.4.3
    77License: © 2024 Thireen32 Pty Ltd
    88Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=K6VKWB3HZB2T2&item_name=Donation%20to%20jameslow%2ecom&currency_code=USD&bn=PP%2dDonationsBF&charset=UTF%2d8
     
    2828
    2929== Changelog ==
     30
     31= 1.4.3 =
     32* Add optional API key authentication for WP JSON API
    3033
    3134= 1.4.2 =
Note: See TracChangeset for help on using the changeset viewer.