示例#1
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();
 }
示例#2
0
function gp_tmpl_filter_args($args)
{
    $clean_args = array();
    foreach ($args as $k => $v) {
        if ($k[0] != '_' && $k != 'GLOBALS' && !gp_startswith($k, 'HTTP') && !gp_startswith($k, 'PHP')) {
            $clean_args[$k] = $v;
        }
    }
    return $clean_args;
}
示例#3
0
文件: misc.php 项目: rmccue/GlotPress
function gp_populate_notices()
{
    GP::$redirect_notices = array();
    $prefix = '_gp_notice_';
    foreach ($_COOKIE as $key => $value) {
        if (gp_startswith($key, $prefix) && ($suffix = substr($key, strlen($prefix)))) {
            GP::$redirect_notices[$suffix] = $value;
        }
        setcookie($key, '', 0, gp_url_path());
    }
}
示例#4
0
文件: url.php 项目: rmccue/GlotPress
/**
 * Joins paths, and takes care of slashes between them
 */
function gp_url_join()
{
    $args = func_get_args();
    // we need array_values() in order to make sure the indices of $args are consecutive from 0 to count()-1
    $args = array_values(array_filter(gp_array_flatten($args)));
    if (empty($args)) {
        return '';
    }
    $start_slash = gp_startswith($args[0], '/') && trim($args[0], '/') != '' ? '/' : '';
    $end_slash = gp_endswith($args[count($args) - 1], '/') && trim($args[count($args) - 1], '/') != '' ? '/' : '';
    $args = array_map(create_function('$x', 'return trim($x, "/");'), $args);
    return $start_slash . implode('/', $args) . $end_slash;
}
 private function delTree($dir)
 {
     if (!gp_startswith($dir, sys_get_temp_dir())) {
         return false;
     }
     $files = array_diff(scandir($dir), array('.', '..'));
     foreach ($files as $file) {
         if (is_dir("{$dir}/{$file}")) {
             $this->delTree("{$dir}/{$file}");
         } else {
             unlink("{$dir}/{$file}");
         }
     }
     return rmdir($dir);
 }
示例#6
0
 public function route()
 {
     $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);
     }
     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)) {
                 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();
                     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();
 }
示例#7
0
 function login_post()
 {
     global $wp_users_object, $wp_auth_object;
     $user = GP::$user->by_login($_POST['user_login']);
     if (!$user || is_wp_error($user)) {
         $this->errors[] = __("Invalid username!");
         gp_redirect(gp_url('/login'));
         return;
     }
     if ($user->login($_POST['user_pass'])) {
         if (gp_post('redirect_to') && gp_startswith(gp_post('redirect_to'), gp_url_base())) {
             gp_redirect(gp_post('redirect_to'));
         } else {
             $this->notices[] = sprintf(__("Welcome, %s!"), $_POST['user_login']);
             gp_redirect(gp_url('/'));
         }
     } else {
         $this->errors[] = __("Invalid password!");
         gp_redirect(gp_url('/login'));
     }
 }
示例#8
0
 /**
  * Adds all methods starting with warning_ to $translation_warnings
  */
 public function add_all(&$translation_warnings)
 {
     $warnings = array_filter(get_class_methods(get_class($this)), function ($key) {
         return gp_startswith($key, 'warning_');
     });
     foreach ($warnings as $warning) {
         $translation_warnings->add(str_replace('warning_', '', $warning), array($this, $warning));
     }
 }
示例#9
0
 function warning_both_begin_end_on_newlines($original, $translation, $locale)
 {
     if (gp_endswith($original, "\n") xor gp_endswith($translation, "\n")) {
         return 'Original and translation should both end on newline.';
     }
     if (gp_startswith($original, "\n") xor gp_startswith($translation, "\n")) {
         return 'Original and translation should both begin on newline.';
     }
     return true;
 }
 function assertRedirect()
 {
     $this->assertTrue(gp_startswith($this->body, 'Redirecting to: '));
 }
示例#11
0
function gp_populate_notices()
{
    GP::$redirect_notices = array();
    $prefix = '_gp_notice_';
    $cookie_path = '/' . ltrim(gp_url_path(), '/');
    // Make sure that the cookie path is never empty.
    foreach ($_COOKIE as $key => $value) {
        if (gp_startswith($key, $prefix) && ($suffix = substr($key, strlen($prefix)))) {
            GP::$redirect_notices[$suffix] = wp_unslash($value);
            gp_set_cookie($key, '', 0, $cookie_path);
        }
    }
}
示例#12
0
 function approve_post($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_redirect('approve', 'translation-set', $translation_set->id);
     $bulk = gp_post('bulk');
     $action = gp_startswith($bulk['action'], 'approve-') ? 'approve' : 'reject';
     $bulk['translation-ids'] = array_filter(explode(',', $bulk['translation-ids']));
     if (empty($bulk['translation-ids'])) {
         $this->errors[] = 'No translations were supplied.';
     }
     $ok = $error = 0;
     $method_name = 'approve' == $action ? 'set_as_current' : 'reject';
     foreach ($bulk['translation-ids'] as $translation_id) {
         $translation = GP::$translation->get($translation_id);
         if (!$translation) {
             continue;
         }
         if ($translation->{$method_name}()) {
             $ok++;
         } else {
             $error++;
         }
     }
     // TODo: refactor this, out in another method
     if (0 === $error) {
         $this->notices[] = 'approve' == $action ? sprintf(_n('One translation approved.', '%d translations approved.', $ok), $ok) : sprintf(_n('One translation rejected.', '%d translations rejected.', $ok), $ok);
     } else {
         if ($ok > 0) {
             $message = 'approve' == $action ? sprintf(_n('Error with approving one translation.', 'Error with approving %s translations.', $error), $error) : sprintf(_n('Error with rejecting one translation.', 'Error with rejecting %s translations.', $error), $error);
             $message .= ' ';
             $message .= 'approve' == $action ? sprintf(_n('The remaining translation was approved successfully.', 'The remaining %s translations were approved successfully.', $ok), $ok) : sprintf(_n('The remaining translation was rejected successfully.', 'The remaining %s translations were rejected successfully.', $ok), $ok);
         } else {
             $this->errors[] = 'approve' == $action ? sprintf(_n('Error with approving the translation.', 'Error with approving all %s translation.', $error), $error) : sprintf(_n('Error with rejecting the translation.', 'Error with rejecting all %s translation.', $error), $error);
         }
     }
     wp_cache_delete($translation_set->id, 'translation_set_status_breakdown');
     // hack, until we make clean_url() to allow [ and ]
     $bulk['redirect_to'] = str_replace(array('[', ']'), array_map('urlencode', array('[', ']')), $bulk['redirect_to']);
     gp_redirect($bulk['redirect_to']);
 }
示例#13
0
		</div>
		<div class="meta">
			<h3>Meta</h3>
			<dl>
				<dt>Status:</dt>
				<dd><?php 
echo display_status($t->translation_status);
?>
</dd>
				<!--
				TODO: ajaxy actions for approve/set as current/reject
			<?php 
if ($can_approve) {
    ?>
				<?php 
    if (gp_startswith($t->translation_status, '-')) {
        ?>
				<dd><a href="#" tabindex="-1">Set as current</a></dd>
				<?php 
    }
    ?>
				<?php 
    if ($t->translation_status) {
        ?>
				<dd><a href="#" tabindex="-1">Reject</a></dd>
				<?php 
    }
    ?>
			<?php 
}
?>
示例#14
0
 /**
  * Escapes a string with c style slashes and html entities as required.
  *
  * @since 1.0.0
  *
  * @param string $string The string to escape.
  *
  * @return string Returns the escaped string.
  */
 protected function escape($string)
 {
     $string = addcslashes($string, "'\n");
     $string = str_replace(array('&', '<'), array('&amp;', '&lt;'), $string);
     // Android strings that start with an '@' are references to other strings and need to be escaped.  See GH469.
     if (gp_startswith($string, '@')) {
         $string = '\\' . $string;
     }
     return $string;
 }
示例#15
0
 /**
  * Splits a properties file line on the = or : character.
  *
  * Skips escaped values (\= or \:) in the key and matches the first unescaped instance.
  *
  * @since 2.0.0
  *
  * @param string $line  The line to split.
  * @param string $key   The key part of the properties file string if found.
  * @param string $value The value part of the properties file string if found.
  *
  * @return bool Returns true if the line was split successfully, false otherwise.
  */
 private function split_properties_line($line, &$key, &$value)
 {
     // Make sure to reset the key/value before continuing.
     $key = '';
     $value = '';
     // Split the string on any = or :, get back where the string was split.
     $matches = preg_split('/[=|:]/', $line, null, PREG_SPLIT_OFFSET_CAPTURE);
     // Check the number of matches.
     $num_matches = sizeof($matches);
     // There's always one match (the entire line) so if we matched more than one, let's see if we can split the line.
     if ($num_matches > 1) {
         // Loop through the matches, starting at the second one.
         for ($i = 1; $i < $num_matches; $i++) {
             // Get the location of the current match.
             $location = $matches[$i][1];
             // If the location of the separator is the first character of the string it's an invalid location so skip it.
             if ($location < 2) {
                 continue;
             }
             // If the character before it (-2 as the separator character is still part of the match)
             // is an escape, we don't have a match yet.
             if ('\\' != $line[$location - 2]) {
                 // Set the return values for the key and value.
                 $key = substr($line, 0, $location - 1);
                 $value = substr($line, $location);
                 // Handle the special case where the separator is actually " = " or " : ".
                 if (gp_endswith($key, ' ') && gp_startswith($value, ' ')) {
                     $key = substr($key, 0, -1);
                     $value = substr($value, 1);
                 }
                 return true;
             }
         }
     }
     // Return false since we didn't find a valid line to split.
     return false;
 }