/** * * spUrl * * URL模式的构建函数 * * @param controller 控制器名称,默认为配置'default_controller' * @param action 动作名称,默认为配置'default_action' * @param args 传递的参数,数组形式 * @param anchor 跳转锚点 * @param no_sphtml 是否应用spHtml设置,在FALSE时效果与不启用spHtml相同。 */ function spUrl($controller = null, $action = null, $args = null, $anchor = null, $no_sphtml = FALSE) { if (TRUE == $GLOBALS['G_SP']['html']["enabled"] && TRUE != $no_sphtml) { // 当开启HTML生成时,将查找HTML列表获取静态文件名称。 $realhtml = spHtml::getUrl($controller, $action, $args, $anchor); if (isset($realhtml[0])) { return $realhtml[0]; } } $controller = null != $controller ? $controller : $GLOBALS['G_SP']["default_controller"]; $action = null != $action ? $action : $GLOBALS['G_SP']["default_action"]; // 使用挂靠点 if ($launch = spLaunch("function_url", array('controller' => $controller, 'action' => $action, 'args' => $args, 'anchor' => $anchor, 'no_sphtml' => $no_sphtml), TRUE)) { return $launch; } if (TRUE == $GLOBALS['G_SP']['url']["url_path_info"]) { // 使用path_info方式 $url = $GLOBALS['G_SP']['url']["url_path_base"] . "/{$controller}/{$action}"; if (null != $args) { foreach ($args as $key => $arg) { $url .= "/{$key}/{$arg}"; } } } else { $url = $GLOBALS['G_SP']['url']["url_path_base"] . "?" . $GLOBALS['G_SP']["url_controller"] . "={$controller}&"; $url .= $GLOBALS['G_SP']["url_action"] . "={$action}"; if (null != $args) { foreach ($args as $key => $arg) { $url .= "&{$key}={$arg}"; } } } if (null != $anchor) { $url .= "#" . $anchor; } return $url; }
/** * 生成单个静态页面 * * @param spurl spUrl的参数 * @param alias_url 生成HTML文件的名称,如果不设置alias_url,将使用年月日生成目录及随机数为文件名的形式生成HTML文件。 * @param update_mode 更新模式,默认2为同时更新列表及文件 * 0是仅更新列表 * 1是仅更新文件 */ public function make($spurl, $alias_url = null, $update_mode = 2) { if (1 == spAccess('r', 'sp_html_making')) { $this->spurls[] = array($spurl, $alias_url); return; } @(list($controller, $action, $args, $anchor) = $spurl); if ($url_item = spHtml::getUrl($controller, $action, $args, $anchor, TRUE)) { @(list($baseuri, $realfile) = $url_item); $update_mode = 1; } else { $file_root_name = '' == $GLOBALS['G_SP']['html']['file_root_name'] ? '' : $GLOBALS['G_SP']['html']['file_root_name'] . '/'; if (null == $alias_url) { $filedir = $file_root_name . date('Y/n/d') . '/'; $filename = substr(time(), 3, 10) . substr(mt_rand(100000, substr(time(), 3, 10)), 4) . ".html"; } else { $filedir = $file_root_name . dirname($alias_url) . '/'; $filename = basename($alias_url); } $baseuri = rtrim(dirname($GLOBALS['G_SP']['url']["url_path_base"]), '/\\') . "/" . $filedir . $filename; $realfile = APP_PATH . "/" . $filedir . $filename; } if (0 == $update_mode or 2 == $update_mode) { spHtml::setUrl($spurl, $baseuri, $realfile); } if (1 == $update_mode or 2 == $update_mode) { $remoteurl = 'http://' . $_SERVER["SERVER_NAME"] . ':' . $_SERVER['SERVER_PORT'] . '/' . ltrim(spUrl($controller, $action, $args, $anchor, TRUE), '/\\'); $cachedata = file_get_contents($remoteurl); if (FALSE === $cachedata) { $cachedata = $this->curl_get_file_contents($remoteurl); if (FALSE === $cachedata) { spError("无法从网络获取页面数据,请检查:<br />1. spUrl生成地址是否正确!<a href='{$remoteurl}' target='_blank'>点击这里测试</a>。<br />2. 设置php.ini的allow_url_fopen为On。<br />3. 检查是否防火墙阻止了APACHE/PHP访问网络。<br />4. 建议安装CURL函数库。"); } } __mkdirs(dirname($realfile)); file_put_contents($realfile, $cachedata); } }