1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
if ( !class_exists( 'WCAP_Tiny_Url' ) ) {
class WCAP_Tiny_Url {
protected static $chars = 'b1c2d3f4g5h6j7k8l9mnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ';
protected static $checkUrlExists = true;
static function get_long_url_from_id( $db_id ) {
global $wpdb;
$query = "SELECT long_url FROM `" . WCAP_TINY_URLS . "`
WHERE id = %d";
$url_data = $wpdb->get_results( $wpdb->prepare( $query, $db_id ) );
if( is_array( $url_data ) && count( $url_data ) > 0 ) {
$long_url = isset( $url_data[0]->long_url ) ? $url_data[0]->long_url : false;
} else {
$long_url = false;
}
return $long_url;
}
static function get_short_url( $url ) {
if( empty( $url ) ) {
throw new Exception( "No URL was supplied." );
}
if( self::validateUrlFormat( $url ) == false ) {
throw new Exception( "URL does not have a valid format." );
}
$shortCode = self::urlExistsInDb( $url );
if ( $shortCode == false ) {
$db_id = self::getDbEntry( $url );
$shortCode = self::createShortCode( $db_id );
}
return $shortCode;
}
static function validateUrlFormat( $url ) {
return filter_var( $url, FILTER_VALIDATE_URL,
FILTER_FLAG_HOST_REQUIRED );
}
static function urlExistsInDb( $url ) {
global $wpdb;
$query_url = "SELECT short_code FROM `" . WCAP_TINY_URLS . "`
WHERE long_url = %s";
$get_url = $wpdb->get_results( $wpdb->prepare( $query_url, $url ) );
if( is_array( $get_url ) && count( $get_url ) > 0 ) {
return isset( $get_url[0]->short_code ) ? $get_url[0]->short_code : false;
} else {
return false;
}
}
static function getDbEntry( $url ) {
global $wpdb;
$query = "SELECT id FROM `" . WCAP_TINY_URLS . "`
WHERE long_url = %s";
$url_data = $wpdb->get_results( $wpdb->prepare( $query, $url ) );
if( is_array( $url_data ) && count( $url_data ) > 0 ) {
$db_id = isset( $url_data[0]->id ) ? $url_data[0]->id : false;
} else {
$db_id = false;
}
return $db_id;
}
static function createShortCode( $id ) {
$id = intval($id);
if( $id < 0 ) {
throw new Exception(
"The ID is not a valid integer");
}
$length = strlen(self::$chars);
if( $length < 10 ) {
throw new Exception("Length of chars is too small");
}
$code = "";
while( $id > $length - 1 ) {
$code = self::$chars[ intval( fmod( $id, $length ) ) ] . $code;
$id = floor( $id / $length );
}
$code = self::$chars[ intval( $id ) ] . $code;
return $code;
}
static function update_short_url( $db_id, $short_url ) {
global $wpdb;
if( $db_id > 0 && $short_url != '' ) {
$wpdb->update( WCAP_TINY_URLS,
array( 'short_code' => $short_url ),
array( 'id' => $db_id )
);
}
}
static function get_long_url( $short_code ) {
global $wpdb;
$long_url = false;
if( $short_code != '' ) {
$query = "SELECT long_url FROM `" . WCAP_TINY_URLS . "`
WHERE BINARY short_code = %s";
$url_data = $wpdb->get_results( $wpdb->prepare( $query, $short_code ) );
if( is_array( $url_data ) && count( $url_data ) > 0 ) {
$long_url = isset( $url_data[0]->long_url ) ? $url_data[0]->long_url : false;
}
}
return $long_url;
}
static function increment_counter( $id ) {
global $wpdb;
if( $id > 0 ) {
$query = "UPDATE `" . WCAP_TINY_URLS ."`
SET counter=counter+1
WHERE id = %d";
$wpdb->query( $wpdb->prepare( $query, $id ) );
}
}
}
}
?>