1<?php
2
3use WHMCS\Application;
4use WHMCS\Billing\Currency;
5use WHMCS\Config\Setting;
6use WHMCS\Exception\ProgramExit;
7use WHMCS\Product\Product;
8use WHMCS\Session;
9use WHMCS\User\Client;
10use WHMCS\Database\Capsule;
11
12require("../init.php");
13
14/*
15*** USAGE SAMPLES ***
16
17<script language="javascript" src="feeds/productsinfo.php?pid=1&get=name"></script>
18
19<script language="javascript" src="feeds/productsinfo.php?pid=1&get=description"></script>
20
21<script language="javascript" src="feeds/productsinfo.php?pid=1&get=price&billingcycle=monthly¤cy=1"></script>
22
23<script language="javascript" src="feeds/productsinfo.php?pid=1&get=orderurl&carttpl=web20cart"></script>
24
25*/
26
27$whmcs = App::self();
28$pid = (int) $whmcs->get_req_var('pid');
29$get = $whmcs->get_req_var('get');
30$language = $whmcs->get_req_var('language') ?: null;
31$data = array();
32$name = $description = '';
33
34// Verify user input for pid exists, is greater than 0, and as is a valid id
35if ($pid > 0) {
36 $data = Capsule::table('tblproducts')
37 ->where('id', '=', $pid)
38 ->first();
39 $pid = null;
40 if (is_object($data)) {
41 $pid = (int) $data->id;
42 // If there is a user logged in, we will use the client language
43 $userId = (int) Session::get('userid');
44 if (!empty($userId)) {
45 $language = Client::find($userId, array('language'))->language ?: null;
46 }
47 unset($userId);
48 $name = Product::getProductName($pid, $data->name, $language);
49 $description = Product::getProductDescription($pid, $data->description, $language);
50 }
51}
52
53if (empty($pid)) {
54 widgetOutput('Product ID Not Found');
55}
56
57if ($get=="name") {
58 widgetOutput($name);
59} elseif ($get=="description") {
60 $description = str_replace(array("\r", "\n", "\r\n"), "", nl2br($description));
61 widgetOutput($description);
62} elseif ($get=="configoption") {
63 $configOptionNum = $whmcs->get_req_var('configoptionnum');
64 if (!$configOptionNum) {
65 widgetOutput('The variable configoptionnum is required when get is configoption.');
66 }
67 widgetoutput($data['configoption' . (int) $configOptionNum]);
68} elseif ($get=="orderurl") {
69 $cartTemplate = $whmcs->get_req_var('carttpl');
70 if ($cartTemplate == "ajax") {
71 $cartTemplate = "ajaxcart";
72 }
73 $systemUrl = App::getSystemUrl();
74 if (!$cartTemplate) {
75 $cartTemplate = Setting::getValue('OrderFormTemplate ');
76 }
77 widgetOutput("{$systemUrl}cart.php?a=add&pid={$pid}&carttpl={$cartTemplate}");
78} elseif ($get=="price") {
79 // Verify user input for currency exists, is numeric, and as is a valid id
80 $billingCycle = $whmcs->get_req_var('billingcycle');
81 $currencyID = $whmcs->get_req_var('currency');
82 if (!is_numeric($currencyID)) {
83 $currency = array();
84 } else {
85 $currency = getCurrency(null, $currencyID);
86 }
87
88 if (!$currency || !is_array($currency) || !isset($currency['id'])) {
89 $currency = Currency::factoryForClientArea();
90 }
91 $currencyID = $currency['id'];
92
93 $data = Capsule::table('tblpricing')
94 ->where('type', '=', 'product')
95 ->where('currency', '=', $currencyID)
96 ->where('relid', '=', $pid)
97 ->first();
98 $price = $data->$billingCycle;
99 $price = formatCurrency($price);
100 widgetOutput($price);
101} else {
102 widgetOutput('Invalid get option. Valid options are "name", "description", "configoption", "orderurl" or "price"');
103}
104
105/**
106 * The function to output the widget data to the browser in a javascript format.
107 *
108 * @throws WHMCS\Exception\ProgramExit
109 * @param string $value the data to output
110 */
111function widgetOutput($value) {
112 echo "document.write('".addslashes($value)."');";
113 throw new ProgramExit();
114}
115