public function execute()
 {
     $domain = siteHelper::getDomain();
     $routes = wa()->getRouting()->getRoutes($domain);
     $route_id = waRequest::post('route');
     if (isset($routes[$route_id])) {
         if (isset($routes[$route_id]['app'])) {
             $robots = new siteRobots($domain);
             $robots->delete($routes[$route_id]['app'], $routes[$route_id]['url']);
         }
         unset($routes[$route_id]);
         // save
         $path = $this->getConfig()->getPath('config', 'routing');
         $all_routes = file_exists($path) ? include $path : array();
         $all_routes[$domain] = $routes;
         waUtils::varExportToFile($all_routes, $path);
         $this->log('route_delete');
     }
 }
 public function execute()
 {
     $domain = siteHelper::getDomain();
     $routes = wa()->getRouting()->getRoutes($domain);
     $route_id = waRequest::post('route');
     if (isset($routes[$route_id])) {
         if (isset($routes[$route_id]['app'])) {
             $robots = new siteRobots($domain);
             $robots->delete($routes[$route_id]['app'], $routes[$route_id]['url']);
         }
         unset($routes[$route_id]);
         // save
         $path = $this->getConfig()->getPath('config', 'routing');
         $all_routes = file_exists($path) ? include $path : array();
         $all_routes[$domain] = $routes;
         if (!waUtils::varExportToFile($all_routes, $path)) {
             $this->errors = sprintf(_w('Settings could not be saved due to the insufficient file write permissions for the file "%s".'), 'wa-config/routing.php');
         } else {
             $this->logAction('route_delete');
         }
     }
 }
 public function execute()
 {
     $path = $this->getConfig()->getPath('config', 'routing');
     if (file_exists($path)) {
         $routes = (include $path);
         if (!is_writable($path)) {
             $this->errors = sprintf(_w('Settings could not be saved due to the insufficient file write permissions for the file "%s".'), 'wa-config/routing.php');
             return;
         }
     } else {
         $routes = array();
     }
     $domain = siteHelper::getDomain();
     $route_id = waRequest::get('route', '');
     // new route
     if (!strlen($route_id)) {
         $route = $this->getRoute();
         if (!empty($route['app'])) {
             if (!$route['url']) {
                 $route['url'] = '*';
             }
             $route_id = $this->getRouteId(isset($routes[$domain]) ? $routes[$domain] : array());
             if ($route['url'] == '*') {
                 $routes[$domain][$route_id] = $route;
                 $this->response['add'] = 'bottom';
             } else {
                 if (strpos($route['url'], '*') === false) {
                     if (substr($route['url'], -1) == '/') {
                         $route['url'] .= '*';
                     } elseif (substr($route['url'], -1) != '*' && strpos(substr($route['url'], -5), '.') === false) {
                         $route['url'] .= '/*';
                     }
                 }
                 $routes[$domain] = array($route_id => $route) + (isset($routes[$domain]) ? $routes[$domain] : array());
                 $this->response['add'] = 'top';
             }
             // save
             waUtils::varExportToFile($routes, $path);
             // add robots
             $robots = new siteRobots($domain);
             $robots->add($route['app'], $route['url']);
             // log
             $this->log('route_add');
         } elseif (isset($route['redirect'])) {
             if ($route['url'] && substr($route['url'], -1) != '*' && substr($route['url'], -1) != '/' && strpos(substr($route['url'], -5), '.') === false) {
                 $route['url'] .= '/';
             }
             if (!$route['redirect']) {
                 $route['redirect'] = '/';
             }
             if (substr($route['redirect'], -1) != '*' && substr($route['redirect'], -1) != '/' && strpos(substr($route['redirect'], -5), '.') === false) {
                 $route['redirect'] .= '/';
             }
             $route_id = $this->getRouteId($routes[$domain]);
             $routes[$domain] = array($route_id => $route) + $routes[$domain];
             $this->response['add'] = 'top';
             // save
             waUtils::varExportToFile($routes, $path);
             // log
             $this->log('route_add');
         }
         $html = '<tr id="route-' . $route_id . '">
                     <td class="s-url">
                         <span><a style="display:inline" href="#"><i class="icon16 sort"></i></a></span> <span class="s-domain-url">' . $domain . '/</span><span class="s-editable-url" style="color:#000">' . htmlspecialchars($route['url']) . '</span>
                     </td>
                     <td class="s-app' . (!empty($route['private']) ? ' gray' : '') . '">';
         $root_url = wa()->getRootUrl();
         if (!empty($route['app'])) {
             $app = wa()->getAppInfo($route['app']);
             $html .= '<img src="' . $root_url . $app['icon'][24] . '" class="s-app24x24icon-menu-v" alt="">' . $app['name'];
         } else {
             $html .= '<img src="' . $root_url . 'wa-apps/site/img/arrow.png" class="s-app24x24icon-menu-v" alt="">
                             <span class="redirect">' . htmlspecialchars($route['redirect']) . '</span>';
         }
         $html .= '</td>
                     <td class="s-actions align-right">
                         <a href="#" class="s-route-action s-route-settings" title="' . _w('Settings') . '"><i class="icon16 settings"></i></a>
                     </td>
                 </tr>';
         $this->response['html'] = $html;
     } else {
         $old = $routes[$domain][$route_id];
         $new = $this->getRoute($old);
         if (waRequest::post('correct_url') && strpos($new['url'], '*') === false) {
             if (!$new['url']) {
                 $new['url'] = '*';
             } elseif (substr($new['url'], -1) == '/') {
                 $new['url'] .= '*';
             } elseif (substr($new['url'], -1) != '*' && strpos(substr($new['url'], -5), '.') === false) {
                 $new['url'] .= '/*';
             }
         }
         if ($new['url'] != $old['url'] || ifset($new['theme']) != ifset($old['theme']) || ifset($new['theme_mobile']) != ifset($old['theme_mobile'])) {
             $this->response['change'] = 1;
         }
         if ($replace_id = waRequest::get('replace')) {
             $this->response['delete'] = $replace_id;
             $tmp = array();
             foreach ($routes[$domain] as $r_id => $r) {
                 if ($r_id == $replace_id) {
                     $tmp[$route_id] = $new;
                 } else {
                     $tmp[$r_id] = $r;
                 }
             }
             $routes[$domain] = $tmp;
         } else {
             foreach ($routes[$domain] as $r_id => $r) {
                 if (isset($r['app']) && $r_id != $route_id && $r['url'] == $new['url']) {
                     $old_app = $r['app'] ? wa()->getAppInfo($r['app']) : array();
                     $old_app = ifset($old_app['name']);
                     $new_app = $new['app'] ? wa()->getAppInfo($new['app']) : array();
                     $new_app = ifset($new_app['name']);
                     $this->response['confirm'] = sprintf(_w('The URL %s is already used by %s app. If you proceed, this will replace %s app with %s app on this URL.'), $new['url'], $old_app, $old_app, $new_app);
                     $this->response['replace'] = $r_id;
                     return;
                 }
             }
             $routes[$domain][$route_id] = $new;
         }
         // save
         waUtils::varExportToFile($routes, $path);
         $this->response['url'] = $routes[$domain][$route_id]['url'];
         $this->response['private'] = !empty($routes[$domain][$route_id]['private']);
         if (isset($routes[$domain][$route_id]['redirect'])) {
             $this->response['redirect'] = $routes[$domain][$route_id]['redirect'];
         }
         if (!isset($routes[$domain][$route_id]['redirect']) && $routes[$domain][$route_id]['url'] != $old['url']) {
             // update pages
             $this->updatePagesRoute($old, $routes[$domain][$route_id]['url']);
             // update robots
             $robots = new siteRobots($domain);
             $robots->update($routes[$domain][$route_id]['app'], $old['url'], $routes[$domain][$route_id]['url']);
         }
         // log
         $this->log('route_edit');
     }
 }
 public function execute()
 {
     $domain = siteHelper::getDomain();
     $routes = wa()->getRouting()->getRoutes($domain);
     $url = waRequest::post('url');
     $app_id = waRequest::post('app');
     if ($app_id) {
         if (!$url) {
             $url = '*';
         }
         $route_id = $this->getRouteCount($routes);
         $route = array('url' => $url, 'app' => $app_id);
         $app_info = wa()->getAppInfo($app_id);
         if (isset($app_info['routing_params']) && is_array($app_info['routing_params'])) {
             foreach ($app_info['routing_params'] as $k => $v) {
                 $route[$k] = $v;
             }
         }
         if ($url == '*') {
             $routes[$route_id] = $route;
         } else {
             if (substr($url, -1) == '/') {
                 $route['url'] .= '*';
             } elseif (substr($url, -1) != '*' && strpos(substr($url, -5), '.') === false) {
                 $route['url'] .= '/*';
             }
             $routes = array($route_id => $route) + $routes;
         }
         $this->response['app'] = array('id' => $app_id, 'icon' => $app_info['icon'], 'name' => $app_info['name']);
         $this->response['route'] = $route_id;
         $this->response['url'] = htmlspecialchars($route['url']);
         $this->save($domain, $routes);
         $robots = new siteRobots($domain);
         $robots->add($app_id, $route['url']);
         $this->log('route_add');
     } elseif (($redirect = waRequest::post('redirect')) !== null) {
         $route = waRequest::post('route');
         if ($route !== null) {
             $routes[$route]['url'] = $url;
             $routes[$route]['redirect'] = $redirect;
         } else {
             if (substr($url, -1) != '*' && substr($url, -1) != '/' && strpos(substr($url, -5), '.') === false) {
                 $url .= '/';
             }
             if (!$redirect) {
                 $redirect = '/';
             }
             if (substr($redirect, -1) != '*' && substr($redirect, -1) != '/' && strpos(substr($redirect, -5), '.') === false) {
                 $redirect .= '/';
             }
             $route = $this->getRouteCount($routes);
             $routes = array($route => array('url' => $url, 'redirect' => $redirect)) + $routes;
         }
         $this->response['route'] = $route;
         $this->response['url'] = htmlspecialchars($url);
         $this->response['redirect'] = htmlspecialchars($redirect);
         $this->save($domain, $routes);
         $this->log('route_edit');
     } elseif (($route = waRequest::post('route')) !== null) {
         if (isset($routes[$route]) && $routes[$route]['url'] != $url) {
             $this->updatePagesRoute($routes[$route], $url);
             $robots = new siteRobots($domain);
             $robots->update($routes[$route]['app'], $routes[$route]['url'], $url);
             $routes[$route]['url'] = $url;
             $this->save($domain, $routes);
             $this->log('route_edit');
         }
         $this->response['route'] = $route;
         $this->response['url'] = htmlspecialchars($url);
     }
 }