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