You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.9 KiB

  1. <?php
  2. // Default timezone
  3. date_default_timezone_set('UTC');
  4. ORM::configure('mysql:host=' . Config::$dbHost . ';dbname=' . Config::$dbName);
  5. ORM::configure('username', Config::$dbUsername);
  6. ORM::configure('password', Config::$dbPassword);
  7. function friendly_url($url) {
  8. return preg_replace(['/https?:\/\//','/\/$/'],'',$url);
  9. }
  10. function friendly_date($date_string, $tz_offset) {
  11. if(!$date_string)
  12. return '';
  13. $date = new DateTime($date_string);
  14. if($tz_offset > 0)
  15. $date->add(new DateInterval('PT'.$tz_offset.'S'));
  16. elseif($tz_offset < 0)
  17. $date->sub(new DateInterval('PT'.abs($tz_offset).'S'));
  18. $tz = ($tz_offset < 0 ? '-' : '+') . sprintf('%02d:%02d', abs($tz_offset/60/60), ($tz_offset/60)%60);
  19. return $date->format('F j, Y H:i:s') . ' ' . $tz;
  20. }
  21. function format_date($date_string, $tz_offset) {
  22. if(!$date_string)
  23. return '';
  24. $date = new DateTime($date_string);
  25. if($tz_offset > 0)
  26. $date->add(new DateInterval('PT'.$tz_offset.'S'));
  27. elseif($tz_offset < 0)
  28. $date->sub(new DateInterval('PT'.abs($tz_offset).'S'));
  29. $tz = ($tz_offset < 0 ? '-' : '+') . sprintf('%02d:%02d', abs($tz_offset/60/60), ($tz_offset/60)%60);
  30. return $date->format('Y-m-d H:i:s') . ' ' . $tz;
  31. }
  32. function build_url($parsed_url) {
  33. $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
  34. $host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
  35. $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
  36. $user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
  37. $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
  38. $pass = ($user || $pass) ? "$pass@" : '';
  39. $path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
  40. $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
  41. $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
  42. return "$scheme$user$pass$host$port$path$query$fragment";
  43. }
  44. // Input: Any URL or string like "aaronparecki.com"
  45. // Output: Normlized URL (default to http if no scheme, force "/" path)
  46. // or return false if not a valid URL
  47. function normalize_url($url) {
  48. $me = parse_url($url);
  49. if(array_key_exists('path', $me) && $me['path'] == '')
  50. return false;
  51. // parse_url returns just "path" for naked domains
  52. if(count($me) == 1 && array_key_exists('path', $me)) {
  53. $me['host'] = $me['path'];
  54. unset($me['path']);
  55. }
  56. if(!array_key_exists('scheme', $me))
  57. $me['scheme'] = 'http';
  58. if(!array_key_exists('path', $me))
  59. $me['path'] = '/';
  60. // Invalid scheme
  61. if(!in_array($me['scheme'], array('http','https')))
  62. return false;
  63. return build_url($me);
  64. }
  65. // Checks if a string is a valid URL that we recognize as a PuSH topic or callback.
  66. // Must be http or https
  67. // If there is no path, '/' is assumed
  68. function is_valid_push_url($url) {
  69. $url = parse_url($url);
  70. if(!array_key_exists('scheme', $url))
  71. return false;
  72. if(!in_array($url['scheme'], ['http','https']))
  73. return false;
  74. if(!array_key_exists('path', $url))
  75. $url['path'] = '/';
  76. return build_url($url);
  77. }
  78. function render($page, $data) {
  79. global $app;
  80. ob_start();
  81. $app->render('layout.php', array_merge($data, array('page' => $page)));
  82. return ob_get_clean();
  83. };
  84. function partial($template, $data=array(), $debug=false) {
  85. global $app;
  86. if($debug) {
  87. $tpl = new Savant3(\Slim\Extras\Views\Savant::$savantOptions);
  88. echo '<pre>' . $tpl->fetch($template . '.php') . '</pre>';
  89. return '';
  90. }
  91. ob_start();
  92. $tpl = new Savant3(\Slim\Extras\Views\Savant::$savantOptions);
  93. foreach($data as $k=>$v) {
  94. $tpl->{$k} = $v;
  95. }
  96. $tpl->display($template . '.php');
  97. return ob_get_clean();
  98. }
  99. function json_response(&$app, $response) {
  100. $app->response()['Content-Type'] = 'application/json';
  101. $app->response()->body(json_encode($response));
  102. }
  103. function session($key) {
  104. if(array_key_exists($key, $_SESSION))
  105. return $_SESSION[$key];
  106. else
  107. return null;
  108. }
  109. function k($a, $k, $default=null) {
  110. if(is_array($k)) {
  111. $result = true;
  112. foreach($k as $key) {
  113. $result = $result && array_key_exists($key, $a);
  114. }
  115. return $result;
  116. } else {
  117. if(is_array($a) && array_key_exists($k, $a) && $a[$k])
  118. return $a[$k];
  119. elseif(is_object($a) && property_exists($a, $k) && $a->$k)
  120. return $a->$k;
  121. else
  122. return $default;
  123. }
  124. }
  125. function relative_time($date) {
  126. static $rel;
  127. if(!isset($rel)) {
  128. $config = array(
  129. 'language' => '\RelativeTime\Languages\English',
  130. 'separator' => ', ',
  131. 'suffix' => true,
  132. 'truncate' => 1,
  133. );
  134. $rel = new \RelativeTime\RelativeTime($config);
  135. }
  136. return $rel->timeAgo($date);
  137. }
  138. function bs()
  139. {
  140. static $pheanstalk;
  141. if(!isset($pheanstalk)) {
  142. $pheanstalk = new Pheanstalk\Pheanstalk(Config::$beanstalkServer, Config::$beanstalkPort);
  143. }
  144. return $pheanstalk;
  145. }
  146. function pluralize($word, $num) {
  147. if($num == 1) {
  148. return $word;
  149. } else {
  150. return $word . 's';
  151. }
  152. }