-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutil.php
411 lines (337 loc) · 9.75 KB
/
util.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
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
<?php
/**
* Send email via Amazon SES
*
* The AWSSDKforPHP library must be installed first!
* http://aws.amazon.com/sdkforphp/
*
* @author Dotan Cohen
* @version 2013-11-03
*
* @param string|array $to The addresss(es) of the intended recipients of the mail
* @param string $subject The subject of the mail
* @param string|array $message The text of the mail, or an array containing 'text' and 'html' elements.
* @param string $to The addresss of the sender of the mail
* @param string|array $cc The addresss(es) to which copies of the mail should be sent
* @param string|array $bcc The addresss(es) to which copies of the mail should be surreptitiously sent
*
* @return bool
*/
function send_email_ses($to, $subject, $message, $from, $cc=NULL, $bcc=NULL)
{
// http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.Ses.SesClient.html#_sendEmail
$client = get_aws_client('SES');
$addresses = array();
$addresses['ToAddresses'] = is_array($to) ? $to : array($to);
if ( $cc!==NULL ) {
$addresses['CcAddresses'] = is_array($cc) ? $bcc : array($cc);
}
if ( $bcc!==NULL ) {
$addresses['BccAddresses'] = is_array($bcc) ? $bcc : array($bcc);
}
if ( !is_array($message) ) {
$message = array('text'=>$message);
}
$formatted = array(
'Source' => $from,
'Destination' => array(
'ToAddresses' => $addresses['ToAddresses'],
//'BccAddresses' => array($addresses['BccAddresses']),
//'CcAddresses' => array($addresses['CcAddresses']),
),
'Message' => array(
'Subject' => array(
'Data' => $subject,
'Charset' => 'UTF-8',
),
'Body' => array(
'Text' => array(
'Data' => $message['text'],
'Charset' => 'UTF-8',
),
//'Html' => array(
//// Data is required
//'Data' => $message['html'],
//'Charset' => 'UTF-8',
//),
),
),
//'ReplyToAddresses' => array('string', ... ),
//'ReturnPath' => 'string',
);
if ( isset($addresses['BccAddresses']) ) {
$formatted['Destination']['BccAddresses'] == $addresses['BccAddresses'];
}
if ( isset($addresses['CcAddresses']) ) {
$formatted['Destination']['CcAddresses'] == $addresses['CcAddresses'];
}
if ( isset($message['html']) ) {
$formatted['Message']['Body']['Html'] == $message['html'];
}
$result = $client->sendEmail($formatted);
// TODO: Error handling
return True;
}
/**
* Return an Amazon AWS client
*
* The AWSSDKforPHP library must be installed first!
* http://aws.amazon.com/sdkforphp/
* Your AWS Key and Secret must be retrieved from here:
* https://portal.aws.amazon.com/gp/aws/securityCredentials
*
* @author Dotan Cohen
* @version 2013-11-03
*
* @return bool
*/
function get_aws_client($service)
{
// http://docs.aws.amazon.com/aws-sdk-php/guide/latest/quick-start.html#creating-a-client
require './vendor/autoload.php';
$service = strtolower($service);
switch($service){
case('ses'):
//use Aws\Ses\SesClient;
$client = Aws\Ses\SesClient::factory(array(
'key' => '',
'secret' => '',
'region' => 'us-east-1',
));
break;
}
return $client;
}
/**
* Send email via Amazon SES
*
* The AWSSDKforPHP library must be installed first!
* http://aws.amazon.com/sdkforphp/
* Note that this function refers to an outdated AWSSDK version.
*
* @author Dotan Cohen
* @version 2013-08-29
*
* @param string|array $to The addresss(es) of the intended recipients of the mail
* @param string $subject The subject of the mail
* @param string $message The text of the mail
* @param string $to The addresss of the sender of the mail
* @param string|array $cc The addresss(es) to which copies of the mail should be sent
* @param string|array $bcc The addresss(es) to which copies of the mail should be surreptitiously sent
*
* @return bool
*/
function send_email_ses_SDK1($to, $subject, $message, $from, $cc=NULL, $bcc=NULL)
{
require_once('AWSSDKforPHP/sdk.class.php');
require_once('AWSSDKforPHP/services/ses.class.php');
$amazonSes = new AmazonSES();
$addresses = array();
$addresses['ToAddresses'] = is_array($to) ? $to : array($to);
if ( $cc!==NULL ) {
$addresses['CcAddresses'] = is_array($cc) ? $bcc : array($cc);
}
if ( $bcc!==NULL ) {
$addresses['BccAddresses'] = is_array($bcc) ? $bcc : array($bcc);
}
$message_array = array(
'Subject.Charset' => 'UTF-8',
'Body.Text.Charset' => 'UTF-8',
'Subject.Data' => $subject,
'Body.Text.Data' => $message
);
$response = $amazonSes->send_email($from, $addresses, $message_array);
// TODO: Error handling. See Zim file for error examples.
return $response->isOK();
}
/**
* Ensure that all necessary array elements are present and not empty.
*
* Pass to this function the array to check in the first parameter, then
* in each additional parameter pass in strings which represent array
* elements which must be present and not empty.
*
* @author Dotan Cohen
* @version 2013-06-03
*
* @param array $consideration Array to be checked
* @param string ... Elements which must exist in array and not be empty strings.
*
* @return bool
*/
function ensure_fields($consideration)
{
if ( !is_array($consideration) ) {
return NULL;
}
$args = func_get_args();
$pass = 0;
foreach ( $args as $a ) {
if ( $pass++ == 0 ) {
continue;
}
if ( !is_string($a) ) {
return NULL;
}
if ( !isset($consideration[$a]) || $consideration[$a]=='' ) {
return FALSE;
}
}
return TRUE;
}
/**
* Return an array containing the quoted, unquoted, plused, and minused portions of $text
*
* @author Dotan Cohen
* @version 2013-07-03
*
* @param string $text The text to parse
* @param array $combine Array of elements to combine into strings.
*
* @return array
*/
function separate_operator_text($text, $combine_unquoted=FALSE, $combine_quoted=FALSE)
{
// TODO: Do not disregard lone +/-
// TODO: Handle +/- before quoted text
if ( !is_string($text) || !is_bool($combine_unquoted) || !is_bool($combine_quoted) ) {
return NULL;
}
$output = array();
$output['quoted'] = array();
$output['unquoted'] = array();
$output['plus'] = array();
$output['minus'] = array();
$output['unquoted_preliminary'] = array(); // Using subarray in $output to simplify un/quoted separation
$text_parts = explode('"', $text);
$quoted = FALSE;
foreach ( $text_parts as $tp ) {
$output_element = $quoted ? 'quoted' : 'unquoted_preliminary';
$quoted = !$quoted;
if ( trim($tp)!='' ) {
$output[$output_element][] = trim($tp);
}
}
foreach ( $output['unquoted_preliminary'] as $up ) {
$bits = explode(' ', $up);
$plus = '';
$minus = '';
$unquoted = '';
foreach ( $bits as $bit ) {
if ( $bit[0]=='+' ) {
$plus .= substr($bit, 1) . ' ';
} else if ( $up[0]=='-' ) {
$minus .= substr($bit, 1) . ' ';
} else {
$unquoted .= $bit . ' ';
}
}
if ( $plus!='' ) {
$output['plus'][] = substr($plus, 0 ,-1);
}
if ( $minus!='' ) {
$output['minus'][] = substr($minus, 0 ,-1);
}
if ( $unquoted!='' ) {
$output['unquoted'][] = substr($unquoted, 0 ,-1);
}
}
unset($output['unquoted_preliminary']);
if ( $combine_unquoted ) {
$output['unquoted'] = implode(' ', $output['unquoted']);
}
if ( $combine_quoted ) {
$output['quoted'] = implode(' ', $output['quoted']);
}
return $output;
}
/**
* Return an array containing the typical values of it's elements
*
* Different instances of an array will typically hold different values for each
* of the defined (associative) elements. In order to reverse-engineer the format
* of the array, one must have access to many 'typical values' for each element.
* Thus, this function takes an array of 'typical arrays' and creates a master array
* for which each element of the 'typical arrays' contains an array 'values' which
* contain the values of the original arrays.
*
* http://stackoverflow.com/questions/17194649/get-path-and-value-of-all-elements-in-nested-associative-array
*
* @author Dotan Cohen
* @author Jacob S
* @version ???? (Not done yet)
*
* @param array $input An array containing 'typical arrays'.
*
* @return array
*/
function get_typical_results($input)
{
if ( !is_array($input) ) {
return FALSE;
}
$typical_results = new stdClass();
$rn = 1; // Result Number
foreach ( $input as $element ) {
if ( !is_array($elementi) ) {
continue;
}
$pd = 0; // Path Depth
$path = array();
foreach ( $element as $f1=>$v1 ) {
//$path[] =
$typical_results[$k1][$rn] = $v1;
}
$rn += 1;
}
return $typical_results;
}
/**
* Return an valid US phone number, in digits only without leading '1'
*
* @author Dotan Cohen
* @version 2013-12-02
*
* @param string/int $phone A string containing a potential US phone number.
*
* @return string If valid US phone number found.
* @return bool FALSE if no valid US phone number found.
*/
function validate_phone($phone)
{
if ( is_int($phone) ) {
$phone = strval($phone);
}
if ( !is_string($phone) ) {
return NULL;
}
$phone = preg_replace("/[^0-9]/", "", $phone);
// If the string include the leading '1' then remove it.
// This is simplified due to the fact that no US area codes start with '1'
if ( substr($phone,0, 1) == '1' ) {
$phone = substr($phone, 1);
}
if ( 10 != mb_strlen($phone, 'utf-8') ) {
return FALSE;
}
// No US area codes start with '0' or '1'
if ( substr($phone, 0, 1)=='0' || substr($phone, 0, 1)=='1' ) {
return FALSE;
}
return $phone;
}
/**
* Return a new GUID v4
*
* http://stackoverflow.com/a/15875555/343302
*
* @return string
*/
function getNewGuid()
{
$data = openssl_random_pseudo_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
?>