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