5 <meta name=
"generator" content=
"pandoc">
6 <meta name=
"viewport" content=
"width=device-width, initial-scale=1.0, user-scalable=yes">
8 <style type=
"text/css">code{white-space: pre;}
</style>
10 <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
12 <link rel=
"stylesheet" href=
"github-markdown.css">
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><!---- ?php --
>
18 <!---- Copyright (c)
2014 Nicolas Delsaux (https://github.com/Riduidel) --
>
19 <!---- License: zlib (http://www.gzip.org/zlib/zlib_license.html) --
>
22 * Source: https://github.com/Riduidel
23 * Download css styles for shaarlis listed in an opml file
25 define(
"SHAARLI_RSS_OPML
",
"https://www.ecirtam.net/shaarlirss/custom/people.opml
");
27 define(
"THEMES_TEMP_FOLDER
",
"new_themes
");
29 if(!file_exists(THEMES_TEMP_FOLDER)) {
30 mkdir(THEMES_TEMP_FOLDER);
33 function siteUrl($pathInSite) {
34 $indexPos = strpos($pathInSite,
"index.php
");
38 return substr($pathInSite,
0, $indexPos);
42 function createShaarliHashFromOPMLL($opmlFile) {
44 $opml = file_get_contents($opmlFile);
45 $opmlXml = simplexml_load_string($opml);
46 $outlineElements = $opmlXml-
>xpath(
"body/outline
");
47 foreach($outlineElements as $site) {
48 $siteUrl = siteUrl((string) $site[
'htmlUrl
']);
49 $result[$siteUrl]=((string) $site[
'text
']);
54 function getSiteFolder($url) {
55 $domain = parse_url($url, PHP_URL_HOST);
56 return THEMES_TEMP_FOLDER.
"/
".str_replace(
".
",
"_
", $domain);
59 function get_http_response_code($theURL) {
60 $headers = get_headers($theURL);
61 return substr($headers[
0],
9,
3);
65 * This makes the code PHP-
5 only (particularly the call to
"get_headers
")
67 function copyUserStyleFrom($url, $name, $knownStyles) {
68 $userStyle = $url.
"inc/user.css
";
69 if(in_array($url, $knownStyles)) {
70 // TODO add log message
72 $statusCode = get_http_response_code($userStyle);
73 if(intval($statusCode)
<300) {
74 $styleSheet = file_get_contents($userStyle);
75 $siteFolder = getSiteFolder($url);
76 if(!file_exists($siteFolder)) {
79 if(!file_exists($siteFolder.
'/user.css
')) {
81 file_put_contents($siteFolder.
'/user.css
', $styleSheet);
83 if(!file_exists($siteFolder.
'/README.md
')) {
84 // Then write a readme.md file
85 file_put_contents($siteFolder.
'/README.md
',
86 "User style from
".$name.
"\n
"
87 .
"=============================
"
89 .
"This stylesheet was downloaded from
".$userStyle.
" on
".date(DATE_RFC822)
92 if(!file_exists($siteFolder.
'/config.ini
')) {
93 // Write a config file containing useful informations
94 file_put_contents($siteFolder.
'/config.ini
',
95 "site_url=
".$url.
"\n
"
96 .
"site_name=
".$name.
"\n
"
99 if(!file_exists($siteFolder.
'/home.png
')) {
100 // And finally copy generated thumbnail
101 $homeThumb = $siteFolder.
'/home.png
';
102 file_put_contents($siteFolder.
'/home.png
', file_get_contents(getThumbnailUrl($url)));
104 echo
'Theme have been downloaded from
<a href=
"'.$url.
'">'.$url.
'</a
> into
'.$siteFolder
105 .
'. It looks like
<img src=
"'.$homeThumb.
'"><br/
>';
110 function getThumbnailUrl($url) {
111 return
'http://api.webthumbnail.org/?url=
'.$url;
114 function copyUserStylesFrom($urlToNames, $knownStyles) {
115 foreach($urlToNames as $url =
> $name) {
116 copyUserStyleFrom($url, $name, $knownStyles);
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 (
".
") and parent one(
"..
")
124 * are not listed here
126 function getDirectoryList ($directory) {
127 $realPath = realpath($directory);
128 // create an array to hold directory list
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
't this directory or its parent, add it to the results
135 if ($file !=
".
" && $file !=
"..
") {
136 $results[] = realpath($realPath .
"/
" . $file);
139 // tidy up: close the handler
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
149 function findKnownStyles() {
151 $subFolders = getDirectoryList(
"themes
");
152 foreach($subFolders as $folder) {
153 $configFile = $folder.
"/config.ini
";
154 if(file_exists($configFile)) {
155 $iniParameters = parse_ini_file($configFile);
156 array_push($result, $iniParameters[
'site_url
']);
162 $knownStyles = findKnownStyles();
163 copyUserStylesFrom(createShaarliHashFromOPMLL(SHAARLI_RSS_OPML), $knownStyles);
165 <!--- ? ----
></code></pre>