function dispatch($request_uri, $options) { /* parse request uri start * ----------------------------------- */ $p = strpos($request_uri, '?'); $request_uri = $p !== false ? substr($request_uri, 0, $p) : $request_uri; /* security request uri filter. */ if (preg_match('/(\\.\\.|\\"|\'|<|>)/', $request_uri)) { return array('errno' => 403, 'data' => "permission denied."); } /* get display format. */ if (($p = strrpos($request_uri, '.')) !== false) { $tail = substr($request_uri, $p + 1); if (preg_match('/^[a-zA-Z0-9]+$/', $tail)) { $display = $tail; //'json' $request_uri = substr($request_uri, 0, $p); } } /* get version, module, id, method. */ $requests = array_pad(explode('/', $request_uri, 5), 5, NULL); list(, $module, $id, $method) = $requests; /* default format: json */ empty($display) && ($display = 'json'); empty($method) && ($method = 'get'); if (isset($_GET['id'])) { $id = $_GET['id']; } $_ENV['module'] = $module; $_ENV['id'] = $id; $_ENV['method'] = $method; $_ENV['display'] = $display; //$_ENV['version']= $version; /*------------------------------------- * parse request uri end */ if (empty($module)) { $dt = array('errno' => CYE_PARAM_ERROR, 'code' => 404, 'data' => 'Not found.'); goto end; } if ($module == 'static') { $file = CY_HOME . '/app' . $request_uri . '.' . $display; if (is_file($file)) { $content = file_get_contents($file); return ['errno' => 0, 'code' => 200, 'data' => $content]; } } $classname = 'CA_Entry_' . $module; if (!method_exists($classname, $method) && !method_exists($classname, '__call')) { $classname .= '_' . $method; $run = isset($_GET['a']) ? $_GET['a'] : 'run'; if (!method_exists($classname, $run)) { $errno = CYE_PARAM_ERROR; $error = "method is not exists {$classname}:{$method}"; $dt = array('errno' => $errno, 'code' => 404, 'data' => 'unkown request.', 'error' => $error); goto end; } $eny = new $classname(); $dt = $eny->{$method}($id, $_REQUEST, $_ENV); } else { $eny = new $classname(); $dt = $eny->{$method}($id, $_REQUEST, $_ENV); } unset($eny); if ($dt['errno'] !== 0) { cy_log(CYE_ERROR, json_encode($dt)); if ($dt['errno'] != CYE_PARAM_ERROR) { $dt['code'] = 500; } else { $dt['code'] = 200; } if (empty($dt['error'])) { $dt['data'] = 'Internal error.'; } else { $dt['data'] = $dt['error']; } } else { $dt['code'] = 200; } end: if ($display === 'html' || $display === 'php') { if ($dt['errno'] == 0) { $files = array(); $files[] = CY_HOME . '/app/html/' . $module . '/' . $method . '.' . $display; $files[] = CY_HOME . '/app/html/' . $module . '.' . $display; $files[] = CY_HOME . '/app/html/default.' . $display; foreach ($files as $i => $file) { if (is_file($file)) { break; } if ($i === 1 && $display === 'html') { $_ENV['display'] = $display = 'php'; goto end; } } } else { $file = CY_HOME . '/app/html/error.' . $display; } } else { $file = NULL; } $t = new CY_Util_Output(); $t->assign(['errno' => $dt['errno'], 'data' => $dt['data']]); $data = $t->get($file); return ['errno' => 0, 'code' => $dt['code'], 'data' => $data]; }
$eny = new $classname(); $dt = $eny->{$_g_method}($_g_id, $_REQUEST, $_ENV); } if (!isset($dt['errno'])) { exit("Bad entry return value."); } end: if ($_g_display === 'html' || $_g_display === 'php') { if ($dt['errno'] == 0) { $files = array(); $files[] = CY_HOME . '/app/html/' . $_g_module . '/' . $_g_method . '.' . $_g_display; $files[] = CY_HOME . '/app/html/' . $_g_module . '.' . $_g_display; $files[] = CY_HOME . '/app/html/default.' . $_g_display; foreach ($files as $i => $file) { if (is_file($file)) { break; } if ($i === 1 && $_g_display === 'html') { $_ENV['display'] = $_g_display = 'php'; goto end; } } } else { $file = CY_HOME . '/app/html/error.' . $_g_display; } } else { $file = NULL; } $t = new CY_Util_Output(); $t->assign($dt); $t->render($file);
/** * 脚本执行终止 * cy_exit可以跟据请求的不同,输入json,xml,html等不同格式的错误信息 * * @author xiaoyjy@gmail.com * * @param int $errno error code int lib/util/errno.php * @param string $message user defined error message * @param CY_Util_Output $t output template */ function cy_exit($errno = 0, $message = NULL, $t = NULL) { /* Many be used in shutdown functions. */ $_ENV['errno'] = $errno; $data = []; /* hacked, return data by message. */ if (is_array($message)) { $tmp = $message; $error = isset($message['error']) ? $message['error'] : NULL; unset($tmp['error']); $data += $tmp; } elseif (is_string($message)) { $error = $message; } /* First field must be 'result'. */ $data = array('errno' => $errno, 'error' => empty($error) ? cy_strerror($errno) : $error, 'exitting' => true); $t == NULL && ($t = new CY_Util_Output()); $t->assign($data); $file = $_ENV['display'] === 'html' || $_ENV['display'] === 'php' ? CY_HOME . '/app/html/error.' . $_ENV['display'] : NULL; echo $t->render($file); /* useful in nginx, useless in apache. */ if (function_exists('fastcgi_finish_request')) { /* if use fastcgi, we may finish it first. */ fastcgi_finish_request(); } exit($errno); }