Merge pull request #8 from radiocontrolled/0004872
[atutor.git] / headstuff.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                                                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12 define('AT_INCLUDE_PATH', 'include/');
13 require(AT_INCLUDE_PATH . 'vitals.inc.php');
14
15 // hack for eXe content, remove now
16 header ('content-type: text/css');
17 echo('@import url(exestyles.css);');
18 exit;
19
20
21 if (isset ($_GET['cid'])){
22         $cid = intval ($_GET['cid']);
23
24         $sql = "SELECT * FROM ".TABLE_PREFIX."head WHERE course_id=$_SESSION[course_id] AND content_id=$cid";
25         $result = mysql_query($sql, $db);
26
27         if (($result != 0) && ($row = mysql_fetch_assoc($result))){
28                 // get the CSS used
29                 $styleText = get_styles ($row['text'], $_GET['path']);
30
31                 header ('content-type: text/css');
32                 echo($styleText);
33         }
34 }
35
36 // Takes the contents of the 'head' section and returns the 'style' and 'link' elements.
37 function get_styles ($headText, $path) {
38         $styleText = '';
39
40         // get the contents of all the 'style' elements
41         $styleStartPos = strpos ($headText, '<style');
42         while ($styleStartPos !== false) {
43
44                 // remove the start and end 'style' elements
45                 // (we're using just the contents of the 'style' element)
46                 $styleStartPos = strPos ($headText, '>', $styleStartPos) + 1;
47                 $styleEndPos = strPos ($headText, '</style', $styleStartPos) -1;
48
49                 // contents of 'style' element
50                 $tempString = substr ($headText, $styleStartPos, ($styleEndPos - $styleStartPos) + 1);
51
52                 // add full path to any 'import' statements
53                 $importStartPos = strpos ($tempString, '@import');
54                 if ($importStartPos !== false){
55                         $bracketStartPos = strpos ($tempString, '(', $importStartPos);
56                         if ($bracketStartPos !== false){
57                                 $bracketEndPos = strpos ($tempString, ')', $bracketStartPos);
58                                 if ($bracketEndPos !== false){
59                                         $uriCss = trim (substr ($tempString, $bracketStartPos + 1, ($bracketEndPos - $bracketStartPos)));
60
61                                         $tempString2 = substr ($tempString, '0', $bracketStartPos);
62                                         $tempString = $tempString2.'('.$path.$uriCss.substr ($tempString, $bracketEndPos + '1');
63                                 }
64                         }
65                 }
66
67                 $styleText = $styleText."\n".$tempString;
68
69                 // look for another 'style' element
70                 $styleStartPos = strpos ($headText, '<style', $styleEndPos);
71         }
72
73         // get all the links to external stylesheets
74         $linkStartPos = strpos ($headText, '<link');
75         while ($linkStartPos !== false) {
76                 $linkEndPos = strPos ($headText, '>', $linkStartPos);
77
78                 // ensure this is a 'stylesheet' link
79                 $stylesheetPos = strpos ($headText, "\"text/css\"", $linkStartPos);
80                 if (($stylesheetPos !== false) && ($stylesheetPos < $linkEndPos)){
81
82                         // get the 'href' attribute value
83                         $hrefPos = strpos ($headText, 'href', $linkStartPos);
84                         if ($hrefPos !== false){
85                                 $hrefPos = strpos ($headText, '=', $hrefPos);
86                                 if ($hrefPos !== false){
87                                         $hrefPos += '1';
88
89                                         // get first character in attribute value
90                                         $index = $hrefPos;
91
92                                         // find first attribute character
93                                         while (($headText[$index] == " ") || ($headText[$index] == '\'') || ($headText[$index] == "\"")){
94                                                 $index++;
95                                                 if ($index > strlen($headText)){
96                                                         break;
97                                                 }
98                                         }
99                                         $indexStart = $index;
100
101                                         // find end of attribute character
102                                         $indexEnd = $indexStart;
103                                         while (($headText[$indexEnd] != " ") && ($headText[$indexEnd] != '\'') && ($headText[$indexEnd] != "\"")){
104                                                 $indexEnd++;
105                                                 if ($index > strlen($headText)){
106                                                         break;
107                                                 }
108                                         }
109                                 }
110                         }
111
112                         // convert the href attribute value to an "import url" statement
113                         $importStatement = '@import url('.$path.substr ($headText, $indexStart, ($indexEnd - $indexStart)).');';
114                         $styleText = $styleText."\n".$importStatement;
115                 }
116
117                 // look for another 'link' element
118                 $linkStartPos = strpos ($headText, '<link', $linkEndPos);
119         }
120         return $styleText;
121 }
122 ?>