all repos — wp-ffpc @ 35fca7f41a82c2223c66193107decc1f0219b7f9

wp-ffpc.php (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
<?php
/*
Plugin Name: WP-FFPC
Version: 0.5
Plugin URI: http://petermolnar.eu/wordpress/wp-ffpc
Description: Fast Full Page Cache, backend can be memcached or APC
Author: Peter Molnar
Author URI: http://petermolnar.eu/
License: GPL2
*/

/*  Copyright 2010-2011 Peter Molnar  (email : hello@petermolnar.eu )

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    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 St, Fifth Floor, Boston, MA  02110-1301  USA

*/

/**
 *  checks for SSL connection
*/
if ( ! function_exists ( 'replace_if_ssl' ) ) {
	function replace_if_ssl ( $string ) {
		if ( isset($_SERVER['HTTPS']) && ( ( strtolower($_SERVER['HTTPS']) == 'on' )  || ( $_SERVER['HTTPS'] == '1' ) ) )
			return str_replace ( 'http://' , 'https://' , $string );
		else
			return $string;
	}
}

/* fix */
if ( ! defined( 'WP_PLUGIN_URL_' ) )
{
	if ( defined( 'WP_PLUGIN_URL' ) )
		define( 'WP_PLUGIN_URL_' , replace_if_ssl ( WP_PLUGIN_URL ) );
	else
		define( 'WP_PLUGIN_URL_', replace_if_ssl ( get_option( 'siteurl' ) ) . '/wp-content/plugins' );
}

if ( ! defined( 'WP_PLUGIN_DIR' ) )
	define( 'WP_PLUGIN_DIR', ABSPATH . 'wp-content/plugins' );

/* constants */
define ( 'WP_FFPC_PARAM' , 'wp-ffpc' );
define ( 'WP_FFPC_OPTION_GROUP' , 'wp-ffpcparams' );
define ( 'WP_FFPC_OPTIONS_PAGE' , 'wp-ffpcoptions' );
define ( 'WP_FFPC_URL' , WP_PLUGIN_URL_ . '/' . WP_FFPC_PARAM  );
define ( 'WP_FFPC_DIR' , WP_PLUGIN_DIR . '/' . WP_FFPC_PARAM );
define ( 'WP_FFPC_CONF_DIR' , WP_PLUGIN_DIR . '/' . WP_FFPC_PARAM .'/config' );
define ( 'WP_FFPC_ACACHE_MAIN_FILE' , ABSPATH . 'wp-content/advanced-cache.php' );
define ( 'WP_FFPC_ACACHE_INC_FILE' , WP_FFPC_DIR. '/advanced-cache.php' );
define ( 'WP_FFPC_ACACHE_COMMON_FILE' , WP_FFPC_DIR. '/wp-ffpc-common.php' );
define ( 'WP_FFPC_CONFIG_VAR' , '$wp_ffpc_config' );
define ( 'WP_FFPC_SERVER_LIST_SEPARATOR' , ',' );
define ( 'WP_FFPC_SERVER_SEPARATOR', ':' );

include_once (WP_FFPC_DIR .'/wp-ffpc-common.php');

if (!class_exists('WPFFPC')) {

	/**
	 * main class
	 *
	 */
	class WPFFPC {

		/* for options array */
		var $options = array();
		/* for default options array */
		var $defaults = array();
		/* memcached server object */
		var $memcached = NULL;
		var $memcached_string = '';

		/* status, 0 = nothing happened*/
		var $status = 0;

		var $network = false;

		/**
		* constructor
		*
		*/
		function __construct() {
			$this->check_for_network();

			/* register options */
			$this->get_options();

			/* check is backend is available */
			$alive = wp_ffpc_init( $this->options );

			/* don't register hooks if backend is dead */
			if ($alive)
			{
				/* init inactivation hooks */
				add_action('switch_theme', array( $this , 'invalidate'), 0);
				add_action('edit_post', array( $this , 'invalidate'), 0);
				add_action('publish_post', array( $this , 'invalidate'), 0);
				add_action('delete_post', array( $this , 'invalidate'), 0);

				/* Capture and register if a redirect is sent back from WP, so the cache
				can cache (or ignore) it. Redirects were source of problems for blogs
				 with more than one host name (eg. domain.com and www.domain.com) comined
				with the use of Hyper Cache.*/
				add_filter('redirect_canonical', array( $this , 'redirect_canonical') , 10, 2);
			}

			/* add admin styling */
			if( is_admin() )
			{
				wp_enqueue_script ( "jquery-ui-tabs" );
				wp_enqueue_style( WP_FFPC_PARAM . '.admin.css' , WP_FFPC_URL . '/css/'. WP_FFPC_PARAM .'.admin.css', false, '0.1');
			}

			/* on activation */
			register_activation_hook(__FILE__ , array( $this , 'activate') );

			/* on deactivation */
			register_deactivation_hook(__FILE__ , array( $this , 'deactivate') );

			/* on uninstall */
			register_uninstall_hook(__FILE__ , array( $this , 'uninstall') );


			/* init plugin in the admin section */
			/* if multisite, admin page will be on network admin section */
			if ( $this->network )
				add_action('network_admin_menu', array( $this , 'admin_init') );
			/* not network, will be in simple admin menu */
			else
				add_action('admin_menu', array( $this , 'admin_init') );
		}

		/**
		 * activation hook: save default settings in order to eliminate bugs.
		 *
		 */
		function activate ( ) {
			$this->save_settings( true );
		}

		/**
		 * init function for admin section
		 *
		 */
		function admin_init () {
			/* register options */
			add_site_option( WP_FFPC_PARAM, $this->options , '' , 'no');

			/* save parameter updates, if there are any */
			if ( isset($_POST[WP_FFPC_PARAM . '-save']) )
			{
				$this->save_settings ();
				$this->status = 1;
				header("Location: admin.php?page=" . WP_FFPC_OPTIONS_PAGE . "&saved=true");
			}

			add_submenu_page('settings.php', 'Edit WP-FFPC options', __('WP-FFPC', WP_FFPC_PARAM ), 10, WP_FFPC_OPTIONS_PAGE , array ( $this , 'admin_panel' ) );
			//add_menu_page('Edit WP-FFPC options', __('WP-FFPC', WP_FFPC_PARAM ), 10, WP_FFPC_OPTIONS_PAGE , array ( $this , 'admin_panel' ) );
		}

		/**
		 * settings panel at admin section
		 *
		 */
		function admin_panel ( ) {

			/**
			 * security
			 */
			if( ! function_exists( 'current_user_can' ) || ! current_user_can( 'manage_options' ) ){
				die( );
			}

			/**
			 * if options were saved
			 */
			if ($_GET['saved']=='true' || $this->status == 1) : ?>
				<div id='setting-error-settings_updated' class='updated settings-error'><p><strong>Settings saved.</strong></p></div>
			<?php endif;

			/**
			 * the admin panel itself
			 */
			?>

			<script>
				jQuery(document).ready(function($) {
					jQuery( "#wp-ffpc-settings" ).tabs();
				});
			</script>

			<div class="wrap">

			<h4>This plugin helped your business? <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XU3DG7LLA76WC">Buy me a coffee for having it :)</a></h4>

			<?php if ( !WP_CACHE ) : ?>
				<div class="updated settings-error"><p><strong><?php _e("WARNING: WP_CACHE is disabled, plugin will not work that way. Please add define( 'WP_CACHE', true ); into the beginning of wp-config.php", WP_FFPC_PARAM); ?></strong></p></div>
			<?php endif; ?>

			<?php if ( !class_exists('Memcache') && !class_exists('Memcached')  ) : ?>
				<div class="updated settings-error"><p><strong><?php _e('No PHP memcached extension was found. To use memcached, you need PHP Memcache or PHP Memcached extension.', WP_FFPC_PARAM); ?></strong></p></div>
			<?php endif; ?>

			<?php
				$memcached_settings = ini_get_all( 'memcache' );
				$memcached_protocol = strtolower($memcached_settings['memcache.protocol']['local_value']);
			?>

			<?php if ( $this->options['cache_type'] == 'memcached' && $memcached_protocol == 'binary' ) : ?>
				<div class="updated settings-error"><p><strong><?php _e('WARNING: Memcache extension is configured to use binary mode. This is very buggy and the plugin will most probably not work. Please consider to change either to ascii mode or to Mecached extension.', WP_FFPC_PARAM); ?></strong></p></div>
			<?php endif; ?>

			<?php if ( $this->options['cache_type'] == 'memcached' || $this->options['cache_type'] == 'memcache' ) : ?>
				<div class="updated settings-error">
					<p><strong>
					<?php
						_e ( 'Driver: ' , WP_FFPC_PARAM);
						echo $this->options['cache_type'];
					?>
					</strong></p>
					<p>
					<?php
						_e( '<strong>Backend status:</strong><br />', WP_FFPC_PARAM );
						$init = wp_ffpc_init( $this->options);
						foreach ( $this->options['servers'] as $server_string => $server ) {
							echo $server['host'] . ":" . $server['port'] ." => ";
							$server_status = ( empty($init) || $init[$server_string] == 0 ) ? '<span class="error-msg">down</span>' : '<span class="ok-msg">up & running</span>' ;
							echo $server_status ."<br />\n";
						}
					?>
					</p>
				</div>
			<?php endif; ?>

			<h2><?php _e( 'WP-FFPC settings', WP_FFPC_PARAM ) ; ?></h2>
			<form method="post" action="#" id="wp-ffpc-settings" class="wp-ffpc-panel">

				<ul class="tabs">
					<li><a href="#wp-ffpc-type" class="wp-switch-editor"><?php _e( 'Cache type', WP_FFPC_PARAM ); ?></a></li>
					<li><a href="#wp-ffpc-debug" class="wp-switch-editor"><?php _e( 'Debug & in-depth', WP_FFPC_PARAM ); ?></a></li>
					<li><a href="#wp-ffpc-exceptions" class="wp-switch-editor"><?php _e( 'Cache exceptions', WP_FFPC_PARAM ); ?></a></li>
					<li><a href="#wp-ffpc-apc" class="wp-switch-editor"><?php _e( 'APC', WP_FFPC_PARAM ); ?></a></li>
					<li><a href="#wp-ffpc-memcached" class="wp-switch-editor"><?php _e( 'Memcache(d)', WP_FFPC_PARAM ); ?></a></li>
					<li><a href="#wp-ffpc-nginx" class="wp-switch-editor"><?php _e( 'nginx', WP_FFPC_PARAM ); ?></a></li>
				</ul>

				<fieldset id="wp-ffpc-type">
				<legend><?php _e( 'Set cache type', WP_FFPC_PARAM ); ?></legend>
				<dl>
					<dt>
						<label for="cache_type"><?php _e('Select backend', WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<select name="cache_type" id="cache_type">
							<?php $this->cache_type ( $this->options['cache_type'] ) ?>
						</select>
						<span class="description"><?php _e('Select cache driver: ', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php $this->cache_type( $this->defaults['cache_type'] , true ) ; ?></span>
					</dd>

					<dt>
						<label for="expire"><?php _e('Entry invalidation time', WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="number" name="expire" id="expire" value="<?php echo $this->options['expire']; ?>" />
						<span class="description"><?php _e('How long will an entry be valid', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php echo $this->defaults['expire']; ?></span>
					</dd>

					<dt>
						<label for="charset"><?php _e('Charset to send data with.', WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="text" name="charset" id="charset" value="<?php echo $this->options['charset']; ?>" />
						<span class="description"><?php _e('Charset of HTML and XML (pages and feeds) data.', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php echo $this->defaults['charset']; ?></span>
					</dd>

					<dt>
						<label for="invalidation_method"><?php _e('Cache invalidation method', WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<select name="invalidation_method" id="invalidation_method">
							<?php $this->invalidation_method ( $this->options['invalidation_method'] ) ?>
						</select>
						<span class="description"><?php _e('Select cache invalidation method. <p><strong>WARNING! When selection "all", the cache will be fully flushed, including elements that were set by other applications.</strong></p>', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php $this->invalidation_method( $this->defaults['invalidation_method'] , true ) ; ?></span>
					</dd>

					<dt>
						<label for="prefix_data"><?php _e('Data prefix', WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="text" name="prefix_data" id="prefix_data" value="<?php echo $this->options['prefix_data']; ?>" />
						<span class="description"><?php _e('Prefix for HTML content keys, can be used in nginx.', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php echo $this->defaults['prefix_data']; ?></span>
					</dd>

					<dt>
						<label for="prefix_meta"><?php _e('Meta prefix', WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="text" name="prefix_meta" id="prefix_meta" value="<?php echo $this->options['prefix_meta']; ?>" />
						<span class="description"><?php _e('Prefix for meta content keys, used only with PHP processing.', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php echo $this->defaults['prefix_meta']; ?></span>
					</dd>
				</dl>
				</fieldset>

				<fieldset id="wp-ffpc-debug">
				<legend><?php _e( 'Debug & in-depth settings', WP_FFPC_PARAM ); ?></legend>
				<dl>
					<dt>
						<label for="debug"><?php _e("Enable debug mode", WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="checkbox" name="debug" id="debug" value="1" <?php checked($this->options['debug'],true); ?> />
						<span class="description"><?php _e('An additional header, "X-Cache-Engine" will be added when pages are served through WP-FFPC.', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php $this->print_bool( $this->defaults['debug']); ?></span>
					</dd>

					<dt>
						<label for="syslog"><?php _e("Enable syslog messages", WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="checkbox" name="syslog" id="syslog" value="1" <?php checked($this->options['syslog'],true); ?> />
						<span class="description"><?php _e('Writes sets, gets and flushes at INFO level into syslog, using "syslog" function of PHP.', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php $this->print_bool( $this->defaults['syslog']); ?></span>
					</dd>

					<dt>
						<label for="pingback_status"><?php _e("Enable pingback links", WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="checkbox" name="pingback_status" id="pingback_status" value="1" <?php checked($this->options['pingback_status'],true); ?> />
						<span class="description"><?php _e('Enable "X-Pingback" headers in cached pages; will always use accessed hostname as host!', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php $this->print_bool( $this->defaults['pingback_status']); ?></span>
					</dd>

					<dt>
						<label for="sync_protocols"><?php _e("Enable sync protocolls", WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="checkbox" name="sync_protocols" id="sync_protocols" value="1" <?php checked($this->options['sync_protocols'],true); ?> />
						<span class="description"><?php _e('Enable to replace every protocol to the same as in the request for site\'s domain', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php $this->print_bool( $this->defaults['sync_protocols']); ?></span>
					</dd>
				</dl>
				</fieldset>

				<fieldset id="wp-ffpc-exceptions">
				<legend><?php _e( 'Set cache excepions', WP_FFPC_PARAM ); ?></legend>
				<dl>
					<dt>
						<label for="cache_loggedin"><?php _e('Enable cache for logged in users', WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="checkbox" name="cache_loggedin" id="cache_loggedin" value="1" <?php checked($this->options['cache_loggedin'],true); ?> />
						<span class="description"><?php _e('Cache pages even if user is logged in.', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php $this->print_bool( $this->defaults['cache_loggedin']); ?></span>
					</dd>

					<dt>
						<label for="nocache_home"><?php _e("Don't cache home", WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="checkbox" name="nocache_home" id="nocache_home" value="1" <?php checked($this->options['nocache_home'],true); ?> />
						<span class="description"><?php _e('Exclude home page from caching', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php $this->print_bool( $this->defaults['nocache_home']); ?></span>
					</dd>

					<dt>
						<label for="nocache_feed"><?php _e("Don't cache feeds", WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="checkbox" name="nocache_feed" id="nocache_feed" value="1" <?php checked($this->options['nocache_feed'],true); ?> />
						<span class="description"><?php _e('Exclude feeds from caching.', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php $this->print_bool( $this->defaults['nocache_feed']); ?></span>
					</dd>

					<dt>
						<label for="nocache_archive"><?php _e("Don't cache archives", WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="checkbox" name="nocache_archive" id="nocache_archive" value="1" <?php checked($this->options['nocache_archive'],true); ?> />
						<span class="description"><?php _e('Exclude archives from caching.', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php $this->print_bool( $this->defaults['nocache_archive']); ?></span>
					</dd>

					<dt>
						<label for="nocache_single"><?php _e("Don't cache posts (and single-type entries)", WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="checkbox" name="nocache_single" id="nocache_single" value="1" <?php checked($this->options['nocache_single'],true); ?> />
						<span class="description"><?php _e('Exclude singles from caching.', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php $this->print_bool( $this->defaults['nocache_single']); ?></span>
					</dd>

					<dt>
						<label for="nocache_page"><?php _e("Don't cache pages", WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="checkbox" name="nocache_page" id="nocache_page" value="1" <?php checked($this->options['nocache_page'],true); ?> />
						<span class="description"><?php _e('Exclude pages from caching.', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php $this->print_bool( $this->defaults['nocache_page']); ?></span>
					</dd>
				</dl>
				</fieldset>

				<fieldset id="wp-ffpc-apc">
				<legend><?php _e('Settings for APC', WP_FFPC_PARAM); ?></legend>
				<dl>

					<dt>
						<label for="apc_compress"><?php _e("Compress entries", WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="checkbox" name="apc_compress" id="apc_compress" value="1" <?php checked($this->options['apc_compress'],true); ?> />
						<span class="description"><?php _e('Try to compress APC entries. Requires PHP ZLIB.', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php $this->print_bool( $this->defaults['apc_compress']); ?></span>
					</dd>

				</dl>
				</fieldset>

				<fieldset id="wp-ffpc-memcached">
				<legend><?php _e('Settings for memcached backend', WP_FFPC_PARAM); ?></legend>
				<dl>
					<dt>
						<label for="host"><?php _e('Host:Port', WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="text" name="hosts" id="hosts" value="<?php echo $this->options['hosts']; ?>" />
						<span class="description"><?php _e('List all valid Hostname:Port[,Hostname:Port] for memcached server(s)', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php echo $this->defaults['host']; ?></span>
					</dd>
					<dt>
						<label for="persistent"><?php _e('Persistent memcache connections', WP_FFPC_PARAM); ?></label>
					</dt>
					<dd>
						<input type="checkbox" name="persistent" id="persistent" value="1" <?php checked($this->options['persistent'],true); ?> />
						<span class="description"><?php _e('Make all memcache connections persistent. Be carefull with this setting, always test the outcome.', WP_FFPC_PARAM); ?></span>
						<span class="default"><?php _e('Default ', WP_FFPC_PARAM); ?>: <?php echo $this->defaults['persistent']; ?></span>
					</dd>
				</dl>
				</fieldset>

				<fieldset id="wp-ffpc-nginx">
				<legend><?php _e('Sample config for nginx to utilize the data entries', WP_FFPC_PARAM); ?></legend>
				<?php
					$search = array( 'DATAPREFIX', 'MEMCACHEDHOST', 'MEMCACHEDPORT');
					$replace = array ( $this->options['prefix_data'], $this->options['host'], $this->options['port'] );
					$nginx = file_get_contents ( WP_FFPC_DIR .'/nginx-sample.conf' );
					$nginx = str_replace ( $search , $replace , $nginx );

					foreach ( array_keys( $this->options['servers'] ) as $server ) {
						$nginx_servers .= "		server ". $server .";\n";
					}

					$nginx = str_replace ( 'MEMCACHED_SERVERS' , $nginx_servers , $nginx );

				?>
				<pre><?php echo $nginx; ?></pre>
				</fieldset>

				<p class="clearcolumns"><input class="button-primary" type="submit" name="<?php echo WP_FFPC_PARAM; ?>-save" id="<?php echo WP_FFPC_PARAM; ?>-save" value="Save Changes" /></p>
			</form>
			<?php

		}

		/**
		 * generates cache type select box
		 *
		 * @param $current
		 * 	the active or required size's identifier
		 *
		 * @param $returntext
		 * 	boolean: is true, the description will be returned of $current size
		 *
		 * @return
		 * 	prints either description of $current
		 * 	or option list for a <select> input field with $current set as active
		 *
		 */
		function cache_type ( $current , $returntext = false ) {

			$e = array (
				'apc' => 'use APC as store',
				'memcache' => 'use memcached server with Memcache extension',
				'memcached' => 'use memcached server with Memcached extension',
			);

			$this->print_select_options ( $e , $current , $returntext );

		}

		/**
		 * see if we are using network-wide setup or not
		 *
		 */
		function check_for_network( ) {
			if ( is_multisite() ) {
				$plugins = get_site_option( 'active_sitewide_plugins');
				if ( isset($plugins['wp-ffpc/wp-ffpc.php']) ) {
					$this->network = true;
				}
			}
		}

		/**
		 * deactivation hook: clear advanced-cache config file
		 *
		 */
		function deactivate ( ) {
			if (@file_exists (WP_FFPC_ACACHE_MAIN_FILE))
				@unlink (WP_FFPC_ACACHE_MAIN_FILE);
		}

		/**
		 * invalidate cache
		 */
		function invalidate ( $post_id ) {
			wp_ffpc_clear ( $post_id );
		}

		/**
		 * generates invalidation method select box
		 *
		 * @param $current
		 * 	the active or required size's identifier
		 *
		 * @param $returntext
		 * 	boolean: is true, the description will be returned of $current size
		 *
		 * @return
		 * 	prints either description of $current
		 * 	or option list for a <select> input field with $current set as active
		 *
		 */
		function invalidation_method ( $current , $returntext = false ) {

			$e = array (
				0 => 'all cached pages (WARNING! Flushes _all_ cached entrys! )',
				1 => 'only modified post',
			);

			$this->print_select_options ( $e , $current , $returntext );
		}

		/**
		 * generates main advanced-cache system-wide config file
		 *
		 */
		function generate_config() {

			$acache = WP_FFPC_ACACHE_MAIN_FILE;
			/* is file currently exists, delete it*/
			if ( @file_exists( $acache ))
				unlink ($acache);

			/* is deletion was unsuccessful, die, we have no rights to do that */
			if ( @file_exists( $acache ))
				return false;

			$string = '<?php'. "\n" . 'global '. WP_FFPC_CONFIG_VAR .";\n";

			$string .= WP_FFPC_CONFIG_VAR .' = ' .var_export( $this->options , true ) . ';';

			$string .= "\n\ninclude_once ('" . WP_FFPC_ACACHE_COMMON_FILE . "');\ninclude_once ('" . WP_FFPC_ACACHE_INC_FILE . "');\n";

			file_put_contents($acache, $string);
			return true;
		}


		/**
		 * parameters array with default values;
		 *
		 */
		function get_options ( ) {
			$defaults = array (
				'hosts'=>'127.0.0.1:11211',
				'expire'=>300,
				'invalidation_method'=>0,
				'prefix_meta' =>'meta-',
				'prefix_data' =>'data-',
				'charset' => 'utf-8',
				'pingback_status'=> false,
				'debug' => true,
				'syslog' => false,
				'cache_type' => 'memcached',
				'cache_loggedin' => false,
				'nocache_home' => false,
				'nocache_feed' => false,
				'nocache_archive' => false,
				'nocache_single' => false,
				'nocache_page' => false,
				'apc_compress' => false,
				'sync_protocols' => false,
				'persistent' => false,
				'servers' => array (
					'host' => '127.0.0.1',
					'port' => 11211
				),
			);

			$this->defaults = $defaults;

			$this->options = get_site_option( WP_FFPC_PARAM , $defaults, false );

			$this->split_hosts();

		}

		/**
		 * prints `true` or `false` depending on a bool variable.
		 *
		 * @param $val
		 * 	The boolen variable to print status of.
		 *
		 */
		function print_bool ( $val ) {
			$bool = $val? 'true' : 'false';
			echo $bool;
		}

		/**
		 * select field processor
		 *
		 * @param sizes
		 * 	array to build <option> values of
		 *
		 * @param $current
		 * 	the current resize type
		 *
		 * @param $returntext
		 * 	boolean: is true, the description will be returned of $current type
		 *
		 * @return
		 * 	prints either description of $current
		 * 	or option list for a <select> input field with $current set as active
		 *
		 */
		function print_select_options ( $sizes, $current, $returntext=false ) {

			if ( $returntext )
			{
				_e( $sizes[ $current ] , WP_FFPC_PARAM);
				return;
			}

			foreach ($sizes as $ext=>$name)
			{
				?>
				<option value="<?php echo $ext ?>" <?php selected( $ext , $current ); ?>>
					<?php _e( $name , WP_FFPC_PARAM); ?>
				</option>
				<?php
			}

		}

		/**
		 * function to be able to store redirects
		 */
		function redirect_canonical($redirect_url, $requested_url) {
			global $wp_nmc_redirect;
			$wp_nmc_redirect = $redirect_url;
			return $redirect_url;
		}

		/**
		 * save settings function
		 *
		 */
		function save_settings ( $firstrun = false ) {

			$defaults = $this->defaults;

			foreach ( $defaults as $key => $default )
			{
				if (!empty($_POST[$key]))
				{
					$update = $_POST[$key];
					if ( strlen( $update ) !=0 && !is_numeric($update) )
						$update = stripslashes($update);
				}
				elseif ( ( empty( $_POST[$name] ) && is_bool ( $default ) ) || is_int( $default ) )
				{
					$update = 0;
				}
				else
				{
					$update = $this->options[$key];
				}

				$options[$key] = $update;
			}

			$this->options = $options;
			$this->split_hosts();

			update_site_option( WP_FFPC_PARAM , $this->options );

			$this->invalidate('system_flush');

			if ( ! $firstrun )
				$this->generate_config();

		}

		function split_hosts ( ) {

			$servers = explode( WP_FFPC_SERVER_LIST_SEPARATOR , $this->options['hosts']);
			$good_servers = array();

			foreach ( $servers as $server_num => $server_string ) {
				$separator = strpos( $server_string , WP_FFPC_SERVER_SEPARATOR );
				$host = substr( $server_string, 0, $separator );
				$port = substr( $server_string, $separator + 1 );

				if ( !empty ( $host )  && !empty($port) && is_numeric($port) ) {
					$good_servers[$server_string] = array (
						'host' => $host,
						'port' => $port
					);
				}
			}

			if ( !empty ( $good_servers ))
				$this->options['servers'] = $good_servers;
		}

		/**
		 * clean up at uninstall
		 *
		 */
		function uninstall ( ) {
			delete_site_option( WP_FFPC_PARAM );
		}

	}
}

/**
 * instantiate the class
 */
$wp_nmc = new WPFFPC();


?>