diff options
-rw-r--r-- | Zotlabs/Module/Admin.php | 34 | ||||
-rw-r--r-- | Zotlabs/Storage/GitRepo.php | 55 | ||||
-rwxr-xr-x | view/tpl/admin_plugins.tpl | 3 | ||||
-rw-r--r-- | view/tpl/admin_plugins_addrepo.tpl | 1 |
4 files changed, 72 insertions, 21 deletions
diff --git a/Zotlabs/Module/Admin.php b/Zotlabs/Module/Admin.php index b4c372f84..c068110a9 100644 --- a/Zotlabs/Module/Admin.php +++ b/Zotlabs/Module/Admin.php @@ -1356,6 +1356,7 @@ class Admin extends \Zotlabs\Web\Controller { '$post' => 'admin/plugins/addrepo', '$desc' => t('Enter the public git repository URL of the plugin repo.'), '$repoURL' => array('repoURL', t('Plugin repo git URL'), '', ''), + '$repoName' => array('repoName', t('Custom repo name'), '', '', t('(optional)')), '$submit' => t('Download Plugin Repo') ) ); @@ -1708,12 +1709,37 @@ class Admin extends \Zotlabs\Web\Controller { case 'addrepo': require_once('library/markdown.php'); if(array_key_exists('repoURL',$_REQUEST)) { - require __DIR__ . '/../../library/PHPGit.autoload.php'; // Load PHPGit dependencies - logger('Repo URL submitted: ' . $_REQUEST['repoURL']); + require __DIR__ . '/../../library/PHPGit.autoload.php'; // Load PHPGit dependencies $repoURL = $_REQUEST['repoURL']; - $git = new GitRepo('sys', $repoURL, true); + $extendDir = __DIR__ . '/../../store/git/sys/extend'; + $addonDir = $extendDir.'/addon'; + if(!file_exists($extendDir)) { + if(!mkdir($extendDir, 0770, true)) { + logger('Error creating extend folder: ' . $extendDir); + json_return_and_die(array('message' => 'Error creating extend folder: ' . $extendDir, 'success' => false)); + } else { + if(!symlink(__DIR__ . '/../../extend/addon', $addonDir)) { + logger('Error creating symlink to addon folder: ' . $addonDir); + json_return_and_die(array('message' => 'Error creating symlink to addon folder: ' . $addonDir, 'success' => false)); + } + } + } + $repoName = null; + if(array_key_exists('repoName',$_REQUEST)) { + $repoName = $_REQUEST['repoName']; + logger('repoName: ' . $repoName); + } else { + $repoName = GitRepo::getRepoNameFromURL($repoURL); + } + if(!$repoName) { + logger('Invalid git repo'); + json_return_and_die(array('message' => 'Invalid git repo', 'success' => false)); + } + $repoDir = $addonDir.'/'.$repoName; + // clone the repo if new automatically + $git = new GitRepo('sys', $repoURL, true, $repoName, $repoDir); - $repo = $git->probeRepo($git->path); + $repo = $git->probeRepo(); $repo['readme'] = $repo['manifest'] = null; foreach ($git->git->tree('master') as $object) { if ($object['type'] == 'blob' && (strtolower($object['file']) === 'readme.md' || strtolower($object['file']) === 'readme')) { diff --git a/Zotlabs/Storage/GitRepo.php b/Zotlabs/Storage/GitRepo.php index de5dd3b19..81356950b 100644 --- a/Zotlabs/Storage/GitRepo.php +++ b/Zotlabs/Storage/GitRepo.php @@ -15,16 +15,39 @@ class GitRepo { public $url = null; public $name = null; - public $path = null; + private $path = null; private $repoID = null; private $channel = null; public $git = null; private $repoBasePath = null; - function __construct($channel = 'sys', $url = null, $clone = false, $name = null) { + function __construct($channel = 'sys', $url = null, $clone = false, $name = null, $path = null) { + + if($channel === 'sys' && ! is_site_admin()) { + logger('Only admin can use channel sys'); + return null; + } + $this->repoBasePath = __DIR__ . '/../../store/git'; $this->channel = $channel; $this->git = new PHPGit(); + + // Allow custom path for repo in the case of , for example + if($path) { + //if(mkdir($path, 0770, true)) { + $this->path = $path; + //} else { + // logger('Error creating GitRepo. Path not created.'); + // return null; + //} + } else { + $this->path = $this->repoBasePath . "/" . $this->channel . "/" . $this->name; + } + + if ($this->isValidGitRepoURL($url)) { + $this->url = $url; + } + if ($name) { $this->name = $name; } else { @@ -34,14 +57,17 @@ class GitRepo { logger('Error creating GitRepo. No repo name found.'); return null; } - $this->path = $this->repoBasePath . "/" . $this->channel . "/" . $this->name; - if (file_exists($this->path)) { + + if (is_dir($this->path)) { // ignore the $url input if it exists + // TODO: Check if the path is either empty or is a valid git repo and error if not $this->git->setRepository($this->path); // TODO: get repo metadata - } else if ($url && validate_url($url) && $this->isValidGitRepoURL($url)) { - $this->url = $url; - $this->repoID = random_string(); + return; + } + + if ($this->url) { + //$this->repoID = random_string(); // create the folder and clone the repo at url to that folder if $clone is true if ($clone) { if (mkdir($this->path, 0770, true)) { @@ -69,7 +95,7 @@ class GitRepo { } public function setRepoPath($directory) { - if (file_exists($directory)) { + if (is_dir($directory)) { $this->path->$directory; $this->git->setRepository($directory); return true; @@ -88,17 +114,14 @@ class GitRepo { } public function cloneRepo() { - if (validate_url($this->url) && $this->isValidGitRepoURL($this->url) && file_exists($this->path)) { + if (validate_url($this->url) && $this->isValidGitRepoURL($this->url) && is_dir($this->path)) { return $this->git->clone($this->url, $this->path); } } - public static function probeRepo($dir) { - if (!file_exists($dir)) { - return null; - } - $git = new PHPGit(); - $git->setRepository($dir); + public function probeRepo() { + $git = $this->git; + logger('probeRepo path: ' . $this->path); $repo = array(); $repo['remote'] = $git->remote(); $repo['branches'] = $git->branch(['all' => true]); @@ -107,7 +130,7 @@ class GitRepo { } public static function isValidGitRepoURL($url) { - if (strrpos(parse_url($url, PHP_URL_PATH), '.')) { + if (validate_url($url) && strrpos(parse_url($url, PHP_URL_PATH), '.')) { return true; } else { return false; diff --git a/view/tpl/admin_plugins.tpl b/view/tpl/admin_plugins.tpl index 9e1d05a44..87d380a01 100755 --- a/view/tpl/admin_plugins.tpl +++ b/view/tpl/admin_plugins.tpl @@ -54,9 +54,10 @@ function adminPluginsAddRepo() { var repoURL = $('#id_repoURL').val(); + var repoName = $('#id_repoName').val(); $('#chat-rotator').spin('tiny'); $.post( - "/admin/plugins/addrepo", {repoURL: repoURL}, + "/admin/plugins/addrepo", {repoURL: repoURL, repoName: repoName}, function(response) { $('#chat-rotator').spin(false); if (response.success) { diff --git a/view/tpl/admin_plugins_addrepo.tpl b/view/tpl/admin_plugins_addrepo.tpl index fe5fecc25..de7b465bb 100644 --- a/view/tpl/admin_plugins_addrepo.tpl +++ b/view/tpl/admin_plugins_addrepo.tpl @@ -2,6 +2,7 @@ <p class="descriptive-text">{{$desc}}</p> {{include file="field_input.tpl" field=$repoURL}} + {{include file="field_input.tpl" field=$repoName}} <div class="btn-group pull-right"> <button id="add-plugin-repo-submit" class="btn btn-primary" type="submit" name="submit" onclick="adminPluginsAddRepo(); return false;">{{$submit}}</button> </div> |