├── .gitignore ├── LICENSE ├── README.txt ├── admin ├── assets │ ├── css │ │ └── index.php │ └── js │ │ └── index.php ├── class-pt-wp-discourse-sso-admin.php └── includes │ ├── class.settings-api.php │ ├── class.settings.php │ └── index.php ├── composer.json ├── includes └── index.php ├── index.php ├── pt-wp-discourse-sso.php ├── public ├── assets │ ├── css │ │ └── index.php │ ├── index.php │ └── js │ │ └── index.php ├── class-pt-wp-discourse-sso.php └── includes │ ├── helpers.php │ └── index.php └── uninstall.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | === PrimeTime WordPress + Discourse SSO === 2 | Contributors: etcio, nphaskins, dphrag 3 | Tags: discourse, forum, sso 4 | Requires at least: 3.6 5 | Tested up to: 4.3.1 6 | Stable tag: 0.2.4 7 | License: GPLv2 or later 8 | License URI: http://www.gnu.org/licenses/gpl-2.0.html 9 | 10 | This plugin provides single sign-on capabilities for Discourse using WordPress user authentication. 11 | 12 | == Description == 13 | 14 | Discourse is a fantastic new forum that can add another layer to your WordPress community. This plugin allows you to create a fluid experience by using your WordPress installation as the authentication server, creating a single-sign-on (SSO) for your users! 15 | 16 | Notes: 17 | 18 | * The Discourse option "Staff must approve all new user accounts before they are allowed to access the site." needs to be disabled for this to work properly. 19 | 20 | Some Features: 21 | 22 | * Seamless integration into almost any WordPress installation. 23 | * Get setup within minutes through 3 easy steps. Anyone can do it. 24 | * Additional integration with MemberMouse. 25 | 26 | Coming Soon: 27 | 28 | * Only allow access with certain capabilities or roles. 29 | 30 | == Installation == 31 | 32 | There are only three steps to configuring your WP + Discourse SSO! 33 | 34 | = 1. Install this plugin and activate it = 35 | 36 | = Uploading in WordPress Dashboard = 37 | 38 | 1. Navigate to the 'Add New' in the plugins dashboard 39 | 2. Navigate to the 'Upload' area 40 | 3. Select `plugin-name.zip` from your computer 41 | 4. Click 'Install Now' 42 | 5. Activate the plugin in the Plugin dashboard 43 | 44 | = Using FTP = 45 | 46 | 1. Download `pt-wp-discourse-sso.zip` 47 | 2. Extract the `pt-wp-discourse-sso` directory to your computer 48 | 3. Upload the `pt-wp-discourse-sso` directory to the `/wp-content/plugins/` directory 49 | 4. Activate the plugin in the Plugin dashboard 50 | 51 | = 2. Configure the plugin = 52 | 53 | Under settings, you'll see the SSO configuration page. Add the url to your Discourse community forum. Generate a secret key that will be used to secure the authentication between the installations and paste it here. 54 | 55 | = 3. Enabling SSO on Discourse = 56 | 57 | In your Discourse admin settings, find the settings labeled, enable_sso, sso_url and sso_secret. Enter the URL of your WP installation, as well as the secret key, and enable sso. 58 | 59 | More information about these settings can be found here: 60 | https://meta.discourse.org/t/official-single-sign-on-for-discourse/13045 61 | 62 | That's it! 63 | 64 | == Frequently Asked Questions == 65 | 66 | = Where can I get a Discourse forum? = 67 | 68 | * https://discourse.org 69 | * https://www.discoursehosting.com/ 70 | 71 | == Screenshots == 72 | 73 | 1. The plugin settings screen. Here you enter your secret key as well as the url to your discourse site. 74 | 2. Custom SSO page can be assigned with any theme. 75 | 3. Enjoy single sign on between WP + Discourse. Cool! 76 | 77 | == Credits == 78 | 79 | * Request processing adapted from Adam Capirola : https://gist.github.com/adamcapriola/11300529 80 | * SSO methods adapted from ArmedGuy : https://github.com/ArmedGuy/discourse_sso_php 81 | 82 | == Changelog == 83 | 84 | = 0.2.4 = 85 | * Added basic support for MemberMouse 86 | * Added option to synchronize logouts, if user logging out from WP site. 87 | 88 | = 0.2.3 = 89 | * Send avatars to discourse (Thanks @pjv https://github.com/PrimeTimeCode/pt-wp-discourse-sso/pull/1) 90 | * Additional sanitization (Thanks @pratyushmittal https://github.com/PrimeTimeCode/pt-wp-discourse-sso/pull/2) 91 | 92 | = 0.2.2 = 93 | * Better compatibility with PHP 5.2 94 | 95 | = 0.2.1 = 96 | * More user friendly configuration 97 | * Fields a bit more forgiving 98 | 99 | = 0.2 = 100 | * Refactoring away from template based system 101 | 102 | = 0.1 = 103 | * Initial release -------------------------------------------------------------------------------- /admin/assets/css/index.php: -------------------------------------------------------------------------------- 1 | 7 | * @license GPL-2.0+ 8 | * @link http://example.com 9 | * @copyright 2014 Your Name or Company Name 10 | */ 11 | 12 | class WP_Discourse_SSO_Admin { 13 | 14 | /** 15 | * Instance of this class. 16 | * 17 | * @since 0.1 18 | * 19 | * @var object 20 | */ 21 | protected static $instance = null; 22 | 23 | /** 24 | * Slug of the plugin screen. 25 | * 26 | * @since 0.1 27 | * 28 | * @var string 29 | */ 30 | protected $plugin_screen_hook_suffix = null; 31 | 32 | /** 33 | * Initialize the plugin by loading admin scripts & styles and adding a 34 | * settings page and menu. 35 | * 36 | * @since 0.1 37 | */ 38 | private function __construct() { 39 | 40 | $plugin = WP_Discourse_SSO::get_instance(); 41 | $this->plugin_slug = $plugin->get_plugin_slug(); 42 | 43 | require_once(PT_WP_DISCOURSE_SSO_DIR.'admin/includes/class.settings.php'); 44 | 45 | } 46 | 47 | /** 48 | * Return an instance of this class. 49 | * 50 | * @since 0.1 51 | * 52 | * @return object A single instance of this class. 53 | */ 54 | public static function get_instance() { 55 | 56 | // If the single instance hasn't been set, set it now. 57 | if ( null == self::$instance ) { 58 | self::$instance = new self; 59 | } 60 | 61 | return self::$instance; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /admin/includes/class.settings-api.php: -------------------------------------------------------------------------------- 1 | 8 | * @link http://tareq.weDevs.com Tareq's Planet 9 | * @example settings-api.php How to use the class 10 | */ 11 | if ( !class_exists( 'WeDevs_Settings_API' ) ): 12 | class WeDevs_Settings_API { 13 | /** 14 | * settings sections array 15 | * 16 | * @var array 17 | */ 18 | private $settings_sections = array(); 19 | /** 20 | * Settings fields array 21 | * 22 | * @var array 23 | */ 24 | private $settings_fields = array(); 25 | /** 26 | * Singleton instance 27 | * 28 | * @var object 29 | */ 30 | private static $_instance; 31 | public function __construct() { 32 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); 33 | } 34 | /** 35 | * Enqueue scripts and styles 36 | */ 37 | function admin_enqueue_scripts() { 38 | wp_enqueue_style( 'wp-color-picker' ); 39 | wp_enqueue_media(); 40 | wp_enqueue_script( 'wp-color-picker' ); 41 | wp_enqueue_script( 'jquery' ); 42 | } 43 | /** 44 | * Set settings sections 45 | * 46 | * @param array $sections setting sections array 47 | */ 48 | function set_sections( $sections ) { 49 | $this->settings_sections = $sections; 50 | return $this; 51 | } 52 | /** 53 | * Add a single section 54 | * 55 | * @param array $section 56 | */ 57 | function add_section( $section ) { 58 | $this->settings_sections[] = $section; 59 | return $this; 60 | } 61 | /** 62 | * Set settings fields 63 | * 64 | * @param array $fields settings fields array 65 | */ 66 | function set_fields( $fields ) { 67 | $this->settings_fields = $fields; 68 | return $this; 69 | } 70 | function add_field( $section, $field ) { 71 | $defaults = array( 72 | 'name' => '', 73 | 'label' => '', 74 | 'desc' => '', 75 | 'type' => 'text' 76 | ); 77 | $arg = wp_parse_args( $field, $defaults ); 78 | $this->settings_fields[$section][] = $arg; 79 | return $this; 80 | } 81 | /** 82 | * Initialize and registers the settings sections and fileds to WordPress 83 | * 84 | * Usually this should be called at `admin_init` hook. 85 | * 86 | * This function gets the initiated settings sections and fields. Then 87 | * registers them to WordPress and ready for use. 88 | */ 89 | function admin_init() { 90 | //register settings sections 91 | foreach ( $this->settings_sections as $section ) { 92 | if ( false == get_option( $section['id'] ) ) { 93 | add_option( $section['id'] ); 94 | } 95 | if ( isset($section['desc']) && !empty($section['desc']) ) { 96 | $section['desc'] = '
'.$section['desc'].'
'; 97 | $callback = create_function('', 'echo "'.str_replace('"', '\"', $section['desc']).'";'); 98 | } else { 99 | $callback = '__return_false'; 100 | } 101 | add_settings_section( $section['id'], $section['title'], $callback, $section['id'] ); 102 | } 103 | //register settings fields 104 | foreach ( $this->settings_fields as $section => $field ) { 105 | foreach ( $field as $option ) { 106 | $type = isset( $option['type'] ) ? $option['type'] : 'text'; 107 | $args = array( 108 | 'id' => $option['name'], 109 | 'desc' => isset( $option['desc'] ) ? $option['desc'] : '', 110 | 'name' => $option['label'], 111 | 'section' => $section, 112 | 'size' => isset( $option['size'] ) ? $option['size'] : null, 113 | 'options' => isset( $option['options'] ) ? $option['options'] : '', 114 | 'std' => isset( $option['default'] ) ? $option['default'] : '', 115 | 'sanitize_callback' => isset( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '', 116 | ); 117 | add_settings_field( $section . '[' . $option['name'] . ']', $option['label'], array( $this, 'callback_' . $type ), $section, $section, $args ); 118 | } 119 | } 120 | // creates our settings in the options table 121 | foreach ( $this->settings_sections as $section ) { 122 | register_setting( $section['id'], $section['id'], array( $this, 'sanitize_options' ) ); 123 | } 124 | } 125 | /** 126 | * Displays a text field for a settings field 127 | * 128 | * @param array $args settings field args 129 | */ 130 | function callback_text( $args ) { 131 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); 132 | $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular'; 133 | $html = sprintf( '', $size, $args['section'], $args['id'], $value ); 134 | $html .= sprintf( ' %s', $args['desc'] ); 135 | echo $html; 136 | } 137 | /** 138 | * Displays a checkbox for a settings field 139 | * 140 | * @param array $args settings field args 141 | */ 142 | function callback_checkbox( $args ) { 143 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); 144 | $html = sprintf( '', $args['section'], $args['id'] ); 145 | $html .= sprintf( '', $args['section'], $args['id'], $value, checked( $value, 'on', false ) ); 146 | $html .= sprintf( '', $args['section'], $args['id'], $args['desc'] ); 147 | echo $html; 148 | } 149 | /** 150 | * Displays a multicheckbox a settings field 151 | * 152 | * @param array $args settings field args 153 | */ 154 | function callback_multicheck( $args ) { 155 | $value = $this->get_option( $args['id'], $args['section'], $args['std'] ); 156 | $html = ''; 157 | foreach ( $args['options'] as $key => $label ) { 158 | $checked = isset( $value[$key] ) ? $value[$key] : '0'; 159 | $html .= sprintf( '', $args['section'], $args['id'], $key, checked( $checked, $key, false ) ); 160 | $html .= sprintf( '
', $args['section'], $args['id'], $label, $key ); 161 | } 162 | $html .= sprintf( ' %s', $args['desc'] ); 163 | echo $html; 164 | } 165 | /** 166 | * Displays a multicheckbox a settings field 167 | * 168 | * @param array $args settings field args 169 | */ 170 | function callback_radio( $args ) { 171 | $value = $this->get_option( $args['id'], $args['section'], $args['std'] ); 172 | $html = ''; 173 | foreach ( $args['options'] as $key => $label ) { 174 | $html .= sprintf( '', $args['section'], $args['id'], $key, checked( $value, $key, false ) ); 175 | $html .= sprintf( '
', $args['section'], $args['id'], $label, $key ); 176 | } 177 | $html .= sprintf( ' %s', $args['desc'] ); 178 | echo $html; 179 | } 180 | /** 181 | * Displays a selectbox for a settings field 182 | * 183 | * @param array $args settings field args 184 | */ 185 | function callback_select( $args ) { 186 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); 187 | $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular'; 188 | $html = sprintf( '' ); 193 | $html .= sprintf( ' %s', $args['desc'] ); 194 | echo $html; 195 | } 196 | /** 197 | * Displays a textarea for a settings field 198 | * 199 | * @param array $args settings field args 200 | */ 201 | function callback_textarea( $args ) { 202 | $value = esc_textarea( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); 203 | $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular'; 204 | $html = sprintf( '', $size, $args['section'], $args['id'], $value ); 205 | $html .= sprintf( '
%s', $args['desc'] ); 206 | echo $html; 207 | } 208 | /** 209 | * Displays a textarea for a settings field 210 | * 211 | * @param array $args settings field args 212 | */ 213 | function callback_html( $args ) { 214 | echo $args['desc']; 215 | } 216 | /** 217 | * Displays a rich text textarea for a settings field 218 | * 219 | * @param array $args settings field args 220 | */ 221 | function callback_wysiwyg( $args ) { 222 | $value = $this->get_option( $args['id'], $args['section'], $args['std'] ); 223 | $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : '500px'; 224 | echo '
'; 225 | wp_editor( $value, $args['section'] . '-' . $args['id'] . '', array( 'teeny' => true, 'textarea_name' => $args['section'] . '[' . $args['id'] . ']', 'textarea_rows' => 10 ) ); 226 | echo '
'; 227 | echo sprintf( '
%s', $args['desc'] ); 228 | } 229 | /** 230 | * Displays a file upload field for a settings field 231 | * 232 | * @param array $args settings field args 233 | */ 234 | function callback_file( $args ) { 235 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); 236 | $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular'; 237 | $id = $args['section'] . '[' . $args['id'] . ']'; 238 | $html = sprintf( '', $size, $args['section'], $args['id'], $value ); 239 | $html .= ''; 240 | $html .= sprintf( ' %s', $args['desc'] ); 241 | echo $html; 242 | } 243 | /** 244 | * Displays a password field for a settings field 245 | * 246 | * @param array $args settings field args 247 | */ 248 | function callback_password( $args ) { 249 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); 250 | $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular'; 251 | $html = sprintf( '', $size, $args['section'], $args['id'], $value ); 252 | $html .= sprintf( ' %s', $args['desc'] ); 253 | echo $html; 254 | } 255 | /** 256 | * Displays a color picker field for a settings field 257 | * 258 | * @param array $args settings field args 259 | */ 260 | function callback_color( $args ) { 261 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); 262 | $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular'; 263 | $html = sprintf( '', $size, $args['section'], $args['id'], $value, $args['std'] ); 264 | $html .= sprintf( ' %s', $args['desc'] ); 265 | echo $html; 266 | } 267 | /** 268 | * Sanitize callback for Settings API 269 | */ 270 | function sanitize_options( $options ) { 271 | foreach( $options as $option_slug => $option_value ) { 272 | $sanitize_callback = $this->get_sanitize_callback( $option_slug ); 273 | // If callback is set, call it 274 | if ( $sanitize_callback ) { 275 | $options[ $option_slug ] = call_user_func( $sanitize_callback, $option_value ); 276 | continue; 277 | } 278 | } 279 | return $options; 280 | } 281 | /** 282 | * Get sanitization callback for given option slug 283 | * 284 | * @param string $slug option slug 285 | * 286 | * @return mixed string or bool false 287 | */ 288 | function get_sanitize_callback( $slug = '' ) { 289 | if ( empty( $slug ) ) { 290 | return false; 291 | } 292 | // Iterate over registered fields and see if we can find proper callback 293 | foreach( $this->settings_fields as $section => $options ) { 294 | foreach ( $options as $option ) { 295 | if ( $option['name'] != $slug ) { 296 | continue; 297 | } 298 | // Return the callback name 299 | return isset( $option['sanitize_callback'] ) && is_callable( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : false; 300 | } 301 | } 302 | return false; 303 | } 304 | /** 305 | * Get the value of a settings field 306 | * 307 | * @param string $option settings field name 308 | * @param string $section the section name this field belongs to 309 | * @param string $default default text if it's not found 310 | * @return string 311 | */ 312 | function get_option( $option, $section, $default = '' ) { 313 | $options = get_option( $section ); 314 | if ( isset( $options[$option] ) ) { 315 | return $options[$option]; 316 | } 317 | return $default; 318 | } 319 | /** 320 | * Show navigations as tab 321 | * 322 | * Shows all the settings section labels as tab 323 | */ 324 | function show_navigation() { 325 | $html = ''; 330 | echo $html; 331 | } 332 | /** 333 | * Show the section settings forms 334 | * 335 | * This function displays every sections in a different form 336 | */ 337 | function show_forms() { 338 | ?> 339 |
340 |
341 | settings_sections as $form ) { ?> 342 |
343 |
344 | 345 | 346 | 347 | 348 | 349 | 350 |
351 | 352 |
353 |
354 |
355 | 356 |
357 |
358 | script(); 360 | } 361 | /** 362 | * Tabbable JavaScript codes & Initiate Color Picker 363 | * 364 | * This code uses localstorage for displaying active tabs 365 | */ 366 | function script() { 367 | ?> 368 | 436 | 437 | 442 | dir = plugin_dir_path(__FILE__); 22 | $this->url = plugins_url('', __FILE__); 23 | $this->settings_api = new WeDevs_Settings_API; 24 | 25 | add_action('admin_init', array($this, 'admin_init')); 26 | add_action('admin_menu', array($this, 'sub_menu_page')); 27 | 28 | } 29 | 30 | function admin_init() { 31 | 32 | //set the settings 33 | $this->settings_api->set_sections($this->get_settings_sections()); 34 | $this->settings_api->set_fields($this->get_settings_fields()); 35 | 36 | //initialize settings 37 | $this->settings_api->admin_init(); 38 | } 39 | 40 | function sub_menu_page() { 41 | add_submenu_page('options-general.php', 'PrimeTime WP + Discourse SSO Settings', __('WP Discourse SSO', 'pt-wp-discourse-sso'), 'manage_options', 'wp-sso-settings', array($this, 'submenu_page_callback')); 42 | } 43 | 44 | 45 | function submenu_page_callback() { 46 | 47 | echo '
'; 48 | ?>

settings_api->show_navigation(); 51 | $this->settings_api->show_forms(); 52 | 53 | echo '
'; 54 | 55 | } 56 | 57 | function get_settings_sections() { 58 | $sections = array( 59 | array( 60 | 'id' => 'pt_wp_sso_settings', 61 | 'title' => 'Discourse Settings' 62 | ) 63 | ); 64 | 65 | if (is_plugin_active('membermouse/index.php')) { 66 | $sections[] = array( 67 | 'id' => 'pt_wp_sso_mm_settings', 68 | 'title' => 'Membermouse Settings' 69 | ); 70 | } 71 | return $sections; 72 | } 73 | 74 | function get_settings_fields() { 75 | 76 | $settings_fields = array( 77 | 'pt_wp_sso_settings' => array( 78 | array( 79 | 'name' => 'secret_key', 80 | 'label' => 'Secret Key', 81 | 'desc' => 'Random string that will be the same on both your WP and Discourse installation.', 82 | 'type' => 'text', 83 | 'default' => '', 84 | 'sanitize_callback' => 'sanitize_text_field' 85 | ), 86 | array( 87 | 'name' => 'discourse_url', 88 | 'label' => 'Discourse URL', 89 | 'desc' => 'The base URL to your Discourse installation (include protocol)', 90 | 'type' => 'text', 91 | 'default' => '', 92 | 'sanitize_callback' => 'sanitize_text_field' 93 | ), 94 | array( 95 | 'name' => 'sync_logout', 96 | 'label' => 'Synchronize Logout', 97 | 'desc' => 'If user logs out of WP, also log them out of your Discourse installation', 98 | 'type' => 'checkbox', 99 | 'default' => 'off', 100 | 'sanitize_callback' => 'sanitize_checkbox' 101 | ), 102 | array( 103 | 'name' => 'api_key', 104 | 'label' => 'Admin API Key', 105 | 'desc' => 'Required to synchronize logout', 106 | 'type' => 'text', 107 | 'default' => '', 108 | 'sanitize_callback' => 'sanitize_text_field' 109 | ), 110 | array( 111 | 'name' => 'api_username', 112 | 'label' => 'Admin Username', 113 | 'desc' => 'Required to synchronize logout', 114 | 'type' => 'text', 115 | 'default' => '', 116 | 'sanitize_callback' => 'sanitize_text_field' 117 | ) 118 | ) 119 | ); 120 | 121 | //If Membermouse is present, add option to disallow free members. 122 | if (is_plugin_active('membermouse/index.php')) { 123 | $settings_fields['pt_wp_sso_mm_settings'] = array(array( 124 | 'name' => 'block_membermouse_free', 125 | 'label' => 'Disallow Free Members', 126 | 'desc' => 'Block free MemberMouse members from accessing your Discourse installation.', 127 | 'type' => 'checkbox', 128 | 'default' => 'off', 129 | 'sanitize_callback' => 'sanitize_checkbox' 130 | )); 131 | }; 132 | 133 | return $settings_fields; 134 | } 135 | 136 | /** 137 | * 138 | * Sanitize checkbox input 139 | * 140 | */ 141 | function sanitize_checkbox($input) { 142 | 143 | if ($input) { 144 | 145 | $output = '1'; 146 | 147 | } else { 148 | 149 | $output = false; 150 | 151 | } 152 | 153 | return $output; 154 | } 155 | 156 | /** 157 | * 158 | * Sanitize integers 159 | * 160 | */ 161 | function sanitize_int($input) { 162 | 163 | if ($input) { 164 | 165 | $output = absint($input); 166 | 167 | } else { 168 | 169 | $output = false; 170 | 171 | } 172 | 173 | return $output; 174 | } 175 | } 176 | endif; 177 | 178 | $settings = new pt_wp_discourse_sso_settings_api_wrap(); 179 | 180 | 181 | 182 | 183 | 184 | 185 | -------------------------------------------------------------------------------- /admin/includes/index.php: -------------------------------------------------------------------------------- 1 | 6 | * @license GPL-2.0+ 7 | * @link http://primetimecode.com 8 | * @copyright 2014 PrimeTimeCode 9 | * 10 | * Plugin Name: PrimeTime WP Discourse SSO 11 | * Plugin URI: http://etc.io 12 | * Description: Single Sign on between WordPress and Discourse 13 | * Version: 0.2.3 14 | * GitHub Plugin URI: https://github.com/PrimeTimeCode/pt-wp-discourse-sso 15 | */ 16 | 17 | // If this file is called directly, abort. 18 | if ( ! defined( 'WPINC' ) ) { 19 | die; 20 | } 21 | 22 | // Set some constants 23 | define('PT_WP_DISCOURSE_SSO_VERSION', '0.2.2'); 24 | define('PT_WP_DISCOURSE_SSO_DIR', plugin_dir_path( __FILE__ )); 25 | define('PT_WP_DISCOURSE_SSO_URL', plugins_url( '', __FILE__ )); 26 | /*----------------------------------------------------------------------------* 27 | * Public-Facing Functionality 28 | *----------------------------------------------------------------------------*/ 29 | require_once( PT_WP_DISCOURSE_SSO_DIR . 'public/class-pt-wp-discourse-sso.php' ); 30 | 31 | /* 32 | * Register hooks that are fired when the plugin is activated or deactivated. 33 | * When the plugin is deleted, the uninstall.php file is loaded. 34 | * 35 | */ 36 | register_activation_hook( __FILE__, array( 'WP_Discourse_SSO', 'activate' ) ); 37 | register_deactivation_hook( __FILE__, array( 'WP_Discourse_SSO', 'deactivate' ) ); 38 | 39 | add_action( 'plugins_loaded', array( 'WP_Discourse_SSO', 'get_instance' ) ); 40 | 41 | /*----------------------------------------------------------------------------* 42 | * Dashboard and Administrative Functionality 43 | *----------------------------------------------------------------------------*/ 44 | 45 | /* 46 | * 47 | * If you want to include Ajax within the dashboard, change the following 48 | * conditional to: 49 | * 50 | * if ( is_admin() ) { 51 | * ... 52 | * } 53 | * 54 | * The code below is intended to to give the lightest footprint possible. 55 | */ 56 | if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) { 57 | 58 | require_once( plugin_dir_path( __FILE__ ) . 'admin/class-pt-wp-discourse-sso-admin.php' ); 59 | add_action( 'plugins_loaded', array( 'WP_Discourse_SSO_Admin', 'get_instance' ) ); 60 | 61 | } 62 | -------------------------------------------------------------------------------- /public/assets/css/index.php: -------------------------------------------------------------------------------- 1 | 7 | * @license GPL-2.0+ 8 | * @link http://primetimecode.com 9 | * @copyright 2014 PrimeTime Code 10 | */ 11 | 12 | /** 13 | * Plugin class. This class should ideally be used to work with the 14 | * public-facing side of the WordPress site. 15 | * 16 | * 17 | * @package WP_Discourse_SSO 18 | * @author ET Cook 19 | */ 20 | class WP_Discourse_SSO { 21 | 22 | public $configured = array(); 23 | private $sso_secret; 24 | public $discourse_url; 25 | public $admin_url; 26 | 27 | /** 28 | * Plugin version, used for cache-busting of style and script file references. 29 | * 30 | * @since 0.1 31 | * 32 | * @var string 33 | */ 34 | const VERSION = PT_WP_DISCOURSE_SSO_VERSION; 35 | 36 | /** 37 | * Unique identifier 38 | * 39 | * 40 | * The variable name is used as the text domain when internationalizing strings 41 | * of text. Its value should match the Text Domain file header in the main 42 | * plugin file. 43 | * 44 | * @since 0.1 45 | * 46 | * @var string 47 | */ 48 | protected $plugin_slug = 'pt-wp-discourse-sso'; 49 | 50 | /** 51 | * Instance of this class. 52 | * 53 | * @since 0.1 54 | * 55 | * @var object 56 | */ 57 | protected static $instance = null; 58 | 59 | /** 60 | * Initialize the plugin by setting localization and loading public scripts 61 | * and styles. 62 | * 63 | * @since 0.1 64 | */ 65 | private function __construct() { 66 | 67 | // Activate plugin when new blog is added 68 | add_action('wpmu_new_blog', array($this, 'activate_new_site')); 69 | 70 | require_once(PT_WP_DISCOURSE_SSO_DIR . 'public/includes/helpers.php'); 71 | 72 | $this->check_configuration(); 73 | 74 | $this->admin_url = admin_url('options-general.php?page=wp-sso-settings'); 75 | 76 | add_action('init', array($this, 'interceptSSORequest')); 77 | 78 | add_action('clear_auth_cookie', array($this, 'interceptLogout')); 79 | 80 | } 81 | 82 | private function check_configuration() { 83 | $this->configured['all'] = FALSE; 84 | $this->configured['secret'] = FALSE; 85 | $this->configured['discourse_url'] = FALSE; 86 | $this->configured['activated'] = FALSE; 87 | 88 | if (get_site_option('pt_wp_discourse_sso_activated')) { 89 | $this->configured['activated'] = TRUE; 90 | } 91 | 92 | if (NULL != pt_wp_sso_get_option('secret_key', 'pt_wp_sso_settings')) { 93 | $this->configured['secret'] = TRUE; 94 | $this->sso_secret = pt_wp_sso_get_option('secret_key', 'pt_wp_sso_settings'); 95 | } 96 | 97 | if (NULL != pt_wp_sso_get_option('discourse_url', 'pt_wp_sso_settings')) { 98 | $this->configured['discourse_url'] = TRUE; 99 | $this->discourse_url = rtrim(pt_wp_sso_get_option('discourse_url', 'pt_wp_sso_settings'), '/'); 100 | } 101 | 102 | // Make sure the plugin is configured 103 | if ($this->configured['secret'] && $this->configured['discourse_url'] && $this->configured['activated']) { 104 | $this->configured['all'] = TRUE; 105 | } 106 | 107 | if (!$this->configured['all']) { 108 | add_action('admin_notices', array($this, 'render_admin_notice')); 109 | } 110 | 111 | } 112 | 113 | public function render_admin_notice() { 114 | ?> 115 |
116 |

Setting up your WP + Discourse SSO connection is easy!

117 | 118 |

119 |

    120 |
  1. configured['secret'] ? '' : ''); ?>Generate or come up with a random string of 121 | characters to use as a "key." Enter it in the settings 122 | page.configured['secret'] ? '' : ''); ?>
  2. 123 |
  3. configured['discourse_url'] ? '' : ''); ?>Paste your Discourse URL 124 | (http://example.discourse.org) into settings 125 | page.configured['discourse_url'] ? '' : ''); ?>
  4. 126 |
  5. configured['activated'] ? '' : ''); ?>Handle your first authentication. (This 127 | message will go away once you'd validated your first SSO 128 | request)configured['activated'] ? '' : ''); ?>
  6. 129 |
130 |

131 |
132 | plugin_slug; 144 | } 145 | 146 | /** 147 | * Return an instance of this class. 148 | * 149 | * @since 0.1 150 | * 151 | * @return object A single instance of this class. 152 | */ 153 | public static function get_instance() { 154 | 155 | // If the single instance hasn't been set, set it now. 156 | if (null == self::$instance) { 157 | self::$instance = new self; 158 | } 159 | 160 | return self::$instance; 161 | } 162 | 163 | /** 164 | * Fired when the plugin is activated. 165 | * 166 | * @since 0.1 167 | * 168 | * @param boolean $network_wide True if WPMU superadmin uses 169 | * "Network Activate" action, false if 170 | * WPMU is disabled or plugin is 171 | * activated on an individual blog. 172 | */ 173 | public static function activate($network_wide) { 174 | 175 | if (function_exists('is_multisite') && is_multisite()) { 176 | 177 | if ($network_wide) { 178 | 179 | // Get all blog ids 180 | $blog_ids = self::get_blog_ids(); 181 | 182 | foreach ($blog_ids as $blog_id) { 183 | 184 | switch_to_blog($blog_id); 185 | self::single_activate(); 186 | } 187 | 188 | restore_current_blog(); 189 | 190 | } else { 191 | self::single_activate(); 192 | } 193 | 194 | } else { 195 | self::single_activate(); 196 | } 197 | 198 | } 199 | 200 | /** 201 | * Fired when the plugin is deactivated. 202 | * 203 | * @since 0.1 204 | * 205 | * @param boolean $network_wide True if WPMU superadmin uses 206 | * "Network Deactivate" action, false if 207 | * WPMU is disabled or plugin is 208 | * deactivated on an individual blog. 209 | */ 210 | public static function deactivate($network_wide) { 211 | 212 | if (function_exists('is_multisite') && is_multisite()) { 213 | 214 | if ($network_wide) { 215 | 216 | // Get all blog ids 217 | $blog_ids = self::get_blog_ids(); 218 | 219 | foreach ($blog_ids as $blog_id) { 220 | 221 | switch_to_blog($blog_id); 222 | self::single_deactivate(); 223 | 224 | } 225 | 226 | restore_current_blog(); 227 | 228 | } else { 229 | self::single_deactivate(); 230 | } 231 | 232 | } else { 233 | self::single_deactivate(); 234 | } 235 | 236 | } 237 | 238 | /** 239 | * Fired when a new site is activated with a WPMU environment. 240 | * 241 | * @since 0.1 242 | * 243 | * @param int $blog_id ID of the new blog. 244 | */ 245 | public function activate_new_site($blog_id) { 246 | 247 | if (1 !== did_action('wpmu_new_blog')) { 248 | return; 249 | } 250 | 251 | switch_to_blog($blog_id); 252 | self::single_activate(); 253 | restore_current_blog(); 254 | 255 | } 256 | 257 | /** 258 | * Get all blog ids of blogs in the current network that are: 259 | * - not archived 260 | * - not spam 261 | * - not deleted 262 | * 263 | * @since 0.1 264 | * 265 | * @return array|false The blog ids, false if no matches. 266 | */ 267 | private static function get_blog_ids() { 268 | 269 | global $wpdb; 270 | 271 | // get an array of blog ids 272 | $sql = "SELECT blog_id FROM $wpdb->blogs 273 | WHERE archived = '0' AND spam = '0' 274 | AND deleted = '0'"; 275 | 276 | return $wpdb->get_col($sql); 277 | 278 | } 279 | 280 | /** 281 | * Fired for each blog when the plugin is activated. 282 | * 283 | * @since 0.1 284 | */ 285 | private static function single_activate() { 286 | // @TODO: Define activation functionality here 287 | } 288 | 289 | /** 290 | * Fired for each blog when the plugin is deactivated. 291 | * 292 | * @since 0.1 293 | */ 294 | private static function single_deactivate() { 295 | // @TODO: Define deactivation functionality here 296 | } 297 | 298 | /** 299 | * Validate the SSO payload 300 | * @param [type] $payload [description] 301 | * @param [type] $sig [description] 302 | * @return [type] [description] 303 | */ 304 | public function validate($payload, $sig) { 305 | 306 | $payload = urldecode($payload); 307 | if (hash_hmac("sha256", $payload, $this->sso_secret) === $sig) { 308 | if (!$this->configured['activated']) { 309 | add_site_option('pt_wp_discourse_sso_activated', TRUE); 310 | } 311 | return true; 312 | } else { 313 | return false; 314 | } 315 | } 316 | 317 | /** 318 | * Get nonce out of original payload 319 | * @param [type] $payload [description] 320 | * @return [type] [description] 321 | */ 322 | public function getNonce($payload) { 323 | $payload = urldecode($payload); 324 | $query = array(); 325 | parse_str(base64_decode($payload), $query); 326 | if (isset($query["nonce"])) { 327 | return $query["nonce"]; 328 | } else { 329 | throw new Exception("Nonce not found in payload!"); 330 | } 331 | } 332 | 333 | /** 334 | * Create a login string to authenticate with Discourse 335 | * @param [type] $params [description] 336 | * @return [type] [description] 337 | */ 338 | public function buildLoginString($params) { 339 | if (!isset($params["external_id"])) { 340 | throw new Exception("Missing required parameter 'external_id'"); 341 | } 342 | if (!isset($params["nonce"])) { 343 | throw new Exception("Missing required parameter 'nonce'"); 344 | } 345 | if (!isset($params["email"])) { 346 | throw new Exception("Missing required parameter 'email'"); 347 | } 348 | $payload = base64_encode(http_build_query($params)); 349 | $sig = hash_hmac("sha256", $payload, $this->sso_secret); 350 | 351 | return http_build_query(array("sso" => $payload, "sig" => $sig)); 352 | } 353 | 354 | private function cleansePayload($p) { 355 | return str_replace('%0A', '%0B', $p); 356 | } 357 | 358 | private function restorePayload($p) { 359 | return urldecode(str_replace('%0B', '%0A', urlencode($p))); 360 | } 361 | 362 | private function get_avatar_url($user_id) { 363 | $avatar = get_avatar($user_id); 364 | if (preg_match("/src=['\"](.*?)['\"]/i", $avatar, $matches)) 365 | return utf8_uri_encode($matches[1]); 366 | } 367 | 368 | /** 369 | * Callback function to intercept and validate SSO requests 370 | * @return [type] [description] 371 | */ 372 | public function interceptSSORequest() { 373 | 374 | if (isset($_GET['sso']) && isset($_GET['sig'])) { 375 | 376 | $varsso = $_GET['sso']; 377 | $varsig = $_GET['sig']; 378 | 379 | if (!$this->validate($varsso, $varsig)) { 380 | //return; 381 | } 382 | 383 | // Check to see whether the user is logged in or not 384 | if (!is_user_logged_in()) { 385 | 386 | // Preserve sso and sig parameters 387 | $redirect = add_query_arg(array('sso' => urlencode($varsso), 'sig' => urlencode($varsig))); 388 | 389 | // Change %0A to %0B so it's not stripped out in wp_sanitize_redirect 390 | $redirect = $this->cleansePayload($redirect); 391 | 392 | // Build login URL 393 | $login = wp_login_url(esc_url_raw($redirect)); 394 | 395 | // Redirect to login 396 | wp_redirect($login); 397 | exit; 398 | 399 | } // Logged in to WordPress, now try to log in to Discourse with WordPress user information 400 | else { 401 | $allowedToLogin = true; 402 | if (is_plugin_active('membermouse/index.php')) { 403 | if (pt_wp_sso_get_option('block_membermouse_free', 'pt_wp_sso_mm_settings') == 'on') { 404 | if (mm_member_decision(array("isFree" => "true"))) { 405 | $allowedToLogin = false; 406 | } 407 | } 408 | } 409 | 410 | if ($allowedToLogin) { 411 | $ssopayload = $this->restorePayload($_GET['sso']); 412 | $sigpayload = $_GET['sig']; 413 | 414 | if (!($this->validate($ssopayload, $sigpayload))) { 415 | 416 | // Error message 417 | echo('Invalid request.'); 418 | 419 | // Terminate 420 | exit; 421 | 422 | } 423 | 424 | // Nonce 425 | $nonce = $this->getNonce($ssopayload); 426 | 427 | $current_user = wp_get_current_user(); 428 | 429 | // Map information 430 | $params = array( 431 | 'nonce' => $nonce, 432 | 'name' => $current_user->display_name, 433 | 'username' => $current_user->user_login, 434 | 'email' => $current_user->user_email, 435 | 'bio' => $current_user->description, 436 | 'external_id' => $current_user->ID, 437 | 'avatar_url' => self::get_avatar_url($current_user->ID) 438 | ); 439 | 440 | // Build login string 441 | $q = $this->buildLoginString($params); 442 | 443 | // Redirect back to Discourse 444 | wp_redirect($this->discourse_url . '/session/sso_login?' . $q); 445 | } else { 446 | //redirect to home 447 | wp_redirect(home_url()); 448 | } 449 | 450 | exit; 451 | 452 | } 453 | } 454 | } 455 | 456 | //If sync logout is enabled, we need to find the user id, then log that user out of discourse. 457 | public function interceptLogout() { 458 | if (pt_wp_sso_get_option('sync_logout', 'pt_wp_sso_settings') == 'on') { 459 | $api_key = pt_wp_sso_get_option('api_key', 'pt_wp_sso_settings'); 460 | $api_username = pt_wp_sso_get_option('api_username', 'pt_wp_sso_settings'); 461 | 462 | if (NULL != $api_key && NULL != $api_username) { 463 | $userinfo = wp_get_current_user(); 464 | 465 | $params = '?api_key=' . $api_key . '&api_username=' . $api_username; 466 | $get_url = $this->discourse_url . '/users/by-external/' . $userinfo->ID . '.json' . $params; 467 | 468 | //First Talk to Discourse and get Discourse ID 469 | $response = wp_safe_remote_get($get_url); 470 | 471 | if (is_array($response) && !is_wp_error($response)) { 472 | $body = json_decode($response['body'], true); 473 | $id = $body['user']['id']; 474 | if (NULL != $id) { 475 | //fire the logout trigger 476 | $post_url = $this->discourse_url . '/admin/users/' . $id . '/log_out' . $params; 477 | $response = wp_safe_remote_post($post_url); 478 | } 479 | } 480 | } 481 | 482 | } 483 | 484 | } 485 | 486 | } 487 | 488 | 489 | 490 | 491 | 492 | -------------------------------------------------------------------------------- /public/includes/helpers.php: -------------------------------------------------------------------------------- 1 | 7 | * @license GPL-2.0+ 8 | * @link http://example.com 9 | * @copyright 2014 Your Name or Company Name 10 | */ 11 | 12 | // If uninstall not called from WordPress, then exit 13 | if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { 14 | exit; 15 | } 16 | 17 | // @TODO: Define uninstall functionality here --------------------------------------------------------------------------------