aboutsummaryrefslogtreecommitdiff
path: root/src/ZipStream.php
blob: b6953ef77df2501dce313c08ebaed7bc912a11e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php

namespace Pablotron\ZipStream;

final class ZipStream {
  const VERSION = "0.2.0";

  public $zip_name;
  private $args;
  private $entries = array();

  static $DEFAULT_ARCHIVE_OPTIONS = [
    'method'  => 'deflate'
    'comment' => '',
    'header'  => true,
  ];

  static $DEFAULT_FILE_OPTIONS = [
    'comment' => '',
  ];

  public function __construct(
    string $zip_name,
    array $args = array()
  ) {
    $this->zip_name = $zip_name;
    $this->args = array_merge(
      self::$DEFAULT_ARCHIVE_OPTIONS,
      $args
    );
  }

  public function add_text(
    string $dst_path, 
    array $args = array()
  ) {
    $this->check_path($dst_path);
    $this->entries[] = pack('VvvvvvVVVvv'
  }

  public function add_path(
    string $dst_path,
    string $src_path = null,
    array $args = array()
  )

  public function add_stream(
    string $dst_path,
    $src_stream,
    array $args = array()
  )

  public function finish() {
  }

  public static function send($name, array $args, function $cb) {
    $zip = new self($name, $args);
    $cb($zip);
    $zip->finish();
  }

  private function check_path(string $path) {
    # make sure path is non-null
    if (!$path) {
      throw new Exception("null path");
    }

    # check for empty path
    if (!strlen($path)) {
      throw new Exception("empty path");
    }

    # check for long path
    if (strlen($path) > 65535) {
      throw new Exception("path too long");
    }

    # check for leading slash
    if (!$path[0] == '/') {
      throw new Exception("path contains leading slash");
    }

    # check for trailing slash
    if (preg_match('/\\$/', $path)) {
      throw new Exception("path contains trailing slash");
    }

    # check for double slashes
    if (preg_match('/\/\//', $path))
      throw new Exception("path contains double slashes");
    }

    # check for double dots
    if (preg_match('/\.\./', $path))
      throw new Exception("relative path");
    }
  }

};