forked from atutor/ATutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheadstuff.php
122 lines (100 loc) · 4.14 KB
/
headstuff.php
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
<?php
/************************************************************************/
/* ATutor */
/************************************************************************/
/* Copyright (c) 2002-2010 */
/* Inclusive Design Institute */
/* http://atutor.ca */
/* This program is free software. You can redistribute it and/or */
/* modify it under the terms of the GNU General Public License */
/* as published by the Free Software Foundation. */
/************************************************************************/
define('AT_INCLUDE_PATH', 'include/');
require(AT_INCLUDE_PATH . 'vitals.inc.php');
// hack for eXe content, remove now
header ('content-type: text/css');
echo('@import url(exestyles.css);');
exit;
if (isset ($_GET['cid'])){
$cid = intval ($_GET['cid']);
$sql = "SELECT * FROM ".TABLE_PREFIX."head WHERE course_id=$_SESSION[course_id] AND content_id=$cid";
$result = mysql_query($sql, $db);
if (($result != 0) && ($row = mysql_fetch_assoc($result))){
// get the CSS used
$styleText = get_styles ($row['text'], $_GET['path']);
header ('content-type: text/css');
echo($styleText);
}
}
// Takes the contents of the 'head' section and returns the 'style' and 'link' elements.
function get_styles ($headText, $path) {
$styleText = '';
// get the contents of all the 'style' elements
$styleStartPos = strpos ($headText, '<style');
while ($styleStartPos !== false) {
// remove the start and end 'style' elements
// (we're using just the contents of the 'style' element)
$styleStartPos = strPos ($headText, '>', $styleStartPos) + 1;
$styleEndPos = strPos ($headText, '</style', $styleStartPos) -1;
// contents of 'style' element
$tempString = substr ($headText, $styleStartPos, ($styleEndPos - $styleStartPos) + 1);
// add full path to any 'import' statements
$importStartPos = strpos ($tempString, '@import');
if ($importStartPos !== false){
$bracketStartPos = strpos ($tempString, '(', $importStartPos);
if ($bracketStartPos !== false){
$bracketEndPos = strpos ($tempString, ')', $bracketStartPos);
if ($bracketEndPos !== false){
$uriCss = trim (substr ($tempString, $bracketStartPos + 1, ($bracketEndPos - $bracketStartPos)));
$tempString2 = substr ($tempString, '0', $bracketStartPos);
$tempString = $tempString2.'('.$path.$uriCss.substr ($tempString, $bracketEndPos + '1');
}
}
}
$styleText = $styleText."\n".$tempString;
// look for another 'style' element
$styleStartPos = strpos ($headText, '<style', $styleEndPos);
}
// get all the links to external stylesheets
$linkStartPos = strpos ($headText, '<link');
while ($linkStartPos !== false) {
$linkEndPos = strPos ($headText, '>', $linkStartPos);
// ensure this is a 'stylesheet' link
$stylesheetPos = strpos ($headText, "\"text/css\"", $linkStartPos);
if (($stylesheetPos !== false) && ($stylesheetPos < $linkEndPos)){
// get the 'href' attribute value
$hrefPos = strpos ($headText, 'href', $linkStartPos);
if ($hrefPos !== false){
$hrefPos = strpos ($headText, '=', $hrefPos);
if ($hrefPos !== false){
$hrefPos += '1';
// get first character in attribute value
$index = $hrefPos;
// find first attribute character
while (($headText[$index] == " ") || ($headText[$index] == '\'') || ($headText[$index] == "\"")){
$index++;
if ($index > strlen($headText)){
break;
}
}
$indexStart = $index;
// find end of attribute character
$indexEnd = $indexStart;
while (($headText[$indexEnd] != " ") && ($headText[$indexEnd] != '\'') && ($headText[$indexEnd] != "\"")){
$indexEnd++;
if ($index > strlen($headText)){
break;
}
}
}
}
// convert the href attribute value to an "import url" statement
$importStatement = '@import url('.$path.substr ($headText, $indexStart, ($indexEnd - $indexStart)).');';
$styleText = $styleText."\n".$importStatement;
}
// look for another 'link' element
$linkStartPos = strpos ($headText, '<link', $linkEndPos);
}
return $styleText;
}
?>