1 | <?php |
---|
2 | /***************************************************************************\ |
---|
3 | * Expresso - Expresso Messenger * |
---|
4 | * - Alexandre Correia / Rodrigo Souza * |
---|
5 | * ------------------------------------------------------------------------- * |
---|
6 | * This program is free software; you can redistribute it and/or modify it * |
---|
7 | * under the terms of the GNU General Public License as published by the * |
---|
8 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
9 | * option) any later version. * |
---|
10 | \***************************************************************************/ |
---|
11 | |
---|
12 | class Controller |
---|
13 | { |
---|
14 | const __CONTROLLER_SECURITY__ = 'controller-security'; |
---|
15 | const __CONTROLLER_CONTENTES__ = 'controller-contentes'; |
---|
16 | const __CONTROLLER_SECTIONS__ = 'controller-sections'; |
---|
17 | |
---|
18 | const __STRING_ACCESS__ = 'string-access'; |
---|
19 | const __STRING_DELIMITER__ = 'string-delimiter'; |
---|
20 | |
---|
21 | const __CONTROLLER_CONTENTES_ITEM__ = 'item'; |
---|
22 | const __CONTROLLER_CONTENTES_ITEM_PARAM__ = 'param'; |
---|
23 | const __CONTROLLER_CONTENTES_ITEM_SECTION__ = 'section'; |
---|
24 | |
---|
25 | const __CONTROLLER_SECTIONS_ITEM__ = 'item'; |
---|
26 | |
---|
27 | private $fatalError = false; |
---|
28 | |
---|
29 | public function __construct() |
---|
30 | { |
---|
31 | try |
---|
32 | { |
---|
33 | $controler_xml = dirname(__FILE__) . '/controller.xml'; |
---|
34 | |
---|
35 | if ( !file_exists($controler_xml) ) |
---|
36 | throw new Exception(__CLASS__ . ' [ ERROR ] :: the configuration file does not exist'); |
---|
37 | |
---|
38 | $this->dom = new DOMDocument; |
---|
39 | $this->dom->preserveWhiteSpace = FALSE; |
---|
40 | $this->dom->load($controler_xml); |
---|
41 | |
---|
42 | unset($controler_xml); |
---|
43 | |
---|
44 | $this->controller_security = $this->dom->getElementsByTagName(self::__CONTROLLER_SECURITY__); |
---|
45 | if ( $this->controller_security->length === (int)0 ) |
---|
46 | throw new Exception(__CLASS__ . ' [ ERROR #0 ] :: the tag "' . self::__CONTROLLER_SECURITY__ . '" does not exist'); |
---|
47 | if ( $this->controller_security->length !== (int)1 ) |
---|
48 | throw new Exception(__CLASS__ . ' [ ERROR #1 ] :: exists more of a tag "' . self::__CONTROLLER_SECURITY__ . '"'); |
---|
49 | |
---|
50 | $this->controller_contentes = $this->dom->getElementsByTagName(self::__CONTROLLER_CONTENTES__); |
---|
51 | if ( $this->controller_contentes->length === (int)0 ) |
---|
52 | throw new Exception(__CLASS__ . ' [ ERROR #2 ] :: the tag "' . self::__CONTROLLER_CONTENTES__ . '" does not exist'); |
---|
53 | if ( $this->controller_contentes->length !== (int)1 ) |
---|
54 | throw new Exception(__CLASS__ . ' [ ERROR #3 ] :: exists more of a tag "' . self::__CONTROLLER_CONTENTES__ . '"'); |
---|
55 | $this->controller_contentes = $this->controller_contentes->item(0); |
---|
56 | |
---|
57 | $this->controller_sections = $this->dom->getElementsByTagName("controller-sections"); |
---|
58 | if ( $this->controller_sections->length === (int)0 ) |
---|
59 | throw new Exception(__CLASS__ . ' [ ERROR #4 ] :: the tag "' . self::__CONTROLLER_SECTIONS__ . '" does not exist'); |
---|
60 | if ( $this->controller_sections->length !== (int)1 ) |
---|
61 | throw new Exception(__CLASS__ . ' [ ERROR #5 ] :: exists more of a tag "' . self::__CONTROLLER_SECTIONS__ . '"'); |
---|
62 | $this->controller_sections = $this->controller_sections->item(0); |
---|
63 | |
---|
64 | $this->string_access = $this->controller_security->item(0)->getElementsByTagName(self::__STRING_ACCESS__); |
---|
65 | if ( $this->string_access->length === (int)0 ) |
---|
66 | throw new Exception(__CLASS__ . ' [ ERROR #6 ] :: the tag "' . self::__STRING_ACCESS__ . '" does not exist'); |
---|
67 | if ( $this->string_access->length !== (int)1 ) |
---|
68 | throw new Exception(__CLASS__ . ' [ ERROR #7 ] :: exists more of a tag "' . self::__STRING_ACCESS__ . '"'); |
---|
69 | $this->string_access = $this->string_access->item(0)->nodeValue; |
---|
70 | |
---|
71 | $this->string_delimiter = $this->controller_security->item(0)->getElementsByTagName(self::__STRING_DELIMITER__); |
---|
72 | ( $this->string_delimiter->length === (int)0 ) |
---|
73 | and die(__CLASS__ . ' [ ERROR #8 ] :: the tag "' . self::__STRING_DELIMITER__ . '" does not exist'); |
---|
74 | if ( $this->string_delimiter->length !== (int)1 ) |
---|
75 | throw new Exception(__CLASS__ . ' [ ERROR #9 ] :: exists more of a tag "' . self::__STRING_DELIMITER__ . '"'); |
---|
76 | $this->string_delimiter = $this->string_delimiter->item(0)->nodeValue; |
---|
77 | } |
---|
78 | catch(Exception $e) |
---|
79 | { |
---|
80 | $this->fatalError = true; |
---|
81 | return $e->getMessage(); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | public function __call($name, $arguments) |
---|
86 | { |
---|
87 | if ( !$this->fatalError ) |
---|
88 | switch ( $name ) |
---|
89 | { |
---|
90 | case 'exec' : |
---|
91 | return $this->_exec($arguments[0]); |
---|
92 | break; |
---|
93 | default : return "Method not avaible"; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | public function __toString() |
---|
98 | { |
---|
99 | return __CLASS__; |
---|
100 | } |
---|
101 | |
---|
102 | private final function _exec(array &$pRequest) |
---|
103 | { |
---|
104 | ( $pRequest[$this->string_access] ) |
---|
105 | or die(__CLASS__ . ' [ ERROR #10 ] :: bad string action argument'); |
---|
106 | |
---|
107 | list($section_name, $ref, $alias) = explode($this->string_delimiter, $pRequest[$this->string_access]); |
---|
108 | unset($pRequest[$this->string_access]); |
---|
109 | |
---|
110 | $contents_itens = $this->controller_contentes->getElementsByTagName(self::__CONTROLLER_CONTENTES_ITEM__); |
---|
111 | |
---|
112 | for ( $i = 0; $i < $contents_itens->length && $contents_itens->item($i)->getAttribute(self::__CONTROLLER_CONTENTES_ITEM_PARAM__) != $section_name; $i++ ); |
---|
113 | ( !($i < $contents_itens->length) ) |
---|
114 | and die(__CLASS__ . ' [ ERROR #11 ] :: invalid section "' . $section_name . '"'); |
---|
115 | |
---|
116 | $section_name = $contents_itens->item($i)->getAttribute(self::__CONTROLLER_CONTENTES_ITEM_SECTION__); |
---|
117 | |
---|
118 | $section = $this->controller_sections->getElementsByTagName($section_name); |
---|
119 | ( $section->length === (int)0 ) |
---|
120 | and die(__CLASS__ . ' [ ERROR #12 ] :: the tag "' . $section_name . '" does not exist'); |
---|
121 | ( $section->length === (int)1 ) |
---|
122 | or die(__CLASS__ . ' [ ERROR #13 ] :: exists more of a tag "' . $section_name . '"'); |
---|
123 | $section = $section->item(0); |
---|
124 | |
---|
125 | $section_itens = $section->getElementsByTagName(self::__CONTROLLER_SECTIONS_ITEM__); |
---|
126 | |
---|
127 | if ( empty($alias) && $alias !== '0' ) |
---|
128 | for ( $i = 0; $i < $section_itens->length && $section_itens->item($i)->getAttribute('ref') != $ref; $i++ ); |
---|
129 | else |
---|
130 | for ( $i = 0; $i < $section_itens->length && ( $section_itens->item($i)->getAttribute('ref') != $ref || $section_itens->item($i)->getAttribute('alias') !== $alias); $i++ ); |
---|
131 | |
---|
132 | ( !($i < $section_itens->length) ) |
---|
133 | and die(__CLASS__ . ' [ ERROR #14 ] :: invalid reference "' . $ref . '"'); |
---|
134 | |
---|
135 | $path = $section_itens->item($i)->getAttribute('path') |
---|
136 | or $path = $section->getAttribute('path') |
---|
137 | or die(__CLASS__ . ' [ ERROR #15 ] :: bad path argument'); |
---|
138 | |
---|
139 | $prefix = $section_itens->item($i)->getAttribute('prefix') |
---|
140 | or $prefix = $section->getAttribute('prefix'); |
---|
141 | |
---|
142 | $suffix = $section_itens->item($i)->getAttribute('suffix') |
---|
143 | or $suffix = $section->getAttribute('suffix') |
---|
144 | or die(__CLASS__ . ' [ ERROR #16 ] :: bad suffix argument'); |
---|
145 | |
---|
146 | return $this->$section_name(array("pSectionItem" => $section_itens->item($i), "pPath" => $path, "pPrefix" => $prefix, "pSuffix" => $suffix, "pRequest" => $pRequest)); |
---|
147 | } |
---|
148 | |
---|
149 | private final function php() |
---|
150 | { |
---|
151 | $params = func_get_args(); |
---|
152 | extract($params[0]); |
---|
153 | |
---|
154 | $class = $pSectionItem->getAttribute('class') |
---|
155 | and $method = $pSectionItem->getAttribute('method') |
---|
156 | or die(__CLASS__ . ' [ ERROR #17 ] :: bad class or method argument'); |
---|
157 | |
---|
158 | $file = "{$pPath}/{$pPrefix}{$class}{$pSuffix}"; |
---|
159 | |
---|
160 | file_exists($file) |
---|
161 | or die(__CLASS__ . ' [ ERROR #18 ] :: the file that has the class was not opened'); |
---|
162 | |
---|
163 | require_once $file; |
---|
164 | |
---|
165 | $obj = new ReflectionClass($class); |
---|
166 | |
---|
167 | if ( $pRequest['classConstructor'] ) |
---|
168 | { |
---|
169 | $obj = $obj->newInstance($pRequest['classConstructor']); |
---|
170 | unset($pRequest['classConstructor']); |
---|
171 | } |
---|
172 | else |
---|
173 | $obj = $obj->newInstance(); |
---|
174 | |
---|
175 | $method = new ReflectionMethod($class, $method); |
---|
176 | $result = $method->invoke($obj, $pRequest); |
---|
177 | |
---|
178 | return $result; |
---|
179 | } |
---|
180 | |
---|
181 | private final function js() |
---|
182 | { |
---|
183 | $params = func_get_args(); |
---|
184 | extract($params[0]); |
---|
185 | |
---|
186 | $js = $pSectionItem->getAttribute('js') |
---|
187 | or die(__CLASS__ . ' [ ERROR #18 ] :: bad js argument'); |
---|
188 | |
---|
189 | $file = "{$pPath}/{$pPrefix}{$js}{$pSuffix}"; |
---|
190 | //$file = "../jabberit_messenger/{$pPath}/{$pPrefix}{$js}{$pSuffix}"; |
---|
191 | |
---|
192 | file_exists($file) |
---|
193 | or die(__CLASS__ . ' [ ERROR #19 ] :: the file that has the class was not opened'); |
---|
194 | |
---|
195 | $debug = $pSectionItem->parentNode->getAttribute('debug'); |
---|
196 | |
---|
197 | return file_get_contents($file); |
---|
198 | |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | ?> |
---|