1 #Download CSS styles from an OPML list
2 ###Download CSS styles for shaarlis listed in an opml file
7 <!---- Copyright (c) 2014 Nicolas Delsaux (https://github.com/Riduidel) -->
8 <!---- License: zlib (http://www.gzip.org/zlib/zlib_license.html) -->
11 * Source: https://github.com/Riduidel
12 * Download css styles for shaarlis listed in an opml file
14 define("SHAARLI_RSS_OPML", "https://www.ecirtam.net/shaarlirss/custom/people.opml");
16 define("THEMES_TEMP_FOLDER", "new_themes");
18 if(!file_exists(THEMES_TEMP_FOLDER)) {
19 mkdir(THEMES_TEMP_FOLDER);
22 function siteUrl($pathInSite) {
23 $indexPos = strpos($pathInSite, "index.php");
27 return substr($pathInSite, 0, $indexPos);
31 function createShaarliHashFromOPMLL($opmlFile) {
33 $opml = file_get_contents($opmlFile);
34 $opmlXml = simplexml_load_string($opml);
35 $outlineElements = $opmlXml->xpath("body/outline");
36 foreach($outlineElements as $site) {
37 $siteUrl = siteUrl((string) $site['htmlUrl']);[](.html)
38 $result[$siteUrl]=((string) $site['text']);[](.html)
43 function getSiteFolder($url) {
44 $domain = parse_url($url, PHP_URL_HOST);
45 return THEMES_TEMP_FOLDER."/".str_replace(".", "_", $domain);
48 function get_http_response_code($theURL) {
49 $headers = get_headers($theURL);
50 return substr($headers[0], 9, 3);[](.html)
54 * This makes the code PHP-5 only (particularly the call to "get_headers")
56 function copyUserStyleFrom($url, $name, $knownStyles) {
57 $userStyle = $url."inc/user.css";
58 if(in_array($url, $knownStyles)) {
59 // TODO add log message
61 $statusCode = get_http_response_code($userStyle);
62 if(intval($statusCode)<300) {
63 $styleSheet = file_get_contents($userStyle);
64 $siteFolder = getSiteFolder($url);
65 if(!file_exists($siteFolder)) {
68 if(!file_exists($siteFolder.'/user.css')) {
70 file_put_contents($siteFolder.'/user.css', $styleSheet);
72 if(!file_exists($siteFolder.'/README.md')) {
73 // Then write a readme.md file
74 file_put_contents($siteFolder.'/README.md',
75 "User style from ".$name."\n"
76 ."============================="
78 ."This stylesheet was downloaded from ".$userStyle." on ".date(DATE_RFC822)
81 if(!file_exists($siteFolder.'/config.ini')) {
82 // Write a config file containing useful informations
83 file_put_contents($siteFolder.'/config.ini',
85 ."site_name=".$name."\n"
88 if(!file_exists($siteFolder.'/home.png')) {
89 // And finally copy generated thumbnail
90 $homeThumb = $siteFolder.'/home.png';
91 file_put_contents($siteFolder.'/home.png', file_get_contents(getThumbnailUrl($url)));
93 echo 'Theme have been downloaded from <a href="'.$url.'">'.$url.'</a> into '.$siteFolder
94 .'. It looks like <img src="'.$homeThumb.'"><br/>';
99 function getThumbnailUrl($url) {
100 return 'http://api.webthumbnail.org/?url='.$url;
103 function copyUserStylesFrom($urlToNames, $knownStyles) {
104 foreach($urlToNames as $url => $name) {
105 copyUserStyleFrom($url, $name, $knownStyles);
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 (".") and parent one("..")
113 * are not listed here
115 function getDirectoryList ($directory) {
116 $realPath = realpath($directory);
117 // create an array to hold directory list
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't this directory or its parent, add it to the results
124 if ($file != "." && $file != "..") {
125 $results[ = realpath($realPath . "/" . $file);](-=-realpath($realPath-.-"/"-.-$file);.html)
128 // tidy up: close the handler
135 * Start in themes folder and look in all subfolders for config.ini files.
136 * These config.ini files allow us not to download styles again and again
138 function findKnownStyles() {
140 $subFolders = getDirectoryList("themes");
141 foreach($subFolders as $folder) {
142 $configFile = $folder."/config.ini";
143 if(file_exists($configFile)) {
144 $iniParameters = parse_ini_file($configFile);
145 array_push($result, $iniParameters['site_url']);[](.html)
151 $knownStyles = findKnownStyles();
152 copyUserStylesFrom(createShaarliHashFromOPMLL(SHAARLI_RSS_OPML), $knownStyles);