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.

52 lines
1.3 KiB

9 years ago
9 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. ORM::configure('mysql:host=' . Config::$db['host'] . ';dbname=' . Config::$db['database']);
  9. ORM::configure('username', Config::$db['username']);
  10. ORM::configure('password', Config::$db['password']);
  11. function view($template, $data=[]) {
  12. global $templates;
  13. return $templates->render($template, $data);
  14. }
  15. function q() {
  16. static $caterpillar = false;
  17. if(!$caterpillar) {
  18. $logdir = __DIR__.'/../scripts/logs/';
  19. $caterpillar = new Caterpillar('telegraph', '127.0.0.1', 11300, $logdir);
  20. }
  21. return $caterpillar;
  22. }
  23. function redis() {
  24. static $client = false;
  25. if(!$client)
  26. $client = new Predis\Client('tcp://127.0.0.1:6379');
  27. return $client;
  28. }
  29. function random_string($len) {
  30. $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  31. $str = '';
  32. $c = strlen($charset)-1;
  33. for($i=0; $i<$len; $i++) {
  34. $str .= $charset[mt_rand(0, $c)];
  35. }
  36. return $str;
  37. }
  38. function display_url($url) {
  39. return preg_replace(['/^https?:\/\//','/\/$/'], '', $url);
  40. }
  41. function session($k, $default=null) {
  42. if(!isset($_SESSION)) return $default;
  43. return array_key_exists($k, $_SESSION) ? $_SESSION[$k] : $default;
  44. }