share
9 years ago
.htaccess
11 years ago
anti_malware.php
5 years ago
class-coupon.php
7 months ago
class-email-sodium.php
1 month ago
class-firewall-log.php
1 month ago
class-helpers.php
9 months ago
class-import-export.php
5 months ago
class-ip.php
5 months ago
class-nfw-database.php
7 months ago
class-nfw-session.php
1 month ago
class-php-session.php
1 year ago
class-plugin-upgrade.php
1 month ago
class_mail.php
1 month ago
event_updates.php
11 months ago
firewall.php
5 months ago
fw_centlog.php
1 month ago
fw_fileguard.php
5 months ago
fw_livelog.php
1 year ago
help.php
1 month ago
helpers.php
1 month ago
i18n-extra.php
1 month ago
i18n.php
1 year ago
index.html
13 years ago
init_update.php
2 years ago
install.php
1 year ago
install_default.php
7 months ago
loader.php
7 months ago
mail_template_firewall.php
1 year ago
mail_template_plugin.php
2 months ago
scheduled_tasks.php
3 years ago
settings_dashboard.php
2 months ago
settings_dashboard_about.php
1 month ago
settings_dashboard_statistics.php
2 months ago
settings_event_notifications.php
2 months ago
settings_events.php
2 months ago
settings_firewall_options.php
2 months ago
settings_firewall_policies.php
1 month ago
settings_login_protection.php
2 months ago
settings_logs.php
2 months ago
settings_logs_firewall_log.php
1 month ago
settings_logs_live_log.php
2 months ago
settings_monitoring.php
1 month ago
settings_monitoring_file_check.php
2 months ago
settings_monitoring_file_guard.php
2 months ago
settings_network.php
2 months ago
settings_security_rules.php
2 months ago
settings_security_rules_editor.php
2 months ago
settings_security_rules_update.php
1 month ago
sign.pub
7 years ago
thickbox.php
4 years ago
widget.php
3 years ago
wpplus.php
5 months ago
class-coupon.php
149 lines
| 1 | <?php |
| 2 | /* |
| 3 | +=====================================================================+ |
| 4 | | _ _ _ _ _____ _ _ _ | |
| 5 | | | \ | (_)_ __ (_) __ _| ___(_)_ __ _____ ____ _| | | | |
| 6 | | | \| | | '_ \ | |/ _` | |_ | | '__/ _ \ \ /\ / / _` | | | | |
| 7 | | | |\ | | | | || | (_| | _| | | | | __/\ V V / (_| | | | | |
| 8 | | |_| \_|_|_| |_|/ |\__,_|_| |_|_| \___| \_/\_/ \__,_|_|_| | |
| 9 | | |__/ | |
| 10 | | (c) NinTechNet Limited ~ https://nintechnet.com/ | |
| 11 | +=====================================================================+ |
| 12 | */ |
| 13 | |
| 14 | if ( class_exists('NinjaFirewall_coupon') ) { |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | class NinjaFirewall_coupon { |
| 19 | |
| 20 | private $options = []; |
| 21 | private $frequency = 86400; /** Daily check only */ |
| 22 | private $url = 'https://api.nintechnet.com/coupons'; |
| 23 | private $cache = NFW_LOG_DIR . '/nfwlog/cache'; |
| 24 | private $file = 'coupon.png'; |
| 25 | |
| 26 | /** |
| 27 | * Retrieve NinjaFirewall's options. |
| 28 | */ |
| 29 | function __construct() { |
| 30 | |
| 31 | $this->options = nfw_get_option('nfw_options'); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Display any available coupon. |
| 37 | */ |
| 38 | function show() { |
| 39 | |
| 40 | if ( empty( $this->options['coupon']['date'] ) ) { |
| 41 | return ['error' => 'no coupon']; |
| 42 | } |
| 43 | /** |
| 44 | * Make sure it didn't expire yet. |
| 45 | */ |
| 46 | $today = date('Y-m-d'); |
| 47 | if ( $today > $this->options['coupon']['date'] ) { |
| 48 | return ['error' => 'expired coupon']; |
| 49 | } |
| 50 | |
| 51 | if (! is_file( "{$this->cache}/{$this->file}" ) ) { |
| 52 | return ['error' => 'missing file']; |
| 53 | } |
| 54 | $data = file_get_contents( "{$this->cache}/{$this->file}" ); |
| 55 | |
| 56 | $until = 'This offer is valid until '. |
| 57 | date('F d', strtotime( $this->options['coupon']['date'] ) ); |
| 58 | |
| 59 | if (! empty( $this->options['coupon']['url'] ) ) { |
| 60 | $url = $this->options['coupon']['url']; |
| 61 | } else { |
| 62 | $url = 'https://nintechnet.com/'; |
| 63 | } |
| 64 | |
| 65 | echo '<a href="'. esc_url( $url ) .'" alt="Go Pro! Limited time offer" '. |
| 66 | 'title="Go Pro! Limited time offer" target="_blank" rel="noreferrer noopener">'. |
| 67 | '<img style="max-width:250px" src="data:image/png;base64, '. esc_attr( $data ) .'" />'. |
| 68 | '<br />'. esc_html( $until ) .'</a>'; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Remote connection (WP-CRON). |
| 74 | */ |
| 75 | function run() { |
| 76 | /** |
| 77 | * We run on the main site only. |
| 78 | */ |
| 79 | if (! is_main_site() ) { |
| 80 | return ['error' => 'child site']; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * It should not run more than once daily. |
| 85 | */ |
| 86 | if (! empty( $this->options['cronjobs']['coupon']['last'] ) && |
| 87 | $this->options['cronjobs']['coupon']['last'] + $this->frequency > time() ) { |
| 88 | |
| 89 | return ['error' => 'frequency']; |
| 90 | } |
| 91 | /** |
| 92 | * Update last checked time. |
| 93 | */ |
| 94 | $this->options['cronjobs']['coupon']['last'] = time(); |
| 95 | nfw_update_option('nfw_options', $this->options ); |
| 96 | /** |
| 97 | * Connect. |
| 98 | */ |
| 99 | global $wp_version; |
| 100 | $res = wp_remote_get( |
| 101 | $this->url, |
| 102 | [ |
| 103 | 'timeout' => 5, |
| 104 | 'httpversion' => '1.1' , |
| 105 | 'user-agent' => 'Mozilla/5.0 (compatible; NinjaFirewall/'. |
| 106 | NFW_ENGINE_VERSION ."; WordPress/$wp_version)", |
| 107 | 'sslverify' => true, |
| 108 | 'headers' => [ |
| 109 | 'ntn-plugin' => 'nf', |
| 110 | 'ntn-cache' => md5( network_site_url() ) |
| 111 | ] |
| 112 | ] |
| 113 | ); |
| 114 | if (! is_wp_error( $res ) && $res['response']['code'] == 200 ) { |
| 115 | $coupon = json_decode( $res['body'], true ); |
| 116 | |
| 117 | if ( empty( $coupon['nf']['img'] ) ) { |
| 118 | /** |
| 119 | * Clear the old coupon. |
| 120 | */ |
| 121 | if (! empty( $this->options['coupon'] ) ) { |
| 122 | unset( $this->options['coupon'] ); |
| 123 | nfw_update_option('nfw_options', $this->options ); |
| 124 | } |
| 125 | return ['error' => 'no coupon']; |
| 126 | } |
| 127 | /** |
| 128 | * Save the image. |
| 129 | */ |
| 130 | @ file_put_contents( "{$this->cache}/{$this->file}", $coupon['nf']['img'] ); |
| 131 | $coupon['nf']['img'] = $this->file; |
| 132 | |
| 133 | if ( empty( $this->options['coupon'] ) || $this->options['coupon'] != $coupon['nf'] ) { |
| 134 | /** |
| 135 | * Save/update the coupon. |
| 136 | */ |
| 137 | $this->options['coupon'] = $coupon['nf']; |
| 138 | nfw_update_option('nfw_options', $this->options ); |
| 139 | } |
| 140 | return $coupon['nf']; |
| 141 | } |
| 142 | return ['error' => 'HTTP error']; |
| 143 | } |
| 144 | |
| 145 | } |
| 146 | |
| 147 | // ===================================================================== |
| 148 | // EOF |
| 149 |