WordPress.org

Forums

Short URL
[resolved] Few things I noticed in the code (2 posts)

  1. xFOrCE
    Member
    Posted 1 year ago #

    One of the problem I face when using this plugin is that if the links ends with a '/' at the end then wordpress redirects to 404. So I was checking the code shorten-url.php -> redirect_404() [line number 845].

    I have added a fix for the backslash, let me know if its correct.
    What I am doing is checking if the last element is blank then remove it from the array 'param'.

    function redirect_404() {
    		global $post;
    		global $wpdb;
    		$table_name = $this->table_name;
    
    		if(is_404()) {
    			$param = explode("/", $_SERVER['REQUEST_URI']) ; 
    
    			// Fixed by Ahmed, if there is a slash at the end.
    			if( count( $param ) > 2 ) {
    				$last_elem = array_pop( $param );
    				if( $last_elem != '' )
    					$param[] = $last_elem;
    			}
    			// End fix
    			if (preg_match("/^([a-zA-Z0-9_.-])*$/",$param[count($param)-1],$matches)==1) {
    				$select = "SELECT id_post FROM {$table_name} WHERE short_url='".$param[count($param)-1]."'" ;
    				$temp_id = $wpdb->get_var( $select ) ;
    				if (($temp_id==null)||($temp_id===false)) {
    					return ;
    				} else if (is_numeric($temp_id)) {
    					if ($temp_id==0) {
    						$select = "SELECT url_externe FROM {$table_name} WHERE short_url='".$param[count($param)-1]."'" ;
    						$temp_url = $wpdb->get_var( $select ) ;
    						$wpdb->query("UPDATE {$table_name} SET nb_hits = IFNULL(nb_hits, 0) + 1 WHERE short_url='".$param[count($param)-1]."'") ;
    						header("HTTP/1.1 301 Moved Permanently");
    						$temp_url = str_replace("&", "&", $temp_url);
    						header("Location: ".$temp_url );
    						exit();
    					} else {
    						$wpdb->query("UPDATE {$table_name} SET nb_hits = IFNULL(nb_hits, 0) + 1 WHERE id_post=".$temp_id) ;
    						header("HTTP/1.1 301 Moved Permanently");
    						//$temp_url = str_replace("&", "&", $temp_url); -- Why is this line added ? -Ahmed
    						header("Location: ".get_permalink($temp_id));
    						exit();
    					}
    				}
    			}
    		}
    	}

    What I am not trying to understand is this piece of code,
    preg_match("/^([a-zA-Z0-9_.-])*$/",$param[count($param)-1],$matches)

    When a blank is passed as the second argument, then it return 1, I think it should return FALSE.

    Thanks

    https://wordpress.org/plugins/shorten-url/

  2. Sed Lex
    Member
    Plugin Author

    Posted 1 year ago #

    That should be corrected in the next version

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Short URL
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.