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.

60 lines
1.6 KiB

8 years ago
8 years ago
  1. <?php
  2. date_default_timezone_set('UTC');
  3. if(array_key_exists('ENV', $_ENV)) {
  4. require(dirname(__FILE__).'/../config.'.$_ENV['ENV'].'.php');
  5. } else {
  6. require(dirname(__FILE__).'/../config.php');
  7. }
  8. function initdb() {
  9. ORM::configure('mysql:host=' . Config::$db['host'] . ';dbname=' . Config::$db['database']);
  10. ORM::configure('username', Config::$db['username']);
  11. ORM::configure('password', Config::$db['password']);
  12. }
  13. function view($template, $data=[]) {
  14. global $templates;
  15. return $templates->render($template, $data);
  16. }
  17. function q() {
  18. static $caterpillar = false;
  19. if(!$caterpillar) {
  20. $logdir = __DIR__.'/../scripts/logs/';
  21. $caterpillar = new Caterpillar('telegraph', '127.0.0.1', 11300, $logdir);
  22. }
  23. return $caterpillar;
  24. }
  25. function redis() {
  26. static $client = false;
  27. if(!$client)
  28. $client = new Predis\Client('tcp://127.0.0.1:6379');
  29. return $client;
  30. }
  31. function random_string($len) {
  32. $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  33. $str = '';
  34. $c = strlen($charset)-1;
  35. for($i=0; $i<$len; $i++) {
  36. $str .= $charset[mt_rand(0, $c)];
  37. }
  38. return $str;
  39. }
  40. // Returns true if $needle is the end of the $haystack
  41. function str_ends_with($haystack, $needle) {
  42. if($needle == '' || $haystack == '') return false;
  43. return strpos(strrev($haystack), strrev($needle)) === 0;
  44. }
  45. function display_url($url) {
  46. return preg_replace(['/^https?:\/\//','/\/$/'], '', $url);
  47. }
  48. function session($k, $default=null) {
  49. if(!isset($_SESSION)) return $default;
  50. return array_key_exists($k, $_SESSION) ? $_SESSION[$k] : $default;
  51. }