You are here

function _gotwo_get in Go - url redirects 6

Same name and namespace in other branches
  1. 7 gotwo.module \_gotwo_get()

Return the GO object url for a given link

2 calls to _gotwo_get()
gotwo_add_form_submit in ./gotwo.admin.inc
Go redirect submitted
_gotwo_get_url in ./gotwo.module
Return the GO url for a given link

File

./gotwo.module, line 238
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));
    if (!empty($res)) {
      $src_old = substr($res->gid . '/' . $src, 0, $maxlength);
      if ($src_old != $res->src) {
        $res == FALSE;
      }
    }
  }
  if ($res === FALSE) {
    if ($flags & GOTWO_CREATE) {

      // Force unique src.
      $res = db_fetch_object(db_query("SELECT * FROM {gotwo} WHERE src = '%s'", $src));
      if (!empty($res)) {

        // TODO: find a better solution.
        // Insert a dummy first with an uniqe src value to get the 'gid' value.
        db_query("INSERT INTO {gotwo} (src, dst) VALUES ('%s', '%s')", uniqid(), $url);
        $gid = db_last_insert_id('gotwo', 'gid');
        $src = substr($gid . '/' . $src, 0, $maxlength);
        db_query("UPDATE {gotwo} SET src = '%s' WHERE gid = %d", $src, $gid);
        $res->gid = $gid;
        $res->src = $src;
        $res->dst = $url;
      }
      else {
        db_query("INSERT INTO {gotwo} (src, dst) VALUES ('%s', '%s')", $src, $url);
        $gid = db_last_insert_id('gotwo', 'gid');
        $res->gid = $gid;
        $res->src = $src;
        $res->dst = $url;
      }
    }
    else {
      return FALSE;
    }
  }
  return $res;
}