From 693ece85fe6a72d4763c8523d74f61a979e323b1 Mon Sep 17 00:00:00 2001 From: Evgeny Gryaznov Date: Sat, 26 Feb 2011 14:57:23 +0100 Subject: [PATCH] format code, fix minor issues (link param, etc.) --- src/messenger/webim/install/dbinfo.php | 44 ++++----- src/messenger/webim/install/dbperform.php | 94 ++++++++++---------- src/messenger/webim/install/index.php | 72 ++++++++------- src/messenger/webim/libs/common.php | 8 +- src/messenger/webim/operator/blocked.php | 2 +- src/messenger/webim/operator/userhistory.php | 2 +- 6 files changed, 114 insertions(+), 108 deletions(-) diff --git a/src/messenger/webim/install/dbinfo.php b/src/messenger/webim/install/dbinfo.php index 0e672a36..2f215e65 100644 --- a/src/messenger/webim/install/dbinfo.php +++ b/src/messenger/webim/install/dbinfo.php @@ -98,7 +98,7 @@ $dbtables = array( "blockedCount" => "int DEFAULT 0" ), - "${mysqlprefix}chatconfig" => array ( + "${mysqlprefix}chatconfig" => array( "id" => "INT NOT NULL auto_increment PRIMARY KEY", "vckey" => "varchar(255)", "vcvalue" => "varchar(255)", @@ -124,7 +124,8 @@ $dbtables_can_update = array( "${mysqlprefix}chatresponses" => array(), ); -function show_install_err($text) { +function show_install_err($text) +{ global $page, $version, $errors, $webimroot; $page = array( 'version' => $version, @@ -136,17 +137,18 @@ function show_install_err($text) { exit; } -function create_table($id,$link) { +function create_table($id, $link) +{ global $dbtables, $memtables, $dbencoding, $mysqlprefix; - if(!isset($dbtables[$id])) { - show_install_err("Unknown table: $id, ".mysql_error()); + if (!isset($dbtables[$id])) { + show_install_err("Unknown table: $id, " . mysql_error($link)); } $query = - "CREATE TABLE $id\n". - "(\n"; - foreach( $dbtables[$id] as $k => $v ) { + "CREATE TABLE $id\n" . + "(\n"; + foreach ($dbtables[$id] as $k => $v) { $query .= " $k $v,\n"; } @@ -158,19 +160,20 @@ function create_table($id,$link) { $query .= " TYPE=InnoDb"; } - mysql_query($query,$link) or show_install_err(' Query failed: '.mysql_error()); + mysql_query($query, $link) or show_install_err(' Query failed: ' . mysql_error($link)); - if( $id == "${mysqlprefix}chatoperator" ) { + if ($id == "${mysqlprefix}chatoperator") { create_operator_("admin", "", "", "Administrator", "Administrator", "", $link); - } else if( $id == "${mysqlprefix}chatrevision" ) { - perform_query("INSERT INTO ${mysqlprefix}chatrevision VALUES (1)",$link); + } else if ($id == "${mysqlprefix}chatrevision") { + perform_query("INSERT INTO ${mysqlprefix}chatrevision VALUES (1)", $link); } } -function get_tables($link) { +function get_tables($link) +{ global $mysqldb, $errors; - $result = mysql_query("SHOW TABLES FROM `$mysqldb`"); - if( $result ) { + $result = mysql_query("SHOW TABLES FROM `$mysqldb`", $link); + if ($result) { $arr = array(); while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $arr[] = $row[0]; @@ -179,15 +182,16 @@ function get_tables($link) { return $arr; } else { - $errors[] = "Cannot get tables from database. Error: ".mysql_error(); + $errors[] = "Cannot get tables from database. Error: " . mysql_error($link); return false; } } -function get_columns($tablename,$link) { +function get_columns($tablename, $link) +{ global $errors; - $result = mysql_query("SHOW COLUMNS FROM $tablename"); - if( $result ) { + $result = mysql_query("SHOW COLUMNS FROM $tablename", $link); + if ($result) { $arr = array(); while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $arr[] = $row[0]; @@ -196,7 +200,7 @@ function get_columns($tablename,$link) { return $arr; } else { - $errors[] = "Cannot get columns from table \"$tablename\". Error: ".mysql_error(); + $errors[] = "Cannot get columns from table \"$tablename\". Error: " . mysql_error($link); return false; } } diff --git a/src/messenger/webim/install/dbperform.php b/src/messenger/webim/install/dbperform.php index 2e22d3cc..0186f40c 100644 --- a/src/messenger/webim/install/dbperform.php +++ b/src/messenger/webim/install/dbperform.php @@ -23,139 +23,135 @@ require_once('../libs/common.php'); require_once('../libs/operator.php'); require_once('dbinfo.php'); -function runsql($query,$link) { - $res = mysql_query($query,$link) - or show_install_err(' Query failed: '.mysql_error()); +function runsql($query, $link) +{ + $res = mysql_query($query, $link) or show_install_err(' Query failed: ' . mysql_error($link)); return $res; } -$act = verifyparam( "act", "/^(silentcreateall|createdb|ct|dt|addcolumns)$/"); +$act = verifyparam("act", "/^(silentcreateall|createdb|ct|dt|addcolumns)$/"); -$link = @mysql_connect($mysqlhost,$mysqllogin ,$mysqlpass ) - or show_install_err('Could not connect: ' . mysql_error()); +$link = @mysql_connect($mysqlhost, $mysqllogin, $mysqlpass) + or show_install_err('Could not connect: ' . mysql_error($link)); if ($act == "silentcreateall") { - mysql_query("CREATE DATABASE $mysqldb",$link) - or show_install_err(' Query failed: '.mysql_error()); - foreach($dbtables as $id) { + mysql_query("CREATE DATABASE $mysqldb", $link) or show_install_err(' Query failed: ' . mysql_error($link)); + foreach ($dbtables as $id) { create_table($id, $link); - } -} else if($act == "createdb") { - mysql_query("CREATE DATABASE $mysqldb",$link) - or show_install_err(' Query failed: '.mysql_error()); + } +} else if ($act == "createdb") { + mysql_query("CREATE DATABASE $mysqldb", $link) or show_install_err(' Query failed: ' . mysql_error($link)); } else { - mysql_select_db($mysqldb,$link) - or show_install_err('Could not select database'); - if( $force_charset_in_connection ) { + mysql_select_db($mysqldb, $link) or show_install_err('Could not select database'); + if ($force_charset_in_connection) { mysql_query("SET character set $dbencoding", $link); } - if( $act == "ct") { + if ($act == "ct") { $curr_tables = get_tables($link); - if( $curr_tables === false) { + if ($curr_tables === false) { show_install_err($errors[0]); } $tocreate = array_diff(array_keys($dbtables), $curr_tables); - foreach( $tocreate as $id) { + foreach ($tocreate as $id) { create_table($id, $link); } - } else if( $act == "dt") { - foreach( array_keys($dbtables) as $id) { - mysql_query("DROP TABLE IF EXISTS $id",$link) - or show_install_err(' Query failed: '.mysql_error()); + } else if ($act == "dt") { + foreach (array_keys($dbtables) as $id) { + mysql_query("DROP TABLE IF EXISTS $id", $link) or show_install_err(' Query failed: ' . mysql_error($link)); } - } else if( $act == "addcolumns") { + } else if ($act == "addcolumns") { $absent = array(); - foreach( $dbtables as $id => $columns) { + foreach ($dbtables as $id => $columns) { $curr_columns = get_columns($id, $link); - if( $curr_columns === false ) { + if ($curr_columns === false) { show_install_err($errors[0]); } $tocreate = array_diff(array_keys($columns), $curr_columns); - foreach($tocreate as $v) { + foreach ($tocreate as $v) { $absent[] = "$id.$v"; } } - if( in_array("${mysqlprefix}chatmessage.agentId", $absent) ) { + if (in_array("${mysqlprefix}chatmessage.agentId", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatmessage ADD agentId int NOT NULL DEFAULT 0 AFTER ikind", $link); runsql("update ${mysqlprefix}chatmessage,${mysqlprefix}chatoperator set agentId = operatorid where agentId = 0 AND ikind = 2 AND (vclocalename = tname OR vccommonname = tname)", $link); } - if( in_array("${mysqlprefix}chatthread.agentId", $absent) ) { + if (in_array("${mysqlprefix}chatthread.agentId", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatthread ADD agentId int NOT NULL DEFAULT 0 AFTER agentName", $link); runsql("update ${mysqlprefix}chatthread,${mysqlprefix}chatoperator set agentId = operatorid where agentId = 0 AND (vclocalename = agentName OR vccommonname = agentName)", $link); } - if( in_array("${mysqlprefix}chatthread.agentTyping", $absent) ) { + if (in_array("${mysqlprefix}chatthread.agentTyping", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatthread ADD agentTyping int DEFAULT 0", $link); } - if( in_array("${mysqlprefix}chatthread.userTyping", $absent) ) { + if (in_array("${mysqlprefix}chatthread.userTyping", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatthread ADD userTyping int DEFAULT 0", $link); } - if( in_array("${mysqlprefix}chatthread.messageCount", $absent) ) { + if (in_array("${mysqlprefix}chatthread.messageCount", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatthread ADD messageCount varchar(16)", $link); runsql("ALTER TABLE ${mysqlprefix}chatmessage ADD INDEX idx_threadid_ikind (threadid, ikind)", $link); runsql("UPDATE ${mysqlprefix}chatthread t SET t.messageCount = (SELECT COUNT(*) FROM ${mysqlprefix}chatmessage WHERE ${mysqlprefix}chatmessage.threadid = t.threadid AND ikind = 1)", $link); runsql("ALTER TABLE ${mysqlprefix}chatmessage DROP INDEX idx_threadid_ikind", $link); } - if( in_array("${mysqlprefix}chatthread.nextagent", $absent) ) { + if (in_array("${mysqlprefix}chatthread.nextagent", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatthread ADD nextagent int NOT NULL DEFAULT 0", $link); } - if( in_array("${mysqlprefix}chatthread.shownmessageid", $absent) ) { + if (in_array("${mysqlprefix}chatthread.shownmessageid", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatthread ADD shownmessageid int NOT NULL DEFAULT 0", $link); } - if( in_array("${mysqlprefix}chatthread.userid", $absent) ) { + if (in_array("${mysqlprefix}chatthread.userid", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatthread ADD userid varchar(255) DEFAULT \"\"", $link); } - if( in_array("${mysqlprefix}chatoperator.iperm", $absent) ) { + if (in_array("${mysqlprefix}chatoperator.iperm", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD iperm int DEFAULT 65535", $link); } - if( in_array("${mysqlprefix}chatoperator.istatus", $absent) ) { + if (in_array("${mysqlprefix}chatoperator.istatus", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD istatus int DEFAULT 0", $link); } - - if( in_array("${mysqlprefix}chatoperator.vcavatar", $absent) ) { + + if (in_array("${mysqlprefix}chatoperator.vcavatar", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD vcavatar varchar(255)", $link); } - if( in_array("${mysqlprefix}chatoperator.vcjabbername", $absent) ) { + if (in_array("${mysqlprefix}chatoperator.vcjabbername", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD vcjabbername varchar(255)", $link); } - if( in_array("${mysqlprefix}chatoperator.vcemail", $absent) ) { + if (in_array("${mysqlprefix}chatoperator.vcemail", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD vcemail varchar(64)", $link); } - if( in_array("${mysqlprefix}chatoperator.dtmrestore", $absent) ) { + if (in_array("${mysqlprefix}chatoperator.dtmrestore", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD dtmrestore datetime DEFAULT 0", $link); } - if( in_array("${mysqlprefix}chatoperator.vcrestoretoken", $absent) ) { + if (in_array("${mysqlprefix}chatoperator.vcrestoretoken", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD vcrestoretoken varchar(64)", $link); } - - if( in_array("${mysqlprefix}chatthread.groupid", $absent) ) { + + if (in_array("${mysqlprefix}chatthread.groupid", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatthread ADD groupid int references ${mysqlprefix}chatgroup(groupid)", $link); } - if( in_array("${mysqlprefix}chatthread.userAgent", $absent) ) { + if (in_array("${mysqlprefix}chatthread.userAgent", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatthread ADD userAgent varchar(255)", $link); } - if( in_array("${mysqlprefix}chatgroup.vcemail", $absent) ) { + if (in_array("${mysqlprefix}chatgroup.vcemail", $absent)) { runsql("ALTER TABLE ${mysqlprefix}chatgroup ADD vcemail varchar(64)", $link); } - + $res = mysql_query("select null from information_schema.statistics where table_name = '${mysqlprefix}chatmessage' and index_name = 'idx_agentid'", $link); - if($res && mysql_num_rows($res) == 0) { + if ($res && mysql_num_rows($res) == 0) { runsql("ALTER TABLE ${mysqlprefix}chatmessage ADD INDEX idx_agentid (agentid)", $link); } } diff --git a/src/messenger/webim/install/index.php b/src/messenger/webim/install/index.php index 1376a3c2..26807ade 100644 --- a/src/messenger/webim/install/index.php +++ b/src/messenger/webim/install/index.php @@ -33,35 +33,37 @@ $page['nextstep'] = false; $page['nextnotice'] = false; $errors = array(); -function check_webimroot() { +function check_webimroot() +{ global $page, $errors, $webimroot; $requestUri = $_SERVER["REQUEST_URI"]; - if(!preg_match('/^(.*)\\/install(\\/[^\\/\\\\]*)?$/', $requestUri, $matches)) { + if (!preg_match('/^(.*)\\/install(\\/[^\\/\\\\]*)?$/', $requestUri, $matches)) { $errors[] = "Cannot detect application location: $requestUri"; - return false; + return false; } $applocation = $matches[1]; - - if($applocation != $webimroot) { + + if ($applocation != $webimroot) { $errors[] = "Please, check file ${applocation}/libs/config.php
Wrong value of \$webimroot variable, should be \"$applocation\""; $webimroot = $applocation; - return false; + return false; } $page['done'][] = getlocal2("install.0.app", array($applocation)); return true; } -function check_connection() { - global $mysqlhost,$mysqllogin,$mysqlpass, $page, $errors, $webimroot; - $link = @mysql_connect($mysqlhost,$mysqllogin,$mysqlpass); +function check_connection() +{ + global $mysqlhost, $mysqllogin, $mysqlpass, $page, $errors, $webimroot; + $link = @mysql_connect($mysqlhost, $mysqllogin, $mysqlpass); if ($link) { $result = mysql_query("SELECT VERSION() as c", $link); - if( $result && $ver = mysql_fetch_array($result, MYSQL_ASSOC)) { + if ($result && $ver = mysql_fetch_array($result, MYSQL_ASSOC)) { $page['done'][] = getlocal2("install.1.connected", array($ver['c'])); mysql_free_result($result); } else { - $errors[] = "Version of your SQL server is unknown. Please check. Error: ".mysql_error(); + $errors[] = "Version of your SQL server is unknown. Please check. Error: " . mysql_error($link); mysql_close($link); return null; } @@ -72,11 +74,12 @@ function check_connection() { } } -function check_database($link) { +function check_database($link) +{ global $mysqldb, $force_charset_in_connection, $dbencoding, $page, $webimroot; - if(mysql_select_db($mysqldb,$link)) { + if (mysql_select_db($mysqldb, $link)) { $page['done'][] = getlocal2("install.2.db_exists", array($mysqldb)); - if( $force_charset_in_connection ) { + if ($force_charset_in_connection) { mysql_query("SET character set $dbencoding", $link); } return true; @@ -88,12 +91,13 @@ function check_database($link) { return false; } -function check_tables($link) { +function check_tables($link) +{ global $dbtables, $page, $webimroot; $curr_tables = get_tables($link); - if( $curr_tables !== false) { + if ($curr_tables !== false) { $tocreate = array_diff(array_keys($dbtables), $curr_tables); - if( count($tocreate) == 0 ) { + if (count($tocreate) == 0) { $page['done'][] = getlocal("install.3.tables_exist"); return true; } else { @@ -104,19 +108,20 @@ function check_tables($link) { return false; } -function check_columns($link) { +function check_columns($link) +{ global $dbtables, $dbtables_can_update, $errors, $page, $webimroot; $need_to_create_columns = false; - foreach( $dbtables as $id => $columns) { + foreach ($dbtables as $id => $columns) { $curr_columns = get_columns($id, $link); - if( $curr_columns === false ) { + if ($curr_columns === false) { return false; } $tocreate = array_diff(array_keys($columns), $curr_columns); - if( count($tocreate) != 0 ) { + if (count($tocreate) != 0) { $cannot_update = array_diff($tocreate, $dbtables_can_update[$id]); - if( count($cannot_update) != 0) { + if (count($cannot_update) != 0) { $errors[] = "Key columns are absent in table `$id'. Unable to continue installation."; $page['nextstep'] = getlocal("install.kill_tables"); $page['nextstepurl'] = "$webimroot/install/dbperform.php?act=dt"; @@ -127,7 +132,7 @@ function check_columns($link) { } } - if( $need_to_create_columns ) { + if ($need_to_create_columns) { $page['nextstep'] = getlocal("install.4.create"); $page['nextstepurl'] = "$webimroot/install/dbperform.php?act=addcolumns"; $page['nextnotice'] = getlocal("install.4.notice"); @@ -138,29 +143,30 @@ function check_columns($link) { return true; } -function check_status() { +function check_status() +{ global $page, $webimroot, $settings, $dbversion; - - if(!check_webimroot()) { + + if (!check_webimroot()) { return; } - + $link = check_connection(); - if(!$link) { + if (!$link) { return; } - if( !check_database($link)) { + if (!check_database($link)) { mysql_close($link); return; } - if( !check_tables($link)) { + if (!check_tables($link)) { mysql_close($link); return; } - if( !check_columns($link)) { + if (!check_columns($link)) { mysql_close($link); return; } @@ -168,9 +174,9 @@ function check_status() { $page['done'][] = getlocal("installed.message"); $page['nextstep'] = getlocal("installed.login_link"); - $page['nextnotice'] = getlocal2("installed.notice", array($webimroot."/install/")); + $page['nextnotice'] = getlocal2("installed.notice", array("${webimroot}/install/")); $page['nextstepurl'] = "$webimroot/"; - + $page['show_small_login'] = true; mysql_close($link); diff --git a/src/messenger/webim/libs/common.php b/src/messenger/webim/libs/common.php index d449b948..d9ab098c 100644 --- a/src/messenger/webim/libs/common.php +++ b/src/messenger/webim/libs/common.php @@ -335,12 +335,12 @@ function connect() { function perform_query($query,$link) { mysql_query($query,$link) - or die(' Query failed: '.mysql_error()/*.": ".$query*/); + or die(' Query failed: '.mysql_error($link)/*.": ".$query*/); } function select_one_row($query,$link) { $result = mysql_query($query,$link) or die(' Query failed: ' . - mysql_error().": ".$query); + mysql_error($link) /*.": ".$query*/); $line = mysql_fetch_array($result, MYSQL_ASSOC); mysql_free_result($result); return $line; @@ -348,7 +348,7 @@ function select_one_row($query,$link) { function select_multi_assoc($query, $link) { $sqlresult = mysql_query($query,$link) or die(' Query failed: ' . - mysql_error().": ".$query); + mysql_error($link) /*.": ".$query*/); $result = array(); while ($row = mysql_fetch_array($sqlresult, MYSQL_ASSOC)) { @@ -366,7 +366,7 @@ function db_build_select($fields, $table, $conditions, $orderandgroup) { function db_rows_count($table,$conditions,$countfields, $link) { $result = mysql_query(db_build_select("count(".($countfields ? $countfields : "*").")", $table, $conditions, ""),$link) - or die(' Count query failed: '.mysql_error()); + or die(' Count query failed: '.mysql_error($link)); $line = mysql_fetch_array($result, MYSQL_NUM); mysql_free_result($result); return $line[0]; diff --git a/src/messenger/webim/operator/blocked.php b/src/messenger/webim/operator/blocked.php index 4a392c9c..e75ce7c4 100644 --- a/src/messenger/webim/operator/blocked.php +++ b/src/messenger/webim/operator/blocked.php @@ -47,7 +47,7 @@ if( isset($_GET['act']) && $_GET['act'] == 'del' ) { } $result = mysql_query("select banid,unix_timestamp(dtmtill) as till,address,comment from ${mysqlprefix}chatban", $link) - or die(' Query failed: ' .mysql_error()); + or die(' Query failed: ' .mysql_error($link)); $blockedList = array(); while ($ban = mysql_fetch_array($result, MYSQL_ASSOC)) { diff --git a/src/messenger/webim/operator/userhistory.php b/src/messenger/webim/operator/userhistory.php index 1f896ef3..d7e419f2 100644 --- a/src/messenger/webim/operator/userhistory.php +++ b/src/messenger/webim/operator/userhistory.php @@ -49,7 +49,7 @@ function threads_by_userid($userid) { "from ${mysqlprefix}chatthread ". "where userid=\"$userid\" order by created DESC", $userid); - $result = mysql_query($query, $link) or die(' Query failed: ' .mysql_error().": ".$query); + $result = mysql_query($query, $link) or die(' Query failed: ' .mysql_error($link) /*.": ".$query*/); $foundThreads = array(); while ($thread = mysql_fetch_array($result, MYSQL_ASSOC)) {