1: <?php
2: /**
3: * LICENSE (BSD)
4: *
5: * Copyright (c) 2013, Gerd Christian Kunze
6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions are
10: * met:
11: *
12: * * Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: *
15: * * Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * * Neither the name of Gerd Christian Kunze nor the names of the
20: * contributors may be used to endorse or promote products derived from
21: * this software without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34: *
35: * Download
36: * 31.05.2013 09:05
37: */
38: namespace MOC\Generic\Device\Extension;
39: use MOC\Api;
40: use MOC\Module\Drive\File;
41:
42: /**
43: *
44: */
45: abstract class Download {
46:
47: /** @var string $ProjectPage */
48: private $ProjectPage = '';
49: /** @var string $ProjectLocation */
50: private $ProjectLocation = '';
51: /** @var array $ProjectFilter */
52: private $ProjectFilter = array();
53:
54: /**
55: * Remote Project-Page
56: *
57: * @param $Url
58: */
59: final protected function DownloadProjectPage( $Url ) {
60: $this->ProjectPage = Api::Module()->Network()->Http()->Get()->Request( $Url );
61: }
62:
63: /**
64: * Local Project-Path
65: *
66: * @param $Path
67: */
68: final protected function DownloadProjectLocation( $Path ) {
69: $this->ProjectLocation = $Path;
70: }
71:
72: /**
73: * RegExp
74: *
75: * @param string $Filter
76: */
77: final protected function DownloadProjectFilter( $Filter ) {
78: $this->ProjectFilter[] = $Filter;
79: }
80:
81: /**
82: * @throws \Exception
83: */
84: final protected function DownloadProject() {
85:
86: foreach( (array)$this->ProjectFilter as $Filter ) {
87: if( preg_match( $Filter, $this->ProjectPage, $Match ) ) {
88: $this->DownloadProjectPage( $Match[0] );
89: } else {
90: throw new \Exception( 'Unable to determine download location' );
91: }
92: }
93:
94: $this->ProjectPage = Api::Module()->Drive()->File()->Open(
95: Api::Core()->Cache()->Identifier( $this->ProjectPage )->Timeout(3600)->Set('')->Location()
96: )->Write( $this->ProjectPage );
97:
98: $this->ProjectPage = Api::Module()->Packer()->Zip()->Decoder()->Open( $this->ProjectPage );
99:
100: $RootIndex = 0;
101: for( $Run = ( count( $this->ProjectPage ) -1 ); $Run > 1; $Run-- ) {
102: $DifferenceAt = strspn(
103: $this->ProjectPage[$Run]->GetLocation() ^ $this->ProjectPage[$Run -1]->GetLocation(), "\0"
104: );
105: if( $DifferenceAt < $RootIndex || $RootIndex == 0 ) {
106: $RootIndex = $DifferenceAt;
107: }
108: }
109:
110: foreach( $this->ProjectPage as $Object ) {
111: if( $Object instanceof File ) {
112: $Path = $this->ProjectLocation.DIRECTORY_SEPARATOR.substr( $Object->GetLocation(), $RootIndex );
113: Api::Module()->Drive()->Directory()->Open( dirname( $Path ) );
114: $Object->CopyTo( $Path );
115: }
116: }
117: }
118: }
119: