(no commit message)
[acontent.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: ContentOutputParser.class.php 10278 2010-10-04 16:47:45Z hwong $
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
56                                         /* some attributes allow a listing of files to include seperated by commas (ie. applet->archive). */
57                                         if (strpos($attrs[$item], ',') !== false) {
58                                                 $files = explode(',', $attrs[$item]);
59                                                 foreach ($files as $file) {
60                                                         $my_files[] = trim($file);
61                                                 }
62                                         } else {
63                                                 $my_files[] = $attrs[$item];
64                                         }
65                                 }
66                         }
67                 } else if (isset($elements[$name]) && ($attrs[$elements[$name]] != '')) {
68                         //hack, if param[name]=src or none <param> tag, extract. Skip all other <param> attributes.  
69                         if ($name!='param' || $attrs['name']=='src'){
70                                 //skip glossary.html, tweak to accomodate atutor imscp; also skip repeated entries.
71                                 //skip javascript: links, void();, #, mailto:
72                                 if (strpos($attrs[$elements[$name]], 'glossary.html')===false 
73                                     && !in_array($attrs[$elements[$name]], $my_files)
74                                     && $attrs[$elements[$name]]!='#'
75                                     && strpos($attrs[$elements[$name]], 'javascript:')===false 
76                                     && strpos($attrs[$elements[$name]], 'mailto:')===false 
77                                     && strpos($attrs[$elements[$name]], 'void(')===false 
78                                    ){
79                                         $my_files[] = $attrs[$elements[$name]];
80                                 }
81                         }
82                 }
83         }
84         
85         function closeHandler(& $parser,$name) { }
86 }
87 ?>