91896c4d960c086630720840cafe1307210cf805
[atutor.git] / docs / include / classes / ContentOutputParser.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002 - 2009                                            */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
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 // $Id$
13
14 class ContentOutputParser {
15     function ContentOutputParser(){}
16     function openHandler(& $parser,$name,$attrs) {
17                 global $my_files;
18
19                 $name = strtolower($name);
20                 $attrs = array_change_key_case($attrs, CASE_LOWER);
21
22         /*
23             the following resources are to be identified:
24             even if some of these can't be images, they can still be files in the content dir.
25             theoretically the only urls we wouldn't deal with would be for a <!DOCTYPE and <form>
26
27             img         => src
28             a           => href                         // ignore if href doesn't exist (ie. <a name>)
29             object      => data | classid       // probably only want data
30             applet      => classid | archive                    // whatever these two are should double check to see if it's a valid file (not a dir)
31             link        => href
32             script      => src
33             form        => action
34             input       => src
35             iframe      => src
36
37         */
38                 $elements = array(      'img'           => 'src',
39                                                         'a'                     => 'href',                              
40                                                         'object'        => array('data', 'classid'),
41                                                         'applet'        => array('classid', 'archive'),
42                                                         'link'          => 'href',
43                                                         'script'        => 'src',
44                                                         'form'          => 'action',
45                                                         'input'         => 'src',
46                                                         'iframe'        => 'src',
47                                                         'embed'         => 'src',
48                                                         'param'         => 'value');
49         
50                 /* check if this attribute specifies the files in different ways: (ie. java) */
51                 if (is_array($elements[$name])) {
52                         $items = $elements[$name];
53                         foreach ($items as $item) {
54                                 if ($attrs[$item] != '') {
55                                         /* some attributes allow a listing of files to include seperated by commas (ie. applet->archive). */
56                                         if (strpos($attrs[$item], ',') !== false) {
57                                                 $files = explode(',', $attrs[$item]);
58                                                 foreach ($files as $file) {
59                                                         $my_files[] = trim($file);
60                                                 }
61                                         } else {
62                                                 $file = trim($attrs[$item]);
63                                                 // filter our classid="clsid:..."
64                                                 if (!strpos($file, "clsid:")) {
65                                                         $my_files[] = $file;
66                                                 }
67                                         }
68                                 }
69                         }
70                 } else if (isset($elements[$name]) && ($attrs[$elements[$name]] != '')) {
71                         //hack, if param[name]=src or none <param> tag, extract. Skip all other <param> attributes.  
72                         if ($name!='param' || $attrs['name']=='src'){
73                                 //skip glossary.html, tweak to accomodate atutor imscp; also skip repeated entries.
74                                 //skip javascript: links, void();, #, mailto:
75                                 if (strpos($attrs[$elements[$name]], 'glossary.html')===false 
76                                     && !in_array($attrs[$elements[$name]], $my_files)
77                                     && $attrs[$elements[$name]]!='#'
78                                     && strpos($attrs[$elements[$name]], 'javascript:')===false 
79                                     && strpos($attrs[$elements[$name]], 'mailto:')===false 
80                                     && strpos($attrs[$elements[$name]], 'void(')===false 
81                                    ){
82                                         $my_files[] = $attrs[$elements[$name]];
83                                 }
84                         }
85                 }
86         }
87         
88         function closeHandler(& $parser,$name) { }
89 }
90 ?>