$value) {
$var[$key] = qs_base64_serialize($value);
}
}
$var = serialize($var);
$var = strtr(base64_encode($var), '-_,', '+/=');
return $var;
}
function qs_base64_unserialize($var) {
$var = base64_decode(strtr($var, '-_,', '+/='));
$var = unserialize($var);
if(is_array($var)) {
foreach($var as $key => $value) {
$var[$key] = qs_base64_unserialize($value);
}
}
return $var;
}
// sends a encrypted message to qTranslate Services and decrypts the received data
function qs_queryQS($action, $data='', $fast = false) {
global $qs_public_key;
// generate new private key
$key = openssl_pkey_new();
openssl_pkey_export($key, $private_key);
$public_key=openssl_pkey_get_details($key);
$public_key=$public_key["key"];
$message = qs_base64_serialize(array('key'=>$public_key, 'data'=>$data));
openssl_seal($message, $message, $server_key, array($qs_public_key));
$message = qs_base64_serialize(array('key'=>$server_key[0], 'data'=>$message));
$data = "message=".$message;
// connect to qts
if($fast) {
$fp = fsockopen('www.qianqin.de', 80, $errno, $errstr, QS_FAST_TIMEOUT);
stream_set_timeout($fp, QS_FAST_TIMEOUT);
} else {
$fp = fsockopen('www.qianqin.de', 80);
}
if(!$fp) return false;
fputs($fp, "POST /qtranslate/services/$action HTTP/1.1\r\n");
fputs($fp, "Host: www.qianqin.de\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ". strlen($data) ."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);
$res = '';
while(!feof($fp)) {
$res .= fgets($fp, 128);
}
// check for timeout
$info = stream_get_meta_data($fp);
if($info['timed_out']) return false;
fclose($fp);
preg_match("#^Content-Length:\s*([0-9]+)\s*$#ism",$res, $match);
if(isset($match[1])) {
$content_length = $match[1];
$content = substr($res, -$content_length, $content_length);
} else {
$content = $res;
}
$debug = $content;
$content = qs_base64_unserialize($content);
openssl_open($content['data'], $content, $content['key'], $private_key);
if($content===false) {
echo "
DEBUG:\n";
echo $debug;
echo "
";
}
openssl_free_key($key);
return qs_cleanup(qs_base64_unserialize($content), $action);
}
function qs_clean_uri($clean_uri) {
return preg_replace("/&(qs_delete|qs_cron)=[^]*/i","",$clean_uri);
}
function qs_translateButtons($available_languages, $missing_languages) {
global $q_config, $post;
if(sizeof($missing_languages)==0) return;
$missing_languages_name = array();
foreach($missing_languages as $language) {
$missing_languages_name[] = ''.$q_config['language_name'][$language].'';
}
$missing_languages_names = join(', ', $missing_languages_name);
printf(__('Translate to %s
', 'qtranslate') ,$missing_languages_names);
}
function qs_css() {
echo "#qs_content_preview { width:100%; height:200px }";
echo ".service_description { margin-left:20px; margin-top:0 }";
echo "#qtranslate-services h4 { margin-top:0 }";
echo "#qtranslate-services h5 { margin-bottom:0 }";
echo "#qtranslate-services .description { font-size:11px }";
echo "#qtrans_select_translate { margin-right:11px }";
echo ".qs_status { border:0 }";
echo ".qs_no-bottom-border { border-bottom:0 !important }";
}
function qs_load() {
global $q_config, $qs_public_key;
$qtranslate_services = get_option('qtranslate_qtranslate_services');
$qtranslate_services = qtrans_validateBool($qtranslate_services, $q_config['qtranslate_services']);
$q_config['qtranslate_services'] = $qtranslate_services && function_exists('openssl_get_publickey');
if($q_config['qtranslate_services'] && is_string($qs_public_key)) {
$qs_public_key = openssl_get_publickey(join("\n",explode("|",$qs_public_key)));
}
}
function qs_init() {
global $q_config;
if($q_config['qtranslate_services']) {
/* disabled for meta box
add_filter('qtranslate_toolbar', 'qs_toobar');
add_filter('qtranslate_modify_editor_js', 'qs_editor_js');
*/
add_meta_box('translatediv', __('Translate to','qtranslate'), 'qs_translate_box', 'post', 'side', 'core');
add_meta_box('translatediv', __('Translate to','qtranslate'), 'qs_translate_box', 'page', 'side', 'core');
add_action('qtranslate_languageColumn', 'qs_translateButtons', 10, 2);
// add plugin page without menu link for users with permission
if(current_user_can('edit_published_posts')) {
//add_posts_page(__('Translate','qtranslate'), __('Translate','qtranslate'), 'edit_published_posts', 'qtranslate_services', 'qs_service');
global $_registered_pages;
$hookname = get_plugin_page_hookname('qtranslate_services', 'edit.php');
add_action($hookname, 'qs_service');
$_registered_pages[$hookname] = true;
}
}
}
function qs_save() {
global $q_config;
if($q_config['qtranslate_services'])
update_option('qtranslate_qtranslate_services', '1');
else
update_option('qtranslate_qtranslate_services', '0');
}
function qs_cleanup($var, $action) {
switch($action) {
case QS_GET_SERVICES:
foreach($var as $service_id => $service) {
// make array out ouf serialized field
$fields = array();
$required_fields = explode('|',$service['service_required_fields']);
foreach($required_fields as $required_field) {
if(strpos($required_field, " ")!==false) {
list($fieldname, $title) = explode(' ', $required_field, 2);
if($fieldname!='') {
$fields[] = array('name' => $fieldname, 'value' => '', 'title' => $title);
}
}
}
$var[$service_id]['service_required_fields'] = $fields;
}
break;
}
if(isset($var['error']) && $var['error'] == QS_DEBUG) {
echo "Debug message received from Server: \n";
var_dump($var['message']);
echo "
";
}
return $var;
}
function qs_config_pre_hook($message) {
global $q_config;
if(isset($_POST['default_language'])) {
qtrans_checkSetting('qtranslate_services', true, QT_BOOLEAN);
qs_load();
if($q_config['qtranslate_services']) {
$services = qs_queryQS(QS_GET_SERVICES);
$service_settings = get_option('qs_service_settings');
if(!is_array($service_settings)) $service_settings = array();
foreach($services as $service_id => $service) {
// check if there are already settings for the field
if(!isset($service_settings[$service_id])||!is_array($service_settings[$service_id])) $service_settings[$service_id] = array();
// update fields
foreach($service['service_required_fields'] as $field) {
if(isset($_POST['qs_'.$service_id.'_'.$field['name']])) {
// skip empty passwords to keep the old value
if($_POST['qs_'.$service_id.'_'.$field['name']]=='' && $field['name']=='password') continue;
$service_settings[$service_id][$field['name']] = $_POST['qs_'.$service_id.'_'.$field['name']];
}
}
}
update_option('qs_service_settings', $service_settings);
}
}
if(isset($_GET['qs_delete'])) {
$_GET['qs_delete'] = intval($_GET['qs_delete']);
$orders = get_option('qs_orders');
if(is_array($orders)) {
foreach($orders as $key => $order) {
if($orders[$key]['order']['order_id'] == $_GET['qs_delete']) {
unset($orders[$key]);
update_option('qs_orders',$orders);
}
}
}
$message = __('Order deleted.','qtranslate');
}
if(isset($_GET['qs_cron'])) {
qs_cron();
$message = __('Status updated for all open orders.','qtranslate');
}
return $message;
}
function qs_translate_box($post) {
global $q_config;
$languages = qtrans_getSortedLanguages();
?>
-
'.__('Please save your post first.','qtranslate').'';
}
}
?>
__('Post Title', 'qtranslate'),
'service' => __('Service', 'qtranslate'),
'source_language' => __('Source Language', 'qtranslate'),
'target_language' => __('Target Language', 'qtranslate'),
'action' => __('Action', 'qtranslate')
);
}
function qs_config_hook($request_uri) {
global $q_config;
?>
()
$order) {
qs_UpdateOrder($order['order']['order_id']);
}
}
function qs_UpdateOrder($order_id) {
global $wpdb;
$orders = get_option('qs_orders');
if(!is_array($orders)) return false;
foreach($orders as $key => $order) {
// search for wanted order
if($order['order']['order_id']!=$order_id) continue;
// query server for updates
$order['order']['order_url'] = get_option('home');
$result = qs_queryQS(QS_RETRIEVE_TRANSLATION, $order['order']);
if(isset($result['order_comment'])) $orders[$key]['status'] = $result['order_comment'];
// update db if post is updated
if(isset($result['order_status']) && $result['order_status']==QS_STATE_CLOSED) {
$order['post_id'] = intval($order['post_id']);
$post = &get_post($order['post_id']);
$title = qtrans_split($post->post_title);
$content = qtrans_split($post->post_content);
$title[$order['target_language']] = $result['order_translated_title'];
$content[$order['target_language']] = $result['order_translated_text'];
$post->post_title = qtrans_join($title);
$post->post_content = qtrans_join($content);
$wpdb->show_errors();
$wpdb->query('UPDATE '.$wpdb->posts.' SET post_title="'.mysql_escape_string($post->post_title).'", post_content = "'.mysql_escape_string($post->post_content).'" WHERE ID = "'.$post->ID.'"');
wp_cache_add($post->ID, $post, 'posts');
unset($orders[$key]);
}
update_option('qs_orders',$orders);
return true;
}
return false;
}
function qs_service() {
global $q_config, $qs_public_key, $qs_error_messages;
if(!isset($_REQUEST['post'])) {
echo '';
printf(__('To translate a post, please go to the edit posts overview.','qtranslate'), 'edit.php');
exit();
}
$post_id = intval($_REQUEST['post']);
$translate_from = '';
if(isset($_REQUEST['source_language'])&&qtrans_isEnabled($_REQUEST['source_language']))
$translate_from = $_REQUEST['source_language'];
if(isset($_REQUEST['target_language'])&&qtrans_isEnabled($_REQUEST['target_language']))
$translate_to = $_REQUEST['target_language'];
if($translate_to == $translate_from) $translate_to = '';
$post = &get_post($post_id);
if(!$post) {
printf(__('Post with id "%s" not found!','qtranslate'), $post_id);
return;
}
$default_service = intval(get_option('qs_default_service'));
$service_settings = get_option('qs_service_settings');
// Detect available Languages and possible target languages
$available_languages = qtrans_getAvailableLanguages($post->post_content);
if(sizeof($available_languages)==0) {
$error = __('The requested Post has no content, no Translation possible.', 'qtranslate');
}
// try to guess source and target language
if(!in_array($translate_from, $available_languages)) $translate_from = '';
$missing_languages = array_diff($q_config['enabled_languages'], $available_languages);
if(empty($translate_from) && in_array($q_config['default_language'], $available_languages) && $translate_to!=$q_config['default_language']) $translate_from = $q_config['default_language'];
if(empty($translate_to) && sizeof($missing_languages)==1) $translate_to = $missing_languages[0];
if(in_array($translate_to, $available_languages)) {
$message = __('The Post already has content for the selected target language. If a translation request is send, the current text for the target language will be overwritten.','qtranslate');
}
if(sizeof($available_languages)==1) {
if($available_languages[0] == $translate_to) {
unset($translate_to);
}
$translate_from = $available_languages[0];
} elseif($translate_from == '' && sizeof($available_languages) > 1) {
$languages = qtrans_getSortedLanguages();
foreach($languages as $language) {
if($language != $translate_to && in_array($language, $available_languages)) {
$translate_from = $language;
break;
}
}
}
// link to current page with get variables
$url_link = add_query_arg('post', $post_id);
if(!empty($translate_to)) $url_link = add_query_arg('target_language', $translate_to, $url_link);
if(!empty($translate_from)) $url_link = add_query_arg('source_language', $translate_from, $url_link);
// get correct title and content
$post_title = qtrans_use($translate_from,$post->post_title);
$post_content = qtrans_use($translate_from,$post->post_content);
if(isset($translate_from) && isset($translate_to)) {
$title = sprintf('Translate "%1$s" from %2$s to %3$s', htmlspecialchars($post_title), $q_config['language_name'][$translate_from], $q_config['language_name'][$translate_to]);
} elseif(isset($translate_from)) {
$title = sprintf('Translate "%1$s" from %2$s', htmlspecialchars($post_title), $q_config['language_name'][$translate_from]);
} else {
$title = sprintf('Translate "%1$s"', htmlspecialchars($post_title));
}
// Check data
if(isset($_POST['service_id'])) {
$service_id = intval($_POST['service_id']);
$default_service = $service_id;
update_option('qs_default_service', $service_id);
$order_key = substr(md5(time().AUTH_KEY),0,20);
$request = array(
'order_service_id' => $service_id,
'order_url' => get_option('home'),
'order_key' => $order_key,
'order_title' => $post_title,
'order_text' => $post_content,
'order_source_language' => $translate_from,
'order_source_locale' => $q_config['locale'][$translate_from],
'order_target_language' => $translate_to,
'order_target_locale' => $q_config['locale'][$translate_to]
);
// check for additional fields
if(is_array($service_settings[$service_id])) {
$request['order_required_field'] = array();
foreach($service_settings[$service_id] as $setting => $value) {
$request['order_required_field'][$setting] = $value;
}
}
$answer = qs_queryQS(QS_INIT_TRANSLATION, $request);
if(isset($answer['error'])) {
$error = sprintf(__('An error occured: %s', 'qtranslate'), $qs_error_messages[$answer['error']]);
if($answer['message']!='') {
$error.='
'.sprintf(__('Additional information: %s', 'qtranslate'), qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($answer['message']));
}
}
if(isset($answer['order_id'])) {
$orders = get_option('qs_orders');
if(!is_array($orders)) $orders = array();
$orders[] = array('post_id'=>$post_id, 'service_id' => $service_id, 'source_language'=>$translate_from, 'target_language'=>$translate_to, 'order' => array('order_key' => $order_key, 'order_id' => $answer['order_id']));
update_option('qs_orders', $orders);
if(empty($answer['message'])) {
$order_completed_message = '';
} else {
$order_completed_message = htmlspecialchars($answer['message']);
}
qs_UpdateOrder($answer['order_id']);
}
}
if(isset($error)) {
?>
Support Forum','qtranslate'), 'http://www.qianqin.de/qtranslate/forum/');?>