From 426faa94502218f110e5391e32aba795fa75b1e8 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 3 Apr 2012 19:03:58 +0300 Subject: Fix an issue in CI_Trackback::validate_url() and do some cleanup and optimizations in the library --- system/libraries/Trackback.php | 100 +++++++++++++++--------------------- user_guide_src/source/changelog.rst | 1 + 2 files changed, 43 insertions(+), 58 deletions(-) diff --git a/system/libraries/Trackback.php b/system/libraries/Trackback.php index be1de6f3f..6761f63a5 100644 --- a/system/libraries/Trackback.php +++ b/system/libraries/Trackback.php @@ -25,8 +25,6 @@ * @filesource */ -// ------------------------------------------------------------------------ - /** * Trackback Class * @@ -49,7 +47,7 @@ class CI_Trackback { public function __construct() { - log_message('debug', "Trackback Class Initialized"); + log_message('debug', 'Trackback Class Initialized'); } // -------------------------------------------------------------------- @@ -97,9 +95,10 @@ class CI_Trackback { } // Build the Trackback data string - $charset = ( ! isset($tb_data['charset'])) ? $this->charset : $tb_data['charset']; + $charset = isset($tb_data['charset']) ? $tb_data['charset'] : $this->charset; - $data = "url=".rawurlencode($url)."&title=".rawurlencode($title)."&blog_name=".rawurlencode($blog_name)."&excerpt=".rawurlencode($excerpt)."&charset=".rawurlencode($charset); + $data = 'url='.rawurlencode($url).'&title='.rawurlencode($title).'&blog_name='.rawurlencode($blog_name) + .'&excerpt='.rawurlencode($excerpt).'&charset='.rawurlencode($charset); // Send Trackback(s) $return = TRUE; @@ -139,7 +138,7 @@ class CI_Trackback { return FALSE; } - $this->data['charset'] = ( ! isset($_POST['charset'])) ? 'auto' : strtoupper(trim($_POST['charset'])); + $this->data['charset'] = isset($_POST['charset']) ? strtoupper(trim($_POST['charset'])) : 'auto'; if ($val != 'url' && MB_ENABLED === TRUE) { @@ -164,7 +163,7 @@ class CI_Trackback { /** * Send Trackback Error Message * - * Allows custom errors to be set. By default it + * Allows custom errors to be set. By default it * sends the "incomplete information" error, as that's * the most common one. * @@ -173,7 +172,7 @@ class CI_Trackback { */ public function send_error($message = 'Incomplete Information') { - echo "\n\n1\n".$message."\n"; + echo '\n\n1\n".$message."\n"; exit; } @@ -189,7 +188,7 @@ class CI_Trackback { */ public function send_success() { - echo "\n\n0\n"; + echo '\n\n0\n"; exit; } @@ -203,7 +202,7 @@ class CI_Trackback { */ public function data($item) { - return ( ! isset($this->data[$item])) ? '' : $this->data[$item]; + return isset($this->data[$item]) ? $this->data[$item] : ''; } // -------------------------------------------------------------------- @@ -212,7 +211,7 @@ class CI_Trackback { * Process Trackback * * Opens a socket connection and passes the data to - * the server. Returns TRUE on success, FALSE on failure + * the server. Returns TRUE on success, FALSE on failure * * @param string * @param string @@ -230,37 +229,36 @@ class CI_Trackback { } // Build the path - $ppath = ( ! isset($target['path'])) ? $url : $target['path']; + $ppath = isset($target['path']) ? $target['path'] : $url; - $path = (isset($target['query']) && $target['query'] != "") ? $ppath.'?'.$target['query'] : $ppath; + $path = empty($target['query']) ? $ppath : $ppath.'?'.$target['query']; // Add the Trackback ID to the data string if ($id = $this->get_id($url)) { - $data = "tb_id=".$id."&".$data; + $data = 'tb_id='.$id.'&'.$data; } // Transfer the data - fputs ($fp, "POST " . $path . " HTTP/1.0\r\n" ); - fputs ($fp, "Host: " . $target['host'] . "\r\n" ); - fputs ($fp, "Content-type: application/x-www-form-urlencoded\r\n" ); - fputs ($fp, "Content-length: " . strlen($data) . "\r\n" ); - fputs ($fp, "Connection: close\r\n\r\n" ); - fputs ($fp, $data); + fputs($fp, 'POST '.$path." HTTP/1.0\r\n"); + fputs($fp, 'Host: '.$target['host']."\r\n"); + fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); + fputs($fp, 'Content-length: '.strlen($data)."\r\n"); + fputs($fp, "Connection: close\r\n\r\n"); + fputs($fp, $data); // Was it successful? - $this->response = ""; + $this->response = ''; while ( ! feof($fp)) { $this->response .= fgets($fp, 128); } @fclose($fp); - if (stripos($this->response, '0') === FALSE) { - $message = (preg_match('/(.*?)<\/message>/is', $this->response, $match)) ? trim($match[1]) : 'An unknown error was encountered'; + $message = preg_match('/(.*?)<\/message>/is', $this->response, $match) ? trim($match[1]) : 'An unknown error was encountered'; $this->set_error($message); return FALSE; } @@ -282,11 +280,8 @@ class CI_Trackback { */ public function extract_urls($urls) { - // Remove the pesky white space and replace with a comma. - $urls = preg_replace("/\s*(\S+)\s*/", "\\1,", $urls); - - // If they use commas get rid of the doubles. - $urls = str_replace(",,", ",", $urls); + // Remove the pesky white space and replace with a comma, then replace doubles. + $urls = str_replace(',,', ',', preg_replace('/\s*(\S+)\s*/', '\\1,', $urls)); // Remove any comma that might be at the end if (substr($urls, -1) === ',') @@ -294,11 +289,8 @@ class CI_Trackback { $urls = substr($urls, 0, -1); } - // Break into an array via commas - $urls = preg_split('/[,]/', $urls); - - // Removes duplicates - $urls = array_unique($urls); + // Break into an array via commas and remove duplicates + $urls = array_unique(preg_split('/[,]/', $urls)); array_walk($urls, array($this, 'validate_url')); @@ -313,9 +305,9 @@ class CI_Trackback { * Simply adds "http://" if missing * * @param string - * @return string + * @return void */ - public function validate_url($url) + public function validate_url(&$url) { $url = trim($url); @@ -335,7 +327,7 @@ class CI_Trackback { */ public function get_id($url) { - $tb_id = ""; + $tb_id = ''; if (strpos($url, '?') !== FALSE) { @@ -359,18 +351,11 @@ class CI_Trackback { if ( ! is_numeric($tb_id)) { - $tb_id = $tb_array[count($tb_array)-2]; + $tb_id = $tb_array[count($tb_array)-2]; } } - if ( ! preg_match ("/^([0-9]+)$/", $tb_id)) - { - return FALSE; - } - else - { - return $tb_id; - } + return preg_match('/^[0-9]+$/', $tb_id) ? $tb_id : FALSE; } // -------------------------------------------------------------------- @@ -385,15 +370,13 @@ class CI_Trackback { { $temp = '__TEMP_AMPERSANDS__'; - $str = preg_replace(array('/&#(\d+);/', '/&(\w+);/'), "$temp\\1;", $str); - - $str = str_replace(array("&","<",">","\"", "'", "-"), - array("&", "<", ">", """, "'", "-"), - $str); + $str = preg_replace(array('/&#(\d+);/', '/&(\w+);/'), $temp.'\\1;', $str); - $str = preg_replace(array("/$temp(\d+);/", "/$temp(\w+);/"), array('&#\\1;', '&\\1;'), $str); + $str = str_replace(array('&', '<', '>', '"', "'", '-'), + array('&', '<', '>', '"', ''', '-'), + $str); - return $str; + return preg_replace(array('/'.$temp.'(\d+);/', '/'.$temp.'(\w+);/'), array('&#\\1;', '&\\1;'), $str); } // -------------------------------------------------------------------- @@ -404,7 +387,7 @@ class CI_Trackback { * Limits the string based on the character count. Will preserve complete words. * * @param string - * @param integer + * @param int * @param string * @return string */ @@ -415,7 +398,7 @@ class CI_Trackback { return $str; } - $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str)); + $str = preg_replace('/\s+/', ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str)); if (strlen($str) <= $n) { @@ -469,7 +452,9 @@ class CI_Trackback { if (count($temp) === $count) { - $number = ($count == 3) ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64) : (($temp[0] % 32) * 64) + ($temp[1] % 64); + $number = ($count === 3) + ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64) + : (($temp[0] % 32) * 64) + ($temp[1] % 64); $out .= '&#'.$number.';'; $count = 1; @@ -506,11 +491,10 @@ class CI_Trackback { */ public function display_errors($open = '

', $close = '

') { - return (count($this->error_msg) > 0) ? $open . implode($close . $open, $this->error_msg) . $close : ''; + return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : ''; } } -// END Trackback Class /* End of file Trackback.php */ -/* Location: ./system/libraries/Trackback.php */ +/* Location: ./system/libraries/Trackback.php */ \ No newline at end of file diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 9f0d55ad5..fe0670a82 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -188,6 +188,7 @@ Bug fixes for 3.0 - Fixed a bug (#88) - An unexisting property was used for configuration of the Memcache cache driver. - Fixed a bug (#14) - create_database() method in the :doc:`Database Forge Library ` didn't utilize the configured database character set. - Fixed a bug (#1238) - delete_all() in the `Database Caching Library ` used to delete .htaccess and index.html files, which is a potential security risk. +- Fixed a bug in :doc:`Trackback Library ` method validate_url() where it didn't actually do anything, due to input not being passed by reference. Version 2.1.1 ============= -- cgit v1.2.3-24-g4f1b