Dotclear

Changeset 1684:b7294dd181ee


Ignore:
Timestamp:
09/01/13 18:00:42 (10 years ago)
Author:
Florent Cotton <florent.cotton@…>
Branch:
default
Children:
1685:8d8ae6da4e22, 1687:1b2c41ca89a1
Message:

Ré-aménagement de la classe dcTrackback afin de limiter les blocs de code inutilement redondants (closes #1628)

File:
1 edited

Legend:

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

    r1674 r1684  
    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 addBacklink($post_id, $url, $blog_name, $title, $excerpt, &$comment) 
     150     { 
     151          if (empty($blog_name)) { 
     152               $blog_name = 'Anonymous blog'; 
     153          } 
     154           
     155          $comment = 
     156          "<!-- TB -->\n". 
     157          '<p><strong>'.($title ? $title : $blog_name)."</strong></p>\n". 
     158          '<p>'.$excerpt.'</p>'; 
     159           
     160          $cur = $this->core->con->openCursor($this->core->prefix.'comment'); 
     161          $cur->comment_author = (string) $blog_name; 
     162          $cur->comment_site = (string) $url; 
     163          $cur->comment_content = (string) $comment; 
     164          $cur->post_id = $post_id; 
     165          $cur->comment_trackback = 1; 
     166          $cur->comment_status = $this->core->blog->settings->system->trackbacks_pub ? 1 : -1; 
     167          $cur->comment_ip = http::realIP(); 
     168           
     169          # --BEHAVIOR-- publicBeforeTrackbackCreate 
     170          $this->core->callBehavior('publicBeforeTrackbackCreate',$cur); 
     171          if ($cur->post_id) { 
     172               $comment_id = $this->core->blog->addComment($cur); 
     173                
     174               # --BEHAVIOR-- publicAfterTrackbackCreate 
     175               $this->core->callBehavior('publicAfterTrackbackCreate',$cur,$comment_id); 
     176          } 
     177     } 
    148178      
    149179     /// @name Receive trackbacks 
     
    216246                
    217247               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'); 
     248                    $charset = self::detectCharset($title.' '.$excerpt.' '.$blog_name); 
    222249               } 
    223250                
     
    246273               $url = trim(html::clean($url)); 
    247274                
    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                 
    266275               try 
    267276               { 
    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                     } 
     277                    $this->addBacklink($post_id, $url, $blog_name, $title, $excerpt, $comment); 
    276278               } 
    277279               catch (Exception $e) 
     
    281283               } 
    282284          } 
    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"; 
    294285           
    295286          $resp = 
     
    303294           
    304295          if (!empty($_POST['__debug'])) { 
    305                $resp .= $debug_trace; 
     296               $resp .=  
     297               "  <debug>\n". 
     298               '    <title>'.$title."</title>\n". 
     299               '    <excerpt>'.$excerpt."</excerpt>\n". 
     300               '    <url>'.$url."</url>\n". 
     301               '    <blog_name>'.$blog_name."</blog_name>\n". 
     302               '    <charset>'.$charset."</charset>\n". 
     303               '    <comment>'.$comment."</comment>\n". 
     304               "  </debug>\n"; 
    306305          } 
    307306           
     
    381380               $remote_content = $http->getContent(); 
    382381 
    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  
     382               $charset = self::getCharsetFromRequest($http->getHeader('content-type')); 
     383 
     384               if (!$charset) { 
     385                    $charset = self::detectCharset($remote_content); 
     386               } 
     387                
    388388               if (strtolower($charset) != 'utf-8') { 
    389389                    $remote_content = iconv($charset,'UTF-8',$remote_content); 
     
    416416               } 
    417417               if ($excerpt) { 
    418                     $excerpt = '(&#8230;) '.text::cutString(html::escapeHTML($excerpt),255).' (&#8230;)'; 
     418                    $excerpt = '(&#8230;) '.text::cutString(html::escapeHTML($excerpt),200).' (&#8230;)'; 
    419419               } 
    420420               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                } 
     421                    $excerpt = '(&#8230;)'; 
     422               } 
     423 
     424               $this->addBacklink($posts->post_id, $from_url, '', $title, $excerpt, $comment); 
    446425          } 
    447426          catch (Exception $e) { 
     
    464443     } 
    465444      
    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)) { 
     445     private static function getCharsetFromRequest($header = '') 
     446     { 
     447          if (!$header && isset($_SERVER['CONTENT_TYPE'])) { 
     448               $header = $_SERVER['CONTENT_TYPE']; 
     449          } 
     450           
     451          if ($header) { 
     452               if (preg_match('|charset=([a-zA-Z0-9-]+)|',$header,$m)) { 
    471453                    return $m[1]; 
    472454               } 
     
    475457          return null; 
    476458     } 
    477       
     459 
     460     private static function detectCharset($string) 
     461     { 
     462          return mb_detect_encoding($remote_content, 
     463                    'UTF-8,ISO-8859-1,ISO-8859-2,ISO-8859-3,'. 
     464                    'ISO-8859-4,ISO-8859-5,ISO-8859-6,ISO-8859-7,ISO-8859-8,'. 
     465                    'ISO-8859-9,ISO-8859-10,ISO-8859-13,ISO-8859-14,ISO-8859-15'); 
     466     } 
     467           
    478468     /// @name Trackbacks auto discovery 
    479469     //@{ 
Note: See TracChangeset for help on using the changeset viewer.

Sites map