private function install_from_market($item)
{
if (isset($item['url']) and !isset($item['download'])) {
$item['download'] = $item['url'];
} elseif (isset($item['download_url']) and !isset($item['download'])) {
$item['download'] = $item['download_url'];
}
$download_target = false;
if (isset($item['download']) and !isset($item['size'])) {
$url = $item['download'];
$download_target = $this->temp_dir . md5($url) . basename($url);
$download_target_extract_lock = $this->temp_dir . md5($url) . basename($url) . '.unzip_lock';
$this->_log_msg('Downloading from marketplace');
//if (!is_file($download_target)){
$dl = $this->http()->url($url)->download($download_target);
//}
} elseif (isset($item['download']) and isset($item['size'])) {
$expected = intval($item['size']);
$download_link = $item['download'];
$ext = get_file_extension($download_link);
if ($ext != 'zip') {
return;
}
if ($download_link != false and $expected > 0) {
$text = $download_link;
$regex = '/\\b((?:[\\w\\d]+\\:\\/\\/)?(?:[\\w\\-\\d]+\\.)+[\\w\\-\\d]+(?:\\/[\\w\\-\\d]+)*(?:\\/|\\.[\\w\\-\\d]+)?(?:\\?[\\w\\-\\d]+\\=[\\w\\-\\d]+\\&?)?(?:\\#[\\w\\-\\d]*)?)\\b/';
preg_match_all($regex, $text, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
if (isset($match[0])) {
$url = $download_link;
$download_target = $this->temp_dir . basename($download_link);
$download_target_extract_lock = $this->temp_dir . basename($download_link) . '.unzip_lock';
$expectd_item_size = $item['size'];
if (!is_file($download_target) or filesize($download_target) != $item['size']) {
$dl = $this->http()->url($url)->download($download_target);
if ($dl == false) {
if (is_file($download_target) and filesize($download_target) != $item['size']) {
$fs = filesize($download_target);
return array('size' => $fs, 'expected_size' => $expected, 'try_again' => 'true', 'warning' => 'Only ' . $fs . ' bytes downloaded of total ' . $expected);
}
}
}
}
}
}
}
if ($download_target != false and is_file($download_target)) {
$where_to_unzip = MW_ROOTPATH;
if (isset($item['item_type'])) {
if ($item['item_type'] == 'module') {
$where_to_unzip = modules_path();
} elseif ($item['item_type'] == 'module_template') {
$where_to_unzip = modules_path();
} elseif ($item['item_type'] == 'template') {
$where_to_unzip = templates_path();
} elseif ($item['item_type'] == 'element') {
$where_to_unzip = elements_path();
}
if (isset($item['install_path']) and $item['install_path'] != false) {
if ($item['item_type'] == 'module_template') {
$where_to_unzip = $where_to_unzip . DS . $item['install_path'] . DS . 'templates' . DS;
} else {
$where_to_unzip = $where_to_unzip . DS . $item['install_path'];
}
}
$where_to_unzip = str_replace('..', '', $where_to_unzip);
$where_to_unzip = normalize_path($where_to_unzip, true);
$this->_log_msg('Unzipping in ' . $where_to_unzip);
$unzip = new \Microweber\Utils\Unzip();
$target_dir = $where_to_unzip;
$result = $unzip->extract($download_target, $target_dir, $preserve_filepath = true);
$new_composer = $target_dir . 'composer.json';
if (is_file($new_composer)) {
// $this->composer_merge($new_composer);
}
$num_files = count($result);
return array('files' => $result, 'location' => $where_to_unzip, 'success' => "Item is installed. {$num_files} files extracted in {$where_to_unzip}");
}
}
}