Reformating

This commit is contained in:
Eric van der Vlist 2020-05-07 10:21:10 +02:00
parent 0d4af6419a
commit b7c70cfd10
1 changed files with 558 additions and 568 deletions

164
owark.php
View File

@ -1,56 +1,62 @@
<?php
/* Copyright 2011-2020 Eric van der Vlist (vdv@dyomedea.com)
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 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.
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
*/
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
*/
/*
Plugin Name: owark
Plugin URI: http://owark.org
Description: Tired of broken links? Archive yours with owark, the Open Web Archive!
Version: 0.2
Author: Eric van der Vlist
Author URI: http://eric.van-der-vlist.com
License: GLP2
*/
Plugin Name: owark
Plugin URI: http://owark.org
Description: Tired of broken links? Archive yours with owark, the Open Web Archive!
Version: 1.0
Author: Eric van der Vlist
Author URI: http://eric.van-der-vlist.com
License: GLP2
*/
/*
Log
*/
if ( ! function_exists('print_r_log')) {
function print_r_log ( $log ) {
Log
*/
if (!function_exists('print_r_log')) {
function print_r_log($log) {
$caller_strace = debug_backtrace()[1];
if ( is_array( $log ) || is_object( $log ) ) {
error_log($caller_strace['file'] . '/#' .$caller_strace['line'] . ':');
error_log( print_r( $log, true ) );
if (is_array($log) || is_object($log)) {
error_log($caller_strace['file'] . '/#' . $caller_strace['line'] . ':');
error_log(print_r($log, true));
} else {
error_log($caller_strace['file'] . '/#' .$caller_strace['line'] . ': ' . $log );
error_log($caller_strace['file'] . '/#' . $caller_strace['line'] . ': ' . $log);
}
}
}
if ( ! function_exists('log_function_call')) {
function log_function_call () {
if (!function_exists('log_function_call')) {
function log_function_call() {
$caller_strace = debug_backtrace()[1];
error_log((isset($caller_strace['file']) ? $caller_strace['file'] : '<undefined>') . '/#' . (isset($caller_strace['line']) ? $caller_strace['line'] : '<undefined>') . ' function: ' . (isset($caller_strace['function']) ? $caller_strace['function'] : '<undefined>') . '(');
foreach ($caller_strace['args'] as $arg) {
error_log(' * ' . gettype($arg) . ': ' . print_r( $arg, true ));
error_log(' * ' . gettype($arg) . ': ' . print_r($arg, true));
}
}
}
if ( ! function_exists('archives_dir')) {
if (!function_exists('archives_dir')) {
function archives_dir() {
if (defined('OWARK_ARCHIVES_REL_PATH')) {
return WP_CONTENT_DIR . '/' . OWARK_ARCHIVES_REL_PATH;
@ -58,9 +64,11 @@ if ( ! function_exists('archives_dir')) {
return WP_PLUGIN_DIR . '/archives';
}
}
}
if ( ! function_exists('archives_url')) {
if (!function_exists('archives_url')) {
function archives_url() {
if (defined('OWARK_ARCHIVES_REL_PATH')) {
return WP_CONTENT_URL . '/' . OWARK_ARCHIVES_REL_PATH;
@ -68,9 +76,11 @@ if ( ! function_exists('archives_url')) {
return WP_PLUGIN_URL . '/archives';
}
}
}
if (!class_exists("Owark")) {
class Owark {
private $broken_links = array();
@ -100,28 +110,25 @@ if (!class_exists("Owark")) {
// Using a filter instead of an action to create the rewrite rules.
// Write rules -> Add query vars -> Recalculate rewrite rules
add_filter('rewrite_rules_array', array($this, 'create_rewrite_rules'));
add_filter('query_vars',array($this, 'add_query_vars'));
add_filter('query_vars', array($this, 'add_query_vars'));
// Recalculates rewrite rules during admin init to save resources.
// Could probably run it once as long as it isn't going to change or check the
// $wp_rewrite rules to see if it's active.
add_filter('admin_init', array($this, 'flush_rewrite_rules'));
add_action( 'template_redirect', array($this, 'template_redirect_intercept') );
add_action('template_redirect', array($this, 'template_redirect_intercept'));
add_filter ( 'the_content', array($this, 'content_filter'), 20);
add_filter ( 'comment_text', array($this, 'comment_filter'), 20, 2);
add_filter ( 'get_comment_author_url', array($this, 'comment_author_url_filter'), 20, 1);
add_filter('the_content', array($this, 'content_filter'), 20);
add_filter('comment_text', array($this, 'comment_filter'), 20, 2);
add_filter('get_comment_author_url', array($this, 'comment_author_url_filter'), 20, 1);
add_action('owark_schedule_event', array('Owark', 'schedule'), 10, 2);
if ( !wp_next_scheduled( 'owark_schedule_event', array('occurrences' => 30, 'version' => $this->version) ) ) {
if (!wp_next_scheduled('owark_schedule_event', array('occurrences' => 30, 'version' => $this->version))) {
wp_schedule_event(time(), 'hourly', 'owark_schedule_event', array('occurrences' => 30, 'version' => $this->version));
}
}
function Owark()
{
function Owark() {
// PHP4-style constructor.
// This will NOT be invoked, unless a sub-class that extends `foo` calls it.
// In that case, call the new-style constructor to keep compatibility.
@ -136,15 +143,15 @@ if (!class_exists("Owark")) {
*
*
*/
function sanity_checks(){
function sanity_checks() {
// Install or upgrade tables if needed
$installed_ver = get_option( "owark_db_version" );
$installed_ver = get_option("owark_db_version");
$update_required = ($installed_ver != $this->db_version);
print_r_log("update_required: $update_required ($installed_ver vs {$this->db_version})");
global $wpdb;
$table = $wpdb->prefix."owark";
$table = $wpdb->prefix . "owark";
if ($installed_ver == '0.1') {
// In version 0.1 final URLs where used but the broken link checkers update these URLs when a link is detected broken
// Let's replace these URLS by raw URLs...
@ -157,7 +164,7 @@ if (!class_exists("Owark")) {
owark.url = instances.raw_url ";
$wpdb->query($sql);
$installed_ver = '1.0';
update_option( "owark_db_version", $installed_ver );
update_option("owark_db_version", $installed_ver);
}
if ($installed_ver != $this->db_version) {
print_r_log("Database upgrade from $installed_ver to {$this->version}");
@ -172,8 +179,7 @@ if (!class_exists("Owark")) {
KEY url (`url`(150)) )";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
update_option( "owark_db_version", $this->db_version );
update_option("owark_db_version", $this->db_version);
}
if ($update_required) {
@ -224,16 +230,15 @@ if (!class_exists("Owark")) {
$wpdb->query($sql);
$this->notices = "<div class=\"updated fade\"><p><strong>The owark table has been installed or upgraded to version {$this->db_version}</strong></p></div>";
}
// Check that the broken link checker is installed
if (!function_exists('get_plugins'))
require_once (ABSPATH."wp-admin/includes/plugin.php");
require_once (ABSPATH . "wp-admin/includes/plugin.php");
$blc = 'not-found';
foreach(get_plugins() as $plugin_file => $plugin_data) {
foreach (get_plugins() as $plugin_file => $plugin_data) {
if ($plugin_data['Title'] == 'Broken Link Checker') {
if (is_plugin_active($plugin_file)) {
$blc = 'active';
@ -257,15 +262,15 @@ if (!class_exists("Owark")) {
if (!is_dir($archives_dir)) {
$this->notices = $this->notices . "<div class=\"error fade\"><p><strong>The Open Web Archive has not been able to create the folder /archives in its installation directory. Please create it by hand and make it writable for the web server.</strong></p></div>";
}
} elseif (! is_writable($archives_dir)) {
} elseif (!is_writable($archives_dir)) {
$this->notices = $this->notices . "<div class=\"error fade\"><p><strong>The Open Web Archive needs a writable folder /archives in its installation directory. Please make it writable for the web server.</strong></p></div>";
}
// Check that we can execute commands
if ( ini_get('disable_functions') ) {
if (ini_get('disable_functions')) {
$not_allowed = ini_get('disable_functions');
if ( stristr($not_allowed, 'exec') ) {
if (stristr($not_allowed, 'exec')) {
$this->notices = $this->notices . "<div class=\"error fade\"><p><strong>The Open Web Archives requires that exec() is allowed to run wget and retrieve the pages to archive.</strong></p></div>";
}
}
@ -276,21 +281,20 @@ if (!class_exists("Owark")) {
exec('/usr/bin/wget -V', $output);
if ( empty($output) ) {
if (empty($output)) {
$this->notices = $this->notices .
"<div class=\"error fade\"><p><strong>The Open Web Archives is not able to run GNU wget and retrieve the pages to archive. Please check that wget is installed and on the default path.</strong></p></div>";
}
// We need as least version 1.11 or higher
$helper = preg_match('/GNU Wget ([0-9\.]+) /', $output[0], $wget_version);
if ( $wget_version[0] < '1.11' ) {
if ($wget_version[0] < '1.11') {
$this->notices = $this->notices . "<div class=\"error fade\"><p><strong>The Open Web Archives needs GNU wget version 1.11 or higher.</strong><br />Version read: {$wget_version[0]}</p></div>";
}
if ($this->notices != '') {
add_action('admin_notices', array($this, 'admin_notices'));
}
}
/**
@ -301,10 +305,9 @@ if (!class_exists("Owark")) {
*
*
*/
function admin_notices(){
function admin_notices() {
echo $this->notices;
}
/**
@ -328,7 +331,7 @@ if (!class_exists("Owark")) {
*
*/
function get_archive_url($archive_id) {
return home_url().'/owark/'.$archive_id;
return home_url() . '/owark/' . $archive_id;
}
/**
@ -342,7 +345,7 @@ if (!class_exists("Owark")) {
function management_page() {
//must check that the user has the required capability
if (!current_user_can('edit_others_posts')) {
wp_die( __('You do not have sufficient permissions to access this page.') );
wp_die(__('You do not have sufficient permissions to access this page.'));
}
global $wpdb;
@ -380,8 +383,6 @@ if (!class_exists("Owark")) {
echo '</tbody>';
echo '</table>';
}
/**
@ -394,7 +395,7 @@ if (!class_exists("Owark")) {
*/
function create_rewrite_rules($rules) {
global $wp_rewrite;
$newRule = array('owark/(.+)' => 'index.php?owark='.$wp_rewrite->preg_index(1));
$newRule = array('owark/(.+)' => 'index.php?owark=' . $wp_rewrite->preg_index(1));
$newRules = $newRule + $rules;
return $newRules;
}
@ -529,7 +530,6 @@ if (!class_exists("Owark")) {
foreach ($results as $link) {
$this->broken_links[$link->url] = $link->id;
}
}
@ -538,7 +538,7 @@ if (!class_exists("Owark")) {
}
// Regexp : see http://stackoverflow.com/questions/2609095/hooking-into-comment-text-to-add-surrounding-tag
$replaced = preg_replace_callback('/(<a.*?href\s*=\s*["\'])([^"\'>]+)(["\'][^>]*>.*?<\/a>)/si', array( $this, 'replace_a_link'), $content);
$replaced = preg_replace_callback('/(<a.*?href\s*=\s*["\'])([^"\'>]+)(["\'][^>]*>.*?<\/a>)/si', array($this, 'replace_a_link'), $content);
print_r_log("replaced: $replaced");
return $replaced;
}
@ -554,13 +554,12 @@ if (!class_exists("Owark")) {
function replace_a_link($matches) {
log_function_call();
if (array_key_exists($matches[2], $this->broken_links)) {
return $matches[1].$this->get_archive_url($this->broken_links[$matches[2]]).$matches[3];
return $matches[1] . $this->get_archive_url($this->broken_links[$matches[2]]) . $matches[3];
} else {
return $matches[0];
}
}
/**
* Display an archive page
*
@ -590,7 +589,7 @@ if (!class_exists("Owark")) {
// The file name is either index.html or guessed from the URL
if ($link->url[strlen($link->url) - 1] == '/') {
$file_location = $loc .'/index.html';
$file_location = $loc . '/index.html';
} else {
$parts = explode($link->url, '/');
$file_location = $loc . $parts[count($parts) - 1] . '.html';
@ -602,7 +601,7 @@ if (!class_exists("Owark")) {
if ($dir) {
while (false !== ($file = readdir($dir))) {
if ('.html' === substr($file, strlen($file) - 5)) {
$file_location = $loc.'/' . $file;
$file_location = $loc . '/' . $file;
break;
}
}
@ -648,7 +647,7 @@ if (!class_exists("Owark")) {
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-Type" content="text/html; charset='.$encoding.'">';
<meta http-equiv="Content-Type" content="text/html; charset=' . $encoding . '">';
echo "<base href=\"{$arc_base}/\">";
echo '<div style="background:#fff;border:1px solid #999;margin:-1px -1px 0;padding:0;">';
@ -662,7 +661,6 @@ if (!class_exists("Owark")) {
$f = fopen($file_location, "r");
echo $content;
echo '</div>';
}
/**
@ -677,8 +675,8 @@ if (!class_exists("Owark")) {
log_function_call();
$archiving = get_option( 'owark_archiving', false);
if (! $archiving) {
$archiving = get_option('owark_archiving', false);
if (!$archiving) {
update_option('owark_archiving', true);
} else {
return;
@ -696,7 +694,7 @@ if (!class_exists("Owark")) {
if ($url != NULL) {
$date = date('c');
$relpath = str_replace('%2F', '/', urlencode(preg_replace('/https?:\/\//', '', $url->url))) . '/' . $date;
$path = archives_dir()."/$relpath";
$path = archives_dir() . "/$relpath";
//mkdir($path, $recursive=true);
$output = array();
@ -717,24 +715,16 @@ if (!class_exists("Owark")) {
if ($occurrences > 0) {
wp_schedule_single_event(time() + 90, 'owark_schedule_event', array('occurrences' => $occurrences - 1, 'version' => $version));
}
}
delete_option('owark_archiving');
}
}
}
}
if (class_exists("Owark")) {
if (class_exists("Owark")) {
$owark = new Owark();
}
?>
}
?>