PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/mysql.php

https://bitbucket.org/fieldsofview/athena
PHP | 1034 lines | 925 code | 34 blank | 75 comment | 110 complexity | 6f35fdb830c27a8d97a2e756f65ea939 MD5 | raw file
  1. <?php
  2. function database_query($query)
  3. {
  4. return mysql_query($query);
  5. }
  6. function database_fetch_array($res)
  7. {
  8. return mysql_fetch_array($res);
  9. }
  10. function database_num_rows($res)
  11. {
  12. return mysql_num_rows($res);
  13. }
  14. function database_error()
  15. {
  16. return mysql_error();
  17. }
  18. function escape_sql($str)
  19. {
  20. return mysql_real_escape_string($str);
  21. }
  22. function check_for_php_modules()
  23. {
  24. /**Checks for MySQL and cURL PHP modules, without which Athena won't work.*/
  25. $mysql=function_exists("mysql_connect");
  26. $curl=function_exists("curl_init");
  27. if(!$mysql or !$curl)
  28. {
  29. if(!$mysql)
  30. echo "You haven't installed PHP MySQL module.";
  31. if(!$curl)
  32. echo "You haven't installed PHP cURL module";
  33. return false;
  34. die;
  35. }
  36. return true;
  37. }
  38. check_for_php_modules();
  39. $db=mysql_connect("$hostname","$dbuser","$dbpw");
  40. $d=mysql_select_db("$dbname");
  41. $error=fopen("error.log","a");
  42. check_db_connectivity($db,$d);
  43. /* Function definitions */
  44. //-----------------------
  45. function check_db_connectivity($db,$d)
  46. {
  47. if(!$db)
  48. {
  49. echo "<script>window.location='notinstalled.php';</script>";
  50. }
  51. if(!$d)
  52. {
  53. echo "<script>window.location='notinstalled.php';</script>";
  54. }
  55. return true;
  56. }
  57. function validate($email,$pass)
  58. {
  59. /** Used to validate users when they login and whenever else required*/
  60. $email=mysql_real_escape_string($email);
  61. $pass=mysql_real_escape_string($pass);
  62. $res=mysql_query("select password('$pass');");
  63. $p1=mysql_fetch_array($res);
  64. $res=mysql_query("select password,uid from users where email='$email';");
  65. if(!$res)
  66. {
  67. fputs($error,mysql_error());
  68. return "0";
  69. }
  70. $p2=mysql_fetch_array($res);
  71. if($p1[0]==$p2[0])
  72. return "1";
  73. else return "0";
  74. }
  75. function check_file_write()
  76. {
  77. /*$f1=substr(sprintf("%o",fileperms("upload/")),-2);
  78. echo $f1;
  79. if($f1<77)
  80. {
  81. echo "you need to grant write to upload";
  82. die;
  83. }*/
  84. }
  85. function getemail($uid)
  86. {
  87. /**Returns the email (as a string) of the users with id as $uid. */
  88. $uid=mysql_real_escape_string($uid);
  89. $row=mysql_fetch_array(mysql_query("select email from users where uid='$uid';"));
  90. return $row[0];
  91. }
  92. function getuserid($email)
  93. {
  94. $email=mysql_real_escape_string($email);
  95. $row=mysql_fetch_array(mysql_query("select uid from users where email='$email';"));
  96. return $row[0];
  97. }
  98. function getname($email)
  99. {
  100. $email=mysql_real_escape_string($email);
  101. $row=mysql_fetch_array(mysql_query("select fname,sname from users where email='$email';"));
  102. return $row[0]." ".$row[1];
  103. }
  104. function getname_uid($uid)
  105. {
  106. $uid=mysql_real_escape_string($uid);
  107. $row=mysql_fetch_array(mysql_query("select fname,sname from users where uid='$uid';"));
  108. return $row[0]." ".$row[1];
  109. }
  110. function getuserdetails($email)
  111. {
  112. $email=mysql_real_escape_string($email);
  113. $row=mysql_fetch_array(mysql_query("select * from users where email='$email' or uid='$email';"));
  114. return $row;
  115. }
  116. function gettypes()
  117. {
  118. $res=mysql_query("select * from type;");
  119. return $res;
  120. }
  121. function getattr($tyid)
  122. {
  123. $tyid=mysql_real_escape_string($tyid);
  124. $res=mysql_query("select attrib from resource_attrib where tyid='$tyid';");
  125. //echo "select attrib from resource_attrib where tyid='$x';";
  126. if(!$res)
  127. fputs($error,mysql_error());
  128. return $res;
  129. }
  130. function gettypename($x)
  131. {
  132. $x=mysql_real_escape_string($x);
  133. $row=mysql_fetch_array(mysql_query("select description from type where tyid='$x';"));
  134. $name=str_replace("_"," ",$row[0]);
  135. return $name;
  136. }
  137. function gettypename_eid($x)
  138. {
  139. $x=mysql_real_escape_string($x);
  140. $row=mysql_fetch_array(mysql_query("select description from type natural join resource where eid='$x';"));
  141. $name=str_replace("_"," ",$row[0]);
  142. return $name;
  143. }
  144. /*function getresources($l,$p,$t)
  145. {
  146. $l=mysql_real_escape_string($l);
  147. $p=mysql_real_escape_string($p);
  148. $t=mysql_real_escape_string($t);
  149. $y=($p-1)*12;
  150. if($t=="")
  151. $query="select distinct eid,name,fname,sname,description,tyid,lost from (resource natural join type) natural join users where name like '$l%' order by name limit $y,12";
  152. else
  153. $query="select distinct eid,name,fname,sname,description,tyid,lost from (resource natural join type) natural join users where name like '$l%' and tyid='$t' order by name limit $y,10;";
  154. //$query="select * from browse_view limit 12;";
  155. $res=mysql_query($query);
  156. if(!$res)
  157. {
  158. fputs($error,mysql_error());
  159. die;
  160. }
  161. return $res;
  162. }
  163. function getbr_count($l,$t)
  164. {
  165. $l=mysql_real_escape_string($l);
  166. $t=mysql_real_escape_string($t);
  167. //$y=($p-1)*10;
  168. if($t=="")
  169. $query="select distinct eid,name,fname,sname,description,tyid,lost from (resource natural join type) natural join users where name like '$l%' order by name";
  170. else
  171. $query="select distinct eid,name,fname,sname,description,tyid,lost from (resource natural join type) natural join users where name like '$l%' and tyid='$t' order by name;";
  172. //$query="select * from browse_view limit 12;";
  173. $res=mysql_query($query);
  174. if(!$res)
  175. {
  176. fputs($error,mysql_error());
  177. die;
  178. }
  179. return $res;
  180. }*/
  181. function getresources($l,$p,$t)
  182. {
  183. $l=mysql_real_escape_string($l);
  184. $p=mysql_real_escape_string($p);
  185. $t=mysql_real_escape_string($t);
  186. $y=($p-1)*12;
  187. if($t=="")
  188. $query="select distinct eid,name,fname,sname,description,tyid,lost from (resource natural join type) natural join users where name like '$l%' order by name limit 12 offset $y";
  189. else
  190. $query="select distinct eid,name,fname,sname,description,tyid,lost from (resource natural join type) natural join users where name like '$l%' and tyid='$t' order by name limit 10 offset $y;";
  191. $res=mysql_query($query);
  192. if(!$res)
  193. {
  194. fputs($error,database_error());
  195. die;
  196. }
  197. return $res;
  198. }
  199. function getbr_count($l,$t)
  200. {
  201. $l=mysql_real_escape_string($l);
  202. $t=mysql_real_escape_string($t);
  203. //$y=($p-1)*10;
  204. if($t=="")
  205. $query="select distinct eid,name,fname,sname,description,tyid,lost from (resource natural join type) natural join users where name like '$l%' order by name";
  206. else
  207. $query="select distinct eid,name,fname,sname,description,tyid,lost from (resource natural join type) natural join users where name like '$l%' and tyid='$t' order by name;";
  208. $res=mysql_query($query);
  209. if(!$res)
  210. {
  211. fputs($error,database_error());
  212. die;
  213. }
  214. return $res;
  215. }
  216. function checkborrow($eid)
  217. {
  218. $eid=mysql_real_escape_string($eid);
  219. $res=mysql_query("select * from borrowed where eid='$eid';");
  220. if(mysql_num_rows($res)==0)
  221. {
  222. return "0";
  223. }
  224. else
  225. {
  226. $row=mysql_fetch_array($res);
  227. return $row[1];
  228. }
  229. }
  230. function borrowed($uid)
  231. {
  232. $uid=mysql_real_escape_string($uid);
  233. $res=mysql_query("select * from borrowed,resource where borrowed.uid='$uid' and resource.eid=borrowed.eid;");
  234. if(!$res)
  235. {
  236. fputs($error,mysql_error());
  237. die;
  238. }
  239. return $res;
  240. }
  241. function getresname($eid)
  242. {
  243. $eid=mysql_real_escape_string($eid);
  244. $res=mysql_query("select name from resource where eid='$eid';");
  245. if(!$res)
  246. {
  247. fputs($error,mysql_error());
  248. die;
  249. }
  250. $row=mysql_fetch_array($res);
  251. return stripslashes($row[0]);
  252. }
  253. function getmessages($uid)
  254. {
  255. $uid=mysql_real_escape_string($uid);
  256. $res=mysql_query("select * from message where to_uid='$uid' and delete_flag='1';");
  257. if(!$res)
  258. {
  259. fputs($error,mysql_error());
  260. die;
  261. }
  262. return $res;
  263. }
  264. function gettyid($eid)
  265. {
  266. $eid=mysql_real_escape_string($eid);
  267. $res=mysql_query("select tyid from resource where eid='$eid';");
  268. $row=mysql_fetch_array($res);
  269. return $row[0];
  270. }
  271. function gettypeid($t)
  272. {
  273. $t=mysql_real_escape_string($t);
  274. $res=mysql_query("select tyid from type where description='$t';");
  275. $row=mysql_fetch_array($res);
  276. return $row[0];
  277. }
  278. function searchresources($q,$by,$p,$t)
  279. {
  280. $q=mysql_real_escape_string($q);
  281. $by=mysql_real_escape_string($by);
  282. $p=mysql_real_escape_string($p);
  283. $cl = new SphinxClient ();
  284. $sql = "";
  285. $mode = SPH_MATCH_EXTENDED;
  286. $host = "localhost";
  287. $port = 9312;
  288. $groupby = "";
  289. $groupsort = "@group desc";
  290. $filter = "group_id";
  291. $filtervals = array();
  292. $distinct = "";
  293. $sortby = "";
  294. $limit = 1000;
  295. $ranker = SPH_RANK_PROXIMITY_BM25;
  296. $select = "";
  297. $cl->SetServer ( $host, $port );
  298. $cl->SetConnectTimeout ( 1 );
  299. $cl->SetArrayResult ( true );
  300. $cl->SetWeights ( array ( 100, 1 ) );
  301. $cl->SetMatchMode ( $mode );
  302. if ( count($filtervals) ) $cl->SetFilter ( $filter, $filtervals );
  303. if ( $groupby ) $cl->SetGroupBy ( $groupby, SPH_GROUPBY_ATTR, $groupsort );
  304. if ( $sortby ) $cl->SetSortMode ( SPH_SORT_EXTENDED, $sortby );
  305. if ( $sortexpr ) $cl->SetSortMode ( SPH_SORT_EXPR, $sortexpr );
  306. if ( $distinct ) $cl->SetGroupDistinct ( $distinct );
  307. if ( $select ) $cl->SetSelect ( $select );
  308. if ( $limit ) $cl->SetLimits ( 0, $limit, ( $limit>1000 ) ? $limit : 1000 );
  309. $cl->SetRankingMode ( $ranker );
  310. $y=($p-1)*10;
  311. $uid=$_SESSION['uid'];
  312. if($by=="all")
  313. {
  314. //$query="select eid,name,fname,sname,description,tyid from (resource natural join type) natural join users where name like '%$q%' or description like '$q%' order by name limit $y,10";
  315. $index = "*";
  316. $resname = $cl->Query("$q*",$index);
  317. if ( $resname===false )
  318. {
  319. echo "Query failed: " . $cl->GetLastError() . ".\n";
  320. } else
  321. {
  322. if ( $cl->GetLastWarning() )
  323. //echo "WARNING: " . $cl->GetLastWarning() . "\n\n";
  324. $res1[3]= "Search for '$q' retrieved $resname[total] of $resname[total_found] matches in $resname[time] sec.<br/>";
  325. if ( is_array($resname["matches"]) )
  326. {
  327. foreach ( $resname["matches"] as $docinfo )
  328. {
  329. $results.=$docinfo[id].",";
  330. }
  331. }
  332. }
  333. $query="select eid,name,fname,sname,description,tyid from (resource natural join type) natural join users where eid in ($results"."0) and tyid in ($t"."0".") limit $y,10";
  334. //echo $query;
  335. }
  336. else if($by=="name")
  337. {
  338. //$query="select eid,name,fname,sname,description,tyid from (resource natural join type) natural join users where name like '%$q%' order by name limit $y,10;";
  339. //$query="select eid,name,fname,sname,description,tyid from (resource natural join type) natural join users where name like \"$q%\" limit $y,10;";
  340. $index = "nameindex";
  341. $resname = $cl->Query ( "$q*", $index );
  342. if ( $resname===false )
  343. {
  344. echo "Query failed: " . $cl->GetLastError() . ".\n";
  345. } else
  346. {
  347. if ( $cl->GetLastWarning() )
  348. echo "WARNING: " . $cl->GetLastWarning() . "\n\n";
  349. $message= "Search for '$q' retrieved $resname[total] of $resname[total_found] matches in $resname[time] sec.<br/>";
  350. echo $message;
  351. if ( is_array($resname["matches"]) )
  352. {
  353. foreach ( $resname["matches"] as $docinfo )
  354. {
  355. $results.=$docinfo[id].",";
  356. }
  357. }
  358. }
  359. $query="select eid,name,fname,sname,description,tyid from (resource natural join type) natural join users where eid in ($results"."0) and tyid in ($t"."0".") limit $y,10";
  360. }
  361. else if($by=="tag")
  362. $query="select distinct resource.eid,name,fname,sname,description,type.tyid from resource,type,user,resource_tag where resource.tyid=type.tyid and resource.uid=user.uid and resource.eid=resource_tag.eid and tagname like '%$q%' and type.tyid in ($t"."0".") limit $y,10;";
  363. elseif($by=="taguser")
  364. $query="select distinct resource.eid,resource.name,user.fname,user.sname,type.description,type.tyid from resource,type,user,resource_tag where resource.tyid=type.tyid and resource.uid=user.uid and resource.eid=resource_tag.eid and tagname like '%$q%' and user.uid='$uid' and type.tyid in ($t"."0".")order by name limit $y,10;";
  365. else
  366. {
  367. $i=0;
  368. $res=mysql_query("select description from type natural join resource_attrib where attrib='$by';");
  369. while($row=mysql_fetch_array($res))
  370. {
  371. $q1="select eid from $row[0]details where $by like '$q%';";
  372. $res2=mysql_query($q1);
  373. if(!$res2)
  374. {
  375. fputs($error,mysql_error());
  376. die;
  377. }
  378. while($row2=mysql_fetch_array($res2))
  379. {
  380. $searchr[$i]=$row2[0];
  381. $i++;
  382. }
  383. }
  384. $query="select eid,name,fname,sname,description,tyid from (resource natural join type) natural join users where ";
  385. if($i>=1)
  386. foreach($searchr as $x1)
  387. {
  388. $query.="eid='$x1' or ";
  389. }
  390. $query.="eid='0' order by name;";
  391. }
  392. $res=mysql_query($query);
  393. if(!$res)
  394. {
  395. fputs($error,mysql_error());
  396. die;
  397. }
  398. $res1[0]=$res;
  399. $res1[1]=$resname[total];
  400. $res1[2]=$message;
  401. return $res1;
  402. }
  403. function searchresources_exists($q,$by,$p,$t)
  404. {
  405. $q=mysql_real_escape_string($q);
  406. $by=mysql_real_escape_string($by);
  407. $p=mysql_real_escape_string($p);
  408. $cl = new SphinxClient ();
  409. $sql = "";
  410. $mode = SPH_MATCH_PHRASE;
  411. $host = "localhost";
  412. $port = 9312;
  413. $groupby = "";
  414. $groupsort = "@group desc";
  415. $filter = "group_id";
  416. $filtervals = array();
  417. $distinct = "";
  418. $sortby = "";
  419. $limit = 1000;
  420. $ranker = SPH_RANK_PROXIMITY_BM25;
  421. $select = "";
  422. $cl->SetServer ( $host, $port );
  423. $cl->SetConnectTimeout ( 1 );
  424. $cl->SetArrayResult ( true );
  425. $cl->SetWeights ( array ( 100, 1 ) );
  426. $cl->SetMatchMode ( $mode );
  427. if ( count($filtervals) ) $cl->SetFilter ( $filter, $filtervals );
  428. if ( $groupby ) $cl->SetGroupBy ( $groupby, SPH_GROUPBY_ATTR, $groupsort );
  429. if ( $sortby ) $cl->SetSortMode ( SPH_SORT_EXTENDED, $sortby );
  430. if ( $sortexpr ) $cl->SetSortMode ( SPH_SORT_EXPR, $sortexpr );
  431. if ( $distinct ) $cl->SetGroupDistinct ( $distinct );
  432. if ( $select ) $cl->SetSelect ( $select );
  433. if ( $limit ) $cl->SetLimits ( 0, $limit, ( $limit>1000 ) ? $limit : 1000 );
  434. $cl->SetRankingMode ( $ranker );
  435. $y=($p-1)*10;
  436. $uid=$_SESSION['uid'];
  437. if($by=="all")
  438. {
  439. //$query="select eid,name,fname,sname,description,tyid from (resource natural join type) natural join users where name like '%$q%' or description like '$q%' order by name limit $y,10";
  440. $index = "*";
  441. $resname = $cl->Query("$q",$index);
  442. if ( $resname===false )
  443. {
  444. echo "";
  445. } else
  446. {
  447. if ( $cl->GetLastWarning() )
  448. //echo "WARNING: " . $cl->GetLastWarning() . "\n\n";
  449. $res1[3]= "Search for '$q' retrieved $resname[total] of $resname[total_found] matches in $resname[time] sec.<br/>";
  450. if ( is_array($resname["matches"]) )
  451. {
  452. foreach ( $resname["matches"] as $docinfo )
  453. {
  454. $results.=$docinfo[id].",";
  455. }
  456. }
  457. }
  458. $query="select eid,name,fname,sname,description,tyid from (resource natural join type) natural join users where eid in ($results"."0) and tyid in ($t"."0".") limit $y,10";
  459. //echo $query;
  460. }
  461. else if($by=="name")
  462. {
  463. //$query="select eid,name,fname,sname,description,tyid from (resource natural join type) natural join users where name like '%$q%' order by name limit $y,10;";
  464. //$query="select eid,name,fname,sname,description,tyid from (resource natural join type) natural join users where name like \"$q%\" limit $y,10;";
  465. $index = "nameindex";
  466. $resname = $cl->Query ( "$q", $index );
  467. if ( $resname===false )
  468. {
  469. echo "";
  470. } else
  471. {
  472. if ( $cl->GetLastWarning() )
  473. echo "WARNING: " . $cl->GetLastWarning() . "\n\n";
  474. $message= "Search for '$q' retrieved $resname[total] of $resname[total_found] matches in $resname[time] sec.<br/>";
  475. if ( is_array($resname["matches"]) )
  476. {
  477. foreach ( $resname["matches"] as $docinfo )
  478. {
  479. $results.=$docinfo[id].",";
  480. }
  481. }
  482. }
  483. $query="select eid,name,fname,sname,description,tyid from (resource natural join type) natural join users where eid in ($results"."0) and tyid in ($t"."0".") limit $y,10";
  484. }
  485. else if($by=="tag")
  486. $query="select distinct resource.eid,name,fname,sname,description,type.tyid from resource,type,user,resource_tag where resource.tyid=type.tyid and resource.uid=user.uid and resource.eid=resource_tag.eid and tagname like '%$q%' and type.tyid in ($t"."0".") limit $y,10;";
  487. elseif($by=="taguser")
  488. $query="select distinct resource.eid,resource.name,user.fname,user.sname,type.description,type.tyid from resource,type,user,resource_tag where resource.tyid=type.tyid and resource.uid=user.uid and resource.eid=resource_tag.eid and tagname like '%$q%' and user.uid='$uid' and type.tyid in ($t"."0".")order by name limit $y,10;";
  489. else
  490. {
  491. $i=0;
  492. $res=mysql_query("select description from type natural join resource_attrib where attrib='$by';");
  493. while($row=mysql_fetch_array($res))
  494. {
  495. $q1="select eid from $row[0]details where $by like '$q%';";
  496. $res2=mysql_query($q1);
  497. if(!$res2)
  498. {
  499. fputs($error,mysql_error());
  500. die;
  501. }
  502. while($row2=mysql_fetch_array($res2))
  503. {
  504. $searchr[$i]=$row2[0];
  505. $i++;
  506. }
  507. }
  508. $query="select eid,name,fname,sname,description,tyid from (resource natural join type) natural join users where ";
  509. if($i>=1)
  510. foreach($searchr as $x1)
  511. {
  512. $query.="eid='$x1' or ";
  513. }
  514. $query.="eid='0' order by name;";
  515. }
  516. $res=mysql_query($query);
  517. if(!$res)
  518. {
  519. fputs($error,mysql_error());
  520. die;
  521. }
  522. $res1[0]=$res;
  523. $res1[1]=$resname[total];
  524. $res1[2]=$message;
  525. return $res1;
  526. }
  527. function getcount($q,$by,$p)
  528. {
  529. $q=mysql_real_escape_string($q);
  530. $by=mysql_real_escape_string($by);
  531. $p=mysql_real_escape_string($p);
  532. if($by=="all")
  533. $query="select distinct count(*) from (resource natural join type) natural join users where name like '$q%' or description like '$q%' order by name";
  534. else if($by=="name")
  535. {
  536. //$query="select distinct count(*) from (resource natural join type) natural join users where name like '$q%' order by name;";
  537. $query="select count(eid) from (resource natural join type) natural join users where name like '$q%';";
  538. }
  539. else if($by=="tag")
  540. {
  541. //$query="select distinct count(*) from ((resource natural join type) natural join user) natural join resource_tag where tagname like '$q%' order by name;";
  542. $query="select count(distinct resource.eid) from resource,type,user,resource_tag where resource.tyid=type.tyid and resource.uid=user.uid and resource.eid=resource_tag.eid and tagname like '%$q%' order by name";
  543. }
  544. else
  545. {
  546. $i=0;
  547. $res=mysql_query("select description from type natural join resource_attrib where attrib='$by';");
  548. while($row=mysql_fetch_array($res))
  549. {
  550. $q1="select eid from $row[0]details where $by like '%$q%';";
  551. $res2=mysql_query($q1);
  552. if(!$res2)
  553. {
  554. fputs($error,mysql_error());
  555. die;
  556. }
  557. while($row2=mysql_fetch_array($res2))
  558. {
  559. $searchr[$i]=$row2[0];
  560. $i++;
  561. }
  562. }
  563. $query="select count(*) from (resource natural join type) natural join users where ";
  564. if($i>=1)
  565. foreach($searchr as $x1)
  566. {
  567. $query.="eid='$x1' or ";
  568. }
  569. $query.="eid='0' order by name;";
  570. }
  571. $res=mysql_query($query);
  572. if(!$res)
  573. {
  574. echo mysql_error();
  575. die;
  576. }
  577. $c=mysql_fetch_array($res);
  578. return $c[0];
  579. }
  580. function check_exists_index($value,$type)
  581. {
  582. $value=mysql_real_escape_string($value);
  583. $type=mysql_real_escape_string($type);
  584. $q="$value";
  585. $cl = new SphinxClient ();
  586. $sql = "";
  587. $mode = SPH_MATCH_EXTENDED;
  588. $host = "localhost";
  589. $port = 9312;
  590. $groupby = "";
  591. $groupsort = "@group desc";
  592. $filter = "group_id";
  593. $filtervals = array();
  594. $distinct = "";
  595. $sortby = "";
  596. $limit = 1000;
  597. $ranker = SPH_RANK_PROXIMITY_BM25;
  598. $select = "";
  599. $cl->SetServer ( $host, $port );
  600. $cl->SetConnectTimeout ( 1 );
  601. $cl->SetArrayResult ( true );
  602. $cl->SetWeights ( array ( 100, 1 ) );
  603. $cl->SetMatchMode ( $mode );
  604. if ( count($filtervals) ) $cl->SetFilter ( $filter, $filtervals );
  605. if ( $groupby ) $cl->SetGroupBy ( $groupby, SPH_GROUPBY_ATTR, $groupsort );
  606. if ( $sortby ) $cl->SetSortMode ( SPH_SORT_EXTENDED, $sortby );
  607. if ( $sortexpr ) $cl->SetSortMode ( SPH_SORT_EXPR, $sortexpr );
  608. if ( $distinct ) $cl->SetGroupDistinct ( $distinct );
  609. if ( $select ) $cl->SetSelect ( $select );
  610. if ( $limit ) $cl->SetLimits ( 0, $limit, ( $limit>1000 ) ? $limit : 1000 );
  611. $cl->SetRankingMode ( $ranker );
  612. $index = "*";
  613. $resname = $cl->Query("$q",$index);
  614. if ( $resname===false )
  615. {
  616. echo "error";
  617. } else
  618. {
  619. if($resname[total_found]==0)
  620. {
  621. echo "error";
  622. return;
  623. }
  624. if ( $cl->GetLastWarning() )
  625. //echo "WARNING: " . $cl->GetLastWarning() . "\n\n";
  626. if ( is_array($resname["matches"]) )
  627. {
  628. foreach ( $resname["matches"] as $docinfo )
  629. {
  630. $results.=$docinfo[id].",";
  631. echo $docinfo[id];
  632. }
  633. }
  634. }
  635. return $results;
  636. }
  637. function gettags($eid,$uid)
  638. {
  639. $eid=mysql_real_escape_string($eid);
  640. $uid=mysql_real_escape_string($uid);
  641. $res=mysql_query("select tagname from resource_tag where eid='$eid' and uid='$uid';");
  642. return $res;
  643. }
  644. function getpoptags($eid)
  645. {
  646. $eid=mysql_real_escape_string($eid);
  647. if($eid=="0")
  648. {
  649. $res=mysql_query("select distinct tagname from resource_tag group by tagname having count(tagname)>'150';");
  650. }
  651. else
  652. $res=mysql_query("select distinct tagname from resource_tag where eid='$eid';");
  653. return $res;
  654. }
  655. function gettagcount($tag,$eid)
  656. {
  657. $eid=mysql_real_escape_string($eid);
  658. $tag=mysql_real_escape_string($tag);
  659. if($eid=='0')
  660. $res=mysql_query("select count(*) from resource_tag where tagname='$tag';");
  661. else
  662. $res=mysql_query("select count(*) from resource_tag where tagname='$tag' and eid='$eid';");
  663. $row=mysql_fetch_array($res);
  664. return $row[0];
  665. }
  666. function getcomments($eid)
  667. {
  668. $eid=mysql_real_escape_string($eid);
  669. $res=mysql_query("select * from comment_table where eid='$eid';");
  670. if(!$res)
  671. {
  672. fputs($error,mysql_error());
  673. die;
  674. }
  675. return $res;
  676. }
  677. function getmodule($page)
  678. {
  679. $page=mysql_real_escape_string($page);
  680. $res1=mysql_query("select * from module where mod_loc='$page';");
  681. if(mysql_num_rows($res1)==0)
  682. {
  683. return;
  684. }
  685. if(!$res1)
  686. {
  687. fputs($error,mysql_error());
  688. die;
  689. }
  690. else
  691. {
  692. while($row=mysql_fetch_array($res1))
  693. {
  694. echo "<div id='$row[0]' class='module'>";
  695. if($page=="menu.php")
  696. include_once "modules/$row[0]";
  697. else
  698. include_once "modules/$row[0]";
  699. echo "</div>";
  700. }
  701. }
  702. }
  703. function getlostres()
  704. {
  705. $res=mysql_query("select * from resource where lost='1'");
  706. if(!$res)
  707. {
  708. fputs($error,mysql_error());
  709. die;
  710. }
  711. return $res;
  712. }
  713. function borrowed_from_log($uid)
  714. {
  715. $uid=mysql_real_escape_string($uid);
  716. $res=mysql_query("select * from log,resource where log.uid='$uid' and resource.eid=log.eid and activity='borrowed' group by name order by time_entry desc;");
  717. if(!$res)
  718. {
  719. fputs($error,mysql_error());
  720. die;
  721. }
  722. return $res;
  723. }
  724. function getattributes()
  725. {
  726. $res=mysql_query("select distinct attrib from resource_attrib;");
  727. if(!$res)
  728. {
  729. fputs($error,mysql_error());
  730. die;
  731. }
  732. return $res;
  733. }
  734. function checkauthor($tyid)
  735. {
  736. $tyid=mysql_real_escape_string($tyid);
  737. $res=mysql_query("select attrib from resource_attrib where tyid='$tyid' and attrib='author';");
  738. if(!$res)
  739. {
  740. fputs($error,mysql_error());
  741. die;
  742. }
  743. $c=mysql_num_rows($res);
  744. return $c;
  745. }
  746. function getauthor($eid,$tyid)
  747. {
  748. $eid=mysql_real_escape_string($eid);
  749. $tyid=mysql_real_escape_string($tyid);
  750. $ty=gettypename($tyid);
  751. $res=mysql_query("select author from $ty where eid='$eid';");
  752. //echo "select author from $ty where eid='$eid';";
  753. if(!$res)
  754. {
  755. fputs($error,mysql_error());
  756. die;
  757. }
  758. $row=mysql_fetch_array($res);
  759. return $row[0];
  760. }
  761. function getusers()
  762. {
  763. $res=mysql_query("select fname,sname,uid from users where uid<>'-1';");
  764. return $res;
  765. }
  766. function getrating($eid,$uid)
  767. {
  768. $eid=mysql_real_escape_string($eid);
  769. $uid=mysql_real_escape_string($uid);
  770. $res=mysql_query("select rating from ratings where eid='$eid' and uid='$uid';");
  771. if(!$res)
  772. {
  773. fputs($error,mysql_error());
  774. die;
  775. }
  776. $row=mysql_fetch_array($res);
  777. return $row[0];
  778. }
  779. function getglobalrating($eid)
  780. {
  781. $eid=mysql_real_escape_string($eid);
  782. $res=mysql_query("select rating from ratings where eid='$eid';");
  783. $avg=0;
  784. $count=mysql_num_rows($res);
  785. if($count!=0)
  786. {
  787. while($row=mysql_fetch_array($res))
  788. {
  789. //$count++;
  790. $avg+=$row[0];
  791. }
  792. $avg=$avg/$count;
  793. }
  794. else $avg=0;
  795. return $avg;
  796. }
  797. function logger($s)
  798. {
  799. $s=mysql_real_escape_string($s);
  800. $uid=$_SESSION['uid'];
  801. if($s=='Logged in' or $s=='Logged out')
  802. {
  803. $res=mysql_query("insert into log (time_entry,activity,uid) values(now(),'$s','$uid')");
  804. return;
  805. }
  806. $data=explode("::",$s);
  807. if($data[0]=="details")
  808. {
  809. $data[1]=mysql_real_escape_string($data[1]);
  810. $res=mysql_query("insert into log (time_entry,activity,uid,eid) values(now(),'viewed','$uid','$data[1]')");
  811. return;
  812. }
  813. if($data[0]=="wished for")
  814. {
  815. $data[1]=mysql_real_escape_string($data[1]);
  816. $res=mysql_query("insert into log (time_entry,activity,uid,wid,comments) values(now(),'wished for','$uid','$data[1]',\"$data[2]\")");
  817. //echo "insert into log (time_entry,activity,uid,wid,comments) values(now(),'viewed','$uid','$data[1]',\"$data[2]\")";
  818. return;
  819. }
  820. if($data[0]=="search")
  821. {
  822. $data[1]=mysql_real_escape_string($data[1]);
  823. $res=mysql_query("insert into log (time_entry,activity,uid,comments) values (now(),'search','$uid',\"$data[1]\")");
  824. return;
  825. }
  826. if($data[0]=="added")
  827. {
  828. $data[1]=mysql_real_escape_string($data[1]);
  829. $res=mysql_query("insert into log (time_entry,activity,uid,eid) values (now(),'added','$uid',\"$data[1]\")");
  830. return;
  831. }
  832. if($data[0]=="borrowed")
  833. {
  834. $data[1]=mysql_real_escape_string($data[1]);
  835. $res=mysql_query("insert into log (time_entry,activity,uid,eid) values (now(),'borrowed','$data[1]',\"$data[2]\")");
  836. if(!$res)
  837. {
  838. echo "insert into log (time_entry,activity,uid,eid) values (now(),'borrowed','$data[1]',\"$data[2]\")";
  839. }
  840. return;
  841. }
  842. if($data[0]=="returned")
  843. {
  844. $data[1]=mysql_real_escape_string($data[1]);
  845. $res=mysql_query("insert into log (time_entry,activity,uid,eid) values (now(),'returned','$data[1]',\"$data[2]\")");
  846. if(!$res)
  847. {
  848. echo mysql_error();
  849. }
  850. return;
  851. }
  852. if($data[0]=="added new resource type")
  853. {
  854. $data[1]=mysql_real_escape_string($data[1]);
  855. $res=mysql_query("insert into log(time_entry,activity,uid,comments) values(now(),'added new resource type','$uid',\"$data[1]\")");
  856. return;
  857. }
  858. if($data[0]=="edited")
  859. {
  860. $data[1]=mysql_real_escape_string($data[1]);
  861. $data[2]=mysql_real_escape_string($data[2]);
  862. $res=mysql_query("insert into log(time_entry,activity,uid,eid,comments) values(now(),'edited','$uid','$data[1]','$data[2]');");
  863. return;
  864. }
  865. }
  866. function api_logger($uid,$s)
  867. {
  868. }
  869. function rand_str($length = 32, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')
  870. {
  871. // Length of character list
  872. $chars_length = (strlen($chars) - 1);
  873. // Start our string
  874. $string = $chars{rand(0, $chars_length)};
  875. // Generate random string
  876. for ($i = 1; $i < $length; $i = strlen($string))
  877. {
  878. // Grab a random character from our list
  879. $r = $chars{rand(0, $chars_length)};
  880. // Make sure the same two characters don't appear next to each other
  881. if ($r != $string{$i - 1}) $string .= $r;
  882. }
  883. // Return the string
  884. return $string;
  885. }
  886. function getfav($uid)
  887. {
  888. $uid=mysql_real_escape_string($uid);
  889. $res=mysql_query("select resource.eid,name,tyid from favourites,resource where favourites.uid='$uid' and favourites.eid=resource.eid;");
  890. if(!$res or mysql_num_rows($res)==0)
  891. {
  892. echo "You havn't added anything to your favourites list.";
  893. die;
  894. }
  895. return $res;
  896. }
  897. function login($uid)
  898. {
  899. $uid=mysql_real_escape_string($uid);
  900. $res=mysql_query("insert into online values('$uid',now());");
  901. }
  902. function logout($uid)
  903. {
  904. $uid=mysql_real_escape_string($uid);
  905. $res=mysql_query("delete from online where uid='$uid';");
  906. }
  907. function getoverallrating($eid)
  908. {
  909. $eid=mysql_real_escape_string($eid);
  910. $row=mysql_fetch_array(mysql_query("select rating from resource where eid='$eid';"));
  911. return $row[0];
  912. }
  913. function delete_resource($eid)
  914. {
  915. mysql_query("delete from resource where eid='$eid';");
  916. echo "done";
  917. }
  918. function getauthors()
  919. {
  920. $f=fopen("authors.txt","w");
  921. $i=0;
  922. $res=mysql_query(" select author,count(author) from Book where author<>' ' group by author order by count(author) desc");
  923. while($row=mysql_fetch_array($res))
  924. {
  925. $authors=explode(",",$row[0]);
  926. foreach($authors as $a)
  927. {
  928. fputs($f,"$a-$row[1]\n");
  929. $i++;
  930. }
  931. }
  932. fclose($f);
  933. return $i;
  934. }
  935. function getcovers($eid)
  936. {
  937. $res=mysql_query("select url from images where eid='$eid' limit 0,3;");
  938. return $res;
  939. }
  940. function getallcovers($eid)
  941. {
  942. $res=mysql_query("select url,uid from images where eid='$eid';");
  943. return $res;
  944. }
  945. function check_for_download_request($eid,$uid)
  946. {
  947. $eid=mysql_real_escape_string($eid);
  948. $uid=mysql_real_escape_string($uid);
  949. $res=mysql_query("select * from download_request where eid='$eid' and uid='$uid'");
  950. return $res;
  951. }
  952. function pending_download_requests_for_eid($eid,$uid)
  953. {
  954. $eid=mysql_real_escape_string($eid);
  955. $uid=mysql_real_escape_string($uid);
  956. $res=mysql_query("select * from download_request where eid='$eid' and status='pending' and id in(select id from uploaded where uid='$uid');");
  957. return $res;
  958. }
  959. function get_installed_modules()
  960. {
  961. $res=mysql_query("select module_name,mod_id from installed_modules");
  962. return $res;
  963. }
  964. function notinstalled($name)
  965. {
  966. $res=mysql_query("select * from installed_modules where module_name='$name'");
  967. if(mysql_num_rows($res)==0)
  968. {
  969. return true;
  970. }
  971. else
  972. return false;
  973. }
  974. function most_popular()
  975. {
  976. $res=mysql_query("select eid from log where activity<>'returned' and eid<>'NULL' group by eid order by count(*) desc limit 9;");
  977. return $res;
  978. }
  979. function check_exists($eid)
  980. {
  981. $res=mysql_num_rows(mysql_query("select eid from resource where eid='$eid';"));
  982. return $res;
  983. }
  984. function getUserWishlist($uid)
  985. {
  986. $uid=mysql_real_escape_string($uid);
  987. $res=database_query("select * from wishlist where uid='$uid';");
  988. return $res;
  989. }
  990. function getPublicWishlist()
  991. {
  992. $res=database_query("select * from wishlist;");
  993. return $res;
  994. }
  995. ?>