.
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # WHMCS-Product-Limiter
2 |
3 | WHMCS Product Limiter is a free addon module that gives you the ability to limit purchasing a product to specified times (adjustable).
4 |
5 | When activated, once the client will try to purchase the same product again – he will receive an error message (customizable)
6 |
7 | That can be handy when giving free trial versions.
8 | This module will help you to stop free trial versions abuse, as the client will not be able to download more then once product.
9 |
10 | # Installation
11 |
12 | Upload the folder "limit_purchase" (as is) to your WHMCS installtion.
13 | After uploaded, the full path to the module should be: “modules/addons/limit_purchase“.
14 |
15 | Login to your WHMCS as admin user, and navigate to “Setup” -> “Addon modules“.
16 |
17 | in the “WHMCS Product limter” module, and activate it.
18 |
19 | Now go back to “WHMCS Product limiter”, click “Configure” to add Access Control to admin groups.
20 | Save your changes and your module is all set.
21 |
22 | You will be able to access it throught the “Addons” top menu.
23 |
--------------------------------------------------------------------------------
/limit_purchase/functions.php:
--------------------------------------------------------------------------------
1 | loadConfig();
13 | }
14 |
15 | function loadConfig()
16 | {
17 | $this->config = array();
18 |
19 | $sql = "SELECT *
20 | FROM mod_limit_purchase_config";
21 | $result = mysql_query($sql);
22 |
23 | while($config_details = mysql_fetch_assoc($result))
24 | {
25 | $this->config[$config_details['name']] = $config_details['value'];
26 | }
27 | mysql_free_result($result);
28 | }
29 |
30 | function setConfig($name, $value)
31 | {
32 | if(isset($this->config[$name]))
33 | {
34 | $sql = "UPDATE mod_limit_purchase_config
35 | SET value = '" . mysql_escape_string($value) . "'
36 | WHERE name = '" . mysql_escape_string($name) . "'";
37 | $result = mysql_query($sql);
38 | }
39 | else
40 | {
41 | $sql = "INSERT INTO mod_limit_purchase_config (`name`,`value`) VALUES
42 | ('" . mysql_escape_string($name) . "','" . mysql_escape_string($value) . "')";
43 | $result = mysql_query($sql);
44 | }
45 |
46 | $this->config[$name] = $value;
47 | }
48 |
49 | function getLimitedProducts()
50 | {
51 | $output = array();
52 |
53 | $sql = "SELECT l.*
54 | FROM mod_limit_purchase as l
55 | INNER JOIN tblproducts as p
56 | ON p.id = l.product_id
57 | WHERE l.active = 1";
58 | $result = mysql_query($sql);
59 |
60 | while($limits = mysql_fetch_assoc($result))
61 | {
62 | $output[$limits['product_id']] = array('limit' => $limits['limit'], 'error' => $limits['error']);
63 | }
64 | mysql_free_result($result);
65 |
66 | return $output;
67 | }
68 | }
69 |
70 | ?>
--------------------------------------------------------------------------------
/limit_purchase/hooks.php:
--------------------------------------------------------------------------------
1 | getLimitedProducts();
15 | $user_id = intval($_SESSION['uid']);
16 |
17 | if(sizeof($_SESSION['cart']['products']))
18 | {
19 | $counter = $delete = array();
20 |
21 | foreach($_SESSION['cart']['products'] as $i => $product_details)
22 | {
23 | if(in_array($product_details['pid'], array_keys($pids)))
24 | {
25 | if(!isset($counter[$product_details['pid']]))
26 | {
27 | $counter[$product_details['pid']] = 0;
28 |
29 | if($user_id)
30 | {
31 | $sql = "SELECT COUNT(id) as total_products
32 | FROM tblhosting
33 | WHERE userid = '{$user_id}'
34 | AND packageid = '{$product_details['pid']}'";
35 | $result = mysql_query($sql);
36 | $product = mysql_fetch_assoc($result);
37 |
38 | $counter[$product_details['pid']] = intval($product['total_products']);
39 | }
40 | }
41 |
42 | if($pids[$product_details['pid']]['limit'] <= intval($counter[$product_details['pid']]))
43 | {
44 | if(!isset($delete[$product_details['pid']]))
45 | {
46 | $sql = "SELECT name
47 | FROM tblproducts
48 | WHERE id = '{$product_details['pid']}'";
49 | $result = mysql_query($sql);
50 | $delete[$product_details['pid']] = mysql_fetch_assoc($result);
51 | }
52 |
53 | // if you want to automatically delete the unwanted products from the cart, remark the line below
54 | //unset($_SESSION['cart']['products'][$i]);
55 | }
56 |
57 | $counter[$product_details['pid']]++;
58 | }
59 | }
60 |
61 | foreach($delete as $product_id => $product_details)
62 | {
63 | $errors[] = str_replace('{PNAME}', $product_details['name'], $pids[$product_id]['error']);
64 | }
65 | }
66 |
67 | return $errors;
68 | }
69 |
70 | function limit_purchase_delete($vars)
71 | {
72 | $sql = "DELETE
73 | FROM mod_limit_purchase
74 | WHERE product_id = '{$vars['pid']}'";
75 | mysql_query($sql);
76 | }
77 |
78 | add_hook('ShoppingCartValidateCheckout', 0, 'limit_purchase');
79 | add_hook('ProductDelete', 0, 'limit_purchase_delete');
80 |
81 | ?>
--------------------------------------------------------------------------------
/limit_purchase/lang/english.php:
--------------------------------------------------------------------------------
1 | Please go to our product page for more details";
31 |
32 | ?>
--------------------------------------------------------------------------------
/limit_purchase/limit_purchase.php:
--------------------------------------------------------------------------------
1 | "Product Limiter",
10 | "description" => "This addon allows you to limit the purchase of an products/services for each client",
11 | "version" => "1.0.5",
12 | "author" => "Idan Ben-Ezra",
13 | "language" => "english",
14 | );
15 | }
16 |
17 | function limit_purchase_activate()
18 | {
19 | $sql = "CREATE TABLE IF NOT EXISTS `mod_limit_purchase_config` (
20 | `name` varchar(255) NOT NULL,
21 | `value` text NOT NULL,
22 | PRIMARY KEY (`name`)
23 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
24 | $result = mysql_query($sql);
25 |
26 | if($result)
27 | {
28 | $sql = "INSERT INTO mod_limit_purchase_config (`name`,`value`) VALUES
29 | ('localkey', ''),
30 | ('version_check', '0'),
31 | ('version_new', '')";
32 | $result = mysql_query($sql);
33 | }
34 | else
35 | {
36 | $error[] = "Can't create the table `mod_limit_purchase_config`. SQL Error: " . mysql_error();
37 | }
38 |
39 | $sql = "CREATE TABLE IF NOT EXISTS `mod_limit_purchase` (
40 | `id` int(11) NOT NULL AUTO_INCREMENT,
41 | `product_id` int(11) NOT NULL DEFAULT '0',
42 | `limit` int(11) NOT NULL DEFAULT '0',
43 | `error` varchar(255) NOT NULL,
44 | `active` tinyint(1) NOT NULL DEFAULT '0',
45 | PRIMARY KEY (`id`)
46 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
47 | $result = mysql_query($sql);
48 |
49 | if(!$result) $error[] = "Can't create the table `mod_limit_purchase`. SQL Error: " . mysql_error();
50 |
51 | if(sizeof($error))
52 | {
53 | limit_purchase_deactivate();
54 | }
55 |
56 | return array(
57 | 'status' => sizeof($error) ? 'error' : 'success',
58 | 'description' => sizeof($error) ? implode(" -> ", $error) : '',
59 | );
60 | }
61 |
62 | function limit_purchase_deactivate()
63 | {
64 | $sql = "DROP TABLE IF EXISTS `mod_limit_purchase`";
65 | $result = mysql_query($sql);
66 |
67 | if(!$result) $error[] = "Can't drop the table `mod_limit_purchase`. SQL Error: " . mysql_error();
68 |
69 | $sql = "DROP TABLE IF EXISTS `mod_limit_purchase_config`";
70 | $result = mysql_query($sql);
71 |
72 | if(!$result) $error[] = "Can't drop the table `mod_limit_purchase_config`. SQL Error: " . mysql_error();
73 |
74 | return array(
75 | 'status' => sizeof($error) ? 'error' : 'success',
76 | 'description' => sizeof($error) ? implode(" -> ", $error) : '',
77 | );
78 | }
79 |
80 | function limit_purchase_upgrade($vars)
81 | {
82 | if(version_compare($vars['version'], '1.0.1', '<'))
83 | {
84 | $sql = "CREATE TABLE IF NOT EXISTS `mod_limit_purchase_config` (
85 | `name` varchar(255) NOT NULL,
86 | `value` text NOT NULL,
87 | PRIMARY KEY (`name`)
88 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
89 | $result = mysql_query($sql);
90 |
91 | if($result)
92 | {
93 | $sql = "INSERT INTO mod_limit_purchase_config (`name`,`value`) VALUES
94 | ('localkey', ''),
95 | ('version_check', '0'),
96 | ('version_new', '')";
97 | $result = mysql_query($sql);
98 | }
99 | }
100 | }
101 |
102 | function limit_purchase_output($vars)
103 | {
104 | $modulelink = $vars['modulelink'];
105 | $version = $vars['version'];
106 |
107 | require_once(dirname(__FILE__) . '/functions.php');
108 |
109 | $lp = new limit_purchase;
110 |
111 | if($lp->config['version_check'] <= (time() - (60 * 60 * 24)))
112 | {
113 | $url = "http://clients.jetserver.net/version/limitpurchase.txt";
114 |
115 | $remote_version = file_get_contents($url);
116 | $remote_version = trim($remote_version);
117 |
118 | if($remote_version)
119 | {
120 | $lp->setConfig('version_new', $remote_version);
121 | $lp->config['version_new'] = $remote_version;
122 | }
123 |
124 | $lp->setConfig('version_check', time());
125 | }
126 |
127 | if(version_compare($version, $lp->config['version_new'], '<'))
128 | {
129 | ?>
130 |
131 |
132 | config['version_new']); ?>
133 |
134 | 'success',
170 | 'message' => $vars['_lang']['actionlimit' . ($action == 'disable' ? 'disabled' : 'enabled')],
171 | );
172 | }
173 | else
174 | {
175 | $_SESSION['limit_purchase'] = array(
176 | 'type' => 'error',
177 | 'message' => $vars['_lang']['actionnolimitid'],
178 | );
179 | }
180 | }
181 | else
182 | {
183 | $_SESSION['limit_purchase'] = array(
184 | 'type' => 'error',
185 | 'message' => $vars['_lang']['actionnolimitprovided'],
186 | );
187 | }
188 |
189 | header('Location: ' . $modulelink);
190 | exit;
191 |
192 | break;
193 |
194 | case 'add':
195 |
196 | if($product_id)
197 | {
198 | $sql = "SELECT id
199 | FROM tblproducts
200 | WHERE id = '{$product_id}'";
201 | $result = mysql_query($sql);
202 | $product_details = mysql_fetch_assoc($result);
203 |
204 | if($product_details)
205 | {
206 | $sql = "SELECT id
207 | FROM mod_limit_purchase
208 | WHERE product_id = '{$product_id}'";
209 | $result = mysql_query($sql);
210 | $limit_details = mysql_fetch_assoc($result);
211 |
212 | if(!$limit_details)
213 | {
214 | if($limit > 0 && $error)
215 | {
216 | $sql = "INSERT INTO mod_limit_purchase (`product_id`,`limit`,`error`,`active`) VALUES
217 | ('{$product_id}','{$limit}','{$error}','" . ($active ? 1 : 0) . "')";
218 | mysql_query($sql);
219 |
220 | $_SESSION['limit_purchase'] = array(
221 | 'type' => 'success',
222 | 'message' => $vars['_lang']['actionadded'],
223 | );
224 | }
225 | else
226 | {
227 | $errors = array();
228 |
229 | if(!$limit) $errors[] = '• ' . $vars['_lang']['limit'];
230 | if(!$error) $errors[] = '• ' . $vars['_lang']['errormessage'];
231 |
232 | $_SESSION['limit_purchase'] = array(
233 | 'type' => 'error',
234 | 'message' => $vars['_lang']['actionfieldsreq'] . '
' . implode("
", $errors),
235 | );
236 | }
237 | }
238 | else
239 | {
240 | $_SESSION['limit_purchase'] = array(
241 | 'type' => 'error',
242 | 'message' => $vars['_lang']['actionlimitexists'],
243 | );
244 | }
245 | }
246 | else
247 | {
248 | $_SESSION['limit_purchase'] = array(
249 | 'type' => 'error',
250 | 'message' => $vars['_lang']['actionnoproductid'],
251 | );
252 | }
253 | }
254 | else
255 | {
256 | $_SESSION['limit_purchase'] = array(
257 | 'type' => 'error',
258 | 'message' => $vars['_lang']['actionselectproduct'],
259 | );
260 | }
261 |
262 | header('Location: ' . $modulelink);
263 | exit;
264 | break;
265 |
266 | case 'edit':
267 |
268 | if($id)
269 | {
270 | $sql = "SELECT id
271 | FROM mod_limit_purchase
272 | WHERE id = '{$id}'";
273 | $result = mysql_query($sql);
274 | $limit_details = mysql_fetch_assoc($result);
275 |
276 | if($limit_details)
277 | {
278 | if($product_id)
279 | {
280 | $sql = "SELECT id
281 | FROM tblproducts
282 | WHERE id = '{$product_id}'";
283 | $result = mysql_query($sql);
284 | $product_details = mysql_fetch_assoc($result);
285 |
286 | if($product_details)
287 | {
288 | if($limit > 0 && $error)
289 | {
290 | $sql = "UPDATE mod_limit_purchase
291 | SET `product_id` = '{$product_id}', `limit` = '{$limit}', `error` = '{$error}', active = '" . ($active ? 1 : 0) . "'
292 | WHERE id = '{$id}'";
293 | mysql_query($sql);
294 |
295 | $_SESSION['limit_purchase'] = array(
296 | 'type' => 'success',
297 | 'message' => $vars['_lang']['actionlimitedited'],
298 | );
299 | }
300 | else
301 | {
302 | $errors = array();
303 |
304 | if(!$limit) $errors[] = '• ' . $vars['_lang']['limit'];
305 | if(!$error) $errors[] = '• ' . $vars['_lang']['errormessage'];
306 |
307 | $_SESSION['limit_purchase'] = array(
308 | 'type' => 'error',
309 | 'message' => $vars['_lang']['actionfieldsreq'] . '
' . implode("
", $errors),
310 | );
311 | }
312 | }
313 | else
314 | {
315 | $_SESSION['limit_purchase'] = array(
316 | 'type' => 'error',
317 | 'message' => $vars['_lang']['actionnoproductid'],
318 | );
319 | }
320 | }
321 | else
322 | {
323 | $_SESSION['limit_purchase'] = array(
324 | 'type' => 'error',
325 | 'message' => $vars['_lang']['actionselectproduct'],
326 | );
327 | }
328 | }
329 | else
330 | {
331 | $_SESSION['limit_purchase'] = array(
332 | 'type' => 'error',
333 | 'message' => $vars['_lang']['actionnolimitid'],
334 | );
335 | }
336 | }
337 | else
338 | {
339 | $_SESSION['limit_purchase'] = array(
340 | 'type' => 'error',
341 | 'message' => $vars['_lang']['actionnolimitprovided'],
342 | );
343 | }
344 |
345 | header('Location: ' . $modulelink);
346 | exit;
347 | break;
348 |
349 | case 'delete':
350 |
351 | if($id)
352 | {
353 | $sql = "SELECT id
354 | FROM mod_limit_purchase
355 | WHERE id = '{$id}'";
356 | $result = mysql_query($sql);
357 | $limit_details = mysql_fetch_assoc($result);
358 |
359 | if($limit_details)
360 | {
361 | $sql = "DELETE
362 | FROM mod_limit_purchase
363 | WHERE id = '{$id}'";
364 | mysql_query($sql);
365 |
366 | $_SESSION['limit_purchase'] = array(
367 | 'type' => 'success',
368 | 'message' => $vars['_lang']['actionlimitdeleted'],
369 | );
370 | }
371 | else
372 | {
373 | $_SESSION['limit_purchase'] = array(
374 | 'type' => 'error',
375 | 'message' => $vars['_lang']['actionnolimitid'],
376 | );
377 | }
378 | }
379 | else
380 | {
381 | $_SESSION['limit_purchase'] = array(
382 | 'type' => 'error',
383 | 'message' => $vars['_lang']['actionnolimitprovided'],
384 | );
385 | }
386 |
387 | header('Location: ' . $modulelink);
388 | exit;
389 | break;
390 |
391 | case 'manage':
392 |
393 | if($id)
394 | {
395 | $sql = "SELECT id
396 | FROM mod_limit_purchase
397 | WHERE id = '{$id}'";
398 | $result = mysql_query($sql);
399 | $limit_details = mysql_fetch_assoc($result);
400 |
401 | if($limit_details)
402 | {
403 | $sql = "SELECT *
404 | FROM mod_limit_purchase
405 | WHERE id = '{$id}'";
406 | $result = mysql_query($sql);
407 | $manage_details = mysql_fetch_assoc($result);
408 | }
409 | else
410 | {
411 | $_SESSION['limit_purchase'] = array(
412 | 'type' => 'error',
413 | 'message' => $vars['_lang']['actionnolimitid'],
414 | );
415 | }
416 | }
417 | else
418 | {
419 | $_SESSION['limit_purchase'] = array(
420 | 'type' => 'error',
421 | 'message' => $vars['_lang']['actionnolimitprovided'],
422 | );
423 | }
424 |
425 | if(isset($_SESSION['limit_purchase']))
426 | {
427 | header('Location: ' . $modulelink);
428 | exit;
429 | }
430 | break;
431 | }
432 |
433 | $sql = "SELECT *
434 | FROM mod_limit_purchase";
435 | $result = mysql_query($sql);
436 |
437 | while($row = mysql_fetch_assoc($result))
438 | {
439 | if($manage_details['product_id'] != $row['product_id'])
440 | {
441 | $sql = "SELECT name
442 | FROM tblproducts
443 | WHERE id = '{$row['product_id']}'";
444 | $result2 = mysql_query($sql);
445 | $product = mysql_fetch_assoc($result2);
446 |
447 | $ids[] = $row['product_id'];
448 | $limits[] = array_merge($row, array('product_details' => $product));
449 | }
450 | }
451 | mysql_free_result($result);
452 |
453 | if(isset($_SESSION['limit_purchase']))
454 | {
455 | ?>
456 |
457 |
458 |
459 |
460 |
478 |
479 |
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 |
--------------------------------------------------------------------------------