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