17 require_once SQ_INCLUDE_PATH.
'/general_occasional.inc';
18 require_once SQ_CORE_PACKAGE_PATH.
'/system/triggers/trigger_action/trigger_action.inc';
19 require_once SQ_SYSTEM_ROOT.
'/core/attributes/email_format/email_format.inc';
20 require_once SQ_SYSTEM_ROOT.
'/core/attributes/parameter_map/parameter_map.inc';
46 public static function execute($settings, &$state)
49 'details' => translate(
'database_details'),
50 'dsn' => translate(
'dsn'),
51 'connector' => translate(
'db_connector'),
53 $method = array_get_index($settings,
'method',
'');
54 $type = array_get_index($settings,
'type',
'pgsql');
55 $database = array_get_index($settings,
'database',
'');
56 $host = array_get_index($settings,
'host',
'');
57 $dsn = array_get_index($settings,
'dsn',
'');
58 $connector = array_get_index($settings,
'connector',
'0');
59 $username = array_get_index($settings,
'username',
'');
60 $password = array_get_index($settings,
'password',
'');
61 $sql = array_get_index($settings,
'sql',
'');
62 $prefix = array_get_index($settings,
'prefix', date(
'dmY'));
63 if (!array_key_exists($method, $options)) {
68 if (empty($state[
'asset'])) {
70 if (empty($state[
'assetid'])) {
71 $state[
'asset'] = NULL;
73 $state[
'asset'] = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($state[
'assetid']);
81 if ($method ==
'connector' && !empty($connector)) {
82 $connector = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($connector);
83 if (!empty($connector)) {
84 $actual_dsn = $connector->getDSN();
89 if ($method ==
'dsn' && !empty($dsn)) {
94 if ($method ==
'details' && !empty($type) && !empty($host) && !empty($database)) {
97 $actual_dsn =
'pgsql:';
100 $actual_dsn =
'mysql:';
103 $actual_dsn .=
"dbname=$database;host=$host";
107 if (empty($actual_dsn))
return FALSE;
109 if ($method ==
'connector') {
111 $connect_dsn = $actual_dsn;
113 $connect_dsn = Array(
'DSN' => $actual_dsn,
'user' => $username,
'password' => $password,
'type' => $type);
119 }
catch (Exception $e) {
124 if ($connection === FALSE)
return FALSE;
125 $sql = trim($sql,
';');
129 $keyword_wrapper =
'%';
130 $keyword_pattern =
'('.$keyword_wrapper.
'([a-zA-Z_\-0-9\.]+)'.$keyword_wrapper.
')';
131 preg_match_all (
'/'.$keyword_pattern.
'/', $sql, $matches, PREG_PATTERN_ORDER);
133 if (!isset($matches[2])) $matches[2] = Array();
135 $keywords =& $matches[2];
138 $replacements = Array();
139 $bind_vars = Array();
140 foreach ($keywords as $id => $keyword) {
142 if (!is_null($state[
'asset'])) {
143 $value = $state[
'asset']->getKeywordReplacement($keyword);
145 if ($value != $keyword_wrapper.$keyword.$keyword_wrapper) {
147 $value = str_replace(
'$',
'\$', $value);
148 $replacements[] =
':'.$keyword.$id;
149 $patterns[] =
'/('.$keyword_wrapper.$keyword.$keyword_wrapper.
')/';
150 $bind_vars[$keyword.$id] = $value;
155 $includes_globals = FALSE;
156 foreach(array_unique($keywords) as $keyword) {
157 if (0 === strpos($keyword,
'globals_')) {
158 $whole_keyword = $keyword_wrapper.$keyword.$keyword_wrapper;
160 $includes_globals = TRUE;
164 $sql = preg_replace($patterns, $replacements, $sql,
'1');
165 if($includes_globals) $sql = replace_global_keywords($sql);
168 foreach ($bind_vars as $bind_var => $bind_value) {
170 if (preg_match(
'/%:'.$bind_var.
'/i', $sql) != FALSE) {
171 $sql = str_replace(
'%:'.$bind_var,
':'.$bind_var, $sql);
172 $bind_vars[$bind_var] =
'%'.$bind_vars[$bind_var];
176 if (preg_match(
'/'.$bind_var.
'%/i', $sql) != FALSE) {
177 $sql = str_replace(
':'.$bind_var.
'%',
':'.$bind_var, $sql);
178 $bind_vars[$bind_var] = $bind_vars[$bind_var].
'%';
183 if (!isset($GLOBALS[
'SQ_SYSTEM']->tm) || !($GLOBALS[
'SQ_SYSTEM']->tm instanceof
Transaction_Manager)) {
184 require_once SQ_INCLUDE_PATH.
'/transaction_manager.inc';
185 $GLOBALS[
'SQ_SYSTEM']->tm =
new Transaction_Manager();
188 $GLOBALS[
'SQ_SYSTEM']->tm->begin(
'trigger_action_db');
190 foreach ($bind_vars as $bind_var => $bind_value) {
194 }
catch (Exception $e) {
195 $GLOBALS[
'SQ_SYSTEM']->tm->rollback(
'trigger_action_db');
197 trigger_localised_error(
'DATA0001', E_USER_WARNING, $e->getMessage(), $sql);
202 $GLOBALS[
'SQ_SYSTEM']->tm->commit(
'trigger_action_db');
221 public static function getInterface($settings, $prefix, $write_access=FALSE)
223 $method = array_get_index($settings,
'method',
'details');
224 $type = array_get_index($settings,
'type',
'pgsql');
225 $database = array_get_index($settings,
'database',
'');
226 $host = array_get_index($settings,
'host',
'');
227 $dsn = array_get_index($settings,
'dsn',
'');
228 $connector = array_get_index($settings,
'connector',
'0');
229 $username = array_get_index($settings,
'username',
'');
230 $password = array_get_index($settings,
'password',
'');
231 $sql = array_get_index($settings,
'sql',
'');
233 'details' => translate(
'database_details'),
234 'dsn' => translate(
'dsn'),
235 'connector' => translate(
'db_connector'),
237 if (!array_key_exists($method, $options) || empty($method)) {
240 $enable =
' style="display:block;visibility:visible;"';
241 $disable =
' style="display:none;visibility:hidden;"';
244 <script
type=
"text/javascript">
245 function toggleDatabaseSetup(connectionType)
248 var username = document.getElementById(
'trigger_action_user_username');
249 var password = document.getElementById(
'trigger_action_user_password');
250 var dbdetail = document.getElementById(
'trigger_action_database_details');
251 var dbcomdsn = document.getElementById(
'trigger_action_dsn');
252 var dbconnec = document.getElementById(
'trigger_action_connector');
253 dbdetail.style.display =
"none";
254 dbdetail.style.visibility =
"hidden";
255 dbcomdsn.style.display =
"none";
256 dbcomdsn.style.visibility =
"hidden";
257 dbconnec.style.display =
"none";
258 dbconnec.style.visibility =
"hidden";
259 username.style.display =
"block";
260 username.style.visibility =
"visible";
261 password.style.display =
"block";
262 password.style.visibility =
"visible";
264 if (connectionType ==
"dsn") {
265 dbcomdsn.style.display =
"block";
266 dbcomdsn.style.visibility =
"visible";
267 }
else if (connectionType ==
"connector") {
268 dbconnec.style.display =
"block";
269 dbconnec.style.visibility =
"visible";
270 username.style.display =
"none";
271 username.style.visibility =
"hidden";
272 password.style.display =
"none";
273 password.style.visibility =
"hidden";
275 dbdetail.style.display =
"block";
276 dbdetail.style.visibility =
"visible";
280 <table
class=
"sq-backend-table" style=
"width:auto">
282 <th><?php echo translate(
'connection_type'); ?></th>
285 combo_box($prefix.
'[method]', $options, FALSE, $method,
'1',
'onchange="toggleDatabaseSetup(document.getElementById(\''.$prefix.
'[method]\').value)"');
287 echo $options[$method];
293 <th><?php echo translate(
'connection_setup'); ?></th>
295 <table
class=
"sq-backend-table" style=
"width:auto">
296 <?php $style = ($method !=
'connector') ? $enable : $disable; ?>
297 <tr
id=
"trigger_action_user_username" <?php echo $style; ?>>
298 <th style=
"width:100px;"><?php echo translate(
'user_name');?></th>
301 text_box($prefix.
'[username]', $username, 35);
308 <tr
id=
"trigger_action_user_password" <?php echo $style; ?>>
309 <th style=
"width:100px;"><?php echo translate(
'password');?></th>
312 password_box($prefix.
'[password]', $password, 35);
314 echo translate(
'data_password_not_displayed');
319 <?php $style = ($method ==
'details') ? $enable : $disable; ?>
320 <tr
id=
"trigger_action_database_details" <?php echo $style; ?>>
321 <th style=
"width:100px;"><?php echo translate(
'database_details'); ?></th>
323 <table
class=
"sq-backend-table" style=
"width:auto">
325 <th><?php echo translate(
'database_type');?></th>
328 $db_options = Array(
'pgsql' =>
'PostgreSQL',
'oci' =>
'Oracle',
'mysql' =>
'MySQL');
329 if (!array_key_exists($type, $db_options) || empty($type)) {
333 combo_box($prefix.
'[type]', $db_options, FALSE, $type);
335 echo $db_options[$type];
341 <th><?php echo translate(
'database_name');?></th>
344 text_box($prefix.
'[database]', $database, 35);
352 <th><?php echo translate(
'host_name'); ?></th>
355 text_box($prefix.
'[host]', $host, 35);
365 <?php $style = ($method ==
'dsn') ? $enable : $disable; ?>
366 <tr
id=
"trigger_action_dsn" <?php echo $style; ?>>
367 <th style=
"width:100px;"><?php echo translate(
'dsn'); ?></th>
370 text_box($prefix.
'[dsn]', $dsn, 70);
377 <?php $style = ($method ==
'connector') ? $enable : $disable; ?>
378 <tr
id=
"trigger_action_connector" <?php echo $style; ?>>
379 <th style=
"width:100px;"><?php echo translate(
'db_connector'); ?></th>
382 asset_finder($prefix.
'[connector]', $connector, Array(
'db_connector'=>
'D'));
384 if (!empty($connector)) {
385 echo get_asset_tag_line($connector);
387 echo translate(
'no_asset_selected');
397 <th><?php echo translate(
'sql_query'); ?></th>
400 text_area($prefix.
'[sql]', $sql, 100, 40, 0,
'style="width: 80%; height: 30em; font-family: \'Courier New\', Courier, monospace; display: block"');
405 echo translate(
'trigger_execute_database_query_note');
412 $interface = ob_get_contents();
435 $settings[
'method'] = array_get_index($request_data,
'method',
'details');
436 $settings[
'type'] = array_get_index($request_data,
'type',
'pgsql');
437 $settings[
'database'] = array_get_index($request_data,
'database',
'');
438 $settings[
'host'] = array_get_index($request_data,
'host',
'');
439 $settings[
'dsn'] = array_get_index($request_data,
'dsn',
'');
440 $settings[
'connector'] = array_get_index((array_get_index($request_data,
'connector', Array())),
'assetid',
'0');
441 $settings[
'username'] = array_get_index($request_data,
'username',
'');
442 $settings[
'password'] = array_get_index($request_data,
'password',
'');
443 $settings[
'sql'] = array_get_index($request_data,
'sql',
'');