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