PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wwwfiles/functions.php

https://github.com/annulen/MolDB
PHP | 501 lines | 445 code | 41 blank | 15 comment | 108 complexity | eb3331861070ee67c637cbb7cebda4e1 MD5 | raw file
  1. <?php
  2. // functions.php Norbert Haider, University of Vienna, 2009-2010
  3. // a collection of common functions for MolDB5R, last change: 2010-06-09
  4. function getmicrotime() {
  5. list($usec, $sec) = explode(" ", microtime());
  6. return ((float)$usec + (float)$sec);
  7. }
  8. function getostype() {
  9. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  10. $ostype = 2; // Windows
  11. } else {
  12. $ostype = 1; // Linux
  13. }
  14. return $ostype;
  15. }
  16. function show_header($myname,$dbstr) {
  17. global $enablereactions;
  18. $item = array();
  19. $item[0][0] = "index.php"; $item[0][1] = "Home";
  20. $item[1][0] = "moldblst.php"; $item[1][1] = "Browse";
  21. $item[2][0] = "moldbtxt.php"; $item[2][1] = "Text Search";
  22. $item[3][0] = "moldbfg.php"; $item[3][1] = "Functional Group Search";
  23. $item[4][0] = "moldbsss.php"; $item[4][1] = "Structure Search";
  24. if ($enablereactions == "y") {
  25. $item[5][0] = "moldbrss.php"; $item[5][1] = "Reaction Search";
  26. $nitems = 6;
  27. } else { $nitems = 5; }
  28. for ($i = 0; $i < $nitems; $i++) {
  29. $url = $item[$i][0];
  30. $label = $item[$i][1];
  31. //echo "$i: $url : $label <br>\n";
  32. $pos = strpos($myname,$url);
  33. if ($pos !== false) {
  34. echo "[$label]";
  35. } else {
  36. echo "[<a href=\"${url}?db=${dbstr}\">$label</a>]";
  37. }
  38. }
  39. echo "<br /><hr />\n";
  40. echo "<small>selected data collection: $dbstr</small><p />\n";
  41. }
  42. function filterthroughcmmm($input,$commandline) {
  43. global $socket;
  44. $input = $commandline . "\n" . $input . "####" . "\n";
  45. socket_write ($socket, $input, strlen ($input));
  46. $output = '';
  47. $a = '';
  48. while (($a = socket_read($socket, 250, PHP_NORMAL_READ)) && (strpos($a,'####') === false)) {
  49. if (strpos($a,'####') === false) {
  50. $output = $output . $a;
  51. }
  52. }
  53. return $output;
  54. }
  55. function filterthroughcmd($input, $commandLine) {
  56. $pipe = popen("echo \"$input\"|$commandLine" , 'r');
  57. if (!$pipe) {
  58. print "pipe failed.";
  59. return "";
  60. }
  61. $output = '';
  62. while(!feof($pipe)) {
  63. $output .= fread($pipe, 1024);
  64. }
  65. pclose($pipe);
  66. return $output;
  67. }
  68. function filterthroughcmd2($input, $commandLine) { // Windows version
  69. global $tempdir; // if not set, use system temporary directory
  70. $tempdir = realpath($tempdir);
  71. $tmpfname = tempnam($tempdir, "mdb");
  72. #$tmpfname = tempnam(realpath("C:/temp/"), "mdb"); // for testing (directory must exist!)
  73. $tfhandle = fopen($tmpfname, "wb");
  74. $myinput = str_replace("\n","",$input);
  75. $myinput = str_replace("\\\$","\$",$myinput);
  76. $inputlines = explode("\r",$myinput);
  77. $newinput = implode("\r\n",$inputlines);
  78. fwrite($tfhandle, $newinput);
  79. fclose($tfhandle);
  80. $output = `type $tmpfname | $commandLine `;
  81. #$output = `$commandLine < $tmpfname |`;
  82. unlink($tmpfname);
  83. return $output;
  84. }
  85. function showHit($id,$s) {
  86. global $bitmapURLdir;
  87. global $molstructable;
  88. global $moldatatable;
  89. global $digits;
  90. global $subdirdigits;
  91. global $db_id;
  92. global $pic2dtable;
  93. $result2 = mysql_query("SELECT mol_name FROM $moldatatable WHERE mol_id = $id")
  94. or die("Query failed! (1a)");
  95. while ($line2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
  96. $txt = $line2["mol_name"];
  97. }
  98. mysql_free_result($result2);
  99. echo "<tr>\n<td bgcolor=\"#EEEEEE\">\n";
  100. print "<a href=\"details.php?mol=${id}&db=${db_id}\" target=\"blank\">$db_id:$id</a></td>\n";
  101. echo "<td bgcolor=\"#EEEEEE\"> <b>$txt</b>";
  102. if ($s != '') {
  103. echo " $s";
  104. }
  105. echo "</td>\n</tr>\n";
  106. // for faster display, we should have bitmap files (GIF or PNG) of the 2D structures
  107. // instead of invoking the JME applet:
  108. $qstr = "SELECT status FROM $pic2dtable WHERE mol_id = $id";
  109. $result2 = mysql_query($qstr)
  110. or die("Query failed! (pic2d)");
  111. while ($line2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
  112. $status = $line2["status"];
  113. }
  114. mysql_free_result($result2);
  115. if ($status != 1) { $usebmp = false; } else { $usebmp = true; }
  116. echo "<tr>\n<td colspan=\"2\">\n";
  117. if ((isset($bitmapURLdir)) && ($bitmapURLdir != "") && ($usebmp == true)) {
  118. while (strlen($id) < $digits) { $id = "0" . $id; }
  119. $subdir = '';
  120. if ($subdirdigits > 0) { $subdir = substr($id,0,$subdirdigits) . '/'; }
  121. print "<img src=\"${bitmapURLdir}/${db_id}/${subdir}${id}.png\" alt=\"hit structure\">\n";
  122. } else {
  123. // if no bitmaps are available, we must invoking another instance of JME
  124. // in "depict" mode for structure display of each hit
  125. $qstr = "SELECT struc FROM $molstructable WHERE mol_id = $id";
  126. $result3 = mysql_query($qstr) or die("Query failed! (struc)");
  127. while ($line3 = mysql_fetch_array($result3, MYSQL_ASSOC)) {
  128. $molstruc = $line3["struc"];
  129. }
  130. mysql_free_result($result3);
  131. // JME needs MDL molfiles with the "|" character instead of linebreaks
  132. $jmehitmol = strtr($molstruc,"\n","|");
  133. echo "<applet code=\"JME.class\" archive=\"JME.jar\" \n";
  134. echo "width=\"250\" height=\"120\">";
  135. echo "<param name=\"options\" value=\"depict\"> \n";
  136. echo "<param name=\"mol\" value=\"$jmehitmol\">\n";
  137. echo "</applet>\n";
  138. }
  139. echo "</td>\n</tr>\n";
  140. }
  141. function showHitRxn($id,$s) {
  142. global $rxnstructable;
  143. global $rxndatatable;
  144. global $db_id;
  145. $result2 = mysql_query("SELECT rxn_name FROM $rxndatatable WHERE rxn_id = $id")
  146. or die("Query failed! (showHitRxn #1)");
  147. while ($line2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
  148. $txt = $line2["rxn_name"];
  149. }
  150. mysql_free_result($result2);
  151. echo "<tr>\n<td bgcolor=\"#EEEEEE\">\n";
  152. print "<a href=\"details.php?rxn=${id}&db=${db_id}\" target=\"blank\">$db_id:$id</a></td>\n";
  153. echo "<td bgcolor=\"#EEEEEE\"> <b>$txt</b>";
  154. if ($s != '') {
  155. echo " $s";
  156. }
  157. echo "</td>\n</tr>\n";
  158. echo "<tr>\n<td colspan=\"2\">\n";
  159. // use JME in "depict" mode for reaction display
  160. $qstr = "SELECT struc FROM $rxnstructable WHERE rxn_id = $id";
  161. $result3 = mysql_query($qstr) or die("Query failed! (struc)");
  162. while ($line3 = mysql_fetch_array($result3, MYSQL_ASSOC)) {
  163. $molstruc = $line3["struc"];
  164. }
  165. mysql_free_result($result3);
  166. // remove reaction map labels
  167. $molstruc = strip_labels($molstruc);
  168. // JME needs MDL molfiles with the "|" character instead of linebreaks
  169. $jmehitmol = strtr($molstruc,"\n","|");
  170. echo "<applet code=\"JME.class\" archive=\"JME.jar\" \n";
  171. echo "width=\"450\" height=\"120\">";
  172. echo "<param name=\"options\" value=\"depict\"> \n";
  173. echo "<param name=\"mol\" value=\"$jmehitmol\">\n";
  174. echo "</applet>\n";
  175. echo "</td>\n</tr>\n";
  176. }
  177. function check_db($id) {
  178. global $metatable;
  179. $db_id = -1;
  180. if (is_numeric($id)) {
  181. $result = mysql_query("SELECT db_id, name FROM $metatable WHERE (db_id = $id) AND (access > 0)")
  182. or die("Query failed! (1)");
  183. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  184. $db_id = $line["db_id"];
  185. }
  186. mysql_free_result($result);
  187. }
  188. if ($db_id == -1) { // check if there is any data collection at all
  189. $result = mysql_query("SELECT COUNT(db_id) AS dbcount FROM $metatable")
  190. or die("Query failed! (2)");
  191. $dbcount = 0;
  192. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  193. $dbcount = $line["dbcount"];
  194. }
  195. mysql_free_result($result);
  196. if ($dbcount == 0) { $db_id = 0; }
  197. }
  198. return($db_id);
  199. }
  200. function get_numdb() {
  201. global $metatable;
  202. $numdb = 0;
  203. $result1 = mysql_query("SELECT COUNT(db_id) AS numdb FROM $metatable")
  204. or die("Query failed! (get_numdb)");
  205. $line1 = mysql_fetch_row($result1);
  206. mysql_free_result($result1);
  207. $numdb = $line1[0];
  208. return($numdb);
  209. }
  210. function exist_db($db_id) {
  211. global $metatable;
  212. $numdb = 0;
  213. $result1 = mysql_query("SELECT COUNT(db_id) AS numdb FROM $metatable WHERE db_id = $db_id")
  214. or die("Query failed! (get_numdb)");
  215. $line1 = mysql_fetch_row($result1);
  216. mysql_free_result($result1);
  217. $numdb = $line1[0];
  218. if ($numdb > 0) { $result = TRUE; } else { $result = FALSE; }
  219. return($result);
  220. }
  221. function get_highestdbid() {
  222. global $metatable;
  223. $dbmax = 0;
  224. $result1 = mysql_query("SELECT MAX(db_id) AS dbmax FROM $metatable")
  225. or die("Query failed! (get_highestdbid)");
  226. $line1 = mysql_fetch_row($result1);
  227. $dbmax = $line1[0];
  228. mysql_free_result($result1);
  229. return($dbmax);
  230. }
  231. function get_lowestdbid() {
  232. global $metatable;
  233. $dbmin = 0;
  234. $result1 = mysql_query("SELECT MIN(db_id) AS dbmin FROM $metatable")
  235. or die("Query failed! (get_lowestdbid)");
  236. $line1 = mysql_fetch_row($result1);
  237. $dbmin = $line1[0];
  238. mysql_free_result($result1);
  239. return($dbmin);
  240. }
  241. function get_dbproperties($db_id) {
  242. global $metatable;
  243. $prop = array();
  244. $result1 = mysql_query("SELECT db_id, type, access, name, description, usemem,
  245. memstatus, digits, subdirdigits, trustedIP FROM $metatable WHERE (db_id = $db_id)")
  246. or die("Query failed! (1a)");
  247. while ($line1 = mysql_fetch_assoc($result1)) {
  248. $prop['db_id'] = $line1['db_id'];
  249. $prop['type'] = $line1['type'];
  250. $prop['access'] = $line1['access'];
  251. $prop['name'] = $line1['name'];
  252. $prop['description'] = $line1['description'];
  253. $prop['usemem'] = $line1['usemem'];
  254. $prop['memstatus'] = $line1['memstatus'];
  255. $prop['digits'] = $line1['digits'];
  256. $prop['subdirdigits'] = $line1['subdirdigits'];
  257. $prop['trustedIP'] = $line1['trustedIP'];
  258. }
  259. mysql_free_result($result1);
  260. return($prop);
  261. }
  262. function mfreformat($instring) {
  263. $outstring = "";
  264. $firstnum = 1;
  265. $sub = 0;
  266. $instring = trim($instring);
  267. for ($l = 0; $l < strlen($instring); $l++) {
  268. $c = substr($instring,$l,1);
  269. if (is_numeric($c)) {
  270. if (($firstnum == 0) && ($sub == 0)) {
  271. $outstring = $outstring . "<sub>";
  272. $sub = 1;
  273. }
  274. $outstring = $outstring . $c;
  275. } else {
  276. $firstnum = 0;
  277. if ($c == ".") { $firstnum = 1; }
  278. if ($sub == 1) {
  279. $outstring = $outstring . "</sub>";
  280. $sub = 0;
  281. }
  282. $outstring = $outstring . $c;
  283. }
  284. } // for
  285. if ($sub == 1) {
  286. $outstring = $outstring . "</sub>";
  287. }
  288. return($outstring);
  289. }
  290. function urlreformat($instring) {
  291. $outstring = "";
  292. $instring = trim($instring);
  293. $urlarr = explode("|",$instring);
  294. $url_addr = $urlarr[0];
  295. if (count($urlarr) > 1) {
  296. $url_label = $urlarr[1];
  297. } else {
  298. $url_label = $url_addr;
  299. }
  300. $url_label = trim($url_label);
  301. if (strlen($url_label) == 0) {
  302. $url_label = $url_addr;
  303. }
  304. $outstring = "<a href=\"" . $url_addr . "\">" . $url_label . "</a>";
  305. return($outstring);
  306. }
  307. function is_trustedIP($ip) {
  308. global $trustedIP;
  309. $IPlist = str_replace(";",",",$trustedIP);
  310. $IPlist = preg_replace("/,\ +/",",",$IPlist);
  311. $IParray = explode(",",$IPlist);
  312. $result = false;
  313. foreach($IParray as $value) {
  314. $value = trim($value);
  315. $pos = strpos($ip,$value);
  316. if ($pos !== false) { $result = true; }
  317. }
  318. return($result);
  319. }
  320. function is_db_trustedIP($db_id,$ip) {
  321. global $metatable;
  322. $result = false;
  323. if (is_numeric($db_id)) {
  324. $res = mysql_query("SELECT db_id, trustedIP FROM $metatable WHERE (db_id = $db_id)")
  325. or die("Query failed! (1)");
  326. while ($line = mysql_fetch_array($res, MYSQL_ASSOC)) {
  327. $trustedIP = $line["trustedIP"];
  328. }
  329. mysql_free_result($res);
  330. $IPlist = str_replace(";",",",$trustedIP);
  331. $IPlist = preg_replace("/,\ +/",",",$IPlist);
  332. if (strlen($IPlist)>0) {
  333. $IParray = explode(",",$IPlist);
  334. $result = false;
  335. foreach($IParray as $value) {
  336. $value = trim($value);
  337. $pos = strpos($ip,$value);
  338. if ($pos !== false) { $result = true; }
  339. }
  340. }
  341. }
  342. return($result);
  343. }
  344. function clean_fieldstr($instr) {
  345. $outstr = "";
  346. $allowedstr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
  347. $instr = trim($instr);
  348. #$instr = str_replace(".","_",$instr);
  349. if ($instr != "") {
  350. $c = "";
  351. $pos = false;
  352. for ($i = 0; $i < strlen($instr); $i++) {
  353. $c = substr($instr,$i,1);
  354. $pos = strpos($allowedstr,$c);
  355. if ($pos !== false) {
  356. $outstr .= $c;
  357. }
  358. }
  359. }
  360. return($outstr);
  361. }
  362. function getfieldprop($comment) {
  363. $prop = array();
  364. $pos = strpos($comment, ">>>>");
  365. if ($pos !== false) {
  366. if ($pos == 0) {
  367. $comment = str_replace(">>>>","",$comment);
  368. $acomment = explode("<",$comment);
  369. $label = $acomment[0];
  370. $prop["label"] = $label;
  371. $format = 1;
  372. $nformat = $acomment[1];
  373. if ($nformat == 0) { $format = 0; }
  374. if ($nformat == 1) { $format = 1; }
  375. if ($nformat == 2) { $format = 2; }
  376. if ($nformat == 3) { $format = 3; }
  377. $prop["format"] = $format;
  378. $sdflabel = $acomment[2];
  379. $prop["sdflabel"] = $sdflabel;
  380. $searchmode = $acomment[3];
  381. if ($searchmode != 1) { $searchmode = 0; }
  382. $prop["searchmode"] = $searchmode;
  383. }
  384. }
  385. return $prop;
  386. }
  387. function is_stringtype($columntype) {
  388. $res = FALSE;
  389. $columntype = strtoupper($columntype);
  390. if (strpos($columntype,"CHAR") !== FALSE) { $res = TRUE; }
  391. if (strpos($columntype,"TEXT") !== FALSE) { $res = TRUE; }
  392. if (strpos($columntype,"ENUM") !== FALSE) { $res = TRUE; }
  393. if (strpos($columntype,"SET") !== FALSE) { $res = TRUE; }
  394. if (strpos($columntype,"VARBINARY") !== FALSE) { $res = TRUE; }
  395. return $res;
  396. }
  397. function is_validmol($mol) {
  398. $res = FALSE;
  399. if ((strpos($mol,'M END') > 40) &&
  400. (strpos($mol,'V2000') > 30)) { $res = TRUE; } // rather simple, for now
  401. return $res;
  402. }
  403. function strip_labels($myrxn) {
  404. //$myrxn = str_replace("\r\n","\n",$myrxn);
  405. $line_arr = array();
  406. $line_arr = explode("\n",$myrxn);
  407. $myrxn = "";
  408. foreach ($line_arr as $line) {
  409. if ((strlen($line) > 68) && (strpos($line,"0 0",63) !== FALSE)) {
  410. $line = substr_replace($line," 0 0 0",60,9);
  411. }
  412. $myrxn .= $line . "\n";
  413. }
  414. return($myrxn);
  415. }
  416. function mk_fpqstr($colname,$fplist) {
  417. $result = "";
  418. $fpa = explode(",",$fplist);
  419. $n_el = count($fpa);
  420. for ($i = 0; $i < $n_el; $i++) {
  421. $fpval = $fpa[$i];
  422. $fpnum = $i + 1;
  423. while (strlen($fpnum) < 2) { $fpnum = "0" . $fpnum; }
  424. $fpcol = $colname . $fpnum;
  425. if (is_numeric($fpval)) {
  426. if ($fpval > 0) {
  427. if (strlen($result) > 0) { $result .= " AND"; }
  428. $result .= " ($fpcol & $fpval = $fpval)";
  429. }
  430. }
  431. }
  432. return($result);
  433. }
  434. function debug_output($msg) {
  435. global $debug;
  436. if (($debug & 1) == 1) {
  437. $begin = "<!-- ";
  438. $end = " --!>";
  439. } else {
  440. $begin = "<pre>\n";
  441. $end = "</pre>\n";
  442. }
  443. echo "$begin";
  444. echo "$msg";
  445. echo "$end";
  446. }
  447. function config_quickcheck() {
  448. global $database;
  449. global $ro_user;
  450. global $ro_password;
  451. $result = 0;
  452. if ((!isset($database)) || (strlen($database) == 0)) { $result = 1; }
  453. if ((!isset($ro_user)) || (strlen($ro_user) == 0)) { $result = 1; }
  454. if ((!isset($ro_password)) || (strlen($ro_password) == 0)) { $result = 1; }
  455. if ($result > 0) {
  456. echo "<h3>Attention! Missing, invalid, or unreadable configuration file!</h3>\n";
  457. }
  458. return($result);
  459. }
  460. ?>