Dotclear


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/core/class.dc.trackback.php

    r1674 r1689  
    1414/** 
    1515@ingroup DC_CORE 
    16 @brief Trackbacks sender and server 
    17  
    18 Sends and receives trackbacks. Also handles trackbacks auto discovery. 
     16@brief Trackbacks/Pingbacks sender and server 
     17 
     18Sends and receives trackbacks/pingbacks. Also handles trackbacks/pingbacks auto discovery. 
    1919*/ 
    2020class dcTrackback 
     
    146146     } 
    147147     //@} 
     148      
     149     private function pingAlreadyDone($post_id, $from_url) 
     150     { 
     151          $params = array( 
     152               'post_id' => $post_id, 
     153               'comment_site' => $from_url, 
     154               'comment_trackback' => 1, 
     155          ); 
     156           
     157          $rs = $this->core->blog->getComments($params, true); 
     158          if ($rs && !$rs->isEmpty()) { 
     159               return ($rs->f(0)); 
     160          } 
     161           
     162          return false; 
     163     } 
     164      
     165     private function addBacklink($post_id, $url, $blog_name, $title, $excerpt, &$comment) 
     166     { 
     167          if (empty($blog_name)) { 
     168               $blog_name = 'Anonymous blog'; 
     169          } 
     170           
     171          $comment = 
     172          "<!-- TB -->\n". 
     173          '<p><strong>'.($title ? $title : $blog_name)."</strong></p>\n". 
     174          '<p>'.$excerpt.'</p>'; 
     175           
     176          $cur = $this->core->con->openCursor($this->core->prefix.'comment'); 
     177          $cur->comment_author = (string) $blog_name; 
     178          $cur->comment_site = (string) $url; 
     179          $cur->comment_content = (string) $comment; 
     180          $cur->post_id = $post_id; 
     181          $cur->comment_trackback = 1; 
     182          $cur->comment_status = $this->core->blog->settings->system->trackbacks_pub ? 1 : -1; 
     183          $cur->comment_ip = http::realIP(); 
     184           
     185          # --BEHAVIOR-- publicBeforeTrackbackCreate 
     186          $this->core->callBehavior('publicBeforeTrackbackCreate',$cur); 
     187          if ($cur->post_id) { 
     188               $comment_id = $this->core->blog->addComment($cur); 
     189                
     190               # --BEHAVIOR-- publicAfterTrackbackCreate 
     191               $this->core->callBehavior('publicAfterTrackbackCreate',$cur,$comment_id); 
     192          } 
     193     } 
    148194      
    149195     /// @name Receive trackbacks 
     
    176222          $charset = ''; 
    177223          $comment = ''; 
    178            
     224                     
    179225          $err = false; 
    180226          $msg = ''; 
     
    209255                    $msg = 'Trackbacks are not allowed for this post or weblog.'; 
    210256               } 
     257 
     258               $url = trim(html::clean($url)); 
     259               if ($this->pingAlreadyDone($post->post_id, $url)) { 
     260                    $err = true; 
     261                    $msg = 'The trackback has already been registered'; 
     262               } 
    211263          } 
    212264           
     
    216268                
    217269               if (!$charset) { 
    218                     $charset = mb_detect_encoding($title.' '.$excerpt.' '.$blog_name, 
    219                     'UTF-8,ISO-8859-1,ISO-8859-2,ISO-8859-3,'. 
    220                     'ISO-8859-4,ISO-8859-5,ISO-8859-6,ISO-8859-7,ISO-8859-8,'. 
    221                     'ISO-8859-9,ISO-8859-10,ISO-8859-13,ISO-8859-14,ISO-8859-15'); 
     270                    $charset = self::detectCharset($title.' '.$excerpt.' '.$blog_name); 
    222271               } 
    223272                
     
    244293               $blog_name = text::cutString($blog_name,60); 
    245294                
    246                $url = trim(html::clean($url)); 
    247                 
    248                if (!$blog_name) { 
    249                     $blog_name = 'Anonymous blog'; 
    250                } 
    251                 
    252                $comment = 
    253                "<!-- TB -->\n". 
    254                '<p><strong>'.($title ? $title : $blog_name)."</strong></p>\n". 
    255                '<p>'.$excerpt.'</p>'; 
    256                 
    257                $cur = $this->core->con->openCursor($this->core->prefix.'comment'); 
    258                $cur->comment_author = (string) $blog_name; 
    259                $cur->comment_site = (string) $url; 
    260                $cur->comment_content = (string) $comment; 
    261                $cur->post_id = $post_id; 
    262                $cur->comment_trackback = 1; 
    263                $cur->comment_status = $this->core->blog->settings->system->trackbacks_pub ? 1 : -1; 
    264                $cur->comment_ip = http::realIP(); 
    265                 
    266295               try 
    267296               { 
    268                     # --BEHAVIOR-- publicBeforeTrackbackCreate 
    269                     $this->core->callBehavior('publicBeforeTrackbackCreate',$cur); 
    270                     if ($cur->post_id) { 
    271                          $comment_id = $this->core->blog->addComment($cur); 
    272                           
    273                          # --BEHAVIOR-- publicAfterTrackbackCreate 
    274                          $this->core->callBehavior('publicAfterTrackbackCreate',$cur,$comment_id); 
    275                     } 
     297                    $this->addBacklink($post_id, $url, $blog_name, $title, $excerpt, $comment); 
    276298               } 
    277299               catch (Exception $e) 
     
    281303               } 
    282304          } 
    283            
    284            
    285           $debug_trace = 
    286           "  <debug>\n". 
    287           '    <title>'.$title."</title>\n". 
    288           '    <excerpt>'.$excerpt."</excerpt>\n". 
    289           '    <url>'.$url."</url>\n". 
    290           '    <blog_name>'.$blog_name."</blog_name>\n". 
    291           '    <charset>'.$charset."</charset>\n". 
    292           '    <comment>'.$comment."</comment>\n". 
    293           "  </debug>\n"; 
    294305           
    295306          $resp = 
     
    303314           
    304315          if (!empty($_POST['__debug'])) { 
    305                $resp .= $debug_trace; 
     316               $resp .=  
     317               "  <debug>\n". 
     318               '    <title>'.$title."</title>\n". 
     319               '    <excerpt>'.$excerpt."</excerpt>\n". 
     320               '    <url>'.$url."</url>\n". 
     321               '    <blog_name>'.$blog_name."</blog_name>\n". 
     322               '    <charset>'.$charset."</charset>\n". 
     323               '    <comment>'.$comment."</comment>\n". 
     324               "  </debug>\n"; 
    306325          } 
    307326           
     
    362381          } 
    363382 
     383          if ($this->pingAlreadyDone($posts->post_id, $from_url)) { 
     384               throw new Exception(__('Don\'t repeat yourself, please.'), 48); 
     385          } 
     386           
    364387          # OK. We've found our champion. Time to check the remote part. 
    365388          try { 
     
    381404               $remote_content = $http->getContent(); 
    382405 
    383                $charset = mb_detect_encoding($remote_content, 
    384                     'UTF-8,ISO-8859-1,ISO-8859-2,ISO-8859-3,'. 
    385                     'ISO-8859-4,ISO-8859-5,ISO-8859-6,ISO-8859-7,ISO-8859-8,'. 
    386                     'ISO-8859-9,ISO-8859-10,ISO-8859-13,ISO-8859-14,ISO-8859-15'); 
    387  
     406               $charset = self::getCharsetFromRequest($http->getHeader('content-type')); 
     407 
     408               if (!$charset) { 
     409                    $charset = self::detectCharset($remote_content); 
     410               } 
     411                
    388412               if (strtolower($charset) != 'utf-8') { 
    389413                    $remote_content = iconv($charset,'UTF-8',$remote_content); 
     
    416440               } 
    417441               if ($excerpt) { 
    418                     $excerpt = '(&#8230;) '.text::cutString(html::escapeHTML($excerpt),255).' (&#8230;)'; 
     442                    $excerpt = '(&#8230;) '.text::cutString(html::escapeHTML($excerpt),200).' (&#8230;)'; 
    419443               } 
    420444               else { 
    421                     $excerpt = '(??)'; 
    422                } 
    423  
    424                $comment = 
    425                "<!-- TB -->\n". 
    426                '<p><strong>'.$title."</strong></p>\n". 
    427                '<p>'.$excerpt.'</p>'; 
    428                 
    429                $cur = $this->core->con->openCursor($this->core->prefix.'comment'); 
    430                $cur->comment_author = 'Anonymous blog'; 
    431                $cur->comment_site = (string) $from_url; 
    432                $cur->comment_content = (string) $comment; 
    433                $cur->post_id = $posts->post_id; 
    434                $cur->comment_trackback = 1; 
    435                $cur->comment_status = $this->core->blog->settings->system->trackbacks_pub ? 1 : -1; 
    436                $cur->comment_ip = http::realIP(); 
    437                 
    438                # --BEHAVIOR-- publicBeforeTrackbackCreate 
    439                $this->core->callBehavior('publicBeforeTrackbackCreate',$cur); 
    440                if ($cur->post_id) { 
    441                     $comment_id = $this->core->blog->addComment($cur); 
    442                      
    443                     # --BEHAVIOR-- publicAfterTrackbackCreate 
    444                     $this->core->callBehavior('publicAfterTrackbackCreate',$cur,$comment_id); 
    445                } 
     445                    $excerpt = '(&#8230;)'; 
     446               } 
     447 
     448               $this->addBacklink($posts->post_id, $from_url, '', $title, $excerpt, $comment); 
    446449          } 
    447450          catch (Exception $e) { 
     
    464467     } 
    465468      
    466      private static function getCharsetFromRequest() 
    467      { 
    468           if (isset($_SERVER['CONTENT_TYPE'])) 
    469           { 
    470                if (preg_match('|charset=([a-zA-Z0-9-]+)|',$_SERVER['CONTENT_TYPE'],$m)) { 
     469     private static function getCharsetFromRequest($header = '') 
     470     { 
     471          if (!$header && isset($_SERVER['CONTENT_TYPE'])) { 
     472               $header = $_SERVER['CONTENT_TYPE']; 
     473          } 
     474           
     475          if ($header) { 
     476               if (preg_match('|charset=([a-zA-Z0-9-]+)|',$header,$m)) { 
    471477                    return $m[1]; 
    472478               } 
     
    475481          return null; 
    476482     } 
    477       
     483 
     484     private static function detectCharset($string) 
     485     { 
     486          return mb_detect_encoding($remote_content, 
     487                    'UTF-8,ISO-8859-1,ISO-8859-2,ISO-8859-3,'. 
     488                    'ISO-8859-4,ISO-8859-5,ISO-8859-6,ISO-8859-7,ISO-8859-8,'. 
     489                    'ISO-8859-9,ISO-8859-10,ISO-8859-13,ISO-8859-14,ISO-8859-15'); 
     490     } 
     491           
    478492     /// @name Trackbacks auto discovery 
    479493     //@{ 
Note: See TracChangeset for help on using the changeset viewer.

Sites map