PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/dbhandler.php

https://bitbucket.org/MaheshDhaduk/sr-analytics
PHP | 382 lines | 228 code | 66 blank | 88 comment | 5 complexity | 5e01373e663ad2c89cafa3cafd9db0d7 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-3.0
  1. <?php
  2. /**
  3. * This class contain all database related functions as utilities.
  4. * @author Mahesh Dhaduk
  5. */
  6. include_once ("config.php");
  7. class DbHandler
  8. {
  9. public $connection;
  10. function __construct()
  11. {
  12. $this->connection = mysql_connect(Config::$DBURL,Config::$DBUSERNAME,Config::$DBPASSWORD);
  13. if(!$this->connection)
  14. {
  15. echo mysql_error();
  16. }
  17. else
  18. {
  19. mysql_select_db(Config::$DBNAME,$this->connection);
  20. }
  21. }
  22. /**
  23. * Get MantisBt configuration which contain mantisconnect url for mantisbt server
  24. * and administrator's username and password
  25. * @return array
  26. */
  27. function GetMantisBtConfig()
  28. {
  29. $result = mysql_query("select url,admin_username,admin_password from mantisconfig",$this->connection);
  30. $config = mysql_fetch_array($result,MYSQL_ASSOC);
  31. return $config;
  32. }
  33. /**
  34. * Set MantisBt configuration which contain mantisconnect url for mantisbt server
  35. * and administrator's username and password
  36. * @param array $config associative array which contain url,admin_username,admin_password
  37. * @return boolean
  38. */
  39. function SetMantisBtConfig($config)
  40. {
  41. mysql_query('delete from mantisconfig',$this->connection);
  42. $insertSql = "insert into mantisconfig values ('".$config['url']."','".$config['admin_username']."','".$config['admin_password']."')";
  43. $config = mysql_query($insertSql,$this->connection);
  44. return $config;
  45. }
  46. /**
  47. * Get MantisBt Data which is useful to map username to userid
  48. * @return array It is array of associative arrays with userid,usename,realname,emailid
  49. */
  50. function GetMantisBtData()
  51. {
  52. $result = mysql_query("select userid,username,real_name,emailid from mantisbtdata",$this->connection);
  53. $mantisdata = array();
  54. while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  55. {
  56. array_push($mantisdata,$row);
  57. }
  58. return $mantisdata;
  59. }
  60. /**
  61. * Set MantisBt Data which is useful to map username to userid
  62. * @param string $mantisbtData It is array of associative arrays with userid,usename,realname,emailid
  63. * @return boolean
  64. */
  65. function SetMantisBtData($mantisbtData)
  66. {
  67. mysql_query('delete from mantisbtdata',$this->connection);
  68. $ret;
  69. foreach ($mantisbtData as $row)
  70. {
  71. $insertSql = "insert into mantisbtdata values ('".$row['id']."','".$row['name']."','".
  72. $row['real_name']."','".$row['email']."')";
  73. $ret = mysql_query($insertSql,$this->connection);
  74. }
  75. return $ret;
  76. }
  77. /**
  78. * Get Mantisbt User Id from Mantisbt User Name
  79. * @param string $userName Mantisbt User Name for User
  80. * @return integer
  81. */
  82. function GetMantisBtUserId($userName)
  83. {
  84. $result = mysql_query("select userid from mantisbtdata where username ='".$userName."'",$this->connection);
  85. $userId = 0;
  86. if($row = mysql_fetch_array($result,MYSQL_ASSOC))
  87. {
  88. $userId = $row['userid'];
  89. }
  90. return $userId;
  91. }
  92. /**
  93. * It return Bit Bucket User Names and Email Ids
  94. * @return array
  95. */
  96. function GetBitBucketData()
  97. {
  98. $result = mysql_query("select username,emailid from bitbucketdata",$this->connection);
  99. $bitbucketData = array();
  100. while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  101. {
  102. array_push($bitbucketData,$row);
  103. }
  104. return $bitbucketData;
  105. }
  106. /**
  107. * Set User Names and User Ids
  108. * @param string $bitbucketData array of arrays which contais username and emailid
  109. * @return boolean
  110. */
  111. function SetBitBucketData($bitbucketData)
  112. {
  113. mysql_query('delete from bitbucketdata',$this->connection);
  114. $ret;
  115. foreach ($bitbucketData as $row)
  116. {
  117. $insertSql = "insert into bitbucketdata values ('".$row['username']."','".$row['emailid']."')";
  118. $ret = mysql_query($insertSql,$this->connection);
  119. }
  120. return $ret;
  121. }
  122. /**
  123. * Get Facebook Data which is useful to map username to userid
  124. * @return array It is array of associative arrays with userid,usename,realname,emailid
  125. */
  126. function GetFacebookData()
  127. {
  128. $result = mysql_query("select userid,username,realname,emailid from facebookdata",$this->connection);
  129. $facebookData = array();
  130. while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  131. {
  132. array_push($facebookData,$row);
  133. }
  134. return $facebookData;
  135. }
  136. /**
  137. * Set Facebook Data which is useful to map username to userid
  138. * @param string $mantisbtData It is array of associative arrays with userid,usename,realname,emailid
  139. * @return boolean
  140. */
  141. function SetFacebookData($facebookData)
  142. {
  143. mysql_query('delete from facebookdata',$this->connection);
  144. $ret;
  145. foreach ($facebookData as $row)
  146. {
  147. $insertSql = "insert into facebookdata values (".$row['userid'].",'".$row['username']."','".
  148. $row['realname']."','".$row['emailid']."')";
  149. $ret = mysql_query($insertSql,$this->connection);
  150. }
  151. return $ret;
  152. }
  153. /**
  154. * It return You Tube User Names and Email Ids
  155. * @return array
  156. */
  157. function GetYouTubeData()
  158. {
  159. $result = mysql_query("select username,emailid from youtubedata",$this->connection);
  160. $youtubeData = array();
  161. while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  162. {
  163. array_push($youtubeData,$row);
  164. }
  165. return $youtubeData;
  166. }
  167. /**
  168. * Set User Names and User Ids
  169. * @param string $youtubeData array of arrays which contais username and emailid
  170. * @return boolean
  171. */
  172. function SetYouTubeData($youtubeData)
  173. {
  174. mysql_query('delete from youtubedata',$this->connection);
  175. $ret;
  176. foreach ($youtubeData as $row)
  177. {
  178. $insertSql = "insert into youtubedata values ('".$row['username']."','".$row['emailid']."')";
  179. $ret = mysql_query($insertSql,$this->connection);
  180. }
  181. return $ret;
  182. }
  183. /**
  184. * It give user names of Bitbucket,Mantisbt,Facebook,Youtube according to Security Research username for all users
  185. * @return array
  186. */
  187. function GetSrData()
  188. {
  189. $result = mysql_query("select srusername,bbusername,mbusername,fbusername,ytusername from srdata",$this->connection);
  190. $srData = array();
  191. while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  192. {
  193. array_push($srData,$row);
  194. }
  195. return $srData;
  196. }
  197. /**
  198. * Set user names of Bitbucket,Mantisbt,Facebook,Youtube according to Security Research username for all users
  199. * @param array $srData value of the name
  200. * @return boolean
  201. */
  202. function SetSrData($srData)
  203. {
  204. mysql_query('delete from srdata',$this->connection);
  205. $ret;
  206. foreach ($srData as $row)
  207. {
  208. $insertSql = "insert into srdata values ('".$row['srusername']."','".$row['bbusername']."','".
  209. $row['mbusername']."','".$row['fbusername']."','".$row['ytusername']."')";
  210. $ret = mysql_query($insertSql,$this->connection);
  211. }
  212. return $ret;
  213. }
  214. /**
  215. * It give list of Users and Repositories owned by him.
  216. * @return array
  217. */
  218. function GetBitBucketRepository()
  219. {
  220. $result = mysql_query("select username,repositoryname from bitbucketrepository",$this->connection);
  221. $repos = array();
  222. while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  223. {
  224. array_push($repos,$row);
  225. }
  226. return $repos;
  227. }
  228. /**
  229. * Set Users and his Repositories
  230. * @return boolean
  231. */
  232. function SetBitBucketRepository($repos)
  233. {
  234. mysql_query('delete from bitbucketrepository',$this->connection);
  235. $ret;
  236. foreach ($repos as $row)
  237. {
  238. $insertSql = "insert into bitbucketrepository values ('".$row['username']."','".$row['repositoryname']."')";
  239. $ret = mysql_query($insertSql,$this->connection);
  240. }
  241. return $ret;
  242. }
  243. /**
  244. * This function gives user names of given user for Bitbucket,Mantisbt,Facebook,Youtube
  245. * @param string $userName Security Research User name
  246. * @return array
  247. */
  248. function GetAllUserId($userName)
  249. {
  250. $query="select fbusername,ytusername,mbusername,bbusername from srdata where srusername='".$userName."'";
  251. $result = mysql_query($query,$this->connection);
  252. $ret = array();
  253. if($result!=false)
  254. {
  255. while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  256. {
  257. array_push($ret,$row);
  258. }
  259. }
  260. return $ret;
  261. }
  262. /**
  263. * Get Facebook user id Using Facebook user name
  264. * @param string $userName Facebook user name
  265. * @return array
  266. */
  267. function GetFBUserId($userName)
  268. {
  269. $query="select userid from facebookdata where username='".$userName."'";
  270. $result = mysql_query($query,$this->connection);
  271. $ret = array();
  272. if($result!=false)
  273. {
  274. while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  275. {
  276. array_push($ret,$row);
  277. }
  278. }
  279. return $ret;
  280. }
  281. /**
  282. * Give possible matches for given keywords for user name
  283. * @param string $text value of the name
  284. * @return array
  285. */
  286. function GetSuggestion($text)
  287. {
  288. $query="SELECT srusername FROM srdata where srusername like '%".$text."%'";
  289. $result = mysql_query($query,$this->connection);
  290. $ret = array();
  291. if($result!=false)
  292. {
  293. while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  294. {
  295. array_push($ret,$row);
  296. }
  297. }
  298. return $ret;
  299. }
  300. function InsertSRData($srUserName,$fbUserName,$ytUserName,$mbUserName,$bbUserName)
  301. {
  302. $ret;
  303. $insertsql = "insert into srdata values ('".$srUserName."','".$bbUserName."','".$mbUserName."','".$fbUserName."','".$ytUserName."')";
  304. $ret = mysql_query($insertsql,$this->connection);
  305. return $ret;
  306. }
  307. function UpdateSrData($srUserName,$fbUserName,$ytUserName,$mbUserName,$bbUserName)
  308. {
  309. mysql_query("delete from srdata where srusername = '".$srUserName."'",$this->connection);
  310. $ret;
  311. $insertSql = "insert into srdata values ('".$srUserName."','".$bbUserName."','".$mbUserName."','".$fbUserName."','".$ytUserName."')";
  312. $ret = mysql_query($insertSql,$this->connection);
  313. return $ret;
  314. }
  315. }
  316. ?>