public static function removeThumbs($filename) { if (!file_exists($filename)) { return false; } $za = new \ZipArchive(); $za->open($filename); $count = 0; for ($i = 0; $i < $za->numFiles; $i++) { $stat = $za->statIndex($i); if (basename($stat['name']) !== 'Thumbs.db') { continue; } $za->deleteIndex($i); $count++; } $za->close(); return $count; }
private function isCorrectFormat($file) { if (class_exists('ZipArchive')) { $success = false; $zip = new ZipArchive(); if ($zip->open($file) === true) { for ($i = 0; $i < $zip->numFiles; ++$i) { $filename = $zip->getNameIndex($i); if (substr($filename, 0, strlen($this->addonkey . '/')) != $this->addonkey . '/') { $zip->deleteIndex($i); } else { $success = true; } } $zip->close(); } return $success; } return file_exists("phar://{$file}/" . $this->addonkey); }
<?php /** * Created by PhpStorm. * User: sergey * Date: 15.11.15 * Time: 14:02 */ $sourcefile = 'flowers_odtTemplate.odt'; $xslt = 'flowers_odt.xslt'; $zip = new ZipArchive(); $zip->open($sourcefile); $content = $zip->getFromName('content.xml'); if (file_put_contents($xslt, $content)) { echo 'Main content extracted<br>'; } else { echo 'Problem extracting main content'; } for ($i = 0; $i < $zip->numFiles; $i++) { $filename = $zip->getNameIndex($i); if (pathinfo($filename, PATHINFO_DIRNAME) == 'Pictures') { if ($zip->deleteIndex($i)) { echo 'Image deleted<br>'; } else { echo 'Problem deleting image<br>'; } } } $zip->close();
public function execute() { global $I18N; $this->addonkey = rex_request('addonkey', 'string'); $function = static::GET_PACKAGES_FUNCTION; $packages = rex_install_packages::$function(); $this->fileId = rex_request('file', 'int'); if (!isset($packages[$this->addonkey]['files'][$this->fileId])) { return null; } $this->file = $packages[$this->addonkey]['files'][$this->fileId]; $this->checkPreConditions(); $archivefile = rex_install_webservice::getArchive($this->file['path']); $message = ''; $this->archive = $archivefile; if (class_exists("ZipArchive")) { $success = false; $zip = new ZipArchive(); if ($zip->open($archivefile) === TRUE) { for ($i = 0; $i < $zip->numFiles; $i++) { $filename = $zip->getNameIndex($i); if (substr($filename, 0, strlen($this->addonkey . '/')) != $this->addonkey . '/') { $zip->deleteIndex($i); } else { $success = true; } } $zip->close(); } if (!$success) { $message = $I18N->msg('install_warning_zip_wrong_format'); } } else { if (!file_exists("phar://{$archivefile}/" . $this->addonkey)) { $message = $I18N->msg('install_warning_zip_wrong_format'); } } if ($message != "") { } else { if ($this->file['checksum'] != md5_file($archivefile)) { $message = $I18N->msg('install_warning_zip_wrong_checksum'); } else { if (is_string($msg = $this->doAction())) { $message = $msg; } } } rex_file::delete($archivefile); if ($message) { $message = $I18N->msg('install_warning_addon_not_' . static::VERB, $this->addonkey) . '<br />' . $message; $success = false; } else { $message = $I18N->msg('install_info_addon_' . static::VERB, $this->addonkey) . (static::SHOW_LINK ? ' <a href="index.php?page=addon">' . $I18N->msg('install_to_addon_page') . '</a>' : ''); $success = true; unset($_REQUEST['addonkey']); } if ($success) { return $message; } else { throw new rex_install_functional_exception($message); } }
if ($uploadfilename) { // Check ZIP file contents for extra directory at the top $za = new ZipArchive(); $za->open($uploadfilename); for ($i = 0; $i < $za->numFiles; $i++) { $entryName = $za->getNameIndex($i); $firstSlash = strpos($entryName, '/'); if ($entryName === 'manifest.xml' || $entryName === './manifest.xml' || $firstSlash === false) { $za->unchangeAll(); break; } $newEntryName = substr($entryName, $firstSlash + 1); if ($newEntryName !== false) { $za->renameIndex($i, $newEntryName); } else { $za->deleteIndex($i); } } $za->close(); $package = new Vtiger_Package(); $moduleimport_name = $package->getModuleNameFromZip($uploadfilename); if ($moduleimport_name == null) { $smarty->assign("MODULEIMPORT_FAILED", "true"); $smarty->assign("MODULEIMPORT_FILE_INVALID", "true"); } else { if (!$package->isLanguageType() && !$package->isModuleBundle()) { $moduleInstance = Vtiger_Module::getInstance($moduleimport_name); $moduleimport_exists = $moduleInstance ? "true" : "false"; $moduleimport_dir_name = "modules/{$moduleimport_name}"; $moduleimport_dir_exists = is_dir($moduleimport_dir_name) ? "true" : "false"; $smarty->assign("MODULEIMPORT_EXISTS", $moduleimport_exists);
<?php $dirname = dirname(__FILE__) . '/'; $arch = new ZipArchive(); $arch->open($dirname . 'foo.zip', ZIPARCHIVE::CREATE); var_dump($arch->getStatusString()); //delete an index that does not exist - trigger error $arch->deleteIndex(2); var_dump($arch->getStatusString()); $arch->close(); unlink($dirname . 'foo.zip');
function _rename_zip_path($file, $original_path, $replacement_path) { $original_path = preg_replace('|^[/\\\\]+|', '', $original_path); $original_path = preg_replace('|([/\\\\])+$|', '', $original_path); $original_path .= '/'; $replacement_path = preg_replace('|^[/\\\\]+|', '', $replacement_path); $replacement_path = preg_replace('|[/\\\\]+$|', '', $replacement_path); if (!empty($replacement_path)) { $replacement_path .= '/'; } $zip = new ZipArchive(); echo $file; $open_status = $zip->open($file); if (true !== $open_status) { // return array( 'error' => $this->get_zip_archive_error_message( $open_status ) ); return array('error' => 'Unable to rename path'); } $status = array('removed' => 0, 'renamed' => 0); for ($index = 0; $index < $zip->numFiles; $index++) { $name = $zip->getNameIndex($index); if ($name === $original_path) { // Remove original container directory $zip->deleteIndex($index); $status['removed']++; } else { if (preg_match('/^' . preg_quote($original_path, '/') . '(.+)/', $name, $matches)) { // Rename paths $zip->renameIndex($index, "{$replacement_path}{$matches[1]}"); $status['renamed']++; } } } $zip->close(); return $status; }
public function getFirmwareAction($params) { $q = mysqli_query($this->dbConnect, "select * from settings"); while ($r = mysqli_fetch_assoc($q)) { $settings[$r['id']]['username'] = $r['username']; $settings[$r['id']]['password'] = $r['password']; } $types = array(1 => 'upd', 2 => 'noupd'); $typesFile = array(1 => 'U', 2 => 'NU'); if (empty($params['serial'])) { $serialName = $params['crum']; } else { $serialName = $params['serial']; } $firmawareName = str_replace(" ", "_", $params['firmware']); $firmawareName = str_replace(".", "", $firmawareName); $firmawareModel = str_replace(" ", "_", $params['model']); $filename = "FIX_" . $serialName . "_" . $firmawareModel . "_" . $firmawareName . "_" . $typesFile[$params['type']]; if ($params['modelType'] == 1) { $postdata = "username={$settings[1]['username']}&password={$settings[1]['password']}"; } else { $postdata = "username={$settings[2]['username']}&password={$settings[2]['password']}"; } echo $postdata; $url1 = "http://www.korotron-online.net/Login"; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); $data = curl_exec($curl); preg_match_all('|Set-Cookie: (.*);|U', $data, $matches); $cookies = implode(';', $matches[1]); #Запрос на получение версии $getModel = str_replace(" ", "%20", $params['model']); $reffer = "http://www.korotron-online.net/Firmware"; $url = "http://www.korotron-online.net/Firmware/GetVersions/{$getModel}"; // echo $url; $curl = curl_init(); curl_setopt($curl, CURLOPT_REFERER, $reffer); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_COOKIE, $cookies); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); $data = curl_exec($curl); curl_close($curl); //echo $data; $data = json_decode($data, true); //echo $params['firmware']; $result['fname'] = explode(" ", $params['firmware']); foreach ($data as $val) { $val['optionDisplay'] = explode(" ", $val['optionDisplay']); //echo $val['optionDisplay'][0]; if ($val['optionDisplay'][2] == 9) { } // echo $result['fname'][0]; if ($result['fname'][1] == 9) { if (strtoupper($val['optionDisplay'][0]) == strtoupper($result['fname'][0]) and $val['optionDisplay'][2] == 9) { // echo 1; $params['version'] = $val['optionValue']; } } else { if (strtoupper($val['optionDisplay'][0]) == strtoupper($result['fname'][0]) and $val['optionDisplay'][2] != 9) { // echo 1; $params['version'] = $val['optionValue']; } } } if (!isset($params['serial']) or empty($params['serial'])) { $params['serial'] = $params['crum']; } $postdata = "generate=1&manufacturer={$params['vendor']}&model={$params['model']}&version={$params['version']}&serial={$params['serial']}&crum={$params['crum']}&type={$types[$params['type']]}"; /// echo $postdata; // exit(); $url1 = "http://www.korotron-online.net/Firmware"; $reffer = "http://www.korotron-online.net/Firmware"; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_REFERER, $reffer); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_COOKIE, $cookies); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); $data = curl_exec($curl); curl_close($curl); // echo $data; preg_match_all('|Location: (.*)|', $data, $matches); list($header, $body) = explode("\r\n\r\n", $data, 2); // print_r($matches); //echo $matches[1][0]; $m = htmlspecialchars(trim($matches[1][0])); $m = explode("\r", $matches[1][0]); $postdata = ''; //exit(); //открываем файловый дескриптор (куда сохранять файл) $fp = fopen($filename . '.zip', 'w+b'); $idDown = explode("/", $matches[1][0]); $idDown[3] = trim($idDown[3]); $id = $idDown[3]; $url = "http://www.korotron-online.net/Firmware/Download/{$id}/firmware.zip"; //echo $url; //$reffer = "http://firmware-online.com/Firmware/Redirect/$id"; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); curl_setopt($curl, CURLOPT_REFERER, $reffer); curl_setopt($curl, CURLOPT_AUTOREFERER, false); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_NOBODY, false); curl_setopt($curl, CURLOPT_COOKIE, $cookies); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 600); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); curl_setopt($curl, CURLOPT_FILE, $fp); $data = curl_exec($curl); $response = curl_getinfo($curl); curl_close($curl); fclose($fp); //preg_match_all('|Content-Length: (.*)|', $data, $matches); if (filesize($filename . '.zip') < 100000) { mysqli_query($this->dbConnect, "update users set summa = summa + {$params['price']} where id = '{$_SESSION['user_id']}'"); mysqli_query($this->dbConnect, "update users set summa = summa + {$params['parrent_price']} where id = '{$params['parrent_id']}'"); exit; } //file_put_contents($filename . '.zip', $data, LOCK_EX); if ($params['modelType'] == 3) { $zip = new ZipArchive(); if ($zip->open($filename . '.zip') === true) { $count = $zip->numFiles; for ($i = 0; $i < $count; $i++) { $stat = $zip->statIndex($i); if (strstr($stat['name'], ".txt")) { $zip->deleteIndex($i); break; } } $zip->close(); echo 'ok!'; } else { echo 'ошибка'; } } $this->saveResults($params, $filename); }
function wpcomplete_zipextract($file, $extractPath) { $zip = new ZipArchive(); $res = @$zip->open($file); if (!@($index = $zip->locateName('.wpcmanifest'))) { return false; } else { if ($res === TRUE) { //REMOVE THE MANIFEST @$zip->deleteIndex($index); //EXTRACT THE ARCHIVE @$zip->extractTo($extractPath); //RE-ADD THE MANIFEST $zip->addFromString('.wpcmanifest', time()); //CLOSE THE ZIP @$zip->close(); return TRUE; } else { return FALSE; } } }
public function publish_mixtape_upload() { if (!$this->ion_auth->is_admin()) { redirect('mixtapes', 'refresh'); } $this->load->library('tags'); $this->load->library('images'); $this->load->model('Mixtape_model'); //$this->output->set_header('Content-Type: application/json; charset=utf-8'); $jsonResponse = json_decode($this->input->post('response'), true); if (!isset($jsonResponse['error'])) { $ti = $jsonResponse['mixtape']; if ($ti) { foreach ($ti as $key => $val) { $tapeInfo = $val; } } } $user = $this->ion_auth->user()->row()->username; $mixtape = $this->Mixtape_model->get_mixtape($this->input->post('mid')); // delete existing tracks from DB and file system $tracks = $this->Mixtape_model->get_tracks(array('tape_id' => $mixtape->id)); if ($tracks) { foreach ($tracks as $key => $track) { if ($this->Mixtape_model->delete_track(array('tape_id' => $mixtape->id, 'id' => $track->id))) { if (file_exists('audio_uploads/' . $user . '/mixtapes/' . $track->file_name)) { if (unlink('audio_uploads/' . $user . '/mixtapes/' . $track->file_name)) { } } } } } if (!isset($tapeInfo)) { $output_array = array('validation' => 'error', 'response' => 'error', 'message' => 'No songs were found in the zip file'); die(json_encode($output_array)); } $file = FCPATH . 'audio_uploads/' . $user . '/mixtapes/' . $tapeInfo['file']; if (!$mixtape) { $output_array = array('validation' => 'error', 'response' => 'error', 'message' => 'Server Error. Mixtape not found.'); die(json_encode($output_array)); } $slug = $this->input->post('slug'); $userPhotoDir = FCPATH . 'asset_uploads/' . $user; $tapePhotoDir = FCPATH . 'asset_uploads/' . $user . '/mixtapes/'; $tapePhotoSlugDir = $tapePhotoDir . '/' . $slug; $userAudioDir = FCPATH . 'audio_uploads/' . $user; $userTapeDir = FCPATH . 'audio_uploads/' . $user . '/mixtapes'; $tapePath = $userTapeDir . '/' . $slug; $tape_id = $mixtape->id; $can_download = $mixtape->can_download; $mixtape_title = $mixtape->tape_title; $allowExtensions = array('txt', 'doc', 'pdf', 'mp3', 'jpg', 'png', 'gif'); $images = array('jpg', 'png', 'gif'); $songs = array('mp3'); $extra_image = NULL; $extra_image2 = NULL; $zip = new ZipArchive(); $res = $zip->open($file); if ($res === TRUE) { //create asset_uploads/user dir if (!file_exists($userPhotoDir)) { mkdir($userPhotoDir); file_put_contents($userPhotoDir . '/index.html', ''); } //create asset_uploads/user/mixtape dir if (!file_exists($tapePhotoDir)) { mkdir($tapePhotoDir); file_put_contents($tapePhotoDir . '/index.html', ''); } //create asset_uploads/user/mixtape/tape-slug dir if (!file_exists($tapePhotoSlugDir)) { mkdir($tapePhotoSlugDir); file_put_contents($tapePhotoSlugDir . '/index.html', ''); } //create audio_uploads/user dir if (!file_exists($userAudioDir)) { mkdir($userAudioDir); file_put_contents($userAudioDir . '/index.html', ''); } //create audio_uploads/user/mixtape dir if (!file_exists($userTapeDir)) { mkdir($userTapeDir); file_put_contents($userTapeDir . '/index.html', ''); } //create audio_uploads/user/mixtape/tape-slug dir if (!file_exists($tapePath)) { mkdir($tapePath); file_put_contents($tapePath . '/index.html', ''); } $tape_order = 1; $p = 1; //for extra photos $song_count = 0; // how many songs are in the zip for ($i = 0; $i < $zip->numFiles; $i++) { $filename = $zip->getNameIndex($i); $fullFileName = $zip->statIndex($i); $ext = substr(strrchr($filename, '.'), 1); $info = pathinfo($fullFileName['name']); if ($filename[strlen($filename) - 1] == "/") { //this is a folder, so do nothing } else { if (!in_array($ext, $allowExtensions)) { //rogue files, delete them from the zip file $zip->deleteIndex($i); } //end rogue if (in_array($ext, $images)) { $photoFileName = url_slug($mixtape_title) . '-' . $i; file_put_contents($tapePhotoSlugDir . '/' . $photoFileName . '.jpg', $zip->getFromIndex($i)); $sizes = array('64', '150', '300', '500'); foreach ($sizes as $size) { $this->images->resizeImage($tapePhotoSlugDir . '/', $photoFileName, 'jpg', $size); } //end foreach if ($p === 1) { $extra_image = $photoFileName . '.jpg'; } elseif ($p === 2) { $extra_image2 = $photoFileName . '.jpg'; } $p++; unlink($tapePhotoSlugDir . '/' . $photoFileName . '.jpg'); } //end images if (in_array($ext, $songs)) { $song_file_name = str_replace($this->removePiff, '', $info['filename']); if (file_put_contents($tapePath . '/' . file_slug($song_file_name) . '.' . $info['extension'], $zip->getFromIndex($i))) { //write track info to DB // Get ID3 Infoc $dir_file = $tapePath . '/' . file_slug($song_file_name) . '.' . $info['extension']; $tags = $this->tags->getTagsInfo($dir_file); $temp_title = isset($tags['Title']) ? $tags['Title'] : ''; $temp_artist = isset($tags['Author']) ? $tags['Author'] : ''; $title = str_replace($this->removePiff, '', $temp_title); $artist = str_replace($this->removePiff, '', $temp_artist); $data = array('tape_id' => $tape_id, 'user_id' => $this->ion_auth->user()->row()->id, 'song_artist' => $artist, 'song_title' => $title, 'song_url' => url_slug($song_file_name), 'file_name' => $slug . '/' . file_slug($song_file_name) . '.' . $info['extension'], 'can_download' => $can_download, 'tape_order' => $tape_order); $addTrack = $this->Mixtape_model->add_track($data); $tape_order++; if (!$addTrack) { // output something like "THE FOLLOWING FILES COULD NOT BE PROCESSED" //tape not added, error out or something } else { $song_count++; } //add track to DB } //end song move } // end songs } // end if/else folder } // end for loop $zip->close(); } else { echo 'failed, code:' . $res; } //end zip open if ($song_count > 0) { $fileName = str_replace($this->removePiff, '', $this->input->post('name')); $fileName = preg_replace('/[^\\w\\-._]+/', '_', $fileName); $where = array('id' => $this->input->post('mid'), 'tape_url' => $this->input->post('slug'), 'user_id' => $this->ion_auth->user()->row()->id); $data = array('file_name' => $fileName, 'status' => 'published', 'extra_image' => $extra_image, 'extra_image2' => $extra_image2, 'published_date' => time()); $this->Mixtape_model->update_mixtape($where, $data); $output_array = array('validation' => 'valid', 'response' => 'success', 'count' => $song_count, 'name' => $fileName); } else { //no songs in the array $output_array = array('validation' => 'error', 'response' => 'error', 'message' => 'No MP3s were found in the zip file. Please try again.'); } $this->output->set_output(json_encode($output_array)); //JSON RESPONSE SHOULD INCLUDE LIST OF SONGS }