Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
counterdata.php
1<?php
9
12
29class CounterDataTable extends Entity\DataManager
30{
31 public static function getTableName()
32 {
33 return 'b_counter_data';
34 }
35
36 public static function getMap()
37 {
38 return array(
39 new Entity\StringField('ID', array(
40 'primary' => true,
41 'default_value' => array(__CLASS__ , 'getUniqueEventId')
42 )),
43 new Entity\StringField('TYPE', array(
44 'required' => true
45 )),
46 new Entity\TextField('DATA', array(
47 'serialized' => true
48 ))
49 );
50 }
51
52 public static function getUniqueEventId()
53 {
54 list($usec, $sec) = explode(" ", microtime());
55
56 $uniqid = mb_substr(base_convert($sec.mb_substr($usec, 2), 10, 36), 0, 16);
57
58 if (mb_strlen($uniqid) < 16)
59 {
60 $uniqid .= Random::getString(16 - mb_strlen($uniqid));
61 }
62
63 return $uniqid;
64 }
65
66 public static function submitData($limit = 50)
67 {
68 if (!Catalog::isOn())
69 {
70 return '\\'.__METHOD__.'();';
71 }
72
73 $rows = array();
74
75 $r = static::getList(array(
76 'order' => array('ID' => 'ASC'),
77 'limit' => $limit
78 ));
79
80 while ($row = $r->fetch())
81 {
82 $rows[$row['ID']] = array(
83 'type' => $row['TYPE'],
84 'data' => $row['DATA']
85 );
86 }
87
88 if (!empty($rows))
89 {
90 // get queue size
91 $totalCount = static::getCount();
92 $queueSize = $totalCount > $limit ? $totalCount-$limit : 0;
93
94 // set limit
95 $dataSizeLimit = 45000;
96
97 // make an optimal dataset
98 $dataSize = mb_strlen(base64_encode(json_encode(array_values($rows))));
99
100 // records to delete
101 $toDelete = array();
102
103 if ($dataSize > $dataSizeLimit)
104 {
105 $reducedRows = array();
106
107 foreach ($rows as $id => $row)
108 {
109 $rowSize = mb_strlen(base64_encode(json_encode(array_values($row))));
110 $reducedDataSize = mb_strlen(base64_encode(json_encode(array_values($reducedRows))));
111
112 if ($rowSize > $dataSizeLimit)
113 {
114 // abnormally big row, delete it
115 $toDelete[] = $id;
116 }
117 elseif (!empty($reducedRows) && ($reducedDataSize + $rowSize) > $dataSizeLimit)
118 {
119 // it's enough
120 break;
121 }
122 else
123 {
124 $reducedRows[$id] = $row;
125 }
126 }
127
128 $rows = $reducedRows;
129 }
130
131 if (!empty($rows))
132 {
133 // if there are still some data, send it
134 $data = \http_build_query(array(
135 'op' => 'e',
136 'aid' => Counter::getAccountId(),
137 'ad[cd][value]' => base64_encode(json_encode(array_values($rows))),
138 'ad[cd][queue]' => $queueSize
139 ));
140
141 $f = fsockopen('bitrix.info', 80, $errno, $errstr, 3);
142
143 if ($f)
144 {
145 $out = "POST /bx_stat HTTP/1.1\r\n";
146 $out .= "Host: bitrix.info\r\n";
147 $out .= "Content-type: application/x-www-form-urlencoded\r\n";
148 $out .= "Content-length: ".mb_strlen($data) . "\r\n";
149 $out .= "User-Agent: Bitrix Stats Counter\r\n";
150 $out .= "Connection: Close\r\n";
151 $out .= "\r\n";
152 $out .= $data . "\r\n\r\n";
153
154 fwrite($f, $out);
155
156 $response = '';
157
158 while (!feof($f))
159 {
160 $response .= fgets($f, 128);
161 }
162
163 fclose($f);
164
165 // delete rows if service received data
166 if(mb_strpos($response, '200 OK'))
167 {
168 $toDelete = array_merge($toDelete, array_keys($rows));
169 }
170 }
171 }
172
173 // delete abnormally big and sent rows
174 foreach ($toDelete as $id)
175 {
176 static::delete($id);
177 }
178 }
179
180 return '\\'.__METHOD__.'();';
181 }
182}