Example-patch---add-new-via-field-for-links.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="generator" content="pandoc">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title></title>
  8. <style type="text/css">code{white-space: pre;}</style>
  9. <!--[if lt IE 9]>
  10. <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
  11. <![endif]-->
  12. <link rel="stylesheet" href="github-markdown.css">
  13. </head>
  14. <body>
  15. <p>Example patch to add a new field (&quot;via&quot;) for links, an input field to set the &quot;via&quot; property from the &quot;edit link&quot; dialog, and display the &quot;via&quot; field in the link list display. <strong>Untested, use at your own risk</strong></p>
  16. <p>Thanks to @Knah-Tsaeb in <a href="https://github.com/sebsauvage/Shaarli/pull/158">https://github.com/sebsauvage/Shaarli/pull/158</a></p>
  17. <pre><code>From e0f363c18e8fe67990ed2bb1a08652e24e70bbcb Mon Sep 17 00:00:00 2001
  18. From: Knah Tsaeb &lt;knah-tsaeb@knah-tsaeb.org&gt;
  19. Date: Fri, 11 Oct 2013 15:18:37 +0200
  20. Subject: [PATCH] Add a &quot;via&quot;/origin property for links, add new input in &quot;edit link&quot; dialog
  21. Thanks to:
  22. * https://github.com/Knah-Tsaeb/Shaarli/commit/040eb18ec8cdabd5ea855e108f81f97fbf0478c4
  23. * https://github.com/Knah-Tsaeb/Shaarli/commit/4123658eae44d7564d1128ce52ddd5689efee813
  24. * https://github.com/Knah-Tsaeb/Shaarli/commit/f1a8ca9cc8fe49b119d51b2d8382cc1a34542f96
  25. ---
  26. index.php | 43 ++++++++++++++++++++++++++++++++-----------
  27. tpl/editlink.html | 1 +
  28. tpl/linklist.html | 1 +
  29. 3 files changed, 34 insertions(+), 11 deletions(-)
  30. diff --git a/index.php b/index.php
  31. index 6fae2f8..53f798e 100644
  32. --- a/index.php
  33. +++ b/index.php
  34. @@ -436,6 +436,12 @@ if (isset($_POST[&#39;login&#39;]))
  35. // ------------------------------------------------------------------------------------------
  36. // Misc utility functions:
  37. +// Try to get just domain for @via
  38. +function getJustDomain($url){
  39. + $parts = parse_url($url);
  40. + return trim($parts[&#39;host&#39;]);
  41. + }
  42. +
  43. // Returns the server URL (including port and http/https), without path.
  44. // e.g. &quot;http://myserver.com:8080&quot;
  45. // You can append $_SERVER[&#39;SCRIPT_NAME&#39;] to get the current script URL.
  46. @@ -799,7 +805,8 @@ class linkdb implements Iterator, Countable, ArrayAccess
  47. $found= (strpos(strtolower($l[&#39;title&#39;]),$s)!==false)
  48. || (strpos(strtolower($l[&#39;description&#39;]),$s)!==false)
  49. || (strpos(strtolower($l[&#39;url&#39;]),$s)!==false)
  50. - || (strpos(strtolower($l[&#39;tags&#39;]),$s)!==false);
  51. + || (strpos(strtolower($l[&#39;tags&#39;]),$s)!==false)
  52. + || (!empty($l[&#39;via&#39;]) &amp;&amp; (strpos(strtolower($l[&#39;via&#39;]),$s)!==false));
  53. if ($found) $filtered[$l[&#39;linkdate&#39;]] = $l;
  54. }
  55. krsort($filtered);
  56. @@ -814,7 +821,7 @@ class linkdb implements Iterator, Countable, ArrayAccess
  57. $t = str_replace(&#39;,&#39;,&#39; &#39;,($casesensitive?$tags:strtolower($tags)));
  58. $searchtags=explode(&#39; &#39;,$t);
  59. $filtered=array();
  60. - foreach($this-&gt;links as $l)
  61. + foreach($this-&gt; links as $l)
  62. {
  63. $linktags = explode(&#39; &#39;,($casesensitive?$l[&#39;tags&#39;]:strtolower($l[&#39;tags&#39;])));
  64. if (count(array_intersect($linktags,$searchtags)) == count($searchtags))
  65. @@ -905,7 +912,7 @@ function showRSS()
  66. else $linksToDisplay = $LINKSDB;
  67. $nblinksToDisplay = 50; // Number of links to display.
  68. if (!empty($_GET[&#39;nb&#39;])) // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links.
  69. - {
  70. + {
  71. $nblinksToDisplay = $_GET[&#39;nb&#39;]==&#39;all&#39; ? count($linksToDisplay) : max($_GET[&#39;nb&#39;]+0,1) ;
  72. }
  73. @@ -944,7 +951,12 @@ function showRSS()
  74. // If user wants permalinks first, put the final link in description
  75. if ($usepermalinks===true) $descriptionlink = &#39;(&lt;a href=&quot;&#39;.$absurl.&#39;&quot;&gt;Link&lt;/a&gt;)&#39;;
  76. if (strlen($link[&#39;description&#39;])&gt;0) $descriptionlink = &#39;&lt;br&gt;&#39;.$descriptionlink;
  77. - echo &#39;&lt;description&gt;&lt;![CDATA[&#39;.nl2br(keepMultipleSpaces(text2clickable(htmlspecialchars($link[&#39;description&#39;])))).$descriptionlink.&#39;]]&gt;&lt;/description&gt;&#39;.&quot;\n&lt;/item&gt;\n&quot;;
  78. + if(!empty($link[&#39;via&#39;])){
  79. + $via = &#39;&lt;br&gt;Origine =&gt; &lt;a href=&quot;&#39;.htmlspecialchars($link[&#39;via&#39;]).&#39;&quot;&gt;&#39;.htmlspecialchars(getJustDomain($link[&#39;via&#39;])).&#39;&lt;/a&gt;&#39;;
  80. + } else {
  81. + $via = &#39;&#39;;
  82. + }
  83. + echo &#39;&lt;description&gt;&lt;![CDATA[&#39;.nl2br(keepMultipleSpaces(text2clickable(htmlspecialchars($link[&#39;description&#39;])))).$via.$descriptionlink.&#39;]]&gt;&lt;/description&gt;&#39;.&quot;\n&lt;/item&gt;\n&quot;;
  84. $i++;
  85. }
  86. echo &#39;&lt;/channel&gt;&lt;/rss&gt;&lt;!-- Cached version of &#39;.htmlspecialchars(pageUrl()).&#39; --&gt;&#39;;
  87. @@ -980,7 +992,7 @@ function showATOM()
  88. else $linksToDisplay = $LINKSDB;
  89. $nblinksToDisplay = 50; // Number of links to display.
  90. if (!empty($_GET[&#39;nb&#39;])) // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links.
  91. - {
  92. + {
  93. $nblinksToDisplay = $_GET[&#39;nb&#39;]==&#39;all&#39; ? count($linksToDisplay) : max($_GET[&#39;nb&#39;]+0,1) ;
  94. }
  95. @@ -1006,11 +1018,16 @@ function showATOM()
  96. // Add permalink in description
  97. $descriptionlink = htmlspecialchars(&#39;(&lt;a href=&quot;&#39;.$guid.&#39;&quot;&gt;Permalink&lt;/a&gt;)&#39;);
  98. + if(isset($link[&#39;via&#39;]) &amp;&amp; !empty($link[&#39;via&#39;])){
  99. + $via = htmlspecialchars(&#39;&lt;/br&gt; Origine =&gt; &lt;a href=&quot;&#39;.$link[&#39;via&#39;].&#39;&quot;&gt;&#39;.getJustDomain($link[&#39;via&#39;]).&#39;&lt;/a&gt;&#39;);
  100. + } else {
  101. + $via = &#39;&#39;;
  102. + }
  103. // If user wants permalinks first, put the final link in description
  104. if ($usepermalinks===true) $descriptionlink = htmlspecialchars(&#39;(&lt;a href=&quot;&#39;.$absurl.&#39;&quot;&gt;Link&lt;/a&gt;)&#39;);
  105. if (strlen($link[&#39;description&#39;])&gt;0) $descriptionlink = &#39;&amp;lt;br&amp;gt;&#39;.$descriptionlink;
  106. - $entries.=&#39;&lt;content type=&quot;html&quot;&gt;&#39;.htmlspecialchars(nl2br(keepMultipleSpaces(text2clickable(htmlspecialchars($link[&#39;description&#39;]))))).$descriptionlink.&quot;&lt;/content&gt;\n&quot;;
  107. + $entries.=&#39;&lt;content type=&quot;html&quot;&gt;&#39;.htmlspecialchars(nl2br(keepMultipleSpaces(text2clickable(htmlspecialchars($link[&#39;description&#39;]))))).$descriptionlink.$via.&quot;&lt;/content&gt;\n&quot;;
  108. if ($link[&#39;tags&#39;]!=&#39;&#39;) // Adding tags to each ATOM entry (as mentioned in ATOM specification)
  109. {
  110. foreach(explode(&#39; &#39;,$link[&#39;tags&#39;]) as $tag)
  111. @@ -1478,7 +1495,7 @@ function renderPage()
  112. if (!startsWith($url,&#39;http:&#39;) &amp;&amp; !startsWith($url,&#39;https:&#39;) &amp;&amp; !startsWith($url,&#39;ftp:&#39;) &amp;&amp; !startsWith($url,&#39;magnet:&#39;) &amp;&amp; !startsWith($url,&#39;?&#39;))
  113. $url = &#39;http://&#39;.$url;
  114. $link = array(&#39;title&#39;=&gt;trim($_POST[&#39;lf_title&#39;]),&#39;url&#39;=&gt;$url,&#39;description&#39;=&gt;trim($_POST[&#39;lf_description&#39;]),&#39;private&#39;=&gt;(isset($_POST[&#39;lf_private&#39;]) ? 1 : 0),
  115. - &#39;linkdate&#39;=&gt;$linkdate,&#39;tags&#39;=&gt;str_replace(&#39;,&#39;,&#39; &#39;,$tags));
  116. + &#39;linkdate&#39;=&gt;$linkdate,&#39;tags&#39;=&gt;str_replace(&#39;,&#39;,&#39; &#39;,$tags), &#39;via&#39;=&gt;trim($_POST[&#39;lf_via&#39;]));
  117. if ($link[&#39;title&#39;]==&#39;&#39;) $link[&#39;title&#39;]=$link[&#39;url&#39;]; // If title is empty, use the URL as title.
  118. $LINKSDB[$linkdate] = $link;
  119. $LINKSDB-&gt;savedb(); // Save to disk.
  120. @@ -1556,7 +1573,8 @@ function renderPage()
  121. $title = (empty($_GET[&#39;title&#39;]) ? &#39;&#39; : $_GET[&#39;title&#39;] ); // Get title if it was provided in URL (by the bookmarklet).
  122. $description = (empty($_GET[&#39;description&#39;]) ? &#39;&#39; : $_GET[&#39;description&#39;]); // Get description if it was provided in URL (by the bookmarklet). [Bronco added that]
  123. $tags = (empty($_GET[&#39;tags&#39;]) ? &#39;&#39; : $_GET[&#39;tags&#39;] ); // Get tags if it was provided in URL
  124. - $private = (!empty($_GET[&#39;private&#39;]) &amp;&amp; $_GET[&#39;private&#39;] === &quot;1&quot; ? 1 : 0); // Get private if it was provided in URL
  125. + $via = (empty($_GET[&#39;via&#39;]) ? &#39;&#39; : $_GET[&#39;via&#39;] );
  126. + $private = (!empty($_GET[&#39;private&#39;]) &amp;&amp; $_GET[&#39;private&#39;] === &quot;1&quot; ? 1 : 0); // Get private if it was provided in URL
  127. if (($url!=&#39;&#39;) &amp;&amp; parse_url($url,PHP_URL_SCHEME)==&#39;&#39;) $url = &#39;http://&#39;.$url;
  128. // If this is an HTTP link, we try go get the page to extract the title (otherwise we will to straight to the edit form.)
  129. if (empty($title) &amp;&amp; parse_url($url,PHP_URL_SCHEME)==&#39;http&#39;)
  130. @@ -1567,7 +1585,7 @@ function renderPage()
  131. {
  132. // Look for charset in html header.
  133. preg_match(&#39;#&lt;meta .*charset=.*&gt;#Usi&#39;, $data, $meta);
  134. -
  135. +
  136. // If found, extract encoding.
  137. if (!empty($meta[0]))
  138. {
  139. @@ -1577,7 +1595,7 @@ function renderPage()
  140. $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : &#39;utf-8&#39;;
  141. }
  142. else { $html_charset = &#39;utf-8&#39;; }
  143. -
  144. +
  145. // Extract title
  146. $title = html_extract_title($data);
  147. if (!empty($title))
  148. @@ -1592,7 +1610,7 @@ function renderPage()
  149. $url=&#39;?&#39;.smallHash($linkdate);
  150. $title=&#39;Note: &#39;;
  151. }
  152. - $link = array(&#39;linkdate&#39;=&gt;$linkdate,&#39;title&#39;=&gt;$title,&#39;url&#39;=&gt;$url,&#39;description&#39;=&gt;$description,&#39;tags&#39;=&gt;$tags,&#39;private&#39;=&gt;$private);
  153. + $link = array(&#39;linkdate&#39;=&gt;$linkdate,&#39;title&#39;=&gt;$title,&#39;url&#39;=&gt;$url,&#39;description&#39;=&gt;$description,&#39;tags&#39;=&gt;$tags,&#39;via&#39; =&gt; $via,&#39;private&#39;=&gt;$private);
  154. }
  155. $PAGE = new pageBuilder;
  156. @@ -1842,6 +1860,9 @@ function buildLinkList($PAGE,$LINKSDB)
  157. $taglist = explode(&#39; &#39;,$link[&#39;tags&#39;]);
  158. uasort($taglist, &#39;strcasecmp&#39;);
  159. $link[&#39;taglist&#39;]=$taglist;
  160. + if(!empty($link[&#39;via&#39;])){
  161. + $link[&#39;via&#39;]=htmlspecialchars($link[&#39;via&#39;]);
  162. + }
  163. $linkDisp[$keys[$i]] = $link;
  164. $i++;
  165. }
  166. diff --git a/tpl/editlink.html b/tpl/editlink.html
  167. index 4a2c30c..14d4f9c 100644
  168. --- a/tpl/editlink.html
  169. +++ b/tpl/editlink.html
  170. @@ -16,6 +16,7 @@
  171. &lt;i&gt;Title&lt;/i&gt;&lt;br&gt;&lt;input type=&quot;text&quot; name=&quot;lf_title&quot; value=&quot;{$link.title|htmlspecialchars}&quot; style=&quot;width:100%&quot;&gt;&lt;br&gt;
  172. &lt;i&gt;Description&lt;/i&gt;&lt;br&gt;&lt;textarea name=&quot;lf_description&quot; rows=&quot;4&quot; cols=&quot;25&quot; style=&quot;width:100%&quot;&gt;{$link.description|htmlspecialchars}&lt;/textarea&gt;&lt;br&gt;
  173. &lt;i&gt;Tags&lt;/i&gt;&lt;br&gt;&lt;input type=&quot;text&quot; id=&quot;lf_tags&quot; name=&quot;lf_tags&quot; value=&quot;{$link.tags|htmlspecialchars}&quot; style=&quot;width:100%&quot;&gt;&lt;br&gt;
  174. + &lt;i&gt;Origine&lt;/i&gt;&lt;br&gt;&lt;input type=&quot;text&quot; name=&quot;lf_via&quot; value=&quot;{$link.via|htmlspecialchars}&quot; style=&quot;width:100%&quot;&gt;&lt;br&gt;
  175. {if condition=&quot;($link_is_new &amp;&amp; $GLOBALS[&#39;privateLinkByDefault&#39;]==true) || $link.private == true&quot;}
  176. &lt;input type=&quot;checkbox&quot; checked=&quot;checked&quot; name=&quot;lf_private&quot; id=&quot;lf_private&quot;&gt;
  177. &amp;nbsp;&lt;label for=&quot;lf_private&quot;&gt;&lt;i&gt;Private&lt;/i&gt;&lt;/label&gt;&lt;br&gt;
  178. diff --git a/tpl/linklist.html b/tpl/linklist.html
  179. index ddc38cb..0a8475f 100644
  180. --- a/tpl/linklist.html
  181. +++ b/tpl/linklist.html
  182. @@ -43,6 +43,7 @@
  183. &lt;span class=&quot;linktitle&quot;&gt;&lt;a href=&quot;{$redirector}{$value.url|htmlspecialchars}&quot;&gt;{$value.title|htmlspecialchars}&lt;/a&gt;&lt;/span&gt;
  184. &lt;br&gt;
  185. {if=&quot;$value.description&quot;}&lt;div class=&quot;linkdescription&quot;{if condition=&quot;$search_type==&#39;permalink&#39;&quot;} style=&quot;max-height:none !important;&quot;{/if}&gt;{$value.description}&lt;/div&gt;{/if}
  186. + {if condition=&quot;isset($value.via) &amp;&amp; !empty($value.via)&quot;}&lt;div&gt;&lt;a href=&quot;{$value.via}&quot;&gt;Origine =&gt; {$value.via|getJustDomain}&lt;/a&gt;&lt;/div&gt;{/if}
  187. {if=&quot;!$GLOBALS[&#39;config&#39;][&#39;HIDE_TIMESTAMPS&#39;] || isLoggedIn()&quot;}
  188. &lt;span class=&quot;linkdate&quot; title=&quot;Permalink&quot;&gt;&lt;a href=&quot;?{$value.linkdate|smallHash}&quot;&gt;{$value.localdate|htmlspecialchars} - permalink&lt;/a&gt; - &lt;/span&gt;
  189. {else}
  190. --
  191. 2.1.1</code></pre>
  192. </body>
  193. </html>