REST MySQL Service


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  *
  4.  * @name MySQLRESTService
  5.  * @author Jonnie Spratley
  6.  * @version 1.0
  7.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8.  *
  9.  */
  10. //echo header('Content-type: application/x-json');
  11. require_once 'library/MySQLService.php';
  12. require_once 'library/FileSystemService.php';
  13.  
  14. $f = new FileSystemService ( );
  15.  
  16. error_reporting ( E_ERROR | E_USER_ERROR | E_PARSE );
  17.  
  18. if ( isset ( $_REQUEST[ 'h' ] ) )
  19. {
  20. $host = $_REQUEST[ 'h' ]; //Host
  21. }
  22.  
  23. if ( isset ( $_REQUEST[ 'u' ] ) )
  24. {
  25. $user = $_REQUEST[ 'u' ]; //User
  26. }
  27.  
  28. if ( isset ( $_REQUEST[ 'p' ] ) )
  29. {
  30. $pass = $_REQUEST[ 'p' ]; //Password
  31. }
  32. if ( isset ( $_REQUEST[ 'm' ] ) )
  33. {
  34. $mode = $_REQUEST[ 'm' ]; //Mode
  35. }
  36.  
  37. if ( isset ( $_REQUEST[ 'd' ] ) )
  38. {
  39. $database = $_REQUEST[ 'd' ]; //Database
  40. }
  41.  
  42. if ( isset ( $_REQUEST[ 't' ] ) )
  43. {
  44. $table = $_REQUEST[ 't' ]; //Table
  45. }
  46.  
  47. if ( isset ( $_REQUEST[ 'q' ] ) )
  48. {
  49. $query = $_REQUEST[ 'q' ]; //Query
  50. }
  51.  
  52. //$user = $_REQUEST[ 'u' ]; //Username
  53. //$pass = $_REQUEST[ 'p' ]; //Password
  54. //$database = $_REQUEST[ 'd' ]; //Database
  55. //$table = $_REQUEST[ 't' ]; //Table
  56. //$mode = $_REQUEST[ 'm' ]; //Mode
  57. //$query = $_REQUEST[ 'q' ]; //Query
  58.  
  59.  
  60. $service = new MySQLService ( $host, $user, $pass );
  61.  
  62. //Switch depending on mode
  63. switch ( $mode )
  64. {
  65. case 'ping' :
  66. $db = $service->ping ();
  67. echo $db;
  68. break;
  69.  
  70. /*********************************************
  71. * ANALYZE/OPTIMIZE/CHECK/REPAIR TABLES
  72. *********************************************/
  73. case 'analyzeTable' :
  74. $db = $service->analyzeTable ( $database, $table );
  75. echo $db;
  76. break;
  77. case 'optimizeTable' :
  78. $db = $service->optimizeTable ( $database, $table );
  79. echo $db;
  80. break;
  81. case 'checkTable' :
  82. $db = $service->checkTable ( $database, $table );
  83. echo $db;
  84. break;
  85. case 'repairTable' :
  86. $db = $service->repairTable ( $database, $table );
  87. echo $db;
  88. break;
  89.  
  90. /*********************************************
  91. * CREATE DATEBASE/TABLE/USER
  92. *********************************************/
  93. case 'createDatabase' :
  94. $db = $service->createDatabase ( $database );
  95. echo $db;
  96. break;
  97. case 'createTable' :
  98. $db = $service->createTable ( $database, $table );
  99. echo $db;
  100. break;
  101.  
  102. case 'alterTable' :
  103. $db = $service->alterTable ( $database, $table, $query );
  104. echo $db;
  105. break;
  106.  
  107. case 'removeTable' :
  108. $db = $service->removeTable ( $database, $query );
  109. echo $db;
  110. break;
  111.  
  112. case 'renameTable' :
  113. $db = $service->renameTable ( $database, $table, $query );
  114. echo $db;
  115. break;
  116.  
  117. // case 'createUser' :
  118. // $db = $service->createUser ( $user );
  119. // echo $db;
  120. // break;
  121.  
  122.  
  123. /*********************************************
  124. * INSERT/UPDATE/REMOVE DATA
  125. *********************************************/
  126. case 'insertRecord' :
  127. $insert = $service->insertRecord ( $query );
  128. echo $insert;
  129. break;
  130. case 'updateRecord' :
  131. $update = $service->updateRecord ( $query );
  132. echo $update;
  133. break;
  134. case 'removeRecord' :
  135. $remove = $service->removeRecord ( $query );
  136. echo $remove;
  137. break;
  138.  
  139. /*********************************************
  140. * DATABASE/TABLE/FIELD INFO
  141. *********************************************/
  142. case 'describeTable' :
  143. $db = $service->describeTable ( $database, $table );
  144. echo $db;
  145. break;
  146. case 'getTableColumns' :
  147. $db = $service->getTableColumns ( $database, $table );
  148. echo $db;
  149. break;
  150. case 'getDatabases' :
  151. $db = $service->getDatabases ();
  152. echo $db;
  153. break;
  154. case 'getDatabasesAndTables' :
  155. $db = $service->getDatabasesAndTables ();
  156. echo $db;
  157. break;
  158. case 'getTableIndex' :
  159. $db = $service->getTableIndex ( $database, $table );
  160. echo $db;
  161. break;
  162. case 'getTables' :
  163. $db = $service->_getTableAndFields ( $database );
  164. echo $db;
  165. break;
  166. case 'getServerInfo' :
  167. $db = $service->_getServerInfo ();
  168. echo $db;
  169. break;
  170. case 'getTableData' :
  171. $db = $service->getTableData ( $database, $table );
  172. echo $db;
  173. break;
  174. case 'getUserInfo' :
  175. $db = $service->getUserInfo ( $query );
  176. echo $db;
  177. break;
  178.  
  179. /*********************************************
  180. * EXECUTE QUERY/ RESULT XML/JSON
  181. *********************************************/
  182. case 'executeQuery' :
  183. $db = $service->executeQuery ( $query );
  184. echo $db;
  185. break;
  186. case 'explainQuery' :
  187. $db = $service->executeQuery ( "EXPLAIN $query" );
  188. echo $db;
  189. break;
  190.  
  191. case 'executeQueryToXML' :
  192. header ( "Content-type: text/xml" );
  193. $db = $service->exportToXML ( $database, $table );
  194. echo $db;
  195. break;
  196. case 'exportToJSON' :
  197. $db = $service->exportToJSON ( $database, $table );
  198. echo $db;
  199. break;
  200.  
  201. /*********************************************
  202. * MYSQL STATS
  203. *********************************************/
  204. case 'getBytes' : //check
  205. $db = $service->_getBytes ();
  206. echo $db;
  207. break;
  208. case 'getCommands' : //check
  209. $db = $service->_getCommands ();
  210. echo $db;
  211. break;
  212. case 'getConnections' : //check
  213. $db = $service->_getConnections ();
  214. echo $db;
  215. break;
  216. case 'getInnoDb' : //check
  217. $db = $service->_getInnoDb ();
  218. echo $db;
  219. break;
  220. case 'getKeys' : //check
  221. $db = $service->_getKeys ();
  222. echo $db;
  223. break;
  224. case 'getOpen' : //check
  225. $db = $service->_getOpen ();
  226. echo $db;
  227. break;
  228. case 'getPerformance' : //check
  229. $db = $service->_getPerformance ();
  230. echo $db;
  231. break;
  232. case 'getQcache' : //check
  233. $db = $service->_getQcache ();
  234. echo $db;
  235. break;
  236. case 'getShowCommands' : //check
  237. $db = $service->_getShowCommands ();
  238. echo $db;
  239. break;
  240. case 'getSort' : //check
  241. $db = $service->_getSort ();
  242. echo $db;
  243. break;
  244. case 'getTemp' : //check
  245. $db = $service->_getTemp ();
  246. echo $db;
  247. break;
  248. case 'getThreads' : //check
  249. $db = $service->_getThreads ();
  250. echo $db;
  251. break;
  252. case 'getReplication' : //check
  253. $db = $service->_getReplication ();
  254. echo $db;
  255. break;
  256. case 'getQuestions' : //check
  257. $db = $service->_getQuestions ();
  258. echo $db;
  259. break;
  260. case 'getUptime' : //check
  261. $db = $service->_getUptime();
  262. echo $db;
  263. break;
  264. case 'getHandlers' : //check
  265. $db = $service->_getHandlers();
  266. echo $db;
  267. break;
  268. case 'pollTraffic' : //check
  269. $db = $service->pollTraffic ();
  270. echo $db;
  271. break;
  272. case 'pollQueries' : //check
  273. $db = $service->pollQueries ();
  274. echo $db;
  275. break;
  276. case 'pollConnections' : //check
  277. $db = $service->pollConnections ();
  278. echo $db;
  279. break;
  280.  
  281. /*********************************************
  282. * SYSTEM INFORMATION
  283. *********************************************/
  284. case 'showSystemVariables' :
  285. $db = $service->showSystemVariables ();
  286. echo $db;
  287. break;
  288. case 'showSystemPrivileges' :
  289. $db = $service->showPrivileges ();
  290. echo $db;
  291. break;
  292. case 'showSystemStatus' :
  293. $db = $service->showSystemStatus ();
  294. echo $db;
  295. break;
  296. case 'showSystemProcess' :
  297. $db = $service->showSystemProcess ();
  298. echo $db;
  299. break;
  300. case 'showTableStatus' :
  301. $db = $service->showTableStatus ( $database );
  302. echo $db;
  303. break;
  304. case 'showSystemUsers' :
  305. $db = $service->showSystemUsers ();
  306. echo $db;
  307. break;
  308.  
  309. case 'killProcess' :
  310. $db = $service->killProcess ( $query );
  311. echo $db;
  312. break;
  313.  
  314. /*********************************************
  315. * BACKUPS/EXPORTS/IMPORTS
  316. *********************************************/
  317. case 'createBackup' :
  318. $db = $service->createBackup ( $database, $table, $query );
  319. echo $db;
  320. break;
  321. case 'getDatabaseBackups' :
  322. $db = $service->getDatabaseBackups ();
  323. echo $db;
  324. break;
  325. case 'removeBackup' :
  326. $db = $service->removeBackup ( $database, $query );
  327. echo $db;
  328. break;
  329.  
  330. case 'getDiskInfo' :
  331. $files = $f->getDiskInfo ( '../backups' );
  332. echo json_encode ( $files );
  333. break;
  334.  
  335. case 'getDatabaseSpace' :
  336. $db = $service->getDatabaseSpace ();
  337. echo $db;
  338. break;
  339. case 'analyzeQuery' :
  340. $db = $service->analyzeQuery( $query );
  341. echo $db;
  342. break;
  343.  
  344.  
  345. default :
  346. trigger_error ( 'Please choose a mode.' );
  347. exit ();
  348. }
  349.  
  350. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.