PageRenderTime 67ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/sql.php

https://github.com/radixcl/powerirc
PHP | 503 lines | 421 code | 74 blank | 8 comment | 113 complexity | a8bac3cbc41a48d26459d41d0cdc9af3 MD5 | raw file
  1. <?
  2. define("METHOD_ADD", 1);
  3. define("METHOD_EDIT", 2);
  4. class SQL {
  5. private $__QUERIES;
  6. private $__link;
  7. private $__SQL_CONNECTION = false;
  8. private $__addFieldReg = Array();
  9. private $__sqlRegTitle = Array();
  10. private $__requiredField = Array();
  11. private $__retURLVal = Array();
  12. private $__getIgnoredField = Array();
  13. private $__sqlCustomField = Array();
  14. public function dbconnect() {
  15. global $DB_HOST, $DB_USER, $DB_PASS, $DB_NAME;
  16. $link = mysql_connect($DB_HOST, $DB_USER, $DB_PASS) or die("No se pudo conectar a la base de datos");
  17. mysql_select_db($DB_NAME) or die("No se pudo seleccionar la base de datos");
  18. if ($link) {
  19. $this->__link = $link;
  20. $this->__SQL_CONNECTION = true;
  21. return true;
  22. } else {
  23. return false;
  24. }
  25. }
  26. public function dbquery($query) {
  27. $result = @mysql_query($query, $this->__link);
  28. $this->__QUERIES++;
  29. return $result;
  30. }
  31. public function dbdisconnect($link) {
  32. mysql_close($link);
  33. }
  34. public function fetchrow($text) {
  35. return mysql_fetch_row($text);
  36. }
  37. public function fetcharray($text) {
  38. return @mysql_fetch_array($text);
  39. }
  40. public function reset($res) {
  41. mysql_data_seek($res, 0);
  42. }
  43. public function fastquery($query) {
  44. if ($this->__SQL_CONNECTION == false) $this->dbconnect();
  45. $result = $this->dbquery($query);
  46. return $result;
  47. }
  48. public function sql_num_rows($query) {
  49. return mysql_num_rows($query);
  50. }
  51. public function getErrno() {
  52. return mysql_errno($this->__link);
  53. }
  54. public function addFieldReg($table, $field, $newname, $required = false) {
  55. if ($required == true)
  56. $this->__requiredField[$table][$field] = true;
  57. $this->__addFieldReg[$table][$field] = $newname;
  58. }
  59. private function getFieldReg($table, $field) {
  60. if ($this->__addFieldReg[$table][$field]) {
  61. return $this->__addFieldReg[$table][$field];
  62. } else {
  63. return $field;
  64. }
  65. }
  66. public function addSQLRegTitle($table, $title) {
  67. $this->__sqlRegTitle[$table] = $title;
  68. }
  69. public function getReferences($table, $column) {
  70. global $DB_HOST, $DB_USER, $DB_PASS, $DB_NAME;
  71. $query = "SELECT u.column_name, u.referenced_table_schema, u.referenced_table_name, u.referenced_column_name
  72. FROM information_schema.table_constraints AS c
  73. INNER JOIN information_schema.key_column_usage AS u
  74. USING ( constraint_schema, constraint_name )
  75. WHERE c.constraint_type = 'FOREIGN KEY'
  76. AND c.table_schema = '$DB_NAME'
  77. AND c.table_name = '$table'
  78. AND u.column_name = '$column'
  79. LIMIT 0 , 30
  80. ";
  81. $sql = $this->fastquery($query);
  82. $res = $this->fetcharray($sql);
  83. return($res);
  84. }
  85. public function addSQLReg($table) {
  86. $this->_putForm($table, $id, METHOD_ADD, $GET['oldurl']);
  87. }
  88. public function doAddSQL($data) {
  89. as_array($data);
  90. reset($data);
  91. //print_r2($data);die();
  92. while (list($key, $val) = each($data)) {
  93. if ($key == '__SQLReturnVal') continue;
  94. if ($key == '__sqlRegTable') {
  95. $__sqltmp1 = "$val";
  96. } else if ($key == 'pass' || $key == 'password') {
  97. $__sqltmp2 .= "$key, ";
  98. $__sqltmp3 .= "SHA1('$val'), ";
  99. } else if ($key{0} == '_' && $key{1} == '_') {
  100. continue;
  101. } else {
  102. $__sqltmp2 .= "$key, ";
  103. $__sqltmp3 .= "'$val', ";
  104. }
  105. }
  106. $__sqltmp1 = trim($__sqltmp1, " ,");
  107. $__sqltmp2 = trim($__sqltmp2, " ,");
  108. $__sqltmp3 = trim($__sqltmp3, " ,");
  109. $__sqltmp = "INSERT INTO $__sqltmp1 ($__sqltmp2) VALUES ($__sqltmp3)";
  110. //echo $__sqltmp;die();
  111. $ret = $this->fastquery($__sqltmp);
  112. if (!$ret) {
  113. __sql_error($this->getErrno());
  114. }
  115. }
  116. public function retURL($table, $url) {
  117. $this->__retURLVal[$table] = $url;
  118. }
  119. public function getSQLIgnoreField($table, $field) {
  120. $this->__getIgnoredField[$table][$field] = true;
  121. }
  122. public function getFieldName($table, $field) {
  123. $query = "SHOW FULL COLUMNS IN $table WHERE Field = '$field'";
  124. $sql = $this->fastquery($query);
  125. $res = $this->fetcharray($sql);
  126. if($res['Comment'])
  127. return($res['Comment']);
  128. else
  129. return($field);
  130. }
  131. private function isNULL($table, $field) {
  132. $query = "SHOW FULL COLUMNS IN $table WHERE Field = '$field'";
  133. $sql = $this->fastquery($query);
  134. $res = $this->fetcharray($sql);
  135. if ($res['Null'] == 'YES')
  136. return true;
  137. else
  138. return false;
  139. }
  140. public function getSQLRegs($table, $match) {
  141. global $SELECT, $DESC, $_REF;
  142. if ($SELECT[$table] != '') {
  143. $query = "SELECT $SELECT[$table] FROM $table";
  144. } else {
  145. $query = "SELECT * FROM $table";
  146. }
  147. $fields = Array();
  148. $i = 0;
  149. $ret = $this->fastquery("EXPLAIN $table");
  150. while($tmp = $this->fetcharray($ret)) {
  151. if ($this->__getIgnoredField[$table][$tmp['Field']]) {
  152. continue;
  153. }
  154. $fields[$i++] = $tmp['Field'];
  155. }
  156. if ($match != '') {
  157. $subq = 'WHERE (';
  158. reset($fields);
  159. while (list($key, $val) = each($fields)) {
  160. $subq .= " upper($val) LIKE upper('%$match%') OR";
  161. }
  162. $subq = preg_replace('/ OR$/', '', $subq);
  163. $query = "$query $subq)";
  164. }
  165. reset($fields);
  166. $ret = $this->fastquery($query);
  167. if (!$ret) {
  168. __error("Error ejecutando sentencia SQL");
  169. }
  170. ?>
  171. <div align="center">
  172. <?
  173. $ii = 0;
  174. while ($res = $this->fetcharray($ret)) {
  175. if ($ii == 0) {
  176. ?>
  177. <table border="0" class="listTable" style="width: 90%;">
  178. <thead>
  179. <tr>
  180. <?
  181. while (list($key1, $val1) = each($res)) {
  182. if (is_numeric($key1)) continue;
  183. ?>
  184. <td>
  185. <nobr><?=$this->getFieldName($table, $key1)?></nobr>
  186. </td>
  187. <?
  188. }
  189. ?>
  190. <td>
  191. &nbsp;
  192. </td>
  193. </tr>
  194. </thead>
  195. <tbody>
  196. <?
  197. reset($res);
  198. }
  199. $ii++;
  200. $trid = uniqid();
  201. ?>
  202. <tr id="_sql_reg_<?=$trid?>">
  203. <?
  204. while (list($key, $val) = each($res)) {
  205. if ($this->__getIgnoredField[$table][$key]) continue;
  206. if (is_numeric($key)) continue;
  207. ?>
  208. <td><?
  209. $references = $this->getReferences($table, $key);
  210. //print_r2($references);
  211. $reference_table = $references[2];
  212. $reference_column = $references[3];
  213. if ($reference_column != '') {
  214. $fk_query = "SELECT * FROM $reference_table WHERE $reference_column = $val";
  215. //echo $fk_query;
  216. $fk_sql = $this->fastquery($fk_query);
  217. $refcount = intval($_REF[$reference_table]);
  218. if ($refcount == 0) $refcount = 1;
  219. while ($fk_res = $this->fetcharray($fk_sql)) {
  220. for ($i=1; $i <= $refcount ; $i++)
  221. echo "$fk_res[$i] ";
  222. }
  223. } else {
  224. echo "$val";
  225. }
  226. ?></td>
  227. <?
  228. }
  229. ?>
  230. <td><nobr>
  231. <a href="generic.php?op=edit&amp;table=<?=$table?>&amp;id=<?=$res['id']?>&amp;oldurl=<?=urlencode($_SERVER['REQUEST_URI'])?>"><img src="template/img/icons/document-properties.png" border="0" alt="Editar" title="Editar"/></a>
  232. <a href="#" onclick="return(js_delete_reg(<?=$res['id']?>, '<?=$table?>', '_sql_reg_<?=$trid?>'))"><img src="template/img/icons/edit-delete.png" border="0" alt="Eliminar" title="Eliminar" /></a>
  233. </nobr>
  234. </td>
  235. </tr>
  236. <?
  237. }
  238. ?>
  239. </tbody>
  240. </table>
  241. </div>
  242. <?
  243. }
  244. public function _putForm($table, $id, $method, $oldurl) {
  245. global $_REF;
  246. $sql = $this->fastquery("EXPLAIN $table");
  247. $sqldata = $this->fastquery("SELECT * FROM $table WHERE id = '$id'");
  248. $resdata = $this->fetcharray($sqldata);
  249. if ($method == METHOD_EDIT) {
  250. $_form_action = 'generic.php?op=doEditSQL';
  251. $_nochange = '__[[NOCHANGE]]__';
  252. } else {
  253. $_form_action = 'generic.php?op=doAddSQL';
  254. }
  255. ?>
  256. <form name="frm_<?=$table?>" id="frm_<?=$table?>" method="post" action="<?=$_form_action?>" enctype="multipart/form-data" onsubmit="return __submitForm_<?=$table?>(this);">
  257. <input type="hidden" name="__sqlRegTable" value="<?=$table?>">
  258. <input type="hidden" name="__SQLReturnVal" value="<?=htmlentities($this->__retURLVal[$table])?>">
  259. <input type="hidden" name="__oldurl" value="<?=$oldurl?>">
  260. <div align="left">
  261. <table border="0" class="tableReg" style="width: 350pt;">
  262. <?
  263. if ($this->__sqlRegTitle[$table]) {
  264. ?>
  265. <tr>
  266. <td colspan="3" align="center">
  267. <h1 class="tableRegTitle"><?=$this->__sqlRegTitle[$table]?></h1>
  268. </td>
  269. </tr>
  270. <?
  271. }
  272. $ii = 0;
  273. while ($res = $this->fetcharray($sql)) {
  274. if ($res['Field'] == 'id' && $res['Extra'] == 'auto_increment') {
  275. ?>
  276. <input type="hidden" name="id" value="<?=$id?>">
  277. <?
  278. continue;
  279. }
  280. $res['Type2'] = preg_replace('/\(.*\)/','',$res['Type']);
  281. ?><tr id="_tr_<?=$table?>_<?=$ii++?>">
  282. <td id="_td_<?=$table?>_<?=$ii++?>" align="right">
  283. <?=$this->getFieldName($table, $res['Field'])?>
  284. </td>
  285. <td align="left" id="_td_<?=$table?>_<?=$ii++?>">
  286. <?
  287. $references = $this->getReferences($table, $res['Field']);
  288. //print_r2($references);
  289. $reference_table = $references[2];
  290. $reference_column = $references[3];
  291. if ($reference_column != '') {
  292. $fk_query = "SELECT * FROM $reference_table";
  293. //echo $fk_query;
  294. $fk_sql = $this->fastquery($fk_query);
  295. $refcount = intval($_REF[$reference_table]);
  296. if ($refcount == 0) $refcount = 1;
  297. echo "<select name=\"$res[Field]\">";
  298. while ($fk_res = $this->fetcharray($fk_sql)) {
  299. if ($resdata[$res['Field']] == $fk_res[0]) $fk_selected = ' selected';
  300. echo "<option value=\"$fk_res[0]\"$fk_selected>";
  301. for ($i=1; $i <= $refcount ; $i++)
  302. echo "$fk_res[$i] ";
  303. echo "</option>";
  304. $fk_selected = '';
  305. }
  306. echo "</select>";
  307. } else {
  308. if (!$this->isNULL($table, $res['Field']))
  309. $required = 'required';
  310. else
  311. $required = '';
  312. if ($res['Type2'] == 'varchar') {
  313. if ($res['Field'] == 'pass' || $res['Field'] == 'password') {
  314. ?><input <?=$required?> style="width: 100%;" type="password" name="<?=$res['Field']?>" value="<?=$_nochange?>" /><?
  315. } else {
  316. ?><input <?=$required?> style="width: 100%;" type="text" name="<?=$res['Field']?>" value="<?=$resdata[$res['Field']]?>" /><?
  317. }
  318. } else if ($res['Type2'] == 'text') {
  319. ?><textarea <?=$required?> style="width: 100%;" name="<?=$res['Field']?>" rows="4"><?=$resdata[$res['Field']]?></textarea><?
  320. } else if ($res['Type2'] == 'float') {
  321. ?>
  322. <div align="right">
  323. <input <?=$required?> style="width: 60pt; text-align: right;" type="text" name="<?=$res['Field']?>" value="<?=$resdata[$res['Field']]?>" />
  324. </div>
  325. <?
  326. } else if ($res['Type2'] == 'enum') {
  327. // hacky: convertir enum en aray...
  328. $code = preg_replace('/^enum/', 'Array', $res['Type']);
  329. eval("\$data = $code;");
  330. echo "<select name=\"$res[Field]\">";
  331. while (list($key, $val) = each($data)) {
  332. if ($resdata[$res['Field']] == $val) $selected = ' selected';
  333. echo "<option value=\"$val\"$selected>";
  334. echo $key;
  335. echo "</option>";
  336. $selected = '';
  337. }
  338. echo "</select>";
  339. } else {
  340. echo "unhandled ". $res['Type2'];
  341. }
  342. }
  343. ?>
  344. </td>
  345. <td id="_td_<?=$table?>_<?=$ii++?>">
  346. <?
  347. if (!$this->isNULL($table, $res['Field'])) {
  348. echo " *";
  349. } else {
  350. echo "&nbsp;";
  351. }
  352. ?>
  353. </td>
  354. </tr>
  355. <?
  356. }
  357. $this->reset($sql);
  358. ?>
  359. <tr id="_tr_<?=$table?>_<?=$ii++?>">
  360. <td id="_td_<?=$table?>_<?=$ii++?>" colspan="2" align="right">
  361. <div style="display: none;" id="_<?=$table?>_additional_1">ad</div>
  362. <input type="submit" value="Guardar">
  363. </td>
  364. <td>&nbsp;</td>
  365. </tr>
  366. </table>
  367. </div>
  368. </form>
  369. <script type="text/javascript">
  370. <!--
  371. function __submitForm_<?=$table?>(frm) {
  372. <?
  373. while ($res = $this->fetcharray($sql)) {
  374. if ($this->isNULL($table, $res['Field'])) continue;
  375. ?>
  376. if (frm.<?=$res['Field']?>.value == '' && frm.<?=$res['Field']?>.type != 'hidden') {
  377. alert('Ingrese el valor del campo <?=$res['Field']?>');
  378. frm.<?=$res['Field']?>.focus();
  379. return(false);
  380. }
  381. <?
  382. }
  383. ?>
  384. return(true);
  385. }
  386. -->
  387. </script>
  388. <?
  389. }
  390. public function editSQLReg($table, $id) {
  391. $this->_putForm($table, $id, METHOD_EDIT, $GET['oldurl']);
  392. }
  393. public function findPriKey($table) {
  394. // encontrar llave primaria
  395. $_query = "SHOW FULL COLUMNS IN $table WHERE `Key` = 'PRI'";
  396. $q = $this->fastquery($_query);
  397. $reskey = $this->fetcharray($q);
  398. if ($reskey['Field'] == '') {
  399. return(false);
  400. }
  401. return($reskey['Field']);
  402. }
  403. public function doEditSQL($data) {
  404. as_array($data);
  405. $table = $data['__sqlRegTable'];
  406. $back = $data['__SQLReturnVal'];
  407. $prikey = $this->findPriKey($table);
  408. if (!$prikey) {
  409. __error("Tabla $table no tiene llave primaria");
  410. }
  411. $prikeydata = $data[$prikey];
  412. $query = "UPDATE $table SET ";
  413. reset($data);
  414. while (list($key, $val) = each($data)) {
  415. if ($key{0} . $key{1} == '__') continue;
  416. if ($key == 'pass' && $val == '__[[NOCHANGE]]__') continue;
  417. if ($key == 'password' && $val == '__[[NOCHANGE]]__') continue;
  418. $query .= "$key = NULLIF('$val', ''), ";
  419. }
  420. $query = trim($query, ' ,');
  421. $query .= " WHERE $prikey = '$prikeydata' LIMIT 1";
  422. $sql = new SQL();
  423. $ret = $sql->fastquery($query);
  424. if (!$ret) {
  425. __error("Error ejecutando sentencia SQL");
  426. }
  427. }
  428. }
  429. ?>