Download-CSS-styles-for-shaarlis-listed-in-an-opml-file.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. <h3 id="download-css-styles-for-shaarlis-listed-in-an-opml-file">Download CSS styles for shaarlis listed in an opml file</h3>
  16. <p>Example php script:</p>
  17. <pre><code>&lt;!---- ?php --&gt;
  18. &lt;!---- Copyright (c) 2014 Nicolas Delsaux (https://github.com/Riduidel) --&gt;
  19. &lt;!---- License: zlib (http://www.gzip.org/zlib/zlib_license.html) --&gt;
  20. /**
  21. * Source: https://github.com/Riduidel
  22. * Download css styles for shaarlis listed in an opml file
  23. */
  24. define(&quot;SHAARLI_RSS_OPML&quot;, &quot;https://www.ecirtam.net/shaarlirss/custom/people.opml&quot;);
  25. define(&quot;THEMES_TEMP_FOLDER&quot;, &quot;new_themes&quot;);
  26. if(!file_exists(THEMES_TEMP_FOLDER)) {
  27. mkdir(THEMES_TEMP_FOLDER);
  28. }
  29. function siteUrl($pathInSite) {
  30. $indexPos = strpos($pathInSite, &quot;index.php&quot;);
  31. if(!$indexPos) {
  32. return $pathInSite;
  33. } else {
  34. return substr($pathInSite, 0, $indexPos);
  35. }
  36. }
  37. function createShaarliHashFromOPMLL($opmlFile) {
  38. $result = array();
  39. $opml = file_get_contents($opmlFile);
  40. $opmlXml = simplexml_load_string($opml);
  41. $outlineElements = $opmlXml-&gt;xpath(&quot;body/outline&quot;);
  42. foreach($outlineElements as $site) {
  43. $siteUrl = siteUrl((string) $site[&#39;htmlUrl&#39;]);
  44. $result[$siteUrl]=((string) $site[&#39;text&#39;]);
  45. }
  46. return $result;
  47. }
  48. function getSiteFolder($url) {
  49. $domain = parse_url($url, PHP_URL_HOST);
  50. return THEMES_TEMP_FOLDER.&quot;/&quot;.str_replace(&quot;.&quot;, &quot;_&quot;, $domain);
  51. }
  52. function get_http_response_code($theURL) {
  53. $headers = get_headers($theURL);
  54. return substr($headers[0], 9, 3);
  55. }
  56. /**
  57. * This makes the code PHP-5 only (particularly the call to &quot;get_headers&quot;)
  58. */
  59. function copyUserStyleFrom($url, $name, $knownStyles) {
  60. $userStyle = $url.&quot;inc/user.css&quot;;
  61. if(in_array($url, $knownStyles)) {
  62. // TODO add log message
  63. } else {
  64. $statusCode = get_http_response_code($userStyle);
  65. if(intval($statusCode)&lt;300) {
  66. $styleSheet = file_get_contents($userStyle);
  67. $siteFolder = getSiteFolder($url);
  68. if(!file_exists($siteFolder)) {
  69. mkdir($siteFolder);
  70. }
  71. if(!file_exists($siteFolder.&#39;/user.css&#39;)) {
  72. // Copy stylesheet
  73. file_put_contents($siteFolder.&#39;/user.css&#39;, $styleSheet);
  74. }
  75. if(!file_exists($siteFolder.&#39;/README.md&#39;)) {
  76. // Then write a readme.md file
  77. file_put_contents($siteFolder.&#39;/README.md&#39;,
  78. &quot;User style from &quot;.$name.&quot;\n&quot;
  79. .&quot;=============================&quot;
  80. .&quot;\n\n&quot;
  81. .&quot;This stylesheet was downloaded from &quot;.$userStyle.&quot; on &quot;.date(DATE_RFC822)
  82. );
  83. }
  84. if(!file_exists($siteFolder.&#39;/config.ini&#39;)) {
  85. // Write a config file containing useful informations
  86. file_put_contents($siteFolder.&#39;/config.ini&#39;,
  87. &quot;site_url=&quot;.$url.&quot;\n&quot;
  88. .&quot;site_name=&quot;.$name.&quot;\n&quot;
  89. );
  90. }
  91. if(!file_exists($siteFolder.&#39;/home.png&#39;)) {
  92. // And finally copy generated thumbnail
  93. $homeThumb = $siteFolder.&#39;/home.png&#39;;
  94. file_put_contents($siteFolder.&#39;/home.png&#39;, file_get_contents(getThumbnailUrl($url)));
  95. }
  96. echo &#39;Theme have been downloaded from &lt;a href=&quot;&#39;.$url.&#39;&quot;&gt;&#39;.$url.&#39;&lt;/a&gt; into &#39;.$siteFolder
  97. .&#39;. It looks like &lt;img src=&quot;&#39;.$homeThumb.&#39;&quot;&gt;&lt;br/&gt;&#39;;
  98. }
  99. }
  100. }
  101. function getThumbnailUrl($url) {
  102. return &#39;http://api.webthumbnail.org/?url=&#39;.$url;
  103. }
  104. function copyUserStylesFrom($urlToNames, $knownStyles) {
  105. foreach($urlToNames as $url =&gt; $name) {
  106. copyUserStyleFrom($url, $name, $knownStyles);
  107. }
  108. }
  109. /**
  110. * Reading directory list, courtesy of http://www.laughing-buddha.net/php/dirlist/
  111. * @param directory the directory we want to list files of
  112. * @return a simple array containing the list of absolute file paths. Notice that current file (&quot;.&quot;) and parent one(&quot;..&quot;)
  113. * are not listed here
  114. */
  115. function getDirectoryList ($directory) {
  116. $realPath = realpath($directory);
  117. // create an array to hold directory list
  118. $results = array();
  119. // create a handler for the directory
  120. $handler = opendir($directory);
  121. // open directory and walk through the filenames
  122. while ($file = readdir($handler)) {
  123. // if file isn&#39;t this directory or its parent, add it to the results
  124. if ($file != &quot;.&quot; &amp;&amp; $file != &quot;..&quot;) {
  125. $results[] = realpath($realPath . &quot;/&quot; . $file);
  126. }
  127. }
  128. // tidy up: close the handler
  129. closedir($handler);
  130. // done!
  131. return $results;
  132. }
  133. /**
  134. * Start in themes folder and look in all subfolders for config.ini files.
  135. * These config.ini files allow us not to download styles again and again
  136. */
  137. function findKnownStyles() {
  138. $result = array();
  139. $subFolders = getDirectoryList(&quot;themes&quot;);
  140. foreach($subFolders as $folder) {
  141. $configFile = $folder.&quot;/config.ini&quot;;
  142. if(file_exists($configFile)) {
  143. $iniParameters = parse_ini_file($configFile);
  144. array_push($result, $iniParameters[&#39;site_url&#39;]);
  145. }
  146. }
  147. return $result;
  148. }
  149. $knownStyles = findKnownStyles();
  150. copyUserStylesFrom(createShaarliHashFromOPMLL(SHAARLI_RSS_OPML), $knownStyles);
  151. &lt;!--- ? ----&gt;</code></pre>
  152. </body>
  153. </html>