PageRenderTime 1657ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/www/inc/util.lib.php

https://github.com/linkeddata/ldphp
PHP | 273 lines | 224 code | 34 blank | 15 comment | 49 complexity | 711e54b8cc53dc71d453d20efc1a5804 MD5 | raw file
  1. <?php
  2. /* util.lib.php
  3. * PHP utility functions
  4. */
  5. // check if the LDPC offers default prefixes
  6. function LDP_get_prefix($path, $uri, $type='http://ns.rww.io/ldpx#ldprPrefix') {
  7. if ($path && $uri) {
  8. $g = new Graph('', $path, '',$uri);
  9. if ($g->size() > 0) {
  10. // specific authorization
  11. $q = 'SELECT ?s, ?prefix WHERE { ?s <'.$type.'> ?prefix }';
  12. $s = $g->SELECT($q);
  13. $res = $s['results']['bindings'];
  14. if (isset($res) && count($res) > 0)
  15. return $res[0]['prefix']['value'];
  16. }
  17. }
  18. return null;
  19. }
  20. function clearSession($i_next) {
  21. foreach ($_SESSION as $k=>$v) {
  22. sess($k, null);
  23. }
  24. if (isset($i_next)) {
  25. sess('next', $i_next);
  26. } elseif (isMethod('GET') && isset($_SERVER['HTTP_REFERER'])) {
  27. sess('next', $_SERVER['HTTP_REFERER']);
  28. }
  29. if (isSess('next')) {
  30. $next = sess('next', null);
  31. $next = str_replace('https://', 'http://', $next);
  32. header('Location: '.$next);
  33. } else {
  34. header('Location: /');
  35. }
  36. exit;
  37. }
  38. // calculate the md5 of a dir
  39. function md5_dir($dir) {
  40. if (!is_dir($dir)) {
  41. return false;
  42. }
  43. $filemd5s = array();
  44. $d = dir($dir);
  45. while (false !== ($entry = $d->read())) {
  46. if ($entry != '.' && $entry != '..') {
  47. if (is_dir($dir.'/'.$entry))
  48. $filemd5s[] = md5_dir($dir.'/'.$entry);
  49. else
  50. $filemd5s[] = md5_file($dir.'/'.$entry);
  51. }
  52. }
  53. $d->close();
  54. return md5(implode('', $filemd5s));
  55. }
  56. // parse a given http header
  57. function http_parse_link_header( $header ) {
  58. $retVal = array();
  59. $elems = explode(';', $header);
  60. foreach ($elems as $v) {
  61. $v = str_replace('<', '', $v);
  62. $v = str_replace('>', '', $v);
  63. array_push($retVal , trim($v));
  64. }
  65. return $retVal;
  66. }
  67. // check if a dir is empty
  68. function is_dir_empty($dir) {
  69. $files = scandir($dir);
  70. if (sizeof($files) > 2)
  71. return false;
  72. else
  73. return true;
  74. }
  75. // display the file sizes in a human readable format
  76. function human_filesize($bytes, $decimals=2) {
  77. $sz = 'BKMGTP';
  78. $factor = floor((strlen($bytes) - 1) / 3);
  79. return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
  80. }
  81. // get quota for a dir
  82. function get_quota($dir) {
  83. $get_size = '/usr/bin/du -sk '.$dir."|awk '{ print $1; }'";
  84. $used = trim(shell_exec($get_size));
  85. $total = DISK_QUOTA * 1000000; // mega to bytes
  86. $used = $used * 1000; // kilo to bytes
  87. return array('used'=>$used, 'total'=>$total);
  88. }
  89. // check if the size fits inside the quota
  90. function check_quota($dir, $size) {
  91. $q = get_quota($dir);
  92. $a = $q['total'] - $q['used'];
  93. return ($size <= $a)?true:false;
  94. }
  95. // display the quota bar
  96. function display_quota($dir) {
  97. $q = get_quota($dir);
  98. $used = $q['used'] / 1000000; // bytes to mega
  99. $total = $q['total'] / 1000000; // bytes to mega;
  100. $medium = $total * (70/100);
  101. // at 90% we start to get worried
  102. $high = $total * (90/100);
  103. if ($used <= $medium)
  104. $bg = 'green';
  105. else if ($used > $medium && $used <= $high)
  106. $bg = 'yellow';
  107. else if ($used > $high)
  108. $bg = 'red';
  109. $width = ($used *100) / $total;
  110. $width = number_format($width, 2);
  111. $ret = '<div class="quota" title="Quota: used '.
  112. number_format($used, 2).' MB / '.
  113. number_format($total).' MB">';
  114. $ret .= ' <div class="meter '.$bg.'" style="width:'.$width.'%;"></div>';
  115. $ret .= ' <div class="meter-inner">'.number_format($used, 2).'MB /'.number_format($total).'MB</div>';
  116. $ret .= '</div>';
  117. return $ret;
  118. }
  119. function isMethod($method) { return ($_SERVER['REQUEST_METHOD'] == $method); }
  120. function isPost() { return isMethod('POST'); }
  121. function isPostData() {
  122. return isset($_SERVER['REQUEST_METHOD']) && isset($_SERVER['HTTP_CONTENT_LENGTH'])
  123. && $_SERVER['REQUEST_METHOD'] == 'POST' && $_SERVER['HTTP_CONTENT_LENGTH'] > 0;
  124. }
  125. function isSess($id) { return isset($_SESSION[$id]); }
  126. function sess($id,$val=NULL) {
  127. if (func_num_args()==1) {
  128. return (isSess($id)?$_SESSION[$id]:NULL);
  129. } elseif (is_null($val)) {
  130. $r = isset($_SESSION[$id]) ? $_SESSION[$id] : null;
  131. unset($_SESSION[$id]);
  132. return $r;
  133. } else {
  134. $prev = sess($id);
  135. $_SESSION[$id] = $val;
  136. return $prev;
  137. }
  138. }
  139. function newQS($key, $val=null) { return newQSA(array($key=>$val)); }
  140. function newQSA($array=array()) {
  141. parse_str($_SERVER['QUERY_STRING'], $arr);
  142. $s = count($arr);
  143. foreach($array as $key=>$val) {
  144. $arr[$key] = $val;
  145. if (is_null($val))
  146. unset($arr[$key]);
  147. }
  148. return (count($arr)||$s)?'?'.http_build_query($arr):'';
  149. }
  150. function isHTTPS() { return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'); }
  151. function timings($query=null) {
  152. global $timingc;
  153. global $timings;
  154. if(!isset($timings)) {
  155. $timings = array();
  156. }
  157. if (!isset($timingc) || empty($timingc)) {
  158. $timingc = 1;
  159. } elseif (!is_null($query)) {
  160. $timingc++;
  161. }
  162. $key = $timingc;
  163. if (is_null($query)) {
  164. $timings[$key]['time'] = microtime(true)-$timings[$key]['time'];
  165. if (function_exists('mysql_error') && mysql_error())
  166. $timings[$key]['error'] = mysql_error();
  167. return true;
  168. } else {
  169. $timings[$key] = array();
  170. $timings[$key]['time'] = microtime(true);
  171. $timings[$key]['query'] = $query;
  172. return false;
  173. }
  174. }
  175. function httpStatusExit($status, $message, $require=null, $body=null) {
  176. global $_options, $TAGS;
  177. if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
  178. $x_json = json_encode(array('status'=>$status,'message'=>$message));
  179. }
  180. $status = (string)$status;
  181. header("HTTP/1.1 $status $message");
  182. if (isset($x_json))
  183. header('X-JSON: '.$x_json);
  184. if ($require)
  185. require_once($require);
  186. else
  187. echo "<h1>$status $message</h1>\n";
  188. if ($body)
  189. echo "<p>$body</p>\n";
  190. exit;
  191. }
  192. $TAGS = array(array(
  193. 'file' => __FILE__,
  194. 'line' => __LINE__,
  195. 'id' => null,
  196. 'time'=>microtime(true)
  197. ));
  198. function TAG($file, $line, $id) {
  199. global $TAGS;
  200. $TAGS[] = array(
  201. 'file' => $file,
  202. 'line' => $line,
  203. 'id' => $id,
  204. 'time' => microtime(true)
  205. );
  206. }
  207. function http($method, $uri, $content=null) {
  208. $c = stream_context_create(array('http'=>array(
  209. 'method' => $method,
  210. 'header' =>"Connection: close\r\nContent-Type: application/json\r\nAccept: application/json\r\nReferer: https://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}\r\n",
  211. 'content' => $content,
  212. 'ignore_errors' => true,
  213. 'max_redirects' => 0,
  214. )));
  215. $f = fopen($uri, 'r', false, $c);
  216. $h = stream_get_meta_data($f);
  217. $r = stream_get_contents($f);
  218. fclose($f);
  219. $status = 0;
  220. $header = array();
  221. if (isset($h['wrapper_data'])) {
  222. $status = array_shift($h['wrapper_data']);
  223. $header['Status'] = $status;
  224. $lst = explode(' ', $status);
  225. if (count($lst) > 1)
  226. $status = (int)$lst[1];
  227. foreach ($h['wrapper_data'] as $elt) {
  228. $i = strpos($elt, ': ');
  229. $k = substr($elt, 0, $i);
  230. if (!isset($header[$k]))
  231. $header[$k] = array(substr($elt, $i+2));
  232. //elseif (!is_array($header[$k]))
  233. // $header[$k] = array($header[$k], substr($elt, $i+2));
  234. else
  235. $header[$k][] = substr($elt, $i+2);
  236. }
  237. }
  238. return (object)array('uri'=>$uri, 'status'=>$status, 'header'=>$header, 'body'=>$r);
  239. }