protected function onExec() { if (!isCLI()) { if (ApiFw_::$SOLO) { header("Content-Type: text/plain; charset=UTF-8"); #header("Content-Type: application/json; charset=UTF-8"); } header("Cache-Control: no-cache"); } setServerRev(); $ac = param('_ac', null, $_GET); if (!isset($ac)) { // 支持PATH_INFO模式。 @($path = $this->getPathInfo()); if ($path != null) { $ac = $this->parseRestfulUrl($path); } } if (!isset($ac)) { $ac = mparam('ac', $_GET); } Conf::onApiInit(); dbconn(); global $DBH; if (!isCLI()) { session_start(); } $this->apiLog = new ApiLog($ac); $this->apiLog->logBefore(); // API调用监控 $this->apiWatch = new ApiWatch($ac); $this->apiWatch->execute(); if ($ac == "batch") { $useTrans = param("useTrans", false, $_GET); $ret = $this->batchCall($useTrans); } else { $ret = $this->call($ac, true); } return $ret; }
tool.php(ac="base64")(text, isDecode?) base64编码。如果isDecode=1则是解码。 */ $ac = param("ac"); if ($ac) { header("Content-Type: text/plain"); header("Cache-Control: no-cache"); if ($ac == "md5") { $text = mparam("text"); $val = md5($text); echo $val; } else { if ($ac == "base64") { $text = mparam("text"); $isDecode = (int) param("isDecode", 0); if ($isDecode) { $val = base64_decode($text); } else { $val = base64_encode($text); } echo $val; } else { die("Bad param `ac`"); } } exit; } // $col?=$_REQUEST function param($name, $defVal = null, $col = null)
function api_initDb() { $db = mparam("db"); $dbcred = mparam("dbcred"); $cfgonly = (int) param("cfgonly", 0); if (!$cfgonly) { $dbcred0 = mparam("dbcred0"); $dbcred_ro = param("dbcred_ro"); } $urlPath = mparam("urlPath"); if (!preg_match('/^(.*?)\\/(\\w+)$/', $db, $ms)) { die("数据库指定错误: `{$db}`"); } $dbhost = $ms[1]; $dbname = $ms[2]; if (!$cfgonly) { list($dbuser0, $dbpwd0) = explode(":", $dbcred0); if (!$dbuser0 || !isset($dbpwd0)) { die("数据库管理员用户名密码指定错误: `{$dbcred0}`"); } list($dbuser, $dbpwd) = explode(":", $dbcred); if (!$dbuser || !isset($dbpwd)) { die("应用程序使用的数据库用户名密码指定错误: `{$dbcred}`"); } if ($dbcred_ro) { list($dbuser_ro, $dbpwd_ro) = explode(":", $dbcred_ro); } $dbh = dbconn($dbhost, null, $dbuser0, $dbpwd0); try { $dbh->exec("use {$dbname}"); echo "=== 数据库`{$dbname}`已存在。\n"; } catch (Exception $e) { echo "=== 创建数据库: {$dbname}\n"; try { $dbh->exec("create database {$dbname}"); } catch (Exception $e) { die("*** 用户`{$dbuser0}`无法创建数据库!\n"); } $dbh->exec("use {$dbname}"); } echo "=== 设置用户权限: {$dbuser}\n"; try { $str = $dbpwd ? " identified by '{$dbpwd}'" : ""; $sql = "grant all on {$dbname}.* to {$dbuser}@localhost {$str}"; $dbh->exec($sql); $sql = "grant all on {$dbname}.* to {$dbuser}@'%' {$str}"; $dbh->exec($sql); } catch (Exception $e) { die("*** 用户`{$dbuser0}`无法设置用户权限!\n"); } if ($dbcred_ro) { echo "=== 设置只读用户权限: {$dbuser_ro}\n"; $str = $dbpwd_ro ? " identified by '{$dbpwd_ro}'" : ""; $sql = "grant select, lock tables, show view on {$dbname}.* to {$dbuser_ro} {$str}"; $dbh->exec($sql); $sql = "grant select on mysql.* to {$dbuser_ro}"; $dbh->exec($sql); $sql = "grant reload, replication client, replication slave on *.* to {$dbuser_ro}"; $dbh->exec($sql); } } echo "=== 写配置文件 " . CONF_FILE . "\n"; $dbcred_b64 = base64_encode($dbcred); $adminCred = base64_encode(param("adminCred", "")); $str = <<<EOL <?php if (getenv("P_DB") === false) { \tputenv("P_DB={$db}"); \tputenv("P_DBCRED={$dbcred_b64}"); } putenv("P_URL_PATH={$urlPath}"); putenv("P_ADMIN_CRED={$adminCred}"); EOL; file_put_contents(CONF_FILE, $str); echo "=== 完成! 请使用upgrade命令行工具更新数据库。\n"; }
function api_sendSms() { checkAuth(AUTH_EMP); $phone = mparam("phone"); $content = mparam("content"); $channel = param("channel", 0); sendSms($phone, $content, $channel, true); }