-
Notifications
You must be signed in to change notification settings - Fork 0
/
broken_links.php
121 lines (103 loc) · 3.3 KB
/
broken_links.php
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
<?php
/**
* Plugin broken_links.
*
* Allow users to easily remove broken links.
*/
use \Shaarli\Security\SessionManager;
use \Shaarli\Security\LoginManager;
use \Shaarli\Router;
use Shaarli\Config\ConfigManager;
use Shaarli\Plugin\PluginManager;
use \Shaarli\Bookmark\LinkDB;
use \Shaarli\History;
/**
* Display an error if the plugin is active a no action is configured.
*
* @param $conf ConfigManager instance
*
* @return array|null The errors array or null of there is none.
*/
function broken_links_init($conf)
{
$action = $conf->get('plugins.DEFAULT_ACTION');
if (empty($action)) {
$error = t('Broken links plugin error: ' .
'Please define default action in the plugin administration page.');
return array($error);
}
}
/**
* When plugin parameters are saved, we check all links.
*
* @param array $data $_POST array
*
* @return array Updated $_POST array
*/
function hook_broken_links_save_plugin_parameters($data)
{
$conf = new ConfigManager();
$sessionManager = new SessionManager($_SESSION, $conf);
$loginManager = new LoginManager($conf, $sessionManager);
$clientIpId = client_ip_id($_SERVER);
$loginManager->generateStaySignedInToken($_SERVER['REMOTE_ADDR']);
$loginManager->checkLoginState($_COOKIE, $clientIpId);
$history = new History($conf->get('resource.history'));
$action = $data['DEFAULT_ACTION'];
if (!empty($action)) {
$pluginManager = new PluginManager($conf);
$pluginManager->load($conf->get('general.enabled_plugins'));
$LINKSDB = new LinkDB(
$conf->get('resource.datastore'),
true,
$conf->get('privacy.hide_public_links')
);
foreach ($LINKSDB as $id=>$link){
$handle = curl_init($link["url"]);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if(in_array($httpCode,[0,403,404,410])) {
if ($action == "TAG"){
if (stripos($link["tags"],"@404")===false){
$link["tags"] = trim(trim($link["tags"])." @404");
}
$pluginManager->executeHooks('save_link', $link);
$LINKSDB[$id] = $link;
$LINKSDB->save($conf->get('resource.page_cache'));
}else{
$pluginManager->executeHooks('delete_link', $link);
unset($LINKSDB[$id]);
$LINKSDB->save($conf->get('resource.page_cache')); // save to disk
}
}else{
//Remove 404 Tag
$link["tags"] = str_replace("@404","",$link["tags"]);
$pluginManager->executeHooks('save_link', $link);
$LINKSDB[$id] = $link;
$LINKSDB->save($conf->get('resource.page_cache'));
}
curl_close($handle);
}
//Disable the plugin
$pluginMeta = $pluginManager->getPluginsMeta();
// Split plugins into 2 arrays: ordered enabled plugins and disabled.
$enabledPlugins = array_filter($pluginMeta, function ($v) {
return $v['order'] !== false;
});
// Load parameters.
$enabledPlugins = load_plugin_parameter_values($enabledPlugins, $conf->get('plugins', array()));
$activePlugins = [];
foreach ($enabledPlugins as $key=>$plugin){
if ($key != "broken_links"){
$activePlugins[]= $key;
}
}
$conf->set('general.enabled_plugins', $activePlugins);
$conf->write($loginManager->isLoggedIn());
$history->updateSettings();
header('Location: ?do='. Router::$PAGE_PLUGINSADMIN);
exit();
}
return $data;
}