9 $userId = Yii::app()->user->id;
10 $request = Yii::app()->request;
12 'contentTypeMask' => $this->
getMask($options),
13 'siteId' => $options[
'siteId'],
14 'url' => $options[
'url'],
16 'urlMd5' => $options[
'urlMd5'],
19 $path = Yii::app()->getBasePath() .
'/json_temp/';
20 $file = fopen($path . $userId .
'_request_content.json',
'w');
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+');
31 $response = file_get_contents($path . $userId .
'_response_content.json');
32 $this->response = CJSON::decode(
$response);
34 $this->
createFiles($options[
'urlMd5'], $contentsArray[
'contents'], $contentsArray[
'prefix']);
39 $mask =
'000000000000000';
40 for (
$i = 0;
$i <= 10;
$i++) {
41 if (isset($options[
$i])) {
45 $crawled = $options[
'crawled'];
46 $mask[$crawled] =
'1';
47 $mask = strrev($mask);
54 $itemObject = $this->response[
'itemsList'][0][
'itemObject'][0];
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']));
70 foreach ($itemObject[
'processedContents'] as $k => $v) {
71 $contents[
'pro'][$k] = base64_decode($v[
'buffer']);
72 $prefix[
'pro'][$k] = date(
'YmdHis.', strtotime($v[
'cDate']));
76 foreach ($itemObject[
'requests'] as $k => $v) {
77 $contents[
'requests'][$k] = base64_decode($v[
'buffer']);
78 $prefix[
'requests'][$k] = date(
'YmdHis.', strtotime($v[
'cDate']));
82 foreach ($itemObject[
'headers'] as $k => $v) {
83 $contents[
'headers'][$k] = base64_decode($v[
'buffer']);
84 $prefix[
'headers'][$k] = date(
'YmdHis.', strtotime($v[
'cDate']));
88 foreach ($itemObject[
'cookies'] as $k => $v) {
89 $contents[
'cookies'][$k] = base64_decode($v[
'buffer']);
90 $prefix[
'cookies'][$k] = date(
'YmdHis.', strtotime($v[
'cDate']));
94 foreach ($itemObject[
'meta'] as $k => $v) {
95 $contents[
'meta'][$k] = base64_decode($v[
'buffer']);
96 $prefix[
'meta'][$k] = date(
'YmdHis.', strtotime($v[
'cDate']));
100 'contents' => $contents,
105 public function createFiles($dirname, $contents = array(), $prefix = array())
107 $dir = Yii::app()->runtimePath.DIRECTORY_SEPARATOR.$dirname;
112 if (isset($contents[
'raw'])) {
113 foreach ($contents[
'raw'] as $k => $v) {
114 $raw = fopen($dir . DIRECTORY_SEPARATOR . $prefix[
'raw'][$k] .
'raw.bin',
'w');
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);
127 if (isset($contents[
'pro'])) {
128 foreach ($contents[
'pro'] as $k => $v) {
129 $pro = fopen($dir . DIRECTORY_SEPARATOR . $prefix[
'pro'][$k] .
'processed',
'w');
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);
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);
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);
159 if (isset($contents[
'meta'])) {
160 foreach ($contents[
'meta'] as $k => $v) {
161 $meta = fopen($dir . DIRECTORY_SEPARATOR . $prefix[
'meta'][$k] .
'meta',
'w');
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);
175 $zip =
new ZipArchive();
176 $filename = $dir . DIRECTORY_SEPARATOR . $dirname .
'.zip';
177 $zip->open($filename, ZipArchive::OVERWRITE);
178 $zip->addGlob(
'*.*');
180 Yii::app()->request->sendFile(basename($filename), file_get_contents($filename));
183 chdir(Yii::app()->runtimePath);
191 if (empty($this->response[
'itemsList'][0][
'itemObject'])) {
193 } elseif (empty($this->response[
'itemsList'][0][
'itemObject'][0][$type][0])) {
200 $files = array_diff(scandir($dir), array(
'.',
'..'));
201 foreach ($files as $file) {
202 (is_dir(
"$dir/$file")) ? $this->
delTree(
"$dir/$file") : unlink(
"$dir/$file");