$DB_PREFIX = $ret['define']['DB_PREFIX']; $db['user'] = $this->input['dbuser']; $db['pass'] = $_REQUEST['dbpass']; if (!$appinfo['nodb']) { $link = @mysql_connect($db['host'], $db['user'], $db['pass']); if (!$link) { $message = '此数据库无法连接,请确认密码是否准确'; $this->upgrade($message); } mysql_query("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary", $link); } $socket = new hgSocket(); $con = $socket->connect($installinfo['host'], $installinfo['port']); if (!intval($con)) { $message = '服务器无法连接,请确认服务器ip是否正确或服务程序hogeMonitor.py是否监听在' . $installinfo['ip'] . ':' . $installinfo['port'] . '上'; $socket->close(); $this->upgrade($message); } $socket->close(); $curl->initPostData(); $curl->addRequestData('a', 'getapp_path'); $app_path = $curl->request('configuare.php'); $app_path .= '/'; $install_app = trim($this->input['app']); $curl = new curl($this->product_server['host'] . ':' . $this->product_server['port'], ''); $curl->setClient(CUSTOM_APPID, CUSTOM_APPKEY); $curl->setSubmitType('get'); $curl->setReturnFormat('json'); $curl->initPostData(); $curl->addRequestData('app', $install_app); $ret = $curl->request('db.php');
function step7() { $user_name = trim($_REQUEST['user_name']); $password = trim($_REQUEST['password']); $confirmpassword = trim($_REQUEST['confirmpassword']); if (!$user_name) { $message = '请设置用户名'; step6($message); } if (!$password || $confirmpassword != $password) { $message = '未设置密码或两次密码不一致'; step6($message); } $app = file_get_contents(CACHE_DIR . 'app.tmp'); $app = json_decode($app, 1); $db = file_get_contents(CACHE_DIR . 'db.tmp'); $db = json_decode($db, 1); $auth = @file_get_contents(CACHE_DIR . 'auth.tmp'); $auth = json_decode($auth, 1); $socket = new hgSocket(); $con = $socket->connect($app['ip'], $app['port']); if (!intval($con)) { step6('安装无法完成,服务器无法连接,请确认服务器ip是否正确或服务程序hogeMonitor.py是否监听在' . $app['ip'] . ':' . $app['port'] . '上'); } $socket->close(); $password = md5($password); $content = get_serv_file($app, $app['dir'] . 'conf/config.php'); $string = "\$gGlobalConfig['admin_user'] = array(\n'{$user_name}' => '{$password}',\n);"; $content = preg_replace("/\\?>/is", "\n{$string}\n?>", $content); write_serv_file($app, $app['dir'] . 'conf/config.php', $content, 'utf8'); hg_run_cmd($app, 'runcmd', 'chmod -Rf 777 ' . $app['dir'] . 'cache/ ' . $app['dir'] . 'conf/config.php'); if ($db) { $pass = hg_encript_str($db['pass'], false, $auth['appkey']); $link = mysql_connect($db['host'], $db['user'], $pass); mysql_query("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary", $link); mysql_select_db($db['database'], $link); $db_data = json_encode($db); $app_data = json_encode($app); $sql = 'INSERT INTO ' . $db['dbprefix'] . "server (name, brief, ip, outip, type, more_data,create_time) VALUES \n\t\t\t\t('app数据库服务器', '', '{$db['host']}', '', 'db', '{$db_data}', " . time() . ");\n\t\t\t\t"; mysql_query($sql, $link); $sql = 'INSERT INTO ' . $db['dbprefix'] . "server (name, brief, ip, outip, type, more_data,create_time) VALUES \n\t\t\t\t('app服务器', '', '{$app['ip']}', '{$app['outip']}', 'app', '{$app_data}', " . time() . ");\n\t\t\t\t"; mysql_query($sql, $link); $server_id = mysql_insert_id(); $sql = 'INSERT INTO ' . $db['dbprefix'] . "server_domain (server_id, domain, dir, create_time) VALUES \n\t\t\t\t('{$server_id}', '{$app['domain']}', '{$app['dir']}', " . time() . ");\n\t\t\t\t"; mysql_query($sql, $link); } $appstore = new curl('appstore.hogesoft.com:233', ''); $appstore->mAutoInput = false; $appstore->setClient(CUSTOM_APPID, CUSTOM_APPKEY); $appstore->initPostData(); $appstore->addRequestData('a', 'installed'); $appstore->addRequestData('app', 'livworkbench'); $appstore->request('index.php'); header('Location:' . '?action=complete'); }
public function doupgrade() { $installinfo = array('host' => $this->settings['mcphost'], 'ip' => $this->settings['mcphost'], 'port' => 6233); if (!is_writeable('conf/config.php')) { hg_run_cmd($installinfo, 'runcmd', 'chmod -Rf 777 ' . realpath('conf/config.php')); } if (!is_writeable('conf/config.php')) { $message = '平台无法升级,请将此文件(conf/config.php)设为可写'; $this->upgrade($message); } $curl = new curl($this->product_server['host'] . ':' . $this->product_server['port'], $this->product_server['dir']); $curl->mAutoInput = false; $curl->setClient(CUSTOM_APPID, CUSTOM_APPKEY); $curl->initPostData(); $postdata = array('app' => 'livworkbench', 'a' => 'checklastversion'); foreach ($postdata as $k => $v) { $curl->addRequestData($k, $v); } $lastversion = $curl->request('check_version.php'); if ($this->settings['version'] >= $lastversion) { $this->ReportError('当前已是最新版本'); } $db = $this->dbconfig; $db['user'] = $this->input['dbuser']; $db['pass'] = $_REQUEST['dbpass']; $link = @mysql_connect($db['host'], $db['user'], $db['pass']); if (!$link) { $message = '此数据库无法连接,请确认密码是否准确'; $this->show($message); } mysql_query("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary", $link); $socket = new hgSocket(); $con = $socket->connect($installinfo['host'], $installinfo['port']); if (!intval($con)) { $message = '服务器无法连接,请确认服务器ip是否正确或服务程序hogeMonitor.py是否监听在' . $installinfo['ip'] . ':' . $installinfo['port'] . '上'; $this->show($message); } $socket->close(); $app_path = realpath(ROOT_PATH); $app_path .= '/'; $curl = new curl($this->product_server['host'] . ':' . $this->product_server['port'], ''); $curl->mAutoInput = false; $curl->setClient(CUSTOM_APPID, CUSTOM_APPKEY); $curl->setSubmitType('get'); $curl->setReturnFormat('json'); $curl->initPostData(); $curl->addRequestData('app', 'livworkbench'); $ret = $curl->request('db.php'); ob_start(); if (is_array($ret)) { //更新数据库 hg_flushMsg('开始更新数据库'); mysql_select_db($db['database'], $link); $structs = $this->getDbStruct($db['database'], $link); if (!$ret['app']) { $ret['app'] = array(); } foreach ($ret['app'] as $tab => $v) { $pre = substr($tab, 0, 4); if ($pre == 'liv_') { $newtab = DB_PREFIX . substr($tab, 4); } if ($pre == 'm2o_') { $newtab = DB_PREFIX . substr($tab, 4); } if (!$structs[$newtab]) { $addsql = $ret['create'][$tab]; if ($addsql) { $addsql = preg_replace('/CREATE\\s+TABLE\\s+([`]{0,1})' . $pre . '/is', 'CREATE TABLE \\1' . DB_PREFIX, $addsql); hg_flushMsg('新增数据表' . $newtab); mysql_query($addsql, $link); } continue; } $struct = $v['struct']; $index = $v['index']; if ($struct) { $altersql = array(); foreach ($struct as $f => $a) { if (!$structs[$newtab]['struct'][$f]) { if ($a['Null'] == 'NO') { $null = ' NOT NULL'; } else { $null = ' NULL'; } if ($a['Default']) { $default = " DEFAULT '{$a['Default']}'"; } else { $default = ''; } if ($a['Comment']) { $comment = " COMMENT '{$a['Comment']}'"; } else { $comment = ''; } $altersql[] = " ADD `{$f}` {$a['Type']}{$null}{$default}{$comment}"; } else { $cur = $structs[$newtab]['struct'][$f]; if ($a['Null'] == 'NO') { $null = ' NOT NULL'; } else { $null = ' NULL'; } if ($a['Default']) { $default = " DEFAULT '{$a['Default']}'"; } else { $default = ''; } if ($a['Comment']) { $comment = " COMMENT '{$a['Comment']}'"; } else { $comment = ''; } if ($a['Type'] != $cur['Type'] || $a['Default'] != $cur['Default']) { $altersql[] = " CHANGE `{$f}` `{$f}` {$a['Type']}{$null}{$default}{$comment}"; } } } if ($altersql) { hg_flushMsg('开始更新数据表' . $newtab); $altersql = 'ALTER TABLE ' . $newtab . ' ' . implode(',', $altersql); mysql_query($altersql, $link); } } if ($index) { foreach ($index as $unique => $ind) { if (!$ind) { continue; } if (!$unique) { $typ = 'UNIQUE'; } else { $typ = 'INDEX'; } foreach ($ind as $pk => $f) { if ($pk == 'PRIMARY') { continue; } $curind = $structs[$newtab]['index'][$unique][$pk]; if (!$curind) { $altersql = 'ALTER TABLE ' . $newtab . ' ADD ' . $typ . ' (' . implode(',', $f) . ')'; // echo $altersql . '<br />'; mysql_query($altersql, $link); } else { $change = array_diff($curind, $f); $change1 = array_diff($f, $curind); if ($change || $change1) { $altersql = 'ALTER TABLE ' . $newtab . ' DROP INDEX ' . $pk . ', ADD ' . $typ . ' (' . implode(',', $f) . ')'; // echo $altersql . '<br />'; mysql_query($altersql, $link); } } } } } $newindex = $index; $index = $structs[$newtab]['index']; if ($index) { foreach ($index as $unique => $ind) { if (!$ind) { continue; } if (!$unique) { $typ = 'UNIQUE'; } else { $typ = 'INDEX'; } foreach ($ind as $pk => $f) { if ($pk == 'PRIMARY') { continue; } $newind = $newindex[$unique][$pk]; if (!$curind) { $altersql = 'ALTER TABLE ' . $newtab . ' DROP INDEX ' . $pk; mysql_query($altersql, $link); } } } } $sql = 'OPTIMIZE TABLE ' . $newtab; mysql_query($sql, $link); } hg_flushMsg('数据库更新完毕'); } //下载程序 hg_flushMsg('开始下载应用程序更新包'); $curl = new curl($this->product_server['host'] . ':' . $this->product_server['port'], ''); $curl->mAutoInput = false; $curl->setClient(CUSTOM_APPID, CUSTOM_APPKEY); $curl->setSubmitType('get'); $curl->setReturnFormat('json'); $curl->initPostData(); $curl->addRequestData('app', 'livworkbench'); $program_url = $curl->request('check_version.php'); if (!(strstr($program_url, 'http://') && strstr($program_url, '.zip')) || $program_url == 'NO_VERSION') { $message = '获取应用程序失败或程序版本不存在.'; $this->show($message); } hg_run_cmd($installinfo, 'download', $program_url, $app_path); $appversion = @file_get_contents($app_path . 'version'); $match = preg_match('/^[0-9]+\\.[0-9]+\\.[0-9]+$/is', $appversion); if (!$match) { $appversion = ''; } if ($appversion < $lastversion) { $message = '平台程序更新失败,请重试.'; $this->upgrade($message); } hg_run_cmd($installinfo, 'runcmd', 'chmod +x ' . $app_path . 'cron/*.py'); $domain = $installinfo['host']; $dir = $installinfo['dir']; hg_flushMsg('应用程序包下载完成'); hg_flushMsg('应用设置更新完成'); hg_flushMsg('开始更新模板'); hg_flushMsg('模板更新完成'); $content = @file_get_contents('conf/config.php'); $match = preg_match("/\\\$gGlobalConfig\\['version'\\]\\s*=\\s*\\'.*\\';/is", $content); if ($match) { $content = preg_replace("/\\\$gGlobalConfig\\['version'\\]\\s*=\\s*\\'.*?\\';/is", "\$gGlobalConfig['version'] = '{$lastversion}';", $content); } else { $content = preg_replace("/\\?>/is", "\n\$gGlobalConfig['version'] = '{$lastversion}';\n?>", $content); } @file_put_contents('conf/config.php', $content); $sql = 'SELECT * FROM ' . DB_PREFIX . 'modules'; $q = $this->db->query($sql); $program = array(); while ($mod = $this->db->fetch_array($q)) { $program[$mod['id']][$mod['func_name']] = $mod['func_name']; } $sql = 'SELECT * FROM ' . DB_PREFIX . 'module_op'; $q = $this->db->query($sql); while ($mod = $this->db->fetch_array($q)) { $program[$mod['module_id']][$mod['op']] = $mod['op']; } if ($program) { hg_flushMsg('开始重建应用程序'); include ROOT_PATH . 'lib/class/program.class.php'; $rebuildprogram = new program(); foreach ($program as $mid => $ops) { if ($ops) { foreach ($ops as $op) { $rebuildprogram->compile($mid, $op); } } } hg_flushMsg('应用程序重建完成'); } //清理模板 $this->clearfile(CACHE_DIR . 'tpl/livworkbench/'); $this->clearfile(CACHE_DIR . 'tpl/css/livworkbench/'); $update_apps = @file_get_contents(CACHE_DIR . 'onekupdate'); $update_apps = json_decode($update_apps, 1); $tdb = $update_apps['okupdatedbinfo']; unset($update_apps['okupdatedbinfo']); if ($update_apps) { unset($update_apps['livworkbench']); if ($update_apps) { $update_apps['okupdatedbinfo'] = $tdb; $onekupdate = json_encode($update_apps); } else { $onekupdate = ''; } file_put_contents(CACHE_DIR . 'onekupdate', $onekupdate); } if ($this->input['onekupdate']) { $url = 'appstore.php?a=goonekupdate'; } else { $url = 'index.php'; } $this->appstore->initPostData(); $this->appstore->addRequestData('a', 'updated'); $this->appstore->addRequestData('app', 'livworkbench'); $this->appstore->request('index.php'); hg_flushMsg('平台更新成功', $url); //$this->redirect('应用更新成功', $url); }
private function update($url) { if (!$url) { return false; } $serverinfo = array('host' => $this->settings['mcphost'], 'ip' => $this->settings['mcphost'], 'port' => 6233); $socket = new hgSocket(); $con = $socket->connect($serverinfo['host'], $serverinfo['port']); if (!intval($con)) { echo $message = '服务器无法连接,请确认服务器ip是否正确或服务程序hogeMonitor.py是否监听在' . $serverinfo['ip'] . ':' . $serverinfo['port'] . '上'; $socket->close(); return false; } $socket->close(); $app_path = realpath(ROOT_PATH); $app_path .= '/'; hg_run_cmd($serverinfo, 'download', $url, $app_path); return true; }
public function add_appserver() { $app = $this->input['appserver']; if (!$app['ip']) { $message = '请填写服务器ip'; $this->redirect($message, 0, 0, '', "hg_add_appservererr_back('{$message}')"); } $app['ip'] = trim($app['ip']); $sql = 'SELECT * FROM ' . DB_PREFIX . "server WHERE ip = '{$app['ip']}' AND type != 'db'"; $q = $this->db->query_first($sql); if ($q) { $message = '此服务器ip已存在,无需重复添加'; $this->redirect($message, 0, 0, '', "hg_add_appservererr_back('{$message}')"); } $app['port'] = 6233; $socket = new hgSocket(); $con = $socket->connect($app['ip'], $app['port']); if (!intval($con)) { $message = '服务器无法连接,请确认服务器ip是否正确或服务程序hogeMonitor.py是否监听在' . $app['ip'] . ':' . $app['port'] . '上'; $socket->close(); $this->redirect($message, 0, 0, '', "hg_add_appservererr_back('{$message}')"); } $socket->close(); $app_data = json_encode($app); $sql = 'INSERT INTO ' . DB_PREFIX . "server (name, brief, ip, outip, type, more_data,create_time) VALUES \r\n\t\t\t\t('{$app['name']}', '', '{$app['ip']}', '{$app['outip']}', 'api', '{$app_data}', " . TIMENOW . ")"; $this->db->query($sql); $id = $this->db->insert_id(); $sql = 'SELECT * FROM ' . DB_PREFIX . "server_domain sd LEFT JOIN " . DB_PREFIX . 'server s ON sd.server_id=s.id'; $q = $this->db->query($sql); $hostscontent = get_serv_file($app, '/etc/hosts'); $hosts = hg_get_hosts($hostscontent); while ($r = $this->db->fetch_array($q)) { $domain = $r['domain']; if ($domain && !$hosts[$domain]) { $hostscontent .= "\n" . $r['ip'] . ' ' . $domain; } } write_serv_file($app, '/etc/hosts', $hostscontent); $this->redirect('应用服务器添加成功', 0, 0, '', "hg_add_appserver_back({$id}, '{$app['name']}_{$app['ip']}')"); }