ATutor 2.0
[atutor.git] / mods / _core / imsafa / html / resources_parser.inc.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2010                                      */
6 /* Inclusive Design Institute                                   */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id: resources_parser_inc.inc.php 7208 2008-07-04 16:07:24Z silvia $
14
15 if (!defined('AT_INCLUDE_PATH')) { exit; }
16 //require(AT_INCLUDE_PATH.'lib/output.inc.php');
17
18 global $db;
19
20 define('AT_INCLUDE_PATH', '../include/');
21
22 //echo 'uffa';
23 //echo $_POST['body_text'];
24
25 $body_text      = htmlspecialchars($stripslashes($_POST['body_text']));
26 $body_t         = html_entity_decode($body_text);
27                 
28
29 require(AT_INCLUDE_PATH.'classes/XML/XML_HTMLSax/XML_HTMLSax.php');     /* for XML_HTMLSax */
30 require(AT_INCLUDE_PATH.'../mods/_core/imscp/include/ims_template.inc.php');                            /* for ims templates + print_organizations() */
31
32 /*
33 the following resources are to be identified:
34 even if some of these can't be images, they can still be files in the content dir.
35 theoretically the only urls we wouldn't deal with would be for a <!DOCTYPE and <form>
36
37 img             => src
38 a               => href                         // ignore if href doesn't exist (ie. <a name>)
39 object  => data | classid       // probably only want data
40 applet  => classid | archive                    // whatever these two are should double check to see if it's a valid file (not a dir)
41 script  => src
42 input   => src
43 iframe  => src
44 */
45         
46 class MyHandler {
47         function MyHandler(){}
48         function openHandler(& $parser,$name,$attrs) {
49                 global $my_files;
50
51                 $name = strtolower($name);
52                 $attrs = array_change_key_case($attrs, CASE_LOWER);
53
54                 $elements = array(      'img'           => 'src',
55                                                         'a'                     => 'href',                              
56                                                         'object'        => array('data', 'classid'),
57                                                         'applet'        => array('classid', 'archive'),
58                                                         'script'        => 'src',
59                                                         'input'         => 'src',
60                                                         'iframe'        => 'src',
61                                                         'embed'         => 'src',
62                                                         );
63
64                 /* check if this attribute specifies the files in different ways: (ie. java) */
65                 if (is_array($elements[$name])) {
66                         $items = $elements[$name];
67         
68                         foreach ($items as $item) {
69                                 if ($attrs[$item] != '') {
70
71                                         /* some attributes allow a listing of files to include seperated by commas (ie. applet->archive). */
72                                         if (strpos($attrs[$item], ',') !== false) {
73                                                 $files = explode(',', $attrs[$item]);
74                                                 foreach ($files as $file) {
75                                                         $my_files[] = trim($file);
76                                                 }
77                                         } else {
78                                                 $my_files[] = $attrs[$item];
79                                         }
80                                 }
81                         }       
82                 } else if (isset($elements[$name]) && ($attrs[$elements[$name]] != '')) {
83                         /* we know exactly which attribute contains the reference to the file. */
84                         $my_files[] = $attrs[$elements[$name]];
85                 }
86         }
87         function closeHandler(& $parser,$name) { }
88         }
89
90 /* get all the content */
91 $handler=new MyHandler();
92 $parser = new XML_HTMLSax();
93 $parser->set_object($handler);
94 $parser->set_element_handler('openHandler','closeHandler');
95
96 /* generate the resources and save the HTML files */
97                         
98 ob_start();
99                                                          
100 global $parser, $my_files;
101 global $course_id;
102
103 /* add the resource dependancies */
104 $my_files               = array();
105 $content_files  = "\n";
106
107 //in order to control if some [media] is in the body_text
108 $body = embed_media($body_t);
109
110 $parser->parse($body);
111                 
112 // add by Cindy Li. 
113 // This resolves the problem introduced by [media] tag: when [media] is 
114 // parsed into <object>, same resource appears a few times in <object> with different 
115 // format to cater for different browsers or players. This way creates prolem that different
116 // formats in <object> are all parsed and considered as different resource. array_unique()
117 // call solves this problem. But, it introduces the new problem that when a same resource
118 // appears at different places in the content and users do want to have them with different
119 // alternatives. With this solution, this same resource only shows up once at "adapt content"
120 // and only can have one alternative associate with. Table and scripts need to re-design
121 // to solve this problem, for example, include line number in table. 
122 $my_files = array_unique($my_files);
123
124 /* handle @import */
125 $import_files   = get_import_files($body);
126                         
127 if (count($import_files) > 0) $my_files = array_merge($my_files, $import_files);
128
129 $i=0;
130
131 foreach ($my_files as $file) {
132         /* filter out full urls */
133         $url_parts = @parse_url($file);
134 //      if (isset($url_parts['scheme']) && substr($file, 0, strlen(AT_BASE_HREF)) != AT_BASE_HREF) {
135 //              continue;
136 //      }
137
138         /* file should be relative to content. let's double check */
139         if ((substr($file, 0, 1) == '/')) {
140                 continue;
141         }
142         
143         $resources[$i] = $file;
144         $i++;
145 }
146                 
147 $organizations_str = ob_get_contents();
148 ob_end_clean();