2007-10-10 19:15:47 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file is part of Web Instant Messenger project.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2007 Internet Services Ltd.
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* Evgeny Gryaznov - initial API and implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
session_start();
|
|
|
|
|
|
|
|
require(dirname(__FILE__).'/converter.php');
|
|
|
|
require(dirname(__FILE__).'/config.php');
|
|
|
|
|
2007-12-13 13:26:41 +03:00
|
|
|
$version = 'v1.0.8';
|
2007-10-10 19:15:47 +04:00
|
|
|
|
|
|
|
function myiconv($in_enc, $out_enc, $string) {
|
|
|
|
global $_utf8win1251, $_win1251utf8;
|
|
|
|
if( function_exists('iconv') ) {
|
|
|
|
$converted = iconv($in_enc, $out_enc, $string);
|
|
|
|
if( $converted !== FALSE )
|
|
|
|
return $converted;
|
|
|
|
}
|
|
|
|
if( $in_enc == "cp1251" && $out_enc == "utf-8" )
|
|
|
|
return strtr($string, $_win1251utf8);
|
|
|
|
if( $in_enc == "utf-8" && $out_enc == "cp1251" )
|
|
|
|
return strtr($string, $_utf8win1251);
|
|
|
|
|
|
|
|
return $string; // do not know how to convert
|
|
|
|
}
|
|
|
|
|
|
|
|
function getrawparam( $name ) {
|
|
|
|
global $webim_encoding, $request_encoding;
|
|
|
|
if( isset($_POST[$name]) )
|
|
|
|
return myiconv($request_encoding,$webim_encoding,$_POST[$name]);
|
|
|
|
die("no ".$name." parameter");
|
|
|
|
}
|
|
|
|
|
|
|
|
function getparam( $name ) {
|
|
|
|
if( isset($_POST[$name]) )
|
|
|
|
return $_POST[$name];
|
|
|
|
die("no ".$name." parameter");
|
|
|
|
}
|
|
|
|
|
|
|
|
function verifyparam( $name, $regexp, $default = null ) {
|
|
|
|
|
|
|
|
if( isset( $_GET[$name] ) ) {
|
|
|
|
$val = $_GET[$name];
|
|
|
|
if( preg_match( $regexp, $val ) )
|
|
|
|
return $val;
|
|
|
|
|
|
|
|
} else if( isset( $_POST[$name] ) ) {
|
|
|
|
$val = $_POST[$name];
|
|
|
|
if( preg_match( $regexp, $val ) )
|
|
|
|
return $val;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if( isset( $default ) )
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "<html><head></head><body>Wrong parameter used or absent: ".$name."</body></html>";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
function debugexit_print( $var ) {
|
|
|
|
echo "<html><body><pre>";
|
|
|
|
print_r( $var );
|
|
|
|
echo "</pre></body></html>";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_user_locale() {
|
|
|
|
global $available_locales, $default_locale;
|
|
|
|
|
|
|
|
if( isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ) {
|
|
|
|
$requested_langs = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
|
|
|
|
foreach( $requested_langs as $requested_lang) {
|
|
|
|
if( strlen($requested_lang) > 2 )
|
|
|
|
$requested_lang = substr($requested_lang,0,2);
|
|
|
|
|
|
|
|
if( in_array($requested_lang,$available_locales) )
|
|
|
|
return $requested_lang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( in_array($default_locale,$available_locales) )
|
|
|
|
return $default_locale;
|
|
|
|
|
|
|
|
return 'en';
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_locale() {
|
|
|
|
global $available_locales;
|
|
|
|
|
2007-10-17 15:32:50 +04:00
|
|
|
$locale = verifyparam("locale", "/^[\w-]{2,5}$/", "");
|
2007-10-10 19:15:47 +04:00
|
|
|
|
|
|
|
if( $locale && in_array($locale,$available_locales) ) {
|
|
|
|
$_SESSION['locale'] = $locale;
|
|
|
|
} else if( isset($_SESSION['locale']) ){
|
|
|
|
$locale = $_SESSION['locale'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !$locale || !in_array($locale,$available_locales) )
|
|
|
|
$locale = get_user_locale();
|
|
|
|
return $locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_locale($locale) {
|
|
|
|
global $current_locale, $available_locales;
|
|
|
|
if( in_array($locale,$available_locales) )
|
|
|
|
$current_locale = $locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
$current_locale = get_locale();
|
|
|
|
$messages = array();
|
|
|
|
|
2007-10-17 14:43:34 +04:00
|
|
|
function get_locale_links($href) {
|
|
|
|
global $available_locales, $current_locale;
|
|
|
|
$localeLinks = "";
|
|
|
|
foreach($available_locales as $k) {
|
|
|
|
if( strlen($localeLinks) > 0 )
|
|
|
|
$localeLinks .= " • ";
|
|
|
|
if( $k == $current_locale )
|
|
|
|
$localeLinks .= $k;
|
|
|
|
else
|
|
|
|
$localeLinks .= "<a href=\"$href?locale=$k\">$k</a>";
|
|
|
|
}
|
|
|
|
return $localeLinks;
|
|
|
|
}
|
|
|
|
|
2007-10-10 19:15:47 +04:00
|
|
|
function load_messages($locale) {
|
|
|
|
global $messages;
|
|
|
|
$hash = array();
|
2007-10-17 15:32:50 +04:00
|
|
|
$fp = fopen(dirname(__FILE__)."/../locales/$locale/properties","r");
|
2007-10-10 19:15:47 +04:00
|
|
|
while (!feof($fp)) {
|
|
|
|
$line = fgets($fp, 4096);
|
|
|
|
$list = split("=", $line, 2 );
|
|
|
|
if( isset($list[1]) ) {
|
|
|
|
$hash[$list[0]] = str_replace("\\n", "\n",trim($list[1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose($fp);
|
|
|
|
$messages[$locale] = $hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getstring_($text,$locale) {
|
|
|
|
global $messages;
|
|
|
|
if(!isset($messages[$locale]))
|
|
|
|
load_messages($locale);
|
|
|
|
|
|
|
|
$localized = $messages[$locale];
|
|
|
|
if( isset($localized[$text]) )
|
|
|
|
return $localized[$text];
|
|
|
|
|
|
|
|
return "!".$text;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getstring($text) {
|
|
|
|
global $current_locale;
|
|
|
|
return getstring_($text,$current_locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getstring2_($text,$params,$locale) {
|
|
|
|
$string = getstring_($text,$locale);
|
|
|
|
for( $i = 0; $i < count($params); $i++ ) {
|
|
|
|
$string = str_replace("{".$i."}", $params[$i], $string);
|
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getstring2($text,$params) {
|
|
|
|
global $current_locale;
|
|
|
|
return getstring2_($text,$params,$current_locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
function connect() {
|
|
|
|
global $mysqlhost, $mysqllogin, $mysqlpass, $mysqldb, $dbencoding, $force_charset_in_connection;
|
2007-10-17 14:43:34 +04:00
|
|
|
$link = @mysql_connect($mysqlhost,$mysqllogin ,$mysqlpass )
|
2007-10-10 19:15:47 +04:00
|
|
|
or die('Could not connect: ' . mysql_error());
|
2007-10-17 14:43:34 +04:00
|
|
|
mysql_select_db($mysqldb,$link) or die('Could not select database');
|
|
|
|
if( $force_charset_in_connection ) {
|
2007-12-03 00:32:47 +03:00
|
|
|
mysql_query("SET NAMES '$dbencoding'", $link);
|
2007-10-17 14:43:34 +04:00
|
|
|
}
|
2007-10-10 19:15:47 +04:00
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
|
|
|
function perform_query($query,$link) {
|
2007-10-17 14:43:34 +04:00
|
|
|
mysql_query($query,$link)
|
|
|
|
or die(' Query failed: '.mysql_error()/*.": ".$query*/);
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function select_one_row($query,$link) {
|
|
|
|
$result = mysql_query($query,$link) or die(' Query failed: ' .
|
|
|
|
mysql_error().": ".$query);
|
|
|
|
$line = mysql_fetch_array($result, MYSQL_ASSOC);
|
|
|
|
mysql_free_result($result);
|
|
|
|
return $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
function select_multi_assoc($query,$link) {
|
|
|
|
$sqlresult = mysql_query($query,$link) or die(' Query failed: ' .
|
|
|
|
mysql_error().": ".$query);
|
|
|
|
|
|
|
|
$result = array();
|
|
|
|
while ($row = mysql_fetch_array($sqlresult, MYSQL_ASSOC)) {
|
|
|
|
$result[] = $row;
|
|
|
|
}
|
|
|
|
mysql_free_result($sqlresult);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function start_xml_output() {
|
|
|
|
header("Cache-Control: no-store, no-cache, must-revalidate");
|
|
|
|
header("Content-type: text/xml");
|
|
|
|
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
|
|
|
|
}
|
|
|
|
|
|
|
|
function start_html_output() {
|
|
|
|
global $output_charset;
|
|
|
|
header("Cache-Control: no-store, no-cache, must-revalidate");
|
|
|
|
header("Content-type: text/html".(isset($output_charset)?"; charset=".$output_charset:""));
|
|
|
|
}
|
|
|
|
|
|
|
|
function escape_with_cdata($text) {
|
|
|
|
return "<![CDATA[" . str_replace("]]>", "]]>]]><![CDATA[",$text) . "]]>";
|
|
|
|
}
|
|
|
|
|
|
|
|
function form_value($key) {
|
|
|
|
global $page;
|
|
|
|
if( isset($page) && isset($page["form$key"]) )
|
|
|
|
return $page["form$key"];
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
function form_value_cb($key) {
|
|
|
|
global $page;
|
|
|
|
if( isset($page) && isset($page["form$key"]) )
|
|
|
|
return $page["form$key"] === true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function no_field($key) {
|
|
|
|
return getstring2("errors.required",array(getstring($key)));
|
|
|
|
}
|
|
|
|
|
2008-03-05 01:22:48 +03:00
|
|
|
|
2007-12-03 00:32:47 +03:00
|
|
|
function wrong_field($key) {
|
|
|
|
return getstring2("errors.wrong_field",array(getstring($key)));
|
|
|
|
}
|
|
|
|
|
2007-10-10 19:15:47 +04:00
|
|
|
function get_popup($href,$message,$title,$wndName,$options) {
|
2008-03-05 01:22:48 +03:00
|
|
|
return "<a href=\"$href\" target=\"_blank\" ".($title?"title=\"$title\" ":"")."onclick=\"if(navigator.userAgent.toLowerCase().indexOf('opera') != -1 && window.event.preventDefault) window.event.preventDefault();this.newWindow = window.open('$href', '$wndName', '$options');this.newWindow.focus();this.newWindow.opener=window;return false;\">$message</a>";
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_image($href,$width,$height) {
|
|
|
|
if( $width != 0 && $height != 0 )
|
|
|
|
return "<img src=\"$href\" border=\"0\" width=\"$width\" height=\"$height\"/>";
|
|
|
|
return "<img src=\"$href\" border=\"0\"/>";
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_gifimage_size($file) {
|
|
|
|
if( function_exists('gd_info')) {
|
|
|
|
$info = gd_info();
|
|
|
|
if( isset($info['GIF Read Support']) && $info['GIF Read Support'] ) {
|
|
|
|
$img = @imagecreatefromgif($file);
|
|
|
|
if($img) {
|
|
|
|
$height = imagesy($img);
|
|
|
|
$width = imagesx($img);
|
|
|
|
imagedestroy($img);
|
|
|
|
return array($width,$height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array(0,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function add_params($servlet, $params) {
|
|
|
|
$infix = '?';
|
|
|
|
if( strstr($servlet,$infix) !== FALSE )
|
|
|
|
$infix = '&';
|
|
|
|
foreach($params as $k => $v) {
|
|
|
|
$servlet .= $infix.$k."=".$v;
|
|
|
|
$infix = '&';
|
|
|
|
}
|
|
|
|
return $servlet;
|
|
|
|
}
|
|
|
|
|
|
|
|
function div($a,$b) {
|
|
|
|
return ($a-($a % $b)) / $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
function date_diff($seconds) {
|
|
|
|
$minutes = div($seconds,60);
|
|
|
|
$seconds = $seconds % 60;
|
|
|
|
if( $minutes < 60 ) {
|
|
|
|
return sprintf("%02d:%02d",$minutes, $seconds);
|
|
|
|
} else {
|
|
|
|
$hours = div($minutes,60);
|
|
|
|
$minutes = $minutes % 60;
|
|
|
|
return sprintf("%02d:%02d:%02d",$hours, $minutes, $seconds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-03 00:32:47 +03:00
|
|
|
function is_valid_email($mail) {
|
|
|
|
return preg_match("/^[^@]+@[^\.]+(\.[^\.]+)*$/", $mail);
|
|
|
|
}
|
|
|
|
|
2007-10-10 19:15:47 +04:00
|
|
|
function quote_smart($value,$link) {
|
|
|
|
if (get_magic_quotes_gpc()) {
|
|
|
|
$value = stripslashes($value);
|
|
|
|
}
|
|
|
|
return mysql_real_escape_string($value,$link);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_app_location($showhost,$issecure) {
|
2007-10-30 15:13:04 +03:00
|
|
|
global $webimroot;
|
2007-10-10 19:15:47 +04:00
|
|
|
if( $showhost ) {
|
2007-10-30 15:13:04 +03:00
|
|
|
return ($issecure?"https://":"http://").$_SERVER['HTTP_HOST'].$webimroot;
|
2007-10-10 19:15:47 +04:00
|
|
|
} else {
|
2007-10-30 15:13:04 +03:00
|
|
|
return $webimroot;
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_month_selection($fromtime,$totime) {
|
|
|
|
$start = getdate($fromtime);
|
|
|
|
$month = $start['mon'];
|
|
|
|
$year = $start['year'];
|
|
|
|
$result = array();
|
|
|
|
do {
|
|
|
|
$current = mktime(0,0,0,$month,1,$year);
|
|
|
|
$result[date("m.y",$current)] = date("M, Y",$current);
|
|
|
|
$month++;
|
|
|
|
if( $month > 12 ) {
|
|
|
|
$month = 1;
|
|
|
|
$year++;
|
|
|
|
}
|
|
|
|
} while( $current < $totime );
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_form_date($day,$month) {
|
|
|
|
if( preg_match('/^(\d{2}).(\d{2})$/', $month, $matches)) {
|
|
|
|
return mktime(0,0,0,$matches[1],$day,$matches[2]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_form_date($time,$prefix) {
|
|
|
|
global $page;
|
|
|
|
$page["form${prefix}day"] = date("d", $time);
|
|
|
|
$page["form${prefix}month"] = date("m.y", $time);
|
|
|
|
}
|
|
|
|
|
2008-03-05 01:22:48 +03:00
|
|
|
|
|
|
|
|
2007-05-10 21:31:10 +04:00
|
|
|
?>
|