/** * Handle AJAX */ public function ajaxHandler() { $request = $_REQUEST; switch ($request['command']) { case "saveCustomLang": if (empty($_POST['id'])) { $this->addCustomLanguage($_POST['language'], $_POST['description']); } else { $this->updateCustomLanguage($_POST['id'], $_POST['language'], $_POST['description']); } return array("status" => true); break; case "convert": set_time_limit(0); $media = $this->FreePBX->Media; $temporary = $_POST['temporary']; $name = basename($_POST['name']); $codec = $_POST['codec']; $lang = $_POST['language']; $directory = $_POST['directory']; $path = $this->path . "/" . $lang; if (!empty($directory)) { $path = $path . "/" . $directory; } if (!file_exists($path)) { mkdir($path); } $name = preg_replace("/\\s+|'+|`+|\"+|<+|>+|\\?+|\\*|\\.+|&+/", "-", $name); if (!empty($codec)) { $media->load($temporary); try { $media->convert($path . "/" . $name . "." . $codec); unlink($temporary); } catch (\Exception $e) { return array("status" => false, "message" => $e->getMessage() . " [" . $path . "/" . $name . "." . $codec . "]"); } return array("status" => true, "name" => $name); } else { $ext = pathinfo($temporary, PATHINFO_EXTENSION); if ($temporary && file_exists($temporary)) { rename($temporary, $path . "/" . $name . "." . $ext); return array("status" => true, "name" => $name); } else { return array("status" => true, "name" => $name); } } break; case 'delete': switch ($request['type']) { case 'customlangs': $ret = array(); foreach ($request['customlangs'] as $language) { $ret[$language] = $this->delCustomLanguage($language); } return array('status' => true, 'message' => $ret); break; } break; case "upload": if (empty($_FILES["files"])) { return array("status" => false, "message" => _("No files were sent to the server")); } foreach ($_FILES["files"]["error"] as $key => $error) { switch ($error) { case UPLOAD_ERR_OK: $extension = pathinfo($_FILES["files"]["name"][$key], PATHINFO_EXTENSION); $extension = strtolower($extension); $supported = $this->FreePBX->Media->getSupportedFormats(); $archives = array("tgz", "gz", "tar", "zip"); if (in_array($extension, $supported['in']) || in_array($extension, $archives)) { $tmp_name = $_FILES["files"]["tmp_name"][$key]; $dname = \Media\Media::cleanFileName($_FILES["files"]["name"][$key]); $dname = pathinfo($dname, PATHINFO_FILENAME); $id = time() . rand(1, 1000); $name = $dname . '-' . $id . '.' . $extension; move_uploaded_file($tmp_name, $this->temp . "/" . $name); $gfiles = $bfiles = array(); if (in_array($extension, $archives)) { //this is an archive if ($extension == "zip") { $tar = new Zip(); } else { $tar = new Tar(); } $archive = $this->temp . "/" . $name; $tar->open($archive); $path = $this->temp . "/" . $id; if (!file_exists($path)) { mkdir($path); } $tar->extract($path); $objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path)); foreach ($objects as $name => $object) { if ($object->isDir()) { continue; } $file = (string) $object; $extension = pathinfo($file, PATHINFO_EXTENSION); $extension = strtolower($extension); $dir = dirname(str_replace($path . "/", "", $file)); $dir = $dir != "." ? $dir : ""; $dname = \Media\Media::cleanFileName(pathinfo($file, PATHINFO_FILENAME)); if (!in_array($extension, $supported['in'])) { $bfiles[] = array("directory" => $dir, "filename" => (!empty($dir) ? $dir . "/" : "") . $dname, "localfilename" => $file, "id" => ""); continue; } $gfiles[] = array("directory" => $dir, "filename" => (!empty($dir) ? $dir . "/" : "") . $dname, "localfilename" => $file, "id" => ""); } unlink($archive); } else { $gfiles[] = array("directory" => "", "filename" => pathinfo($dname, PATHINFO_FILENAME), "localfilename" => $this->temp . "/" . $name, "id" => $id); } return array("status" => true, "gfiles" => $gfiles, "bfiles" => $bfiles); } else { return array("status" => false, "message" => _("Unsupported file format")); break; } break; case UPLOAD_ERR_INI_SIZE: return array("status" => false, "message" => _("The uploaded file exceeds the upload_max_filesize directive in php.ini")); break; case UPLOAD_ERR_FORM_SIZE: return array("status" => false, "message" => _("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form")); break; case UPLOAD_ERR_PARTIAL: return array("status" => false, "message" => _("The uploaded file was only partially uploaded")); break; case UPLOAD_ERR_NO_FILE: return array("status" => false, "message" => _("No file was uploaded")); break; case UPLOAD_ERR_NO_TMP_DIR: return array("status" => false, "message" => _("Missing a temporary folder")); break; case UPLOAD_ERR_CANT_WRITE: return array("status" => false, "message" => _("Failed to write file to disk")); break; case UPLOAD_ERR_EXTENSION: return array("status" => false, "message" => _("A PHP extension stopped the file upload")); break; } } break; default: echo json_encode(_("Error: You should never see this")); break; } }
public function test_blockdata() { $tar = new Tar(); $tar->setCompression(0); $tar->create(); $tar->addData('block.txt', str_pad('', 512, 'x')); $file = $tar->getArchive(); $this->assertEquals(512 * 4, strlen($file)); // 1 header block + data block + 2 footer blocks }
/** * Unpack a downloaded archive file. * * @param string $file * The filename of the archive you wish to extract. * @param string $directory * The directory you wish to extract the archive into. * @return Archive * The Archiver object used to extract the archive. * @throws \Exception on failure. */ protected function extractArchive($file, $directory) { $archive = new Tar(); // Remove the directory if it exists, otherwise it might contain a mixture of // old files mixed with the new files (e.g. in cases where files were removed // from a later release). $archive->open($file); $files = $archive->contents(); // First entry contains the root folder. $project_path = $files[0]->getPath(); if (file_exists($directory)) { $this->removeDir($directory); } // Reopen archive to extract all files. $archive->open($file); // Strip first folder level. $archive->extract($directory, $project_path); return $archive; }
public function get_hashes() { $this->job->log(__('Getting checksum file from archive...', 'my-wp-backup'), 'debug'); if ('zip' === $this->format) { $archive = new Zip(); } else { $archive = new Tar(); if ($this->is_compressed()) { $archive->setCompression(9, $this->compression); } } $archive->open($this->archives[0]); $archive->extract(sys_get_temp_dir(), '', '', '#\\.my-wp-backup$#'); $this->job->move_hashfile(sys_get_temp_dir() . '/.my-wp-backup'); $this->job->log(__('Ok.', 'my-wp-backup'), 'debug'); return $this->backup->get_files(); }