functions.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. require_once(realpath(dirname(__FILE__)).'/config.php');
  3. // TODO - create php functions for the api
  4. function retj($json,$title=null){
  5. global $LOGGEDIN;
  6. $type=$_GET['type'];
  7. $id=$_GET['id'];
  8. if(!isset($_GET['no_state'])){
  9. // State
  10. if(!isset($json['state'])){
  11. $json['state'] = array();
  12. }
  13. unset($_GET['password']);
  14. unset($_GET['password1']);
  15. if(!isset($json['state']['data'])){
  16. $json['state']['data'] = $_GET;
  17. }else{
  18. foreach($_GET as $key => $val){
  19. if(!isset($json['state']['data'][$key])&&$key!='password'){
  20. $json['state']['data'][$key] = $val;
  21. }
  22. }
  23. }
  24. }
  25. // Title
  26. if(is_null($title)){
  27. if(isset($json['title'])){
  28. $title = $json['title'];
  29. }elseif(isset($json['state']['title'])&&!isset($_GET['no_state'])){
  30. $title = $json['state']['title'];
  31. }else{
  32. $title = $_GET['id'];
  33. }
  34. }
  35. if(!isset($_GET['no_state'])){
  36. $json['state']['title'] = $title;
  37. if(!isset($json['state']['url'])){
  38. // URL
  39. switch($type){
  40. case 'user':$url='~'.$id;break;
  41. case 'group':$url='+'.$id;break;
  42. case 'issue':$url='!'.$id;break;
  43. case 'action':$url='';break;
  44. default:$url=$type.'-'.$id;
  45. }
  46. $json['state']['url'] = $url;
  47. }else{
  48. $url = $json['state']['url'];
  49. }
  50. }
  51. // Tobar
  52. if($LOGGEDIN){
  53. $context = array(
  54. 'user'=>userObj($_SESSION['username']),
  55. 'key'=>true
  56. );
  57. }else{
  58. $context = array();
  59. }
  60. if(file_exists(PATH_DATA.'topbars/'.$id.'.options')){
  61. $options = objectToarray(json_decode(file_get_contents(PATH_DATA.'topbars/'.$id.'.options'),true));
  62. foreach($options as $key => $option){
  63. $context[$key] = $option;
  64. }
  65. }
  66. $context['title'] = $title;
  67. if(!isset($_GET['no_state'])){
  68. $context['url'] = $url;
  69. }
  70. if(isset($json['topbar'])){
  71. $topbar = $json['topbar'];
  72. }else{
  73. $topbar = 'default';
  74. }
  75. $json['topbar'] = array(
  76. 'template'=>$topbar,
  77. 'context'=>$context
  78. );
  79. // Sidebar
  80. if(isset($json['sidebar'])){
  81. $sidebar = $json['sidebar'];
  82. }else{
  83. $sidebar = 'default';
  84. }
  85. $json['sidebar'] = array(
  86. 'template'=>$sidebar,
  87. 'context'=>$context
  88. );
  89. header('Content-type: application/json');
  90. die(json_encode($json));
  91. }
  92. function is_valid($col,$v=null){
  93. if($v == null){
  94. $v = $_GET;
  95. }
  96. if(isset($v[$col]) && !empty($v[$col])){
  97. $v = $v[$col];
  98. switch($col){
  99. case 'email':
  100. return filter_var($v,FILTER_VALIDATE_EMAIL) != false;
  101. case 'username':
  102. return strip_tags($v) == $v;
  103. default:
  104. return true;
  105. }
  106. }else{
  107. return false;
  108. }
  109. }
  110. function back($ifNotLoggedIn=false){
  111. global $LOGGEDIN;
  112. if($ifNotLoggedIn && $LOGGEDIN){
  113. return false;
  114. }
  115. retj(array(
  116. 'state'=>array(
  117. 'url'=>'page-index'
  118. )
  119. ));
  120. }
  121. function stateObj($type,$id){
  122. $json = array(
  123. 'data'=>$_GET
  124. );
  125. $title = ucwords($type.' - '.$id);
  126. switch($type){
  127. case 'user':$url='~'.$id;break;
  128. case 'group':$url='+'.$id;break;
  129. case 'issue':
  130. $url = '!'.$id;
  131. $title = issueObj($id);
  132. $title = 'Issue #'.$title['title'];
  133. break;
  134. case 'page':$url='page-'.$id;break;
  135. case 'project':
  136. $url = $type.'-'.$id;
  137. $title = projectObj($id);
  138. $title = 'Project - '.$title['title'];
  139. break;
  140. default:$url=$type.'-'.$id;
  141. }
  142. $json['url'] = $url;
  143. $json['title'] = $title;
  144. return $json;
  145. }
  146. function vnsprintf($format,array $data){
  147. preg_match_all('/ (?<!%) % ( (?: [[:alpha:]_-][[:alnum:]_-]* | ([-+])? [0-9]+ (?(2) (?:\.[0-9]+)? | \.[0-9]+ ) ) ) \$ [-+]? \'? .? -? [0-9]* (\.[0-9]+)? \w/x', $format, $match, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
  148. $offset = 0;
  149. $keys = array_keys($data);
  150. foreach( $match as &$value ){
  151. if(($key = array_search( $value[1][0], $keys, TRUE) ) !== FALSE || ( is_numeric( $value[1][0] ) && ( $key = array_search( (int)$value[1][0], $keys, TRUE) ) !== FALSE) ){
  152. $len = strlen( $value[1][0]);
  153. $format = substr_replace( $format, 1 + $key, $offset + $value[1][1], $len);
  154. $offset -= $len - strlen( 1 + $key);
  155. }
  156. }
  157. return vsprintf( $format, $data);
  158. }
  159. ?>