bugs.class.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. require_once('sql.class.php');
  3. require_once('template.class.php');
  4. require_once('user.class.php');
  5. require_once('project.class.php');
  6. require_once('router.class.php');
  7. foreach(glob("widgets/*.widget.php") as $filename){
  8. require_once($filename);
  9. }
  10. class Bugs {
  11. public static $sql;
  12. public static $cache = array(
  13. 'users'=>array(),
  14. 'issue'=>array(),
  15. 'projects'=>array(),
  16. 'statuses'=>array(),
  17. 'priorities'=>array()
  18. );
  19. public static $user = false;
  20. public function __construct(){
  21. }
  22. public function __set($name,$value){
  23. switch($name){
  24. default:
  25. static::$cache[$name] = $value;
  26. }
  27. }
  28. public function __get($name){
  29. switch($name){
  30. default:
  31. if(isset(static::$cache)){
  32. return static::$cache[$name];
  33. }
  34. }
  35. }
  36. static function connect($server='localhost',$user='bugs',$pass='bugs',$db='bugs'){
  37. static::$sql = new SQL($server,$user,$pass,$db);
  38. if(session_status() == PHP_SESSION_NONE){
  39. session_start();
  40. }
  41. if(isset($_COOKIE['key']) && isset($_SESSION['key']) && isset($_SESSION['user']) && static::user_id($_SESSION['user']) && $_SESSION['key'] == $_COOKIE['key']){
  42. $user = static::user($_SESSION['user']);
  43. $session = static::$sql->query("
  44. SELECT count(id) AS count
  45. FROM sessions
  46. WHERE id = ?
  47. AND u_id = ?
  48. AND ip = ?
  49. ",'sis',$_SESSION['key'],$user->id,static::ip())->assoc_result;
  50. if($session && intval($session['count']) == 1){
  51. static::$user = $user;
  52. static::$sql->query("
  53. UPDATE sessions
  54. SET date = CURRENT_TIMESTAMP
  55. WHERE id = ?
  56. AND u_id = ?
  57. AND ip = ?
  58. ",'sis',$_SESSION['key'],$user->id,static::ip())->execute();
  59. }else{
  60. static::logout();
  61. }
  62. }
  63. Router::base(static::setting('url.base'));
  64. }
  65. static function ip(){
  66. if(getenv('HTTP_CLIENT_IP')){
  67. $ipaddress = getenv('HTTP_CLIENT_IP');
  68. }else if(getenv('HTTP_X_FORWAR0DED_FOR')){
  69. $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
  70. }else if(getenv('HTTP_X_FORWARDED')){
  71. $ipaddress = getenv('HTTP_X_FORWARDED');
  72. }else if(getenv('HTTP_FORWARDED_FOR')){
  73. $ipaddress = getenv('HTTP_FORWARDED_FOR');
  74. }else if(getenv('HTTP_FORWARDED')){
  75. $ipaddress = getenv('HTTP_FORWARDED');
  76. }else if(getenv('REMOTE_ADDR')){
  77. $ipaddress = getenv('REMOTE_ADDR');
  78. }else{
  79. $ipaddress = 'UNKNOWN';
  80. }
  81. return $ipaddress;
  82. }
  83. static function user_agent(){
  84. return substr($_SERVER['HTTP_USER_AGENT'],0,4000);
  85. }
  86. static function session(){
  87. return $_SESSION['key'];
  88. }
  89. static function login($user,$pass){
  90. if(!$user instanceof User && static::user_id($user)){
  91. $user = static::user($user);
  92. }
  93. if($user instanceof User && $user->active && $user->hash($pass) == $user->password){
  94. $key = $user->login_key;
  95. static::$sql->query("
  96. INSERT INTO sessions (id,u_id,ip,info)
  97. VALUES (?,?,?,?)
  98. ",'siss',$key,$user->id,static::ip(),static::user_agent())->execute();
  99. $_SESSION['user'] = $user->name;
  100. setcookie('user',$user->name,0,'/');
  101. $_SESSION['key'] = $key;
  102. setcookie('key',$key,0,'/');
  103. static::$user = $user;
  104. }
  105. return static::$user !== false;
  106. }
  107. static function logout(){
  108. if(static::$user){
  109. static::$sql->query("
  110. DELETE FROM sessions
  111. WHERE id = ?
  112. AND u_id = ?
  113. AND ip = ?
  114. ",'sis',$_SESSION['key'],static::$user->id,static::ip())->execute();
  115. }
  116. unset($_SESSION['user']);
  117. unset($_SESSION['key']);
  118. unset($_COOKIE['user']);
  119. unset($_COOKIE['key']);
  120. setcookie("user", "", time()-3600, '/');
  121. setcookie("key", "", time()-3600, '/');
  122. static::$user = false;
  123. }
  124. static function user($id){
  125. if(func_num_args()==1){
  126. if(is_string($id)){
  127. $id = static::user_id($id);
  128. if(!$id){
  129. trigger_error("User {$id} does not exist");
  130. }
  131. }
  132. }else{
  133. $id = new User(func_get_arg(0),func_get_arg(1),func_get_arg(2));
  134. $id = $id->id;
  135. }
  136. if(!isset(static::$cache['users'][$id])){
  137. static::$cache['users'][$id] = new User($id);
  138. }
  139. return static::$cache['users'][$id];
  140. }
  141. static function user_id($name){
  142. $user = static::$sql->query("
  143. SELECT id
  144. FROM users
  145. WHERE name = ?;
  146. ",'s',$name)->assoc_result;
  147. if(is_null($user)){
  148. return false;
  149. }else{
  150. return $user['id'];
  151. }
  152. }
  153. static function project_id($name){
  154. $project = static::$sql->query("
  155. SELECT id
  156. FROM projects
  157. WHERE name = ?;
  158. ",'s',$name)->assoc_result;
  159. if(is_null($project)){
  160. return false;
  161. }else{
  162. return $project['id'];
  163. }
  164. }
  165. static function issue($id){
  166. if(func_num_args()>1){
  167. $id = new Issue(
  168. func_get_arg(0),
  169. func_get_arg(1),
  170. func_num_args()>=3?func_get_arg(2):null,
  171. func_num_args()>=4?func_get_arg(3):null,
  172. func_num_args()==5?func_get_arg(4):null
  173. );
  174. $id = $id->id;
  175. }
  176. if(!isset(static::$cache['issues'][$id])){
  177. static::$cache['issues'][$id] = new Issue($id);
  178. }
  179. return static::$cache['issues'][$id];
  180. }
  181. static function project($id){
  182. if(func_num_args()==1){
  183. if(is_string($id)){
  184. $id = static::$sql->query("
  185. SELECT id
  186. FROM projects
  187. WHERE name = ?;
  188. ",'s',$id)->assoc_result['id'];
  189. }
  190. }else{
  191. $id = new Project(func_get_arg(0),func_get_arg(1),func_num_args()==3?func_get_arg(2):null);
  192. $id = $id->id;
  193. }
  194. if(!isset(static::$cache['projects'][$id])){
  195. static::$cache['projects'][$id] = new Project($id);
  196. }
  197. return static::$cache['projects'][$id];
  198. }
  199. static function template($name){
  200. return new Template($name);
  201. }
  202. static function actions(){
  203. $args = func_get_args();
  204. foreach($args as $action){
  205. static::$sql->query("
  206. SELECT `action`(?)
  207. FROM DUAL;
  208. ",'s',$action)->assoc_result;
  209. }
  210. }
  211. static function status($id){
  212. if(empty(static::$cache['statuses'][$id])){
  213. static::$cache['statuses'][$id] = static::$sql->query("
  214. SELECT max(name) name
  215. FROM statuses
  216. WHERE id = ?
  217. ",'i',intval($id))->assoc_result['name'];
  218. }
  219. return static::$cache['statuses'][$id];
  220. }
  221. static function priority($id){
  222. if(empty(static::$cache['priorities'][$id])){
  223. static::$cache['priorities'][$id] = static::$sql->query("
  224. SELECT max(name) name
  225. FROM priorities
  226. WHERE id = ?
  227. ",'i',intval($id))->assoc_result['name'];
  228. }
  229. return static::$cache['priorities'][$id];
  230. }
  231. static function activity($action,$data){
  232. static::$sql->query("
  233. INSERT INTO activities (a_id,data)
  234. VALUES (`action`(?),?)
  235. ",'ss',$action,json_encode($data))->execute();
  236. }
  237. static function setting($name){
  238. return static::$sql->query("
  239. SELECT getsetting(?)
  240. FROM DUAL;
  241. ",'s',$name)->num_result[0];
  242. }
  243. static function permission($permission){
  244. return static::$user->permission($permission);
  245. }
  246. static function authorized(){
  247. foreach(func_get_args() as $permission){
  248. if(!static::permission($permission)){
  249. trigger_error("Access denied. You do not have the {$permission} permission");
  250. }
  251. }
  252. }
  253. static function options_statuses(){
  254. $res = static::$sql->query("
  255. SELECT id,
  256. name
  257. FROM statuses
  258. ")->assoc_results;
  259. $ret = array();
  260. foreach($res as $row){
  261. $ret[$row['id']] = $row['name'];
  262. }
  263. return $ret;
  264. }
  265. static function options_priorities(){
  266. $res = static::$sql->query("
  267. SELECT id,
  268. name
  269. FROM priorities
  270. ")->assoc_results;
  271. $ret = array();
  272. foreach($res as $row){
  273. $ret[$row['id']] = $row['name'];
  274. }
  275. return $ret;
  276. }
  277. }
  278. register_shutdown_function(function(){
  279. $emails = Bugs::$sql->query("
  280. SELECT u.email,
  281. u_id,
  282. e.subject,
  283. e.body,
  284. e.date_created
  285. FROM emails e
  286. JOIN users u
  287. ON u.id = e.u_id
  288. ORDER by e.date_created ASC
  289. ")->assoc_results;
  290. foreach($emails as $email){
  291. $status = @mail($email['email'],$email['subject'],$email['body'],"From: ".Bugs::setting('admin.email')."\r\nMIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n");
  292. if($status){
  293. Bugs::$sql->query("
  294. DELETE FROM emails
  295. WHERE subject = ?
  296. AND body = ?
  297. AND u_id = ?
  298. ",'ssi',$email['subject'],$email['body'],$email['u_id'])->execute();
  299. }
  300. }
  301. Bugs::$sql->query("
  302. CALL maintain_sessions;
  303. ")->execute();
  304. });
  305. ?>