HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
ResourceDownload.php
Go to the documentation of this file.
1 <?php
2 
4 {
5  protected $response;
6 
7  public function download($options)
8  {
9  $userId = Yii::app()->user->id;
10  $request = Yii::app()->request;
11  $json[] = array(
12  'contentTypeMask' => $this->getMask($options),
13  'siteId' => $options['siteId'],
14  'url' => $options['url'],
15  'urlFetch' => null,
16  'urlMd5' => $options['urlMd5'],
17  );
18  $json = CJSON::encode($json);
19  $path = Yii::app()->getBasePath() . '/json_temp/';
20  $file = fopen($path . $userId . '_request_content.json', 'w');
21  fwrite($file, $json);
22  $api = Yii::app()->params['api'];
23  $path = Yii::app()->getBasePath() . '/shell/';
24  $pathFile = Yii::app()->getBasePath() . '/json_temp/' . $userId . '_request_content.json';
25  $cmd = "sh " . $path . "url_content.sh $api $pathFile";
26  $json = shell_exec($cmd);
27  $path = Yii::app()->getBasePath() . '/json_temp/';
28  $file = fopen($path . $userId . '_response_content.json', 'w+');
29  fwrite($file, $json);
30  fclose($file);
31  $response = file_get_contents($path . $userId . '_response_content.json');
32  $this->response = CJSON::decode($response);
33  $contentsArray = $this->createContents();
34  $this->createFiles($options['urlMd5'], $contentsArray['contents'], $contentsArray['prefix']);
35  }
36 
37  public function getMask($options)
38  {
39  $mask = '000000000000000';
40  for ($i = 0; $i <= 10; $i++) {
41  if (isset($options[$i])) {
42  $mask[$i] = '1';
43  }
44  }
45  $crawled = $options['crawled'];
46  $mask[$crawled] = '1';
47  $mask = strrev($mask);
48  return bindec($mask);
49  }
50 
51  public function createContents() {
52  $contents = array();
53  $prefix = array();
54  $itemObject = $this->response['itemsList'][0]['itemObject'][0];
55  if ($this->isSetContent('rawContents')) {
56  foreach ($itemObject['rawContents'] as $k => $v) {
57  if ($v['fieldId'] == 0) {
58  $contents['raw'][$k] = base64_decode($v['buffer']);
59  $prefix['raw'][$k] = date('YmdHis.', strtotime($v['cDate']));
60  } elseif ($v['fieldId'] == 1) {
61  $contents['tidy'][$k] = base64_decode($v['buffer']);
62  $prefix['tidy'][$k] = date('YmdHis.', strtotime($v['cDate']));
63  } elseif ($v['fieldId'] == 9) {
64  $contents['dynamic'][$k] = base64_decode($v['buffer']);
65  $prefix['dynamic'][$k] = date('YmdHis.', strtotime($v['cDate']));
66  }
67  }
68  }
69  if ($this->isSetContent('processedContents')) {
70  foreach ($itemObject['processedContents'] as $k => $v) {
71  $contents['pro'][$k] = base64_decode($v['buffer']);
72  $prefix['pro'][$k] = date('YmdHis.', strtotime($v['cDate']));
73  }
74  }
75  if ($this->isSetContent('requests')) {
76  foreach ($itemObject['requests'] as $k => $v) {
77  $contents['requests'][$k] = base64_decode($v['buffer']);
78  $prefix['requests'][$k] = date('YmdHis.', strtotime($v['cDate']));
79  }
80  }
81  if ($this->isSetContent('headers')) {
82  foreach ($itemObject['headers'] as $k => $v) {
83  $contents['headers'][$k] = base64_decode($v['buffer']);
84  $prefix['headers'][$k] = date('YmdHis.', strtotime($v['cDate']));
85  }
86  }
87  if ($this->isSetContent('cookies')) {
88  foreach ($itemObject['cookies'] as $k => $v) {
89  $contents['cookies'][$k] = base64_decode($v['buffer']);
90  $prefix['cookies'][$k] = date('YmdHis.', strtotime($v['cDate']));
91  }
92  }
93  if ($this->isSetContent('meta')) {
94  foreach ($itemObject['meta'] as $k => $v) {
95  $contents['meta'][$k] = base64_decode($v['buffer']);
96  $prefix['meta'][$k] = date('YmdHis.', strtotime($v['cDate']));
97  }
98  }
99  return array(
100  'contents' => $contents,
101  'prefix' => $prefix
102  );
103  }
104 
105  public function createFiles($dirname, $contents = array(), $prefix = array())
106  {
107  $dir = Yii::app()->runtimePath.DIRECTORY_SEPARATOR.$dirname;
108  if (is_dir($dir)) {
109  $this->delTree($dir);
110  }
111  if(mkdir($dir)) {
112  if (isset($contents['raw'])) {
113  foreach ($contents['raw'] as $k => $v) {
114  $raw = fopen($dir . DIRECTORY_SEPARATOR . $prefix['raw'][$k] . 'raw.bin', 'w');
115  fwrite($raw, $v);
116  fclose($raw);
117  }
118  }
119  if (isset($contents['tidy'])) {
120  foreach ($contents['tidy'] as $k => $v) {
121  $rawTidy = fopen($dir . DIRECTORY_SEPARATOR . $prefix['tidy'][$k] . 'raw.bin.tidy', 'w');
122  fwrite($rawTidy, $v);
123  fclose($rawTidy);
124  }
125  }
126 
127  if (isset($contents['pro'])) {
128  foreach ($contents['pro'] as $k => $v) {
129  $pro = fopen($dir . DIRECTORY_SEPARATOR . $prefix['pro'][$k] . 'processed', 'w');
130  fwrite($pro, $v);
131  fclose($pro);
132  }
133  }
134 
135  if (isset($contents['requests'])) {
136  foreach ($contents['requests'] as $k => $v) {
137  $requests = fopen($dir . DIRECTORY_SEPARATOR . $prefix['requests'][$k] . 'requests', 'w');
138  fwrite($requests, $v);
139  fclose($requests);
140  }
141  }
142 
143  if (isset($contents['headers'])) {
144  foreach ($contents['headers'] as $k => $v) {
145  $headers = fopen($dir . DIRECTORY_SEPARATOR . $prefix['headers'][$k] . 'headers', 'w');
146  fwrite($headers, $v);
147  fclose($headers);
148  }
149  }
150 
151  if (isset($contents['cookies'])) {
152  foreach ($contents['cookies'] as $k => $v) {
153  $cookies = fopen($dir . DIRECTORY_SEPARATOR . $prefix['cookies'][$k] . 'cookies', 'w');
154  fwrite($cookies, $v);
155  fclose($cookies);
156  }
157  }
158 
159  if (isset($contents['meta'])) {
160  foreach ($contents['meta'] as $k => $v) {
161  $meta = fopen($dir . DIRECTORY_SEPARATOR . $prefix['meta'][$k] . 'meta', 'w');
162  fwrite($meta, $v);
163  fclose($meta);
164  }
165  }
166 
167  if (isset($contents['dynamic'])) {
168  foreach ($contents['dynamic'] as $k => $v) {
169  $dynamic = fopen($dir . DIRECTORY_SEPARATOR . $prefix['dynamic'][$k] . 'dynamic', 'w');
170  fwrite($dynamic, $v);
171  fclose($dynamic);
172  }
173  }
174  chdir($dir);
175  $zip = new ZipArchive();
176  $filename = $dir . DIRECTORY_SEPARATOR . $dirname . '.zip';
177  $zip->open($filename, ZipArchive::OVERWRITE);
178  $zip->addGlob('*.*');
179  $zip->close();
180  Yii::app()->request->sendFile(basename($filename), file_get_contents($filename));
181  unset($zip);
182  }
183  chdir(Yii::app()->runtimePath);
184  if (is_dir($dir)) {
185  $this->delTree($dir);
186  }
187  }
188 
189  public function isSetContent($type)
190  {
191  if (empty($this->response['itemsList'][0]['itemObject'])) {
192  return false;
193  } elseif (empty($this->response['itemsList'][0]['itemObject'][0][$type][0])) {
194  return false;
195  }
196  return true;
197  }
198 
199  public function delTree($dir) {
200  $files = array_diff(scandir($dir), array('.','..'));
201  foreach ($files as $file) {
202  (is_dir("$dir/$file")) ? $this->delTree("$dir/$file") : unlink("$dir/$file");
203  }
204  return rmdir($dir);
205  }
206 }