Dotclear

source: plugins/twitterTrackback/_public.php @ 1375

Revision 1375, 3.7 KB checked in by hadrien, 14 years ago (diff)

Ajout du plugin TwitterTrackback?

Line 
1<?php
2# ***** BEGIN LICENSE BLOCK *****
3# This file is part of DotClear.
4# Copyright (c) 2007 Olivier Meunier and contributors.
5# All rights reserved.
6#
7# DotClear is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# DotClear is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with DotClear; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20#
21# ***** END LICENSE BLOCK *****
22# This file is part of TwitterComments
23# Hadrien Lanneau http://www.alti.info/
24#
25
26
27$core->addBehavior('publicBeforeDocument',array('TwitterTrackback','publicBeforeDocument'));
28
29if (!defined('DC_RC_PATH')) { return; }
30
31/**
32* TwitterTrackback
33*/
34class TwitterTrackback
35{
36     /**
37      * Check referer
38      *
39      * @return void
40      * @author Hadrien Lanneau (contact at hadrien dot eu)
41      **/
42     static public function publicBeforeDocument(&$core)
43     {
44          if (!isset($_SERVER['HTTP_REFERER']) or
45               !$_SERVER['HTTP_REFERER'])
46          {
47               return;
48          }
49          // Look for same backlink on this post
50          preg_match(
51               '@^post/(.+)$@',
52               $_SERVER['QUERY_STRING'],
53               $m
54          );
55         
56          $curPost = $core->blog->getPosts(
57               array(
58                    'post_url'     => $m[1]
59               )
60          );
61         
62          if ($curPost->count() == 1)
63          {
64               $createNewTrackback = true;
65               
66               $trackbacks = $core->blog->getComments(
67                    array(
68                         'post_id'           => $curPost->post_id,
69                         'comment_trackback' => 1
70                    )
71               );
72               
73               if ($trackbacks->count() > 0)
74               {
75                    while ($trackbacks->fetch())
76                    {
77                         // testing existing trackbacks
78                         if ($trackbacks->comment_site == $_SERVER['HTTP_REFERER'])
79                         {
80                              $createNewTrackback = false;
81                         }
82                    }
83               }
84               
85               if ($createNewTrackback)
86               {
87                    $status = false;
88                    // Test Twitter
89                    if (preg_match(
90                              '@^http://(www\.)?twitter\.com/(.*?)/status/(.*?)$@',
91                              $_SERVER['HTTP_REFERER'],
92                              $m
93                         ))
94                    {
95                         $status = netHttp::quickGet(
96                              'http://twitter.com/statuses/show/' .
97                                   $m[3] . '.xml'
98                         );
99                         $service = 'twitter';
100                    }
101                   
102                    // Test identi.ca TODO
103                    if (!$status and
104                         preg_match(
105                              '@http://identi.ca/notice/(.*?)$@',
106                              $_SERVER['HTTP_REFERER'],
107                              $m
108                         ))
109                    {
110                         $status = netHttp::quickGet(
111                              'http://identi.ca/api/statuses/show/' .
112                                   $m[1] . '.xml'
113                         );
114                         $service = 'identi.ca';
115                    }
116                   
117                    if ($status)
118                    {
119                         $status = @simplexml_load_string(
120                              $status
121                         );
122                         
123                         if ($service == 'twitter')
124                         {
125                              $tweetUrl = html::clean(
126                                   'http://twitter.com/' . strval($status->user->screen_name) . '/status/' . 
127                                   $status->id
128                              );
129                         }
130                         else
131                         {
132                              $tweetUrl = html::clean(
133                                   'http://identi.ca/notice/' . intval($status->id)
134                              );
135                         }
136                         
137                         // Create a new trackback from tweet
138                         $cur = $core->con->openCursor($core->prefix.'comment');
139                         $cur->comment_author = strval($status->user->name) .
140                              ' ' . __('from') . ' ' . $service;
141                         $cur->comment_site = $tweetUrl;
142                         $cur->comment_content = "<!-- TB -->\n" .
143                              '<p><strong>' . $tweetUrl . "</strong></p>\n" .
144                              '<p>' . strval($status->text) . '</p>';
145                         $cur->post_id = intval($curPost->post_id);
146                         $cur->comment_trackback = 1;
147                         $cur->comment_status = $core->blog->settings->trackbacks_pub ? 1 : -1;
148                         $cur->comment_ip = http::realIP();
149                         
150                         $trackbackId = $core->blog->addComment(
151                              $cur
152                         );
153                    }
154               }
155          }
156     }
157}
158
Note: See TracBrowser for help on using the repository browser.

Sites map