first commit
This commit is contained in:
339
wp/wp-content/plugins/wp-native-php-sessions/LICENSE
Normal file
339
wp/wp-content/plugins/wp-native-php-sessions/LICENSE
Normal file
@@ -0,0 +1,339 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Lesser General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program is interactive, make it output a short notice like this
|
||||
when it starts in an interactive mode:
|
||||
|
||||
Gnomovision version 69, Copyright (C) year name of author
|
||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, the commands you use may
|
||||
be called something other than `show w' and `show c'; they could even be
|
||||
mouse-clicks or menu items--whatever suits your program.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||
|
||||
<signature of Ty Coon>, 1 April 1989
|
||||
Ty Coon, President of Vice
|
||||
|
||||
This General Public License does not permit incorporating your program into
|
||||
proprietary programs. If your program is a subroutine library, you may
|
||||
consider it more useful to permit linking proprietary applications with the
|
||||
library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License.
|
||||
139
wp/wp-content/plugins/wp-native-php-sessions/inc/class-admin.php
Normal file
139
wp/wp-content/plugins/wp-native-php-sessions/inc/class-admin.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
/**
|
||||
* Controller for backend functionality.
|
||||
*
|
||||
* @package WPNPS
|
||||
*/
|
||||
|
||||
namespace Pantheon_Sessions;
|
||||
|
||||
/**
|
||||
* Controller for backend functionality.
|
||||
*/
|
||||
class Admin {
|
||||
|
||||
/**
|
||||
* Copy of the singleton instance.
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
private static $instance;
|
||||
|
||||
/**
|
||||
* Name of capability required to perform actions.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $capability = 'manage_options';
|
||||
|
||||
/**
|
||||
* Gets a copy of the singleton instance.
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public static function get_instance() {
|
||||
if ( ! isset( self::$instance ) ) {
|
||||
self::$instance = new Admin();
|
||||
self::$instance->setup_actions();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load admin actions
|
||||
*/
|
||||
private function setup_actions() {
|
||||
|
||||
add_action( 'admin_menu', [ $this, 'action_admin_menu' ] );
|
||||
add_action( 'wp_ajax_pantheon_clear_session', [ $this, 'handle_clear_session' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the admin menu
|
||||
*/
|
||||
public function action_admin_menu() {
|
||||
|
||||
add_management_page( __( 'Pantheon Sessions', 'wp-native-php-sessions' ), __( 'Sessions', 'wp-native-php-sessions' ), self::$capability, 'pantheon-sessions', [ $this, 'handle_page' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the admin page
|
||||
*/
|
||||
public function handle_page() {
|
||||
global $wpdb;
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
|
||||
require_once __DIR__ . '/class-list-table.php';
|
||||
|
||||
echo '<div class="wrap">';
|
||||
|
||||
echo '<div>';
|
||||
$query_args = [
|
||||
'action' => 'pantheon_clear_session',
|
||||
'nonce' => wp_create_nonce( 'pantheon_clear_session' ),
|
||||
'session' => 'all',
|
||||
];
|
||||
if ( $wpdb->get_var( "SELECT COUNT(session_id) FROM $wpdb->pantheon_sessions" ) ) {
|
||||
echo '<a class="button pantheon-clear-all-sessions" style="float:right; margin-top: 9px;" href="' . esc_url( add_query_arg( $query_args, admin_url( 'admin-ajax.php' ) ) ) . '">' . esc_html__( 'Clear All', 'wp-native-php-sessions' ) . '</a>';
|
||||
}
|
||||
echo '<h2>' . esc_html__( 'Pantheon Sessions', 'wp-native-php-sessions' ) . '</h2>';
|
||||
if ( isset( $_GET['message'] ) && in_array( $_GET['message'], [ 'delete-all-session', 'delete-session' ], true ) ) {
|
||||
if ( 'delete-all-session' === $_GET['message'] ) {
|
||||
$message = __( 'Cleared all sessions.', 'wp-native-php-sessions' );
|
||||
} elseif ( 'delete-session' === $_GET['message'] ) {
|
||||
$message = __( 'Session cleared.', 'wp-native-php-sessions' );
|
||||
}
|
||||
echo '<div id="message" class="updated"><p>' . esc_html( $message ) . '</p></div>';
|
||||
}
|
||||
echo '</div>';
|
||||
|
||||
$wp_list_table = new List_Table();
|
||||
$wp_list_table->prepare_items();
|
||||
$wp_list_table->display();
|
||||
|
||||
echo '</div>';
|
||||
|
||||
add_action( 'admin_footer', [ $this, 'action_admin_footer' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a request to clear all sessions
|
||||
*/
|
||||
public function handle_clear_session() {
|
||||
global $wpdb;
|
||||
|
||||
if ( ! current_user_can( self::$capability ) || ! wp_verify_nonce( $_GET['nonce'], 'pantheon_clear_session' ) ) {
|
||||
wp_die( esc_html__( "You don't have permission to do this.", 'wp-native-php-sessions' ) );
|
||||
}
|
||||
|
||||
if ( ! empty( $_GET['session'] ) && 'all' === $_GET['session'] ) {
|
||||
$wpdb->query( "DELETE FROM $wpdb->pantheon_sessions" );
|
||||
$message = 'delete-all-session';
|
||||
} else {
|
||||
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->pantheon_sessions WHERE session_id=%s", sanitize_text_field( $_GET['session'] ) ) );
|
||||
$message = 'delete-session';
|
||||
}
|
||||
wp_safe_redirect( add_query_arg( 'message', $message, wp_get_referer() ) );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stuff that needs to go in the footer
|
||||
*/
|
||||
public function action_admin_footer() {
|
||||
?>
|
||||
<script>
|
||||
(function($){
|
||||
$(document).ready(function(){
|
||||
$('.pantheon-clear-all-sessions').on('click', function( e ){
|
||||
if ( ! confirm( '<?php esc_html_e( 'Are you sure you want to clear all active sessions?', 'wp-native-php-sessions' ); ?>') ) {
|
||||
e.preventDefault();
|
||||
}
|
||||
});
|
||||
});
|
||||
}(jQuery))
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/**
|
||||
* CLI interface to interact with Pantheon sessions.
|
||||
*
|
||||
* @package WPNPS
|
||||
*/
|
||||
|
||||
namespace Pantheon_Sessions;
|
||||
|
||||
use WP_CLI;
|
||||
|
||||
/**
|
||||
* Interact with Pantheon Sessions
|
||||
*/
|
||||
class CLI_Command extends \WP_CLI_Command {
|
||||
|
||||
/**
|
||||
* List all registered sessions.
|
||||
*
|
||||
* [--format=<format>]
|
||||
* : Accepted values: table, csv, json, count, ids. Default: table
|
||||
*
|
||||
* @subcommand list
|
||||
*/
|
||||
public function list_( $args, $assoc_args ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( ! PANTHEON_SESSIONS_ENABLED ) {
|
||||
WP_CLI::error( 'Pantheon Sessions is currently disabled.' );
|
||||
}
|
||||
|
||||
$defaults = [
|
||||
'format' => 'table',
|
||||
'fields' => 'session_id,user_id,datetime,ip_address,data',
|
||||
];
|
||||
$assoc_args = array_merge( $defaults, $assoc_args );
|
||||
|
||||
$sessions = [];
|
||||
foreach ( new \WP_CLI\Iterators\Query( "SELECT * FROM {$wpdb->pantheon_sessions} ORDER BY datetime DESC" ) as $row ) {
|
||||
$sessions[] = $row;
|
||||
}
|
||||
|
||||
\WP_CLI\Utils\Format_Items( $assoc_args['format'], $sessions, $assoc_args['fields'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete one or more sessions.
|
||||
*
|
||||
* [<session-id>...]
|
||||
* : One or more session IDs
|
||||
*
|
||||
* [--all]
|
||||
* : Delete all sessions.
|
||||
*
|
||||
* @subcommand delete
|
||||
*/
|
||||
public function delete( $args, $assoc_args ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( ! PANTHEON_SESSIONS_ENABLED ) {
|
||||
WP_CLI::error( 'Pantheon Sessions is currently disabled.' );
|
||||
}
|
||||
|
||||
if ( isset( $assoc_args['all'] ) ) {
|
||||
$args = $wpdb->get_col( "SELECT session_id FROM {$wpdb->pantheon_sessions}" );
|
||||
if ( empty( $args ) ) {
|
||||
WP_CLI::warning( 'No sessions to delete.' );
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $args as $session_id ) {
|
||||
$session = \Pantheon_Sessions\Session::get_by_sid( $session_id );
|
||||
if ( $session ) {
|
||||
$session->destroy();
|
||||
WP_CLI::log( sprintf( 'Session destroyed: %s', $session_id ) );
|
||||
} else {
|
||||
WP_CLI::warning( sprintf( "Session doesn't exist: %s", $session_id ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
\WP_CLI::add_command( 'pantheon session', '\Pantheon_Sessions\CLI_Command' );
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* List table for displaying all sessions.
|
||||
*
|
||||
* @package WPNPS
|
||||
*/
|
||||
|
||||
namespace Pantheon_Sessions;
|
||||
|
||||
/**
|
||||
* List table for displaying all sessions.
|
||||
*/
|
||||
class List_Table extends \WP_List_Table {
|
||||
|
||||
/**
|
||||
* Prepare the items for the list table
|
||||
*/
|
||||
public function prepare_items() {
|
||||
global $wpdb;
|
||||
|
||||
$columns = $this->get_columns();
|
||||
$hidden = [];
|
||||
$sortable = [];
|
||||
$this->_column_headers = [ $columns, $hidden, $sortable ];
|
||||
|
||||
$per_page = 20;
|
||||
$paged = ( isset( $_GET['paged'] ) ) ? (int) $_GET['paged'] : 1;
|
||||
$offset = $per_page * ( $paged - 1 );
|
||||
|
||||
$this->items = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->pantheon_sessions ORDER BY datetime DESC LIMIT %d,%d", $offset, $per_page ) );
|
||||
$total_items = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->pantheon_sessions" );
|
||||
|
||||
$this->set_pagination_args(
|
||||
[
|
||||
'total_items' => $total_items,
|
||||
'per_page' => $per_page,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Message for no items found
|
||||
*/
|
||||
public function no_items() {
|
||||
_e( 'No sessions found.', 'wp-native-php-sessions' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the columns in the list table
|
||||
*/
|
||||
public function get_columns() {
|
||||
return [
|
||||
'session_id' => __( 'Session ID', 'wp-native-php-sessions' ),
|
||||
'user_id' => __( 'User ID', 'wp-native-php-sessions' ),
|
||||
'ip_address' => __( 'IP Address', 'wp-native-php-sessions' ),
|
||||
'datetime' => __( 'Last Active', 'wp-native-php-sessions' ),
|
||||
'data' => __( 'Data', 'wp-native-php-sessions' ),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a column value
|
||||
*
|
||||
* @param object $item Session to display.
|
||||
* @param string $column_name Name of the column.
|
||||
*/
|
||||
public function column_default( $item, $column_name ) {
|
||||
if ( 'data' === $column_name ) {
|
||||
return '<code>' . esc_html( $item->data ) . '</code>';
|
||||
} elseif ( 'session_id' === $column_name ) {
|
||||
$query_args = [
|
||||
'action' => 'pantheon_clear_session',
|
||||
'nonce' => wp_create_nonce( 'pantheon_clear_session' ),
|
||||
'session' => $item->session_id,
|
||||
];
|
||||
$actions = [
|
||||
'clear' => '<a href="' . esc_url( add_query_arg( $query_args, admin_url( 'admin-ajax.php' ) ) ) . '">' . esc_html__( 'Clear', 'wp-native-php-sessions' ) . '</a>',
|
||||
];
|
||||
return esc_html( $item->session_id ) . $this->row_actions( $actions );
|
||||
} elseif ( 'datetime' === $column_name ) {
|
||||
// translators: Time ago.
|
||||
return esc_html( sprintf( esc_html__( '%s ago', 'wp-native-php-sessions' ), human_time_diff( strtotime( $item->datetime ) ) ) );
|
||||
} else {
|
||||
return esc_html( $item->$column_name );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
/**
|
||||
* Implementation of 'SessionHandlerInterface' that writes to the database.
|
||||
*
|
||||
* @package WPNPS
|
||||
*/
|
||||
|
||||
namespace Pantheon_Sessions;
|
||||
|
||||
use Pantheon_Sessions\Session;
|
||||
|
||||
/**
|
||||
* Implementation of 'SessionHandlerInterface' that writes to the database.
|
||||
*
|
||||
* @package WPNPS
|
||||
*/
|
||||
class Session_Handler implements \SessionHandlerInterface {
|
||||
|
||||
/**
|
||||
* Closes the session.
|
||||
*
|
||||
* @param string $save_path Path to where the session is to be stored.
|
||||
* @param string $session_name Name of the session.
|
||||
* @return boolean
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function open( $save_path, $session_name ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a session to the database.
|
||||
*
|
||||
* @param string $session_id Session id.
|
||||
* @param string $session_data Session data.
|
||||
* @return boolean
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function write( $session_id, $session_data ) {
|
||||
$session = Session::get_by_sid( $session_id );
|
||||
|
||||
if ( ! $session ) {
|
||||
$session = Session::create_for_sid( $session_id );
|
||||
}
|
||||
|
||||
if ( ! $session ) {
|
||||
trigger_error( 'Could not write session to the database. Please check MySQL configuration.', E_USER_WARNING );
|
||||
return false;
|
||||
}
|
||||
|
||||
$session->set_data( $session_data );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads session data from the database.
|
||||
*
|
||||
* @param string $session_id Session id.
|
||||
* @return string
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function read( $session_id ) {
|
||||
// Handle the case of first time visitors and clients that don't store
|
||||
// cookies (eg. web crawlers).
|
||||
$insecure_session_name = substr( session_name(), 1 );
|
||||
if ( empty( $session_id )
|
||||
|| ( ! isset( $_COOKIE[ session_name() ] ) && ! isset( $_COOKIE[ $insecure_session_name ] ) ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$session = Session::get_by_sid( $session_id );
|
||||
if ( $session ) {
|
||||
return $session->get_data() ?: '';
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the session.
|
||||
*
|
||||
* @param string $session_id Session id.
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function destroy( $session_id ) {
|
||||
$session = Session::get_by_sid( $session_id );
|
||||
if ( ! $session ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$session->destroy();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the garbage collection process.
|
||||
*
|
||||
* @param integer $maxlifetime Maximum lifetime in seconds.
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function gc( $maxlifetime ) {
|
||||
global $wpdb;
|
||||
|
||||
$wpdb = Session::restore_wpdb_if_null( $wpdb );
|
||||
|
||||
// Be sure to adjust 'php_value session.gc_maxlifetime' to a large enough
|
||||
// value. For example, if you want user sessions to stay in your database
|
||||
// for three weeks before deleting them, you need to set gc_maxlifetime
|
||||
// to '1814400'. At that value, only after a user doesn't log in after
|
||||
// three weeks (1814400 seconds) will his/her session be removed.
|
||||
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->pantheon_sessions WHERE `datetime` <= %s ", gmdate( 'Y-m-d H:i:s', time() - $maxlifetime ) ) );
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the session.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function close() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
<?php
|
||||
/**
|
||||
* Individual session object.
|
||||
*
|
||||
* @package WPNPS
|
||||
*/
|
||||
|
||||
namespace Pantheon_Sessions;
|
||||
|
||||
/**
|
||||
* Individual session object.
|
||||
*/
|
||||
class Session {
|
||||
|
||||
/**
|
||||
* Any sessions stored statically.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $sessions = [];
|
||||
|
||||
/**
|
||||
* Any secure sessions stored statically.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $secure_sessions = [];
|
||||
|
||||
/**
|
||||
* Session id.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $sid;
|
||||
|
||||
/**
|
||||
* Session data.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $data;
|
||||
|
||||
/**
|
||||
* Session user id.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
private $user_id;
|
||||
|
||||
/**
|
||||
* Get a session based on its ID.
|
||||
*
|
||||
* @param string $sid Session id.
|
||||
* @return Session|false
|
||||
*/
|
||||
public static function get_by_sid( $sid ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( ! $sid ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$wpdb = self::restore_wpdb_if_null( $wpdb );
|
||||
|
||||
$column_name = self::get_session_id_column();
|
||||
$table_name = $wpdb->pantheon_sessions;
|
||||
// phpcs:ignore
|
||||
$session_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE {$column_name}=%s", $sid ) );
|
||||
if ( ! $session_row ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return new Session( $session_row->$column_name, $session_row->data, $session_row->user_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a database entry for this session
|
||||
*
|
||||
* @param string $sid Session id.
|
||||
* @return Session
|
||||
*/
|
||||
public static function create_for_sid( $sid ) {
|
||||
global $wpdb;
|
||||
|
||||
$wpdb = self::restore_wpdb_if_null( $wpdb );
|
||||
|
||||
$insert_data = [
|
||||
'session_id' => $sid,
|
||||
'user_id' => (int) get_current_user_id(),
|
||||
];
|
||||
if ( function_exists( 'is_ssl' ) && is_ssl() ) {
|
||||
$insert_data['secure_session_id'] = $sid;
|
||||
}
|
||||
$wpdb->insert( $wpdb->pantheon_sessions, $insert_data );
|
||||
return self::get_by_sid( $sid );
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a session object.
|
||||
*
|
||||
* @param string $sid Session id.
|
||||
* @param mixed $data Any session data.
|
||||
* @param integer $user_id User id for the session.
|
||||
*/
|
||||
private function __construct( $sid, $data, $user_id ) {
|
||||
$this->sid = $sid;
|
||||
$this->data = $data;
|
||||
$this->user_id = $user_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get this session's ID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_id() {
|
||||
return $this->sid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get this session's data
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_data() {
|
||||
return maybe_unserialize( $this->data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get this session's user id.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function get_user_id() {
|
||||
return (int) $this->user_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user id for this session.
|
||||
*
|
||||
* @param integer $user_id User id.
|
||||
*/
|
||||
public function set_user_id( $user_id ) {
|
||||
global $wpdb;
|
||||
|
||||
$wpdb = self::restore_wpdb_if_null( $wpdb );
|
||||
|
||||
$this->user_id = (int) $user_id;
|
||||
$wpdb->update(
|
||||
$wpdb->pantheon_sessions,
|
||||
[
|
||||
'user_id' => $this->user_id,
|
||||
],
|
||||
[ self::get_session_id_column() => $this->get_id() ]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the session's data
|
||||
*
|
||||
* @param mixed $data Session data.
|
||||
*/
|
||||
public function set_data( $data ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( $data === $this->get_data() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! isset( $_SERVER['REMOTE_ADDR'] ) ) {
|
||||
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
||||
}
|
||||
|
||||
$wpdb = self::restore_wpdb_if_null( $wpdb );
|
||||
|
||||
$wpdb->update(
|
||||
$wpdb->pantheon_sessions,
|
||||
[
|
||||
'user_id' => (int) get_current_user_id(),
|
||||
'datetime' => gmdate( 'Y-m-d H:i:s' ),
|
||||
'ip_address' => self::get_client_ip_server(),
|
||||
'data' => maybe_serialize( $data ),
|
||||
],
|
||||
[ self::get_session_id_column() => $this->get_id() ]
|
||||
);
|
||||
|
||||
$this->data = maybe_serialize( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the clients ip address
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_client_ip_server() {
|
||||
// Set default.
|
||||
$ip_address = apply_filters( 'pantheon_sessions_client_ip_default', '127.0.0.1' );
|
||||
$ip_source = null;
|
||||
|
||||
$keys = [
|
||||
'HTTP_CLIENT_IP',
|
||||
'HTTP_X_FORWARDED_FOR',
|
||||
'HTTP_X_FORWARDED',
|
||||
'HTTP_FORWARDED_FOR',
|
||||
'HTTP_FORWARDED',
|
||||
'REMOTE_ADDR',
|
||||
];
|
||||
|
||||
$ip_filter_flags = apply_filters( 'pantheon_sessions_client_ip_filter_flags', FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE );
|
||||
|
||||
foreach ( $keys as $key ) {
|
||||
if ( array_key_exists( $key, $_SERVER )
|
||||
&& $_SERVER[ $key ]
|
||||
) {
|
||||
$_ip_address = $_SERVER[ $key ];
|
||||
|
||||
if ( false !== strpos( $_ip_address, ',' ) ) {
|
||||
$_ip_address = trim( strstr( $_ip_address, ',', true ) );
|
||||
}
|
||||
|
||||
if ( false === filter_var( $_ip_address, FILTER_VALIDATE_IP, $ip_filter_flags ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$ip_address = $_ip_address;
|
||||
$ip_source = $key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return apply_filters(
|
||||
'pantheon_sessions_client_ip',
|
||||
preg_replace( '/[^0-9a-fA-F:., ]/', '', $ip_address ),
|
||||
$ip_source
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy this session
|
||||
*/
|
||||
public function destroy() {
|
||||
global $wpdb;
|
||||
|
||||
$wpdb = self::restore_wpdb_if_null( $wpdb );
|
||||
|
||||
$wpdb->delete( $wpdb->pantheon_sessions, [ self::get_session_id_column() => $this->get_id() ] );
|
||||
|
||||
// Reset $_SESSION to prevent a new session from being started.
|
||||
$_SESSION = [];
|
||||
|
||||
$this->delete_cookies();
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores $wpdb database connection if missing.
|
||||
*
|
||||
* @param mixed $wpdb Existing global.
|
||||
* @return object
|
||||
*/
|
||||
public static function restore_wpdb_if_null( $wpdb ) {
|
||||
if ( $wpdb instanceof \wpdb ) {
|
||||
return $wpdb;
|
||||
}
|
||||
$dbuser = defined( 'DB_USER' ) ? DB_USER : '';
|
||||
$dbpassword = defined( 'DB_PASSWORD' ) ? DB_PASSWORD : '';
|
||||
$dbname = defined( 'DB_NAME' ) ? DB_NAME : '';
|
||||
$dbhost = defined( 'DB_HOST' ) ? DB_HOST : '';
|
||||
|
||||
return new \wpdb( $dbuser, $dbpassword, $dbname, $dbhost );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete session cookies
|
||||
*/
|
||||
private function delete_cookies() {
|
||||
|
||||
// Cookies don't exist on CLI.
|
||||
if ( self::is_cli() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$session_name = session_name();
|
||||
$cookies = [
|
||||
$session_name,
|
||||
substr( $session_name, 1 ),
|
||||
'S' . $session_name,
|
||||
];
|
||||
|
||||
foreach ( $cookies as $cookie_name ) {
|
||||
|
||||
if ( ! isset( $_COOKIE[ $cookie_name ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$params = session_get_cookie_params();
|
||||
setcookie( $cookie_name, '', $_SERVER['REQUEST_TIME'] - 3600, $params['path'], $params['domain'], $params['secure'], $params['httponly'] );
|
||||
unset( $_COOKIE[ $cookie_name ] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this request via CLI?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function is_cli() {
|
||||
return 'cli' === PHP_SAPI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the session ID column name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function get_session_id_column() {
|
||||
if ( is_ssl() ) {
|
||||
return 'secure_session_id';
|
||||
} else {
|
||||
return 'session_id';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
<?php
|
||||
/**
|
||||
* Plugin Name: Native PHP Sessions for WordPress
|
||||
* Version: 1.3.6
|
||||
* Description: Offload PHP's native sessions to your database for multi-server compatibility.
|
||||
* Author: Pantheon
|
||||
* Author URI: https://www.pantheon.io/
|
||||
* Plugin URI: https://wordpress.org/plugins/wp-native-php-sessions/
|
||||
* Text Domain: wp-native-php-sessions
|
||||
*
|
||||
* @package WPNPS
|
||||
**/
|
||||
|
||||
use Pantheon_Sessions\Session;
|
||||
|
||||
define( 'PANTHEON_SESSIONS_VERSION', '1.3.6' );
|
||||
|
||||
/**
|
||||
* Main controller class for the plugin.
|
||||
*/
|
||||
class Pantheon_Sessions {
|
||||
|
||||
/**
|
||||
* Copy of the singleton instance.
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
private static $instance;
|
||||
|
||||
/**
|
||||
* The admin instance.
|
||||
*
|
||||
* @var \Pantheon_Sessions\Admin
|
||||
*/
|
||||
private $admin;
|
||||
|
||||
/**
|
||||
* Gets a copy of the singleton instance.
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public static function get_instance() {
|
||||
|
||||
if ( ! isset( self::$instance ) ) {
|
||||
self::$instance = new Pantheon_Sessions();
|
||||
self::$instance->load();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the plugin
|
||||
*/
|
||||
private function load() {
|
||||
|
||||
if ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->define_constants();
|
||||
$this->require_files();
|
||||
|
||||
if ( PANTHEON_SESSIONS_ENABLED ) {
|
||||
|
||||
$this->setup_database();
|
||||
$this->initialize_session_override();
|
||||
$this->set_ini_values();
|
||||
add_action( 'set_logged_in_cookie', [ __CLASS__, 'action_set_logged_in_cookie' ], 10, 4 );
|
||||
add_action( 'clear_auth_cookie', [ __CLASS__, 'action_clear_auth_cookie' ] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Define our constants
|
||||
*/
|
||||
private function define_constants() {
|
||||
|
||||
if ( ! defined( 'PANTHEON_SESSIONS_ENABLED' ) ) {
|
||||
define( 'PANTHEON_SESSIONS_ENABLED', 1 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load required files
|
||||
*/
|
||||
private function require_files() {
|
||||
|
||||
if ( defined( 'WP_CLI' ) && WP_CLI ) {
|
||||
require_once __DIR__ . '/inc/class-cli-command.php';
|
||||
}
|
||||
|
||||
if ( is_admin() ) {
|
||||
require_once __DIR__ . '/inc/class-admin.php';
|
||||
$this->admin = Pantheon_Sessions\Admin::get_instance();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the PHP ini settings for the session implementation to work properly
|
||||
*
|
||||
* Largely adopted from Drupal 7's implementation
|
||||
*/
|
||||
private function set_ini_values() {
|
||||
|
||||
if ( headers_sent() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the user specifies the cookie domain, also use it for session name.
|
||||
if ( defined( 'COOKIE_DOMAIN' ) && constant( 'COOKIE_DOMAIN' ) ) {
|
||||
$cookie_domain = constant( 'COOKIE_DOMAIN' );
|
||||
$session_name = $cookie_domain;
|
||||
} else {
|
||||
$session_name = parse_url( home_url(), PHP_URL_HOST );
|
||||
$cookie_domain = ltrim( $session_name, '.' );
|
||||
// Strip leading periods, www., and port numbers from cookie domain.
|
||||
if ( strpos( $cookie_domain, 'www.' ) === 0 ) {
|
||||
$cookie_domain = substr( $cookie_domain, 4 );
|
||||
}
|
||||
$cookie_domain = explode( ':', $cookie_domain );
|
||||
$cookie_domain = '.' . $cookie_domain[0];
|
||||
}
|
||||
|
||||
// Per RFC 2109, cookie domains must contain at least one dot other than the
|
||||
// first. For hosts such as 'localhost' or IP Addresses we don't set a cookie domain.
|
||||
if ( count( explode( '.', $cookie_domain ) ) > 2 && ! is_numeric( str_replace( '.', '', $cookie_domain ) ) ) {
|
||||
ini_set( 'session.cookie_domain', $cookie_domain );
|
||||
}
|
||||
// To prevent session cookies from being hijacked, a user can configure the
|
||||
// SSL version of their website to only transfer session cookies via SSL by
|
||||
// using PHP's session.cookie_secure setting. The browser will then use two
|
||||
// separate session cookies for the HTTPS and HTTP versions of the site. So we
|
||||
// must use different session identifiers for HTTPS and HTTP to prevent a
|
||||
// cookie collision.
|
||||
if ( is_ssl() ) {
|
||||
ini_set( 'session.cookie_secure', true );
|
||||
}
|
||||
$prefix = ini_get( 'session.cookie_secure' ) ? 'SSESS' : 'SESS';
|
||||
|
||||
session_name( $prefix . substr( hash( 'sha256', $session_name ), 0, 32 ) );
|
||||
|
||||
// Use session cookies, not transparent sessions that puts the session id in
|
||||
// the query string.
|
||||
$use_cookies = '1';
|
||||
if ( defined( 'WP_CLI' ) && WP_CLI ) {
|
||||
$use_cookies = '0';
|
||||
}
|
||||
ini_set( 'session.use_cookies', $use_cookies );
|
||||
ini_set( 'session.use_only_cookies', '1' );
|
||||
ini_set( 'session.use_trans_sid', '0' );
|
||||
// Don't send HTTP headers using PHP's session handler.
|
||||
// An empty string is used here to disable the cache limiter.
|
||||
ini_set( 'session.cache_limiter', '' );
|
||||
// Use httponly session cookies. Limits use by JavaScripts.
|
||||
ini_set( 'session.cookie_httponly', '1' );
|
||||
// Get cookie lifetime from filters so you can put your custom lifetime.
|
||||
ini_set( 'session.cookie_lifetime', (int) apply_filters( 'pantheon_session_expiration', 0 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the default sessions implementation with our own
|
||||
*
|
||||
* Largely adopted from Drupal 7's implementation
|
||||
*/
|
||||
private function initialize_session_override() {
|
||||
require_once __DIR__ . '/inc/class-session.php';
|
||||
require_once __DIR__ . '/inc/class-session-handler.php';
|
||||
$session_handler = new Pantheon_Sessions\Session_Handler();
|
||||
if ( PHP_SESSION_ACTIVE !== session_status() ) {
|
||||
// Check if headers have already been sent.
|
||||
if ( headers_sent( $file, $line ) ) {
|
||||
// Output a friendly error message if headers are already sent.
|
||||
trigger_error(
|
||||
sprintf(
|
||||
/* translators: %1s: File path, %2d: Line number */
|
||||
__( "Oops! The wp-native-php-sessions plugin couldn't start the session because output has already been sent. This might be caused by PHP throwing errors. Please check the code in %1s on line %2d.", 'wp-native-php-sessions' ),
|
||||
$file,
|
||||
$line
|
||||
),
|
||||
E_USER_WARNING
|
||||
);
|
||||
} else {
|
||||
session_set_save_handler( $session_handler, false );
|
||||
}
|
||||
}
|
||||
// Close the session before $wpdb destructs itself.
|
||||
add_action( 'shutdown', 'session_write_close', 999, 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up the database
|
||||
*/
|
||||
private function setup_database() {
|
||||
global $wpdb, $table_prefix;
|
||||
|
||||
$table_name = "{$table_prefix}pantheon_sessions";
|
||||
$wpdb->pantheon_sessions = $table_name;
|
||||
$wpdb->tables[] = 'pantheon_sessions';
|
||||
|
||||
if ( get_option( 'pantheon_session_version' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$create_statement = "CREATE TABLE IF NOT EXISTS `{$table_name}` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'An auto-incrementing id to serve as an index.',
|
||||
`user_id` bigint(20) unsigned NOT NULL COMMENT 'The user_id corresponding to a session, or 0 for anonymous user.',
|
||||
`session_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'A session ID. The value is generated by plugin''s session handlers.',
|
||||
`secure_session_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Secure session ID. The value is generated by plugin''s session handlers.',
|
||||
`ip_address` varchar(128) NOT NULL DEFAULT '' COMMENT 'The IP address that last used this session ID.',
|
||||
`datetime` datetime DEFAULT NULL COMMENT 'The datetime value when this session last requested a page. Old records are purged by PHP automatically.',
|
||||
`data` mediumblob COMMENT 'The serialized contents of \$_SESSION, an array of name/value pairs that persists across page requests by this session ID. Plugin loads \$_SESSION from here at the start of each request and saves it at the end.',
|
||||
KEY `session_id` (`session_id`),
|
||||
KEY `secure_session_id` (`secure_session_id`)
|
||||
)";
|
||||
// phpcs:ignore
|
||||
$wpdb->query( $create_statement );
|
||||
update_option( 'pantheon_session_version', PANTHEON_SESSIONS_VERSION );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the user id value to the session when the user logs in.
|
||||
*
|
||||
* @param string $logged_in_cookie Cooke name.
|
||||
* @param integer $expire When the cookie is set to expire.
|
||||
* @param integer $expiration When the cookie is set to expire.
|
||||
* @param integer $user_id Id for the logged-in user.
|
||||
*/
|
||||
public static function action_set_logged_in_cookie( $logged_in_cookie, $expire, $expiration, $user_id ) {
|
||||
$session = Session::get_by_sid( session_id() );
|
||||
if ( $session ) {
|
||||
$session->set_user_id( $user_id );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the user id value from the session when the user logs out.
|
||||
*/
|
||||
public static function action_clear_auth_cookie() {
|
||||
$session = Session::get_by_sid( session_id() );
|
||||
if ( $session ) {
|
||||
$session->set_user_id( 0 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Force the plugin to be the first loaded
|
||||
*/
|
||||
public static function force_first_load() {
|
||||
$path = str_replace( WP_PLUGIN_DIR . '/', '', __FILE__ );
|
||||
$plugins = get_option( 'active_plugins' );
|
||||
if ( $plugins ) {
|
||||
$key = array_search( $path, $plugins, true );
|
||||
if ( $key ) {
|
||||
array_splice( $plugins, $key, 1 );
|
||||
array_unshift( $plugins, $path );
|
||||
update_option( 'active_plugins', $plugins );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Release the kraken!
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
function Pantheon_Sessions() {
|
||||
return Pantheon_Sessions::get_instance();
|
||||
}
|
||||
|
||||
add_action( 'activated_plugin', 'Pantheon_Sessions::force_first_load' );
|
||||
|
||||
Pantheon_Sessions();
|
||||
184
wp/wp-content/plugins/wp-native-php-sessions/readme.txt
Normal file
184
wp/wp-content/plugins/wp-native-php-sessions/readme.txt
Normal file
@@ -0,0 +1,184 @@
|
||||
=== WordPress Native PHP Sessions ===
|
||||
Contributors: getpantheon, outlandish josh, mpvanwinkle77, danielbachhuber, andrew.taylor, jazzs3quence, stovak, jspellman
|
||||
Tags: comments, sessions
|
||||
Requires at least: 4.7
|
||||
Tested up to: 6.2.2
|
||||
Stable tag: 1.3.6
|
||||
Requires PHP: 5.4
|
||||
License: GPLv2 or later
|
||||
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
|
||||
Use native PHP sessions and stay horizontally scalable. Better living through superior technology.
|
||||
|
||||
== Description ==
|
||||
|
||||
[](https://travis-ci.org/pantheon-systems/wp-native-php-sessions) [](https://circleci.com/gh/pantheon-systems/wp-native-php-sessions/tree/master)
|
||||
|
||||
WordPress core does not use PHP sessions, but sometimes they are required by your use-case, a plugin or theme.
|
||||
|
||||
This plugin implements PHP's native session handlers, backed by the WordPress database. This allows plugins, themes, and custom code to safely use PHP `$_SESSION`s in a distributed environment where PHP's default tempfile storage just won't work.
|
||||
|
||||
Note that primary development is on GitHub if you would like to contribute:
|
||||
|
||||
https://github.com/pantheon-systems/wp-native-php-sessions
|
||||
|
||||
== Installation ==
|
||||
|
||||
1. Upload to the `/wp-content/plugins/` directory
|
||||
2. Activate the plugin through the 'Plugins' menu in WordPress
|
||||
|
||||
That's it!
|
||||
|
||||
== Configuration ==
|
||||
|
||||
By default the session lifetime is set to 0, which is until the browser is closed.
|
||||
|
||||
To override this use the `pantheon_session_expiration` filter before the WordPress Native PHP Sessions plugin is loaded. For example a small Must-use plugin (a.k.a. mu-plugin) could contain:
|
||||
|
||||
<?php
|
||||
function my_session_expiration_override() {
|
||||
return 60*60*4; // 4 hours
|
||||
}
|
||||
add_filter( 'pantheon_session_expiration', 'my_session_expiration_override' );
|
||||
|
||||
== Contributing ==
|
||||
|
||||
See [CONTRIBUTING.md](https://github.com/pantheon-systems/wp-native-php-sessions/blob/main/CONTRIBUTING.md) for information on contributing.
|
||||
|
||||
== Frequently Asked Questions ==
|
||||
|
||||
= Why not use another session plugin? =
|
||||
|
||||
This implements the built-in PHP session handling functions, rather than introducing anything custom. That way you can use built-in language functions like the `$_SESSION` superglobal and `session_start()` in your code. Everything else will "just work".
|
||||
|
||||
= Why store them in the database? =
|
||||
|
||||
PHP's fallback default functionality is to allow sessions to be stored in a temporary file. This is what most code that invokes sessions uses by default, and in simple use-cases it works, which is why so many plugins do it.
|
||||
|
||||
However, if you intend to scale your application, local tempfiles are a dangerous choice. They are not shared between different instances of the application, producing erratic behavior that can be impossible to debug. By storing them in the database the state of the sessions is shared across all application instances.
|
||||
|
||||
== Troubleshooting ==
|
||||
|
||||
If you see an error like "Fatal error: session_start(): Failed to initialize storage module:" or "Warning: ini_set(): A session is active.", then you likely have a plugin that is starting a session before WP Native PHP Sessions is loading.
|
||||
|
||||
To fix, create a new file at `wp-content/mu-plugins/000-loader.php` and include the following:
|
||||
|
||||
<?php
|
||||
require_once WP_PLUGIN_DIR . '/wp-native-php-sessions/pantheon-sessions.php';
|
||||
|
||||
This mu-plugin will load WP Native PHP Sessions before all other plugins, while letting you still use the WordPress plugin updater to keep the plugin up-to-date.
|
||||
|
||||
== Changelog ==
|
||||
|
||||
= 1.3.6 (June 1, 2023) =
|
||||
* Fixes PHP 8.2 deprecated dynamic property error [[#251](https://github.com/pantheon-systems/wp-native-php-sessions/pull/251)] (props @miguelaxcar)
|
||||
* Update CONTRIBUTING.md [[#252](https://github.com/pantheon-systems/wp-native-php-sessions/pull/252)].
|
||||
* Update informational Error message for the case of headers already sent [[#249](https://github.com/pantheon-systems/wp-native-php-sessions/pull/249)].
|
||||
* Add pantheon-wp-coding-standards [[#247](https://github.com/pantheon-systems/wp-native-php-sessions/pull/247)].
|
||||
|
||||
= 1.3.5 (April 7, 2023) =
|
||||
* Bump yoast/phpunit-polyfills from 1.0.4 to 1.0.5 [[#245](https://github.com/pantheon-systems/wp-native-php-sessions/pull/245)].
|
||||
* Bump tested up to version
|
||||
|
||||
= 1.3.4 (February 7, 2023) =
|
||||
* Add fallback for $session->get_data() [[#237(https://github.com/pantheon-systems/wp-native-php-sessions/pull/237)]] (reported [on WordPress.org](https://wordpress.org/support/topic/php-warning-session_start-failed-to-read-session-data-user/))
|
||||
* Update CODEOWNERS file [[#239](https://github.com/pantheon-systems/wp-native-php-sessions/pull/239)]
|
||||
* Fix GPL license in `composer.json` file [[#236](https://github.com/pantheon-systems/wp-native-php-sessions/pull/236)]
|
||||
* Bump grunt from 1.5.3 to 1.6.1 [[#235](https://github.com/pantheon-systems/wp-native-php-sessions/pull/235)]
|
||||
|
||||
= 1.3.3 (January 25, 2023) =
|
||||
* Bump version in pantheon-sessions.php [[#234](https://github.com/pantheon-systems/wp-native-php-sessions/pull/234)].
|
||||
|
||||
= 1.3.2 (January 25, 2023) =
|
||||
* PHP 8.2 compatibility [[#232](https://github.com/pantheon-systems/wp-native-php-sessions/pull/232)].
|
||||
* Bump dealerdirect/phpcodesniffer-composer-installer from 0.7.2 to 1.0.0 [[#229](https://github.com/pantheon-systems/wp-native-php-sessions/pull/229)].
|
||||
* Update images for lint and test-behat jobs [[#228](https://github.com/pantheon-systems/wp-native-php-sessions/pull/228)].
|
||||
|
||||
= 1.3.1 (December 5, 2022) =
|
||||
* Document session lifetime handling [[#224](https://github.com/pantheon-systems/wp-native-php-sessions/pull/224)].
|
||||
* Make dependabot target develop branch [[#226](https://github.com/pantheon-systems/wp-native-php-sessions/pull/226)].
|
||||
* Ignore `.wordpress-org` directory [[#223](https://github.com/pantheon-systems/wp-native-php-sessions/pull/223)].
|
||||
|
||||
= 1.3.0 (November 28th, 2022) =
|
||||
* Added CONTRIBUTING.MD and GitHub action to automate deployments to wp.org. [[#219](https://github.com/pantheon-systems/wp-native-php-sessions/pull/219)]
|
||||
|
||||
= 1.2.5 (October 28, 2022) =
|
||||
* Added `#[ReturnTypeWillChange]` where required to silence deprecation warnings in PHP 8.1. [[#216](https://github.com/pantheon-systems/wp-native-php-sessions/pull/216)]
|
||||
|
||||
= 1.2.4 (September 14th, 2021) =
|
||||
* Increases data blob size from 64k to 16M for new session tables; existing tables will need to manually modify the column if they want to apply this change [[#193](https://github.com/pantheon-systems/wp-native-php-sessions/pull/193)].
|
||||
|
||||
= 1.2.3 (April 9th, 2021) =
|
||||
* Assigns the table name to a variable before using in query [[#188](https://github.com/pantheon-systems/wp-native-php-sessions/pull/188)].
|
||||
|
||||
= 1.2.2 (March 29th, 2021) =
|
||||
* Includes an auto-incrementing `id` column for replication support [[#187](https://github.com/pantheon-systems/wp-native-php-sessions/pull/187)].
|
||||
|
||||
= 1.2.1 (September 17th, 2020) =
|
||||
* Plugin textdomain needs to be the same as the WordPress.org slug [[#169](https://github.com/pantheon-systems/wp-native-php-sessions/pull/169)].
|
||||
|
||||
= 1.2.0 (May 18th, 2020) =
|
||||
* Avoids using cookies for sessions when WP-CLI is executing [[#154](https://github.com/pantheon-systems/wp-native-php-sessions/pull/154)].
|
||||
|
||||
= 1.1.0 (April 23rd, 2020) =
|
||||
* Avoids initializing PHP sessions when doing cron [[#149](https://github.com/pantheon-systems/wp-native-php-sessions/pull/149)].
|
||||
|
||||
= 1.0.0 (March 2nd, 2020) =
|
||||
* Plugin is stable.
|
||||
|
||||
= 0.9.0 (October 14th, 2019) =
|
||||
* Refactors session callback logic into `Session_Handler` abstraction, fixing PHP notice in PHP 7.3 [[#135](https://github.com/pantheon-systems/wp-native-php-sessions/pull/135)].
|
||||
|
||||
= 0.8.1 (August 19th, 2019) =
|
||||
* Fixes handling of 'X-Forwarded-For' header in `get_client_ip_server()` [[#126](https://github.com/pantheon-systems/wp-native-php-sessions/pull/126)].
|
||||
|
||||
= 0.8.0 (August 13th, 2019) =
|
||||
* Respects various `HTTP_*` sources for client IP address [[#122](https://github.com/pantheon-systems/wp-native-php-sessions/pull/122)].
|
||||
|
||||
= 0.7.0 (April 3rd, 2019) =
|
||||
* Adds a safety check that restores `$wpdb` when it's missing.
|
||||
|
||||
= 0.6.9 (May 15th, 2018) =
|
||||
* Ensures `_pantheon_session_destroy()` uses a return value.
|
||||
|
||||
= 0.6.8 (May 4th, 2018) =
|
||||
* Switches to `E_USER_WARNING` instead of `E_WARNING` when triggering errors.
|
||||
|
||||
= 0.6.7 (April 26th, 2018) =
|
||||
* Disables plugin load when `WP_INSTALLING`, because session table creation breaks installation process.
|
||||
|
||||
= 0.6.6 (March 8th, 2018) =
|
||||
* Restores session instantiation when WP-CLI is executing, because not doing so causes other problems.
|
||||
|
||||
= 0.6.5 (February 6th, 2018) =
|
||||
* Disables session instantiation when `defined( 'WP_CLI' ) && WP_CLI` because sessions don't work on CLI.
|
||||
|
||||
= 0.6.4 (October 10th, 2017) =
|
||||
* Triggers PHP error when plugin fails to write session to database.
|
||||
|
||||
= 0.6.3 (September 29th, 2017) =
|
||||
* Returns false when we entirely fail to generate a session.
|
||||
|
||||
= 0.6.2 (June 6th, 2017) =
|
||||
* Syncs session user id when a user logs in and logs out.
|
||||
|
||||
= 0.6.1 (May 25th, 2017) =
|
||||
* Bug fix: Prevents warning session_write_close() expects exactly 0 parameters, 1 given.
|
||||
|
||||
= 0.6.0 (November 23rd, 2016) =
|
||||
* Bug fix: Prevents PHP fatal error in `session_write_close()` by running on WordPress' `shutdown` action, before `$wpdb` destructs itself.
|
||||
* Bug fix: Stores the actual user id in the sessions table, instead of `(bool) $user_id`.
|
||||
|
||||
= 0.5 =
|
||||
* Compatibility with PHP 7.
|
||||
* Adds `pantheon_session_expiration` filter to modify session expiration value.
|
||||
|
||||
= 0.4 =
|
||||
* Adjustment to `session_id()` behavior for wider compatibility
|
||||
* Using superglobal for REQUEST_TIME as opposed to `time()`
|
||||
|
||||
= 0.3 =
|
||||
* Fixes issue related to WordPress plugin load order
|
||||
|
||||
= 0.1 =
|
||||
* Initial release
|
||||
Reference in New Issue
Block a user