Beispiel #1
0
 public function connect()
 {
     if ($this->flags & SOCK_PROXY) {
         \Cherry\debug('Proxied connection, creating proxy');
         $proxy = new \cherry\net\proxy\SocksProxy('127.0.0.1', 8088);
         $this->proxyConnect($proxy);
     }
 }
Beispiel #2
0
 public static function __php_handleAssert($file, $line, $code, $desc = null)
 {
     \Cherry\debug("Assertion failed in %s on line %d", $file, $line);
     $log = DebugLog::getDebugLog();
     $str = sprintf("in %s on line %d", $file, $line);
     $bt = Debug::getBacktrace(1);
     self::showError($ca, 'Assertion failed', $str, $file, $line, $log, $bt);
     exit(1);
 }
Beispiel #3
0
 protected function wnd($renew = false)
 {
     if ($renew) {
         if ($this->window) {
             ncurses_delwin($this->window);
         }
         $this->window = null;
     }
     if (!$this->window) {
         $this->window = ncurses_newwin($this->height, $this->width, $this->top, $this->left);
         \Cherry\debug('New window: %dx%d+%d+%d', $this->left, $this->top, $this->width, $this->height);
     }
     return $this->window;
 }
Beispiel #4
0
 public function checkRoute(Request $request)
 {
     \Cherry\debug('Checking static route for request: %s', $request);
     $ru = explode('/', $request->getRequestUrl());
     foreach ($this->routes as $url => $target) {
         $call_controller = $target[0];
         $call_method = 'index';
         $call_args = array();
         $uu = explode('/', $url);
         $matched = true;
         for ($n = 0; $n < count($ru); $n++) {
             if ($uu[$n] == '') {
                 //
             } else {
                 if ($uu[$n][0] == ':') {
                     switch ($uu[$n]) {
                         case ':method':
                             $call_method = $ru[$n];
                             break;
                         case ':controller':
                             $call_controller = $ru[$n];
                             break;
                         case ':args':
                             $call_args = array_slice($ru, $n + 1);
                             break;
                     }
                 } else {
                     if ($ru[$n] != $uu[$n]) {
                         $matched = false;
                         break;
                     }
                 }
             }
         }
         if ($matched) {
             \Cherry\debug('Request %s matched static route %s', $request, $url);
             // Extract method from the call if present, and if not use call_method
             list($cclass, $cmethod) = explode(':', $call_controller . ':' . $call_method);
             \Cherry\debug('Controller: %s, Action: %s', $cclass, $cmethod);
             $ci = \Cherry\Mvc\Controller\Base::factory($cclass, $request);
             $ci->method = $cmethod;
             $ci->args = $call_args;
             return $ci;
         }
     }
 }
Beispiel #5
0
 public function draw()
 {
     static $drawqueue = array();
     if ($this->resized) {
         \Cherry\debug("Viewport has been resized, recalculating layout...");
         $alloc = 0;
         $numfill = 0;
         $layout = explode(',', $this->layout);
         for ($n = 0; $n < count($layout); $n++) {
             if ($layout[$n] == -1) {
                 $numfill++;
             } else {
                 $alloc += $layout[$n];
             }
         }
         if ($numfill > 0) {
             $sizefill = ($this->height - $alloc) / $numfill;
         }
         $keys = explode(',', $this->keys);
         $drawqueue = array();
         for ($n = 0; $n < count($layout); $n++) {
             if ($layout[$n] == -1) {
                 $drawqueue[$keys[$n]] = $sizefill;
             } else {
                 $drawqueue[$keys[$n]] = $layout[$n];
             }
         }
     }
     $row = 0;
     foreach ($drawqueue as $key => $height) {
         $this->curlayout[$key] = $height;
         $ctl = $this->{$key};
         if ($ctl) {
             if ($this->resized) {
                 $ctl->moveTo(0, $row, $this->width, $height);
             }
             $ctl->draw();
         }
         $row += $height;
     }
     $this->resized = false;
 }
Beispiel #6
0
 public function listen($bind = '*', $port = null)
 {
     if (!$port) {
         $port = $this->port;
     }
     if ($this->flags & SOCK_PROXY) {
         \Cherry\debug('Proxied connection, creating proxy');
         $proxy = new \cherry\net\proxy\SocksProxy('127.0.0.1', 8088);
         $this->proxyListen($proxy);
     } else {
         $this->hsocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
         if (!socket_bind($this->hsocket, $bind == '*' ? 0 : $bind, $this->port)) {
             throw new SocketException("Call to socket_bind failed.");
         }
         if (!socket_listen($this->hsocket)) {
             throw new SocketException("Call to socket_listen failed.");
         }
         $this->setState('listening');
     }
 }
Beispiel #7
0
 function __destruct()
 {
     $dur = microtime(true) - $this->tstart;
     \Cherry\debug('%s: %fus', $this->info, $dur);
 }
Beispiel #8
0
 private function setCacheRecord()
 {
     assert(!empty($this->objecturi));
     list($type, $cache, $id) = explode(':', $this->objecturi);
     if ($this->state != self::CS_UPDATED) {
         \Cherry\debug("Cache entry not updated for {$id}");
         return;
     }
     \Cherry\debug("Updating cache entry for {$id}");
     $key_cache = 'entry:' . $id;
     $key_meta = 'meta:' . $id;
     if (!$this->expires) {
         $this->expires = App::config()->get('cache.default-expiry', '30m');
     }
     if ($type == 'cache') {
         if ($cache == 'disk') {
             // Check cache dir for object
             $path = $this->cachepath;
             if (!is_dir($path) && !mkdir($path)) {
                 user_error("Cache directory {$path} does not exist and mkdir failed!");
             }
             $header = ['content-type' => $this->contenttype, 'content-length' => strlen($this->content), 'expires' => Duration::toSeconds($this->expires, time())];
             if ($this->flags & self::CO_COMPRESS) {
                 $header['compressed'] = 'gzip';
                 $content = gzcompress($this->content);
             } else {
                 $content = $this->content;
             }
             $blob = serialize($header) . _CACHE_SEP_ . $content;
             file_put_contents($path . _DS_ . $id, $blob);
         } elseif ($cache == 'ram') {
             // check memcache
         } elseif ($cache == 'auto') {
             // check memcache for metadata, then read entry from disk or
             // memcache.
             $max_blob = App::config()->get('cache.max-blob-size');
         }
     }
 }