PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/hypervm/httpdocs/htmllib/phplib/lib/sqlite.php

https://github.com/lxcenter/hypervm
PHP | 635 lines | 443 code | 164 blank | 28 comment | 119 complexity | a90b65df69de647a6337128c8474633c MD5 | raw file
  1. <?php
  2. class Sqlite {
  3. private $__sqtable;
  4. static $__database = NULL;
  5. private $__column_type;
  6. function __construct($readserver, $table, $force = false)
  7. {
  8. global $gbl, $sgbl, $ghtml;
  9. $name = $sgbl->__var_program_name;
  10. $this->__sqtable = $table;
  11. $this->__readserver = 'localhost';
  12. $readserver = $this->__readserver;
  13. $fdbvar = "__fdb_{$this->__readserver}";
  14. if (!isset($gbl->$fdbvar) || $force) {
  15. if (is_running_secondary()) {
  16. throw new lxexception("this_is_a_running_secondary_master", '', "");
  17. }
  18. $user = $sgbl->__var_admin_user;
  19. $db = $sgbl->__var_dbf;
  20. $pass = getAdminDbPass();
  21. if ($sgbl->__var_database_type === 'mysql') {
  22. $gbl->$fdbvar = mysql_connect($readserver, $user, $pass);
  23. mysql_select_db($db);
  24. self::$__database = 'mysql';
  25. } else if ($sgbl->__var_database_type === 'mssql') {
  26. //print("$user, $pass <br> \n");
  27. //$gbl->$fdbvar = mssql_connect('\\.\pipe\MSSQL$LXLABS\sql\query');
  28. $gbl->$fdbvar = mssql_pconnect("$readserver,$sgbl->__var_mssqlport");
  29. mssql_select_db($db);
  30. self::$__database = 'mssql';
  31. } else {
  32. $gbl->$fdbvar = new PDO("sqlite:$db");
  33. self::$__database = 'sqlite';
  34. }
  35. }
  36. if (!$gbl->$fdbvar) {
  37. print("Could not connect to Mysql server... <br> ");
  38. //rl_exec_get("localhost", "localhost", "restart_mysql", null);
  39. //sleep(8);
  40. //print("<script> window.location.reload(false); </script>");
  41. exit;
  42. }
  43. if (!$gbl->$fdbvar) {
  44. die("could not Open Database Connection.... Exiting... <br> \n\n");
  45. }
  46. }
  47. function reconnect()
  48. {
  49. global $gbl, $sgbl, $login, $ghtml;
  50. $this->__readserver = 'localhost';
  51. $user = $sgbl->__var_admin_user;
  52. $db = $sgbl->__var_dbf;
  53. $pass = getAdminDbPass();
  54. $readserver = $this->__readserver;
  55. $fdbvar = "__fdb_" . $this->__readserver;
  56. log_log("database_reconnect", "Reconnecting again");
  57. if ($sgbl->__var_database_type === 'mysql') {
  58. $gbl->$fdbvar = mysql_connect($readserver, $user, $pass);
  59. mysql_select_db($db);
  60. self::$__database = 'mysql';
  61. } else if ($sgbl->__var_database_type === 'mssql') {
  62. //print("$user, $pass <br> \n");
  63. //$gbl->$fdbvar = mssql_connect('\\.\pipe\MSSQL$LXLABS\sql\query');
  64. $gbl->$fdbvar = mssql_pconnect("$readserver,$sgbl->__var_mssqlport");
  65. mssql_select_db($db);
  66. self::$__database = 'mssql';
  67. } else {
  68. $gbl->$fdbvar = new PDO("sqlite:$db");
  69. self::$__database = 'sqlite';
  70. }
  71. }
  72. final function isLocalhost($var = "__readserver")
  73. {
  74. global $gbl, $sgbl, $login, $ghtml;
  75. if (isset($this->$var) && $this->$var && $this->$var != "localhost") {
  76. return false;
  77. }
  78. return true;
  79. }
  80. function rawQuery($string)
  81. {
  82. $ret = $this->rl_query($string);
  83. return $ret;
  84. }
  85. function setPassword($newp)
  86. {
  87. return $this->rawQuery("set password=Password('$newp');");
  88. }
  89. function database_query($res, $string)
  90. {
  91. //log_log("dbquery", $string);
  92. if (self::$__database == 'mysql') {
  93. //print($string . "\n");
  94. $res = mysql_query($string, $res);
  95. if (!$res) {
  96. dprint("Mysql connection broken. Reconnecting..\n");
  97. debugBacktrace();
  98. $this->reconnect();
  99. $res = mysql_query($string, $res);
  100. }
  101. dprint(mysql_error());
  102. return $res;
  103. } else if (self::$__database == "mssql") {
  104. return mssql_query($string, $res);
  105. } else {
  106. //return $res->query($string);
  107. $st = $res->prepare($string);
  108. if ($st) {
  109. $v = $st->execute();
  110. } else {
  111. dprint($string);
  112. dprintr($res->errorInfo());
  113. }
  114. return $st;
  115. }
  116. }
  117. function database_fetch_array($query)
  118. {
  119. if (self::$__database == 'mysql') {
  120. return mysql_fetch_array($query, MYSQL_ASSOC);
  121. } else if (self::$__database === 'mssql') {
  122. return mssql_fetch_array($query, MSSQL_ASSOC);
  123. } else {
  124. return $query->fetch(PDO::FETCH_ASSOC);
  125. }
  126. }
  127. static function close()
  128. {
  129. global $gbl, $sgbl, $login, $ghtml;
  130. $fdbvar = "__fdb_" . $this->__readserver;
  131. if (self::$__database == 'mysql') {
  132. //mysql_close($gbl->$fdbvar);
  133. } else if (self::$__database == 'mssql') {
  134. //mssql_close($gbl->$fdbvar);
  135. } else {
  136. }
  137. $gbl->$fdbvar = NULL;
  138. }
  139. function rl_query($string)
  140. {
  141. global $gbl, $sgbl, $login, $ghtml;
  142. $fdbvar = "__fdb_{$this->__readserver}";
  143. $query = $this->database_query($gbl->$fdbvar, $string);
  144. if (!$query) {
  145. return 0;
  146. }
  147. if (!(is_resource($query) || is_object($query))) {
  148. return 0;
  149. }
  150. //dprintr($select . ' ' . $string);
  151. $fulresult = null;
  152. while ($result = $this->database_fetch_array($query)) {
  153. if (isset($result['nname']) && $result['nname'] === '__dummy__dummy__') {
  154. continue;
  155. }
  156. if (isset($result['realpass'])) {
  157. $value = $result['realpass'];
  158. if (csb($value, '__lxen:')) {
  159. $value = base64_decode(strfrom($value, "__lxen:"));
  160. }
  161. $result['realpass'] = $value;
  162. }
  163. $fulresult[] = $result;
  164. }
  165. return $fulresult;
  166. }
  167. function getRowsGeneric($string, $list = null)
  168. {
  169. $ret = null;
  170. if ($list) {
  171. $select = implode(",", $list);
  172. } else {
  173. $select = "*";
  174. }
  175. $query = "select $select from $this->__sqtable $string;";
  176. $fulresult = $this->rl_query($query);
  177. /// The ser varialbles are now handled in the setfromarraa, and this saves us a lot time.
  178. return $fulresult;
  179. foreach((array) $fulresult as $result) {
  180. foreach($result as $key => $value) {
  181. if (!strncmp($key, "ser_", 4)) {
  182. $key = substr($key, 4);
  183. if ($value) {
  184. $res[$key] = unserialize(base64_decode($value));
  185. //$res[$key] = unserialize($value);
  186. if (!$res[$key] && strlen($value) > 30) {
  187. log_database("Unserialize failed for $value");
  188. }
  189. } else {
  190. //dprint("Serialized Value for $key Null <br> ", 2);
  191. $res[$key] = NULL;
  192. }
  193. } else {
  194. $res[$key] = $value;
  195. }
  196. }
  197. $ret[] = $res;
  198. }
  199. return $ret;
  200. }
  201. function getClass()
  202. {
  203. return 'sqlite';
  204. }
  205. function existInTable($var, $value)
  206. {
  207. $result = $this->getRowsWhere("$var = '$value'");
  208. if ($result) {
  209. return true;
  210. }
  211. return false;
  212. }
  213. function getRowsWhere($string, $list = null)
  214. {
  215. return $this->getRowsGeneric("where " . $string, $list);
  216. }
  217. function getRowsOr($field1, $value1, $field2, $value2)
  218. {
  219. return $this->getRowsWhere("$field1 = '$value1' or $field2 = '$value2'");
  220. }
  221. function getRowAnd($field1, $value1,$field2,$value2)
  222. {
  223. return $this->getRowsWhere("$field1 = '$value1' and $field2='$value2'");
  224. }
  225. function getRowsNot($field, $notval)
  226. {
  227. return $this->getRowsWhere("$field != '$notval'");
  228. }
  229. function getRows($field, $value)
  230. {
  231. return $this->getRowsWhere("$field = '$value'");
  232. }
  233. function getTable($list = null)
  234. {
  235. return $this->getRowsGeneric("", $list);
  236. }
  237. function getColumnTypes()
  238. {
  239. //return getDbvariable("fieldvar", $this->__sqtable);
  240. global $gbl, $sgbl, $login, $ghtml;
  241. $fdbvar = "__fdb_" . $this->__readserver;
  242. if (!$this->__column_type) {
  243. if ($sgbl->__var_database_type === 'mysql') {
  244. $result = mysql_query("SHOW COLUMNS FROM $this->__sqtable", $gbl->$fdbvar);
  245. if (!$result) {
  246. dprint("Mysql connection broken. Reconnecting..\n");
  247. $this->reconnect();
  248. $result = mysql_query("SHOW COLUMNS FROM $this->__sqtable", $gbl->$fdbvar);
  249. }
  250. } else if ($sgbl->__var_database_type === 'mssql') {
  251. $result = mssql_query("sp_columns $this->__sqtable", $gbl->$fdbvar);
  252. } else {
  253. $f = $gbl->$fdbvar;
  254. $result = $f->prepare("select * from $this->__sqtable where nname = '__dummy__dummy__' ");
  255. if ($result) {
  256. $result->execute();
  257. }
  258. }
  259. if (!$result) {
  260. return null;
  261. }
  262. if ($sgbl->__var_database_type === 'mysql') {
  263. while(($row = mysql_fetch_assoc($result))) {
  264. $res[$row['Field']] = $row['Field'];
  265. }
  266. } else if ($sgbl->__var_database_type === 'mssql') {
  267. while(($row = mssql_fetch_assoc($result))) {
  268. $res[$row['COLUMN_NAME']] = $row['COLUMN_NAME'];
  269. }
  270. } else {
  271. $row = $result->fetch(PDO::FETCH_ASSOC);
  272. if (!$row) {
  273. return null;
  274. }
  275. foreach($row as $k => $v) {
  276. $res[$k] = $k;
  277. }
  278. }
  279. $this->__column_type = $res;
  280. //Fucking Buggy.
  281. //$this->__column_type = mssql_fetch_column_types($this->__sqtable, self::$__fdb);
  282. }
  283. return $this->__column_type;
  284. }
  285. function escapeBack($key, $string)
  286. {
  287. if (!csb($key, "text_")) {
  288. return $string;
  289. }
  290. return $string;
  291. }
  292. function createQueryStringAdd($array)
  293. {
  294. $string = " ( ";
  295. $result = $this->getColumnTypes();
  296. foreach($result as $key => $val) {
  297. $string = $string . " $key,";
  298. }
  299. $string = preg_replace("/,$/i", "", $string);
  300. $string = $string . ") values(" ;
  301. foreach($result as $key => $val) {
  302. if ($key === 'realpass') {
  303. $rp = $array[$key];
  304. $rp = base64_encode($rp);
  305. $rp = "__lxen:$rp";
  306. $string = "$string '$rp',";
  307. continue;
  308. }
  309. $string = "$string '{$this->escapeBack($key, $array[$key])}',";
  310. }
  311. $string = preg_replace("/,$/i", "", $string);
  312. $string = $string . " )";
  313. return $string;
  314. }
  315. function createQueryStringUpdate($array)
  316. {
  317. global $gbl, $sgbl, $login, $ghtml;
  318. $string = "";
  319. $result = $this->getColumnTypes();
  320. foreach($result as $key => $val) {
  321. if (isset($sgbl->__var_collectquota_run) && $sgbl->__var_collectquota_run) {
  322. if (!csb($key, "priv_") && !csb($key,'used_') && !csb($key, "nname") && !csb($key, "status") && !csb($key, "state") && !csb($key, "cpstatus")) {
  323. continue;
  324. }
  325. }
  326. if ($key === 'realpass') {
  327. $rp = $array[$key];
  328. if (!csb($rp, "__lxen:")) {
  329. $rp = base64_encode($rp);
  330. $rp = "__lxen:$rp";
  331. }
  332. $string[] = "$key = '$rp'";
  333. continue;
  334. }
  335. $string[] = "$key = '{$this->escapeBack($key, $array[$key])}'";
  336. }
  337. $string = implode(",", $string);
  338. return $string;
  339. }
  340. function getCountWhere($query)
  341. {
  342. global $gbl, $sgbl, $login, $ghtml;
  343. $countres = $this->rawquery("select count(*) from $this->__sqtable where $query");
  344. if ($sgbl->__var_database_type === 'mysql') {
  345. $countres = $countres[0]['count(*)'];
  346. } else if ($sgbl->__var_database_type === 'mssql') {
  347. $countres = $countres[0]['computed'];
  348. } else {
  349. $countres = $countres[0]['count(*)'];
  350. }
  351. return $countres;
  352. }
  353. function getToArray($object)
  354. {
  355. $col = $this->getColumnTypes();
  356. //dprint_r($array);
  357. foreach($col as $key => $val) {
  358. if (csb($key, "coma_")) {
  359. $cvar = substr($key, 5);
  360. $value = $object->$cvar;
  361. if (cse($key, "_list")) {
  362. $namelist = $value;
  363. } else {
  364. $namelist = get_namelist_from_objectlist($value);
  365. }
  366. $ret[$key] = implode(",", $namelist);
  367. dprint("in COma $key {$ret[$key]}<br> ");
  368. $ret[$key] = ",$ret[$key],";
  369. } else if (csb($key, "ser_")) {
  370. $cvar = substr($key, 4);
  371. $value = $object->$cvar;
  372. if ($value && isset($value->driverApp)) {
  373. unset($value->driverApp);
  374. }
  375. if (cse($key, "_a")) {
  376. if ($value) foreach($value as $kk => $vv) {
  377. unset($value[$kk]->__parent_o);
  378. }
  379. }
  380. $ret[$key] = base64_encode(serialize($object->$cvar));
  381. //$ret[$key] = serialize($object->$cvar);
  382. } else if (csb($key, "priv_q_") || csb($key, "used_q_")) {
  383. $qob = strtil($key, "_q_");
  384. $qkey = strfrom($key, "_q_");
  385. if ($object->get__table() === 'uuser') {
  386. }
  387. $ret[$key] = $object->$qob->$qkey;
  388. } else {
  389. if (!isset($object->$key)) {
  390. $object->$key = null;
  391. }
  392. if (csb($key, "text_")) {
  393. $string = str_replace("\\", '\\\\', $object->$key);
  394. } else {
  395. $string = $object->$key;
  396. }
  397. $ret[$key] = str_replace("'", "\'", $string);
  398. //$ret[$key] = $object->$key;
  399. }
  400. }
  401. return $ret;
  402. }
  403. function setRowObject($nname, $value, $object)
  404. {
  405. $array = $this->getToArray($object);
  406. $this->setRow($nname, $value, $array);
  407. }
  408. function addRowObject($object)
  409. {
  410. $array = $this->getToArray($object);
  411. $this->addRow($array);
  412. }
  413. function setRow($nname, $value, $array)
  414. {
  415. global $gbl, $sgbl, $login, $ghtml;
  416. $fdbvar = "__fdb_" . $this->__readserver;
  417. if (!$this->isLocalhost()) {
  418. print("Major Error\n");
  419. exit;
  420. }
  421. $string = $this->createQueryStringUpdate($array);
  422. $update = "update $this->__sqtable set $string where $nname= '$value'";
  423. if ($array['nname'] === 'boxtrapper.com') {
  424. //dprint($update);
  425. //exit;
  426. }
  427. if (!($upd = $this->database_query($gbl->$fdbvar, $update)))
  428. log_database("DbError: Update Failed for $update");
  429. else {
  430. if ($this->__sqtable !== 'utmp') {
  431. dprint("Success: updated " .$this->__sqtable . " for " . $array['nname'] . "\n", 1);
  432. }
  433. }
  434. }
  435. function addRow($array)
  436. {
  437. global $gbl, $sgbl, $login, $ghtml;
  438. $fdbvar = "__fdb_" . $this->__readserver;
  439. if (!$this->isLocalhost()) {
  440. print("Major Error\n");
  441. exit;
  442. }
  443. $string = $this->createQueryStringAdd($array);
  444. $insert = "insert into $this->__sqtable $string ;";
  445. //dprint($insert, 2);
  446. if ($ins = $this->database_query($gbl->$fdbvar, $insert)) {
  447. dprint("Record inserted in $this->__sqtable for {$array['nname']}\n", 1);
  448. //log_message("insert into {$this->__sqtable}:{$array['nname']} :::: $insert");
  449. } else {
  450. log_database("DbError: Insert Failed for {$this->__sqtable}:{$array['nname']}");
  451. log_bdatabase("DbError: Insert Failed for {$this->__sqtable}:{$array['nname']} $insert");
  452. // Not imporant... I think.. This happens mostly when they try add something twice. Let us just ignore the second time, but log it properly.
  453. if ($sgbl->dbg > 0) {
  454. //throw new lxException("db_add_failed", "{$this->__sqtable}:{$array['nname']}");
  455. }
  456. return true;
  457. }
  458. }
  459. function delRow($nname, $value)
  460. {
  461. global $gbl, $sgbl, $login, $ghtml;
  462. $fdbvar = "__fdb_" . $this->__readserver;
  463. $delete = "delete from $this->__sqtable where $nname = '$value'";
  464. $delresult = $this->database_query($gbl->$fdbvar, $delete);
  465. if(!$delresult) {
  466. log_database("DbError: delete Failed for $delete");
  467. } else {
  468. dprint("Record deleted from $this->__sqtable for $nname <br>.");
  469. }
  470. }
  471. }