root/spiderbouncer/files/lib/system/event/listener/AbstractPageSpiderBouncerListener.class.php @ 23

Revision 23, 1.5 kB (checked in by d0nut, 6 years ago)

initial release of spider bouncer

Line 
1<?php
2// WCF includes
3require_once(WCF_DIR.'lib/system/event/EventListener.class.php');
4
5/**
6 * The bouncer overwrites matching spidersettings with regular expressions
7 *
8 * @author      Torben Brodt
9 * @package     de.easy-coding.wcf.spiderbouncer
10 * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 */
12class AbstractPageSpiderBouncerListener implements EventListener {
13
14        /**
15         * evaluate patterns
16         * @param patterns -> textstring
17         * @param url -> url
18         */
19        protected function evaluateSingle($patterns, $url) {
20                $patterns = explode("\n", StringUtil::unifyNewlines($patterns));
21                try {
22                        foreach ($patterns as $pattern) {
23                                if(preg_match("/{$pattern}/", $url)) {
24                                        return true;
25                                }
26                        }
27
28                } catch (Exception $e) {
29                        return false;
30                }
31
32                return false;
33        }
34
35        /**
36         * evaluate patterns
37         * @param allow -> textstring
38         * @param disallow -> textstring
39         * @param url -> url
40         * @return -> -1 if undefined
41         */
42        public function evaluate($allow, $disallow, $url) {
43                if($allow && $this->evaluateSingle($allow, $url)) {
44                        return true;
45                }
46               
47                if($disallow && $this->evaluateSingle($disallow, $url)) {
48                        return false;
49                }
50               
51                return -1;
52        }
53
54        /**
55         * @see EventListener::execute()
56         */
57        public function execute($eventObj, $className, $eventName) {
58                $result = $this->evaluate(SPIDERBOUNCER_ALLOW_REGEX, SPIDERBOUNCER_DISALLOW_REGEX, substr($_SERVER['REQUEST_URI'],1));
59                if(intval($result) != -1) WCF::getTPL()->assign('allowSpidersToIndexThisPage', (boolean)$result);
60        }
61}
62?>
Note: See TracBrowser for help on using the browser.