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); }
public function update() { $id = intval($this->input['id']); if (!$id) { $this->ReportError('指定记录不存在或已删除!'); } $name = trim($this->input['name']); if (!$name) { $this->form('请填写名称'); } $op = $this->db->query_first('SELECT * FROM ' . DB_PREFIX . 'module_op WHERE id=' . $id); if (!$op) { $this->ReportError('指定记录不存在或已删除!'); } $module_id = intval($this->input['module_id']); foreach ($this->fields as $field) { ${$field} = trim(urldecode($this->input[$field])); /*$$field = unserialize($op[$field]); if (!$$field) { $$field = array(); } $_field = $$field; if ($this->input[$field]) { $_field[$module_id] = $this->input[$field]; } else { unset($_field[$module_id]); } if ($_field) { $$field = serialize($_field); } else { $$field = ''; }*/ } /*$ban = unserialize($op['ban']); //禁用某操作以窜行化数组纪录 $isban = intval($this->input['ban']); if ($isban) { $ban[$module_id] = $module_id; } else { unset($ban[$module_id]); } $is_global = intval($this->input['is_global']); if ($is_global) { $module_id = 0; } $ban = serialize($ban);//禁用某操作以窜行化数组纪录 */ //检测是否添加过 $data = array('name' => $name, 'module_id' => $module_id, 'op' => $this->input['op'], 'brief' => $this->input['brief'], 'host' => $this->input['host'], 'dir' => $this->input['dir'], 'file_name' => $file_name, 'func_name' => $this->input['func_name'], 'template' => $template, 'has_batch' => $this->input['has_batch'], 'need_confirm' => $this->input['need_confirm'], 'callback' => $callback, 'request_type' => $request_type, 'direct_return' => $direct_return, 'group_op' => $this->input['group_op'], 'trigger_pub' => $trigger_pub, 'show_pub' => $show_pub, 'is_show' => $this->input['is_show'], 'ban' => intval($this->input['ban']), 'order_id' => $this->input['order_id']); hg_fetch_query_sql($data, 'module_op', 'id=' . $id); include ROOT_PATH . 'lib/class/program.class.php'; $program = new program(); $id = $program->compile($module_id, $data['op']); $this->redirect('更新成功'); }
public function rebuild_templates($appid = 0) { if ($appid) { $cond = ' WHERE application_id=' . $appid; } $sql = 'SELECT a.*, m.* FROM ' . DB_PREFIX . 'modules m LEFT JOIN ' . DB_PREFIX . 'applications a ON m.application_id=a.id ' . $cond; $q = $this->db->query($sql); $templates = array(); $program = array(); $modid = array(); $modules = array(); while ($mod = $this->db->fetch_array($q)) { $modid[] = $mod['id']; if ($mod['template']) { $templates[$mod['id']][] = $mod['template']; } $program[$mod['id']][$mod['func_name']] = $mod['func_name']; $modules[$mod['id']] = $mod; } $cond = ''; if ($appid && $modid) { $cond = ' WHERE module_id IN (' . implode(',', $modid) . ')'; } $sql = 'SELECT * FROM ' . DB_PREFIX . 'module_op' . $cond; $q = $this->db->query($sql); while ($mod = $this->db->fetch_array($q)) { $program[$mod['module_id']][$mod['op']] = $mod['op']; if ($mod['template']) { $templates[$mod['module_id']][] = $mod['template']; } } if ($templates) { hg_flushMsg('开始更新模板'); $configtpls = array('settings', 'iframe_list'); foreach ($templates as $m => $tpl) { if ($modules[$m]) { hg_flushMsg('开始更新' . $modules[$m]['name'] . '模板'); $this->tpl->setSoftVar($modules[$m]['softvar']); //设置软件界面 if (DEVELOP_MODE) { $this->tpl->setTemplateVersion(''); $this->tpl->setScriptDir(''); } else { $this->tpl->setScriptDir('app_' . $modules[$m]['softvar'] . '/'); $this->tpl->setTemplateVersion($modules[$m]['softvar'] . '/' . $modules[$m]['version']); } if (is_array($tpl)) { foreach ($tpl as $t) { hg_flushMsg('开始更新模板' . $t); $this->tpl->recacheTemplate($t); } } hg_flushMsg($modules[$m]['name'] . '模板更新完成'); } } foreach ($configtpls as $t) { hg_flushMsg('开始更新模板' . $t); $this->tpl->recacheTemplate($t); } hg_flushMsg('模板更新完成'); } if ($appid && $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('模块程序重建完成'); } }