license.php
155 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments - Libraries |
| 4 | * @subpackage update |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * Class used to handle the software license. |
| 16 | * |
| 17 | * @since 1.0 |
| 18 | */ |
| 19 | class VikAppointmentsLicense |
| 20 | { |
| 21 | /** |
| 22 | * Gets the current License Key. |
| 23 | * |
| 24 | * @return string |
| 25 | */ |
| 26 | public static function getKey() |
| 27 | { |
| 28 | return trim(get_option('vikappointments_license_key', '')); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Updates the current License Key. |
| 33 | * |
| 34 | * @param string $key |
| 35 | * |
| 36 | * @return void |
| 37 | */ |
| 38 | public static function setKey($key) |
| 39 | { |
| 40 | /** |
| 41 | * In case of multi-site, update the option on all the network sites. |
| 42 | * |
| 43 | * @since 1.1.9 |
| 44 | */ |
| 45 | JFactory::getApplication()->set('vikappointments_license_key', (string) $key, $network = true); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Gets the current License Expiration date (Y-m-d H:i:s). |
| 50 | * |
| 51 | * @return string |
| 52 | */ |
| 53 | public static function getExpirationDate() |
| 54 | { |
| 55 | return trim(get_option('vikappointments_license_expdate', '')); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Updates the current License Expiration date. |
| 60 | * |
| 61 | * @param string $time |
| 62 | * |
| 63 | * @return void |
| 64 | */ |
| 65 | public static function setExpirationDate($time) |
| 66 | { |
| 67 | // format time |
| 68 | $time = JDate::getInstance($time)->toSql(); |
| 69 | |
| 70 | /** |
| 71 | * In case of multi-site, update the option on all the network sites. |
| 72 | * |
| 73 | * @since 1.1.9 |
| 74 | */ |
| 75 | JFactory::getApplication()->set('vikappointments_license_expdate', $time, $network = true); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Checks whether the software version is Pro. |
| 80 | * |
| 81 | * @return boolean |
| 82 | */ |
| 83 | public static function isPro() |
| 84 | { |
| 85 | $key = self::getKey(); |
| 86 | $date = self::getExpirationDate(); |
| 87 | |
| 88 | return $key && $date && $date > JDate::getInstance()->toSql(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Checks whether the License Key is expired. |
| 93 | * |
| 94 | * @return boolean |
| 95 | */ |
| 96 | public static function isExpired() |
| 97 | { |
| 98 | return !self::isPro(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Gets the current License Hash. |
| 103 | * |
| 104 | * @return string |
| 105 | */ |
| 106 | public static function getHash() |
| 107 | { |
| 108 | $hash = trim(get_option('vikappointments_license_hash', '')); |
| 109 | |
| 110 | if (empty($hash)) |
| 111 | { |
| 112 | $hash = self::setHash(); |
| 113 | } |
| 114 | |
| 115 | return $hash; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Sets and returns the License Hash. |
| 120 | * |
| 121 | * @return string |
| 122 | */ |
| 123 | public static function setHash() |
| 124 | { |
| 125 | $hash = md5(JUri::root() . uniqid()); |
| 126 | update_option('vikappointments_license_hash', $hash); |
| 127 | |
| 128 | return $hash; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Registers some options upon installation of the plugin. |
| 133 | * |
| 134 | * @return void |
| 135 | */ |
| 136 | public static function install() |
| 137 | { |
| 138 | update_option('vikappointments_license_key', ''); |
| 139 | update_option('vikappointments_license_expdate', null); |
| 140 | update_option('vikappointments_license_hash', ''); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Deletes all the options upon uninstallation of the plugin. |
| 145 | * |
| 146 | * @return void |
| 147 | */ |
| 148 | public static function uninstall() |
| 149 | { |
| 150 | delete_option('vikappointments_license_key'); |
| 151 | delete_option('vikappointments_license_expdate'); |
| 152 | delete_option('vikappointments_license_hash'); |
| 153 | } |
| 154 | } |
| 155 |