PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/database.php

https://bitbucket.org/sspssp/akk-berlin
PHP | 578 lines | 280 code | 198 blank | 100 comment | 26 complexity | 3ad2c14b87a685c5105b832d1cb60ca0 MD5 | raw file
  1. <?php
  2. /*
  3. * ----------------------------------------------------------------------------
  4. * Hendrik Stiefel <hendrik@machmaldieaugenauf.de>, Jörg Franke <piratenpartei@newjorg.de>
  5. * and Wilm Schumacher <wilm.schumacher@piratenpartei.de> wrote this file. As
  6. * long as you retain this notice you can do whatever you want with this stuff.
  7. * If we meet some day, and you think this stuff is worth it, you can buy us a
  8. * beer in return
  9. *
  10. * Hendrik, Jörg and Wilm
  11. * ----------------------------------------------------------------------------
  12. */
  13. /*
  14. the database class. This is the connection to the world of mysql
  15. */
  16. class DataBase {
  17. private $db;
  18. // constructor
  19. function __construct() {
  20. $this->db=$this->DBConnect();
  21. }
  22. /*
  23. the fetching function with inbuild x-site-scripting protection
  24. */
  25. public function mysql_fetch_row( $res ) {
  26. $out=mysql_fetch_row( $res );
  27. if( $out==false ){ return false; };
  28. foreach( $out as &$value ){
  29. $value=htmlspecialchars( $value );
  30. };
  31. return $out;
  32. }
  33. /*
  34. the main search function
  35. makes the search depending on the input
  36. */
  37. public function GetSearch ( $lastname , $firstname , $membershipid ,
  38. $lastnamelike=true , $firstnamelike=true , $membershipidlike=true ) {
  39. $lastname=mysql_real_escape_string( $lastname , $this->db );
  40. $firstname=mysql_real_escape_string( $firstname , $this->db );
  41. $membershipid=mysql_real_escape_string( $membershipid , $this->db );
  42. $query="SELECT
  43. dbid,lastname,middlename,firstname,membershipid,birthdate,adress,city,zipcode,
  44. division,paid,akkreditiert,paidchanged,acomment,warning,topay
  45. FROM members ";
  46. if( $lastname!="" or $firstname!="" or $membershipid!="" ){
  47. $query=$query.'WHERE ';
  48. // lastname
  49. if( $lastname!="" ){
  50. if( $lastnamelike==true ){
  51. $query=$query.' lastname LIKE "%'.$lastname.'%" AND';
  52. } else {
  53. $query=$query.' lastname="'.$lastname.'" AND';
  54. };
  55. };
  56. // firstname
  57. if( $firstname!="" ){
  58. if( $firstnamelike==true ){
  59. $query=$query.' firstname LIKE "%'.$firstname.'%" AND';
  60. } else {
  61. $query=$query.' firstname="'.$firstname.'" AND';
  62. };
  63. };
  64. // lastname
  65. if( $membershipid!="" ){
  66. if( $membershipidlike==true ){
  67. $query=$query.' membershipid LIKE "%'.$membershipid.'%" AND';
  68. } else {
  69. $query=$query.' membershipid="'.$membershipid.'" AND';
  70. };
  71. };
  72. $query=substr( $query , 0 , strlen($query)-3);
  73. };
  74. $query=$query.' ORDER BY lastname,firstname,membershipid;';
  75. $out=mysql_query( $query , $this->db ) or die('something went wrong');
  76. return $out;
  77. }
  78. /*
  79. document
  80. */
  81. function DocumentChange( $dbid , $type , $comment=NULL ) {
  82. $userid=mysql_real_escape_string( $_SESSION['id'] );
  83. if( is_null($comment) ){
  84. $query='INSERT INTO changes (dbid,userid,thechange,thetime)
  85. VALUES ('.$dbid.','.$userid.','.$type.',NOW())';
  86. } else {
  87. $comment=mysql_real_escape_string( $comment );
  88. $query="INSERT INTO changes (dbid,userid,thechange,acomment,thetime)
  89. VALUES (".$dbid.",".$userid.",".$type.",'".$comment."',NOW())";
  90. };
  91. mysql_query( $query , $this->db ) or die('somethnig went wrong');
  92. return;
  93. }
  94. /*
  95. deakk a contact (with mysql injection protection)
  96. */
  97. public function DeAkk( $number ) {
  98. // the work
  99. $number=mysql_real_escape_string( $number , $this->db );
  100. $query='UPDATE members SET akkreditiert=0 WHERE dbid='.$number.';';
  101. mysql_query( $query , $this->db ) or die('somethnig went wrong');
  102. // document change
  103. $this->DocumentChange( $number,1 );
  104. return 0;
  105. }
  106. /*
  107. akk a contact (with mysql injection protection)
  108. */
  109. public function Akk( $number ) {
  110. // the work
  111. $number=mysql_real_escape_string( $number , $this->db );
  112. $query='UPDATE members SET akkreditiert=1 WHERE dbid='.$number.';';
  113. mysql_query( $query , $this->db ) or die('somethnig went wrong');
  114. // document change
  115. $this->DocumentChange( $number,0 );
  116. return 0;
  117. }
  118. /*
  119. pay a contact (with mysql injection protection)
  120. */
  121. public function Pay( $number , $comment ) {
  122. // the work
  123. $number=mysql_real_escape_string( $number , $this->db );
  124. $comment=mysql_real_escape_string( $comment , $this->db );
  125. $query='UPDATE members SET paid=1,paidchanged=1,acomment=\''.$comment.'\' WHERE dbid='.$number.';';
  126. mysql_query( $query , $this->db ) or die('somethnig went wrong');
  127. // document change
  128. $this->DocumentChange( $number,2 , $comment=$comment );
  129. return 0;
  130. }
  131. /*
  132. unpay a contact (with mysql injection protection)
  133. */
  134. public function UnPay( $number ) {
  135. // the work
  136. $number=mysql_real_escape_string( $number , $this->db );
  137. $query='UPDATE members SET paid=0,akkreditiert=0,paidchanged=0 WHERE dbid='.$number.';';
  138. mysql_query( $query , $this->db ) or die('somethnig went wrong');
  139. // document change
  140. $this->DocumentChange( $number,3 );
  141. return 0;
  142. }
  143. /*
  144. connecting to a database
  145. */
  146. private function DbConnect()
  147. {
  148. require_once('Constants.php');
  149. $db=mysql_connect($databhost , $databusername, $databpass);
  150. if (!$db) {
  151. die('Verbindung zur Datenbank fehlgeschlagen: '. mysql_error());
  152. }
  153. $db_selected=mysql_select_db($databname, $db);
  154. if (!$db_selected) {
  155. die('Kann die Datenbank nicht benutzen: ' . mysql_error());
  156. }
  157. mysql_set_charset('utf8',$db);
  158. $charset = mysql_client_encoding($db);
  159. //printf("Benutzer Zeichensatz ist %s\n", $charset);
  160. return $db;
  161. }
  162. /*
  163. search for contacts with a specific membership id (with mysql injection protection)
  164. */
  165. public function MemberschipIDSearch( $id ) {
  166. $id=mysql_real_escape_string( $id );
  167. $query='SELECT
  168. dbid,lastname,middlename,firstname,membershipid,birthdate,adress,city,zipcode,
  169. division,paid,akkreditiert,paidchanged,acomment,warning,topay
  170. FROM members WHERE membershipid='.$id.' ORDER BY lastname,firstname,membershipid;';
  171. return mysql_query( $query , $this->db );
  172. }
  173. /*
  174. search for contacts with a specific lastname (with mysql injection protection)
  175. */
  176. public function LastnameSearch( $name ) {
  177. $name=mysql_real_escape_string( $name );
  178. $query='SELECT
  179. dbid,lastname,middlename,firstname,membershipid,birthdate,adress,city,zipcode,
  180. division,paid,akkreditiert,paidchanged,acomment,warning,topay
  181. FROM members WHERE lastname LIKE "%'.$name.'%" ORDER BY lastname,firstname,membershipid;';
  182. return mysql_query( $query , $this->db );
  183. }
  184. /*
  185. gets the devisions in which the contacts are divided
  186. */
  187. public function GetDivisions() {
  188. $query='SELECT division FROM members GROUP BY division;';
  189. return mysql_query( $query );
  190. }
  191. /*
  192. gets the number of contacts by a division
  193. for $paidlive = x "paid on location" = x is checked
  194. for $akkreditiert = x "akkreditiert = x is checked
  195. (with mysql injection protection)
  196. */
  197. public function GetTotalByDivision( $division , $paidlive=NULL , $akkreditiert=NULL ) {
  198. $division=mysql_real_escape_string( $division , $this->db );
  199. $query="SELECT COUNT(*) FROM members WHERE division='".$division."'";;
  200. if( is_null( $paidlive )==false ){
  201. $paidlive=mysql_real_escape_string( $paidlive , $this->db );
  202. $query=$query.' AND paidchanged='.$paidlive;
  203. };
  204. if( is_null( $akkreditiert )==false ){
  205. $akkreditiert=mysql_real_escape_string( $akkreditiert , $this->db );
  206. $query=$query.' AND akkreditiert='.$akkreditiert;
  207. };
  208. $query=$query.';';
  209. $res=mysql_query( $query , $this->db );
  210. $num=mysql_num_rows( $res );
  211. if( $num!=1 ){
  212. die('something went horrible wrong');
  213. };
  214. $row=$this->mysql_fetch_row( $res );
  215. return $row[0];
  216. }
  217. /*
  218. This function is new in rel. 1.1. It's only for get some stats automaticly without using excel-stuff :-)
  219. $whichOne=1 -> returns number of already akk members for given division
  220. $whichOne=2 -> returns number of all members which paid for given division
  221. $whichOne=3 -> returns number of all members
  222. $whichOne=4 -> returns number of all members which paid
  223. */
  224. public function GetStats($division, $whichOne)
  225. {
  226. $division=mysql_real_escape_string( $division , $this->db );
  227. switch($whichOne)
  228. {
  229. case 1: $query="SELECT COUNT(*) FROM members WHERE division='".$division."' and akkreditiert = 1"; break;
  230. case 2: $query="SELECT COUNT(*) FROM members WHERE division='".$division."' and paid = 1"; break;
  231. case 3: $query="SELECT COUNT(*) FROM members"; break;
  232. case 4: $query="SELECT COUNT(*) FROM members where paid = 1"; break;
  233. }
  234. $query=$query.';';
  235. $res=mysql_query( $query , $this->db );
  236. $num=mysql_num_rows( $res );
  237. if( $num!=1 ){
  238. die('something went horrible wrong , maybe the databaseconnection is lost.');
  239. };
  240. $row=$this->mysql_fetch_row( $res );
  241. return $row[0];
  242. }
  243. /*
  244. get all contacts which paid live, by division (with mysql injection protection)
  245. */
  246. public function GetLivePaid( $division ) {
  247. $division=mysql_real_escape_string( $division , $this->db );
  248. $query="SELECT dbid,lastname,middlename,firstname,membershipid,birthdate,acomment FROM members
  249. WHERE division='".$division."' AND paidchanged=1 AND paid=1;";
  250. $res=mysql_query( $query , $this->db );
  251. return $res;
  252. }
  253. /*
  254. gets the whole list with all info to print it out
  255. */
  256. public function GetBackupListByDivision( $division ) {
  257. $division=mysql_real_escape_string( $division , $this->db );
  258. $query="SELECT lastname,middlename,firstname,membershipid,birthdate,adress,city,paid,akkreditiert,topay FROM members
  259. WHERE division='".$division."' ORDER BY lastname,firstname;";
  260. $res=mysql_query( $query , $this->db );
  261. return $res;
  262. }
  263. /*
  264. get all actions on one entry
  265. */
  266. public function GetChanges( $dbid ) {
  267. $dbid=mysql_real_escape_string( $dbid , $this->db );
  268. $query='SELECT changeid,userid,dbid,thechange,acomment,thetime FROM changes
  269. WHERE dbid='.$dbid.' ORDER BY thetime;';
  270. $res=mysql_query( $query , $this->db );
  271. return $res;
  272. }
  273. /*
  274. gets a name of a user by the id
  275. */
  276. public function GetUserById ( $id ) {
  277. $id=mysql_real_escape_string( $id , $this->db );
  278. $query='SELECT name FROM users WHERE userid='.$id.';';
  279. $res=mysql_query( $query , $this->db );
  280. if( mysql_num_rows( $res )!=1 ){
  281. die('something went horrible wrong');
  282. };
  283. return $this->mysql_fetch_row($res);
  284. }
  285. /*
  286. gets the id of a user by the name
  287. */
  288. public function GetIDByName ( $name ) {
  289. $name=mysql_real_escape_string( $name , $this->db );
  290. $query='SELECT userid FROM users WHERE name="'.$name.'";';
  291. $res=mysql_query( $query , $this->db );
  292. if( mysql_num_rows( $res )>1 ){
  293. die('something went horrible wrong');
  294. };
  295. $out=$this->mysql_fetch_row($res);
  296. return $out[0];
  297. }
  298. /*
  299. checks wheather a user + passwd is correct (returns number of entries)
  300. */
  301. public function CheckUserAndPw($name,$passwd)
  302. {
  303. $name=mysql_real_escape_string($name);
  304. $passwd=mysql_real_escape_string($passwd);
  305. $query="SELECT userid FROM users WHERE
  306. name='".$name."' and hash=md5('".$passwd."');";
  307. $result = mysql_query($query , $this->db );
  308. $num=mysql_num_rows($result);
  309. if( $num>1 ){
  310. die( 'something went horrible wrong' );
  311. };
  312. return $num;
  313. }
  314. // gets all users
  315. public function GetAllUsers () {
  316. $query="SELECT userid,name FROM users ORDER BY userid;";
  317. $res=mysql_query( $query , $this->db );
  318. return $res;
  319. }
  320. public function AddUser( $name , $passwd ) {
  321. $name=mysql_real_escape_string($name);
  322. $passwd=mysql_real_escape_string($passwd);
  323. $query='INSERT INTO users (name,hash) VALUES ("'.$name.'",md5("'.$passwd.'"));';
  324. mysql_query( $query , $this->db );
  325. return true;
  326. }
  327. public function DeleteUser( $id ) {
  328. $id=mysql_real_escape_string($id);
  329. $query='DELETE FROM users WHERE userid='.$id.';';
  330. mysql_query( $query , $this->db );
  331. return true;
  332. }
  333. public function ChangePasswd( $id , $passwd ) {
  334. $id=mysql_real_escape_string($id);
  335. $passwd=mysql_real_escape_string($passwd);
  336. $query='UPDATE users SET hash=md5("'.$passwd.'") WHERE userid='.$id.';';
  337. mysql_query( $query , $this->db );
  338. return true;
  339. }
  340. public function GetDBTime( ) {
  341. $res=mysql_query( "SELECT NOW();" );
  342. $row=mysql_fetch_row( $res );
  343. return $row[0];
  344. }
  345. public function GetMemberByID ( $id ) {
  346. $id=mysql_real_escape_string($id);
  347. $query='SELECT lastname,middlename,firstname,membershipid,birthdate,adress,city,zipcode,division
  348. FROM members WHERE dbid='.$id.';';
  349. $res=mysql_query( $query );
  350. $num=mysql_num_rows( $res );
  351. if( $num==0 ){
  352. return false;
  353. } else if ( $num==1 ){
  354. $out=$this->mysql_fetch_row($res);
  355. return $out;
  356. } else {
  357. die( 'something went horrible wring' );
  358. };
  359. }
  360. public function getWahlkreis($street, $streetNumber, $limit = 5)
  361. {
  362. $sql = 'SELECT * FROM `adressen` WHERE `Strassenname` LIKE "%'.mysql_real_escape_string(substr($street,0, $limit)).'%" AND `Nummerierungskennzeichen` IN("F", IF(MOD('.mysql_real_escape_string($streetNumber).', 2)=0, "G", "U")) AND `HausnrVon` <= '.mysql_real_escape_string($streetNumber).' AND `HausnrBis` >= '.mysql_real_escape_string($streetNumber);
  363. echo "<br>";
  364. $res = mysql_query($sql);
  365. $ret = array(); // Return Array
  366. while($row = mysql_fetch_array($res))
  367. {
  368. $ret[] = $row;
  369. }
  370. return $ret;
  371. }
  372. };
  373. ?>