cvs: docweb /include lib_url_entities.inc.php /templates/all/www entities.tpl.php /www entities.php

[email protected] ("Sean Coates")
Newsgroups php.doc.web
Message-ID <cvssean1106714945@cvsserver>
sean		Tue Jan 25 23:49:05 2005 EDT

  Modified files:              
    /docweb/include	lib_url_entities.inc.php 
    /docweb/templates/all/www	entities.tpl.php 
    /docweb/www	entities.php 
  Log:
  - CS
  - now, entities are anchored properly
  - hyperlink callback works intelligently (doesn't try to entity-anchor links)
  - html entities are excluded from entity anchors
  - moved logic out of template
  
  
http://cvs.php.net/diff.php/docweb/include/lib_url_entities.inc.php?r1=1.12&r2=1.13&ty=u
Index: docweb/include/lib_url_entities.inc.php
diff -u docweb/include/lib_url_entities.inc.php:1.12 docweb/include/lib_url_entities.inc.php:1.13
--- docweb/include/lib_url_entities.inc.php:1.12	Tue Jan 25 17:21:01 2005
+++ docweb/include/lib_url_entities.inc.php	Tue Jan 25 23:49:04 2005
@@ -19,7 +19,7 @@
 |              Mehdi Achour <[email protected]>                            |
 |              Sean Coates <[email protected]>                              |
 +----------------------------------------------------------------------+
-$Id: lib_url_entities.inc.php,v 1.12 2005/01/25 22:21:01 sean Exp $
+$Id: lib_url_entities.inc.php,v 1.13 2005/01/26 04:49:04 sean Exp $
 */
 
 // user agent
@@ -296,24 +296,69 @@
     sqlite_query($sqlite, $sql);
 }
 
-function url_callback($m)
+/**
+ * Turns email addresses and URLs into links (for entities)
+ *
+ * @param string $eVal Text to (possibly) link
+ * @return string html-linked text
+ */
+function ent_link($eVal)
 {
+    // generic mail match regex
+    $mailRegex = '!^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$!';
+    if (preg_match($mailRegex, $eVal)) {
+        $eVal = "<a href='mailto:$eVal'>$eVal</a>";
+    } else {
+        $urlRegex = '#(http|https|ftp|news)://([^\s]+)#S';
+        $eVal = preg_replace_callback($urlRegex, 'url_callback', $eVal);
+    }
+    return $eVal;
+}
 
-  $url = $m[1] .'://'. $m[3];
-  $link = str_chop($url, 60, true);
-
-  $html = "<a href='$url' title='$url'>$link</a>";
-  return $html;
+/**
+ * Turns embedded entities into anchor links
+ *
+ * @param string $eVal Text to (possibly) alter
+ * @return string html-linked text
+ */
+function ent_anchors($eVal)
+{
+    $entityRegex = '/&([^;]+);/';
+    $eVal = preg_replace_callback($entityRegex, 'anchor_callback', $eVal);
+    return $eVal;
 }
 
-function ent_value($eVal)
+/**
+ * preg_replace_callback callback for ent_link()
+ *
+ * @param array $m results of match
+ * @return string
+ */
+function url_callback($m)
 {
-  return preg_replace_callback('#((f|ht)tps?)://([^\s]+)#S', 'url_callback', $eVal);
+    $url = $m[1] .'://'. $m[2];
+    $link = str_chop($url, 60, true);
+
+    $html = "<a href='$url' title='$url'>$link</a>";
+    return $html;
 }
 
-function ent_link($eVal)
+/**
+ * preg_replace_callback callback for ent_anchors()
+ *
+ * @param array $m results of match
+ * @return string
+ */
+function anchor_callback($m)
 {
-return preg_replace('/&([^;]+);/', '<a href="entities.php#ent-\1">&amp;\1;</a>', $eVal);
+    $htmlEntities = get_html_translation_table(HTML_ENTITIES);
+    $htmlEntities["'"] = '&apos;'; // hack; this isn't in the src
+
+    if (in_array("&{$m[1]};", $htmlEntities)) {
+        return "&{$m[1]};";
+    } else {
+        return "<a href='entities.php#ent-{$m[1]}'>&amp;{$m[1]};</a>";
+    }
 }
 
 /**
http://cvs.php.net/diff.php/docweb/templates/all/www/entities.tpl.php?r1=1.1&r2=1.2&ty=u
Index: docweb/templates/all/www/entities.tpl.php
diff -u docweb/templates/all/www/entities.tpl.php:1.1 docweb/templates/all/www/entities.tpl.php:1.2
--- docweb/templates/all/www/entities.tpl.php:1.1	Tue Jan 25 00:42:49 2005
+++ docweb/templates/all/www/entities.tpl.php	Tue Jan 25 23:49:05 2005
@@ -9,8 +9,8 @@
 
  <?php foreach ($entData as $eID => $eVal) { ?>
   <tr>
-   <td><a name="ent-<?php echo $eID;?>"><?php echo $eID; ?></a></td>
-   <td><?php echo ent_value(ent_link(strip_tags($eVal))); ?></td>
+   <td valign="top"><a name="ent-<?php echo $eID;?>"><?php echo $eID; ?></a></td>
+   <td valign="top"><?php echo $eVal; ?></td>
   </tr>
  <?php } ?>
 </table>
http://cvs.php.net/diff.php/docweb/www/entities.php?r1=1.1&r2=1.2&ty=u
Index: docweb/www/entities.php
diff -u docweb/www/entities.php:1.1 docweb/www/entities.php:1.2
--- docweb/www/entities.php:1.1	Tue Jan 25 00:42:49 2005
+++ docweb/www/entities.php	Tue Jan 25 23:49:05 2005
@@ -15,7 +15,7 @@
 +----------------------------------------------------------------------+
 | Authors: Sean Coates <[email protected]>                                  |
 +----------------------------------------------------------------------+
-$Id: entities.php,v 1.1 2005/01/25 05:42:49 sean Exp $
+$Id: entities.php,v 1.2 2005/01/26 04:49:05 sean Exp $
 */
 
 require_once '../include/init.inc.php';
@@ -43,7 +43,12 @@
 ";
 $entsQ = sqlite_query($sqlite, $sql);
 while ($row = sqlite_fetch_array($entsQ)) {
-    $entData[$row['entid']] = $row['value'];
+    $entData[$row['entid']] = strip_tags($row['value']);
+    if (substr($row['entid'], 0, 4) == 'url.') {
+        $entData[$row['entid']] = ent_link($entData[$row['entid']]);
+    } else {
+        $entData[$row['entid']] = ent_anchors(ent_link($entData[$row['entid']]));
+    }
 }
 
 echo site_header('docweb.common.header.entities');
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.