Broken Link Checker - Version 0.2.5

Version Description

Download this release

Release Info

Developer whiteshadow
Plugin Icon 128x128 Broken Link Checker
Version 0.2.5
Comparing to
See all releases

Code changes from version 0.3 to 0.2.5

Files changed (3) hide show
  1. broken-link-checker.php +188 -14
  2. readme.txt +4 -3
  3. wsblc_ajax.php +62 -90
broken-link-checker.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Broken Link Checker
4
  Plugin URI: http://w-shadow.com/blog/2007/08/05/broken-link-checker-for-wordpress/
5
  Description: Checks your posts for broken links and missing images and notifies you on the dashboard if any are found.
6
- Version: 0.2.5
7
  Author: Janis Elsts
8
  Author URI: http://w-shadow.com/blog/
9
  */
@@ -20,10 +20,11 @@ class ws_broken_link_checker {
20
  var $options_name='wsblc_options';
21
  var $postdata_name;
22
  var $linkdata_name;
23
- var $version='0.2.5';
24
  var $myfile='';
25
  var $myfolder='';
26
  var $mybasename='';
 
27
 
28
 
29
  function ws_broken_link_checker() {
@@ -32,6 +33,7 @@ class ws_broken_link_checker {
32
  $this->postdata_name=$wpdb->prefix . "blc_postdata";
33
  $this->linkdata_name=$wpdb->prefix . "blc_linkdata";
34
  $this->options=get_option($this->options_name);
 
35
 
36
  $my_file = str_replace('\\', '/',__FILE__);
37
  $my_file = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', $my_file);
@@ -47,6 +49,13 @@ class ws_broken_link_checker {
47
  add_action('admin_footer', array(&$this,'admin_footer'));
48
  add_action('admin_print_scripts', array(&$this,'admin_print_scripts'));
49
  add_action('activity_box_end', array(&$this,'activity_box'));
 
 
 
 
 
 
 
50
  }
51
 
52
  function admin_footer(){
@@ -66,6 +75,126 @@ class ws_broken_link_checker {
66
  <?php
67
  }
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  function activity_box(){
70
  ?>
71
  <!-- wsblc activity box -->
@@ -149,17 +278,21 @@ class ws_broken_link_checker {
149
  function activation(){
150
  global $wpdb;
151
 
 
 
 
 
 
 
 
 
 
 
152
  if(!is_array($this->options)){
153
-
154
- //set default options
155
- $this->options=array(
156
- 'version' => $this->version,
157
- 'max_work_session' => 27,
158
- 'check_treshold' => 72
159
- );
160
-
161
- update_option($this->options_name, $this->options);
162
  };
 
 
163
 
164
  require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
165
 
@@ -226,6 +359,14 @@ class ws_broken_link_checker {
226
  $this->options['check_treshold']=$new_check_treshold;
227
  }
228
 
 
 
 
 
 
 
 
 
229
  update_option($this->options_name,$this->options);
230
  }
231
 
@@ -277,6 +418,31 @@ class ws_broken_link_checker {
277
  </td>
278
  </tr>
279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280
  <tr valign="top">
281
  <th scope="row">Work Session Length:</th>
282
  <td>
@@ -326,9 +492,7 @@ class ws_broken_link_checker {
326
  <th scope="col">Link Text</th>
327
  <th scope="col">URL</th>
328
 
329
- <th scope="col"></th>
330
- <th scope="col"></th>
331
- <th scope="col"></th>
332
 
333
  </tr>
334
  </thead>
@@ -349,6 +513,9 @@ class ws_broken_link_checker {
349
  <td><a href='post.php?action=edit&amp;post=$link->post_id' class='edit'>Edit Post</a></td>
350
  <td><a href='javascript:void(0);' class='delete'
351
  onclick='discardLinkMessage($link->id);return false;' );' title='Discard This Message'>Discard</a></td>
 
 
 
352
  </tr>";
353
 
354
  }
@@ -365,6 +532,13 @@ class ws_broken_link_checker {
365
  { method:'get' });
366
  $('link-'+link_id).hide();
367
  }
 
 
 
 
 
 
 
368
  </script>
369
  </div>
370
  <?php
3
  Plugin Name: Broken Link Checker
4
  Plugin URI: http://w-shadow.com/blog/2007/08/05/broken-link-checker-for-wordpress/
5
  Description: Checks your posts for broken links and missing images and notifies you on the dashboard if any are found.
6
+ Version: 0.3
7
  Author: Janis Elsts
8
  Author URI: http://w-shadow.com/blog/
9
  */
20
  var $options_name='wsblc_options';
21
  var $postdata_name;
22
  var $linkdata_name;
23
+ var $version='0.3';
24
  var $myfile='';
25
  var $myfolder='';
26
  var $mybasename='';
27
+ var $siteurl;
28
 
29
 
30
  function ws_broken_link_checker() {
33
  $this->postdata_name=$wpdb->prefix . "blc_postdata";
34
  $this->linkdata_name=$wpdb->prefix . "blc_linkdata";
35
  $this->options=get_option($this->options_name);
36
+ $this->siteurl = get_option('siteurl');
37
 
38
  $my_file = str_replace('\\', '/',__FILE__);
39
  $my_file = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', $my_file);
49
  add_action('admin_footer', array(&$this,'admin_footer'));
50
  add_action('admin_print_scripts', array(&$this,'admin_print_scripts'));
51
  add_action('activity_box_end', array(&$this,'activity_box'));
52
+
53
+ if ($this->options['mark_broken_links']){
54
+ add_filter('the_content', array(&$this,'the_content'));
55
+ if (!empty($this->options['broken_link_css'])){
56
+ add_action('wp_head', array(&$this,'header_css'));
57
+ }
58
+ }
59
  }
60
 
61
  function admin_footer(){
75
  <?php
76
  }
77
 
78
+ function header_css(){
79
+ echo '<style>',$this->options['broken_link_css'],'</style>';
80
+ }
81
+
82
+ function the_content($content){
83
+ global $post, $wpdb;
84
+ if (empty($post)) return $content;
85
+
86
+ $sql="SELECT url from $this->linkdata_name WHERE post_id = $post->ID AND broken<>0";
87
+ $rows=$wpdb->get_results($sql, ARRAY_A);
88
+ if($rows && (count($rows)>0)){
89
+ //some rows found
90
+ $this->links_to_remove = array_map(
91
+ create_function('$elem', 'return $elem["url"];'),
92
+ $rows);
93
+ $url_pattern='/(<a[\s]+[^>]*href\s*=\s*[\"\']?)([^\'\" >]+)([\'\"]+[^<>]*)(>)((?sU).*)(<\/a>)/i';
94
+ $content = preg_replace_callback($url_pattern, array(&$this,'mark_broken_links'), $content);
95
+ };
96
+
97
+ //print_r($post);
98
+ return $content;
99
+ }
100
+
101
+ function mark_broken_links($matches){
102
+ $url = $this->normalize_url(html_entity_decode($matches[2])) ;
103
+ if(in_array($url, $this->links_to_remove)){
104
+ return $matches[1].$matches[2].$matches[3].' class="broken_link"'.$matches[4].
105
+ $matches[5].$matches[6];
106
+ } else {
107
+ return $matches[0];
108
+ }
109
+ }
110
+
111
+ function normalize_url($url){
112
+ $parts=@parse_url($url);
113
+ if(!$parts) return false;
114
+
115
+ $url = html_entity_decode($url);
116
+ $url=preg_replace(
117
+ array('/([\?&]PHPSESSID=\w+)$/i',
118
+ '/(#[^\/]*)$/',
119
+ '/&amp;/',
120
+ '/^(javascript:.*)/i',
121
+ '/([\?&]sid=\w+)$/i'
122
+ ),
123
+ array('','','&','',''),
124
+ $url);
125
+
126
+ $url=trim($url);
127
+ if($url=='') return false;
128
+
129
+ // turn relative URLs into absolute URLs
130
+ $url = $this->relative2absolute($this->siteurl, $url);
131
+ return $url;
132
+ }
133
+
134
+ function relative2absolute($absolute, $relative) {
135
+ $p = @parse_url($relative);
136
+ if(!$p) {
137
+ //WTF? $relative is a seriously malformed URL
138
+ return false;
139
+ }
140
+ if(isset($p["scheme"])) return $relative;
141
+
142
+ $parts=(parse_url($absolute));
143
+
144
+ if(substr($relative,0,1)=='/') {
145
+ $cparts = (explode("/", $relative));
146
+ array_shift($cparts);
147
+ } else {
148
+ if(isset($parts['path'])){
149
+ $aparts=explode('/',$parts['path']);
150
+ array_pop($aparts);
151
+ $aparts=array_filter($aparts);
152
+ } else {
153
+ $aparts=array();
154
+ }
155
+
156
+ $rparts = (explode("/", $relative));
157
+
158
+ $cparts = array_merge($aparts, $rparts);
159
+ foreach($cparts as $i => $part) {
160
+ if($part == '.') {
161
+ unset($cparts[$i]);
162
+ } else if($part == '..') {
163
+ unset($cparts[$i]);
164
+ unset($cparts[$i-1]);
165
+ }
166
+ }
167
+ }
168
+ $path = implode("/", $cparts);
169
+
170
+ $url = '';
171
+ if($parts['scheme']) {
172
+ $url = "$parts[scheme]://";
173
+ }
174
+ if(isset($parts['user'])) {
175
+ $url .= $parts['user'];
176
+ if(isset($parts['pass'])) {
177
+ $url .= ":".$parts['pass'];
178
+ }
179
+ $url .= "@";
180
+ }
181
+ if(isset($parts['host'])) {
182
+ $url .= $parts['host']."/";
183
+ }
184
+ $url .= $path;
185
+
186
+ return $url;
187
+ }
188
+
189
+ function is_excluded($url){
190
+ foreach($this->options['exclusion_list'] as $excluded_word){
191
+ if (stristr($url, $excluded_word)){
192
+ return true;
193
+ }
194
+ }
195
+ return false;
196
+ }
197
+
198
  function activity_box(){
199
  ?>
200
  <!-- wsblc activity box -->
278
  function activation(){
279
  global $wpdb;
280
 
281
+ //set default options
282
+ $defaults = array(
283
+ 'version' => $this->version,
284
+ 'max_work_session' => 27,
285
+ 'check_treshold' => 72,
286
+ 'mark_broken_links' => true,
287
+ 'broken_link_css' => ".broken_link, a.broken_link {\n\ttext-decoration: line-through;\n}",
288
+ 'exclusion_list' => array(),
289
+ );
290
+
291
  if(!is_array($this->options)){
292
+ $this->options = array();
 
 
 
 
 
 
 
 
293
  };
294
+ $this->options = array_merge($defaults, $this->options);
295
+ update_option($this->options_name, $this->options);
296
 
297
  require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
298
 
359
  $this->options['check_treshold']=$new_check_treshold;
360
  }
361
 
362
+ $new_broken_link_css = trim($_POST['broken_link_css']);
363
+ $this->options['broken_link_css'] = $new_broken_link_css;
364
+
365
+ $this->options['mark_broken_links'] = !empty($_POST['mark_broken_links']);
366
+
367
+ $this->options['exclusion_list']=array_filter(preg_split('/[\s,\r\n]+/',
368
+ $_POST['exclusion_list']));
369
+
370
  update_option($this->options_name,$this->options);
371
  }
372
 
418
  </td>
419
  </tr>
420
 
421
+ <tr valign="top">
422
+ <th scope="row">Broken Link CSS:</th>
423
+ <td>
424
+ <input type="checkbox" name="mark_broken_links" id="mark_broken_links"
425
+ <?php if ($this->options['mark_broken_links']) echo ' checked="checked"'; ?>/>
426
+ <label for='mark_broken_links'>Apply <em>class="broken_link"</em> to broken links</label><br/>
427
+ <textarea type="text" name="broken_link_css" id="broken_link_css" cols='40' rows='4'/><?php
428
+ if( isset($this->options['broken_link_css']) )
429
+ echo $this->options['broken_link_css'];
430
+ ?></textarea>
431
+
432
+ </td>
433
+ </tr>
434
+
435
+ <tr valign="top">
436
+ <th scope="row">Exclusion list:</th>
437
+ <td>Don't check links where the URL contains any of these words (one per line):<br/>
438
+ <textarea type="text" name="exclusion_list" id="exclusion_list" cols='40' rows='4'/><?php
439
+ if( isset($this->options['exclusion_list']) )
440
+ echo implode("\n", $this->options['exclusion_list']);
441
+ ?></textarea>
442
+
443
+ </td>
444
+ </tr>
445
+
446
  <tr valign="top">
447
  <th scope="row">Work Session Length:</th>
448
  <td>
492
  <th scope="col">Link Text</th>
493
  <th scope="col">URL</th>
494
 
495
+ <th scope="col" colspan='4'>Action</th>
 
 
496
 
497
  </tr>
498
  </thead>
513
  <td><a href='post.php?action=edit&amp;post=$link->post_id' class='edit'>Edit Post</a></td>
514
  <td><a href='javascript:void(0);' class='delete'
515
  onclick='discardLinkMessage($link->id);return false;' );' title='Discard This Message'>Discard</a></td>
516
+
517
+ <td><a href='javascript:void(0);' class='delete'
518
+ onclick='removeLinkFromPost($link->id);return false;' );' title='Remove the link from the post'>Unlink</a></td>
519
  </tr>";
520
 
521
  }
532
  { method:'get' });
533
  $('link-'+link_id).hide();
534
  }
535
+ function removeLinkFromPost(link_id){
536
+ new Ajax.Request('<?php
537
+ echo get_option( "siteurl" ).'/wp-content/plugins/'.$this->myfolder.'/wsblc_ajax.php?';
538
+ ?>action=remove_link&id='+link_id,
539
+ { method:'get' });
540
+ $('link-'+link_id).hide();
541
+ }
542
  </script>
543
  </div>
544
  <?php
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: whiteshadow
3
  Tags: links, broken, maintenance
4
  Requires at least: 2.0.2
5
  Tested up to: 2.3.3
6
- Stable tag: 0.2.5
7
 
8
  This plugin will check your posts for broken links and missing images in background and notify you on the dashboard if any are found.
9
 
@@ -13,15 +13,16 @@ This plugin is will monitor your blog looking for broken links and let you know
13
  * Checks your posts (and pages) in the background.
14
  * Detects links that don't work and missing images.
15
  * Notifies you on the Dashboard if any are found.
 
16
  * Link checking intervals can be configured.
17
  * New/modified posts are checked ASAP.
18
 
19
  **How To Use It**
20
  The broken links, if any are found, will show up in a new tab of WP admin panel - Manage -> Broken Links. A notification will also appear on the Dashboard.
21
 
22
- There are several buttons for each broken link - "View" and "Edit Post" do exactly what they say and "Discard" will remove the message about a broken link, but not the link itself (so it will show up again later unless you fix it). If references to missing images are found, they will be listed along with the links, with "[image]" in place of link text.
23
 
24
- You can modify the few available options at Options -> Link Checker. You can also see the current checking status there - e.g. how many posts need to be checked and how many links are in the queue. The plugin runs while you have any page of the WordPress admin panel open.
25
 
26
 
27
  == Installation ==
3
  Tags: links, broken, maintenance
4
  Requires at least: 2.0.2
5
  Tested up to: 2.3.3
6
+ Stable tag: 0.3
7
 
8
  This plugin will check your posts for broken links and missing images in background and notify you on the dashboard if any are found.
9
 
13
  * Checks your posts (and pages) in the background.
14
  * Detects links that don't work and missing images.
15
  * Notifies you on the Dashboard if any are found.
16
+ * Makes broken links display differently in posts (optional).
17
  * Link checking intervals can be configured.
18
  * New/modified posts are checked ASAP.
19
 
20
  **How To Use It**
21
  The broken links, if any are found, will show up in a new tab of WP admin panel - Manage -> Broken Links. A notification will also appear on the Dashboard.
22
 
23
+ There are several buttons for each broken link - "View" and "Edit Post" do exactly what they say and "Discard" will remove the message about a broken link, but not the link itself (so it will show up again later unless you fix it). Use "Unlink" to actually remove the link from the post. If references to missing images are found, they will be listed along with the links, with "[image]" in place of link text.
24
 
25
+ You can modify the available options at Options -> Link Checker. You can also see the current checking status there - e.g. how many posts need to be checked and how many links are in the queue. The plugin runs while you have any page of the WordPress admin panel open.
26
 
27
 
28
  == Installation ==
wsblc_ajax.php CHANGED
@@ -14,18 +14,21 @@
14
  return microtime(true)-$execution_start_time;
15
  }
16
 
 
 
 
17
 
18
  if(!is_object($ws_link_checker)) {
19
  die('Fatal error : undefined object; plugin may not be active.');
20
  };
21
 
22
  $url_pattern='/(<a[\s]+[^>]*href\s*=\s*[\"\']?)([^\'\" >]+)([\'\"]+[^<>]*>)((?sU).*)(<\/a>)/i';
 
23
 
24
  $postdata_name=$wpdb->prefix . "blc_postdata";
25
  $linkdata_name=$wpdb->prefix . "blc_linkdata";
26
 
27
  $options=$ws_link_checker->options; //get_option('wsblc_options');
28
- $siteurl=get_option('siteurl');
29
  $max_execution_time=isset($options['max_work_session'])?intval($options['max_work_session']):27;
30
 
31
  // Check for safe mode
@@ -117,7 +120,7 @@
117
  //some unchecked links found
118
  echo "<!-- checking links (rand : ".rand(1,1000).") -->";
119
  foreach ($links as $link) {
120
- if(page_exists_simple($link->url)){
121
  //link OK, remove from queue
122
  $wpdb->query("DELETE FROM $linkdata_name WHERE id=$link->id");
123
  } else {
@@ -135,31 +138,45 @@
135
  die('<!-- /run_check -->');
136
 
137
  } else if ($action=='discard_link'){
 
 
 
138
  $id=intval($_GET['id']);
139
  $wpdb->query("DELETE FROM $linkdata_name WHERE id=$id LIMIT 1");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  };
141
 
142
-
143
  function parse_link($matches, $post_id){
144
- global $wpdb, $siteurl, $linkdata_name;
145
 
146
  $url=$matches[2];
147
 
148
- $parts=@parse_url($url);
149
-
150
- if(!$parts) return false;
151
-
152
- $url=preg_replace(
153
- array('/([\?&]PHPSESSID=\w+)$/i','/(#[^\/]*)$/i', '/&amp;/','/^(javascript:.*)/i','/([\?&]sid=\w+)$/i'),
154
- array('','','&','',''),
155
- $url);
156
-
157
- $url=trim($url);
158
- if($url=='') return false;
159
 
160
- // turn relative URLs into absolute URLs
161
- $url = relative2absolute($siteurl, $url);
162
-
163
  if(strlen($url)>5){
164
  $wpdb->query(
165
  "INSERT INTO $linkdata_name(post_id, url, link_text)
@@ -171,25 +188,12 @@
171
  }
172
 
173
  function parse_image($matches, $post_id){
174
- global $wpdb, $siteurl, $linkdata_name;
175
 
176
  $url=$matches[2];
 
 
177
 
178
- $parts=@parse_url($url);
179
-
180
- if(!$parts) return false;
181
-
182
- $url=preg_replace(
183
- array('/([\?&]PHPSESSID=\w+)$/i','/(#[^\/]*)$/i', '/&amp;/','/^(javascript:.*)/i','/([\?&]sid=\w+)$/i'),
184
- array('','','&','',''),
185
- $url);
186
-
187
- $url=trim($url);
188
- if($url=='') return false;
189
-
190
- // turn relative URLs into absolute URLs
191
- $url = relative2absolute($siteurl, $url);
192
-
193
  if(strlen($url)>3){
194
  $wpdb->query(
195
  "INSERT INTO $linkdata_name(post_id, url, link_text)
@@ -202,7 +206,7 @@
202
 
203
  function gather_and_save_links($content, $post_id){
204
  //gather links (<a href=...>)
205
- $url_pattern='/(<a[\s]+[^>]*href\s*=\s*[\"\']?)([^\'\" >]+)([\'\"]+[^<>]*>)((?sU).*)(<\/a>)/i';
206
 
207
  if(preg_match_all($url_pattern, $content, $matches, PREG_SET_ORDER)){
208
  foreach($matches as $link){
@@ -211,9 +215,9 @@
211
  };
212
 
213
  //gather images (<img src=...>)
214
- $url_pattern='/(<img[\s]+[^>]*src\s*=\s*[\"\']?)([^\'\" >]+)([\'\"]+[^<>]*>)/i';
215
 
216
- if(preg_match_all($url_pattern, $content, $matches, PREG_SET_ORDER)){
217
  foreach($matches as $img){
218
  parse_image($img, $post_id);
219
  }
@@ -257,6 +261,7 @@
257
  if ( (($code<200) || ($code>=400)) && $nobody) {
258
  curl_setopt($ch, CURLOPT_NOBODY, false);
259
  curl_setopt($ch, CURLOPT_HTTPGET, true);
 
260
  $response = curl_exec($ch);
261
  $code=intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
262
  }
@@ -266,59 +271,26 @@
266
  return (($code>=200) && ($code<400));
267
  }
268
 
269
- function relative2absolute($absolute, $relative) {
270
- $p = @parse_url($relative);
271
- if(!$p) {
272
- //WTF? $relative is a seriously malformed URL
273
- return false;
274
- }
275
- if(isset($p["scheme"])) return $relative;
276
-
277
- $parts=(parse_url($absolute));
278
-
279
- if(substr($relative,0,1)=='/') {
280
- $cparts = (explode("/", $relative));
281
- array_shift($cparts);
282
- } else {
283
- if(isset($parts['path'])){
284
- $aparts=explode('/',$parts['path']);
285
- array_pop($aparts);
286
- $aparts=array_filter($aparts);
287
- } else {
288
- $aparts=array();
289
- }
290
-
291
- $rparts = (explode("/", $relative));
292
-
293
- $cparts = array_merge($aparts, $rparts);
294
- foreach($cparts as $i => $part) {
295
- if($part == '.') {
296
- unset($cparts[$i]);
297
- } else if($part == '..') {
298
- unset($cparts[$i]);
299
- unset($cparts[$i-1]);
300
- }
301
- }
302
- }
303
- $path = implode("/", $cparts);
304
-
305
- $url = '';
306
- if($parts['scheme']) {
307
- $url = "$parts[scheme]://";
308
- }
309
- if(isset($parts['user'])) {
310
- $url .= $parts['user'];
311
- if(isset($parts['pass'])) {
312
- $url .= ":".$parts['pass'];
313
- }
314
- $url .= "@";
315
- }
316
- if(isset($parts['host'])) {
317
- $url .= $parts['host']."/";
318
- }
319
- $url .= $path;
320
-
321
- return $url;
322
  }
323
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
324
  ?>
14
  return microtime(true)-$execution_start_time;
15
  }
16
 
17
+ if (!current_user_can('read')) {
18
+ die("Error: You can't do that. Access denied.");
19
+ }
20
 
21
  if(!is_object($ws_link_checker)) {
22
  die('Fatal error : undefined object; plugin may not be active.');
23
  };
24
 
25
  $url_pattern='/(<a[\s]+[^>]*href\s*=\s*[\"\']?)([^\'\" >]+)([\'\"]+[^<>]*>)((?sU).*)(<\/a>)/i';
26
+ $url_to_replace = '';
27
 
28
  $postdata_name=$wpdb->prefix . "blc_postdata";
29
  $linkdata_name=$wpdb->prefix . "blc_linkdata";
30
 
31
  $options=$ws_link_checker->options; //get_option('wsblc_options');
 
32
  $max_execution_time=isset($options['max_work_session'])?intval($options['max_work_session']):27;
33
 
34
  // Check for safe mode
120
  //some unchecked links found
121
  echo "<!-- checking links (rand : ".rand(1,1000).") -->";
122
  foreach ($links as $link) {
123
+ if( $ws_link_checker->is_excluded($link->url) || page_exists_simple($link->url) ){
124
  //link OK, remove from queue
125
  $wpdb->query("DELETE FROM $linkdata_name WHERE id=$link->id");
126
  } else {
138
  die('<!-- /run_check -->');
139
 
140
  } else if ($action=='discard_link'){
141
+ if (!current_user_can('edit_posts')) {
142
+ die("Error: You can't do that. Access denied.");
143
+ }
144
  $id=intval($_GET['id']);
145
  $wpdb->query("DELETE FROM $linkdata_name WHERE id=$id LIMIT 1");
146
+
147
+ } else if ($action=='remove_link'){
148
+
149
+ //actually deletes the link from the post
150
+ if (!current_user_can('edit_posts')) {
151
+ die("Error: You can't do that. Access denied.");
152
+ }
153
+
154
+ $id=intval($_GET['id']);
155
+ $sql="SELECT * FROM $linkdata_name WHERE id = $id LIMIT 1";
156
+ $the_link=$wpdb->get_row($sql, OBJECT, 0);
157
+ if (!$the_link){
158
+ die('<!-- link not found -->');
159
+ }
160
+ $the_post = get_post($the_link->post_id, ARRAY_A);
161
+ if (!$the_post){
162
+ die('<!-- post not found -->');
163
+ }
164
+
165
+ $new_content = unlink_the_link($the_post['post_content'], $the_link->url);
166
+ $new_content = $wpdb->escape($new_content);
167
+ $wpdb->query("UPDATE $wpdb->posts SET post_content = '$new_content' WHERE id = $the_link->post_id");
168
+ $wpdb->query("DELETE FROM $linkdata_name WHERE id=$id LIMIT 1");
169
+ die('<!-- link deleted -->');
170
  };
171
 
 
172
  function parse_link($matches, $post_id){
173
+ global $wpdb, $linkdata_name, $ws_link_checker;
174
 
175
  $url=$matches[2];
176
 
177
+ $url = $ws_link_checker->normalize_url($url);
178
+ if (!$url) return false;
 
 
 
 
 
 
 
 
 
179
 
 
 
 
180
  if(strlen($url)>5){
181
  $wpdb->query(
182
  "INSERT INTO $linkdata_name(post_id, url, link_text)
188
  }
189
 
190
  function parse_image($matches, $post_id){
191
+ global $wpdb, $linkdata_name, $ws_link_checker;
192
 
193
  $url=$matches[2];
194
+ $url = $ws_link_checker->normalize_url($url);
195
+ if(!$url) return false;
196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
  if(strlen($url)>3){
198
  $wpdb->query(
199
  "INSERT INTO $linkdata_name(post_id, url, link_text)
206
 
207
  function gather_and_save_links($content, $post_id){
208
  //gather links (<a href=...>)
209
+ global $url_pattern;
210
 
211
  if(preg_match_all($url_pattern, $content, $matches, PREG_SET_ORDER)){
212
  foreach($matches as $link){
215
  };
216
 
217
  //gather images (<img src=...>)
218
+ $img_pattern='/(<img[\s]+[^>]*src\s*=\s*[\"\']?)([^\'\" >]+)([\'\"]+[^<>]*>)/i';
219
 
220
+ if(preg_match_all($img_pattern, $content, $matches, PREG_SET_ORDER)){
221
  foreach($matches as $img){
222
  parse_image($img, $post_id);
223
  }
261
  if ( (($code<200) || ($code>=400)) && $nobody) {
262
  curl_setopt($ch, CURLOPT_NOBODY, false);
263
  curl_setopt($ch, CURLOPT_HTTPGET, true);
264
+ curl_setopt($ch, CURLOPT_RANGE, '0-1023');
265
  $response = curl_exec($ch);
266
  $code=intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
267
  }
271
  return (($code>=200) && ($code<400));
272
  }
273
 
274
+ function unlink_the_link($content, $url){
275
+ global $url_pattern, $url_to_replace;
276
+ $url_to_replace = $url;
277
+ $url_pattern='/(<a[\s]+[^>]*href\s*=\s*[\"\']?)([^\'\" >]+)([\'\"]+[^<>]*>)((?sU).*)(<\/a>)/i';
278
+ $content = preg_replace_callback($url_pattern, unlink_link_callback, $content);
279
+ return $content;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280
  }
281
 
282
+ function unlink_link_callback($matches){
283
+ global $url_to_replace;
284
+ $url = $ws_link_checker->normalize_url($matches[2]);
285
+ $text = $matches[4];
286
+
287
+ //echo "$url || $url_to_replace\n";
288
+ if ($url == $url_to_replace){
289
+ //echo "Removed '$text' - '$url'\n";
290
+ return $text;
291
+ //return "<span class='broken_link'>$text</span>";
292
+ } else {
293
+ return $matches[0];
294
+ }
295
+ }
296
  ?>