1<?php
2
3require_once dirname(__FILE__).'/pfcglobalconfig.class.php';
4
5/**
6 * Rewritten by Nathan Codding - Feb 6, 2001.
7 * - Goes through the given string, and replaces xxxx://yyyy with an HTML <a> tag linking
8 *   to that URL
9 * - Goes through the given string, and replaces www.xxxx.yyyy[zzzz] with an HTML <a> tag linking
10 *   to http://www.xxxx.yyyy[/zzzz]
11 * - Goes through the given string, and replaces xxxx@yyyy with an HTML mailto: tag linking
12 *    to that email address
13 * - Only matches these 2 patterns either after a space, or at the beginning of a line
14 *
15 * Notes: the email one might get annoying - it's easy to make it more restrictive, though.. maybe
16 * have it require something like xxxx@yyyy.zzzz or such. We'll see.
17 */
18function pfc_make_hyperlink($text)
19{
20  $c =& pfcGlobalConfig::Instance();
21  $openlinknewwindow = $c->openlinknewwindow;
22
23  if ($openlinknewwindow)
24    $target = " onclick=\"window.open(this.href,\\'_blank\\');return false;\"";
25  else
26    $target = "";
27
28  $text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1&#058;", $text);
29
30  // pad it with a space so we can match things at the start of the 1st line.
31  $ret = ' ' . $text;
32
33  // matches an "xxxx://yyyy" URL at the start of a line, or after a space.
34  // xxxx can only be alpha characters.
35  // yyyy is anything up to the first space, newline, comma, double quote or <
36  //$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
37  $ret = preg_replace("#(^|[\n \]])([\w]+?://[\w\#$%&~/.\-;:=,?@+]*)#ise", "'\\1<a href=\"\\2\"" . $target . ">' . pfc_shorten_url('\\2') . '</a>'", $ret);
38
39  // matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing
40  // Must contain at least 2 dots. xxxx contains either alphanum, or "-"
41  // zzzz is optional.. will contain everything up to the first space, newline,
42  // comma, double quote or <.
43  //$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
44  $ret = preg_replace("#(^|[\n \]])((www|ftp)\.[\w\#$%&~/.\-;:=,?@+]*)#ise", "'\\1<a href=\"http://\\2\"" . $target . ">' . pfc_shorten_url('\\2') . '</a>'", $ret);
45
46  // matches an email@domain type address at the start of a line, or after a space.
47  // Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
48  //$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
49  $ret = preg_replace("#(^|[\n \]])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#ie", "'\\1<a href=\"mailto:\\2@\\3\">' . pfc_shorten_url('\\2@\\3') . '</a>'", $ret);
50
51  // Remove our padding..
52  $ret = substr($ret, 1);
53
54  return($ret);
55}
56
57/**
58 * Nathan Codding - Feb 6, 2001
59 * Reverses the effects of make_clickable(), for use in editpost.
60 * - Does not distinguish between "www.xxxx.yyyy" and "http://aaaa.bbbb" type URLs.
61 *
62 */
63function pfc_undo_make_hyperlink($text)
64{
65  $text = preg_replace("#<!-- BBcode auto-mailto start --><a href=\"mailto:(.*?)\".*?>.*?</a><!-- BBCode auto-mailto end -->#i", "\\1", $text);
66  $text = preg_replace("#<!-- BBCode auto-link start --><a href=\"(.*?)\".*?>.*?</a><!-- BBCode auto-link end -->#i", "\\1", $text);
67
68  return $text;
69
70}
71
72function pfc_shorten_url($url)
73{
74  $c =& pfcGlobalConfig::Instance();
75
76  if (! $c->short_url)
77    return $url;
78
79  // Short URL Width
80  $shurl_w = $c->short_url_width;
81
82  $shurl_end_w = floor($shurl_w * .25) - 3;
83  if ($shurl_end_w < 3) $shurl_end_w = 3;
84  $shurl_begin_w = $shurl_w - $shurl_end_w - 3;
85  if ($shurl_begin_w < 3) $shurl_begin_w = 3;
86
87  $decodedurl = html_entity_decode($url, ENT_QUOTES);
88
89  $len = strlen($decodedurl);
90  $short_url = ($len > $shurl_w) ? substr($decodedurl, 0, $shurl_begin_w) . "..." . substr($decodedurl, -$shurl_end_w) : $decodedurl;
91
92  return htmlentities($short_url, ENT_QUOTES);
93}
94
95?>
96