All Data Structures Functions Variables Pages
TextUtil.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
40  class TextUtil
41  {
47  public static function strToLowerWithDefaultEncoding($string)
48  {
49  assert('is_string($string)');
50  return mb_strtolower($string, Yii::app()->charset);
51  }
52 
57  public static function textWithUrlToTextWithLink($text)
58  {
59  assert('is_string($text)');
60  // Begin Not Coding Standard
61  $rexProtocol = '(https?://)?';
62  $rexDomain = '(?:[-a-zA-Z0-9]{1,63}\.)+[a-zA-Z][-a-zA-Z0-9]{1,62}';
63  $rexIp = '(?:[1-9][0-9]{0,2}\.|0\.){3}(?:[1-9][0-9]{0,2}|0)';
64  $rexPort = '(:[0-9]{1,5})?';
65  $rexPath = '(/[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]*?)?';
66  $rexQuery = '(\?[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
67  $rexFragment = '(#[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
68  $rexUsername = '[^]\\\\\x00-\x20\"(),:-<>[\x7f-\xff]{1,64}';
69  $rexPassword = $rexUsername;
70  $rexUrl = "$rexProtocol(?:($rexUsername)(:$rexPassword)?@)?($rexDomain|$rexIp)($rexPort$rexPath$rexQuery$rexFragment)";
71  $rexUrlLinker = "{\\b$rexUrl(?=[?.!,;:\"]?(\s|$))}";
72  // End Not Coding Standard
73  $html = '';
74 
75  $position = 0;
76  $validTlds = array_fill_keys(explode(" ", ".ac .ad .ae .aero .af .ag .ai .al .am .an .ao .aq .ar .arpa .as .asia .at .au .aw .ax .az .ba .bb " .
77  ".bd .be .bf .bg .bh .bi .biz .bj .bm .bn .bo .br .bs .bt .bv .bw .by .bz .ca .cat .cc .cd .cf .cg " .
78  ".ch .ci .ck .cl .cm .cn .co .com .coop .cr .cu .cv .cx .cy .cz .de .dj .dk .dm .do .dz .ec .edu .ee " .
79  ".eg .er .es .et .eu .fi .fj .fk .fm .fo .fr .ga .gb .gd .ge .gf .gg .gh .gi .gl .gm .gn .gov .gp .gq " .
80  ".gr .gs .gt .gu .gw .gy .hk .hm .hn .hr .ht .hu .id .ie .il .im .in .info .int .io .iq .ir .is .it .je ".
81  ".jm .jo .jobs .jp .ke .kg .kh .ki .km .kn .kp .kr .kw .ky .kz .la .lb .lc .li .lk .lr .ls .lt .lu .lv " .
82  ".ly .ma .mc .md .me .mg .mh .mil .mk .ml .mm .mn .mo .mobi .mp .mq .mr .ms .mt .mu .museum .mv .mw .mx " .
83  ".my .mz .na .name .nc .ne .net .nf .ng .ni .nl .no .np .nr .nu .nz .om .org .pa .pe .pf .pg .ph .pk .pl " .
84  ".pm .pn .pr .pro .ps .pt .pw .py .qa .re .ro .rs .ru .rw .sa .sb .sc .sd .se .sg .sh .si .sj .sk .sl .sm " .
85  ".sn .so .sr .st .su .sv .sy .sz .tc .td .tel .tf .tg .th .tj .tk .tl .tm .tn .to .tp .tr .travel .tt .tv " .
86  ".tw .tz .ua .ug .uk .us .uy .uz .va .vc .ve .vg .vi .vn .vu .wf .ws "), true);
87 
88  while (preg_match($rexUrlLinker, $text, $match, PREG_OFFSET_CAPTURE, $position))
89  {
90  list($url, $urlPosition) = $match[0];
91 
92  // Add the text leading up to the URL.
93  $html .= substr($text, $position, $urlPosition - $position);
94 
95  $protocol = $match[1][0];
96  $username = $match[2][0];
97  $password = $match[3][0];
98  $domain = $match[4][0];
99  $afterDomain = $match[5][0]; // everything following the domain
100  $port = $match[6][0];
101  $path = $match[7][0];
102 
103  // Check that the TLD is valid or that $domain is an IP address.
104  $tld = strtolower(strrchr($domain, '.'));
105  if (preg_match('{^\.[0-9]{1,3}$}', $tld) || isset($validTlds[$tld])) // Not Coding Standard
106  {
107  // Do not permit implicit protocol if a password is specified, as
108  // this causes too many errors (e.g. "my email:foo@example.org").
109  if (!$protocol && $password)
110  {
111  $html .= $username;
112 
113  // Continue text parsing at the ':' following the "username".
114  $position = $urlPosition + strlen($username);
115  continue;
116  }
117 
118  if (!$protocol && $username && !$password && !$afterDomain)
119  {
120  // Looks like an email address.
121  $completeUrl = "mailto:$url";
122  $linkText = $url;
123  $linkHtml = '<a href="' . $completeUrl . '">' . $linkText . '</a>';
124  }
125  else
126  {
127  // Prepend http:// if no protocol specified
128  if ($protocol)
129  {
130  $linkHtml = '<a href="' . $url . '">' . $url . '</a>';
131  }
132  else
133  {
134  $completeUrl = "http://$url";
135  $linkHtml = '<a href="' . $completeUrl . '">' . $url . '</a>';
136  }
137  }
138 
139  // Cheap e-mail obfuscation to trick the dumbest mail harvesters.
140  $linkHtml = str_replace('@', '@', $linkHtml);
141 
142  // Add the hyperlink.
143  $html .= $linkHtml;
144  }
145  else
146  {
147  // Not a valid URL.
148  $html .= $url;
149  }
150 
151  // Continue text parsing from after the URL.
152  $position = $urlPosition + strlen($url);
153  }
154  // Add the remainder of the text.
155  $html .= substr($text, $position);
156  return nl2br($html);
157  }
158  }
159 ?>
static strToLowerWithDefaultEncoding($string)
Definition: TextUtil.php:47
static textWithUrlToTextWithLink($text)
Definition: TextUtil.php:57
Generated on Thu Apr 9 2020 07:10:39