+}
+
+function original_url($url, $depth=1, $fetchbody = false) {
+
+ $a = get_app();
+
+ // Remove Analytics Data from Google and other tracking platforms
+ $urldata = parse_url($url);
+ if (is_string($urldata["query"])) {
+ $query = $urldata["query"];
+ parse_str($query, $querydata);
+
+ if (is_array($querydata))
+ foreach ($querydata AS $param=>$value)
+ if (in_array($param, array("utm_source", "utm_medium", "utm_term", "utm_content", "utm_campaign",
+ "wt_mc", "pk_campaign", "pk_kwd", "mc_cid", "mc_eid",
+ "fb_action_ids", "fb_action_types", "fb_ref",
+ "awesm", "wtrid",
+ "woo_campaign", "woo_source", "woo_medium", "woo_content", "woo_term"))) {
+
+ $pair = $param."=".urlencode($value);
+ $url = str_replace($pair, "", $url);
+
+ // Second try: if the url isn't encoded completely
+ $pair = $param."=".str_replace(" ", "+", $value);
+ $url = str_replace($pair, "", $url);
+
+ // Third try: Maybey the url isn't encoded at all
+ $pair = $param."=".$value;
+ $url = str_replace($pair, "", $url);
+
+ $url = str_replace(array("?&", "&&"), array("?", ""), $url);
+ }
+
+ if (substr($url, -1, 1) == "?")
+ $url = substr($url, 0, -1);
+ }
+
+ if ($depth > 10)
+ return($url);
+
+ $url = trim($url, "'");
+
+ $stamp1 = microtime(true);
+
+ $siteinfo = array();
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, $url);
+ curl_setopt($ch, CURLOPT_HEADER, 1);
+ curl_setopt($ch, CURLOPT_NOBODY, 1);
+ curl_setopt($ch, CURLOPT_TIMEOUT, 10);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
+
+ $header = curl_exec($ch);
+ $curl_info = @curl_getinfo($ch);
+ $http_code = $curl_info['http_code'];
+ curl_close($ch);
+
+ $a->save_timestamp($stamp1, "network");
+
+ if ((($curl_info['http_code'] == "301") OR ($curl_info['http_code'] == "302"))
+ AND (($curl_info['redirect_url'] != "") OR ($curl_info['location'] != ""))) {
+ if ($curl_info['redirect_url'] != "")
+ return(original_url($curl_info['redirect_url'], ++$depth, $fetchbody));
+ else
+ return(original_url($curl_info['location'], ++$depth, $fetchbody));
+ }
+
+ // Check for redirects in the meta elements of the body if there are no redirects in the header.
+ if (!$fetchbody)
+ return(original_url($url, ++$depth, true));
+
+ // if the file is too large then exit
+ if ($curl_info["download_content_length"] > 1000000)
+ return($url);
+
+ // if it isn't a HTML file then exit
+ if (($curl_info["content_type"] != "") AND !strstr(strtolower($curl_info["content_type"]),"html"))
+ return($url);
+
+ $stamp1 = microtime(true);
+
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, $url);
+ curl_setopt($ch, CURLOPT_HEADER, 0);
+ curl_setopt($ch, CURLOPT_NOBODY, 0);
+ curl_setopt($ch, CURLOPT_TIMEOUT, 10);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
+
+ $body = curl_exec($ch);
+ curl_close($ch);
+
+ $a->save_timestamp($stamp1, "network");
+
+ if (trim($body) == "")
+ return($url);
+
+ // Check for redirect in meta elements
+ $doc = new DOMDocument();
+ @$doc->loadHTML($body);
+
+ $xpath = new DomXPath($doc);
+
+ $list = $xpath->query("//meta[@content]");
+ foreach ($list as $node) {
+ $attr = array();
+ if ($node->attributes->length)
+ foreach ($node->attributes as $attribute)
+ $attr[$attribute->name] = $attribute->value;
+
+ if (@$attr["http-equiv"] == 'refresh') {
+ $path = $attr["content"];
+ $pathinfo = explode(";", $path);
+ $content = "";
+ foreach ($pathinfo AS $value)
+ if (substr(strtolower($value), 0, 4) == "url=")
+ return(original_url(substr($value, 4), ++$depth));
+ }
+ }
+
+ return($url);
+}
+
+if (!function_exists('short_link')) {
+function short_link($url) {
+ require_once('library/slinky.php');
+ $slinky = new Slinky($url);
+ $yourls_url = get_config('yourls','url1');
+ if ($yourls_url) {
+ $yourls_username = get_config('yourls','username1');
+ $yourls_password = get_config('yourls', 'password1');
+ $yourls_ssl = get_config('yourls', 'ssl1');
+ $yourls = new Slinky_YourLS();
+ $yourls->set('username', $yourls_username);
+ $yourls->set('password', $yourls_password);
+ $yourls->set('ssl', $yourls_ssl);
+ $yourls->set('yourls-url', $yourls_url);
+ $slinky->set_cascade( array($yourls, new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL()));
+ } else {
+ // setup a cascade of shortening services
+ // try to get a short link from these services
+ // in the order ur1.ca, trim, id.gd, tinyurl
+ $slinky->set_cascade(array(new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL()));
+ }
+ return $slinky->short();
+}};