-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.message.php
More file actions
646 lines (609 loc) · 18 KB
/
model.message.php
File metadata and controls
646 lines (609 loc) · 18 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
<?php
// ABSTRACT CLASS FOR TYPES OF MESSAGE
abstract class Message
{
private $messenger=false;
private $debug=false;
private $kind="";
private $host="";
private $port="";
private $vhost="";
private $queue_prefix="";
private $auth1="";
private $auth2="";
public $connected=false;
abstract protected function connect();
abstract protected function get_queue_list();
abstract protected function send_message($queue,$message);
abstract protected function read_message($queue);
abstract protected function close();
// Functions used by all concrete Message types
public function connectcheck($failed)
{
if ($failed)
{
$this->connected=false;
if ($this->debug)
{
echo "Could not connect to message queue named '".htmlspecialchars($this->kind)."'.";
}
return false;
}
else
{
$this->connected=true;
return true;
}
return false;
}
public function _format_bytes($a_bytes)
{
if ($a_bytes < 1024) {
return $a_bytes .' B';
} elseif ($a_bytes < 1048576) {
return round($a_bytes / 1024, 2) .' KiB';
} elseif ($a_bytes < 1073741824) {
return round($a_bytes / 1048576, 2) . ' MiB';
} elseif ($a_bytes < 1099511627776) {
return round($a_bytes / 1073741824, 2) . ' GiB';
} elseif ($a_bytes < 1125899906842624) {
return round($a_bytes / 1099511627776, 2) .' TiB';
} elseif ($a_bytes < 1152921504606846976) {
return round($a_bytes / 1125899906842624, 2) .' PiB';
} elseif ($a_bytes < 1180591620717411303424) {
return round($a_bytes / 1152921504606846976, 2) .' EiB';
} elseif ($a_bytes < 1208925819614629174706176) {
return round($a_bytes / 1180591620717411303424, 2) .' ZiB';
} else {
return round($a_bytes / 1208925819614629174706176, 2) .' YiB';
}
} // end function
} // end abstract class
class Message_None extends Message
{
public $connected=true;
public function connect(){$this->connected=true;return true;}
public function get_queue_list(){}
public function send_message($queue,$message){}
public function read_message($queue){}
public function close(){}
}
// CONCRETE MESSAGE TYPE: RABBIT MQ
//require_once("");
class Message_RabbitMQ extends Message
{
public function get_queue_list()
{
}
function __construct()
{
}
function connect()
{
if ($this->connected) {return true;}
$failed=false;
$this->debug=true;
$includes = folder_loader("api/PhpAmqpLib",Array("Channel","Exception","Abstract","AMQPException","AMQPProtocolException","Interface","AMQPConnection","AMQPException","AMQPStreamConnection","AbstractConnection"),Array("Tests"));
foreach ($includes as $inc)
{
include_once($inc);
}
//use PhpAmqpLib\Connection\AMQPConnection;
//use PhpAmqpLib\Message\AMQPMessage;
//if (class_exists("PhpAmqpLib\Connection\AMQPConnection")) echo "YES";
//else echo "NO";
//echo "<pre>";
//$this->vhost="";
try
{
$conn = new PhpAmqpLib\Connection\AMQPConnection($this->host, $this->port, $this->auth1, $this->auth2, $this->vhost) or $failed=true;
} catch (Exception $e)
{
echo htmlspecialchars($e);
exit;
}
$this->messenger = $conn;
return $this->connectcheck($failed);
}
function close()
{
$this->connected=false;
$this->messenger->close();
}
function send_message($queue,$message)
{
if (!$this->connected) {$this->connect();}
$ch = $this->messenger->channel();
/*
The following code is the same both in the consumer and the producer.
In this way we are sure we always have a queue to consume from and an
exchange where to publish messages.
*/
/*
name: $queue
passive: false
durable: true // the queue will survive server restarts
exclusive: false // the queue can be accessed in other channels
auto_delete: false //the queue won't be deleted once the channel is closed.
*/
$ch->queue_declare($queue, false, true, false, false);
/*
name: $exchange
type: direct
passive: false
durable: true // the exchange will survive server restarts
auto_delete: false //the exchange won't be deleted once the channel is closed.
*/
$exchanger=$queue;
$ch->exchange_declare($exchanger, 'direct', false, true, false);
$ch->queue_bind($queue, $exchanger);
$msg = new PhpAmqpLib\Message\AMQPMessage($message, array('content_type' => 'text/plain', 'delivery_mode' => 2));
$ch->basic_publish($msg, $exchanger);
$ch->close();
$this->close();
}
function read_message($queue)
{
//echo "READ MESSAGE";
if (!$this->connected) {$this->connect();}
$ch = $this->messenger->channel();
/*
name: $queue
passive: false
durable: true // the queue will survive server restarts
exclusive: false // the queue can be accessed in other channels
auto_delete: false //the queue won't be deleted once the channel is closed.
*/
$ch->queue_declare($queue, false, true, false, false);
/*
name: $exchange
type: direct
passive: false
durable: true // the exchange will survive server restarts
auto_delete: false //the exchange won't be deleted once the channel is closed.
*/
$exchanger=$queue;
$ch->exchange_declare($exchanger, 'direct', false, true, false);
$ch->queue_bind($queue, $exchanger);
$msg = $ch->basic_get($queue);
if ( isset($msg->delivery_info) )
{
if ( isset($msg->delivery_info['delivery_tag']) )
{
//echo "info:";
//print_r($msg);
$ch->basic_ack($msg->delivery_info['delivery_tag']);
}
}
//echo "<pre>";
//print_r($msg);
//var_dump($msg);
$message = "";
if ( isset($msg->body) )
{
$message = $msg->body;
}
$ch->close();
$this->close();
return $message;
} // end function (read messages)
}
// CONCRETE MESSAGE TYPE: SQS
require_once("api/aws-sdk/sdk.class.php");
class Message_SQS extends Message
{
public $region;
public function get_queue_list()
{
if (!$this->connected) {$this->connect();}
return $this->messenger->list_queues();
}
public function send_message($queue,$message)
{
if (!$this->connected) {$this->connect();}
//$this->messenger->set_region(AmazonSQS::$this->region);
$this->messenger->set_region("sqs.".$this->region_url_snippet().".amazonaws.com");
/*$amazon_user_id = $this->get_user_id();
$queue_url="";
if ( strlen($amazon_user_id)>0 )
{
$queue_url = "http://sqs.".str_replace("_","-",strtolower($this->region_url_snippet())).".amazonaws.com/$amazon_user_id/$queue";
}
else
{
echo "<error>Unable to collect IAM User Id for user ".$this->auth1." using IAM. Unable to send to SQS queue.</error>";
exit;
}
*/
$queue_url="";
//echo "QUEUE URL: $queue_url";
$queue_list_response = $this->messenger->get_queue_list();
$found_queue = false;
if ($queue_list_response)
{
if ( count($queue_list_response)>0 )
{
foreach ($queue_list_response as $queue_list_item)
{
if( strpos($queue_list_item,$queue) )
{
$found_queue=true;
$queue_url = $queue_list_item;
break;
}
}
}
}
if (!$found_queue)
{
$queue_created_successfully=false;
$create_queue_response = $this->create_queue($queue);
if ( isset($create_queue_response->body) )
{
if ( isset($create_queue_response->body->CreateQueueResult) )
{
if ( isset($create_queue_response->body->CreateQueueResult->QueueUrl) )
{
$queue_created_successfully=true;
$queue_url = $create_queue_response->body->CreateQueueResult->QueueUrl;
echo $queue_url;
//echo "CREATED QUEUE SUCCESSFULLY";
}
}
}
}
$message_content = "";
$message_receipt_handle = "";
if ($found_queue || $queue_created_successfully)
{
$response = $this->messenger->send_message($queue_url, $message);
//echo "sending message $message";
}
return $response->isOK();
}
public function get_user_id()
{
$failed=false;
$iam = new AmazonIAM(array(
'key' => $this->auth1,
'secret' => $this->auth2
)) or $failed=true;
$amazon_user_id = "";
if (!$failed)
{
$user_response = $iam->get_user();
if ( isset($user_response->body) )
{
if ( isset($user_response->body->GetUserResult) )
{
if ( isset($user_response->body->GetUserResult) )
{
if ( isset($user_response->body->GetUserResult->User) )
{
if ( isset($user_response->body->GetUserResult->User->UserId) )
{
$amazon_user_id = $user_response->body->GetUserResult->User->UserId;
}
}
}
}
}
}
return $amazon_user_id;
}
public function region_url_snippet()
{
// from http://docs.aws.amazon.com/general/latest/gr/rande.html#sqs_region
if ( $this->region=="REGION_US_E1" )
{
return "us-east-1";
}
else if ( $this->region=="REGION_US_W1" )
{
return "us-west-1";
}
else if ( $this->region=="REGION_US_W2" )
{
return "us-west-2";
}
else if ( $this->region=="REGION_APAC_SE1" )
{
return "ap-southeast-1";
}
else if ( $this->region=="REGION_APAC_SE2" )
{
return "ap-southeast-2";
}
else if ( $this->region=="REGION_APAC_NE1" )
{
return "ap-northeast-1";
}
else if ( $this->region=="REGION_SA_E1" )
{
return "sa-east-1";
}
else if ( $this->region=="REGION_US_GOV1" )
{
return "us-gov-1";
}
return "us-east-1";
}
public function read_message($queue)
{
if (!$this->connected) {$this->connect();}
$this->messenger->set_region("sqs.".$this->region_url_snippet().".amazonaws.com");
/*$amazon_user_id=$this->get_user_id();
$queue_url="";
if ( strlen($amazon_user_id)>0 )
{
$queue_url = "http://sqs.".str_replace("_","-",strtolower($this->region_url_snippet())).".amazonaws.com/$amazon_user_id/$queue";
}
else
{
return "<error>Unable to collect IAM User Id for user ".$this->auth1." using IAM. Unable to read SQS queue.</error>";
}
*/
$queue_url="";
//echo "QUEUE URL: $queue_url";
$queue_list_response = $this->messenger->get_queue_list();
$found_queue = false;
if ( $queue_list_response )
{
if ( count($queue_list_response)>0 )
{
foreach ($queue_list_response as $queue_list_item)
{
if( strpos($queue_list_item,$queue) )
{
$found_queue=true;
$queue_url = $queue_list_item;
break;
}
}
}
}
if (!$found_queue)
{
$queue_created_successfully=false;
$create_queue_response = $this->create_queue($queue);
if ( isset($create_queue_response->body) )
{
if ( isset($create_queue_response->body->CreateQueueResult) )
{
if ( isset($create_queue_response->body->CreateQueueResult->QueueUrl) )
{
$queue_created_successfully=true;
$queue_url = $create_queue_response->body->CreateQueueResult->QueueUrl;
//echo $queue_url;
//echo "CREATED QUEUE SUCCESSFULLY";
}
}
}
}
$message_content = "";
$message_receipt_handle = "";
if ($found_queue || $queue_created_successfully)
{
$response = $this->messenger->receive_message($queue_url, array(
'MaxNumberOfMessages' => 1
));
if ( isset($response->body) )
{
if ( isset($response->body->ReceiveMessageResult) )
{
if ( isset($response->body->ReceiveMessageResult->Message) )
{
if ( isset($response->body->ReceiveMessageResult->Message->Body) )
{
$message_content = $response->body->ReceiveMessageResult->Message->Body;
}
if ( isset($response->body->ReceiveMessageResult->Message->ReceiptHandle) )
{
$message_receipt_handle = $response->body->ReceiveMessageResult->Message->ReceiptHandle;
}
}
}
}
if (strlen($message_receipt_handle)>0 )
{
$response = $this->messenger->delete_message($queue_url, $message_receipt_handle);
if (!$response->isOK())
{
return "<warning>Warning: unable to delete message using receipt handle</warning>";
}
}
return $message_content;
}
else
{
return "<error>Unable to create queue, or queue status was not able to be determined.</error>";
}
}
public function close()
{
}
function __construct()
{
}
function create_queue($queue)
{
if (!$this->connected) {$this->connect();}
//echo "REGION:".$this->region;
//$this->messenger->set_region(AmazonSQS::$this->region);
$this->messenger->set_region("sqs.".$this->region_url_snippet().".amazonaws.com");
$response = $this->messenger->create_queue($queue);
if ( $response->isOK() )
{
$queue_url="";
//echo "QUEUE URL: $queue_url";
sleep(5);
$queue_list_response = $this->messenger->get_queue_list();
$found_queue = false;
if ( $queue_list_response )
{
if ( count($queue_list_response)>0 )
{
foreach ($queue_list_response as $queue_list_item)
{
if( strpos($queue_list_item,$queue) )
{
$found_queue=true;
$queue_url = $queue_list_item;
break;
}
}
}
}
$response_setattr = $this->messenger->set_queue_attributes($queue_url, array(
array( // Attribute.0
'Name' => 'MessageRetentionPeriod',
'Value' => 60
)
));
}
return $response;
}
function connect()
{
if ($this->connected) {return true;}
$failed=false;
$sqs = new AmazonSQS(array(
'key' => $this->auth1,
'secret' => $this->auth2
)) or $failed=true;
$this->messenger=$sqs;
return $this->connectcheck($failed);
}
public function bucket_syntax()
{
return "bucket";
}
} // end class
abstract class Message_Adapter
{
public $messenger;
public $kind;
function is_none()
{
if ( strpos($this->kind,"no-")!==false || strpos($this->kind,"messaging")!==false )
{
return true;
}
else
{
return false;
}
}
function is_sqs()
{
if ( strpos($this->kind,"aws")!==false || strpos($this->kind,"sqs")!==false || strpos($this->kind,"amazon")!==false )
{
return true;
}
else
{
return false;
}
}
function is_rabbit()
{
if ( strpos($this->kind,"rabbit")!==false )
{
return true;
}
else
{
return false;
}
}
}
class Post_Message_Adapter extends Message_Adapter
{
function __construct($settings)
{
if ( isset($_POST['mstype']) )
{
$this->kind=$_POST['mstype'];
}
else
{
$this->kind="unknown";
return;
}
if ($this->is_none() )
{
$this->messenger = new Message_None();
}
else if ($this->is_sqs() )
{
$this->messenger = new Message_SQS();
$this->messenger->auth1=$_POST['SQS-ACCESS-KEY'];
$this->messenger->auth2=$_POST['SQS-SECRET-KEY'];
$this->messenger->region=$_POST['SQS-REGION-NAME'];
$this->messenger->queue_prefix=$_POST['SQS-QUEUE-PREFIX'];
$this->messenger->kind=$this->kind;
$this->messenger->connect();
}
else if ($this->is_rabbit() )
{
$this->messenger = new Message_RabbitMQ();
$this->messenger->host=$_POST['RABBITHOST'];
$this->messenger->port=$_POST['RABBITPORT'];
$this->messenger->auth1=$_POST['RABBITUSER'];
$this->messenger->auth2=$_POST['RABBITPASS'];
$this->messenger->vhost=$_POST['RABBITVHOST'];
$this->messenger->queue_prefix=$_POST['RABBITQUEUEPREFIX'];
$this->messenger->kind=$this->kind;
$this->messenger->connect();
}
else
{
echo "UNRECOGNIZED MESSENGER TYPE ".$this->kind;
$this->messenger=false;
exit;
}
}
}
class Settings_Message_Adapter extends Message_Adapter
{
function __construct($settings)
{
// settings array
$this->kind=$settings['messenger']['@attributes']['value'];
if ($this->is_none() )
{
$this->messenger = new Message_None();
$this->messenger->kind=$this->kind;
}
else if ($this->is_sqs() )
{
$this->messenger = new Message_SQS();
$this->messenger->auth1=$settings[$this->kind]['access-key']['@attributes']['value'];
$this->messenger->auth2=$settings[$this->kind]['secret-key']['@attributes']['value'];
$this->messenger->region=$settings[$this->kind]['region']['@attributes']['value'];
$this->messenger->queue_prefix=$settings[$this->kind]['queue-prefix']['@attributes']['value'];
$this->messenger->kind=$this->kind;
//$this->messenger->connect();
}
else if ($this->is_rabbit() )
{
$this->messenger = new Message_RabbitMQ();
$this->messenger->host=$settings[$this->kind]['server']['@attributes']['value'];
$this->messenger->port=$settings[$this->kind]['port']['@attributes']['value'];
$this->messenger->auth1=$settings[$this->kind]['user']['@attributes']['value'];
$this->messenger->auth2=$settings[$this->kind]['pass']['@attributes']['value'];
$this->messenger->vhost=$settings[$this->kind]['vhost']['@attributes']['value'];
$this->messenger->queue_prefix=$settings[$this->kind]['queue-prefix']['@attributes']['value'];
//$this->messenger->exchanger=$settings[$this->kind]['exchange']['@attributes']['value'];
$this->messenger->kind=$this->kind;
//$this->messenger->connect();
}
else
{
echo "UNRECOGNIZED MESSENGER TYPE "+$this->kind;
$this->messenger=false;
exit;
}
}
} // end class
?>