1 | <?php |
---|
2 | require_once(PHPGW_SERVER_ROOT.SEP.'workflow'.SEP.'inc'.SEP.'natural'.SEP.'class.natural_resultset.php'); |
---|
3 | require_once(PHPGW_SERVER_ROOT.SEP.'workflow'.SEP.'inc'.SEP.'natural'.SEP.'pos_string.php'); |
---|
4 | require_once(PHPGW_SERVER_ROOT.SEP.'workflow'.SEP.'inc'.SEP.'natural'.SEP.'nat_types.php'); |
---|
5 | |
---|
6 | /** |
---|
7 | * Código ascii do caracter espaço |
---|
8 | * @name SPACE |
---|
9 | */ |
---|
10 | define (SPACE, ' '); |
---|
11 | |
---|
12 | /** |
---|
13 | * Reader mainframe data class (PHP NatAPI) |
---|
14 | * |
---|
15 | * @author Everton Flávio Rufino Seára - rufino@celepar.pr.gov.br |
---|
16 | * @version 1.0 |
---|
17 | * @package Workflow |
---|
18 | * @subpackage natural |
---|
19 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
20 | */ |
---|
21 | class Natural |
---|
22 | { |
---|
23 | /** |
---|
24 | * @var string $protocol Protocolo a ser utilizado |
---|
25 | * @access private |
---|
26 | */ |
---|
27 | private $protocol; |
---|
28 | /** |
---|
29 | * @var string $endOfProtocol string delimitadora do protocolo |
---|
30 | * @access private |
---|
31 | */ |
---|
32 | private $endOfProtocol; |
---|
33 | /** |
---|
34 | * @var string $strSubProgram Sub-programa |
---|
35 | * @access private |
---|
36 | */ |
---|
37 | private $strSubProgram; |
---|
38 | /** |
---|
39 | * @var string $strInputParameter Parametro de entrada |
---|
40 | * @access private |
---|
41 | */ |
---|
42 | private $strInputParameter; |
---|
43 | /** |
---|
44 | * @var string $acronym acronym of sub-program |
---|
45 | * @access private |
---|
46 | */ |
---|
47 | private $acronym; |
---|
48 | /** |
---|
49 | * @var boolean $isDBGateway Flag utilizado para determinar se será utilizado o protocolo DBGateway ou DBCON |
---|
50 | * @access private |
---|
51 | */ |
---|
52 | private $isDBGateway; |
---|
53 | /** |
---|
54 | * @var int $numRows Número de linhas |
---|
55 | * @access private |
---|
56 | */ |
---|
57 | private $numRows; |
---|
58 | /** |
---|
59 | * @var string $IPAddress Endereço IP do Mainframe e.g:"10.15.60.20" |
---|
60 | * @access private |
---|
61 | */ |
---|
62 | private $IPAddress; |
---|
63 | /** |
---|
64 | * @var int $serverPort Porta do servidor |
---|
65 | * @access private |
---|
66 | */ |
---|
67 | private $serverPort; |
---|
68 | /** |
---|
69 | * @var string $key USU (8 bytes) - user => N000 + 3 positions for key + 1 space byte |
---|
70 | * @access private |
---|
71 | */ |
---|
72 | private $key; |
---|
73 | /** |
---|
74 | * @var string $password Senha de acesso ao mainframe (8 bytes) |
---|
75 | * @access private |
---|
76 | */ |
---|
77 | private $password; |
---|
78 | /** |
---|
79 | * @var string $application APLIC (4 bytes) application => 3 bytes for system acronym AND 1 byte to environment 'P' or 'D' |
---|
80 | * @access private |
---|
81 | */ |
---|
82 | private $application; |
---|
83 | /** |
---|
84 | * @var string $system Sistema (4 bytes) |
---|
85 | * @access private |
---|
86 | */ |
---|
87 | private $system; |
---|
88 | /** |
---|
89 | * @var string $logon LOGON (8 bytes) |
---|
90 | * @access private |
---|
91 | */ |
---|
92 | private $logon; |
---|
93 | /** |
---|
94 | * @var string $rc PA_RC - Código de Retorno (4 bytes) |
---|
95 | * @access private |
---|
96 | */ |
---|
97 | private $rc; |
---|
98 | /** |
---|
99 | * @var string $msg PA_MSG - Mensagem de retorno (60 bytes) |
---|
100 | * @access private |
---|
101 | */ |
---|
102 | private $msg; |
---|
103 | /** |
---|
104 | * @var string $dataParameter Return of MAINFRAME environment |
---|
105 | * @access private |
---|
106 | */ |
---|
107 | private $dataParameter; |
---|
108 | /** |
---|
109 | * @var object $resultSet Armazena o resultado das consulta |
---|
110 | * @access public |
---|
111 | */ |
---|
112 | public $resultSet; |
---|
113 | |
---|
114 | |
---|
115 | /** |
---|
116 | * @var object $obj Objeto de Classe que contém a especificação e estrutura do programa natural acessado |
---|
117 | * @access protected |
---|
118 | */ |
---|
119 | protected $obj; |
---|
120 | |
---|
121 | /** |
---|
122 | * Construtor da clase Natural |
---|
123 | * @access public |
---|
124 | */ |
---|
125 | public function Natural() |
---|
126 | { |
---|
127 | $this->protocol = "/cics/cwba/jaspin?"; |
---|
128 | $this->endOfProtocol = "!jaspin_fim_de_dados!"; |
---|
129 | $this->isDBGateway = false; |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * Carrega atributos da classe com valores iniciais |
---|
134 | * @param String - Name of sub-program |
---|
135 | * @param String - Input parameters to sub-program |
---|
136 | * @access protected |
---|
137 | * @return void |
---|
138 | */ |
---|
139 | protected function initialize($strSubProgram/*, $strInputParameter*/) |
---|
140 | { |
---|
141 | $this->strSubProgram = strtoupper($strSubProgram); |
---|
142 | // $this->strInputParameter = strtoupper($strInputParameter); |
---|
143 | |
---|
144 | // get the acronym of the sub-program |
---|
145 | $this->acronym = strtoupper(substr($strSubProgram, 0, 3)); |
---|
146 | |
---|
147 | // set the system |
---|
148 | $this->system = str_repeat("X", 4); |
---|
149 | |
---|
150 | // set the static logon |
---|
151 | $this->logon = "N000" . $this->acronym . SPACE; |
---|
152 | |
---|
153 | // initialize the return code |
---|
154 | $this->rc = "9999"; |
---|
155 | |
---|
156 | // initialize the return message |
---|
157 | $this->msg = str_repeat(SPACE, 60); |
---|
158 | |
---|
159 | // initialize the data parameter |
---|
160 | $this->dataParameter = "0000000000"; |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * Determina o protocolo a ser usado pelo Gateway |
---|
165 | * |
---|
166 | * In DBGateway protocol the size of returned data does not have any bound. |
---|
167 | * if you does not set DBGataway, PHP NatAPI will use DBCON. Using DBCon you can transfer only 32Kb of data. |
---|
168 | * @param String $dbGateway set BDGateway protocol |
---|
169 | * @access public |
---|
170 | * @return void |
---|
171 | */ |
---|
172 | public function setDBGatewayProtocol($dbGateway = true) |
---|
173 | { |
---|
174 | $this->isDBGateway = $dbGateway; |
---|
175 | if ($dbGateway){ |
---|
176 | $this->protocol = "/cics/cwba/jasppion?"; |
---|
177 | $this->endOfProtocol = "!jasppion_fim_de_dados!"; |
---|
178 | } else { |
---|
179 | $this->protocol = "/cics/cwba/jaspin?"; |
---|
180 | $this->endOfProtocol = "!jaspin_fim_de_dados!"; |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * Access and retrieve data from mainframe |
---|
186 | * |
---|
187 | * @return boolean |
---|
188 | * @access protected |
---|
189 | */ |
---|
190 | protected function execute($inputParams = "") |
---|
191 | { |
---|
192 | |
---|
193 | if (!empty($this->application) && !empty($this->strSubProgram) && !empty($this->key) && !empty($this->password) && |
---|
194 | !empty($this->IPAddress) && !empty($this->serverPort)) |
---|
195 | { |
---|
196 | try |
---|
197 | { |
---|
198 | $str = Factory::newInstance('PosString', Factory::newInstance('NatType')); |
---|
199 | $this->strInputParameter = $str->mountString(array_merge($this->obj->input, $this->obj->output), $inputParams); |
---|
200 | |
---|
201 | $url = $this->protocol . ($this->acronym . $this->application) . $this->system . $this->logon . $this->strSubProgram . $this->key . $this->password . $this->rc . $this->msg . $this->strInputParameter . $this->endOfProtocol; |
---|
202 | |
---|
203 | $link = "http://" . $this->IPAddress . ":". $this->serverPort . $url; |
---|
204 | // echo $link . "<br><br>"; |
---|
205 | |
---|
206 | $fp = @fsockopen($this->IPAddress, $this->serverPort, $errnum, $errstr); |
---|
207 | $line = ""; |
---|
208 | $size = 0; |
---|
209 | |
---|
210 | if ($fp) { |
---|
211 | @fputs($fp, "GET " . $url . " HTTP/1.1\r\nHost: " . $this->IPAddress . "\r\nConnection: close\r\n\r\n"); |
---|
212 | $line = ""; |
---|
213 | while (!feof($fp)) { |
---|
214 | $temp = fread($fp, 128); |
---|
215 | $line = $line . $temp; |
---|
216 | } |
---|
217 | @fclose($fp); |
---|
218 | |
---|
219 | $pos1 = @strpos($line, "<html><body>"); |
---|
220 | if ($pos1) { |
---|
221 | $pos1 = $pos1 +12; |
---|
222 | $pos2 = @strpos($line, "</body></html>"); |
---|
223 | if ($pos2) { |
---|
224 | $size = $pos2 - $pos1; |
---|
225 | $line = substr($line, $pos1, $size); |
---|
226 | $this->rc = substr($line, 40, 4); |
---|
227 | $this->msg = substr($line, 44, 60); |
---|
228 | |
---|
229 | // Verify if user chose DBGateway or DBCon format. |
---|
230 | // In DBGateway, the record count is also returned |
---|
231 | if ($this->isDBGateway){ |
---|
232 | $this->numRows = (int)substr($line, 104, 5); |
---|
233 | $this->dataParameter = substr($line, 109, ($size -109)); |
---|
234 | } else { |
---|
235 | $this->dataParameter = substr($line, 104, ($size -104)); |
---|
236 | } |
---|
237 | } else { |
---|
238 | $this->msg = $line; |
---|
239 | } |
---|
240 | } else { |
---|
241 | $this->msg = $line; |
---|
242 | } |
---|
243 | } else { |
---|
244 | $this->rc = $this->rc . $errnum . "-" . $errstr; |
---|
245 | $this->msg = $errnum . "-" . $errstr; |
---|
246 | return false; |
---|
247 | } |
---|
248 | return $str->mountResult(array_merge($this->obj->input, $this->obj->output), $this->dataParameter); |
---|
249 | // return true; |
---|
250 | } catch (Exception $e) { |
---|
251 | return false; |
---|
252 | } |
---|
253 | } else { |
---|
254 | return false; |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | /** |
---|
259 | * Returns the Return Code of the natural sub-program |
---|
260 | * @return int Return Code |
---|
261 | * @access public |
---|
262 | */ |
---|
263 | public function getRC() |
---|
264 | { |
---|
265 | return $this->rc; |
---|
266 | } |
---|
267 | |
---|
268 | /** |
---|
269 | * Returns the return message of the natural sub-program |
---|
270 | * @return string $msg message from mainframe |
---|
271 | * @access public |
---|
272 | */ |
---|
273 | public function getMSG() |
---|
274 | { |
---|
275 | return $this->msg; |
---|
276 | } |
---|
277 | |
---|
278 | /** |
---|
279 | * Retorna os dados recuperados do mainframe |
---|
280 | * @return string $dataParameter data recovered |
---|
281 | * @access public |
---|
282 | */ |
---|
283 | public function getDataParameter() |
---|
284 | { |
---|
285 | return $this->dataParameter; |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | * Sets the key (N000 + $key + space) |
---|
290 | * @param int $key Key |
---|
291 | * @access protected |
---|
292 | */ |
---|
293 | protected function setKey($key) |
---|
294 | { |
---|
295 | if (strlen($key) < 7) |
---|
296 | $this->key = "N000" . $key . SPACE; |
---|
297 | else |
---|
298 | $this->key = $key . SPACE; |
---|
299 | } |
---|
300 | |
---|
301 | /** |
---|
302 | * Sets the logon |
---|
303 | * @param int $logon Logon |
---|
304 | * @access protected |
---|
305 | */ |
---|
306 | protected function setLogon($logon) |
---|
307 | { |
---|
308 | $this->logon = $logon; |
---|
309 | } |
---|
310 | |
---|
311 | /** |
---|
312 | * Sets the password |
---|
313 | * @param string $password Senha de acesso |
---|
314 | * @access protected |
---|
315 | * @return void |
---|
316 | */ |
---|
317 | protected function setPassword($password) |
---|
318 | { |
---|
319 | $this->password = $password . str_repeat(SPACE, (8 - strlen($password))); |
---|
320 | } |
---|
321 | |
---|
322 | /** |
---|
323 | * Sets the application environment |
---|
324 | * @param char $app Defines if the app. is in the production or development environment 'D' (development) or 'P' (production) |
---|
325 | * @access protected |
---|
326 | * @return void |
---|
327 | */ |
---|
328 | protected function setApplication($app) |
---|
329 | { |
---|
330 | $this->application = $app; |
---|
331 | } |
---|
332 | |
---|
333 | /** |
---|
334 | * Sets the IP address to access the mainframe |
---|
335 | * @param string $ip Endereço Ip do Mainframe e.g 10.15.60.20 |
---|
336 | * @access protected |
---|
337 | * @return void |
---|
338 | */ |
---|
339 | protected function setIPAddress($ip) |
---|
340 | { |
---|
341 | $this->IPAddress = $ip; |
---|
342 | } |
---|
343 | |
---|
344 | /** |
---|
345 | * Seta a porta do servidor para acessar o Mainframe |
---|
346 | * @param int $portNumber Porta do Servidor e.g 103 |
---|
347 | * @access protected |
---|
348 | * @return void |
---|
349 | */ |
---|
350 | protected function setServerPort($portNumber) |
---|
351 | { |
---|
352 | $this->serverPort = $portNumber; |
---|
353 | } |
---|
354 | |
---|
355 | /** |
---|
356 | * Seta o sistema oara acesso |
---|
357 | * @param int $system 4 dígitos em caixa alta |
---|
358 | * @access protected |
---|
359 | * @return void |
---|
360 | */ |
---|
361 | protected function setSystem($system) |
---|
362 | { |
---|
363 | $this->system = $system; |
---|
364 | } |
---|
365 | |
---|
366 | /** |
---|
367 | * Seta o código de retorno |
---|
368 | * @param int $rc código de retorno com 4 dígitos |
---|
369 | * @access protected |
---|
370 | * @return void |
---|
371 | */ |
---|
372 | protected function setRC($rc) |
---|
373 | { |
---|
374 | $this->rc = $rc; |
---|
375 | } |
---|
376 | |
---|
377 | |
---|
378 | /** |
---|
379 | * Build a result set ($this->resultSet) to mainframe data. The result is returnd in array format |
---|
380 | * |
---|
381 | * <code> |
---|
382 | * $rowConf example - array (name_of_field => size_of_field) |
---|
383 | * $array = array("id" => 5, "name" => 10); |
---|
384 | * </code> |
---|
385 | * @param int $lineSize Size of each line return from mainframe |
---|
386 | * @param array $rowConf Configuration of the field's name and size of each fild |
---|
387 | * @return void |
---|
388 | * @access public |
---|
389 | */ |
---|
390 | public function configureResultSet($lineSize, $rowConf) |
---|
391 | { |
---|
392 | //$rows = explode("%NAT_SEP%", wordwrap($this->dataParameter, $lineSize, "%NAT_SEP%")); |
---|
393 | for ($i=0; $i < strlen($this->dataParameter)/$lineSize; ++$i){ |
---|
394 | $rows[] = substr($this->dataParameter, $lineSize*$i , $lineSize); |
---|
395 | } |
---|
396 | |
---|
397 | $result = array(); |
---|
398 | // for each row returned from mainframe |
---|
399 | foreach ($rows as $id => $values){ |
---|
400 | $last_id = 0; |
---|
401 | // get configuration array to build the result |
---|
402 | foreach ($rowConf as $id_conf => $value_conf) |
---|
403 | { |
---|
404 | $result[$id][$id_conf] = substr($values, $last_id, $value_conf); |
---|
405 | $last_id += (int)$value_conf; |
---|
406 | } |
---|
407 | } |
---|
408 | |
---|
409 | if (!empty($this->resultSet)) |
---|
410 | unset($this->resultSet); |
---|
411 | $this->resultSet = &Factory::newInstance('NaturalResultSet', $result); |
---|
412 | } |
---|
413 | } |
---|
414 | ?> |
---|