You are here

function gotwo_get in Go - url redirects 5

Return the GO object url for a given link

2 calls to gotwo_get()
gotwo_get_url in ./gotwo.module
Return the GO url for a given link
__gotwo_manual_add_submit in ./gotwo.module
Go entry submitted

File

./gotwo.module, line 229
Module that provides easy to use redirection links. A redirection link would be like: http://examples.org/go/a_label http://examples.org/go/123546 http://examples.org/go/or/like/this

Code

function gotwo_get($url, $src = NULL, $flags = gotwo_CREATE) {

  // Only add valid URLs to the database. Otherwise the disclaimer reload may fail.
  if (!valid_url($url)) {
    return FALSE;
  }

  // If there is no title to mangle, use the url instead.
  if (!$src) {
    $src = preg_replace('#^(http(s)?://)#', '', $url);
  }
  $src = __gotwo_mangle_src($src);
  $maxlength = min(variable_get('gotwo_max_length', 128), 128);
  $res = db_fetch_object(db_query("SELECT * FROM {gotwo} WHERE src = '%s' AND dst = '%s'", $src, $url));
  if ($res === false) {
    $res = db_fetch_object(db_query("SELECT * FROM {gotwo} WHERE src = gid+'/%s' AND dst = '%s'", $src, $url));
    $src_alt = substr($res->gid . '/' . $src, 0, $maxlength);
    if ($src_alt != $res->src) {
      $res == false;
    }
  }
  if ($res === false) {
    if ($flags & gotwo_CREATE) {

      // force unique src
      $gid = db_next_id('gotwo_gid');
      $res = db_fetch_object(db_query("SELECT * FROM {gotwo} WHERE src = '%s'", $src));
      if ($res) {
        $res->src = substr($gid . '/' . $src, 0, $maxlength);
      }
      else {
        $res = new StdClass();
        $res->src = $src;
      }
      $res->gid = $gid;
      $res->dst = $url;
      $res->cnt = 0;
      db_query("INSERT INTO {gotwo} (gid, src, dst) VALUES (%d, '%s', '%s')", $res->gid, $res->src, $res->dst);
    }
    else {
      return false;
    }
  }
  return $res;
}