aboutsummaryrefslogtreecommitdiffhomepage
path: root/doc/Download-CSS-styles-from-an-OPML-list.md
blob: eb66f955121740856df3964b34d49b59949077a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#Download CSS styles from an OPML list
###Download CSS styles for shaarlis listed in an opml file
Example php script:

```php
<!---- ?php -->
<!---- Copyright (c) 2014 Nicolas Delsaux (https://github.com/Riduidel) -->
<!---- License: zlib (http://www.gzip.org/zlib/zlib_license.html) -->

/**
 * Source: https://github.com/Riduidel
 * Download css styles for shaarlis listed in an opml file
 */
define("SHAARLI_RSS_OPML", "https://www.ecirtam.net/shaarlirss/custom/people.opml");

define("THEMES_TEMP_FOLDER", "new_themes");

if(!file_exists(THEMES_TEMP_FOLDER)) {
	mkdir(THEMES_TEMP_FOLDER);
}

function siteUrl($pathInSite) {
	$indexPos = strpos($pathInSite, "index.php");
	if(!$indexPos) {
		return $pathInSite;
	} else {
		return substr($pathInSite, 0, $indexPos);
	}
}

function createShaarliHashFromOPMLL($opmlFile) {
	$result = array();
	$opml = file_get_contents($opmlFile);
	$opmlXml = simplexml_load_string($opml);
	$outlineElements = $opmlXml->xpath("body/outline");
	foreach($outlineElements as $site) {
		$siteUrl = siteUrl((string) $site['htmlUrl']);[](.html)
		$result[$siteUrl]=((string) $site['text']);[](.html)
	}
	return $result;
}

function getSiteFolder($url) {
	$domain = parse_url($url,  PHP_URL_HOST);
	return THEMES_TEMP_FOLDER."/".str_replace(".", "_", $domain);
}

function get_http_response_code($theURL) {
     $headers = get_headers($theURL);
     return substr($headers[0], 9, 3);[](.html)
}

/**
 * This makes the code PHP-5 only (particularly the call to "get_headers")
 */
function copyUserStyleFrom($url, $name, $knownStyles) {
	$userStyle = $url."inc/user.css";
	if(in_array($url, $knownStyles)) {
		// TODO add log message
	} else {
		$statusCode = get_http_response_code($userStyle);
		if(intval($statusCode)<300) {
			$styleSheet = file_get_contents($userStyle);
			$siteFolder = getSiteFolder($url);
			if(!file_exists($siteFolder)) {
				mkdir($siteFolder);
			}
			if(!file_exists($siteFolder.'/user.css')) {
				// Copy stylesheet
				file_put_contents($siteFolder.'/user.css', $styleSheet);
			}
			if(!file_exists($siteFolder.'/README.md')) {
				// Then write a readme.md file
				file_put_contents($siteFolder.'/README.md', 
					"User style from ".$name."\n"
					."============================="
					."\n\n"
					."This stylesheet was downloaded from ".$userStyle." on ".date(DATE_RFC822)
					);
			}
			if(!file_exists($siteFolder.'/config.ini')) {
				// Write a config file containing useful informations
				file_put_contents($siteFolder.'/config.ini', 
					"site_url=".$url."\n"
					."site_name=".$name."\n"
					);
			}
			if(!file_exists($siteFolder.'/home.png')) {
				// And finally copy generated thumbnail
				$homeThumb = $siteFolder.'/home.png';
				file_put_contents($siteFolder.'/home.png', file_get_contents(getThumbnailUrl($url)));
			}
			echo 'Theme have been downloaded from  <a href="'.$url.'">'.$url.'</a> into '.$siteFolder
				.'. It looks like <img src="'.$homeThumb.'"><br/>';
		}
	}
}

function getThumbnailUrl($url) {
	return 'http://api.webthumbnail.org/?url='.$url;
}

function copyUserStylesFrom($urlToNames, $knownStyles) {
	foreach($urlToNames as $url => $name) {
		copyUserStyleFrom($url, $name, $knownStyles);
	}
}

/**
 * Reading directory list, courtesy of http://www.laughing-buddha.net/php/dirlist/
 * @param directory the directory we want to list files of
 * @return a simple array containing the list of absolute file paths. Notice that current file (".") and parent one("..")
 * are not listed here
 */
function getDirectoryList ($directory)  {
    $realPath = realpath($directory);
    // create an array to hold directory list
    $results = array();
    // create a handler for the directory
    $handler = opendir($directory);
    // open directory and walk through the filenames
    while ($file = readdir($handler)) {
        // if file isn't this directory or its parent, add it to the results
        if ($file != "." && $file != "..") {
			$results[ = realpath($realPath . "/" . $file);](-=-realpath($realPath-.-"/"-.-$file);.html)
        }
    }
    // tidy up: close the handler
    closedir($handler);
    // done!
    return $results;
}

/**
 * Start in themes folder and look in all subfolders for config.ini files. 
 * These config.ini files allow us not to download styles again and again
 */
function findKnownStyles() {
	$result = array();
	$subFolders = getDirectoryList("themes");
	foreach($subFolders as $folder) {
		$configFile = $folder."/config.ini";
		if(file_exists($configFile)) {
			$iniParameters = parse_ini_file($configFile);
			array_push($result, $iniParameters['site_url']);[](.html)
		}
	}
	return $result;
}

$knownStyles = findKnownStyles();
copyUserStylesFrom(createShaarliHashFromOPMLL(SHAARLI_RSS_OPML), $knownStyles);

<!--- ? ---->
```