1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: defined ( 'CON_FRAMEWORK' ) || die ( 'Illegal call: Missing framework initialization - request aborted.' );
14: function hasMySQLExtension() {
15: return (isPHPExtensionLoaded ( "mysql" ) == CON_EXTENSION_AVAILABLE) ? true : false;
16: }
17: function hasMySQLiExtension() {
18: return (isPHPExtensionLoaded ( "mysqli" ) == CON_EXTENSION_AVAILABLE) ? true : false;
19: }
20: function doMySQLConnect($host, $username, $password) {
21: $aOptions = array (
22: 'connection' => array (
23: 'host' => $host,
24: 'user' => $username,
25: 'password' => $password
26: )
27: );
28: try {
29: $db = new cDb ( $aOptions );
30: } catch ( Exception $e ) {
31: return array (
32: $db,
33: false
34: );
35: }
36:
37: if ($db->connect () == 0) {
38: return array (
39: $db,
40: false
41: );
42: } else {
43: return array (
44: $db,
45: true
46: );
47: }
48: }
49:
50: 51: 52: 53: 54: 55: 56: 57:
58: function doMySQLSelectDB($linkid, $database) {
59: $extension = getMySQLDatabaseExtension ();
60:
61: if (CON_SETUP_MYSQLI === $extension) {
62: return (@mysqli_select_db ( $linkid, $database )) ? true : false;
63: } elseif (CON_SETUP_MYSQL === $extension) {
64: return (@mysql_select_db ( $database, $linkid )) ? true : false;
65: } else {
66: return false;
67: }
68: }
69: function getSetupMySQLDBConnection($full = true) {
70: global $cfg;
71:
72: $cfgDb = $cfg ['db'];
73:
74: if ($full === false) {
75:
76: unset ( $cfgDb ['connection'] ['database'] );
77: }
78:
79: $db = new cDb ( $cfgDb );
80: return $db;
81: }
82:
83: 84: 85: 86: 87:
88: function getMySQLDatabaseExtension() {
89: if (hasMySQLiExtension ()) {
90: return CON_SETUP_MYSQLI;
91: } elseif (hasMySQLExtension ()) {
92: return CON_SETUP_MYSQL;
93: } else {
94: return null;
95: }
96: }
97: function fetchMySQLVersion($db) {
98: $db->query ( "SELECT VERSION()" );
99:
100: return ($db->nextRecord ()) ? $db->f ( 0 ) : false;
101: }
102: function fetchMySQLUser($db) {
103: $db->query ( "SELECT USER()" );
104:
105: return ($db->nextRecord ()) ? $db->f ( 0 ) : false;
106: }
107: function checkMySQLDatabaseCreation($db, $database, $charset = '', $collation = '') {
108: if (checkMySQLDatabaseExists ( $db, $database )) {
109: return true;
110: } else if($collation == '') {
111: $db->query ( "CREATE DATABASE `%s`", $database );
112: return ($db->getErrorNumber () == 0) ? true : false;
113: } else {
114: $db->query ( "CREATE DATABASE `%s` CHARACTER SET %s COLLATE %s", $database, $charset, $collation );
115: return ($db->getErrorNumber () == 0) ? true : false;
116: }
117: }
118: function checkMySQLDatabaseExists($db, $database) {
119: $db->connect ();
120:
121: if (doMySQLSelectDB ( $db->getLinkId (), $database )) {
122: return true;
123: } else {
124: $db->query ( "SHOW DATABASES LIKE '%s'", $database );
125: return ($db->nextRecord ()) ? true : false;
126: }
127: }
128: function checkMySQLDatabaseUse($db, $database) {
129: $db->connect ();
130: return doMySQLSelectDB ( $db->getLinkId (), $database );
131: }
132: function checkMySQLTableCreation($db, $database, $table) {
133: if (checkMySQLDatabaseUse ( $db, $database ) == false) {
134: return false;
135: }
136:
137: $db->query ( "CREATE TABLE `%s` (test INT(1) NOT NULL) ENGINE = MYISAM;", $table );
138:
139: return ($db->getErrorNumber () == 0) ? true : false;
140: }
141: function checkMySQLLockTable($db, $database, $table) {
142: if (checkMySQLDatabaseUse ( $db, $database ) == false) {
143: return false;
144: }
145:
146: $db->query ( "LOCK TABLES `%s` WRITE", $table );
147:
148: return ($db->getErrorNumber () == 0) ? true : false;
149: }
150: function checkMySQLUnlockTables($db, $database) {
151: if (checkMySQLDatabaseUse ( $db, $database ) == false) {
152: return false;
153: }
154:
155: $db->query ( "UNLOCK TABLES" );
156:
157: return ($db->getErrorNumber () == 0) ? true : false;
158: }
159: function checkMySQLDropTable($db, $database, $table) {
160: if (checkMySQLDatabaseUse ( $db, $database ) == false) {
161: return false;
162: }
163:
164: $db->query ( "DROP TABLE `%s`", $table );
165:
166: return ($db->getErrorNumber () == 0) ? true : false;
167: }
168: function checkMySQLDropDatabase($db, $database) {
169: $db->query ( "DROP DATABASE `%s`", $database );
170:
171: return ($db->getErrorNumber () == 0) ? true : false;
172: }
173: function fetchMySQLStorageEngines($db) {
174: $db->query ( "SHOW ENGINES" );
175:
176: $engines = array ();
177:
178: while ( $db->nextRecord () ) {
179: $engines [] = $db->f ( 0 );
180: }
181:
182: return $engines;
183: }
184:
185: 186: 187: 188: 189: 190:
191: function fetchMySQLCharsets($db = null) {
192: if (! is_object ( $db )) {
193:
194: return array (
195: 'big5',
196: 'dec8',
197: 'cp850',
198: 'hp8',
199: 'koi8r',
200: 'latin1',
201: 'latin2',
202: 'swe7',
203: 'ascii',
204: 'ujis',
205: 'sjis',
206: 'hebrew',
207: 'tis620',
208: 'euckr',
209: 'koi8u',
210: 'gb2312',
211: 'greek',
212: 'cp1250',
213: 'gbk',
214: 'latin5',
215: 'armscii8',
216: 'utf8',
217: 'ucs2',
218: 'cp866',
219: 'keybcs2',
220: 'macce',
221: 'macroman',
222: 'cp852',
223: 'latin7',
224: 'utf8mb4',
225: 'cp1251',
226: 'utf16',
227: 'cp1256',
228: 'cp1257',
229: 'utf32',
230: 'binary',
231: 'geostd8',
232: 'cp932',
233: 'eucjpms'
234: );
235: }
236:
237: $db->query ( 'SHOW CHARACTER SET' );
238:
239: $charsets = array ();
240:
241: while ( $db->nextRecord () ) {
242: $charsets [] = $db->f ( 'Charset' );
243: }
244:
245: return $charsets;
246: }
247:
248: 249: 250: 251: 252: 253: 254:
255: function fetchMySQLCollations($db = null, $charset = "") {
256: if (! is_object ( $db )) {
257:
258: return array (
259: 'big5_chinese_ci',
260: 'dec8_swedish_ci',
261: 'cp850_general_ci',
262: 'hp8_english_ci',
263: 'koi8r_general_ci',
264: 'latin1_swedish_ci',
265: 'latin2_general_ci',
266: 'swe7_swedish_ci',
267: 'ascii_general_ci',
268: 'ujis_japanese_ci',
269: 'sjis_japanese_ci',
270: 'hebrew_general_ci',
271: 'tis620_thai_ci',
272: 'euckr_korean_ci',
273: 'koi8u_general_ci',
274: 'gb2312_chinese_ci',
275: 'greek_general_ci',
276: 'cp1250_general_ci',
277: 'gbk_chinese_ci',
278: 'latin5_turkish_ci',
279: 'armscii8_general_ci',
280: 'utf8_general_ci',
281: 'utf8_unicode_ci',
282: 'ucs2_general_ci',
283: 'cp866_general_ci',
284: 'keybcs2_general_ci',
285: 'macce_general_ci',
286: 'macroman_general_ci',
287: 'cp852_general_ci',
288: 'latin7_general_ci',
289: 'utf8mb4_general_ci',
290: 'cp1251_general_ci',
291: 'utf16_general_ci',
292: 'cp1256_general_ci',
293: 'cp1257_general_ci',
294: 'utf32_general_ci',
295: 'binary',
296: 'geostd8_general_ci',
297: 'cp932_japanese_ci',
298: 'eucjpms_japanese_ci'
299: );
300: }
301:
302: $db->query ( 'SHOW COLLATION' );
303:
304: $charsets = array ();
305:
306: while ( $db->nextRecord () ) {
307: $charsets [] = $db->f ( 'Collation' );
308: }
309:
310: return $charsets;
311: }
312:
313: ?>