-
Notifications
You must be signed in to change notification settings - Fork 14
/
script.php
349 lines (301 loc) · 11.8 KB
/
script.php
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
<?php
// Must be in workflow root directory.
// @TODO: Rewrite all of this as a class so that we can use more persistent variables
// and test connections fewer times.
require_once( __DIR__ . '/init.php' );
require_once( 'libraries/workflows.php' );
require_once( 'functions.php' );
// Set date/time to avoid warnings/errors.
if ( ! ini_get( 'date.timezone' ) ) {
$tz = exec( 'tz=`ls -l /etc/localtime` && echo ${tz#*/zoneinfo/}' );
ini_set( 'date.timezone', $tz );
}
// Get the version of OSX; if we aren't using Mavericks, then they don't get access to the GUI.
$gui = false;
$version = explode( '.', phpversion() );
if ( 5 < $version[0] ) {
$gui = true;
} else if ( 3 < $version[1] && 5 === (int) $version[0] ) {
$gui = true;
}
firstRun();
$bundle = 'com.packal';
$q = $argv;
$w = new Workflows;
$HOME = exec( 'echo $HOME' );
$data = DATA_DIR;
$cache = CACHE_DIR;
$connection = checkConnection();
if ( $connection === false ) {
$w->result( '', '', 'Warning: no viable Internet connection',
'Some features of this workflow will be unavailable without a solid Internet connection', 'assets/icons/task-attention.png', 'no', '');
}
if ( ! file_exists( $data ) ) {
mkdir( "$data", 0775, true );
}
if ( ! file_exists( $cache ) ) {
mkdir( "$cache", 0775, true );
}
if ( ! file_exists( "$data/config" ) ) {
mkdir( "$data/config", 0775, true );
}
if ( ! file_exists( "$data/endpoints" ) ) {
mkdir( "$data/endpoints", 0775, true );
}
if ( ! file_exists( "$data/config/blacklist.json" ) ) {
// Make an empty blacklist file if one doesn't exist.
$blacklist = array();
file_put_contents( "$data/config/blacklist.json", utf8_encode( json_encode( $blacklist ) ) );
}
generateEndpoints();
// Not all of these are used right now.
if ( ! file_exists( "$data/config/config.xml" ) ) {
$d = '<?xml version="1.0" encoding="UTF-8"?><config></config>';
$config = new SimpleXMLElement( $d );
$config->packalAccount = 0;
$config->forcePackal = 0;
$config->backups = 3;
$config->username = '';
$config->authorName = '';
$config->workflowReporting = '1';
$config->notifications = '2';
$config->apiKey = '';
$config->asXML( "$data/config/config.xml" );
unset( $config );
} else {
$config = simplexml_load_file( "$data/config/config.xml" );
}
if ( ! file_exists( "$data/manifest.xml" ) ) {
// This should be taken care of in the firstRun() call,
// but the redundancy is fine.
if ( getManifest() == false ) {
$w->result( '', '', 'Error: Packal Updater',
'The workflow manifest is not valid, and there is no valid Internet connection to retrieve a new one.', 'assets/icons/task-reject.png', 'no', '');
echo $w->toxml();
die();
}
}
// Update the manifest if past cache.
// This is a potential failure spot.
if ( $connection !== false ) {
exec( "'" . __DIR__ . "/cli/packal.sh' update" );
}
// Do the workflow reporting script as long as the config option is set.
// if ( $config->workflowReporting == '1' ) {
// exec( "nohup php '" . __DIR__ . "/report-usage-data.php' > /dev/null 2>&1 &" );
// }
$blacklist = json_decode( file_get_contents( "$data/config/blacklist.json" ), true );
$manifest = @simplexml_load_file( "$data/manifest.xml" );
// The manifest is not valid. Let's try to get it before we fail.
if ( empty( $manifest ) ) {
if ( getManifest() === false ) {
$w->result( '', '', 'Error: Packal Updater',
'The workflow manifest is not valid, and there is no valid Internet connection to retrieve a new one.', 'assets/icons/task-reject.png', 'no', '');
echo $w->toxml();
die();
}
// So, the we didn't get an error, but let's try to make sure it is valid XML.
$manifest = @simplexml_load_file( "$data/manifest.xml" );
if ( empty( $manifest ) ) {
$w->result( '', '', 'Error: Packal Updater',
'The workflow manifest is not valid, and there is no valid Internet connection to retrieve a new one.', 'assets/icons/task-reject.png', 'no', '');
echo $w->toxml();
die();
}
}
// Okay, we're good. Or, at least we should be.
$json = json_decode( file_get_contents( "$data/endpoints/endpoints.json" ), true );
$mine = array_keys( $json );
$count = count( $manifest->workflow );
$me = exec( "php '" . __DIR__ . "/cli/packal.php' getOption username" );
foreach ( $manifest->workflow as $wf ) :
if ( in_array( $wf->bundle, $mine ) ) {
$packal[] = (string) $wf->bundle;
if ( isset( $me ) && ( ! empty( $me ) ) && ( $wf->author == $me ) ) {
$mywf[] = (string) $wf->bundle;
}
}
endforeach;
unset( $wf );
// Grab manifest information.
foreach ( $manifest as $m ) :
$manifestBundles[] = $m->bundle;
$wf[ "$m->bundle" ]['name'] = $m->name;
$wf[ "$m->bundle" ]['author'] = $m->author;
$wf[ "$m->bundle" ]['version'] = $m->version;
$wf[ "$m->bundle" ]['short'] = $m->short;
$wf[ "$m->bundle" ]['updated'] = $m->updated;
$wf[ "$m->bundle" ]['url'] = $m->url;
endforeach;
foreach ( $json as $k => $v ) :
$pattern = '/[\/A-Za-z0-9-_ .]{1,}\/user\.workflow\.([0-9A-Z.-]{1,})/';
preg_match( $pattern, $v, $matches );
if ( isset( $matches[1] ) ) {
$json[ $k ] = 'user.workflow.' . $matches[1];
}
endforeach;
foreach ( $json as $k => $v ) :
if ( file_exists( "../$v/packal/package.xml" ) ) {
$p = simplexml_load_file( "../$v/packal/package.xml" );
if ( in_array( $k, $manifestBundles ) ) {
if ( (string) $p->version != $wf[ "$k" ]['version'] ) {
if ( ! in_array( $k, $blacklist ) ) {
$updates[ $k ]['name'] = (string) $p->name;
$updates[ $k ]['path'] = "../$v";
$updates[ $k ]['version'] = (string) $p->version;
}
}
}
}
endforeach;
if ( empty( $q[1] ) ) {
if ( ! isset( $updates ) ) {
$w->result( 'updates', 'updates', 'All of your workflows are up to date.', '', 'assets/icons/task-complete.png', 'no', '' );
} else {
if ( count( $updates ) > 1 ) {
$message = 'There are ' . count( $updates ) . ' updates pending.';
} else { $message = 'There is 1 update pending.';
}
$w->result( 'updates', 'updates', 'Updates available', $message, '', 'no', 'update' );
}
if ( ( time() - filemtime( "$data/manifest.xml" ) < 86400 ) ) {
$manifestTime = getManifestModTime();
$w->result( '', 'manifest-update', 'The manifest is up to date.', "Last updated $manifestTime", 'assets/icons/task-complete.png', 'yes', '' );
} else {
$manifestTime = getManifestModTime();
$w->result( '', 'manifest-update', 'The manifest is out of date.', "Last updated $manifestTime", 'assets/icons/task-attention.png', 'yes', '' );
}
if ( $gui === true ) {
$w->result( 'open-gui', 'open-gui', "Open Graphical 'Application'",
'Configure this workflow, Update workflows, and learn about this workflow. (RECOMMENDED)', 'assets/icons/applications-education-miscellaneous.png', 'yes', '');
} else {
$w->result( '', '', "GUI Not Available'",
'A GUI to configure and operate this workflow is available if you have OS X 10.9 or 10.10.', 'assets/icons/applications-education-miscellaneous.png', 'no', '');
}
if ( isset( $mywf ) ) {
$mywf = count( $mywf );
} else { $mywf = 0;
}
// if ( isset( $config->authorName ) && ( ! empty( $config->authorName ) ) {
// $w->result( '', '', 'Informational',
// "There are $count workflows in the manifest," .
// " of which, you have " . count( $packal ) .
// " installed, and you wrote " . $mywf . " of those.", '', 'no', '');
// }
$w->result( 'blacklist', 'blacklist', 'Manage Blacklist', 'Configure which workflows Packal updates', 'assets/icons/flag-black.png', 'no', 'blacklist' );
$w->result( '', '', 'Informational',
"There are $count workflows in the manifest," .
' of which, you have ' . count( $packal ) .
' installed, and you wrote ' . $mywf . ' of those.', 'assets/icons/help-about.png', 'no', '');
$w->result( '', 'setup', 'Configure', 'Make this workflow work best for you.', 'assets/icons/applications-system.png', 'no', 'setup' );
echo $w->toxml();
die();
}
if ( strpos( $q[1], 'update' ) !== false ) {
if ( ! isset( $updates ) ) {
$updates = array();
}
if ( count( $updates ) > 0 ) {
if ( count( $updates ) > 1 ) {
// Don't allow the updates if there is no internet connection.
if ( $connection !== false ) {
$w->result( 'update-all', 'update-all', 'Update all workflows', '', '', 'yes', '' );
}
}
foreach ( $updates as $k => $v ) :
// Get the workflow icon, if it exists, otherwise, fallback to package icon
if ( file_exists( $v['path'] . '/icon.png' ) ) {
$icon = $v['path'] . '/icon.png';
} else { $icon = 'assets/icons/package.png';
}
if ( $connection !== false ) {
$w->result( "update-$k", "update-$k", 'Update ' . $v['name'], 'Update version ' . $v['version'] . ' => ' . $wf[ $k ]['version'], $v['path'] . '/icon.png', '', '' );
} else { $w->result( "update-$k", '', 'An update for ' . $v['name'] . ' is available.', 'Update version ' . $v['version'] . ' => ' . $wf[ $k ]['version'] . '. << Exception: no viable Internet connection. Update impossible. >>' , $v['path'] . '/icon.png', 'no', '' );
}
endforeach;
} else {
$w->result( '', '', 'All of your workflows are up to date.', '', 'assets/icons/task-complete.png', 'no', '' );
}
echo $w->toxml();
// We're done here.
die();
}
if ( strpos( $q[1], 'setup' ) !== false ) {
// The next argument removes the forced-setup option.
if ( ( ! file_exists( "$data/config/first-run-alfred" ) ) && ( ! file_exists( "$data/config/first-run" ) ) ) {
file_put_contents( "$data/config/first-run-alfred", 'done' );
}
$options = array(
'authorName' => 'What name do you use when you write workflows?',
'packalAccount' => 'Do you have an account on Packal?',
'username' => 'What is your Packal username?',
'workflowReporting' => 'Would you like to send anonymous data about your installed workflows to Packal.org?',
'backups' => 'How many backups of workflows would you like to keep?',
);
foreach ( $options as $k => $v ) :
if ( isset( $config->$k ) ) {
if ( ( (string) $config->$k == '1' ) && ( $k != 'backups' ) ) {
$message = 'Current value: Yes';
} elseif ( ( (string) $config->$k == '0' ) && ( $k != 'backups' ) ) {
$message = 'Current value: No';
} else {
if ( ! empty( $config->$k ) ) {
$message = 'Current value: ' . $config->$k;
} else { $message = '< not set >';
}
}
} else {
$message = 'Not set.';
}
// Setup icons per option
$icon = 'assets/icons/';
switch ( $k ) :
case 'authorName':
$icon .= 'code-context.png';
break;
case 'username' :
$icon .= 'im-user.png';
break;
case 'workflowReporting' :
$icon .= 'svn-commit.png';
break;
case 'backups' :
$icon .= 'player-time.png';
break;
case 'packalAccount' :
$icon .= 'flag.png';
break;
default:
$icon = '';
break;
endswitch;
if ( ( $k == 'username' ) && ( $config->packalAccount == 1 ) ) {
$w->result( "set-$k", "set-$k", $v, $message, $icon, 'yes', '' );
} elseif ( ( $k == 'packalAccount' ) && ( $config->packalAccount == 1 ) ) {
continue;
} elseif ( ! ( $k == 'username' ) ) {
$w->result( "set-$k", "set-$k", $v, $message, $icon, 'yes', '' );
}
endforeach;
echo $w->toxml();
die();
} elseif ( strpos( $q[1], 'blacklist' ) !== false ) {
// Option to Blacklist a workflow
// @TODO: Sort the workflows by name.
foreach ( $json as $k => $v ) :
if ( file_exists( "../$v/packal/package.xml" ) ) {
if ( in_array( $k, $manifestBundles ) ) {
if ( ! in_array( $k, $blacklist ) ) {
$w->result( "blacklist-$k", "blacklist-$k", "Add '" . $wf[ $k ]['name'] . "' to blacklist", "Prevent Packal from updating '" . $wf[ $k ]['name'] . "' ($k)", 'assets/icons/user-online.png', 'yes', '' );
} else {
$w->result( "whitelist-$k", "whitelist-$k", "Remove '" . $wf[ $k ]['name'] . "' from blacklist", "Let Packal update '" . $wf[ $k ]['name'] . "' ($k)", 'assets/icons/user-offline.png', 'yes', '' );
}
}
}
endforeach;
echo $w->toxml();
die();
}
// For all good measures....
echo $w->toxml();
die();