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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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>Shaarli – Example patch add new via field for links</title>
  8. <style type="text/css">code{white-space: pre;}</style>
  9. <link rel="stylesheet" href="github-markdown.css">
  10. <!--[if lt IE 9]>
  11. <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
  12. <![endif]-->
  13. </head>
  14. <body>
  15. <div id="local-sidebar">
  16. <ul>
  17. <li><a href="Home.html">Home</a></li>
  18. <li>Setup
  19. <ul>
  20. <li><a href="Download-and-Installation.html">Download and Installation</a></li>
  21. <li><a href="Upgrade-and-migration.html">Upgrade and migration</a></li>
  22. <li><a href="Server-requirements.html">Server requirements</a></li>
  23. <li><a href="Server-configuration.html">Server configuration</a></li>
  24. <li><a href="Server-security.html">Server security</a></li>
  25. <li><a href="Shaarli-configuration.html">Shaarli configuration</a></li>
  26. <li><a href="Plugins.html">Plugins</a></li>
  27. </ul></li>
  28. <li><a href="Docker.html">Docker</a></li>
  29. <li><a href="Usage.html">Usage</a>
  30. <ul>
  31. <li><a href="Sharing-button.html">Sharing button</a> (bookmarklet)</li>
  32. <li><a href="Browsing-and-Searching.html">Browsing and Searching</a></li>
  33. <li><a href="Firefox-share.html">Firefox share</a></li>
  34. <li><a href="RSS-feeds.html">RSS feeds</a></li>
  35. </ul></li>
  36. <li>How To
  37. <ul>
  38. <li><a href="Backup,-restore,-import-and-export.html">Backup, restore, import and export</a></li>
  39. <li><a href="Copy-an-existing-installation-over-SSH-and-serve-it-locally.html">Copy an existing installation over SSH and serve it locally</a></li>
  40. <li><a href="Create-and-serve-multiple-Shaarlis-(farm).html">Create and serve multiple Shaarlis (farm)</a></li>
  41. <li><a href="Download-CSS-styles-from-an-OPML-list.html">Download CSS styles from an OPML list</a></li>
  42. <li><a href="Datastore-hacks.html">Datastore hacks</a></li>
  43. </ul></li>
  44. <li><a href="Troubleshooting.html">Troubleshooting</a></li>
  45. <li><a href="Development.html">Development</a>
  46. <ul>
  47. <li><a href="GnuPG-signature.html">GnuPG signature</a></li>
  48. <li><a href="Coding-guidelines.html">Coding guidelines</a></li>
  49. <li><a href="Directory-structure.html">Directory structure</a></li>
  50. <li><a href="3rd-party-libraries.html">3rd party libraries</a></li>
  51. <li><a href="Plugin-System.html">Plugin System</a></li>
  52. <li><a href="Release-Shaarli.html">Release Shaarli</a></li>
  53. <li><a href="Security.html">Security</a></li>
  54. <li><a href="Static-analysis.html">Static analysis</a></li>
  55. <li><a href="Theming.html">Theming</a></li>
  56. <li><a href="Unit-tests.html">Unit tests</a></li>
  57. </ul></li>
  58. <li>About
  59. <ul>
  60. <li><a href="FAQ.html">FAQ</a></li>
  61. <li><a href="Community-&amp;-Related-software.html">Community &amp; Related software</a></li>
  62. </ul></li>
  63. </ul>
  64. </div>
  65. <h1 id="example-patch-add-new-via-field-for-links">Example patch add new via field for links</h1>
  66. <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>
  67. <p>Thanks to @Knah-Tsaeb in <a href="https://github.com/sebsauvage/Shaarli/pull/158" class="uri">https://github.com/sebsauvage/Shaarli/pull/158</a></p>
  68. <pre><code>From e0f363c18e8fe67990ed2bb1a08652e24e70bbcb Mon Sep 17 00:00:00 2001
  69. From: Knah Tsaeb &lt;knah-tsaeb@knah-tsaeb.org&gt;
  70. Date: Fri, 11 Oct 2013 15:18:37 +0200
  71. Subject: [PATCH] Add a &quot;via&quot;/origin property for links, add new input in &quot;edit link&quot; dialog[](.html)
  72. Thanks to:
  73. * https://github.com/Knah-Tsaeb/Shaarli/commit/040eb18ec8cdabd5ea855e108f81f97fbf0478c4
  74. * https://github.com/Knah-Tsaeb/Shaarli/commit/4123658eae44d7564d1128ce52ddd5689efee813
  75. * https://github.com/Knah-Tsaeb/Shaarli/commit/f1a8ca9cc8fe49b119d51b2d8382cc1a34542f96
  76. ---
  77. index.php | 43 ++++++++++++++++++++++++++++++++-----------
  78. tpl/editlink.html | 1 +
  79. tpl/linklist.html | 1 +
  80. 3 files changed, 34 insertions(+), 11 deletions(-)
  81. diff --git a/index.php b/index.php
  82. index 6fae2f8..53f798e 100644
  83. --- a/index.php
  84. +++ b/index.php
  85. @@ -436,6 +436,12 @@ if (isset($_POST[&#39;login&#39;]))[](.html)
  86. // ------------------------------------------------------------------------------------------
  87. // Misc utility functions:
  88. +// Try to get just domain for @via
  89. +function getJustDomain($url){
  90. + $parts = parse_url($url);
  91. + return trim($parts[&#39;host&#39;]);[](.html)
  92. + }
  93. +
  94. // Returns the server URL (including port and http/https), without path.
  95. // e.g. &quot;http://myserver.com:8080&quot;
  96. // You can append $_SERVER[&#39;SCRIPT_NAME&#39;] to get the current script URL.[](.html)
  97. @@ -799,7 +805,8 @@ class linkdb implements Iterator, Countable, ArrayAccess
  98. $found= (strpos(strtolower($l[&#39;title&#39;]),$s)!==false)[](.html)
  99. || (strpos(strtolower($l[&#39;description&#39;]),$s)!==false)[](.html)
  100. || (strpos(strtolower($l[&#39;url&#39;]),$s)!==false)[](.html)
  101. - || (strpos(strtolower($l[&#39;tags&#39;]),$s)!==false);[](.html)
  102. + || (strpos(strtolower($l[&#39;tags&#39;]),$s)!==false)[](.html)
  103. + || (!empty($l[&#39;via&#39;]) &amp;&amp; (strpos(strtolower($l[&#39;via&#39;]),$s)!==false));[](.html)
  104. if ($found) $filtered[$l[&#39;linkdate&#39;[ = $l;](-=-$l;.html)
  105. }
  106. krsort($filtered);
  107. @@ -814,7 +821,7 @@ class linkdb implements Iterator, Countable, ArrayAccess
  108. $t = str_replace(&#39;,&#39;,&#39; &#39;,($casesensitive?$tags:strtolower($tags)));
  109. $searchtags=explode(&#39; &#39;,$t);
  110. $filtered=array();
  111. - foreach($this-&gt;links as $l)
  112. + foreach($this-&gt; links as $l)
  113. {
  114. $linktags = explode(&#39; &#39;,($casesensitive?$l[&#39;tags&#39;]:strtolower($l[&#39;tags&#39;])));[](.html)
  115. if (count(array_intersect($linktags,$searchtags)) == count($searchtags))
  116. @@ -905,7 +912,7 @@ function showRSS()
  117. else $linksToDisplay = $LINKSDB;
  118. $nblinksToDisplay = 50; // Number of links to display.
  119. if (!empty($_GET[&#39;nb&#39;])) // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links.[](.html)
  120. - {
  121. + {
  122. $nblinksToDisplay = $_GET[&#39;nb&#39;]==&#39;all&#39; ? count($linksToDisplay) : max($_GET[&#39;nb&#39;]+0,1) ;[](.html)
  123. }
  124. @@ -944,7 +951,12 @@ function showRSS()
  125. // If user wants permalinks first, put the final link in description
  126. if ($usepermalinks===true) $descriptionlink = &#39;(&lt;a href=&quot;&#39;.$absurl.&#39;&quot;&gt;Link&lt;/a&gt;)&#39;;
  127. if (strlen($link[&#39;description&#39;])&gt;0) $descriptionlink = &#39;&lt;br&gt;&#39;.$descriptionlink;[](.html)
  128. - 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;;](&gt;&lt;/description&gt;&#39;.&quot;\n&lt;/item&gt;\n&quot;;.html)
  129. + if(!empty($link[&#39;via&#39;])){[](.html)
  130. + $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;;[](.html)
  131. + } else {
  132. + $via = &#39;&#39;;
  133. + }
  134. + 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;;](&gt;&lt;/description&gt;&#39;.&quot;\n&lt;/item&gt;\n&quot;;.html)
  135. $i++;
  136. }
  137. echo &#39;&lt;/channel&gt;&lt;/rss&gt;&lt;!-- Cached version of &#39;.htmlspecialchars(pageUrl()).&#39; --&gt;&#39;;
  138. @@ -980,7 +992,7 @@ function showATOM()
  139. else $linksToDisplay = $LINKSDB;
  140. $nblinksToDisplay = 50; // Number of links to display.
  141. if (!empty($_GET[&#39;nb&#39;])) // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links.[](.html)
  142. - {
  143. + {
  144. $nblinksToDisplay = $_GET[&#39;nb&#39;]==&#39;all&#39; ? count($linksToDisplay) : max($_GET[&#39;nb&#39;]+0,1) ;[](.html)
  145. }
  146. @@ -1006,11 +1018,16 @@ function showATOM()
  147. // Add permalink in description
  148. $descriptionlink = htmlspecialchars(&#39;(&lt;a href=&quot;&#39;.$guid.&#39;&quot;&gt;Permalink&lt;/a&gt;)&#39;);
  149. + if(isset($link[&#39;via&#39;]) &amp;&amp; !empty($link[&#39;via&#39;])){[](.html)
  150. + $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;);[](.html)
  151. + } else {
  152. + $via = &#39;&#39;;
  153. + }
  154. // If user wants permalinks first, put the final link in description
  155. if ($usepermalinks===true) $descriptionlink = htmlspecialchars(&#39;(&lt;a href=&quot;&#39;.$absurl.&#39;&quot;&gt;Link&lt;/a&gt;)&#39;);
  156. if (strlen($link[&#39;description&#39;])&gt;0) $descriptionlink = &#39;&amp;lt;br&amp;gt;&#39;.$descriptionlink;[](.html)
  157. - $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;;[](.html)
  158. + $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;;[](.html)
  159. if ($link[&#39;tags&#39;]!=&#39;&#39;) // Adding tags to each ATOM entry (as mentioned in ATOM specification)[](.html)
  160. {
  161. foreach(explode(&#39; &#39;,$link[&#39;tags&#39;]) as $tag)[](.html)
  162. @@ -1478,7 +1495,7 @@ function renderPage()
  163. 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;))
  164. $url = &#39;http://&#39;.$url;
  165. $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),[](.html)
  166. - &#39;linkdate&#39;=&gt;$linkdate,&#39;tags&#39;=&gt;str_replace(&#39;,&#39;,&#39; &#39;,$tags));
  167. + &#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;]));[](.html)
  168. if ($link[&#39;title&#39;]==&#39;&#39;) $link[&#39;title&#39;]=$link[&#39;url&#39;]; // If title is empty, use the URL as title.[](.html)
  169. $LINKSDB[$linkdate] = $link;[](.html)
  170. $LINKSDB-&gt;savedb(); // Save to disk.
  171. @@ -1556,7 +1573,8 @@ function renderPage()
  172. $title = (empty($_GET[&#39;title&#39;]) ? &#39;&#39; : $_GET[&#39;title&#39;] ); // Get title if it was provided in URL (by the bookmarklet).[](.html)
  173. $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][](.html)
  174. $tags = (empty($_GET[&#39;tags&#39;]) ? &#39;&#39; : $_GET[&#39;tags&#39;] ); // Get tags if it was provided in URL[](.html)
  175. - $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 [](.html)
  176. + $via = (empty($_GET[&#39;via&#39;]) ? &#39;&#39; : $_GET[&#39;via&#39;] );[](.html)
  177. + $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[](.html)
  178. if (($url!=&#39;&#39;) &amp;&amp; parse_url($url,PHP_URL_SCHEME)==&#39;&#39;) $url = &#39;http://&#39;.$url;
  179. // 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.)
  180. if (empty($title) &amp;&amp; parse_url($url,PHP_URL_SCHEME)==&#39;http&#39;)
  181. @@ -1567,7 +1585,7 @@ function renderPage()
  182. {
  183. // Look for charset in html header.
  184. preg_match(&#39;#&lt;meta .*charset=.*&gt;#Usi&#39;, $data, $meta);
  185. -
  186. +
  187. // If found, extract encoding.
  188. if (!empty($meta[0]))[](.html)
  189. {
  190. @@ -1577,7 +1595,7 @@ function renderPage()
  191. $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : &#39;utf-8&#39;;[](.html)
  192. }
  193. else { $html_charset = &#39;utf-8&#39;; }
  194. -
  195. +
  196. // Extract title
  197. $title = html_extract_title($data);
  198. if (!empty($title))
  199. @@ -1592,7 +1610,7 @@ function renderPage()
  200. $url=&#39;?&#39;.smallHash($linkdate);
  201. $title=&#39;Note: &#39;;
  202. }
  203. - $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);
  204. + $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);
  205. }
  206. $PAGE = new pageBuilder;
  207. @@ -1842,6 +1860,9 @@ function buildLinkList($PAGE,$LINKSDB)
  208. $taglist = explode(&#39; &#39;,$link[&#39;tags&#39;]);[](.html)
  209. uasort($taglist, &#39;strcasecmp&#39;);
  210. $link[&#39;taglist&#39;]=$taglist;[](.html)
  211. + if(!empty($link[&#39;via&#39;])){[](.html)
  212. + $link[&#39;via&#39;]=htmlspecialchars($link[&#39;via&#39;]);[](.html)
  213. + }
  214. $linkDisp[$keys[$i[ = $link;](-=-$link;.html)
  215. $i++;
  216. }
  217. diff --git a/tpl/editlink.html b/tpl/editlink.html
  218. index 4a2c30c..14d4f9c 100644
  219. --- a/tpl/editlink.html
  220. +++ b/tpl/editlink.html
  221. @@ -16,6 +16,7 @@
  222. &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;
  223. &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;
  224. &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;
  225. + &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;
  226. {if condition=&quot;($link_is_new &amp;&amp; $GLOBALS[&#39;privateLinkByDefault&#39;]==true) || $link.private == true&quot;}[](.html)
  227. &lt;input type=&quot;checkbox&quot; checked=&quot;checked&quot; name=&quot;lf_private&quot; id=&quot;lf_private&quot;&gt;
  228. &amp;nbsp;&lt;label for=&quot;lf_private&quot;&gt;&lt;i&gt;Private&lt;/i&gt;&lt;/label&gt;&lt;br&gt;
  229. diff --git a/tpl/linklist.html b/tpl/linklist.html
  230. index ddc38cb..0a8475f 100644
  231. --- a/tpl/linklist.html
  232. +++ b/tpl/linklist.html
  233. @@ -43,6 +43,7 @@
  234. &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;
  235. &lt;br&gt;
  236. {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}
  237. + {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}
  238. {if=&quot;!$GLOBALS[&#39;config&#39;[&#39;HIDE_TIMESTAMPS&#39;] || isLoggedIn()&quot;}](&#39;HIDE_TIMESTAMPS&#39;]-||-isLoggedIn()&quot;}.html)
  239. &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;
  240. {else}
  241. --
  242. 2.1.1</code></pre>
  243. </body>
  244. </html>