Ejemplo n.º 1
0
 function route()
 {
     $request_uri = $this->request_uri();
     $request_method = strtolower($this->request_method());
     foreach ($this->urls as $re => $func) {
         foreach (array('get', 'post', 'head', 'put', 'delete') as $http_method) {
             if (gp_startswith($re, $http_method . ':')) {
                 if ($http_method != $request_method) {
                     continue;
                 }
                 $re = substr($re, strlen($http_method . ':'));
                 break;
             }
         }
         if (preg_match("@^{$re}\$@", $request_uri, $matches)) {
             if (is_array($func)) {
                 list($class, $method) = $func;
                 $route = new $class();
                 $route->last_method_called = $method;
                 $route->class_name = $class;
                 GP::$current_route =& $route;
                 $route->before_request();
                 $route->request_running = true;
                 // make sure after_request() is called even if we exit() in the request
                 register_shutdown_function(array(&$route, 'after_request'));
                 call_user_func_array(array($route, $method), array_slice($matches, 1));
                 $route->after_request();
                 do_action('after_request', $class, $method);
                 $route->request_running = false;
             } else {
                 call_user_func_array($func, array_slice($matches, 1));
             }
             return;
         }
     }
     return gp_tmpl_404();
 }
Ejemplo n.º 2
0
 function delete_get($project_path)
 {
     // TODO: do not delete using a GET request but POST
     // TODO: decide what to do with child projects and translation sets
     // TODO: just deactivate, do not actually delete
     $project = GP::$project->by_path($project_path);
     if (!$project) {
         gp_tmpl_404();
     }
     $this->can_or_redirect('write', 'project', $project->id);
     if ($project->delete()) {
         $this->notices[] = __('The project was deleted.');
     } else {
         $this->errors[] = __('Error in deleting project!');
     }
     gp_redirect(gp_url_project(''));
 }
Ejemplo n.º 3
0
 public function route()
 {
     global $wp_query;
     $real_request_uri = $this->request_uri();
     $api_request_uri = $real_request_uri;
     $request_method = strtolower($this->request_method());
     $api = gp_startswith($real_request_uri, '/' . $this->api_prefix . '/');
     if ($api) {
         $real_request_uri = substr($real_request_uri, strlen($this->api_prefix) + 1);
     }
     $url_path = gp_url_path(gp_url_public_root());
     // If the request URL doesn't match our base URL, don't bother trying to match
     if ($url_path && !gp_startswith($_SERVER['REQUEST_URI'], $url_path)) {
         return;
     }
     foreach (array($api_request_uri, $real_request_uri) as $request_uri) {
         foreach ($this->urls as $re => $func) {
             foreach (array('get', 'post', 'head', 'put', 'delete') as $http_method) {
                 if (gp_startswith($re, $http_method . ':')) {
                     if ($http_method != $request_method) {
                         continue;
                     }
                     $re = substr($re, strlen($http_method . ':'));
                     break;
                 }
             }
             if (preg_match("@^{$re}\$@", $request_uri, $matches)) {
                 /*
                  * WordPress will be returning a 404 status header by default for GlotPress pages
                  * as nothing is found by WP_Query.
                  * This overrides the status header and the `$is_404` property of WP_Query if we've matched
                  * something here. Route controllers still can return a 404 status header.
                  */
                 status_header('200');
                 $wp_query->is_404 = false;
                 if (is_array($func)) {
                     list($class, $method) = $func;
                     $route = new $class();
                     $route->api = $api;
                     $route->last_method_called = $method;
                     $route->class_name = $class;
                     GP::$current_route =& $route;
                     $route->before_request();
                     $route->request_running = true;
                     // make sure after_request() is called even if we $this->exit_() in the request
                     register_shutdown_function(array(&$route, 'after_request'));
                     call_user_func_array(array($route, $method), array_slice($matches, 1));
                     $route->after_request();
                     $route->request_running = false;
                 } else {
                     call_user_func_array($func, array_slice($matches, 1));
                 }
                 exit;
             }
         }
     }
     gp_tmpl_404();
 }
Ejemplo n.º 4
0
 function discard_warning($project_path, $locale_slug, $translation_set_slug)
 {
     $project = GP::$project->by_path($project_path);
     $locale = GP_Locales::by_slug($locale_slug);
     $translation_set = GP::$translation_set->by_project_id_slug_and_locale($project->id, $translation_set_slug, $locale_slug);
     if (!$project || !$locale || !$translation_set) {
         gp_tmpl_404();
     }
     $this->can_or_forbidden('write', 'project', $project->id);
     $translation = GP::$translation->get(gp_post('translation_id'));
     if (!$translation) {
         $this->die_with_error('Translation doesn’t exist!');
     }
     if (!isset($translation->warnings[gp_post('index')][gp_post('key')])) {
         $this->die_with_error('The warning doesn’exist!');
     }
     unset($translation->warnings[gp_post('index')][gp_post('key')]);
     if (empty($translation->warnings[gp_post('index')])) {
         unset($translation->warnings[gp_post('index')]);
     }
     $res = $translation->save();
     if (!$res) {
         $this->die_with_error('Error in saving the translation!');
     }
     $translations = GP::$translation->for_translation($project, $translation_set, 'no-limit', array('translation_id' => gp_post('translation_id')), array());
     if ($translations) {
         $t = $translations[0];
         $parity = returner('even');
         $can_edit = GP::$user->logged_in();
         $can_approve = $this->can('approve', 'translation-set', $translation_set->id);
         gp_tmpl_load('translation-row', get_defined_vars());
     } else {
         $this->die_with_error('Error in retrieving translation!');
     }
 }