-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.php
More file actions
168 lines (133 loc) · 5.35 KB
/
services.php
File metadata and controls
168 lines (133 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
* Copyright (c) 2025 Bloxtor (http://bloxtor.com) and Joao Pinto (http://jplpinto.com)
*
* Multi-licensed: BSD 3-Clause | Apache 2.0 | GNU LGPL v3 | HLNC License (http://bloxtor.com/LICENSE_HLNC.md)
* Choose one license that best fits your needs.
*
* Original PHP Spring Lib Repo: https://github.com/a19836/php-spring-lib/
* Original Bloxtor Repo: https://github.com/a19836/bloxtor
*
* YOU ARE NOT AUTHORIZED TO MODIFY OR REMOVE ANY PART OF THIS NOTICE!
*/
include_once __DIR__ . "/../config.php";
include_once __DIR__ . "/config.php";
include get_lib("sqlmap.ibatis.IBatisClient");
include dirname(__DIR__) . "/MySQLDBBroker.php";
echo $style;
echo '<h1>PHP Spring Lib - iBatis with services</h1>
<p>Extend iBatis to allow services.</p>
<div class="note">
<span>
Learn how to use extend the Spring iBatis library, by creating your own xml nodes.<br/>
In this example we create the \'service\' nodes to work as alias to existing queries.
</span>
</div>';
if (!$password) {
echo '<div class="error">Please edit config.php file and define your DB credentials first!</div>';
die();
}
echo '
<h3>Some Extended Methods:</h3>
<div class="code short input">
<textarea readonly>
$services = parseServicesFile(__DIR__ . "/assets/services.xml");
$query_id = $services[$service_alias];
$query = getQuery($SQLClient, $query_id);
</textarea>
</div>
<h3>Examples:</h3>';
//Init IBatisClient
$SQLClient = new IBatisClient();
//$SQLClient->setCacheRootPath( sys_get_temp_dir() . "/cache/spring/ibatis/" );
$DBBroker = new MySQLDBBroker($host, $username, $password, $db_name);
$SQLClient->setRDBBroker($DBBroker);
//Prepare table if not exists
createTable($DBBroker);
//Load iBatis queries
$SQLClient->loadXML( __DIR__ . "/assets/item.xml" );
$code = '$DBBroker = new MySQLDBBroker($host, $username, $password, $db_name);
$SQLClient = new IBatisClient();
$SQLClient->setRDBBroker($DBBroker);
$SQLClient->loadXML( __DIR__ . "/assets/item.xml" );';
echo '<div>
<h4>Start iBatis Client and load xml file:</h4>
<div class="code short input">
<textarea readonly>' . $code . '</textarea>
</div>
<div class="code xml">
<textarea readonly>' . file_get_contents(__DIR__ . "/assets/item.xml") . '</textarea>
</div>
</div>';
//Extend beans factory
$services = parseServicesFile(__DIR__ . "/assets/services.xml");
$code = '$services = parseServicesFile(__DIR__ . "/assets/services.xml");';
echo '<div>
<h4>Extend iBatis with services:</h4>
<div class="code one-line input">
<textarea readonly>' . $code . '</textarea>
</div>
<div class="code short xml">
<textarea readonly>' . file_get_contents(__DIR__ . "/assets/services.xml") . '</textarea>
</div>
</div>
<h4>Play with query \'get_my_item\':</h4>
<div style="margin-left:20px;">';
//Call and execute iBatis queries
$query = getQuery($SQLClient, $services["get_my_item"]);
if ($query) {
$code = '$query = getQuery($SQLClient, $services["get_my_item"]);';
printTest("Get query", $code, print_r($query, true), "one-line");
$result = $SQLClient->execQuery($query, array("item_id" => 1));
$ItemTest = @$result[0];
$code = '$result = $SQLClient->execQuery($query, array("item_id" => 1));
$ItemTest = @$result[0];
$id = $ItemTest->getId();
$title = $ItemTest->getTitle();
$status = $ItemTest->getStatus();';
$output = "Result:\n" . print_r($result, true) . "\nItemTest:\n- id: " . $ItemTest->getId() . "\n- Title: " . $ItemTest->getTitle() . "\n- Status: " . $ItemTest->getStatus();
printTest("Execute query", $code, $output, "short", "");
}
echo '</div>
<h4>Play with query \'get_my_item_simple\':</h4>
<div style="margin-left:20px;">';
$query = getQuery($SQLClient, $services["get_my_item_simple"]);
if ($query) {
$code = '$query = getQuery($SQLClient, $services["get_my_item_simple"]);';
printTest("Get query", $code, print_r($query, true), "one-line");
$sql = $SQLClient->getQuerySQL($query, array("item_id" => 1));
$code = '$sql = $SQLClient->getQuerySQL($query, array("item_id" => 1));';
printTest("Get sql from query", $code, $sql, "one-line", "one-line");
$result = $SQLClient->getSQL($sql);
$code = '$result = $SQLClient->getSQL($sql);';
printTest("Execute sql", $code, "Result:\n" . print_r($result, true), "one-line");
}
echo '</div>';
/* EXTENDING SERVICES FUNCTIONS */
function getQuery($SQLClient, $query_id) {
$nodes = $SQLClient->getNodesData();
if ($nodes)
foreach ($nodes as $query_type => $queries)
if (isset($queries[$query_id]))
return $queries[$query_id];
return null;
}
function parseServicesFile($services_file_path) {
//get services
$content = file_get_contents($services_file_path);
$nodes = XMLFileParser::parseXMLContentToArray($content);
$first_node_name = is_array($nodes) ? array_keys($nodes) : array();
$first_node_name = isset($first_node_name[0]) ? $first_node_name[0] : null;
$services_node = isset($nodes[$first_node_name][0]["childs"]["services"][0]["childs"]["service"]) && is_array($nodes[$first_node_name][0]["childs"]["services"][0]["childs"]["service"]) ? $nodes[$first_node_name][0]["childs"]["services"][0]["childs"]["service"] : array();
$services = array();
$t = $services_node ? count($services_node) : 0;
for($i = 0; $i < $t; $i++) {
$service_node = $services_node[$i];
$alias = XMLFileParser::getAttribute($service_node, "alias");
$query = XMLFileParser::getAttribute($service_node, "query");
$services[$alias] = $query;
}
//return services
return $services;
}
?>