]>
Commit | Line | Data |
---|---|---|
992af0b9 | 1 | #Download CSS styles from an OPML list |
1acc87ee | 2 | ###Download CSS styles for shaarlis listed in an opml file |
3 | Example php script: | |
4 | ||
992af0b9 | 5 | ```php |
1acc87ee | 6 | <!---- ?php --> |
7 | <!---- Copyright (c) 2014 Nicolas Delsaux (https://github.com/Riduidel) --> | |
8 | <!---- License: zlib (http://www.gzip.org/zlib/zlib_license.html) --> | |
9 | ||
10 | /** | |
11 | * Source: https://github.com/Riduidel | |
12 | * Download css styles for shaarlis listed in an opml file | |
13 | */ | |
14 | define("SHAARLI_RSS_OPML", "https://www.ecirtam.net/shaarlirss/custom/people.opml"); | |
15 | ||
16 | define("THEMES_TEMP_FOLDER", "new_themes"); | |
17 | ||
18 | if(!file_exists(THEMES_TEMP_FOLDER)) { | |
19 | mkdir(THEMES_TEMP_FOLDER); | |
20 | } | |
21 | ||
22 | function siteUrl($pathInSite) { | |
23 | $indexPos = strpos($pathInSite, "index.php"); | |
24 | if(!$indexPos) { | |
25 | return $pathInSite; | |
26 | } else { | |
27 | return substr($pathInSite, 0, $indexPos); | |
28 | } | |
29 | } | |
30 | ||
31 | function createShaarliHashFromOPMLL($opmlFile) { | |
32 | $result = array(); | |
33 | $opml = file_get_contents($opmlFile); | |
34 | $opmlXml = simplexml_load_string($opml); | |
35 | $outlineElements = $opmlXml->xpath("body/outline"); | |
36 | foreach($outlineElements as $site) { | |
992af0b9 V |
37 | $siteUrl = siteUrl((string) $site['htmlUrl']);[](.html) |
38 | $result[$siteUrl]=((string) $site['text']);[](.html) | |
1acc87ee | 39 | } |
40 | return $result; | |
41 | } | |
42 | ||
43 | function getSiteFolder($url) { | |
44 | $domain = parse_url($url, PHP_URL_HOST); | |
45 | return THEMES_TEMP_FOLDER."/".str_replace(".", "_", $domain); | |
46 | } | |
47 | ||
48 | function get_http_response_code($theURL) { | |
49 | $headers = get_headers($theURL); | |
992af0b9 | 50 | return substr($headers[0], 9, 3);[](.html) |
1acc87ee | 51 | } |
52 | ||
53 | /** | |
54 | * This makes the code PHP-5 only (particularly the call to "get_headers") | |
55 | */ | |
56 | function copyUserStyleFrom($url, $name, $knownStyles) { | |
57 | $userStyle = $url."inc/user.css"; | |
58 | if(in_array($url, $knownStyles)) { | |
59 | // TODO add log message | |
60 | } else { | |
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)) { | |
66 | mkdir($siteFolder); | |
67 | } | |
68 | if(!file_exists($siteFolder.'/user.css')) { | |
69 | // Copy stylesheet | |
70 | file_put_contents($siteFolder.'/user.css', $styleSheet); | |
71 | } | |
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 | ."=============================" | |
77 | ."\n\n" | |
78 | ."This stylesheet was downloaded from ".$userStyle." on ".date(DATE_RFC822) | |
79 | ); | |
80 | } | |
81 | if(!file_exists($siteFolder.'/config.ini')) { | |
82 | // Write a config file containing useful informations | |
83 | file_put_contents($siteFolder.'/config.ini', | |
84 | "site_url=".$url."\n" | |
85 | ."site_name=".$name."\n" | |
86 | ); | |
87 | } | |
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))); | |
92 | } | |
93 | echo 'Theme have been downloaded from <a href="'.$url.'">'.$url.'</a> into '.$siteFolder | |
94 | .'. It looks like <img src="'.$homeThumb.'"><br/>'; | |
95 | } | |
96 | } | |
97 | } | |
98 | ||
99 | function getThumbnailUrl($url) { | |
100 | return 'http://api.webthumbnail.org/?url='.$url; | |
101 | } | |
102 | ||
103 | function copyUserStylesFrom($urlToNames, $knownStyles) { | |
104 | foreach($urlToNames as $url => $name) { | |
105 | copyUserStyleFrom($url, $name, $knownStyles); | |
106 | } | |
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 (".") and parent one("..") | |
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't this directory or its parent, add it to the results | |
124 | if ($file != "." && $file != "..") { | |
992af0b9 | 125 | $results[ = realpath($realPath . "/" . $file);](-=-realpath($realPath-.-"/"-.-$file);.html) |
1acc87ee | 126 | } |
127 | } | |
128 | // tidy up: close the handler | |
129 | closedir($handler); | |
130 | // done! | |
131 | return $results; | |
132 | } | |
133 | ||
134 | /** | |
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 | |
137 | */ | |
138 | function findKnownStyles() { | |
139 | $result = array(); | |
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); | |
992af0b9 | 145 | array_push($result, $iniParameters['site_url']);[](.html) |
1acc87ee | 146 | } |
147 | } | |
148 | return $result; | |
149 | } | |
150 | ||
151 | $knownStyles = findKnownStyles(); | |
152 | copyUserStylesFrom(createShaarliHashFromOPMLL(SHAARLI_RSS_OPML), $knownStyles); | |
153 | ||
154 | <!--- ? ----> | |
155 | ``` |