public function dispatch()
 {
     $url = $this->system->getConfig()->getRequestUrl();
     $url = preg_replace("!\\?.*\$!", '', $url);
     $url = urldecode($url);
     $r = $this->dispatchRoutes($this->getRoutes(), $url);
     if (!$r || $r['url'] == '*' && $url && strpos(substr($url, -5), '.') === false && substr($url, -1) !== '/') {
         $r2 = $this->dispatchRoutes($this->getRoutes(), $url . '/');
         if ($r2 && (!$r || $r2['url'] != '*')) {
             $this->system->getResponse()->redirect($this->system->getRootUrl() . $url . '/');
         }
     }
     // if route found and app exists
     if ($r && isset($r['app']) && $r['app'] && $this->system->appExists($r['app'])) {
         $this->setRoute($r);
         // dispatch app routes
         $params = waRequest::param();
         $u = $r['url'];
         if (preg_match_all('/<([a-z_]+):?([^>]*)?>/ui', $u, $match, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
             $offset = 0;
             foreach ($match as $m) {
                 $v = $m[1][0];
                 $s = isset($params[$v]) ? $params[$v] : '';
                 $u = substr($u, 0, $m[0][1] + $offset) . $s . substr($u, $m[0][1] + $offset + strlen($m[0][0]));
                 $offset += strlen($s) - strlen($m[0][0]);
             }
         }
         $this->root_url = self::clearUrl($u);
         $url = substr($url, strlen($this->root_url));
         $this->dispatchRoutes($this->getAppRoutes($r['app'], $r), $url);
     }
     return $r;
 }
 public function block($id)
 {
     if ($id && $this->wa->appExists('site')) {
         wa('site');
         $model = new siteBlockModel();
         $block = $model->getById($id);
         if (!$block && strpos($id, '.') !== false) {
             list($app_id, $id) = explode('.', $id);
             $path = $this->wa->getConfig()->getAppsPath($app_id, 'lib/config/site.php');
             if (file_exists($path)) {
                 $site_config = (include $path);
                 if (isset($site_config['blocks'][$id])) {
                     if (!is_array($site_config['blocks'][$id])) {
                         $block = array('content' => $site_config['blocks'][$id]);
                     } else {
                         $block = $site_config['blocks'][$id];
                     }
                 }
             }
         }
         if ($block) {
             try {
                 return $this->view->fetch('string:' . $block['content']);
             } catch (Exception $e) {
                 if (waSystemConfig::isDebug()) {
                     return '<pre class="error">' . htmlentities($e->getMessage(), ENT_QUOTES, 'utf-8') . "</pre>";
                 } else {
                     waLog::log($e->__toString());
                     return '<div class="error">' . _ws('Syntax error at block') . ' ' . $id . '</div>';
                 }
             }
         }
     }
     return '';
 }