<?php /* static settings */ $user = '<github_username>' ; $repo = '<github_reponame>' ; $user_repo = $user . '/' . $repo; $tree_base_url = "http://github.com/api/v2/json/tree/show/" . $user_repo; // path on the server where your repository will go $stage_dir = $_SERVER[ 'DOCUMENT_ROOT' ] . dirname($_SERVER[ 'SCRIPT_NAME' ]); ?>
<?php /* static settings */ $user = '<github_username>' ; $repo = '<github_reponame>' ; $user_repo = $user . '/' . $repo; $tree_base_url = "http://github.com/api/v2/json/tree/show/" . $user_repo; // path on the server where your repository will go $stage_dir = $_SERVER[ 'DOCUMENT_ROOT' ] . dirname($_SERVER[ 'SCRIPT_NAME' ]); ?>
<?php /* static settings */ $user = '<github_username>' ; $repo = '<github_reponame>' ; $user_repo = $user . '/' . $repo; $tree_base_url = "http://github.com/api/v2/json/tree/show/" . $user_repo; // path on the server where your repository will go $stage_dir = $_SERVER[ 'DOCUMENT_ROOT' ] . dirname($_SERVER[ 'SCRIPT_NAME' ]); ?>
<?php /* static settings */ $user = '<github_username>' ; $repo = '<github_reponame>' ; $user_repo = $user . '/' . $repo; $tree_base_url = "http://github.com/api/v2/json/tree/show/" . $user_repo; // path on the server where your repository will go $stage_dir = $_SERVER[ 'DOCUMENT_ROOT' ] . dirname($_SERVER[ 'SCRIPT_NAME' ]); ?>
<?php /* static settings */ $user = '<github_username>' ; $repo = '<github_reponame>' ; $user_repo = $user . '/' . $repo; $tree_base_url = "http://github.com/api/v2/json/tree/show/" . $user_repo; // path on the server where your repository will go $stage_dir = $_SERVER[ 'DOCUMENT_ROOT' ] . dirname($_SERVER[ 'SCRIPT_NAME' ]); ?>
<?php /* static settings */ $user = '<github_username>' ; $repo = '<github_reponame>' ; $user_repo = $user . '/' . $repo; $tree_base_url = "http://github.com/api/v2/json/tree/show/" . $user_repo; // path on the server where your repository will go $stage_dir = $_SERVER[ 'DOCUMENT_ROOT' ] . dirname($_SERVER[ 'SCRIPT_NAME' ]); ?>
<?php /* static settings */ $user = '<github_username>' ; $repo = '<github_reponame>' ; $user_repo = $user . '/' . $repo; $tree_base_url = "http://github.com/api/v2/json/tree/show/" . $user_repo; // path on the server where your repository will go $stage_dir = $_SERVER[ 'DOCUMENT_ROOT' ] . dirname($_SERVER[ 'SCRIPT_NAME' ]); ?>
<?php /* static settings */ $user = '<github_username>' ; $repo = '<github_reponame>' ; $user_repo = $user . '/' . $repo; $tree_base_url = "http://github.com/api/v2/json/tree/show/" . $user_repo; // path on the server where your repository will go $stage_dir = $_SERVER[ 'DOCUMENT_ROOT' ] . dirname($_SERVER[ 'SCRIPT_NAME' ]); ?>
<?php /* static settings */ $user = '<github_username>' ; $repo = '<github_reponame>' ; $user_repo = $user . '/' . $repo; $tree_base_url = "http://github.com/api/v2/json/tree/show/" . $user_repo; // path on the server where your repository will go $stage_dir = $_SERVER[ 'DOCUMENT_ROOT' ] . dirname($_SERVER[ 'SCRIPT_NAME' ]); ?>
- <? php
- / * gets url * /
- function get_content_from_github ($ url)
- {
- $ ch = curl_init ();
- curl_setopt ($ ch, CURLOPT_URL, $ url);
- curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 1);
- echo "Getting: {$ url}" ;
- $ content = curl_exec ($ ch);
- curl_close ($ ch);
- return $ content;
- }
- ?>
- <? php
- function get_repo_json ()
- {
- global $ user, $ repo, $ user_repo, $ tree_base_url, $ stage_dir;
- $ json = array ();
- $ list_commits_url = 'http://github.com/api/v2/json/commits/list/' . $ user_repo. '/ master' ;
- echo "Master branch url: {$ list_commits_url} \ n <br>" ;
- $ json [ 'commit' ] = json_decode (get_content_from_github ($ list_commits_url), true );
- // get sha for the latest tree
- $ tree_sha = $ json [ 'commit' ] [ 'commits' ] [0] [ 'tree' ];
- echo "Tree sha: {$ tree_sha} \ n <br>" ;
- $ cont_str = $ tree_base_url. "/ {$ tree_sha}" ;
- $ base = json_decode (get_content_from_github ($ cont_str), true );
- // output project structure
- echo "<pre>" ;
- get_repo ($ base [ 'tree' ], 0, $ stage_dir);
- echo "</ pre>" ;
- }
- ?>
- <? php
- function get_repo ($ objects, $ level = 0, $ current_dir)
- {
- global $ tree_base_url, $ user_repo;
- chdir ($ current_dir);
- foreach ($ objects as & $ object )
- {
- $ type = $ object [ 'type' ];
- $ sha = $ object [ 'sha' ];
- $ name = $ object [ 'name' ];
- // add padding
- echo str_pad ( "" , $ level, "\ t" );
- echo $ name. "\ n" ;
- if (strcmp ($ type, "tree" ) == 0)
- {
- mkdir ($ name);
- $ new_dir = $ current_dir. '/' . $ name;
- $ tree = $ tree_base_url. '/' . $ sha;
- $ new_objects = json_decode (get_content_from_github ($ tree), true );
- get_repo ($ new_objects [ 'tree' ], $ level + 1, $ new_dir);
- // change current directory back
- chdir ($ current_dir);
- }
- else
- {
- // get file content
- $ blob_url = "http://github.com/api/v2/json/blob/show/" . $ user_repo. "/" . $ sha;
- $ data = get_content_from_github ($ blob_url);
- $ filename = $ current_dir. '/' . $ name;
- file_put_contents ($ filename, $ data);
- }
- }
- }
- ?>
- <? php
- $ download = true ;
- $ unzip = true ;
- $ move = true ;
- $ stage_dir = $ _SERVER [ 'DOCUMENT_ROOT' ]. dirname ($ _ SERVER [ 'SCRIPT_NAME' ]);
- $ filepath = $ stage_dir. '/' . 'master.zip' ;
- echo "<pre>" ;
- ?>
- <? php
- if ($ download)
- {
- $ url = "http://github.com/<your_github_username>/<your_github_repo_name>/zipball/master" ;
- $ ch = curl_init ();
- curl_setopt ($ ch, CURLOPT_URL, $ url);
- curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 1);
- echo "Getting: {$ url} \ n" ;
- $ content = curl_exec ($ ch);
- echo "Got \" {$ content} \ "\ n" ;
- curl_close ($ ch);
- $ dom = new DOMDocument ();
- @ $ dom-> loadHTML ($ content);
- $ xpath = new DOMXPath ($ dom);
- $ hrefs = $ xpath-> evaluate ( "/ html / body // a" );
- $ href = $ hrefs-> item (0);
- $ zipurl = $ href-> getAttribute ( 'href' );
- echo "Zip url: {$ zipurl} \ n" ;
- $ data = http_get_file ($ zipurl);
- if (substr ($ data, "http: //" ))
- {
- $ data = http_get_file ($ data);
- }
- file_put_contents ($ filepath, $ data);
- }
- ?>
- <? php
- function http_get_file ($ url)
- {
- $ url_stuff = parse_url ($ url);
- $ port = isset ($ url_stuff [ 'port' ])? $ url_stuff [ 'port' ]: 80;
- $ path = $ url_stuff [ 'path' ];
- $ last = $ path [strlen ($ path) -1];
- if (strcmp ($ last, "_" ) == 0)
- {
- $ path = substr_replace ($ path, "" , -1);
- }
- $ fp = fsockopen ($ url_stuff [ 'host' ], $ port);
- $ query = 'GET' . $ path. "HTTP / 1.0 \ n" ;
- $ query. = 'Host:' . $ url_stuff [ 'host' ];
- $ query. = "\ n \ n" ;
- fwrite ($ fp, $ query);
- while ($ line = fread ($ fp, 1024))
- {
- $ buffer. = $ line;
- }
- if (preg_match ( '/ ^ Location: (. +?) $ / m' , $ buffer, $ matches))
- {
- return $ matches [1];
- }
- preg_match ( '/ Content-Length: ([0-9] +) /' , $ buffer, $ parts);
- return substr ($ buffer, - $ parts [1]);
- }
- ?>
- <? php
- if ($ unzip)
- {
- echo "Uncompressing archive ... \ n" ;
- $ zip = new ZipArchive;
- $ res = $ zip-> open ($ filepath);
- if ($ res === TRUE)
- {
- $ zip-> extractTo ($ stage_dir);
- $ zip-> close ();
- echo "Done! \ n" ;
- } else
- {
- echo "Failed \ n" ;
- exit (1);
- }
- }
- ?>
- <? php
- if ($ move)
- {
- $ files = scandir ($ stage_dir);
- $ match_array = preg_grep ( '/ <user_name> * /' , $ files);
- if (is_array ($ match_array))
- {
- // remove all directory if any
- delete_directory ( "code" );
- $ dir_name = current ($ match_array);
- $ rep_dir = $ dir_name. "/ code" ;
- echo "Try to move {$ rep_dir} to code \ n" ;
- rename ($ rep_dir, "code" );
- rmdir ($ dir_name);
- echo "Done moving files \ n" ;
- }
- }
- function delete_directory ($ dirname)
- {
- if (is_dir ($ dirname))
- $ dir_handle = opendir ($ dirname);
- if (! $ dir_handle)
- return false ;
- while ($ file = readdir ($ dir_handle))
- {
- if ($ file! = "." && $ file! = ".." )
- {
- if (! is_dir ($ dirname. "/" . $ file))
- unlink ($ dirname. "/" . $ file);
- else
- delete_directory ($ dirname. '/' . $ file);
- }
- }
- closedir ($ dir_handle);
- rmdir ($ dirname);
- }
- ?>
* This source code was highlighted with Source Code Highlighter .
- <? php
- copy ( "config.php" , "<new_path> /core.php" );
- echo "All jobs have been done! \ n" ;
- echo "</ pre>" ;
- ?>
Source: https://habr.com/ru/post/83235/
All Articles