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.

80 lines
1.9 KiB

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