path.module 5.74 KB
Newer Older
Dries's avatar
Dries committed
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
<?php
/* $Id$ */

function path_admin() {
  $op = strtolower($_POST["op"]);
  $edit = $_POST["edit"];

  if (user_access("alias urls")) {

    if (empty($op)) {
      $op = arg(2);
    }

    switch ($op) {
      case t("add"):
        $output = path_form();
        break;

      case t("edit"):
        $output = path_form(object2array(get_path_from_id(arg(3))));
        break;

      case t("delete"):
        if ($edit["confirm"]) {
          if (path_delete($edit['pid'])) {
Dries's avatar
 
Dries committed
26
            $output .= status("Deleted path '". $edit['dst'] ."'");
Dries's avatar
Dries committed
27 28 29 30 31 32 33 34 35 36 37 38
          }
        }
        else {
          $output .= path_confirm_delete(arg(3));
        }
        break;

      case t("create new alias"):
        $output = status(path_save($edit));
        // fall-through

      default:
Dries's avatar
 
Dries committed
39
        $output .= path_overview();
Dries's avatar
Dries committed
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
    }

    return $output;
  }
  else {
    return message_access();
  }
}

/**
 * Returns a path that is acceptable as an url.
 */
function path_clean($path) {
  global $base_url;

  /*
  ** Replace absolute URL for this site with relative URL.
  */
  $path = str_replace($base_url, "", $path);

  /*
  ** Only allow alpha numeric characters, slashes and underscores.
  */
  $path = preg_replace("'[^a-zA-Z0-9/_.]'", " ", $path);

  /*
  ** Remove all whitespace.
  */
  $path = str_replace(" ", "", $path);

  /*
  ** Replace two or more sequential slashes with only one slashes.
  */
  $path = preg_replace("'//*'","/",$path);

  /*
  ** Remove beginning and trailing slashes.
  */
  $path = trim($path, "/");

  return $path;
}

function path_confirm_delete($id) {
  $path = get_path_from_id($id);

  $form .= form_hidden("confirm", 1);
  $form .= form_hidden("pid", $id);
Dries's avatar
 
Dries committed
88 89
  $form .= form_hidden("src", $path->src);
  $form .= form_hidden("dst", $path->dst);
Dries's avatar
Dries committed
90 91 92
  $form .= form_submit(t("Delete"));
  $form .= form_submit(t("Cancel"));

Dries's avatar
 
Dries committed
93
  return form(form_item(t("Delete alias '%dst' that maps to '%src'", array("%dst" => $path->dst, "%src" => $path->src)), $form, t("Are you sure you want to delete this alias?")));
Dries's avatar
Dries committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
}

function path_delete($pid) {
  return db_query("DELETE FROM {path} WHERE pid = '%d'", $pid);
}

function path_help($section = "admin/path/help") {
  switch ($section) {
    case "admin/system/modules":
      $output = "Enables users to create custom URLs.";
      break;
    case "admin/system/modules/path":
      $output = "Documentation yet to be written.";
      break;
    case "admin/path/help":
      $output = "Documentation yet to be written.";
      break;
  }

  return t($output);
}

function path_form($edit = "") {
Dries's avatar
 
Dries committed
117 118
  $form .= form_textfield(t("Existing path"), "src", $edit["src"], 50, 64, t("Specify the existing path you wish to alias. For example: node/view/28, forum/1, taxonomy/page/or/1,2."));
  $form .= form_textfield(t("New path alias"), "dst", $edit["dst"], 50, 64, t("Specify an alternative path by which this data can be accessed.  For example, type 'about' when writing an about page."));
Dries's avatar
Dries committed
119 120 121 122 123 124 125 126 127 128 129
  $form .= form_hidden("pid", $edit["pid"]);
  $form .= form_submit(t("Create new alias"));

  if ($edit["pid"]) {
    $form .= form_submit(t("Delete"));
  }

  return form($form);
}

function path_insert($edit) {
Dries's avatar
 
Dries committed
130
  return db_query("INSERT INTO {path} SET src = '%s', dst = '%s'", $edit["src"], $edit["dst"]);
Dries's avatar
Dries committed
131 132 133 134
}

# DELETE
function path_is_unique($path, $type) {
Dries's avatar
 
Dries committed
135 136
  if ($type == "dst") {
    return !(get_src_url($path));
Dries's avatar
Dries committed
137
  }
Dries's avatar
 
Dries committed
138
  elseif ($type == "src") {
Dries's avatar
Dries committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    return !(get_url_alias($path));
  }
}

function path_link($type, $node = NULL) {
  if ($type == "system" && user_access("alias urls")) {
    menu("admin/path", t("url aliasing"), "path_admin", "", 4);
    menu("admin/path/add", t("new alias"), "path_admin", "");
  }
}

function path_perm() {
  return array("alias urls");
}

function path_overview() {
  $sql = "SELECT * FROM {path}";
  $header = array(
Dries's avatar
 
Dries committed
157 158
    array ("data" => t("alias"), "field" => "dst", "sort" => "asc"),
    array ("data" => t("maps to"), "field" => "src"),
Dries's avatar
Dries committed
159 160 161 162 163 164
    array("data" => t("operations"), "colspan" => 2)
  );
  $sql .= tablesort_sql($header);
  $result = pager_query($sql, 50);

  while ($data = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
165
    $rows[] = array($data->dst, $data->src, l(t("edit"), "admin/path/edit/$data->pid"), l(t("delete"), "admin/path/delete/$data->pid"));
Dries's avatar
Dries committed
166 167 168 169 170 171 172 173 174 175 176
  }

  $pager = pager_display(NULL, 50, 0, "admin", tablesort_pager());
  if (!empty($pager)) {
    $rows[] = array(array("data" => $pager, "colspan" => 3));
  }

  return table($header, $rows);
}

function path_save($edit) {
Dries's avatar
 
Dries committed
177 178
  $dst = path_clean($edit["dst"]);
  $src = path_clean($edit["src"]);
Dries's avatar
Dries committed
179

Dries's avatar
 
Dries committed
180 181
  if ($src == NULL || !valid_url($src)) {
    return t("the specified path is not valid.");
Dries's avatar
Dries committed
182 183
  }

Dries's avatar
 
Dries committed
184 185
  if (db_result(db_query("SELECT COUNT(src) FROM {path} WHERE src = '%s'", $src))) {
    return t("the specified path is already aliased.");
Dries's avatar
Dries committed
186 187
  }

Dries's avatar
 
Dries committed
188 189
  if ($dst == NULL || !valid_url($dst)) {
    return t("the specified path alias is not valid.");
Dries's avatar
Dries committed
190 191
  }

Dries's avatar
 
Dries committed
192 193
  if (db_result(db_query("SELECT COUNT(dst) FROM {path} WHERE dst = '%s'", $dst))) {
    return t("the specified alias is already in use.");
Dries's avatar
Dries committed
194 195 196
  }

  if ($edit["pid"]) {
Dries's avatar
 
Dries committed
197
    path_update($src, $dst);
Dries's avatar
Dries committed
198 199 200 201 202
  }
  else { //Update the path
    path_insert($edit);
  }

Dries's avatar
 
Dries committed
203
  return t("you may access %src via %dst.", array("%src" => url($src), "%dst" => l($dst, url($dst))));
Dries's avatar
Dries committed
204 205 206 207 208 209 210 211 212 213 214
}

function path_system($field) {
  $system["description"] = path_help("admin/system/modules");
  $system["admin_help"] =  path_help("admin/system/modules/path");

  return $system[$field];
}

function path_update($edit) {
  if ($edit["pid"]) {
Dries's avatar
 
Dries committed
215
    return db_query("UPDATE {path} SET src = '%s', dst = '%s' WHERE pid = '%d'", $edit["src"], $edit["dst"], $edit["pid"]);
Dries's avatar
Dries committed
216 217
  }
  else { // intended for nodes
Dries's avatar
 
Dries committed
218
    return db_query("UPDATE {path} SET dst = '%s' WHERE src = '%s'", $edit["dst"], $edit["src"]);
Dries's avatar
Dries committed
219 220 221 222 223 224 225 226
  }
}

function get_path_from_id($id) {
  return db_fetch_object(db_query("SELECT * FROM {path} WHERE pid = '%d'", $id));
}

?>