PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/ww.plugins/online-store/api.php

http://kv-webme.googlecode.com/
PHP | 471 lines | 312 code | 32 blank | 127 comment | 44 complexity | d864690f71f0d5c500d5dd3280af0efa MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, BSD-3-Clause, BSD-2-Clause, Apache-2.0, MIT, LGPL-2.1
  1. <?php
  2. /**
  3. * OnlineStore api functions
  4. *
  5. * PHP version 5
  6. *
  7. * @category None
  8. * @package None
  9. * @author Kae Verens <kae@kvsites.ie>
  10. * @license GPL 2.0
  11. * @link None
  12. */
  13. // { OnlineStore_addProductToCart
  14. /**
  15. * checks that a product got successfully added to a cart.
  16. * or removes it if the item has expired.
  17. *
  18. * @return status
  19. */
  20. function OnlineStore_addProductToCart() {
  21. $id=(int)$_REQUEST['product_id'];
  22. $p=dbRow('select id,expires_on from products where id='.$id);
  23. if ($p && $p['expires_on']>date('Y-m-d')) {
  24. return array('ok'=>1);
  25. }
  26. unset($_SESSION['online-store']['items']['products_'.$id]);
  27. if (!$p) {
  28. return array('error'=>'does not exist');
  29. }
  30. return array('error'=>'expired', 'date_expired'=>$p['expires_on']);
  31. }
  32. // }
  33. // { OnlineStore_getExpiryNotification
  34. /**
  35. * retrieve message for when someone tries to add an expired item to cart
  36. *
  37. * @return string message
  38. */
  39. function OnlineStore_getExpiryNotification() {
  40. $id=(int)$_REQUEST['id'];
  41. $p=dbRow('select * from products where id='.$id);
  42. $product=Product::getInstance($id, $p, true);
  43. $typeid=$p['product_type_id'];
  44. $nfile=USERBASE.'/ww.cache/products/templates/expiry_notification_'.$typeid;
  45. if (!file_exists($nfile)) {
  46. $t=dbRow(
  47. 'select template_expired_notification from products_types where id='
  48. .$typeid
  49. );
  50. $template=strlen($t['template_expired_notification'])>4
  51. ?$t['template_expired_notification']
  52. :''.__('This product has expired. You cannot add it to the cart.').'';
  53. file_put_contents($nfile, $template);
  54. }
  55. require_once SCRIPTBASE.'/ww.incs/common.php';
  56. $smarty=Products_setupSmarty();
  57. $smarty->assign('product', $product);
  58. $smarty->assign('product_id', $product->get('id'));
  59. $smarty->assign('_name', __FromJson($product->name));
  60. $smarty->assign('_stock_number', $product->stock_number);
  61. return $smarty->fetch($nfile);
  62. }
  63. // }
  64. // { OnlineStore_checkQrCode
  65. /**
  66. * check a QR Code voucher to see if it's valid
  67. *
  68. * @return null
  69. */
  70. function OnlineStore_checkQrCode() {
  71. global $DBVARS;
  72. echo '<table style="width:100%"><tr><td><img src="/f/skin_files/logo.png"/>'
  73. .'</td><td><h1>'.$DBVARS['site_title'].'</h1><h3>'
  74. .$DBVARS['site_subtitle'].'</h3></td></tr></table><hr/>';
  75. $oid=(int)@$_REQUEST['oid'];
  76. $pid=@$_REQUEST['pid'];
  77. if (!$oid || !$pid) {
  78. echo ''.__('Product or order ID not found').'';
  79. Core_quit();
  80. }
  81. $order=dbRow('select * from online_store_orders where id='.$oid);
  82. if (!$order) {
  83. echo ''.__('Order ID not found.').'';
  84. Core_quit();
  85. }
  86. $md5=$_REQUEST['md5'];
  87. if ($md5!=md5($order['invoice'])) {
  88. echo ''.__('MD5 check failed. this voucher has been tampered with.').'';
  89. Core_quit();
  90. }
  91. echo '<h1>'.__('Valid Voucher').'</h1>';
  92. $items=json_decode($order['items'], true);
  93. $item=$items[$pid];
  94. echo '<h2>'.$item['short_desc'].'</h2>'.$item['long_desc'];
  95. if (!isset($item['voucher_redeemed'])) {
  96. echo '<em>'
  97. .__(
  98. 'This voucher has not yet been redeemed. To redeem this voucher,'
  99. .' please hand it in to the retailer with your purchase.'
  100. )
  101. .'</em>';
  102. }
  103. else {
  104. echo '<p class="warning">'
  105. .__('Warning: This voucher has already been redeemed.').'</p>';
  106. }
  107. if (!Core_isAdmin()) {
  108. echo '<br/><br/><br/>'
  109. .__(
  110. 'If you are the retailer, please <a href="/ww.admin/">log in</a>,'
  111. .' then scan the QR code again.'
  112. );
  113. }
  114. else {
  115. echo '<br/><br/><br/><a href="/a/p=online-store/f=adminRedeemVoucher/'
  116. .'oid='.$oid.'/pid='.$pid.'">'.__('Mark this voucher as redeemed.').'</a>';
  117. }
  118. Core_quit();
  119. }
  120. // }
  121. // { OnlineStore_checkVoucher
  122. /**
  123. * check a voucher to see if it's valid
  124. *
  125. * @param array $params parameters
  126. *
  127. * @return array success status
  128. */
  129. function OnlineStore_checkVoucher($params) {
  130. require_once dirname(__FILE__).'/frontend/voucher-libs.php';
  131. if (!isset($params['code']) || !isset($params['email'])) {
  132. return array(
  133. 'error'=>__('Invalid or missing parameters')
  134. );
  135. }
  136. $valid=OnlineStore_voucherCheckValidity($params['code'], $params['email']);
  137. if ($valid['error']) {
  138. return $valid;
  139. }
  140. else {
  141. return array('ok'=>1);
  142. }
  143. }
  144. // }
  145. // { OnlineStore_getCountries
  146. /**
  147. * get list of countries selected for the checkout
  148. *
  149. * @return array of countries
  150. */
  151. function OnlineStore_getCountries() {
  152. $page_id=(int)$_REQUEST['page_id'];
  153. $countries=json_decode(
  154. dbOne(
  155. 'select value from page_vars where page_id='.$page_id
  156. .' and name="online-store-countries"', 'value'
  157. )
  158. );
  159. $c=array();
  160. foreach ($countries as $k=>$v) {
  161. $c[]=$k;
  162. }
  163. return $c;
  164. }
  165. // }
  166. // { OnlineStore_getQrCode
  167. /**
  168. * output a QR code for a voucher
  169. *
  170. * @return null
  171. */
  172. function OnlineStore_getQrCode() {
  173. require_once SCRIPTBASE.'/ww.incs/phpqrcode.php';
  174. $url=base64_decode($_REQUEST['b64']);
  175. $fname=USERBASE.'/ww.cache/online-store/qr'.md5($url);
  176. if (!file_exists($fname)) {
  177. @mkdir(USERBASE.'/ww.cache/online-store');
  178. QRcode::png(
  179. $url,
  180. $fname
  181. );
  182. }
  183. header('Content-type: image/png');
  184. header('Cache-Control: max-age=2592000, public');
  185. header('Expires-Active: On');
  186. header('Expires: Fri, 1 Jan 2500 01:01:01 GMT');
  187. header('Pragma:');
  188. header('Content-Length: ' . filesize($fname));
  189. readfile($fname);
  190. Core_quit();
  191. }
  192. // }
  193. // { OnlineStore_invoicePdf
  194. /**
  195. * get a PDF version of the invoice
  196. *
  197. * @return null
  198. */
  199. function OnlineStore_invoicePdf() {
  200. $id=(int)$_REQUEST['id'];
  201. $order=dbRow(
  202. 'select invoice, meta, user_id from online_store_orders where id='.$id
  203. );
  204. $ok=false;
  205. if ($order) {
  206. if ($order['user_id']==$_SESSION['userdata']['id']) {
  207. $ok=true;
  208. }
  209. $meta=json_decode($order['meta'], true);
  210. if (isset($_REQUEST['auth']) && isset($meta['auth-md5'])
  211. && $meta['auth-md5']==$_REQUEST['auth']
  212. ) {
  213. $ok=true;
  214. }
  215. }
  216. if (!$ok) {
  217. Core_quit();
  218. }
  219. $inv=$order['invoice'];
  220. // { check if it's already stored as a PDF
  221. if (isset($meta['invoice-type']) && $meta['invoice-type']=='pdf') {
  222. $pdf=base64_decode($inv);
  223. header('Content-type: application/pdf');
  224. echo $pdf;
  225. Core_quit();
  226. }
  227. // }
  228. // { else generate a PDF and output it
  229. $pdfFile=USERBASE.'/ww.cache/online-store/invoice-pdf-'.$id;
  230. if (!file_exists($pdfFile)) {
  231. $html=OnlineStore_invoiceGet($id);
  232. require_once $_SERVER['DOCUMENT_ROOT']
  233. .'/ww.incs/dompdf/dompdf_config.inc.php';
  234. $dompdf=new DOMPDF();
  235. $dompdf->set_base_path($_SERVER['DOCUMENT_ROOT']);
  236. $dompdf->load_html(utf8_decode(str_replace('รข&#x201A;?', '&euro;', $html)), 'UTF-8');
  237. $dompdf->set_paper('a4');
  238. $dompdf->render();
  239. file_put_contents($pdfFile, $dompdf->output());
  240. }
  241. header('Content-type: application/pdf');
  242. $fp=fopen($pdfFile, 'r');
  243. fpassthru($fp);
  244. fclose($fp);
  245. Core_quit();
  246. // }
  247. }
  248. // }
  249. // { OnlineStore_invoiceGet
  250. /**
  251. * retrieve an invoice owned by the user
  252. *
  253. * @param int $id ID of the invoice
  254. *
  255. * @return string
  256. */
  257. function OnlineStore_invoiceGet($id) {
  258. $inv=dbOne(
  259. 'select invoice from online_store_orders where id='.$id.' and user_id='
  260. .$_SESSION['userdata']['id'], 'invoice'
  261. );
  262. if (strpos($inv, '<body')===false) {
  263. $inv='<body>'.$inv.'</body>';
  264. }
  265. if (isset($_REQUEST['print'])) {
  266. $inv=str_replace('<body', '<body onload="window.print()"', $inv);
  267. }
  268. return $inv;
  269. }
  270. // }
  271. // { OnlineStore_listSavedLists
  272. /**
  273. * shopping lists
  274. *
  275. * @param array $params parameters
  276. *
  277. * @return array shopping list names
  278. */
  279. function OnlineStore_listSavedLists($params) {
  280. if (!@$_SESSION['userdata']['id']) {
  281. return array('error'=>'you are not logged in');
  282. }
  283. $names=array();
  284. $rs=dbAll(
  285. 'select name from online_store_lists where user_id='
  286. .$_SESSION['userdata']['id'].' order by name'
  287. );
  288. foreach ($rs as $r) {
  289. $names[]=$r['name'];
  290. }
  291. return array('names'=>$names);
  292. }
  293. // }
  294. // { OnlineStore_loadSavedList
  295. /**
  296. * save a shopping list
  297. *
  298. * @param array $params parameters
  299. *
  300. * @return array success status
  301. */
  302. function OnlineStore_loadSavedList($params) {
  303. if (!@$_SESSION['userdata']['id']) {
  304. return array('error'=>'you are not logged in');
  305. }
  306. if (!@$params['name']) {
  307. return array('error'=>'no list name supplied');
  308. }
  309. $data=dbOne(
  310. 'select details from online_store_lists where '
  311. .' name="'.addslashes($params['name']).'" and user_id='
  312. .$_SESSION['userdata']['id'], 'details'
  313. );
  314. if (!$data) {
  315. return array('error'=>'no such list exists');
  316. }
  317. $_SESSION['online-store']=json_decode($data, true);
  318. return array('success'=>1);
  319. }
  320. // }
  321. // { OnlineStore_pandpGetList
  322. /**
  323. * get list of post and packaging methods
  324. *
  325. * @return array of pandp methods
  326. */
  327. function OnlineStore_pandpGetList() {
  328. $page_id=(int)$_REQUEST['page_id'];
  329. $pandp=json_decode(
  330. dbOne(
  331. 'select value from page_vars where page_id='.$page_id
  332. .' and name="online_stores_postage"',
  333. 'value'
  334. )
  335. );
  336. $c=array();
  337. foreach ($pandp as $k=>$v) {
  338. $c[]=$v->name;
  339. }
  340. return $c;
  341. }
  342. // }
  343. // { OnlineStore_paymentTypesList
  344. /**
  345. * get list of payment types accepted by a checkout
  346. *
  347. * @return array of payment types
  348. */
  349. function OnlineStore_paymentTypesList() {
  350. $page_id=(int)@$_REQUEST['page_id'];
  351. if ($page_id) {
  352. $page=Page::getInstance($page_id);
  353. $page->initValues();
  354. }
  355. else {
  356. $page=@$GLOBALS['PAGEDATA'];
  357. if ($page->type!='online-store') {
  358. $page=Page::getInstanceByType('online-store');
  359. if (!$page) {
  360. return array(
  361. 'error'=>__('No online-store page created')
  362. );
  363. }
  364. $page->initValues();
  365. }
  366. }
  367. // { build list of payment methods
  368. $arr=array();
  369. if (@$page->vars['online_stores_quickpay_merchantid']) {
  370. $arr['QuickPay']=__('Credit Card');
  371. }
  372. if (@$page->vars['online_stores_realex_sharedsecret']) {
  373. $arr['Realex']=__('Credit Card');
  374. }
  375. if (@$page->vars['online_stores_paypal_address']) {
  376. $arr['PayPal']=__('PayPal');
  377. }
  378. if (@$page->vars['online_stores_bank_transfer_account_number']) {
  379. $arr['Bank Transfer']=__('Bank Transfer');
  380. }
  381. // }
  382. if (!count($arr)) {
  383. return array(
  384. // TODO: translation needed
  385. 'error'=>'No payment methods have been defined.'
  386. );
  387. }
  388. return $arr;
  389. }
  390. // }
  391. // { OnlineStore_saveSavedList
  392. /**
  393. * save a shopping list
  394. *
  395. * @param array $params parameters
  396. *
  397. * @return array success status
  398. */
  399. function OnlineStore_saveSavedList($params) {
  400. if (!@$_SESSION['userdata']['id']) {
  401. return array('error'=>'you are not logged in');
  402. }
  403. if (!@$params['name']) {
  404. return array('error'=>'no list name supplied');
  405. }
  406. $data=json_encode($_SESSION['online-store']);
  407. dbQuery(
  408. 'delete from online_store_lists where name="'.addslashes($params['name'])
  409. .'" and user_id='.$_SESSION['userdata']['id']
  410. );
  411. dbQuery(
  412. 'insert into online_store_lists set name="'.addslashes($params['name'])
  413. .'",user_id='.$_SESSION['userdata']['id'].',details="'
  414. .addslashes($data).'"'
  415. );
  416. return array('success'=>1);
  417. }
  418. // }
  419. // { OnlineStore_userRegister
  420. /**
  421. * register a user in the checkout, without email validation
  422. *
  423. * @return array status
  424. */
  425. function OnlineStore_userRegister() {
  426. $email=$_REQUEST['email'];
  427. if (!isset($_SESSION['privacy'])) {
  428. $_SESSION['privacy']=array();
  429. }
  430. $_SESSION['privacy']['registration']=array(
  431. 'token'=> 'token',
  432. 'custom'=> array(),
  433. 'email'=>$email
  434. );
  435. $_REQUEST['token']='token';
  436. require_once $_SERVER['DOCUMENT_ROOT'].'/ww.incs/api-funcs.php';
  437. return Core_register();
  438. }
  439. // }