-
Notifications
You must be signed in to change notification settings - Fork 0
/
icafe-vpp.php
2726 lines (2200 loc) · 113 KB
/
icafe-vpp.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
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?PHP
/*
PLUGIN META INFO FOR WORDPRESS LISTINGS
Plugin Name: iCafe Apple VPP Manager
Description: Wordpress plugin to assist in managing Apple's Volume Purchase Program
Version: 1.1
Author: Chris Nilsson
*/
register_activation_hook( __FILE__, 'icafe_vpp_activate' );
register_deactivation_hook( __FILE__, 'icafe_vpp_deactivate' );
add_action('plugins_loaded', 'icafe_vpp_update');
add_action('admin_menu', 'icafe_vpp_admin_menu');
add_action( 'admin_enqueue_scripts', 'icafe_vpp_admin_styles_scripts' );
add_action( 'wp_enqueue_scripts', 'icafe_vpp_styles_scripts' );
add_action( 'edit_user_profile', 'icafe_vpp_add_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'icafe_vpp_save_custom_user_profile_fields' );
add_action( 'after_setup_theme', 'icafe_vpp_code_download');
add_action('template_redirect','icafe_vpp_restricted');
add_shortcode( 'iCafe_VPP', 'create_VPP_request_page' );
add_shortcode( 'icafe_VPP', 'create_VPP_request_page' );
add_shortcode( 'icafe_vpp', 'create_VPP_request_page' );
//Activate the plugin
function icafe_vpp_activate() {
update_option("icafe_vpp_version", "1.1");
$current_user = wp_get_current_user();
add_option("icafe_vpp_program_manager", $current_user->ID);
add_option("icafe_vpp_program_manager_itunes_account", "");
add_option("icafe_vpp_program_manager_itunes_pw", "");
add_option("icafe_vpp_use_single_facilitator_pw", "");
add_option("icafe_vpp_facilitator_pw", "");
add_option("icafe_vpp_facilitator_ids", "");
add_option("icafe_vpp_plugin_url", "");
add_option("icafe_vpp_global_itunes", "");
add_option("icafe_vpp_current_appleid_pw", "");
add_option('icafe_vpp_program_descriptions', "");
add_option('icafe_vpp_program_description_lable', "");
add_option('global_appleid_sec1', "");
add_option('global_appleid_sec2', "");
add_option('global_appleid_sec3', "");
add_option('global_appleid_sec1_answer', "");
add_option('global_appleid_sec2_answer', "");
add_option('global_appleid_sec3_answer', "");
add_option('icafe_vpp_new_pf_email', "");
add_option('icafe_vpp_new_pf_universal_pw', "");
add_option('icafe_vpp_new_pf_month', "");
add_option('icafe_vpp_new_pf_day', "");
add_option('icafe_vpp_new_pf_year', "");
add_option('icafe_vpp_new_pf_sec_q', "");
add_option('icafe_vpp_new_pf_sec_a', "");
add_option('global_appleid_change_interval', "");
global $wpdb;
//create apps table
$table_name = $wpdb->prefix . "icafe_vpp_apps";
if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
`aid` int(11) NOT NULL AUTO_INCREMENT,
`pid` int(11) DEFAULT NULL,
`app` text COLLATE utf8_unicode_ci,
`publisher` text COLLATE utf8_unicode_ci,
`quantity` int(11) DEFAULT NULL,
`for_user` text COLLATE utf8_unicode_ci,
`cost` decimal(11,2) DEFAULT NULL,
`url` text COLLATE utf8_unicode_ci,
`codes` longblob,
`size` int(11) DEFAULT NULL,
`type` text COLLATE utf8_unicode_ci,
`name` text COLLATE utf8_unicode_ci,
`app_status` text COLLATE utf8_unicode_ci,
PRIMARY KEY (`aid`)
) ENGINE=MyISAM AUTO_INCREMENT=166 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
//create programs table
$table_name = $wpdb->prefix . "icafe_vpp_programs";
if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
`pid` int(11) NOT NULL AUTO_INCREMENT,
`program_name` text COLLATE utf8_unicode_ci,
`program_description` text COLLATE utf8_unicode_ci,
`owner` int(11) DEFAULT NULL,
`facilitator` int(11) DEFAULT NULL,
`apple_id` text COLLATE utf8_unicode_ci,
`password` text COLLATE utf8_unicode_ci,
`balance` decimal(11,2) DEFAULT NULL,
`status` text COLLATE utf8_unicode_ci,
PRIMARY KEY (`pid`)
) ENGINE=MyISAM AUTO_INCREMENT=104 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
//create vouchers table
$table_name = $wpdb->prefix . "icafe_vpp_vouchers";
if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
`vid` int(11) NOT NULL AUTO_INCREMENT,
`voucher` text COLLATE utf8_unicode_ci,
`pid` int(11) DEFAULT NULL,
`amount` int(11) DEFAULT NULL,
`voucher_status` text COLLATE utf8_unicode_ci,
PRIMARY KEY (`vid`)
) ENGINE=MyISAM AUTO_INCREMENT=175 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
//deactivate the plugin
function icafe_vpp_deactivate() {
}
//check Database Structure
function icafe_vpp_update() {
global $wpdb;
$new_version = '1.1';
$current_version = get_option('icafe_vpp_version');
//Check if upgrade is needed
if ($new_version != $current_version) {
update_option("icafe_vpp_version", $new_version);
}
}
//include css and scripts
function icafe_vpp_admin_styles_scripts() {
wp_register_script('icafe_vpp_admin_js', plugins_url( 'js/icafe-vpp-admin.js', __FILE__ ), array(), '', true );
wp_enqueue_script('icafe_vpp_admin_js');
}
//include css and scripts
function icafe_vpp_styles_scripts() {
wp_register_style( 'icafe_vpp_style', plugins_url( 'css/icafe-vpp.css', __FILE__ ), array(), '', 'all' );
wp_enqueue_style( 'icafe_vpp_style' );
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-form');
wp_register_script('icafe_vpp_js_validate', plugins_url( 'js/jquery.validate.min.js', __FILE__ ), array('jquery'), '', true );
wp_register_script('icafe_vpp_js', plugins_url( 'js/icafe-vpp.js', __FILE__ ), array(), '', true );
wp_enqueue_script('icafe_vpp_js_validate');
wp_enqueue_script('icafe_vpp_js');
}
//build the admin menu locations
function icafe_vpp_admin_menu() {
//$icon = plugin_dir_url(__FILE__) . 'project-icon.png';
add_menu_page('iCafe VPP Manager', 'iCafe VPP', 'edit_posts', 'icafe_vpp-admin', 'icafe_vpp_admin');
add_submenu_page('icafe_vpp-admin', 'Program Manager', 'Program Manager', 'edit_posts', 'icafe_vpp-Program-Manager-Admin', 'icafe_vpp_program_manager_admin');
add_submenu_page('icafe_vpp-admin', 'Program Admins', 'Program Admins', 'edit_posts', 'icafe_vpp-Program-Facilitator-Admin', 'icafe_vpp_program_facilitator_admin');
add_submenu_page('icafe_vpp-admin', 'Program Facilitators', 'Program Facilitators', 'edit_posts', 'icafe_vpp-Manage-VPP-Accounts', 'icafe_vpp_manage_vpp_accounts');
add_submenu_page('icafe_vpp-admin', 'Enterprise Owned iTunes Account', 'Enterprise Owned iTunes Account', 'edit_posts', 'icafe_vpp-Manage-Global_iTunes-Accounts', 'icafe_vpp_manage_globle_itunes_accounts');
add_submenu_page('icafe_vpp-admin', 'Self-Serve Config', 'Self-Serve Config', 'edit_posts', 'icafe_vpp-Config', 'icafe_vpp_config');
add_submenu_page('icafe_vpp-admin', 'New Program Config', 'New Program Config', 'edit_posts', 'icafe_vpp_new_program-Config', 'icafe_vpp_new_program_config');
//add_submenu_page('my_projects-admin', 'General Settings', 'Settings', 'manage_options', 'myprojects-settings', 'myprojects_settings');
}
//build the actual page for the VPP tool
function create_VPP_request_page() {
ob_start(); // begin output buffering
add_filter( 'edit_post_link', '__return_false' );
//Who is logged in?
global $user_identity;
global $user_ID;
//DB funtions
global $wpdb;
//set a mode to display
if (!isset($_GET['wpVPP'])) {
$mode = 'welcome_screen';
//Get the URL for the VPP page and store it (update each time this page loades in case it moves
wp_title();
$uri = explode('?',$_SERVER["REQUEST_URI"]);
$base_url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$uri[0] : "http://".$_SERVER['SERVER_NAME'].$uri[0];
update_option('icafe_vpp_plugin_url', $base_url);
} else {
$mode = $_GET['wpVPP'];
$base_url = get_option('icafe_vpp_plugin_url');
}
$vpp_output = '<div id="icafe-vpp">';
//What mode are we in...controls what we display/process
switch ($mode) {
//Start Screen
case 'welcome_screen':
//Get the URL for the VPP page and store it (update each time this page loades in case it moves
wp_title();
update_option('icafe_vpp_plugin_url', $base_url);
//chris make link dynamic
$vpp_output .= '</br></br>
<div id="stylized" class="myform">
<h1>Step 1</h1>
<strong>Order an Apple VPP Voucher</strong>
</br></br>
$100 - MC758LL/A
</br>
$500 - MC759LL/A
</div>
</br></br>
<div id="stylized" class="myform">
<h1>Step 2</h1>
<strong><a href="'.$base_url.'?wpVPP=voucher_request">Redeem your Voucher</a></strong>
</br></br>
Once your voucher arrives, click the link above redeem your voucher. Once your voucher has been processed, you will receive an email confirming that your account is ready to purchase Apps.
</div>
</br></br>
<div id="stylized" class="myform">
<h1>Step 3</h1>
<strong><a href="'.$base_url.'?wpVPP=app_request">Request App Purchases</a></strong>
</br></br>
Once your voucher has been processed, you may request app purchases at any time as long as you have funds remaining. You may always purchase additional Vouchers to add money to your program. Once your app request has been processed, you will receive an email with codes to install the apps.
</div>
</br></br>
';
$global_appleid = stripslashes(get_option('icafe_vpp_global_itunes'));
if ($global_appleid == '') {
$vpp_output .= '
<div id="stylized" class="myform">
<h1>Step 4</h1>
<strong><a href="'.$base_url.'?wpVPP=lookup_pw">Redeem Codes</a></strong>
</br></br>
Once you have your app codes, you may redeem them using your personal iTunes account Click "Redeem Codes" above for instructions </br>
</div>
';
} else {
$vpp_output .= '
<div id="stylized" class="myform">
<h1>Step 4</h1>
<strong><a href="'.$base_url.'?wpVPP=lookup_pw">Redeem Codes</a></strong>
</br></br>
Once you have your app codes, you must install them using the enterprise owned iTunes account. This account changes passwords every '.get_option('global_appleid_change_interval').' hours. Click "Redeem Codes" above to lookup the current credentials. </br></br><strong><div style="color:red; text-align:center">ENTERPRISE PURCHASED APPS MUST BE INSTALLED USING THE ENTERPRISE ITUNES ACCOUNT.</div></strong>
</div>
';
}
break;
//Create screens for entering and processing new vouchers. New program creation also happens here
case 'voucher_request':
//have they filled out the form?
if (isset($_POST['redeem_voucher'])) {//lets process the first screen form
//grab the form values
$program = $_POST['program'];
$code = $_POST['code'];
$amount = str_replace("$","",$_POST['amount']);
//the program exists (or was just created) so let's add the request to the DB and send some emails
//grab the form values
$pid = $_POST['program'];
//insert the data into the DB
if ($pid == 'new') { //is this a new program?
//grab the location because this is a new program
$location = $_POST['location'];
//insert new program into the DB
$table_name = $wpdb->prefix . "icafe_vpp_programs";
$wpdb->insert($table_name, array('program_name' => "$user_identity", 'program_description' => "$location", 'owner' => "$user_ID", 'balance' => "0", 'status' => "pending"));
$pid = $wpdb->insert_id;
//prepare and send email to program manager that a new program is ready to be created
//link to wpVPP plugin admin page for programs
$admin_url = admin_url('admin.php?page=icafe_vpp-Manage-VPP-Accounts');
$program_manager_id = get_option('icafe_vpp_program_manager');
$program_manager_info = get_userdata($program_manager_id);
$program_manager_email = $program_manager_info->user_email;
$proposed_apple_id = stripslashes(get_option('icafe_vpp_new_pf_email'));
$proposed_email = str_replace("*", $pid, $proposed_apple_id);
$body = '
<h2><strong>Apple VPP New Program Request</strong></h2>
<p><strong>Requestor: </strong><font color="568794">'.$program_manager_email.'</font><br>
<strong>New AppleID: </strong><font color="568794">'.$proposed_email.'</font><br>
</p>
<strong>Step 1: <a href="http://edu-vpp.apple.com/asvpp_manager/">Click to Create Program</a></strong><br>
<br>
<p><strong>Step 2: <a href="'.$admin_url.'">Click to Approve Program</a></strong></p>
';
//send the email to program manager to alert to the new request
add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
wp_mail($program_manager_email, 'VPP New Program Request', $body);
}
//grab the information about the program
$table_name = $wpdb->prefix . "icafe_vpp_programs";
$program_data = $wpdb->get_row("SELECT * FROM $table_name WHERE pid = $pid");
//insert voucher into the DB
$table_name = $wpdb->prefix . "icafe_vpp_vouchers";
$wpdb->insert($table_name, array('voucher' => "$code" , 'pid' => "$pid", 'amount' => "$amount", 'voucher_status' => "pending"));
$vid = $wpdb->insert_id;
//send email to facilitator that a voucher request exisits IF the program is approved already
if ($program_data->status == 'approved') {
$owner_info = get_userdata($program_data->owner);
$facilitator_info = get_userdata($program_data->facilitator);
//Email facilitator that voucher nees to be redeemed
$vpp_key = $vid;
$url = $base_url.'?wpVPP=voucher_approve&vpp_key='.$vpp_key.'&approve=FALSE';
$body = '
<h2><strong>Apple VPP Voucher Redemption Request</strong></h2>
<p>'.$owner_info->display_name.' has requested a voucher redemption.</p>
<p><strong>Apple ID: </strong><font color="568794">'.$program_data->apple_id.'</font><br>
<strong>Voucher Number: </strong><font color="568794">'.$code.'</font><br>
<strong>Voucher Amount: </strong><font color="568794">$'.$amount.'</font><br>
</p>
<strong>Step 1: <a href="https://volume.itunes.apple.com/WebObjects/MZFinance.woa/wa/login?cc=us">Click to Redeem VPP Voucher with Apple</a></strong><br>
<br>
<p><strong>Step 2: <a href="'.$url.'">Click to Process Voucher Redemption</a></strong></p>
';
//send the email to program facilitator to alert to the new request
add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
wp_mail($facilitator_info->user_email, 'VPP Voucher Request', $body);
}
//FINAL VOUCHER REDEMPTION SCREEN
//grab the information about the program
$table_name = $wpdb->prefix . "icafe_vpp_programs";
$program_data = $wpdb->get_row("SELECT * FROM $table_name WHERE pid = $pid");
$owner_info = get_userdata($program_data->owner);
$facilitator_info = get_userdata($program_data->facilitator);
//chris make the from email a variable that the admin can override
$vpp_output .= '</br></br>
<div id="stylized" class="myform">
<h1>Apple Volume Purchase Request</h1>
<strong>Your voucher request has been sent to your program administrator '.$facilitator_info->display_name.'</strong>
</br></br>
Once your voucher has been processed you will recieve an email from '.get_bloginfo().' with instructions for requesting app purchases.
</br></br>
<a href="'.$base_url.'?wpVPP=voucher_request">Redeem Another Voucher</a>
</div>
';
} else { //First Voucher Redemption screen
//First Voucher Redemption Screen
//does this person have an existing program created?
$table_name = $wpdb->prefix . "icafe_vpp_programs";
$active_programs = $wpdb->get_var("SELECT COUNT(*) FROM $table_name WHERE owner = $user_ID");
$locations = '';
if ($active_programs == 0) {
$existing_program = '<input name="program" type="hidden" value="new" />';
$program_descriptions = stripslashes(get_option('icafe_vpp_program_descriptions'));
$program_description_lable = stripslashes(get_option('icafe_vpp_program_description_lable'));
$locations = '';
if ($program_description_lable != '') {
$locations = '
<label>'.$program_description_lable.'
</label>
<select name="location">';
$locations .= icafe_vpp_dropdown_from_textarea($program_descriptions);
$locations .= '</select>';
}
} else { //has a program
$pid = $wpdb->get_var("SELECT pid FROM $table_name WHERE owner = $user_ID");
$existing_program = '<input name="program" type="hidden" value="'.$pid.'" />';
}
//$wpdb->show_errors();
//create the voucher redeem form
$vpp_output .= '
</br></br>
<div id="stylized" class="myform">
<form id="redeem" name="redeem" method="post">
<h1>Redeem an Apple Volume Voucher</h1>
<strong><p>Use the form below to redeem your voucher.
</br></strong>
Vouchers must be redeemed before you can request Apps.
</p>
'.$locations.'
<label>Voucher Code
<span class="small">Code under the scratch off on the back of your card</span>
</label>
<label for="code" class="error" generated="true"></label>
<input type="text" name="code" id="code" maxlength="16" />
<label>Voucher Amount
<span class="small"></span>
</label>
<select name="amount">
<option value="100">$100</option>
<option value="500">$500</option>
<option value="1000">$1000</option>
<option value="5000">$5000</option>
</select>
<input name="redeem_voucher" type="hidden" value="true" />
'.$existing_program.'
<button type="submit">Submit</button>
<div class="spacer"></div>
</form>
</div>
';
}
break;
//Screens for program facilitators to confirm that vouchers have been redeemed in the Apple VPP
case 'voucher_approve':
//check that this is a valid vid key
if (isset($_GET['vpp_key'])) {
//chris create these routines
//is this a valid key
$is_valid = TRUE;
//decode the vid
$vid = $_GET['vpp_key'];
}
if ($is_valid) {
//get the voucher data
$table_name = $wpdb->prefix . "icafe_vpp_vouchers";
$voucher_data = $wpdb->get_row("SELECT * FROM $table_name WHERE vid = $vid");
$pid = $voucher_data->pid;
$table_name = $wpdb->prefix . "icafe_vpp_programs";
$program_data = $wpdb->get_row("SELECT * FROM $table_name WHERE pid = $pid");
$owner_info = get_userdata($program_data->owner);
$facilitator_info = get_userdata($program_data->facilitator);
if ($_GET['approve'] == 'TRUE') { //are we approving
//mark voucher approved in the DB
$table_name = $wpdb->prefix . "icafe_vpp_vouchers";
$wpdb->UPDATE($table_name, array('voucher_status' => "approved"), array('vid' => "$vid"));
//update the program balance to include the new voucher
$new_balance = $voucher_data->amount + $program_data->balance;
$table_name = $wpdb->prefix . "icafe_vpp_programs";
$wpdb->UPDATE($table_name, array('balance' => "$new_balance"), array('pid' => "$pid"));
//Email the program owner that they are ready to request apps
$url = $base_url.'?wpVPP=app_request';
$body = '
<h2><strong>Apple VPP Voucher Redemption Approved</strong></h2>
<p>'.$facilitator_info->display_name.' has processed your VPP Voucher.</p>
<p><strong>Program Name: </strong><font color="568794">'.$program_data->program_name.'</font><br>
<strong>Voucher Number: </strong><font color="568794">'.$voucher_data->voucher.'</font><br>
<strong>Voucher Amount: </strong><font color="568794">$'.$voucher_data->amount.'</font><br>
</p>
<h3>You are now ready to request app purchases!</h3><br>
<br>
<p><strong><a href="'.$url.'">Click to Request an App Purchase</a></strong></p>
';
//send the email
add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
//chris switch below
//wp_mail('cnilsson@lcisd.org', 'VPP Voucher Processed', $body);
wp_mail($owner_info->user_email, 'VPP Voucher Request', $body);
//FINAL VOUCHER APPROVAL SCREEN
$vpp_output .= '</br></br>
<div id="stylized" class="myform">
<h1>Apple VPP Voucher Redemption Confirmed</h1>
<strong>Thank you for confirming voucher '.$voucher_data->voucher.'.</strong>
</br></br>
'.$owner_info->display_name.' has been notified via email that they may now request app purchases.
</div>
';
} else { //are we showing the first screen for voucher redemption processing
$url = $base_url.'?wpVPP=voucher_approve&vpp_key='.$vid.'&approve=TRUE';
$vpp_output .= '
<br><br>
<div id="stylized" class="myform">
<h1>Apple VPP Voucher Redemption Confirmation</h1>
<p><strong> Requestor: </strong><font color="568794">'.$owner_info->display_name.'</font><br>
<strong> Apple ID: </strong><font color="568794">'.$program_data->apple_id.'</font><br>
<strong> Approver: </strong><font color="568794">'.$facilitator_info->display_name.'</font><br>
<strong> Voucher: </strong><font color="568794">'.$voucher_data->voucher.'</font><br>
<strong> Amount: </strong><font color="568794">$'.$voucher_data->amount.'</font><br>
</p>
<h3>Have you redeemed this voucher in the Apple VPP Portal?</strong></h3>
<br>
<strong> No: <a href="https://volume.itunes.apple.com/WebObjects/MZFinance.woa/wa/login?cc=us" target="_new">Reedem Voucher Now</a></strong>
<br>
<br><strong> Yes: <a href="'.$url.'">Click to Confirm Voucher Redemption</a></strong>
';
}
} else {//bad vid_key
$program_manager = stripslashes(get_option('icafe_vpp_program_manager'));
$vpp_output .= '</br></br>
<div id="stylized" class="myform">
<h1>Apple VPP Voucher Redemption Error</h1>
<strong>Sorry, this voucher cannot be located.</strong>
</br></br>
Please notify your Apple Program Manager '.$program_manager.'
</div>
';
}
break;
case 'app_request':
if(isset($_POST['request_app'])) {//process the app request
//grab the form values
$pid = $_POST['program'];
$app_name = $_POST['app_name'];
$publisher = $_POST['publisher'];
$quantity = $_POST['quantity'];
$cost_one = str_replace("$","",$_POST['cost_one']);
$cost_volume = str_replace("$","",$_POST['cost_volume']);
$for_user = $_POST['for_user'];
$app_url = $_POST['url'];
//insert the data into the DB
$table_name = $wpdb->prefix . "icafe_vpp_apps";
$wpdb->insert($table_name, array('pid' => "$pid" , 'app' => "$app_name", 'publisher' => "$publisher", 'quantity' => "$quantity", 'for_user' => "$for_user", 'url' => "$app_url", 'app_status' => "pending"));
$aid = $wpdb->insert_id;
//grab the information about the program
$table_name = $wpdb->prefix . "icafe_vpp_programs";
$program_data = $wpdb->get_row("SELECT * FROM $table_name WHERE pid = $pid");
$owner_info = get_userdata($program_data->owner);
$facilitator_info = get_userdata($program_data->facilitator);
//email program facilitator that there is a new app request
$app_key = $aid;
$url = $base_url.'?wpVPP=app_approve&app_key='.$app_key.'&approve=FALSE';
$body = '
<h2><strong>Apple VPP App Purchase Request</strong></h2>
<p>'.$owner_info->display_name.' has requested an app purchase.</p>
<p><strong>Apple ID: </strong><font color="568794">'.$program_data->apple_id.'</font><br>
<strong>App: </strong><font color="568794">'.$app_name.'</font><br>
<strong>Publisher: </strong><font color="568794">'.$publisher.'</font><br>
<strong>Price each 1-19 copies: </strong><font color="568794">$'.$cost_one.'</font><br>
<strong>Price each 20+ copies: </strong><font color="568794">$'.$cost_volume.'</font><br>
<strong>Quantity Requested: </strong><font color="568794">'.$quantity.'</font><br>
<strong>Requested For: </strong><font color="568794">'.$for_user.'</font><br>
</p>
<strong>Step 1: <a href="'.$app_url.'">Click to Purchase App</a></strong><br>
<br>
<p><strong>Step 2: <a href="'.$url.'">Click to Process App Purchase</a></strong></p>
';
//send the email to program manager to alert to the new request
add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
//chris switch below
//wp_mail('cnilsson@lcisd.org', 'VPP App Request', $body);
wp_mail($facilitator_info->user_email, 'VPP App Request', $body);
//FINAL VOUCHER APPROVAL SCREEN
$vpp_output .= '</br></br>
<div id="stylized" class="myform">
<h1>Apple VPP App Request Pending</h1>
<p><strong>Your app request has been sent to your program administrator, '.$facilitator_info->display_name.'</strong>
</br></br>
Once your purchase has been processed you will receive an email with instructions for installing your apps.</p>
<strong>App: </strong><font color="568794">'.$app_name.'</font><br>
<strong>Publisher: </strong><font color="568794">'.$publisher.'</font><br>
<strong>Price each 1-19 copies: </strong><font color="568794">$'.$cost_one.'</font><br>
<strong>Price each 20+ copies: </strong><font color="568794">$'.$cost_volume.'</font><br>
<strong>Quantity Requested: </strong><font color="568794">'.$quantity.'</font><br>
<strong>Requested For: </strong><font color="568794">'.$for_user.'</font><br><br>
<a href="'.$base_url.'?wpVPP=app_request">Request Another App</a>
</div>
';
} else {//show the app request form
$table_name = $wpdb->prefix . "icafe_vpp_programs";
$active_program = $wpdb->get_row("SELECT pid, owner, balance FROM $table_name WHERE owner = $user_ID AND status = 'approved'");
$owner_info = get_userdata($active_program->owner);
$has_program = false;
//make sure this program has a processed voucher associated with it
$table_name = $wpdb->prefix . "icafe_vpp_vouchers";
$pid = $active_program->pid;
$active_vouchers = $wpdb->get_var("SELECT COUNT(*) FROM $table_name WHERE pid = $pid AND voucher_status = 'approved'");
if ($active_vouchers != 0) {
$has_program = true;
}
//only show the page if they have a program and voucher
if ($has_program) {
$vpp_output .= '
</br></br>
<div id="stylized" class="myform">
<form id="app_request" name="app_request" method="post">
<h1>Request an App Purchase</h1>
<p><strong>Use the form below to request an app purchase using your Volume Voucher.</strong>
</br><br/>Vouchers must be <a href="'.$url.'?wpVPP=voucher_request">redeemed</a> before you can request Apps.
</br></br>
<span style="color: #8B0000"> You must first locate your app in the </span><a href="https://volume.itunes.apple.com/us/store/" target="_new">Apple Education Store.</a></p>
<h1>'.$owner_info->display_name.', you have approximately $'.$active_program->balance.' remaining on your vouchers.</h1>
<label>App Name
<span class="small"> </span>
</label>
<label for="app_name" class="error" generated="true"></label>
<input type="text" name="app_name" id="app_name"/>
<label>Publisher
<span class="small">Who is this app "By"?</span>
</label>
<label for="publisher" class="error" generated="true"></label>
<input type="text" name="publisher" id="publisher"/>
<label>Quantitiy
<span class="small">How many copies do you need (one per device)</span>
</label>
<label for="quantity" class="error" generated="true"></label>
<input type="text" name="quantity" id="quantity"/>
<label>Price each
<span class="small">1-19 copies</span>
</label>
<label for="cost_one" class="error" generated="true"></label>
<input type="text" name="cost_one" id="cost_one"/>
<label>Price each
<span class="small">20+ copies</span>
</label>
<label for="cost_volume" class="error" generated="true"></label>
<input type="text" name="cost_volume" id="cost_volume"/>
<label>Purchasing For
<span class="small">List the names of other users receiving this app<br /><strong>Seperate with commas</strong></span>
</label>
<label for="for_user" class="error" generated="true"></label>
<textarea name="for_user" id="for_user" cols="2" rows="2"></textarea>
<label>App URL
<span class="small">Copy the URL from the Apple Education Store (link above)</span>
</label>
<label for="url" class="error" generated="true"></label>
<input type="text" name="url" id="url"/>
<input name="program" type="hidden" value="'.$pid.'" />
<input name="request_app" type="hidden" value="true" />
<button type="submit">Request</button>
<div class="spacer"></div>
</form>
</div>
';
}else{ //no program or no vouchers processed
$vpp_output .= '</br></br>
<div id="stylized" class="myform">
<h1>Apple Volume Purchase Request</h1>
<strong>You must first redeem an Apple Volume Voucher.</strong>
</br></br>
If you have already entered a voucher you must wait until your program administrator processes your request. You will receive an email when the system is ready for you to request apps.
</div>
';
}
}
break;
case 'app_approve':
//check that this is a valid aid key
if (isset($_GET['app_key'])) {
//chris create these routines
//is this a valid key
$is_valid = TRUE;
//decode the aid
$aid = $_GET['app_key'];
$app_key = $_GET['app_key'];
}
if ($is_valid) {
//get the app data
$table_name = $wpdb->prefix . "icafe_vpp_apps";
$app_data = $wpdb->get_row("SELECT * FROM $table_name WHERE aid = $aid");
$pid = $app_data->pid;
$table_name = $wpdb->prefix . "icafe_vpp_programs";
$program_data = $wpdb->get_row("SELECT * FROM $table_name WHERE pid = $pid");
$owner_info = get_userdata($program_data->owner);
$facilitator_info = get_userdata($program_data->facilitator);
//get the data from the form
$cost = str_replace("$","",$_POST['cost']);
$balance = str_replace("$","",$_POST['balance']);
//$cost_one = $_POST['cost_one'];
if (($_GET['approve'] == 'TRUE') && ($_FILES['codes']['size'] > 0)) { //are we approving and is there a file
//read in the file data
$fileName = $_FILES['codes']['name'];
$tmpName = $_FILES['codes']['tmp_name'];
$fileSize = $_FILES['codes']['size'];
$fileType = $_FILES['codes']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = ($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = ($fileName);
}
//$wpdb->show_errors();
//mark app purchased in the DB and store the code excel file
$table_name = $wpdb->prefix . "icafe_vpp_apps";
$wpdb->UPDATE($table_name, array('cost' => "$cost", 'codes' => "$content", 'size' => "$fileSize", 'type' => "$fileType", 'name' => "$fileName", 'codes' => "$content", 'app_status' => "purchased"), array('aid' => "$aid"));
$table_name = $wpdb->prefix . "icafe_vpp_programs";
$wpdb->UPDATE($table_name, array('balance' => "$balance"), array('pid' => "$pid"));
$url = $base_url.'?wpVPP=app_approve&app_key='.$app_key.'&codes=TRUE';
//Email the program owner their app information
$body = '
<h2><strong>Apple VPP App Purchase</strong></h2>
<p>'.$facilitator_info->display_name.' has purchased your requested app.</p>
<p><strong>Program Name: </strong><font color="568794">'.$program_data->program_name.'</font><br>
<strong>App Purchased: </strong><a href="'.$app_data->url.'"><font color="568794">'.$app_data->app.'</font></a><br>
<strong>Quantity: </strong><font color="568794">'.$app_data->quantity.'</font><br>
<strong>Cost: </strong><font color="568794">$'.$cost.'</font><br>
<strong>Remaining Program Balance: </strong><font color="568794">$'.$balance.'</font><br>
</p>
<h3>You are now ready to install your apps!</h3><br>
<br>
</p>
<strong>Step 1: <a href="'.$url.'">Download your app codes</a></strong><br>
<br>
<strong>Step 2: If you purchased this app for other users, distribute one code per installation to users. Include the link below</strong><br>
<br> '.$app_data->for_user.'
<p><strong>Step 3: <a href="'.$base_url.'?wpVPP=lookup_pw">Instructions for installing your app</a></strong></p>
';
//send the email
add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
//chris switch below
//wp_mail('cnilsson@lcisd.org', 'VPP App Purchased', $body);
wp_mail($owner_info->user_email, 'VPP App Request', $body);
$vpp_output .= '</br></br>
<div id="stylized" class="myform">
<h1>Apple VPP App Purchase Confirmedt</h1>
<strong>Thank you for purchasing '.$app_data->app.'.</strong>
</br></br>
'.$owner_info->display_name.' has been emailed the app installation redemption codes and instructions on how to install.
</div>
';
} else {//are we showing the first screen for app purchase processing
$url = $base_url.'?wpVPP=app_approve&app_key='.$aid.'&approve=TRUE';
//Approval form for updating program balance and uploading codes
$vpp_output .= '
</br></br>
<div id="stylized" class="myform">
<form id="app_approve" name="app_approve" method="post" action="'.$url.'" enctype="multipart/form-data">
<h3><strong>Apple VPP App Purchase Confirmation</strong></h3>
<p><strong> Requestor: </strong><font color="568794">'.$owner_info->display_name.'</font><br>
<strong> Apple ID: </strong><font color="568794">'.$program_data->apple_id.'</font><br>
<strong> Purchaser: </strong><font color="568794">'.$facilitator_info->display_name.'</font><br>
<strong> App: </strong><font color="568794">'.$app_data->app.'</font><br>
<strong> Quantity: </strong><font color="568794">'.$app_data->quantity.'</font><br>
<strong> Requested For: </strong><font color="568794">'.$app_data->for_user.'</font><br>
</p>
<h3>Have you purchased this app in the Apple VPP Education Store?</strong></h3>
<p><strong>No: <a href="'.$app_data->url.'" target="_new">Purchase App Now</a></strong>
<br>
<br><strong>Yes: Proceed below</strong></p>
<label>Total Cost
<span class="small"> </span>
</label>
<label for="cost" class="error" generated="true"></label>
<input type="text" name="cost" id="cost"/>
<label>Remaining Balance
<span class="small">Program balance after the purchase</span>
</label>
<label for="balance" class="error" generated="true"></label>
<input type="text" name="balance" id="balance"/>
<label>Redemption Codes
<span class="small">Upload the Excel Spreadsheet you recieved from the Apple Store</span>
</label>
<label for="codes" class="error" generated="true"></label></br>
<input name="codes" type="file" />
<button type="submit">Submit</button>
</form>
</div>
';
}
} else {//bad aid_key
//chris style this
$vpp_output .= 'sorry that app request cannot be located';
}
break;
//generate and store new itunes passwords used to power auto password change feature
case 'generate_new_pw':
//grab the global account credentials
$global_appleid = stripslashes(get_option('icafe_vpp_global_itunes'));
if ($_GET['changekey'] == '98jh298yan39Ojiyuia879UHYTHJ32') {
//check to see if new password is being set
if ($_GET['update_pw'] == 'true') {
$new = $_GET['new'];
update_option('icafe_vpp_current_appleid_pw', $new);
}
//check to see if an account has been provisioned
if ($_GET['provision'] == 'true') {
$prov_pid = $_GET['pid'];
$prov_apple_id = $_GET['appleid'];
$prov_pw = $_GET['pf_pw'];
//mark app purchased in the DB and store the code excel file
$table_name = $wpdb->prefix . "icafe_vpp_programs";
$wpdb->UPDATE($table_name, array('apple_id' => "$prov_apple_id", 'password' => "$prov_pw", 'status' => "provisioned"), array('pid' => "$prov_pid"));
}
//grab the current settings
$global_appleid = stripslashes(get_option('icafe_vpp_global_itunes'));
$global_appleid_current_pw = stripslashes(get_option('icafe_vpp_current_appleid_pw'));
$global_appleid_sec1 = get_option('global_appleid_sec1');
$global_appleid_sec2 = get_option('global_appleid_sec2');
$global_appleid_sec3 = get_option('global_appleid_sec3');
$global_appleid_sec1_answer = stripslashes(get_option('global_appleid_sec1_answer'));
$global_appleid_sec2_answer = stripslashes(get_option('global_appleid_sec2_answer'));
$global_appleid_sec3_answer = stripslashes(get_option('global_appleid_sec3_answer'));
$email = stripslashes(get_option('icafe_vpp_new_pf_email'));
$universal_pw = stripslashes(get_option('icafe_vpp_new_pf_universal_pw'));
$reset_month = get_option('icafe_vpp_new_pf_month');
$reset_day = get_option('icafe_vpp_new_pf_day');
$reset_year = get_option('icafe_vpp_new_pf_year');
$reset_question = stripslashes(get_option('icafe_vpp_new_pf_sec_q'));
$reset_answer = stripslashes(get_option('icafe_vpp_new_pf_sec_a'));
$program_manager_itunes_account = stripslashes(get_option('icafe_vpp_program_manager_itunes_account'));
$program_manager_itunes_pw = stripslashes(get_option('icafe_vpp_program_manager_itunes_pw'));
$capital = range ('A','Z');
$small = range ('a','z');
$number = range ('0','9');
$special = array ("#","$","@");
$new_pw = $small[array_rand($small)] .
$number[array_rand($number)] .
$number[array_rand($number)] .
$number[array_rand($number)] .
$number[array_rand($number)] .
$number[array_rand($number)] .
$number[array_rand($number)] .
$capital[array_rand($capital)];
$update_pw_url = $base_url.'?wpVPP=generate_new_pw&update_pw=true&changekey=98jh298yan39Ojiyuia879UHYTHJ32&new='.$new_pw;
echo '
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>'.base64_encode($global_appleid).'</td>
</tr>
<tr>
<td>'.base64_encode($global_appleid_current_pw).'</td>
</tr>
<tr>
<td>'.base64_encode($new_pw).'</td>
</tr>
<tr>
<td>'.base64_encode($global_appleid_sec1).'</td>
</tr>
<tr>
<td>'.base64_encode($global_appleid_sec1_answer).'</td>
</tr>
<tr>
<td>'.base64_encode($global_appleid_sec2).'</td>
</tr>
<tr>
<td>'.base64_encode($global_appleid_sec2_answer).'</td>
</tr>
<tr>
<td>'.base64_encode($global_appleid_sec3).'</td>
</tr>
<tr>
<td>'.base64_encode($global_appleid_sec3_answer).'</td>
</tr>
</table>';
echo '