| 1: | <?php
|
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: |
|
| 18: |
|
| 19: | defined('XOOPS_ROOT_PATH') || exit('Restricted access');
|
| 20: |
|
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: |
|
| 28: | class SqlUtility
|
| 29: | {
|
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: |
|
| 42: | public static function splitMySqlFile(&$ret, $sql)
|
| 43: | {
|
| 44: | $sql = trim($sql);
|
| 45: | $sql_len = strlen($sql);
|
| 46: | $char = '';
|
| 47: | $string_start = '';
|
| 48: | $in_string = false;
|
| 49: |
|
| 50: | for ($i = 0; $i < $sql_len; ++$i) {
|
| 51: | $char = $sql[$i];
|
| 52: |
|
| 53: |
|
| 54: | if ($in_string) {
|
| 55: | for (; ;) {
|
| 56: | $i = strpos($sql, $string_start, $i);
|
| 57: |
|
| 58: |
|
| 59: | if (!$i) {
|
| 60: | $ret[] = $sql;
|
| 61: |
|
| 62: | return true;
|
| 63: | }
|
| 64: |
|
| 65: |
|
| 66: |
|
| 67: | elseif ($string_start === '`' || $sql[$i - 1] !== '\\') {
|
| 68: | $string_start = '';
|
| 69: | $in_string = false;
|
| 70: | break;
|
| 71: | }
|
| 72: |
|
| 73: |
|
| 74: | else {
|
| 75: |
|
| 76: | $j = 2;
|
| 77: | $escaped_backslash = false;
|
| 78: | while ($i - $j > 0 && $sql[$i - $j] === '\\') {
|
| 79: | $escaped_backslash = !$escaped_backslash;
|
| 80: | ++$j;
|
| 81: | }
|
| 82: |
|
| 83: |
|
| 84: | if ($escaped_backslash) {
|
| 85: | $string_start = '';
|
| 86: | $in_string = false;
|
| 87: | break;
|
| 88: | }
|
| 89: | else {
|
| 90: | ++$i;
|
| 91: | }
|
| 92: | }
|
| 93: | }
|
| 94: | }
|
| 95: |
|
| 96: | elseif ($char === ';') {
|
| 97: |
|
| 98: | $ret[] = substr($sql, 0, $i);
|
| 99: | $sql = ltrim(substr($sql, min($i + 1, $sql_len)));
|
| 100: | $sql_len = strlen($sql);
|
| 101: | if ($sql_len) {
|
| 102: | $i = -1;
|
| 103: | } else {
|
| 104: |
|
| 105: | return true;
|
| 106: | }
|
| 107: | }
|
| 108: |
|
| 109: | elseif (($char === '"') || ($char === '\'') || ($char === '`')) {
|
| 110: | $in_string = true;
|
| 111: | $string_start = $char;
|
| 112: | }
|
| 113: |
|
| 114: | elseif ($char === '#' || ($char === ' ' && $i > 1 && $sql[$i - 2] . $sql[$i - 1] === '--')) {
|
| 115: |
|
| 116: | $start_of_comment = (($sql[$i] === '#') ? $i : $i - 2);
|
| 117: |
|
| 118: |
|
| 119: | $end_of_comment = strpos(' ' . $sql, "\012", $i + 2) ?: strpos(' ' . $sql, "\015", $i + 2);
|
| 120: | if (!$end_of_comment) {
|
| 121: |
|
| 122: |
|
| 123: |
|
| 124: |
|
| 125: |
|
| 126: |
|
| 127: |
|
| 128: | return true;
|
| 129: | } else {
|
| 130: | $sql = substr($sql, 0, $start_of_comment) . ltrim(substr($sql, $end_of_comment));
|
| 131: | $sql_len = strlen($sql);
|
| 132: | $i--;
|
| 133: | }
|
| 134: | }
|
| 135: | }
|
| 136: |
|
| 137: | if (!empty($sql) && trim($sql) != '') {
|
| 138: | $ret[] = $sql;
|
| 139: | }
|
| 140: |
|
| 141: | return true;
|
| 142: | }
|
| 143: |
|
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: |
|
| 151: | public static function prefixQuery($query, $prefix)
|
| 152: | {
|
| 153: | $pattern = "/^(INSERT[\s]+INTO|CREATE[\s]+TABLE|ALTER[\s]+TABLE|UPDATE)(\s)+([`]?)([^`\s]+)\\3(\s)+/siU";
|
| 154: | $pattern2 = "/^(DROP TABLE)(\s)+([`]?)([^`\s]+)\\3(\s)?$/siU";
|
| 155: | if (preg_match($pattern, $query, $matches) || preg_match($pattern2, $query, $matches)) {
|
| 156: | $replace = "\\1 " . $prefix . "_\\4\\5";
|
| 157: | $matches[0] = preg_replace($pattern, $replace, $query);
|
| 158: |
|
| 159: | return $matches;
|
| 160: | }
|
| 161: |
|
| 162: | return false;
|
| 163: | }
|
| 164: | }
|
| 165: | |