-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapshots.php
More file actions
434 lines (374 loc) · 12.5 KB
/
snapshots.php
File metadata and controls
434 lines (374 loc) · 12.5 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
error_reporting(E_ALL ^ E_NOTICE);
/**
* @package Adobe Edge Inspect
* @author Peter Schmalfeldt <me@peterschmalfeldt.com>
*/
/**
* Begin Document
*/
class Snapshots
{
/**
* Path to your snapshot. Needs to be publicly accessible for image viewing.
* You can create an alias to your Adobe Edge Inspect with something like this:
*
* ln -s /Users/`whoami`/Documents/Edge\ Inspect /path/to/this/folder/snapshots
*
* I recommend creating an alias called "snapshots" in this projects folder
*/
const PATH_TO_SNAPSHOTS = 'snapshots/';
/**
* Allowed Offset Seconds
*
* For grouping snapshots taken at the same time.
* Snapshot files can have different URL's ( mobile version
* might auto add hashtag, while desktop does not, for same initial URL )
* and timestamps can be off by a few seconds from each
* other since they are coming in at different times
*/
const ALLOWED_OFFSET_SECONDS = 5;
/**
* Filters to use for Sorting
*/
var $filters = array(
'dates' => array(),
'operating_system' => array(),
'device_model' => array(),
'device_res' => array(),
'pixel_density' => array(),
'urls' => array()
);
/**
* Completed Sorted Files
*/
var $grouped_files = array();
/**
* Build Snapshots from Directory
*/
function __construct()
{
// Fetch a list of files in this directory, sort them by creation date in DESC order so newest are first
$files = glob(self::PATH_TO_SNAPSHOTS . '*.txt');
// If there were no files, then let's leave the function
if(!$files)
{
return false;
}
// If there are files, then we can sort them for later use
$files = array_reverse($files);
// Setup some temp arrays for sorting
$operating_system = '';
$group_count = -1;
$last_time = 0;
// Loop through our files and create some arrays to store the data later
foreach($files as $filename)
{
// Setup initial details
$file = $filename;
$image = str_replace('.txt', '.png', $file);
$name = basename($file);
// Do some RegEx for getting the date
preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}\.[0-9]{2}\.[0-9]{2}/', $name, $matches);
// Convert the date into something PHP can use
$date = str_replace('_', ' ', $matches[0]);
$date = str_replace('.', ':', $date);
$select_date = date('F jS\, Y \(l\)', strtotime($date));
$display_date = date('l\, F jS\, Y \@ g:i:s A', strtotime($date));
// Get Device Name
$raw_device = str_replace($matches[0].'_', '', $name);
$raw_device = str_replace('.txt', '', $raw_device);
$device = str_replace($matches[0].'_', '', $name);
$device = str_replace('.txt', '', $device);
$device = str_replace('_s_', '’s ', $device);
$device = str_replace('_', ' ', $device);
// Get current file time
$file_time = strtotime($date);
// Check if this file was created around the same time as the last one
if(abs($file_time - $last_time) > self::ALLOWED_OFFSET_SECONDS)
{
$group_count++;
$this->grouped_files[$group_count] = array();
}
// Update last file time
if($file_time != $last_time)
{
$last_time = $file_time;
}
// Do a quick check that the image actually exists, sometimes the file names are off by one second
if( !file_exists($image))
{
$time = new DateTime($date);
$time->modify("+1 second");
$fixed_image = str_replace(' ', '_', $time->format('Y-m-d H:i:s'));
$fixed_image = self::PATH_TO_SNAPSHOTS . str_replace(':', '.', $fixed_image) . '_' . $raw_device . '.png';
if(file_exists($fixed_image))
{
$image = $fixed_image;
}
else
{
$time = new DateTime($date);
$time->modify("-2 second");
$fixed_image = str_replace(' ', '_', $time->format('Y-m-d H:i:s'));
$fixed_image = self::PATH_TO_SNAPSHOTS . str_replace(':', '.', $fixed_image) . '_' . $raw_device . '.png';
if(file_exists($fixed_image))
{
$image = $fixed_image;
}
}
}
// Capture the date in the TXT file and store it to an array with some other info
$temp_array = array();
if (file_exists($filename))
{
// Set some data we already know
$temp_array['device'] = $device;
$temp_array['image'] = $image;
$temp_array['date'] = $date;
$temp_array['select_date'] = $select_date;
$temp_array['display_date'] = $display_date;
$temp_array['data_file'] = $file;
$found_date_filter = false;
foreach($this->filters['dates'] as $filter_date)
{
if($filter_date['select_date'] == $select_date)
{
$found_date_filter = true;
}
}
if( !$found_date_filter)
{
$this->filters['dates'][] = array(
'date' => $date,
'select_date' => $select_date,
'display_date' => $display_date,
'date_class' => 'date_' . md5($select_date)
);
}
// Open the text file to get the snapshot info
$fp = fopen($filename, 'r') or die("Couldn't open $filename");
while ( !feof($fp))
{
$line = str_replace("\n", '', fgets($fp, 1024));
list($key, $value) = explode(' = ', $line, 2);
if( !empty($key))
{
switch($key)
{
case 'os_name':
if( !$this->_search($this->filters['operating_system'], 'name', $value))
{
$this->filters['operating_system'][] = array(
'name' => $value,
'os_class' => strtolower(str_replace(' ', '_', $value)),
'versions' => array()
);
}
$operating_system = $value;
break;
case 'os_version':
foreach($this->filters['operating_system'] as $id => $os)
{
if($os['name'] == $operating_system)
{
if( !in_array($value, $os['versions']))
{
$found_version_filter = false;
foreach($this->filters['operating_system'][$id]['versions'] as $version)
{
if($version['id'] == $value)
{
$found_version_filter = true;
}
}
if( !$found_version_filter)
{
$this->filters['operating_system'][$id]['versions'][] = array(
'id' => $value,
'id_class' => strtolower(str_replace(' ', '_', $operating_system)) . '_version_' . strtolower(str_replace('.', '_', $value))
);
}
}
}
}
break;
case 'device_model':
if( !in_array($value, $this->filters['device_model']))
{
$this->filters['device_model'][] = $value;
}
break;
case 'device_res':
if( !in_array($value, $this->filters['device_res']))
{
$this->filters['device_res'][] = $value;
}
break;
case 'pixel_density':
if( !in_array($value, $this->filters['pixel_density']))
{
$this->filters['pixel_density'][] = $value;
}
break;
}
$temp_array[$key] = $value;
}
}
}
$found_url_filter = false;
foreach($this->filters['urls'] as $filter_url)
{
if($filter_url['url'] == $temp_array['url'])
{
$found_url_filter = true;
}
}
if( !$found_url_filter)
{
$this->filters['urls'][] = array(
'url' => $temp_array['url'],
'url_class' => 'url_' . md5($temp_array['url'])
);
}
// Create new group to store device info
if( !isset($this->grouped_files[$group_count]['devices']))
{
$this->grouped_files[$group_count] = array(
'group' => ($group_count+1),
'group_class' => 'group_' . ($group_count+1),
'url' => $temp_array['url'],
'url_class' => 'url_' . md5($temp_array['url']),
'date' => $temp_array['select_date'],
'date_class' => 'date_' . md5($temp_array['select_date']),
'date_time' => $temp_array['display_date'],
'select_date' => $temp_array['select_date'],
'device_count' => 1,
'devices' => array($temp_array)
);
}
// Group already exists, so just add new info
else
{
$this->grouped_files[$group_count]['devices'][] = $temp_array;
$this->grouped_files[$group_count]['device_count']++;
}
}
}
/**
* Get Snapshots
*/
public function get_snapshots()
{
// Check if we can get the real path, otherwise we do not have permission for the parent folder to do anything
if ( !realpath(self::PATH_TO_SNAPSHOTS))
{
$errors[] = array('message' => '<b style="color: red;">Invalid Parent Folder Permissions.</b><br />CHMOD <b>' . self::PATH_TO_SNAPSHOTS . '</b>\'s Parent Folder to <b>755</b> to fix this issue.');
$return = array(
'errors' => $errors,
'snapshots' => array(),
'filters' => array()
);
return json_encode($return);
}
// Check if the folder is readable, otherwise we cannot get the directory listing
if ( !is_readable(self::PATH_TO_SNAPSHOTS))
{
$errors[] = array('message' => '<b style="color: red;">Invalid Folder Permissions.</b><br />CHMOD <b>' . realpath(self::PATH_TO_SNAPSHOTS) . '</b> to <b>755</b>.');
$return = array(
'errors' => $errors,
'snapshots' => array(),
'filters' => array()
);
return json_encode($return);
}
// Prepare an Object to return
$return = array(
'errors' => array(),
'snapshots' => $this->grouped_files,
'filters' => $this->filters
);
// Convert to JSON
return json_encode($return);
}
/**
* Delete Snapshots from Group
*/
public function delete_snapshot_group($group_id)
{
$errors = array();
$removed_files = array();
// Check if parent directory is writable, otherwise we cannot delete the files
if ( !is_writable(self::PATH_TO_SNAPSHOTS)) {
$errors[] = array('message' => 'Invalid Permissions to Delete Files. CHMOD <b>' . realpath(self::PATH_TO_SNAPSHOTS) . '</b> to <b>777</b> to fix this issue.');
$return = array(
'errors' => $errors,
'removed_files' => $removed_files
);
return json_encode($return);
}
foreach($this->grouped_files[$group_id]['devices'] as $device)
{
// Remove Image File
$image_file = html_entity_decode($device['image'], ENT_QUOTES | ENT_IGNORE, 'UTF-8');
if(file_exists($image_file))
{
chmod($image_file, 0666);
unlink($image_file);
$removed_files[] = $device['image'];
}
// Remove Text File
$text_file = html_entity_decode($device['data_file'], ENT_QUOTES | ENT_IGNORE, 'UTF-8');
if(file_exists($text_file))
{
chmod($text_file, 0666);
unlink($text_file);
$removed_files[] = $device['data_file'];
}
}
$return = array(
'errors' => $errors,
'removed_files' => $removed_files
);
return json_encode($return);
}
/**
* Custom Array Search function
*
* @param array $array Array to use
* @param string $key Array key to use
* @param string $value Value we are looking for
*
* @access private
*/
private function _search($array, $key, $value)
{
$results = array();
if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
{
$results[] = $array;
}
foreach ($array as $subarray)
{
$results = array_merge($results, $this->_search($subarray, $key, $value));
}
}
return $results;
}
}
$snapshots = new Snapshots();
switch($_GET['task'])
{
case 'get_snapshots':
echo $snapshots->get_snapshots();
break;
case 'delete_snapshot_group':
if(isset($_GET['group']))
{
echo $snapshots->delete_snapshot_group($_GET['group']);
}
break;
}
/* End of file snapshots.php */