load->helper("url"); } public function index() { $this->load->view("url_create"); } public function redirect() { $subs = explode("/", uri_string()); $slug = $subs[3]; $this->load->model("Urls_table"); $url = $this->Urls_table->getUrlForSlug($slug); $this->Urls_table->incrementHits($slug); if ($url) { // Redirect to url or to a warning page, // depending on the urls perceived reputation. // Give the site the benefit of the doubt if we // can get no information about it. $rep = $this->_getUrlReputation($url); if ($rep && is_array($rep)) { $min = 100; foreach ($rep as $name => $r) $min = $min < $r["reputation"] ? $min : $r["reputation"]; if ($min < 60) redirect("/url/warning/$slug"); } header("location: $url"); exit(); } else { redirect("/"); } } public function warning() { $subs = explode("/", uri_string()); $slug = $subs[3]; $this->load->model("Urls_table"); $url = $this->Urls_table->getUrlForSlug($slug); if ($url) { $rep = $this->_getUrlReputation($url); $hostname = parse_url($url, PHP_URL_HOST); $data = Array("slug" => $slug, "url" => $url, "rep" => $rep, "hostname" => $hostname); $this->load->view("url_warning", $data); } else { redirect("/"); } } public function create() { $url = $this->input->post("url"); $url = filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED); if (!$url) redirect("/"); $this->load->model("Urls_table"); $n = 5; $i = 0; do { $slug = $this->Urls_table->generateSlug($n); if ($n < 10 && ++$i % 5 == 0) ++$n; } while ($this->Urls_table->doesSlugExist($slug)); $date = new DateTime(); $date = $date->format("Y-m-d H:i:s"); $slug = $this->Urls_table->createUrlRow($url, $slug, $date, 0, 0); redirect("/url/view/$slug"); } public function view() { $slug = $this->uri->segment(3); $this->load->model("Urls_table"); if ($url = $this->Urls_table->getUrlForSlug($slug)) { $rep = $this->_getUrlReputation($url); $hostname = parse_url($url, PHP_URL_HOST); $hits = $this->Urls_table->getHitsForSlug($slug); $data = Array("slug" => $slug, "url" => $url, "rep" => $rep, "hostname" => $hostname, "hits" => $hits); $this->load->view("url_view", $data); } else { redirect("/"); } } public function privacy() { $this->load->view("url_privacy"); } private function _getUrlReputation($url) { // URL reputation data provided by WOT // More info at http://www.mywot.com/wiki/API $attributes = array(0 => "Trustworthiness", 1 => "Vendor reliability", 2 => "Privacy", 4 => "Child safety"); $r = new HttpRequest("http://api.mywot.com/0.4/public_query2", HttpRequest::METH_GET); $r->setOptions(array("timeout" => 3)); $r->addQueryData(array("url" => $url)); try { $r->send(); if ($r->getResponseCode() == 200) { // parse the XML response $doc = new DOMDocument(); $doc->loadXML($r->getResponseBody()); $qs = $doc->getElementsByTagName("query"); if (!$qs || $qs->length < 1) return false; $q = $qs->item(0); $apps = $q->getElementsByTagName("application"); if (!$apps) return false; $reps = array(); $min = 100; for ($i = 0; $i < $apps->length; ++$i) { $a = $apps->item($i); $name = (int)$a->attributes->getNamedItem("name")->nodeValue; $rep = (int)$a->attributes->getNamedItem("r")->nodeValue; $min = $min < $rep ? $min : $rep; $conf = (int)$a->attributes->getNamedItem("c")->nodeValue; $reps[$attributes[$name]] = array("reputation" => $rep, "confidence" => $conf); } return $reps; } } catch (HttpException $ex) { return false; } } }