/** * Route client request * * If the route does not exist, the function will send a HTTP error * 400 and stop execution. This function adds all all arguments in * $_REQUEST to the params array, as well as the regex matches, as * $param[0], $param[1] etc. * @param $verb e.g. $_SERVER['REQUEST_METHOD'] * @param $url e.g. first argument in $_SERVER['QUERY_STRING'] * @param $param additional arguments to add * @return return value from function being invoked */ function route($verb, $url, $param = array()) { global $routes; $verb = strtoupper($verb); if (!@is_array($routes[$verb])) { return false; } foreach ($_REQUEST as $key => $val) { // the first parameter will end up as $param[0], $param[1], etc anyway, so ignore it here if ($key === array_keys($_REQUEST)[0] && empty($val)) { continue; } $param[$key] = $val; } foreach ($routes[$verb] as $pattern => $func) { $found = @preg_match('/^' . str_replace('/', '\\/', $pattern) . '$/', $url, $matches); if ($found) { foreach ($matches as $key => $val) { $param[$key] = $val; } return $func($param); } } // nothing matched, return 400 router_error_400('Route ' . $verb . ' ' . $url . ' does not exist'); }
/** * Create a repository on GitHub and push a local (temporary) repository to it */ function github_post_repo($param = array()) { if (empty($param['github_access_token'])) { router_error_400('Required parameter github_access_token missing or empty'); } if (empty($param['github_repo_name'])) { router_error_400('Required parameter github_repo_name missing or empty'); } if (empty($param['temp'])) { router_error_400('Required parameter temp missing or empty'); } $github_repo = github_create_repo($param['github_access_token'], $param['github_repo_name']); if ($github_repo === false) { router_error_500('Error creating GitHub repository ' . $param['github_repo_name'] . '. Make sure there is no existing repository with the same name.'); } $ret = github_add_collaborator($param['github_access_token'], $github_repo, config('github_push_as')); if ($ret === false) { router_error_500('Error adding ' . config('github_push_as') . ' as a collaborator to ' . $github_repo); } $ret = github_add_webhook($param['github_access_token'], $github_repo); if ($ret === false) { router_error_500('Error adding webhook to ' . $github_repo); } $modified = repo_get_modified_files($param['temp']); $ret = repo_stage_files($param['temp'], $modified); if ($ret === false) { router_error_500('Error staging files ' . implode(', ', $modified) . ' to ' . $param['temp']); } $ret = repo_commit($param['temp'], 'Initial commit'); if ($ret === false) { router_error_500('Error committing ' . $param['temp']); } $ret = repo_push($param['temp'], 'ssh://git@github.com/' . $github_repo . '.git'); if ($ret === false) { router_error_500('Error pushing to ' . $github_repo . '. Make sure all checks in setup.php pass.'); } // add to projects.json // XXX (later): create helper functions, make atomic $s = @file_get_contents(rtrim(config('content_dir', 'content'), '/') . '/projects.json'); $projects = @json_decode($s, true); if (!@is_array($projects)) { $projects = array(); } $projects[] = array('created' => time(), 'updated' => time(), 'github_repo' => $github_repo, 'parent' => repo_get_url($param['temp'])); $old_umask = @umask(00); @file_put_contents(rtrim(config('content_dir', 'content'), '/') . '/projects.json', json_encode($projects)); @umask($old_umask); return $github_repo; }
/** * Move modified files from a temporary (working) repository on top of another (template) repository * @param $param[1] temporary repository * @param $param['repo'] repository to switch to * @param $param['clean_before'] run "make clean" before determining the modified files (default: true) * @param $param['clean_after'] run "make clean" after determining the modified files (default: true) */ function api_post_temp_switch_repo($param = array()) { $temp = $param[1]; if (!@is_dir(tmp_dir($temp))) { router_error_404('Cannot get ' . $temp); } if (empty($param['repo'])) { router_error_400('Required parameter repo missing or empty'); } else { $repo = $param['repo']; } // client can specify whether to run "make clean" before or after the switch if (isset($param['clean_before'])) { $clean_before = (bool) $param['clean_before']; } else { $clean_before = true; } if (isset($param['clean_after'])) { $clean_after = (bool) $param['clean_after']; } else { $clean_after = true; } // create a new repository under a temporary name $staging = get_repo($repo); if ($staging === false) { router_error_404('Cannot get ' . $repo); } // clean, if requested if ($clean_before) { make_run($temp, 'clean'); } // copy the modified files over $old_umask = @umask(00); foreach (repo_get_modified_files($temp) as $fn) { // make sure the containing directories exist create_containing_dir(tmp_dir($staging) . '/' . $fn); // copy @copy(tmp_dir($temp) . '/' . $fn, tmp_dir($staging) . '/' . $fn); } @umask($old_umask); // remove original repository if (false === rm_recursive(tmp_dir($temp))) { router_error_500('Cannot delete ' . $temp); } // move new repository to location of original repository if (false === @rename(tmp_dir($staging), tmp_dir($temp))) { router_error_500('Cannot rename ' . $staging . ' to ' . $temp); } // clean, if requested if ($clean_after) { make_run($temp, 'clean'); } return true; }
it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ @(require_once 'config.inc.php'); require_once 'router.inc.php'; require_once 'util.inc.php'; $query = $_SERVER['QUERY_STRING']; // use the first URL argument for the router $pos = strpos($query, '&'); if ($pos !== false) { $query = substr($query, 0, $pos); } else { if (empty($query)) { $query = config('default_route', ''); } } // render view and return HTML if (@is_file('view-' . $query . '.php')) { echo render_php('view-' . $query . '.php'); } else { router_error_400('Route ' . $query . ' does not exist'); }