root/trackback/files/lib/system/event/listener/ThreadAddFormTrackbackListener.class.php @ 78

Revision 78, 3.2 kB (checked in by d0nut, 6 years ago)

license updates to version 3 of GPL and LGPL

  • Property svn:executable set to *
Line 
1<?php
2// WCF include
3require_once(WCF_DIR.'lib/system/event/EventListener.class.php');
4
5// Utils
6require_once(WBB_DIR.'lib/util/TrackbackUtil.class.php');
7
8/**
9 * Handles activation of the trackback in threadcreation
10 *
11 * @author      Torben Brodt
12 * @package     de.easy-coding.wbb.trackback
13 * @license     GNU General Public License <http://opensource.org/licenses/gpl-3.0.html>
14 */
15class ThreadAddFormTrackbackListener implements EventListener {
16        protected $eventObj;
17        protected $className;
18
19        protected $hasTrackback = 0;
20
21        /**
22         * @see EventListener::execute()
23         */
24        public function execute($eventObj, $className, $eventName) {   
25                $this->eventObj = $eventObj;
26                $this->className = $className;
27
28                switch ($eventName) {
29                        case 'readData':
30                                $this->readData();
31                                break;
32                        case 'readFormParameters':
33                                $this->readFormParameters();
34                                break;
35                        case 'assignVariables':
36                                $this->assignVariables();
37                                break;
38                        case 'saved':
39                                $this->saved();
40                                break;
41                }
42        }
43
44        /**
45         * @see Form::readData()
46         */
47        protected function readData () {
48                $this->hasTrackback = $this->eventObj->post->hasTrackback;
49        }
50
51        /**
52         * @see Form::readFormParameters()
53         */
54        protected function readFormParameters () {
55                if (isset($_REQUEST['hasTrackback'])) $this->hasTrackback = intval($_REQUEST['hasTrackback']);
56        }
57       
58        /**
59         * @see Form::assignVariables()
60         */
61        protected function assignVariables () {
62                WBBCore::getTPL()->assign('hasTrackback', $this->hasTrackback);
63                WBBCore::getTPL()->append('additionalSettings', WBBCore::getTPL()->fetch('postTrackbackSetting'));
64        }
65
66        /**
67         * @see Form::saved()
68         */
69        protected function saved () {
70                $postID = 0;
71               
72                switch ($this->className) {
73                        case 'ThreadAddForm':
74                                $postID = $this->eventObj->newThread->firstPostID;
75                                $this->data = $this->eventObj->newThread->firstPostID;
76                                break;
77                        case 'PostEditForm':
78                                $postID = $this->eventObj->postID;
79                                break;
80                }
81               
82                // send trackbacks
83                $this->sendTrackbackData($postID);
84
85                // UPDATE
86                $sql = "UPDATE          wbb".WBB_N."_post
87                        SET             hasTrackback    = {$this->hasTrackback}
88                        WHERE           postID          = {$postID};";
89
90                WBBCore::getDB()->sendQuery($sql);
91        }
92       
93        /**
94         * fetch trackback data from database (maybe an event would be faster)
95         * @param postID
96         */
97        protected function sendTrackbackData($postID) {
98                // fetch data
99                $row = TrackbackUtil::getPost($postID);
100               
101                $patterns = array();
102                $patterns[] = "#\[url\]([\w]+?://([\w\#$%&~/.\-;:=,?@\]+]+|\[(?!url=))*?)\[/url\]#is";
103                $patterns[] = "#\[url=([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*?)\]([^?\n\r\t].*?)\[/url\]#is";
104               
105                // fetch current postings
106                $urls = array();
107                foreach($patterns as $p) {
108                        preg_match_all($p, $this->eventObj->text, $res);
109                        $urls = array_merge($urls, $res[1]);
110                }
111
112                // fetch made postings
113                $oldurls = array();
114                $sql = "SELECT          alienURL
115                        FROM            wbb".WBB_N."_trackbackLog
116                        WHERE           postID = ".$postID;
117                $result = WCF::getDB()->sendQuery($sql);
118                while ($row = WCF::getDB()->fetchArray($result)) {
119                        $oldurls[] = $row['alienURL'];
120                }
121
122                // send pings
123                foreach(array_diff($urls, $oldurls) as $url) {
124                        TrackbackUtil::trackAndPing($postID, $url, PAGE_TITLE, $row['username'], $row['url'], $row['topic'], substr($row['firstPostPreview'],0,255));
125                }
126        }
127}
128?>
Note: See TracBrowser for help on using the browser.