PageRenderTime 37ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/n3u.php

https://github.com/n3u/n3u-Niche-Store
PHP | 1816 lines | 1518 code | 4 blank | 294 comment | 498 complexity | 50adf1b4605091b26fdfc9a209c1bb9e MD5 | raw file
  1. <?php
  2. /**
  3. n3u Niche Store - Custom Niche PHP Script
  4. Copyright (C) 2012-2014 n3u.com
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>
  15. **/
  16. if(!defined('n3u')){die('Direct access is not permitted.');} // Is n3u defined?
  17. // Get required files and execute required functions in order
  18. require_once('Prosperent_Api.php'); // The Prosperent Api library
  19. $n3u_ServerVars = $_SERVER;
  20. n3u_Input(); // Gets and filters input data
  21. n3u_Config(); // Gets and filters config data
  22. $n3u_PostVars = $_POST;
  23. n3u_Defaults(); // Checks that defaults are set, if not sets them.
  24. //
  25. session_start(); // Check Sessions
  26. if(isset($_SESSION['username']) && isset($_SESSION['password'])){
  27. if($_SESSION['username'] == md5($n3u_configVars['username']) && $_SESSION['password'] == md5($n3u_configVars['password'])){
  28. define('admin',TRUE);
  29. }else{
  30. session_start();
  31. $_SESSION = array();
  32. session_destroy();
  33. header('Location: index.php?x=login');
  34. exit;
  35. }
  36. }
  37. require_once($n3u_configVars['language_dir'] . 'language.php'); // Get the Language
  38. n3u_ErrorChecker(); // Check for errors
  39. n3u_CheckCache(); // Check for cache cleanup routines
  40. n3u_BlockConfig();
  41. /**
  42. n3u_AdditionalStores() looks for additional stores pointed to the same
  43. installation of n3u Niche Store. This is done by looking for domain
  44. configuration files.
  45. **/
  46. function n3u_AdditionalStores($page_limit = NULL){ // Returns array of Custom pages
  47. $n3u_AdditionalStores = array();
  48. $i = 1;
  49. foreach(glob("*_config.php") as $n3u_additional_store){
  50. if(preg_match('/_block/i', $n3u_additional_store) != TRUE){
  51. $n3u_additional_storename = str_replace('_config.php','',$n3u_additional_store);
  52. $n3u_AdditionalStores[$i]['name'] = $n3u_additional_storename;
  53. $n3u_AdditionalStores[$i]['path'] = $n3u_additional_store;
  54. $n3u_AdditionalStores[$i]['url'] = 'http://'. $n3u_additional_storename;
  55. if($page_limit == NULL){ // limits pages returned if specified
  56. $i++;
  57. }elseif($page_limit > $i){
  58. $i++;
  59. }
  60. }
  61. } // finds all custom pages in templates custom directory, builds array
  62. unset($page_limit,$n3u_additional_store,$n3u_additional_storename,$i);
  63. return $n3u_AdditionalStores;
  64. }
  65. function n3u_AutoLinker($text=NULL){
  66. global $n3u_configVars;
  67. $matches=NULL;
  68. preg_match_all("/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/",$text,$matches);
  69. if(isset($matches) && $matches != NULL){
  70. foreach($matches[0] as $match){
  71. // preprint_r($match);
  72. // $text=str_replace($match,'<a href="' . $match . '" target="_blank" title="External Link"> ' . ($match) . '</a>',$text);
  73. if($n3u_configVars['CleanUrls'] == TRUE){
  74. $text=str_replace($match,'<a class="ExternalLink" href="go/'.base64_encode(urlencode($match)) . '.htm" rel="nofollow" target="_blank" title="External Link"> ' . ($match) . '</a>',$text);
  75. }else{
  76. $text=str_replace($match,'<a class="ExternalLink" href="' . $n3u_configVars['self'] . '?x=go&amp;url=' . base64_encode(urlencode($match)) . '" rel="nofollow" target="_blank" title="External Link"> ' . ($match) . '</a>',$text);
  77. }
  78. }
  79. return $text;
  80. }
  81. }
  82. /**
  83. n3u_Block() is used to restrive individual blocks. If $block_area is
  84. specified, n3u_Block() will only look in that position. If TRUE is
  85. passed as a 2nd option, A list will be returned instead.
  86. **/
  87. function n3u_BlockSort($a,$b){
  88. return $a['SortOrder'] - $b['SortOrder'];
  89. }
  90. function n3u_Block($block_area='*',$list=FALSE){
  91. global $n3u_blockData;
  92. global $n3u_configVars;
  93. global $n3u_inputVars;
  94. global $n3u_lang;
  95. global $n3u_results;
  96. global $n3u_PostVars;
  97. global $n3u_ServerVars;
  98. global $prosperentApi;
  99. // preprint_r($n3u_blockData);
  100. if($list == FALSE){
  101. $Blocks = array();
  102. foreach($n3u_blockData as $BlockNameKey => $BlockArrayValue){
  103. // preprint_r($BlockArrayValue);
  104. $n3u_position = $BlockArrayValue['Position'];
  105. if($n3u_position == $block_area){ // If correct Position
  106. $Blocks[$BlockNameKey] = array('Path' => $BlockArrayValue['Path'],'Position' => $BlockArrayValue['Position'], 'SortOrder' => $BlockArrayValue['SortOrder']);
  107. }
  108. unset($n3u_position);
  109. }
  110. $reverse_blockData = $Blocks;
  111. krsort($reverse_blockData); // Reverse Order by keyname so that usort sorts by correct order
  112. usort($reverse_blockData, 'n3u_BlockSort'); // Call n3u_BlockSort Function to sort by SortOrder
  113. foreach($reverse_blockData as $Block){
  114. $n3u_position = $Block['Position'];
  115. require_once $Block['Path'];
  116. unset($n3u_position);
  117. }
  118. // preprint_r($reverse_blockData);
  119. }elseif($list == TRUE){
  120. $n3u_BlockList = array();
  121. $block_name = '*';
  122. $block_filename = 'block_*.php';
  123. foreach($n3u_blockData as $BlockNameKey => $BlockArrayValue){
  124. list($block_dir,$BlockArrayValue['Position'],$BlockNameKey,$block_filename) = explode('/', $filepath);
  125. $n3u_BlockList[$BlockNameKey]['filename'] = $BlockNameKey;
  126. $n3u_BlockList[$BlockNameKey]['Path'] = $BlockArrayValue['Path'];
  127. $n3u_BlockList[$BlockNameKey]['Position'] = $BlockArrayValue['Position'];
  128. if(is_numeric($block_name[0])){
  129. $n3u_BlockList[$BlockNameKey]['SortOrder'] = $block_name[0];
  130. }else{
  131. $n3u_BlockList[$BlockNameKey]['SortOrder'] = NULL;
  132. }
  133. }
  134. unset($block_area,$block_dir,$block_filename,$block_name);
  135. // preprint_r($blocklist);
  136. return $n3u_BlockList;
  137. }
  138. }
  139. /**
  140. n3u_Block() is used to restrive individual blocks. If $block_area is
  141. specified, n3u_Block() will only look in that position. If TRUE is
  142. passed as a 2nd option, A list will be returned instead.
  143. **/
  144. function n3u_BlockConfig(){
  145. global $n3u_blockData;
  146. global $n3u_configVars;
  147. global $n3u_inputVars;
  148. global $n3u_lang;
  149. global $n3u_results;
  150. global $n3u_PostVars;
  151. global $n3u_ServerVars;
  152. global $prosperentApi;
  153. $url = n3u_HTTP_Host();
  154. // if(file_exists($url['host'].'_config.php')){require_once($url['host'].'_config.php');}
  155. if(file_exists($n3u_configVars['include_dir'] . 'configs/' . $url['host'] . '_block_config.php')){ // Use existing but check for new
  156. require_once($n3u_configVars['include_dir'] . 'configs/' . $url['host'] . '_block_config.php');
  157. foreach($n3u_blockData as $BlockName => $BlockArray){
  158. if(!file_exists($BlockArray['Path'])){ // Check to see if path is no longer valid. If so unset data.
  159. unset($n3u_blockData[$BlockName]);
  160. // preprint_r($BlockArray);
  161. n3u_WriteBlockConfig();
  162. }
  163. }
  164. $folderlist = glob($n3u_configVars['blocks_dir'] . "*",GLOB_ONLYDIR);
  165. foreach($folderlist as $folderpath){
  166. $name = str_replace($n3u_configVars['blocks_dir'],'',$folderpath);
  167. if(!isset($n3u_blockData[$name]) || $n3u_blockData[$name] == NULL){
  168. $n3u_blockData[$name] = array(
  169. 'Path' => $n3u_configVars['blocks_dir'] . $name . '/block_'.strtolower($name).'.php',
  170. 'Position' => '#disabled',
  171. 'SortOrder' => '3',
  172. );
  173. n3u_WriteBlockConfig();
  174. }
  175. }
  176. ksort($n3u_blockData);
  177. // preprint_r($n3u_blockData);
  178. }else{ // Get new Block Data
  179. $n3u_blockData = array();
  180. $folderlist = glob($n3u_configVars['blocks_dir'] . "*",GLOB_ONLYDIR);
  181. foreach($folderlist as $folderpath){
  182. $name = str_replace($n3u_configVars['blocks_dir'],'',$folderpath);
  183. $n3u_blockData[$name] = array(
  184. 'Path' => $n3u_configVars['blocks_dir'] . $name . '/block_'.strtolower($name).'.php',
  185. 'Position' => '#disabled',
  186. 'SortOrder' => '3',
  187. );
  188. }
  189. ksort($n3u_blockData);
  190. }
  191. // preprint_r($n3u_blockData);
  192. // n3u_WriteBlockConfig();
  193. // preprint_r($n3u_BlockListArray);
  194. return $n3u_blockData;
  195. }
  196. function n3u_WriteBlockConfig($domainConfig=NULL){
  197. global $n3u_blockData;
  198. global $n3u_configVars;
  199. global $n3u_inputVars;
  200. global $n3u_lang;
  201. global $n3u_results;
  202. global $n3u_PostVars;
  203. global $n3u_ServerVars;
  204. global $prosperentApi;
  205. $url = n3u_HTTP_Host();
  206. if(!isset($domainConfig) || $domainConfig == NULL){
  207. $domainConfig = $n3u_configVars['include_dir'] . 'configs/' . $url['host'] . '_block_config.php';
  208. }
  209. $configFile = '<?php ' . PHP_EOL
  210. . "\t" . '/**' . PHP_EOL
  211. . n3u_GPL_Credits()
  212. . "\t\t" . 'n3u Niche Store - '.$url['host'].'_block_config.php' . PHP_EOL
  213. . "\t\t\t" . 'This configuration file maintains the data used for blocks.' . PHP_EOL
  214. . "\t\t\t" . 'It\'s best to configure Blocks directly from the Admin Panel' . PHP_EOL
  215. . "\t\t\t" . 'Don\'t use a \' character in any option unless you know how to escape properly.' . PHP_EOL
  216. . "\t\t\t" . 'Again it\'s best to use Admin Panel which is safer.' . PHP_EOL
  217. . "\t" . '**/' . PHP_EOL
  218. . "\t" . 'if(!defined(\'n3u\')){die(\'Direct access is not permitted.\');} // Is n3u defined?' . PHP_EOL
  219. . "\t" . '$n3u_blockData = array( // Visit the Blocks admin section to change settings.' . PHP_EOL;
  220. // $BlockData = n3u_BlockConfig();
  221. // preprint_r($BlockData);
  222. foreach($n3u_blockData as $Blockkey => $Blockvalue){
  223. $configFile .= "\t\t" . var_export($Blockkey,TRUE) . ' => ' . preg_replace(array('\'\s+\'','/\s*(?!<\")\/\*[^\*]+\*\/(?!\")\s*/','(,\),)'),array('','','),'),var_export($Blockvalue,TRUE) . ',') . PHP_EOL;
  224. }
  225. $configFile .= "\t" . '); // n3u Niche Store is brought to you by n3u.com' . PHP_EOL
  226. . '?>';
  227. $fp = fopen($domainConfig, "w");
  228. fwrite($fp, $configFile);
  229. fclose($fp);
  230. unset($configFile,$url);
  231. }
  232. /**
  233. n3u_CacheBegin() is used to start the caching process. If the admin is
  234. logged in, or if caching is disabled, This process does not start. If
  235. caching is enabled appropiate headers are sent to notify the browser to
  236. cache as well.
  237. **/
  238. function n3u_CacheBegin(){
  239. global $n3u_inputVars;
  240. global $n3u_configVars;
  241. global $n3u_cacheFile;
  242. global $n3u_ServerVars;
  243. $url = parse_url($n3u_ServerVars['HTTP_HOST']);
  244. if(!isset($url['host']) || $url['host'] == NULL){
  245. if(isset($url['path']) && $url['path'] != NULL){ // Check to see if host is classified as path
  246. $url['host'] = $url['path'];
  247. }elseif(isset($n3u_ServerVars['HTTP_ZONE_NAME']) && $n3u_ServerVars['HTTP_ZONE_NAME'] != NULL){ // Use HTTP_ZONE)NAME
  248. $url['host'] = $n3u_ServerVars['HTTP_ZONE_NAME'];
  249. }else{ // Use SiteURL
  250. $url['host'] = strtolower(str_replace('http://','',$n3u_configVars['SiteURL']));
  251. }
  252. }
  253. $n3u_cacheFile = $n3u_configVars['cache_dir'] .$url['host']. '/' . $n3u_inputVars['x'] . '_' . (md5(@$n3u_inputVars['m'] . '_' . @$n3u_inputVars['b'] . '_' . @urlencode($n3u_inputVars['q']) . '-' . @$n3u_inputVars['sort'] . '-' . @$n3u_inputVars['p'])) . '.htm';
  254. date_default_timezone_set('GMT'); // Sets the default timezone to use, browser headers require GMT.
  255. // Serve from the cache if it is younger than the current $cachetime
  256. if(defined('admin') || $n3u_configVars['caching'] == FALSE){
  257. // Cache is not generated for admin or if caches are disabled.
  258. }elseif(file_exists($n3u_cacheFile) && time() - $n3u_configVars['lifetime'] < filemtime($n3u_cacheFile)){
  259. // if a fresh file exist and admin is not logged in at this point...
  260. header('Cache-Control: max-age='.$n3u_configVars['lifetime'].', must-revalidate');
  261. header('Expires: ' . date('D, d M Y H:i:s e', filemtime($n3u_cacheFile)+$n3u_configVars['lifetime'])); // Date in the future
  262. header('Last-Modified: ' . date('D, d M Y H:i:s e', filemtime($n3u_cacheFile))); // Cache header only sent if not serving cached copy
  263. header('Pragma: public');
  264. header('X-Powered-By: n3u Niche Store ' . $n3u_configVars['Version']);
  265. include($n3u_cacheFile);
  266. // echo "\t\t" . '<!-- This file was generated on ' . date('F jS,Y \@ g:i:s e', filemtime($n3u_cacheFile)) . ' -->' . PHP_EOL;
  267. exit;
  268. }else{
  269. header('Cache-Control: max-age='.$n3u_configVars['lifetime'].', must-revalidate');
  270. header('Expires: ' . date('D, d M Y H:i:s e', time()+$n3u_configVars['lifetime'])); // Date in the future
  271. header('Last-Modified: ' . date('D, d M Y H:i:s e', time())); // Cache header only sent if not serving cached copy
  272. header('Pragma: public');
  273. header('X-Powered-By: n3u Niche Store ' . $n3u_configVars['Version']);
  274. }
  275. unset($url);
  276. ob_start(); // Start the output buffer. The cache don't exist at this point.
  277. }
  278. /**
  279. n3u_CacheCSS() is used to cache CSS if canching is enabled. If disabled,
  280. or Admin is logged in, each individal style link is returned.
  281. **/
  282. function n3u_CacheCSS(){
  283. global $n3u_configVars;
  284. global $n3u_blockData;
  285. global $n3u_ServerVars;
  286. // Cache the contents to a file
  287. if(defined('admin') || $n3u_configVars['caching'] == FALSE){ // if admin or if caching is disabled
  288. foreach(glob($n3u_configVars['blocks_dir'] . '*/*.css') as $filename){ // get block css files
  289. echo "\t\t" . '<link rel="stylesheet" type="text/css" href="' . $filename . '">' . PHP_EOL;
  290. }
  291. foreach(glob($n3u_configVars['Template_Dir'] . $n3u_configVars['Template_Name'] . '/*.css') as $filename){ // get template css files
  292. echo "\t\t" . '<link rel="stylesheet" type="text/css" href="' . $filename . '">' . PHP_EOL;
  293. }
  294. if((defined('admin') == TRUE) && file_exists($n3u_configVars['Template_Dir'] . 'admin/admin.css')){
  295. echo "\t\t" . '<link rel="stylesheet" type="text/css" href="'.$n3u_configVars['Template_Dir'] . 'admin/admin.css">' . PHP_EOL;
  296. }
  297. if((defined('admin') == TRUE) && file_exists($n3u_configVars['Template_Dir'] . 'admin/style.php')){
  298. echo "\t\t" . '<link rel="stylesheet" type="text/css" href="'.$n3u_configVars['Template_Dir'] . 'admin/style.php">' . PHP_EOL;
  299. }
  300. }else{ // if not admin and caching is enabled and cache is old
  301. $url = parse_url($n3u_ServerVars['HTTP_HOST']);
  302. if(!isset($url['host']) || $url['host'] == NULL){
  303. if(isset($url['path']) && $url['path'] != NULL){ // Check to see if host is classified as path
  304. $url['host'] = $url['path'];
  305. }elseif(isset($n3u_ServerVars['HTTP_ZONE_NAME']) && $n3u_ServerVars['HTTP_ZONE_NAME'] != NULL){ // Use HTTP_ZONE_NAME
  306. $url['host'] = $n3u_ServerVars['HTTP_ZONE_NAME'];
  307. }else{ // Use SiteURL
  308. $url['host'] = strtolower(str_replace('http://','',$n3u_configVars['SiteURL']));
  309. }
  310. }
  311. $n3u_cacheFilePath = $n3u_configVars['cache_dir'] .$url['host']. '/' . 'css.css'; // Set cache file name
  312. if(file_exists($n3u_cacheFilePath) && time() - $n3u_configVars['lifetime'] < filemtime($n3u_cacheFilePath)){ // else if a recent cache file exist
  313. // Already exist and is recent so do nothing.
  314. }else{
  315. $css = array();
  316. foreach(glob($n3u_configVars['blocks_dir'] . '*/*.css') as $filename){ // get block css files
  317. // if(preg_match('/disabled/i', $filename) != TRUE){
  318. $css[] = $filename; // add each filename into css array
  319. // }
  320. }
  321. foreach(glob($n3u_configVars['Template_Dir'] . $n3u_configVars['Template_Name'] . '/*.css') as $filename){ // get template css files
  322. if(preg_match('/admin.css/i', $filename) != TRUE){ // Not admin so strip out admin css
  323. $css[] = $filename; // add each filename into css array
  324. }
  325. }
  326. if(file_exists($n3u_configVars['Template_Dir'] . $n3u_configVars['Template_Name'] . '/style.php')){ // get style.php if exist
  327. $css[] = $n3u_configVars['Template_Dir'] . $n3u_configVars['Template_Name'] . '/style.php';
  328. }
  329. if((defined('admin') == TRUE) && file_exists($n3u_configVars['Template_Dir'] . $n3u_configVars['Template_Name'] . '/style.php')){ // get style.php if exist
  330. $css[] = $n3u_configVars['Template_Dir'] . 'admin/style.php';
  331. }
  332. if((defined('admin') == TRUE) && file_exists($n3u_configVars['Template_Dir'] . $n3u_configVars['Template_Name'] . '/style.php')){ // get style.php if exist
  333. $css[] = $n3u_configVars['Template_Dir'] . 'admin/admin.css';
  334. }
  335. $pattern = array('\'\s+\'','/\s*(?!<\")\/\*[^\*]+\*\/(?!\")\s*/','/images\/' . '/'); // what to find
  336. $replacement = array(' ','','../../' . $n3u_configVars['Template_Dir'] . $n3u_configVars['Template_Name'] . '/images/'); // what to replace
  337. ob_start();
  338. foreach($css as $file){require_once($file);} // require every file from the css array
  339. $n3u_cacheFileDump = str_replace(array(' { ',' } '),array('{','} '),preg_replace($pattern,$replacement,ob_get_contents())); // Dump contents as var & replace from patterns
  340. ob_end_clean(); // Stop & flush buffer
  341. $n3u_cacheFileName = fopen($n3u_cacheFilePath, 'w'); // open as writeable
  342. fwrite($n3u_cacheFileName, $n3u_cacheFileDump); // Write the file from var
  343. fclose($n3u_cacheFileName); // Close file
  344. unset($css,$file,$n3u_cacheFileName,$n3u_cacheFileDump,$pattern,$replacement);
  345. }
  346. echo "\t\t" . '<link rel="stylesheet" type="text/css" href="' . $n3u_cacheFilePath . '">' . PHP_EOL;
  347. unset($n3u_cacheFilePath,$url);
  348. }
  349. }
  350. /**
  351. n3u_CacheEnd() is used to end the caching process and write the results
  352. to a single cache file. Cache has already been minified and combined.
  353. **/
  354. function n3u_CacheEnd(){
  355. global $n3u_inputVars;
  356. global $n3u_configVars;
  357. global $n3u_cacheFile;
  358. // Cache the contents to a file
  359. if(defined('admin') || $n3u_configVars['caching'] == FALSE){
  360. ob_end_flush(); // Send the output to browser & flush buffer
  361. }else{
  362. $n3u_cacheFileName = fopen($n3u_cacheFile, 'w'); // Open file as writeable
  363. $n3u_cacheFileDump = str_replace('> <','><',preg_replace("'\s+'",' ',ob_get_contents())); // Remove extra spaces to minify
  364. fwrite($n3u_cacheFileName, $n3u_cacheFileDump); // Write the file
  365. fclose($n3u_cacheFileName); // Close file
  366. ob_end_flush(); // Send the output to browser & flush buffer
  367. }
  368. }
  369. /**
  370. n3u_CacheImage() is used to cache the images returned by Prosperent if
  371. Image Caching is enabled in the Cache Settings of n3u Niche Store.
  372. Images appear to be from your site instead of from another domain. The
  373. browser also deals with less http request and gains performance from
  374. having the file ready to serve. Images are sorted based on their size.
  375. **/
  376. function n3u_CacheImage($src_img,$val){
  377. global $n3u_configVars;
  378. // global $n3u_inputVars;
  379. if($n3u_configVars['cacheImgs'] == FALSE){return $src_img;} // nothing further to do.
  380. $dst_img = $n3u_configVars['img_dir'] . $n3u_configVars['img_size'] . '/' . $val . '_'.$n3u_configVars['img_size'].'.jpg';
  381. if(!is_dir($n3u_configVars['img_dir'])){
  382. mkdir($n3u_configVars['img_dir']);
  383. fclose(fopen($n3u_configVars['img_dir'] . 'index.php', 'w'));
  384. }elseif(!is_dir($n3u_configVars['img_dir'] . $n3u_configVars['img_size'] . '/')){
  385. mkdir($n3u_configVars['img_dir'] . $n3u_configVars['img_size'] . '/');
  386. fclose(fopen($n3u_configVars['img_dir'] . $n3u_configVars['img_size'] . '/' . 'index.php', 'w'));
  387. }
  388. if(!is_file($dst_img)){ // Check if file exist
  389. @copy($src_img,$dst_img);
  390. usleep(50000); // small delay to minimize timeouts from remote images, increase if needed (microseconds)
  391. }
  392. $dst_size = @filesize($dst_img);
  393. if($dst_size < 225){
  394. @unlink($dst_img); // file is empty or likely invalid, delete file
  395. return $n3u_configVars['Template_Dir'] . $n3u_configVars['Template_Name'] . '/images/NoImg'.$n3u_configVars['img_size'].'.png';
  396. }
  397. return $dst_img;
  398. }
  399. /**
  400. n3u_CacheJS() is used to cache JS in a similar manner to n3u_CacheCSS().
  401. **/
  402. function n3u_CacheJS(){
  403. global $n3u_configVars;
  404. global $n3u_ServerVars;
  405. if(defined('admin') || $n3u_configVars['caching'] == FALSE){ // if admin or if caching is disabled
  406. echo "\t\t" . '<script type="text/javascript">' . PHP_EOL
  407. . "\t\t" . ' var _prosperent = {' . PHP_EOL
  408. . "\t\t" . ' \'campaign_id\': \'4fc3f330cc53edb6b1b672001dd0a607\',' . PHP_EOL
  409. . "\t\t" . ' \'platform\': \'other\'' . PHP_EOL
  410. . "\t\t" . ' };' . PHP_EOL
  411. . "\t\t" . '</script>' . PHP_EOL
  412. . "\t\t" . '<script defer type="text/javascript" src="http://prosperent.com/js/prosperent.js"></script>' . PHP_EOL
  413. . "\t\t" . '<script defer src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js" type="text/javascript"></script>' . PHP_EOL;
  414. foreach(glob($n3u_configVars['Template_Dir'] . $n3u_configVars['Template_Name'] . '/*.js') as $filename){echo "\t\t" . '<script defer src="' . $filename . '"></script>' . PHP_EOL;} // get template js files
  415. foreach(glob($n3u_configVars['blocks_dir'] . '*/*.js') as $filename){if(preg_match('/disabled/i', $filename) != TRUE){echo "\t\t" . '<script defer src="' . $filename . '"></script>' . PHP_EOL;}} // get block js files
  416. if($n3u_configVars['jScroll'] == TRUE){echo "\t\t" . '<script defer src="'.$n3u_configVars['include_dir'].'jquery.jscroll.min.js"></script>' . PHP_EOL;}
  417. }else{ // if not admin and caching is enabled and cache is old
  418. $url = parse_url($n3u_ServerVars['HTTP_HOST']);
  419. if(!isset($url['host']) || $url['host'] == NULL){
  420. if(isset($url['path']) && $url['path'] != NULL){ // Check to see if host is classified as path
  421. $url['host'] = $url['path'];
  422. }elseif(isset($n3u_ServerVars['HTTP_ZONE_NAME']) && $n3u_ServerVars['HTTP_ZONE_NAME'] != NULL){ // Use HTTP_ZONE_NAME
  423. $url['host'] = $n3u_ServerVars['HTTP_ZONE_NAME'];
  424. }else{ // Use SiteURL
  425. $url['host'] = strtolower(str_replace('http://','',$n3u_configVars['SiteURL']));
  426. }
  427. }
  428. $n3u_cacheFilePath = $n3u_configVars['cache_dir'] .$url['host']. '/' . 'js.js'; // Set cache file name
  429. if(file_exists($n3u_cacheFilePath) && time() - $n3u_configVars['lifetime'] < filemtime($n3u_cacheFilePath)){ // else if a recent cache file exist
  430. // Already exist and is recent so do nothing.
  431. }else{
  432. $js = array();
  433. foreach(glob($n3u_configVars['Template_Dir'] . $n3u_configVars['Template_Name'] . '/*.js') as $filename){ // get template js files
  434. $js[] = $filename; // add each filename into js array
  435. }
  436. foreach(glob($n3u_configVars['blocks_dir'] . '*/*.js') as $filename){ // get block js files
  437. if(preg_match('/disabled/i', $filename) != TRUE){$js[] = $filename;} // add each filename into js array but ignore disabled blocks
  438. }
  439. ob_start();
  440. foreach($js as $file){require_once($file);} // require every file from the js array
  441. $n3u_cacheFileDump = preg_replace(array('\'\s+\'','/\s*(?!<\")\/\*[^\*]+\*\/(?!\")\s*/'),array(' ',''),ob_get_contents()); // Dump contents as var & replace from patterns
  442. ob_end_clean(); // Stop & flush buffer
  443. $n3u_cacheFileName = fopen($n3u_cacheFilePath, 'w'); // open as writeable
  444. fwrite($n3u_cacheFileName, $n3u_cacheFileDump); // Write the file from var
  445. fclose($n3u_cacheFileName); // Close file
  446. unset($js,$file,$n3u_cacheFileName,$n3u_cacheFileDump);
  447. }
  448. // echo "\t\t" . '<script type="text/javascript">' . PHP_EOL
  449. // . "\t\t\t" . '<!--' . PHP_EOL
  450. // . "\t\t\t" . 'prosperent_pa_uid = '.$n3u_configVars['Prosperent_UserID'].';' . PHP_EOL
  451. // . "\t\t\t" . 'prosperent_pa_fallback_query = \''.$n3u_configVars['defaultKeyword'].'\';' . PHP_EOL
  452. // . "\t\t\t" . '//-->' . PHP_EOL
  453. // . "\t\t" . '</script>' . PHP_EOL
  454. echo "\t\t" . '<script type="text/javascript">' . PHP_EOL
  455. . "\t\t" . ' var _prosperent = {' . PHP_EOL
  456. . "\t\t" . ' \'campaign_id\': \'4fc3f330cc53edb6b1b672001dd0a607\',' . PHP_EOL
  457. . "\t\t" . ' \'platform\': \'other\'' . PHP_EOL
  458. . "\t\t" . ' };' . PHP_EOL
  459. . "\t\t" . '</script>' . PHP_EOL
  460. . "\t\t" . '<script defer src="http://prosperent.com/js/prosperent.js" type="text/javascript"></script>' . PHP_EOL
  461. . "\t\t" . '<script defer src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js" type="text/javascript"></script>' . PHP_EOL
  462. . "\t\t" . '<script defer src="' . $n3u_cacheFilePath . '" type="text/javascript"></script>' . PHP_EOL;
  463. if($n3u_configVars['jScroll'] == TRUE){echo "\t\t" . '<script defer src="'.$n3u_configVars['include_dir'].'jquery.jscroll.min.js" type="text/javascript"></script>' . PHP_EOL;}
  464. unset($n3u_cacheFilePath,$url);
  465. }
  466. }
  467. /**
  468. n3u_CheckApiKey() is used for the Supporter Feature which is fully
  469. configurable in the Administration Panel. Supporters support the project
  470. by allocating n3u.com's api key instead of theirs for that specific item
  471. For example, if a val of 10 is passed (which is default) that translates
  472. into n3u.com having a 1 out of a 10 chance to have it's API Key applied
  473. instead of the default sites API Key. That site would still then average
  474. it's api key being applied 9 out of 10 times. n3u Niche Store allows you
  475. full control of this in the Admin Panel so rather than disable if you
  476. find 1 out of 10 too many, Please try another allocation. It's up to you
  477. though and n3u.com forces nothing.
  478. **/
  479. function n3u_CheckApiKey($api_key,$val=10){ // Check API Key
  480. global $n3u_configVars;
  481. if($val == 0){
  482. return $api_key; // 0 means disabled, so we just return the users api key and never set n3u Niche Stores :(
  483. }elseif($val < 3){ // val was set for 1 or 2 meaning user attempted to allocate over 50% of page views to n3u Niche Store
  484. $val = 3; // This is overridden to 3 meaning 1 out 3 visits (or 33%), The extra view(s) are given back since 3 is minimum for if statement below. Otherwise lower numbers would never return true.
  485. } // $val was set 3 or higher, Process check as normal.
  486. if(rand(1,$val) == 3){ // 3 is symbolic so if 3 is picked at random, replace with n3u's API Key
  487. $api_key = base64_decode('ODcwM2I1OWJhMWE2NDQ5ZmE3ZjM2MWZhMTM5ODI1MjQ=');
  488. }
  489. return $api_key; // returns either the sites api key, or n3u.com's if conditions are met
  490. }
  491. /**
  492. n3u_CheckCache() is used for cache cleanup. You configure the Frequency
  493. in the Administration Panel. This affects both file and image caches.
  494. Files are emptied at random based on the Frequency. Images are only
  495. emptied if they are found to be older than 45 days. This function also
  496. detects when the administrator empties the cache.
  497. **/
  498. function n3u_CheckCache(){ // Check Cache
  499. global $n3u_inputVars;
  500. global $n3u_configVars;
  501. global $n3u_PostVars;
  502. if(rand(1,$n3u_configVars['ClearCacheFreq']) == 3){ // if 3, empty cache (avg 1 out of ClearCacheFreq visits)
  503. n3u_ClearCache(); // Clear File Caches, Auto cleans cache folder 1 out of ClearCacheFreq visits for older files.
  504. }
  505. if(rand(1,$n3u_configVars['ClearImgCacheFreq']) == 3){ // if 3, empty cache (avg 1 out of ClearImgCacheFreq visits)
  506. n3u_ClearImages(60); // Clear Image Caches, Auto cleans images folders 1 out of ClearImgCacheFreq visits for older images.
  507. }
  508. if(isset($n3u_inputVars['clearcache']) && defined('admin')){ // Check if sent via server via GET (Also make sure is admin)
  509. n3u_ClearCache(); // Delete all file caches (but not images)
  510. }
  511. if(isset($n3u_inputVars['clearimgs']) && defined('admin')){ // Clear Image Caches
  512. if($n3u_inputVars['clearimgs'] == 'accessed'){
  513. n3u_ClearImages(60); // Clear images that were last-accessed 45 days or more ago (cleans stale, keeps recently accessed)
  514. }elseif($n3u_inputVars['clearimgs'] == 'modified'){
  515. n3u_ClearImages(60,TRUE); // Clear images that were last-modified 45 days or more ago (cleans any older than 45 days)
  516. }elseif($n3u_inputVars['clearimgs'] == 'all'){
  517. n3u_ClearImages(); // Clear all images (not recommended, Spiders)
  518. }
  519. }
  520. if(isset($n3u_PostVars['ClearCache']) && $n3u_PostVars['ClearCache'] == 'ClearCacheFiles'){ // Check if sent to server via $n3u_PostVars
  521. n3u_ClearCache(); // Clear cache files (does not include images)
  522. }elseif(isset($n3u_PostVars['ClearCache']) && $n3u_PostVars['ClearCache'] == 'ClearAllImages'){
  523. n3u_ClearImages(); // Clear all images (not recommended, Spiders)
  524. }elseif(isset($n3u_PostVars['ClearCache']) && $n3u_PostVars['ClearCache'] == 'ClearLastAccessedImages'){
  525. n3u_ClearImages(60); // Clear images that were last-accessed 45 days or more ago (cleans stale, keep recently accessed)
  526. }elseif(isset($n3u_PostVars['ClearCache']) && $n3u_PostVars['ClearCache'] == 'ClearLastModifiedImages'){
  527. n3u_ClearImages(60,TRUE); // Clear images that were last-modified 45 days or more ago (cleans any older than 30 days ago)
  528. }
  529. }
  530. /**
  531. n3u_Cleaner() may be redundant and needs to be checked.
  532. function n3u_Cleaner($string){
  533. $id_entities = array('%21','%2A','%27','%28','%29','%3B','%3A','%40','%26','&','%3D','%2B','%24','%2C','%2F','%3F','%25','%23','%5B','%5D');
  534. $id_replacements = array('!','*',"'",'(', ')',';',':','@','&amp;','&amp;','=','+','$',',','/','?','%','#','[',']');
  535. return str_replace($id_entities,$id_replacements,$string);
  536. }**/
  537. /**
  538. n3u_Clean() may be redundant and needs to be checked.
  539. function n3u_Clean($string){
  540. $id_entities = array('\'','.',',','/','&',' ');
  541. $id_replacements = array('',' ','','','and',' ');
  542. return strtolower(urlencode(str_replace($id_entities,$id_replacements,$string)));
  543. }**/
  544. /**
  545. n3u_ClearCache() does the actual job of cleaning file caches. If called
  546. directly, expect that file caches are emptied without condition.
  547. **/
  548. function n3u_ClearCache(){
  549. global $n3u_configVars;
  550. $files = array_filter(glob('{' . $n3u_configVars['cache_dir'] . '*,'. $n3u_configVars['cache_dir'] . '*/*}',GLOB_BRACE), 'is_file');
  551. $folders = glob($n3u_configVars['cache_dir'] . '*',GLOB_ONLYDIR);
  552. array_map("unlink", $files);
  553. array_map("rmdir", $folders);
  554. unset($folders,$files);
  555. }
  556. /**
  557. n3u_ClearImages() does the actual job of cleaning image caches. If
  558. called directly, expect that image caches are emptied without condition.
  559. **/
  560. function n3u_ClearImages($val=null,$val2=FALSE){
  561. global $n3u_configVars;
  562. $files = array_filter(glob('{' . $n3u_configVars['img_dir'] . '*,'. $n3u_configVars['img_dir'] . '*/*,'. $n3u_configVars['img_dir'] . '*/*/*}',GLOB_BRACE), 'is_file');
  563. $folders = array_filter($files, 'is_dir');
  564. if($val == null){ // empty, clear all
  565. array_map("unlink", $files);
  566. array_map("rmdir", $folders);
  567. }elseif(is_int($val)){ // number of days
  568. if($val2 == TRUE){ // last modified
  569. foreach ($files as $file){
  570. if(filemtime($file) < strtotime('-' . $val . ' days')){@unlink($file);} // delete file
  571. }
  572. }elseif($val2 == FALSE){ // last accessed
  573. foreach ($files as $file){
  574. if(fileatime($file) < strtotime('-' . $val . ' days')){@unlink($file);} // delete file
  575. }
  576. }
  577. }
  578. unset($folders,$files);
  579. }
  580. /**
  581. n3u_Config() is used to retrieve and filter the ocnfiguration for
  582. n3u_ConfigVars().
  583. **/
  584. function n3u_Config(){ // Check installation, get data
  585. global $n3u_configArgs; // Make $n3u_configArgs Global
  586. global $n3u_configVars;
  587. global $n3u_lang;
  588. global $n3u_ServerVars;
  589. if(file_exists('config.php')){require_once('config.php');} // Backwards compatibility
  590. $url = parse_url($n3u_ServerVars['HTTP_HOST']);
  591. if(!isset($url['host']) || $url['host'] == NULL){
  592. if(isset($url['path']) && $url['path'] != NULL){ // Check to see if host is classified as path
  593. $url['host'] = $url['path'];
  594. }elseif(isset($n3u_ServerVars['HTTP_ZONE_NAME']) && $n3u_ServerVars['HTTP_ZONE_NAME'] != NULL){ // Use HTTP_ZONE_NAME
  595. $url['host'] = $n3u_ServerVars['HTTP_ZONE_NAME'];
  596. }else{ // Use SiteURL
  597. $url['host'] = strtolower(str_replace('http://','',$n3u_configVars['SiteURL']));
  598. }
  599. }
  600. if(file_exists($url['host'].'_config.php')){require_once($url['host'].'_config.php');} // This should get config for sub domains and overtide main configs array in the process.
  601. $n3u_configArgs = array(
  602. 'accessKey' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  603. 'AdminCategories' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  604. 'AdminCatList' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  605. 'api_key' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  606. 'blocks_dir' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  607. 'cache_dir' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  608. 'cacheBackend' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  609. 'cacheImgs' => FILTER_VALIDATE_BOOLEAN,
  610. 'caching' => FILTER_VALIDATE_BOOLEAN,
  611. 'ClearCacheFreq' => FILTER_VALIDATE_INT,
  612. 'ClearImgCacheFreq' => FILTER_VALIDATE_INT,
  613. 'Categories' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_ENCODE_HIGH),
  614. 'CategoryFilters' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_ENCODE_HIGH),
  615. // 'channel_id' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  616. 'CleanUrls' => FILTER_VALIDATE_BOOLEAN,
  617. 'commissionDateMonths' => FILTER_VALIDATE_INT,
  618. 'commissionDateRange' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  619. 'debug' => FILTER_VALIDATE_BOOLEAN,
  620. 'enableCoupons' => FILTER_VALIDATE_BOOLEAN,
  621. 'enableFacets' => FILTER_VALIDATE_BOOLEAN,
  622. 'enableJsonCompression' => FILTER_VALIDATE_BOOLEAN,
  623. 'enableQuerySuggestion' => FILTER_VALIDATE_BOOLEAN,
  624. 'defaultKeyword' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_ENCODE_HIGH),
  625. 'defaultLanguage' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_ENCODE_HIGH),
  626. 'img_dir' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  627. 'img_size' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_HIGH),
  628. 'include_dir' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  629. 'language_dir' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  630. 'lifetime' => FILTER_SANITIZE_NUMBER_INT,
  631. 'limit' => FILTER_SANITIZE_NUMBER_INT,
  632. 'logging' => FILTER_VALIDATE_BOOLEAN,
  633. 'msg_dir' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  634. 'password' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  635. 'Prosperent_Endpoint' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  636. 'Prosperent_UserID' => FILTER_SANITIZE_NUMBER_INT,
  637. 'querySuggestion' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_ENCODE_HIGH),
  638. 'reCaptcha_privKey' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_ENCODE_HIGH),
  639. 'reCaptcha_pubKey' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_ENCODE_HIGH),
  640. 'jScroll' => FILTER_VALIDATE_BOOLEAN,
  641. 'self' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  642. 'sid' => FILTER_SANITIZE_URL,
  643. 'SiteEmail' => FILTER_VALIDATE_EMAIL,
  644. 'SiteIndex' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  645. 'SiteName' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  646. 'SiteURL' => FILTER_SANITIZE_URL,
  647. 'Supporter' => FILTER_SANITIZE_NUMBER_INT,
  648. 'Template_Dir' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  649. 'Template_Name' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  650. 'username' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  651. 'Version' => array('filter' => FILTER_SANITIZE_NUMBER_FLOAT,'flags' => FILTER_FLAG_ALLOW_FRACTION),
  652. );
  653. $n3u_configVars = @filter_var_array($n3u_configData,$n3u_configArgs);
  654. unset($url);
  655. }
  656. /**
  657. n3u_CustomPages() restrieves custom pages and builds and array
  658. **/
  659. function n3u_CustomPages($page_limit = NULL){ // Returns array of Custom pages
  660. global $n3u_configVars;
  661. $n3u_customPages = array();
  662. $i = 1;
  663. $url = n3u_HTTP_Host();
  664. foreach(glob($n3u_configVars['include_dir'] . 'custom/'.$url['host'] . '_' . "*.php") as $n3u_custom_page){
  665. $n3u_custom_pagename = str_replace('.php','',str_replace($n3u_configVars['include_dir'] . 'custom/'.$url['host'] . '_','',$n3u_custom_page));
  666. $n3u_customPages[$i]['name'] = $n3u_custom_pagename;
  667. $n3u_customPages[$i]['path'] = $n3u_custom_page;
  668. if($n3u_configVars['CleanUrls'] == TRUE){
  669. $n3u_customPages[$i]['url'] = './page_'. $n3u_custom_pagename .'.htm';
  670. }else{
  671. $n3u_customPages[$i]['url'] = $n3u_configVars['self'] . '?x=page&amp;page=' . $n3u_custom_pagename;
  672. }
  673. if($page_limit == NULL){ // limits pages returned if specified
  674. $i++;
  675. }elseif($page_limit > $i){
  676. $i++;
  677. }
  678. } // finds all custom pages in templates custom directory, builds array
  679. unset($page_limit,$n3u_custom_page,$n3u_custom_pagename,$i,$url);
  680. return $n3u_customPages;
  681. }
  682. function Boolean2String($boolean = NULL){ // returns string
  683. return($boolean?'True':'False');
  684. }
  685. /**
  686. n3u_date() returns the date in the format requested.
  687. **/
  688. function n3u_date($date, $format){
  689. $n3u_date = date($format, strtotime($date));
  690. return $n3u_date;
  691. }
  692. /**
  693. n3u_Debug() returns debugging information if Debug is enabled.
  694. **/
  695. function n3u_Debug($n3u_val,$info=FALSE){
  696. global $n3u_inputVars;
  697. global $n3u_configVars;
  698. global $n3u_lang;
  699. global $prosperentApi;
  700. global $n3u_results; //$n3u_results
  701. if($info == TRUE){
  702. if($n3u_configVars['debug'] == TRUE){ // Is debug mode enabled?
  703. if(defined('admin')){
  704. // Error & Debug Info
  705. echo "\t\t\t\t" . '<div class="block_footer" id="Debug">' . PHP_EOL
  706. . "\t\t\t\t\t" . '<h3>' . $n3u_lang['Debug'] . '</h3>' . PHP_EOL
  707. . "\t\t\t\t\t" . '<hr />' . PHP_EOL
  708. . "\t\t\t\t\t" . '<form action="' . $n3u_configVars['self'] . '" id="debug_form" method="post">' . PHP_EOL
  709. . "\t\t\t\t\t\t" . '<fieldset>' . PHP_EOL
  710. . "\t\t\t\t\t\t\t" . '<legend>' . $n3u_lang['Debug_Mode'] . '</legend>' . PHP_EOL
  711. . "\t\t\t\t\t\t\t" . '<ul>' . PHP_EOL
  712. . "\t\t\t\t\t\t\t\t" . '<li>' . $n3u_lang['n3u_Niche_Store'] . ': <span>' . $n3u_lang['Debug_Enabled'] . '</span></li>' . PHP_EOL
  713. . "\t\t\t\t\t\t\t\t" . '<li>' . $n3u_lang['n3u_Niche_Store'] . ': <span>' . $n3u_lang['Debug_Explain1'] . '</span></li>' . PHP_EOL;
  714. if(in_array($n3u_inputVars['x'],array('index','item','search'))){
  715. n3u_FetchSearch();
  716. if($prosperentApi->hasWarnings()){
  717. foreach($prosperentApi->getWarnings() as $n3u_Warnings){
  718. echo "\t\t\t\t\t\t\t\t" . '<li>' . $n3u_lang['Prosperent'] . ': <span class="explain">(' . $n3u_Warnings['code'] . ') ' . $n3u_Warnings['msg'] . '</span></li>' . PHP_EOL;
  719. }
  720. }
  721. if($prosperentApi->hasErrors()){
  722. foreach($prosperentApi->getErrors() as $n3u_Errors){
  723. echo "\t\t\t\t\t\t\t\t" . '<li>' . $n3u_lang['Prosperent'] . ': <span class="explain">(' . $n3u_Errors['code'] . ') ' . $n3u_Errors['msg'] . '</span></li>' . PHP_EOL;
  724. }
  725. }
  726. echo "\t\t\t\t\t\t\t" . '</ul>' . PHP_EOL;
  727. if(isset($n3u_results) && $n3u_results != NULL){
  728. echo "\t\t\t\t\t\t\t\t" . '<label>Dump of $n3u_results: </label>' . PHP_EOL;
  729. foreach($n3u_results as $n3u_resultsString){
  730. foreach($n3u_resultsString as $n3u_resultKey => $n3u_resultString){
  731. echo "\t\t\t\t\t\t\t\t" . '<div class="debugvalues"><div class="keyvalue">$n3u_results[\''.$n3u_resultKey.'\']&nbsp;=&nbsp;</div>' . '<div class="stringvalue">'.str_replace('&','&amp;',var_export($n3u_resultString,TRUE)).';</div></div>' . PHP_EOL;
  732. }
  733. echo "\t\t\t\t\t\t\t\t" . '<hr class="hr" />' . PHP_EOL;
  734. }
  735. }
  736. }else{
  737. echo "\t\t\t\t\t\t\t" . '</ul>' . PHP_EOL;
  738. }
  739. echo "\t\t\t\t\t\t\t" . '<label>Dump of $n3u_inputVars: </label>' . PHP_EOL;
  740. foreach($n3u_inputVars as $n3u_inputVarKey => $n3u_inputVarString){
  741. echo "\t\t\t\t\t\t\t" . '<div class="debugvalues"><div class="keyvalue">$n3u_inputVars[\''.$n3u_inputVarKey.'\']&nbsp;=&nbsp;</div>'
  742. . '<div class="stringvalue">'.var_export($n3u_inputVarString,TRUE).';</div></div>' . PHP_EOL;
  743. }
  744. echo "\t\t\t\t\t\t\t\t" . '<hr class="hr" />' . PHP_EOL
  745. . "\t\t\t\t\t\t\t\t" . '<label>Dump of $n3u_configVars: </label>' . PHP_EOL;
  746. foreach($n3u_configVars as $n3u_configVarKey => $n3u_configVarString){
  747. if($n3u_configVarKey == 'password'){
  748. $n3u_configVarString = str_replace($n3u_configVarString,'*******',$n3u_configVarString);
  749. }
  750. echo "\t\t\t\t\t\t\t" . '<div class="debugvalues"><div class="keyvalue">$n3u_configVars[\''.$n3u_configVarKey.'\']&nbsp;=&nbsp;</div>'
  751. . '<div class="stringvalue">'.var_export($n3u_configVarString,TRUE).';</div></div>' . PHP_EOL;
  752. }
  753. echo "\t\t\t\t\t\t" . '</fieldset>' . PHP_EOL
  754. . "\t\t\t\t\t" . '</form>' . PHP_EOL
  755. . "\t\t\t\t" . '</div>' . PHP_EOL; //div debug
  756. }
  757. }
  758. }else{ // Not generating info, instead debug an array
  759. if(isset($n3u_val) && $n3u_val != NULL){
  760. echo "\t\t\t\t\t" . '<pre>' . PHP_EOL;
  761. foreach($n3u_val as $n3u_row){print_r(str_replace('&','&amp;',array_combine(array_keys($n3u_row), array_values($n3u_row))));}
  762. echo "\t\t\t\t\t" . '</pre>' . PHP_EOL;
  763. }
  764. }
  765. }
  766. /**
  767. n3u_Defaults() sets the minimums or defaults for n3u Niche Store.
  768. **/
  769. function n3u_Defaults(){
  770. global $n3u_inputVars;
  771. global $n3u_configVars;
  772. global $n3u_lang;
  773. global $n3u_extendedSort;
  774. global $n3u_ServerVars;
  775. if($n3u_configVars['debug'] == TRUE){
  776. error_reporting(E_ALL ^ E_NOTICE); // Report PHP errors but not notices
  777. }else{
  778. error_reporting(0); // Report no PHP errors
  779. }
  780. // First let's set defaults for $n3u_inputVars
  781. if(!isset($n3u_inputVars['b']) || $n3u_inputVars['b'] == array('Any'||'Unknown')){$n3u_inputVars['b'] = '';}
  782. // if(!isset($n3u_inputVars['compare'])){$n3u_inputVars['compare'] = NULL;}
  783. if(!isset($n3u_inputVars['m']) || $n3u_inputVars['m'] == array('Any'||'Unknown')){$n3u_inputVars['m'] = '';}
  784. if(!isset($n3u_inputVars['url'])){unset($n3u_inputVars['url']);}
  785. // if(!isset($n3u_inputVars['price_max']) || ($n3u_inputVars['price_max'] == NULL)){$n3u_inputVars['price_max'] = '0';}
  786. // if(!isset($n3u_inputVars['price_min']) || ($n3u_inputVars['price_min'] == NULL)){$n3u_inputVars['price_min'] = '0';}
  787. if(!isset($n3u_inputVars['p']) || ($n3u_inputVars['p'] == NULL)){$n3u_inputVars['p'] = '1';}
  788. if(!isset($n3u_inputVars['sort']) || !in_array($n3u_inputVars['sort'],array('ASC','DESC','REL'))){$n3u_inputVars['sort'] = 'REL';}
  789. if(!isset($n3u_inputVars['item'])){$n3u_inputVars['item'] = '';} // implement an error here
  790. if(isset($n3u_inputVars['sort']) && $n3u_inputVars['sort'] == 'ASC'){
  791. $n3u_extendedSort= 'price ASC';
  792. }elseif(isset($n3u_inputVars['sort']) && $n3u_inputVars['sort'] == 'DESC'){
  793. $n3u_extendedSort= 'price DESC';
  794. }elseif(isset($n3u_inputVars['sort']) && $n3u_inputVars['sort'] == 'REL'){
  795. $n3u_extendedSort= '@relevance DESC';
  796. }
  797. // defaults
  798. if(!isset($n3u_ServerVars['HTTP_CF_CONNECTING_IP'])){$n3u_configVars['visitor_ip'] = @$n3u_ServerVars['REMOTE_ADDR'];}else{$n3u_configVars['visitor_ip'] = $n3u_ServerVars['HTTP_CF_CONNECTING_IP'];} // CloudFlare is a reverse proxy, so all ips look like they originate from cloudflare, CloudFlare passes REMOTE_ADDR as HTTP_CF_CONNECTING_IP
  799. if(!isset($n3u_ServerVars['HTTP_REFERER'])){$n3u_configVars['referrer'] = '';$n3u_ServerVars['HTTP_REFERER'] = '';}else{$n3u_configVars['referrer'] = $n3u_ServerVars['HTTP_REFERER'];} // Auto determines Visitors Referrer
  800. if(preg_match("/" . @$n3u_ServerVars['HTTP_HOST'] . "/i",$n3u_ServerVars['HTTP_REFERER'])){$n3u_configVars['referrer'] = '';$n3u_ServerVars['HTTP_REFERER'] = '';} // empties referrer if from site.
  801. if(!isset($n3u_configVars['api_key']) || $n3u_configVars['api_key'] == NULL){$n3u_configVars['api_key'] = '8703b59ba1a6449fa7f361fa13982524'; } // Use n3u's if no other is set (to ensure script functions)
  802. if(!isset($n3u_configVars['blocks_dir']) || $n3u_configVars['blocks_dir'] == NULL){$n3u_configVars['blocks_dir'] = 'blocks/';}
  803. if(!isset($n3u_configVars['cacheBackend']) || $n3u_configVars['cacheBackend'] == NULL){$n3u_configVars['cacheBackend'] = 'File';}
  804. if(!isset($n3u_configVars['cacheImgs']) || $n3u_configVars['cacheImgs'] == NULL){$n3u_configVars['cacheImgs'] = FALSE;}
  805. if(!isset($n3u_configVars['cache_dir']) || $n3u_configVars['cache_dir'] == NULL){$n3u_configVars['cache_dir'] = 'cache/';}
  806. if(!isset($n3u_configVars['caching']) || $n3u_configVars['caching'] == NULL){$n3u_configVars['caching'] = FALSE;}
  807. if(!isset($n3u_configVars['ClearCacheFreq']) || $n3u_configVars['ClearCacheFreq'] == NULL){$n3u_configVars['ClearCacheFreq'] = 100;}
  808. if(!isset($n3u_configVars['ClearImgCacheFreq']) || $n3u_configVars['ClearImgCacheFreq'] == NULL){$n3u_configVars['ClearImgCacheFreq'] = 100;}
  809. if(!is_dir($n3u_configVars['cache_dir'])){mkdir($n3u_configVars['cache_dir']);fclose(fopen($n3u_configVars['cache_dir'] . 'index.php', 'w'));} // Auto creates cache folder & index.php
  810. $url = parse_url($n3u_ServerVars['HTTP_HOST']);
  811. if(!isset($url['host']) || $url['host'] == NULL){
  812. if(isset($url['path']) && $url['path'] != NULL){ // Check to see if host is classified as path
  813. $url['host'] = $url['path'];
  814. }elseif(isset($n3u_ServerVars['HTTP_ZONE_NAME']) && $n3u_ServerVars['HTTP_ZONE_NAME'] != NULL){ // Use HTTP_ZONE_NAME
  815. $url['host'] = $n3u_ServerVars['HTTP_ZONE_NAME'];
  816. }else{ // Use SiteURL
  817. $url['host'] = strtolower(str_replace('http://','',$n3u_configVars['SiteURL']));
  818. }
  819. }
  820. if(!is_dir($n3u_configVars['cache_dir'] . $url['host'] . '/')){mkdir($n3u_configVars['cache_dir'] . $url['host'] . '/');fclose(fopen($n3u_configVars['cache_dir'] . $url['host'] . '/' . 'index.php', 'w'));} // Auto creates cache folder & index.php
  821. unset($url);
  822. if(!isset($n3u_configVars['Categories']) || $n3u_configVars['Categories'] == NULL){$n3u_configVars['Categories'] = '7.2v,9.6v,12v,14.4v,18v,20v';}
  823. if(!isset($n3u_configVars['CategoryFilters']) || $n3u_configVars['CategoryFilters'] == NULL){$n3u_configVars['CategoryFilters'] = 'refurb|refurbished|recondition|reconditioned';}
  824. $n3u_configVars['CategoryFilters'] = str_replace(array(' , ',', ',',',' | ','| ',' '),array('|','|','|','|','|','%20'),$n3u_configVars['CategoryFilters']);
  825. if(!isset($n3u_configVars['CleanUrls']) || $n3u_configVars['CleanUrls'] == NULL){$n3u_configVars['CleanUrls'] = FALSE;}
  826. if(!isset($n3u_configVars['commissionDateMonths']) || $n3u_configVars['commissionDateMonths'] == NULL){$n3u_configVars['commissionDateMonths'] = 3;}
  827. if(!isset($n3u_configVars['commissionDateRange']) || $n3u_configVars['commissionDateRange'] == NULL){$n3u_configVars['commissionDateRange'] = date('Ymd', strtotime('-'.$n3u_configVars['commissionDateMonths'].' months -1 day')).','.date('Ymd', strtotime('yesterday'));}
  828. if(!isset($n3u_configVars['debug']) || $n3u_configVars['debug'] == NULL){$n3u_configVars['debug'] = FALSE;}
  829. if(!isset($n3u_configVars['defaultKeyword']) || $n3u_configVars['defaultKeyword'] == NULL){$n3u_configVars['defaultKeyword'] ='Cordless Drill';}
  830. if(!isset($n3u_inputVars['q']) || $n3u_inputVars['q'] == NULL){$n3u_inputVars['q'] = $n3u_configVars['defaultKeyword'];}
  831. if($n3u_inputVars['q'] != $n3u_configVars['defaultKeyword']){$n3u_inputVars['q'] = $n3u_configVars['defaultKeyword'].' '.str_replace($n3u_configVars['defaultKeyword'] .' ','',$n3u_inputVars['q']);}else{$n3u_inputVars['q'] = $n3u_configVars['defaultKeyword'];}
  832. if($n3u_inputVars['q'] == $n3u_configVars['defaultKeyword'].' '.$n3u_configVars['defaultKeyword']){$n3u_inputVars['q'] = $n3u_configVars['defaultKeyword']; }
  833. if(!isset($n3u_configVars['defaultLanguage']) || $n3u_configVars['defaultLanguage'] == NULL){$n3u_configVars['defaultLanguage'] = 'en-us';}
  834. if(!isset($n3u_configVars['enableCoupons']) || $n3u_configVars['enableCoupons'] == NULL){$n3u_configVars['enableCoupons'] = FALSE;}
  835. if(!isset($n3u_configVars['enableFacets']) || $n3u_configVars['enableFacets'] == NULL){$n3u_configVars['enableFacets'] = TRUE;}
  836. if(!isset($n3u_configVars['enableJsonCompression']) || $n3u_configVars['enableJsonCompression'] == NULL){$n3u_configVars['enableJsonCompression'] = TRUE;}
  837. if(!isset($n3u_configVars['enableQuerySuggestion']) || $n3u_configVars['enableQuerySuggestion'] == NULL){$n3u_configVars['enableQuerySuggestion'] = TRUE;}
  838. if(!isset($n3u_inputVars['lang']) || $n3u_inputVars['lang'] == NULL){$n3u_inputVars['lang'] = $n3u_configVars['defaultLanguage'];} // set default language
  839. if(!isset($n3u_configVars['include_dir']) || $n3u_configVars['include_dir'] == NULL){$n3u_configVars['include_dir'] = 'inc/';}
  840. if(!is_dir($n3u_configVars['include_dir'])){mkdir($n3u_configVars['include_dir']);fclose(fopen($n3u_configVars['include_dir'] . 'index.php', 'w'));} // Auto creates inc folder & index.php
  841. if(!is_dir($n3u_configVars['include_dir'] . 'configs/')){mkdir($n3u_configVars['include_dir'] . 'configs/');fclose(fopen($n3u_configVars['include_dir'] . 'configs/index.php', 'w'));} // Auto creates custom folder & index.php
  842. if(!is_dir($n3u_configVars['include_dir'] . 'custom/')){mkdir($n3u_configVars['include_dir'] . 'custom/');fclose(fopen($n3u_configVars['include_dir'] . 'custom/index.php', 'w'));} // Auto creates custom folder & index.php
  843. if(!is_dir($n3u_configVars['include_dir'] . 'messages/')){mkdir($n3u_configVars['include_dir'] . 'messages/');fclose(fopen($n3u_configVars['include_dir'] . 'messages/index.php', 'w'));} // Auto creates custom folder & index.php
  844. if(!isset($n3u_configVars['img_dir']) || $n3u_configVars['img_dir'] == NULL){$n3u_configVars['img_dir'] = 'images/';}
  845. if(!isset($n3u_configVars['img_size']) || $n3u_configVars['img_size'] == NULL){$n3u_configVars['img_size'] = '125x125';}
  846. if(!isset($n3u_configVars['language_dir']) || $n3u_configVars['language_dir'] == NULL){$n3u_configVars['language_dir'] = 'languages/';}
  847. if(!isset($n3u_configVars['lifetime']) || $n3u_configVars['lifetime'] == NULL){$n3u_configVars['lifetime'] = '86400';}
  848. if(!isset($n3u_configVars['limit']) || $n3u_configVars['limit'] == NULL){$n3u_configVars['limit'] = '10';}
  849. if(!isset($n3u_configVars['username']) || $n3u_configVars['username'] == NULL){$n3u_configVars['username'] = 'n3uadmin';}
  850. if(!isset($n3u_configVars['password']) || $n3u_configVars['password'] == NULL){$n3u_configVars['password'] = 'n3upass';}
  851. if(!isset($n3u_configVars['location']) || $n3u_configVars['location'] == NULL){$n3u_configVars['location'] = 'http://' . $n3u_ServerVars['HTTP_HOST'] . $n3u_ServerVars['REQUEST_URI'];} // Auto determines page location, change to https only if your server supports
  852. if(!isset($n3u_configVars['Prosperent_Endpoint']) || $n3u_configVars['Prosperent_Endpoint'] == NULL){$n3u_configVars['Prosperent_Endpoint'] = 'USA';} // Defaults to USA
  853. if(!isset($n3u_configVars['Prosperent_UserID']) || $n3u_configVars['Prosperent_UserID'] == NULL){$n3u_configVars['Prosperent_UserID'] = '400414';}
  854. if(!isset($n3u_configVars['self'])){$n3u_configVars['self'] = str_replace(' ','%20',($n3u_ServerVars['PHP_SELF']));} // Auto determines current file path, ex: /index.php
  855. if(!isset($n3u_configVars['sid']) || $n3u_configVars['sid'] == NULL){$n3u_configVars['sid'] = @$n3u_ServerVars['HTTP_HOST'];} // Auto determines Domain Name to track by domain, Shouldn't need to change
  856. if(!isset($n3u_configVars['SiteIndex']) || $n3u_configVars['SiteIndex'] == NULL){$n3u_configVars['SiteIndex'] = 'index';} // Defaults to script name
  857. if(!isset($n3u_inputVars['x']) || $n3u_inputVars['x'] == NULL){$n3u_inputVars['x'] = $n3u_configVars['SiteIndex'];}
  858. $n3u_inputVars['x'] = strtolower($n3u_inputVars['x']); // Force lowercase
  859. if($n3u_inputVars['x'] == 'admin' && (!isset($n3u_inputVars['page']) || ($n3u_inputVars['page'] == NULL))){$n3u_inputVars['page'] = 'dashboard';} // Sets default admin page
  860. if(!isset($n3u_configVars['SiteName']) || $n3u_configVars['SiteName'] == NULL){$n3u_configVars['SiteName'] = 'n3u Niche Store';} // Defaults to script name
  861. if(!isset($n3u_configVars['SiteURL']) || $n3u_configVars['SiteURL'] == NULL){$n3u_configVars['SiteURL'] = 'http://' . $n3u_ServerVars['HTTP_HOST'] . '/';} // Defaults to script name
  862. if(!isset($n3u_configVars['Supporter'])){$n3u_configVars['Supporter'] = 10;}elseif(isset($n3u_configVars['Supporter']) && !is_numeric($n3u_configVars['Supporter'])){$n3u_configVars['Supporter'] = '0';} // Defaults on and to 1 out of 10
  863. if(!isset($n3u_configVars['Template_Dir']) || $n3u_configVars['Template_Dir'] == NULL){$n3u_configVars['Template_Dir'] = 'templates/';}
  864. if(!isset($n3u_configVars['Template_Name']) || $n3u_configVars['Template_Name'] == NULL){$n3u_configVars['Template_Name'] = 'n3u';}
  865. if(!isset($n3u_configVars['userAgent']) || $n3u_configVars['userAgent'] == NULL){$n3u_configVars['userAgent'] = @$n3u_ServerVars['HTTP_USER_AGENT'];} // Auto determines Visitors User Agent, Shouldn't need to change
  866. if(!isset($n3u_configVars['Version']) || $n3u_configVars['Version'] == NULL){$n3u_configVars['Version'] = '14.02.20';}
  867. if(!defined('admin') && $n3u_inputVars['x'] == 'item'){$n3u_configVars['api_key'] = n3u_CheckApiKey($n3u_configVars['api_key'],$n3u_configVars['Supporter']);$n3u_configVars['img_size'] = '250x250';}
  868. }
  869. /**
  870. n3u_DirSize() retreives the sizes and file counts of directories.
  871. **/
  872. function n3u_DirSize($path=NULL,$count=FALSE){
  873. $i=0; // used for bytes
  874. $files_count=0; // used to count number of files
  875. $files = array_filter(glob('{'.$path.'*,'.$path.'*/*,'.$path.'*/*/*,'.$path.'*/*/*/*,'.$path.'*/*/*/*}',GLOB_BRACE),'is_file'); // Returns up to 5 levels deep.
  876. foreach($files as $file){
  877. $i=$i+filesize($file); // returned in bytes
  878. if($count == TRUE){$files_count++;} // Auto increase
  879. }
  880. if($count==TRUE){
  881. return round($i/1024/1024,2) . ' MB <em>('.$files_count.' Files)</em>';
  882. }else{
  883. return round($i/1024/1024,2) . ' MB';
  884. }
  885. }
  886. /**
  887. n3u_Error() produces an error. This function is used when you want to
  888. throw a specific error. For example a page not found, or forbidden.
  889. **/
  890. function n3u_Error($ErrorNum,$ErrorDesc=NULL){
  891. global $n3u_configVars;
  892. global $n3u_inputVars;
  893. global $n3u_errorVars;
  894. global $n3u_lang;
  895. $n3u_errorVars['Number'] = $ErrorNum;
  896. $n3u_errorVars['Description'] = $ErrorDesc;
  897. require_once($n3u_configVars['include_dir'] . 'error.php');
  898. require_once($n3u_configVars['Template_Dir'] . $n3u_configVars['Template_Name'] . '/error.php');
  899. }
  900. /**
  901. n3u_ErrorChecker() checks to see if any errors have occured and throws
  902. a relevant error if detected.
  903. **/
  904. function n3u_ErrorChecker(){
  905. global $n3u_inputVars;
  906. global $n3u_errorVars;
  907. global $n3u_lang;
  908. global $n3u_results;
  909. // Handle 404 errors properly to avoid search engine penalties
  910. $error_check = array('admin','contact','download','feed','go','index','item','login','logout','page','privacy','search','terms');
  911. if(!in_array($n3u_inputVars['x'],$error_check)){
  912. $n3u_inputVars['x'] = 'error'; // Set error mode
  913. $n3u_errorVars['Number'] = 404; // Set error type (number)
  914. }
  915. unset($error_check);
  916. if(($n3u_inputVars['x'] == 'download') && !defined('admin')){
  917. $n3u_inputVars['x'] = 'error';
  918. $n3u_errorVars['Number'] = 403; // Set error type (number)
  919. $n3u_errorVars['Description'] = $n3u_lang['Download_Not_Authorized']; // Set error type (number)
  920. }elseif($n3u_inputVars['x'] == 'download' && (!isset($n3u_inputVars['url']) || $n3u_inputVars['url'] == NULL)){
  921. $n3u_inputVars['x'] = 'error';
  922. $n3u_errorVars['Number'] = 403; // Set error type (number)
  923. $n3u_errorVars['Description'] = $n3u_lang['No_URL']; // Set error type (number)
  924. }elseif($n3u_inputVars['x'] == 'go' && (!isset($n3u_inputVars['url']) || $n3u_inputVars['url'] == NULL)){
  925. $n3u_inputVars['x'] = 'error';
  926. $n3u_errorVars['Number'] = 403; // Set error type (number)
  927. $n3u_errorVars['Description'] = $n3u_lang['No_URL']; // Set error type (number)
  928. }
  929. }
  930. /**
  931. n3u_extract() takes a block of text and strips it into sentences.
  932. This is mostly used to limit sentences in Search Results but is also used
  933. in meta descriptions with FALSE passed to prevent links.
  934. **/
  935. function n3u_extract($text,$num=5,$autolink=TRUE){
  936. global $n3u_configVars;
  937. global $n3u_inputVars;
  938. // Create array of sentences using period as the divider
  939. $sentence = explode('. ',$text); // destroys period & space
  940. $extract = ''; // set to empty
  941. $sentences = count($sentence); // Get total number of sentences
  942. $i=0;
  943. // This function is used to replace words actively in item and search result descriptions.
  944. $WordsToBeReplaced = array(
  945. $n3u_inputVars['q'], // search query
  946. $n3u_inputVars['m'], // search merchant
  947. $n3u_inputVars['b'], // search brand
  948. );
  949. // Links
  950. if($n3u_configVars['CleanUrls'] == TRUE){
  951. $LinksToReplaceWith = array(
  952. '<a href="search___'.urlencode($n3u_inputVars['q']).'--1.htm" title="' . n3u_TitleCleaner(urlencode($n3u_inputVars['q'])) . '">'.$n3u_inputVars['q'].'</a>', // replace query with link
  953. '<a href="search_'.urlencode($n3u_inputVars['m']).'__--1.htm" title="' . n3u_TitleCleaner(urlencode($n3u_inputVars['m'])) . '">'.$n3u_inputVars['m'].'</a>', // replace query with link
  954. '<a href="search__'.urlencode($n3u_inputVars['b']).'_--1.htm" title="' . n3u_TitleCleaner(urlencode($n3u_inputVars['b'])) . '">'.$n3u_inputVars['b'].'</a>', // replace query with link
  955. );
  956. }else{
  957. $LinksToReplaceWith = array(
  958. '<a href="index.php?x=search&amp;m=&amp;b=&amp;q='.urlencode($n3u_inputVars['q']).'&amp;sort=&amp;p=1" title="' . n3u_TitleCleaner(urlencode($n3u_inputVars['q'])) . '">'.$n3u_inputVars['q'].'</a>', // replace query with link
  959. '<a href="index.php?x=search&amp;m='.urlencode($n3u_inputVars['m']).'&amp;b=&amp;q=&amp;sort=&amp;p=1" title="' . n3u_TitleCleaner(urlencode($n3u_inputVars['m'])) . '">'.$n3u_inputVars['m'].'</a>', // replace query with link
  960. '<a href="index.php?x=search&amp;m=&amp;b='.urlencode($n3u_inputVars['b']).'&amp;q=&amp;sort=&amp;p=1" title="' . n3u_TitleCleaner(urlencode($n3u_inputVars['b'])) . '">'.$n3u_inputVars['b'].'</a>', // replace query with link
  961. );
  962. }
  963. if($autolink == TRUE){
  964. if($sentences > 1){
  965. // Rebuild, using number defined by $num, and adding period back in.
  966. while(($i<$num) && ($i < $sentences)){
  967. $extract .= "\t\t\t\t\t\t" . str_ireplace($WordsToBeReplaced,$LinksToReplaceWith,n3u_AutoLinker($sentence[$i])) . '.' . PHP_EOL;
  968. if(($i == 4) && ($sentences >= 5)){$extract .= "\t\t\t\t\t\t" . '<br />' . PHP_EOL;} // insert a break
  969. $i++;
  970. }
  971. }else{
  972. $extract = "\t\t\t\t\t\t" . str_ireplace($WordsToBeReplaced,$LinksToReplaceWith,n3u_AutoLinker($sentence[$i])) . '.' . PHP_EOL;
  973. }
  974. }else{
  975. if($sentences > 1){
  976. // Rebuild, using number defined by $num, and adding period back in.
  977. while(($i<$num) && ($i < $sentences)){
  978. $extract .= "\t\t\t\t\t\t" . $sentence[$i] . '.' . PHP_EOL;
  979. if(($i == 4) && ($sentences >= 5)){$extract .= "\t\t\t\t\t\t" . '<br />' . PHP_EOL;} // insert a break
  980. $i++;
  981. }
  982. }else{
  983. $extract = "\t\t\t\t\t\t" . $sentence[$i] . '.' . PHP_EOL;
  984. }
  985. }
  986. return str_replace(array('..','!.',',.'),array('.','!','.'),$extract);
  987. }
  988. /**
  989. n3u_Feed() generates a feed based on the active prosperent data.
  990. This function is mainly used for the category feeds.
  991. **/
  992. function n3u_Feed($val = NULL){
  993. global $n3u_configVars;
  994. global $n3u_inputVars;
  995. global $n3u_lang;
  996. global $prosperentApi;
  997. if($val != NULL){
  998. echo '<?xml version="1.0"?>' . PHP_EOL
  999. . '<feed xmlns="http://www.w3.org/2005/Atom">' . PHP_EOL;
  1000. if(isset($n3u_inputVars['q']) && $n3u_inputVars['q'] != $n3u_configVars['defaultKeyword']){
  1001. echo "\t" . '<title>' . str_replace($n3u_configVars['defaultKeyword'] .' ','',$n3u_inputVars['q']) . ' - ' . $n3u_configVars['defaultKeyword'] . ' - ' . $n3u_configVars['SiteName'] . '</title>' . PHP_EOL;
  1002. }elseif($n3u_inputVars['q'] == $n3u_configVars['defaultKeyword']){
  1003. echo "\t" . '<title>' . $n3u_configVars['defaultKeyword'] . ' - ' . $n3u_configVars['SiteName'] . '</title>' . PHP_EOL;
  1004. }else{
  1005. echo "\t" . '<title>' . $n3u_configVars['SiteName'] . '</title>' . PHP_EOL;
  1006. }
  1007. if($n3u_configVars['CleanUrls'] == TRUE){
  1008. echo "\t" . '<id>' . filter_var(preg_replace('((?<!:)(//+))','/',$n3u_configVars['SiteURL'] . str_replace('index.php','',$n3u_configVars['self']) .$n3u_inputVars['x'] . '_'.$n3u_inputVars['m'].'_'.$n3u_inputVars['b'].'_'.urlencode($n3u_inputVars['q']).'-REL-'.$n3u_inputVars['p'].'.xml',FILTER_SANITIZE_URL)) . '</id>' . PHP_EOL // feed id URI
  1009. . "\t" . '<link rel="self" href="' . filter_var(preg_replace('((?<!:)(//+))','/',$n3u_configVars['SiteURL'] . str_replace('index.php','',$n3u_configVars['self']) .$n3u_inputVars['x'] . '_'.$n3u_inputVars['m'].'_'.$n3u_inputVars['b'].'_'.urlencode($n3u_inputVars['q']).'-REL-'.$n3u_inputVars['p'].'.xml',FILTER_SANITIZE_URL)) . '" />' . PHP_EOL;
  1010. }else{
  1011. echo "\t" . '<id>' . filter_var(preg_replace('((?<!:)(//+))','/',$n3u_configVars['SiteURL'] . $n3u_configVars['self'] . '?x=' . $n3u_inputVars['x'] . '&amp;m=' . urlencode($n3u_inputVars['m']) . '&amp;b=' . urlencode($n3u_inputVars['b']) . '&amp;q=' . urlencode($n3u_inputVars['q']) . '&amp;sort=REL' . '&amp;p='.$n3u_inputVars['p'], FILTER_SANITIZE_URL)) . '</id>' . PHP_EOL // feed id URI
  1012. . "\t" . '<link rel="self" href="' . filter_var(preg_replace('((?<!:)(//+))','/',$n3u_configVars['SiteURL'] . $n3u_configVars['self'] . '?x=' . $n3u_inputVars['x'] . '&amp;m=' . urlencode($n3u_inputVars['m']) . '&amp;b=' . urlencode($n3u_inputVars['b']) . '&amp;q=' . urlencode($n3u_inputVars['q']) . '&amp;sort=REL' . '&amp;p='.$n3u_inputVars['p'], FILTER_SANITIZE_URL)) . '" />' . PHP_EOL;
  1013. }
  1014. echo "\t" . '<updated>'.date('c').'</updated>' . PHP_EOL // last update of feed
  1015. . "\t" . '<author>' . PHP_EOL
  1016. . "\t\t" . '<name>' . $n3u_configVars['SiteName'] . '</name>' . PHP_EOL // feed author, sitename
  1017. . "\t" . '</author>' . PHP_EOL
  1018. . "\t" . '<contributor>' . PHP_EOL
  1019. . "\t\t" . '<name>' . $n3u_lang['n3u_Niche_Store'] . '</name>' . PHP_EOL
  1020. . "\t" . '</contributor>' . PHP_EOL
  1021. . "\t" . '<contributor>' . PHP_EOL
  1022. . "\t\t" . '<name>' . $n3u_lang['Prosperent'] . '</name>' . PHP_EOL
  1023. . "\t" . '</contributor>' . PHP_EOL;
  1024. foreach($val as $item){
  1025. $item['keyword'] = filter_var($item['keyword'],FILTER_SANITIZE_ENCODED,FILTER_FLAG_ENCODE_HIGH);
  1026. $item['description'] = filter_var($item['description'],FILTER_SANITIZE_ENCODED,FILTER_FLAG_ENCODE_HIGH);
  1027. echo "\t" . '<entry>' . PHP_EOL
  1028. . "\t\t" . '<title>' . filter_var($item['keyword'],FILTER_SANITIZE_ENCODED,FILTER_FLAG_ENCODE_HIGH) . '</title>' . PHP_EOL; // item title
  1029. if($n3u_configVars['CleanUrls'] == TRUE){
  1030. echo "\t\t" . '<id>' . filter_var(preg_replace('((?<!:)(//+))','/',$n3u_configVars['SiteURL'] . str_replace('index.php','',$n3u_configVars['self']) . 'item_' . urlencode($item['catalogId']) . '.htm',FILTER_SANITIZE_URL)) . '</id>' . PHP_EOL // feed id URI
  1031. . "\t\t" . '<link href="' . filter_var(preg_replace('((?<!:)(//+))','/',$n3u_configVars['SiteURL'] . str_replace('index.php','',$n3u_configVars['self']) . 'item_' . urlencode($item['catalogId']) . '.htm',FILTER_SANITIZE_URL)) . '" />' . PHP_EOL;
  1032. }else{
  1033. echo "\t\t" . '<id>' . filter_var(preg_replace('((?<!:)(//+))','/',$n3u_configVars['SiteURL'] . $n3u_configVars['self'] . '?x=item&amp;item=' . urlencode($item['catalogId']), FILTER_SANITIZE_URL)) . '</id>' . PHP_EOL // feed id URI
  1034. . "\t\t" . '<link href="' . filter_var(preg_replace('((?<!:)(//+))','/',$n3u_configVars['SiteURL'] . $n3u_configVars['self'] . '?x=item&amp;item=' . urlencode($item['catalogId']), FILTER_SANITIZE_URL)) . '" />' . PHP_EOL;
  1035. }
  1036. echo "\t\t" . '<updated>'.gmdate('Y-m-d\TH:i:sP').'</updated>' . PHP_EOL // item date
  1037. . "\t\t" . '<summary>' . PHP_EOL
  1038. . str_replace("\t\t\t\t\t\t","\t\t\t",n3u_Extract($item['description'],3))
  1039. . "\t\t" . '</summary>' . PHP_EOL // item description
  1040. . "\t" . '</entry>' . PHP_EOL;
  1041. }
  1042. echo '</feed>';
  1043. }
  1044. }
  1045. /**
  1046. n3u_FetchBrands() retrieves all the brand facets.
  1047. **/
  1048. function n3u_FetchBrands($val = NULL){
  1049. global $n3u_configVars;
  1050. global $n3u_inputVars;
  1051. global $n3u_lang;
  1052. global $prosperentApi;
  1053. @$prosperentApi = new Prosperent_Api(array(
  1054. 'imageSize' => '60x30',
  1055. 'limit' => '100',
  1056. 'page' => $n3u_inputVars['p'],
  1057. ));
  1058. if(!$prosperentApi){die($n3u_lang['Prosperent_NoConnect']);} // Check for connection error
  1059. return @$prosperentApi->fetchBrands($val); // Fetch search data, Notices are suppressed with @
  1060. }
  1061. /**
  1062. n3u_FetchCommissions() is used to retreieve commission data.
  1063. **/
  1064. function n3u_FetchCommissions(){
  1065. global $n3u_configVars;
  1066. global $n3u_lang;
  1067. global $prosperentApi;
  1068. global $n3u_stats;
  1069. @$prosperentApi = new Prosperent_Api(array(
  1070. 'accessKey' => $n3u_configVars['accessKey'], // Required
  1071. // 'clickDateRange' => $n3u_configVars['commissionDateRange'], // Required
  1072. 'commissionDateRange' => $n3u_configVars['commissionDateRange'], // Required
  1073. 'enableJsonCompression' => $n3u_configVars['enableJsonCompression'],
  1074. // 'enableFullData' => False,
  1075. // 'userAgent' => $n3u_configVars['userAgent'],
  1076. ));
  1077. try{
  1078. if(!$prosperentApi){die($n3u_lang['Prosperent_NoConnect']);} // Check for connection error
  1079. @$prosperentApi->fetchCommissions(); // Fetch search data, Notices are suppressed with @
  1080. $n3u_stats = @$prosperentApi->getData('');
  1081. }catch(Exception $prosperentApi) {
  1082. die('Caught exception: '. $prosperentApi->getMessage());
  1083. }
  1084. return $n3u_stats;
  1085. }
  1086. /**
  1087. n3u_FetchFeed() is used to pull and parse feeds and to limit results.
  1088. **/
  1089. function n3u_FetchFeed($feedurl = NULL,$limit = 3){
  1090. global $n3u_configVars;
  1091. global $n3u_inputVars;
  1092. global $n3u_lang;
  1093. if($n3u_configVars['caching'] == TRUE){
  1094. $url = n3u_HTTP_Host();
  1095. $n3u_FeedcacheFile = $n3u_configVars['cache_dir'] . $url['host'] . '/feed_' . md5($feedurl) . '_' . $limit . '.htm';
  1096. if(file_exists($n3u_FeedcacheFile) && time() - $n3u_configVars['lifetime'] < filemtime($n3u_FeedcacheFile)){
  1097. // $n3u_FeedcacheFileDump = @filter_var(file_get_contents($n3u_FeedcacheFile),FILTER_SANITIZE_STRING,FILTER_FLAG_STRIP_HIGH);
  1098. // echo 'old' . $url['host'];
  1099. return require_once($n3u_FeedcacheFile); // Exist and is fresh, require.
  1100. }else{ // old or didnt exist, make new
  1101. if($feedurl != NULL){
  1102. // echo 'new' . $url['host'];
  1103. ob_start();
  1104. $feedurl = filter_var($feedurl,FILTER_SANITIZE_URL);
  1105. $headers = get_headers($feedurl); // Get headers
  1106. $return = NULL;
  1107. if(substr($headers[0],9,3) == 200){ // if http 200 status (good)
  1108. $rss = new DOMDocument();
  1109. $rss->load($feedurl);
  1110. $feed = array();
  1111. foreach($rss->getElementsByTagName('item') as $node){
  1112. $item = array(
  1113. 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
  1114. 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
  1115. 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
  1116. );
  1117. array_push($feed,$item);
  1118. }
  1119. for($x=0;$x<$limit;$x++){
  1120. $title = str_replace('&','&amp;',$feed[$x]['title']);
  1121. $link = $feed[$x]['link'];
  1122. if($n3u_configVars['Prosperent_Endpoint'] == 'CA'){ // If Canada
  1123. $date = date('Y-M-d',strtotime($feed[$x]['date']));
  1124. }elseif($n3u_configVars['Prosperent_Endpoint'] == 'UK'){ // If UK
  1125. $date = date('d/m/Y',strtotime($feed[$x]['date']));
  1126. }else{ // Else assume US
  1127. $date = date('M d, Y',strtotime($feed[$x]['date']));
  1128. }
  1129. if($title != NULL || $link != NULL){
  1130. if($n3u_configVars['CleanUrls'] == TRUE){
  1131. $return .= '<p><a class="link" href="go/' . base64_encode(urlencode($link)).'.htm" target="_blank" title="'.$title.'">'.$title.'</a><br /><small><em>'.$n3u_lang['Posted_on'].$date.'</em></small></p>';
  1132. }else{
  1133. $return .= '<p><a class="link" href="' . $n3u_configVars['self'] . '?x=go&amp;url=' . base64_encode(urlencode($link)).'" target="_blank" title="'.$title.'">'.$title.'</a><br /><small><em>'.$n3u_lang['Posted_on'].$date.'</em></small></p>';
  1134. }
  1135. }
  1136. }
  1137. echo $return;
  1138. }else{ // http status code was not 200, Most likely 404...
  1139. echo '<span class="error">'.$n3u_lang['Feed_Cannot_Connect'].$feedurl.'.</span>';
  1140. }
  1141. $n3u_FeedcacheFileDump = ob_get_contents(); // Dump contents as var
  1142. ob_end_flush(); // Stop & flush buffer
  1143. @$n3u_FeedcacheFileName = fopen($n3u_FeedcacheFile, 'w'); // open as writeable
  1144. @fwrite($n3u_FeedcacheFileName, $n3u_FeedcacheFileDump); // Write the file from var
  1145. @fclose($n3u_FeedcacheFileName); // Close file
  1146. unset($date,$feed,$link,$n3u_FeedcacheFileDump,$n3u_FeedcacheFileName,$return,$rss,$title);
  1147. }
  1148. }
  1149. unset($url);
  1150. }else{ // caching is disabled.
  1151. if($feedurl != NULL){
  1152. $feedurl = filter_var($feedurl,FILTER_SANITIZE_URL);
  1153. $headers = get_headers($feedurl); // Get headers
  1154. $return = NULL;
  1155. if(substr($headers[0],9,3) == 200){ // if http 200 status (good)
  1156. $rss = new DOMDocument();
  1157. $rss->load($feedurl);
  1158. $feed = array();
  1159. foreach($rss->getElementsByTagName('item') as $node){
  1160. $item = array(
  1161. 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
  1162. // 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
  1163. 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
  1164. 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
  1165. );
  1166. array_push($feed,$item);
  1167. }
  1168. for($x=0;$x<$limit;$x++){
  1169. $title = str_replace('&','&amp;',$feed[$x]['title']);
  1170. $link = $feed[$x]['link'];
  1171. if($n3u_configVars['Prosperent_Endpoint'] == 'CA'){ // If Canada
  1172. $date = date('Y-M-d',strtotime($feed[$x]['date']));
  1173. }elseif($n3u_configVars['Prosperent_Endpoint'] == 'UK'){ // If UK
  1174. $date = date('d/m/Y',strtotime($feed[$x]['date']));
  1175. }else{ // Else assume US
  1176. $date = date('M d, Y',strtotime($feed[$x]['date']));
  1177. }
  1178. // $description = $feed[$x]['desc'];
  1179. // $return .= "\t\t\t\t\t\t\t\t" . '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />' . PHP_EOL
  1180. // . "\t\t\t\t\t\t\t\t" . '<small><em>'.$n3u_lang['Posted_on'].$date.'</em></small></p>' . PHP_EOL;
  1181. if($title != NULL || $link != NULL){
  1182. if($n3u_configVars['CleanUrls'] == TRUE){
  1183. $return .= '<p><strong><a class="link" href="go/' . base64_encode(urlencode($link)).'.htm" target="_blank" title="'.$title.'">'.$title.'</a></strong><br /><small><em>'.$n3u_lang['Posted_on'].$date.'</em></small></p>';
  1184. }else{
  1185. $return .= '<p><strong><a class="link" href="' . $n3u_configVars['self'] . '?x=go&amp;url=' . base64_encode(urlencode($link)).'" target="_blank" title="'.$title.'">'.$title.'</a></strong><br /><small><em>'.$n3u_lang['Posted_on'].$date.'</em></small></p>';
  1186. }
  1187. }
  1188. }
  1189. echo $return;
  1190. }else{ // http status code was not 200, Most likely 404...
  1191. echo '<span class="error">'.$n3u_lang['Feed_Cannot_Connect'].$feedurl.'.</span>';
  1192. }
  1193. }
  1194. }
  1195. }
  1196. /**
  1197. n3u_FetchItem() is used to fetch individual item information.
  1198. **/
  1199. function n3u_FetchItem($endpoint='USA'){
  1200. global $n3u_configVars;
  1201. global $n3u_inputVars;
  1202. global $n3u_lang;
  1203. global $prosperentApi;
  1204. global $n3u_results;
  1205. $prosperentApi = new Prosperent_Api(array(
  1206. // 'accessKey' => $n3u_configVars['accessKey'],
  1207. 'api_key' => $n3u_configVars['api_key'],
  1208. 'cacheBackend' => $n3u_configVars['cacheBackend'],
  1209. 'cacheOptions' => array(
  1210. 'cache_dir' => $n3u_configVars['cache_dir'],
  1211. 'caching' => $n3u_configVars['caching'],
  1212. 'cache_id_prefix' => 'i'
  1213. ),
  1214. 'debugMode' => $n3u_configVars['debug'],
  1215. 'enableCoupons' => $n3u_configVars['enableCoupons'],
  1216. // 'enableFullData' => TRUE,
  1217. 'enableFacets' => $n3u_configVars['enableFacets'],
  1218. // 'enableJsonCompression' => $n3u_configVars['enableJsonCompression'],
  1219. 'filterCatalogId' => $n3u_inputVars['item'],
  1220. // 'filterProductId' => $n3u_inputVars['compare'],
  1221. 'imageSize' => $n3u_configVars['img_size'],
  1222. 'location' => $n3u_configVars['location'],
  1223. 'logging' => $n3u_configVars['logging'],
  1224. 'query' => $n3u_inputVars['q'],
  1225. 'referrer' => $n3u_configVars['referrer'],
  1226. 'sid' => $n3u_configVars['sid'],
  1227. 'userAgent' => $n3u_configVars['userAgent'],
  1228. 'visitor_ip' => $n3u_configVars['visitor_ip']
  1229. ));
  1230. try{
  1231. if($endpoint == 'CA'){ // Fetch products for Canada
  1232. @$prosperentApi->fetchCaProducts();
  1233. }elseif($endpoint == 'UK'){ // Fetch products for United Kingdom
  1234. @$prosperentApi->fetchUkProducts();
  1235. }else{ // defaults to USA, keeps compatible
  1236. $prosperentApi->fetchProducts(); // Fetch search data, Notices are suppressed with @
  1237. }
  1238. $n3u_results = @$prosperentApi->getData('all');
  1239. }catch(Exception $prosperentApi){
  1240. die('Caught exception: '. $prosperentApi->getMessage());
  1241. }
  1242. return $n3u_results;
  1243. }
  1244. /**
  1245. n3u_FetchMerchants() retreieves all the Merchant facet data.
  1246. **/
  1247. function n3u_FetchMerchants($val = NULL){
  1248. global $n3u_configVars;
  1249. global $n3u_inputVars;
  1250. global $n3u_lang;
  1251. global $prosperentApi;
  1252. @$prosperentApi = new Prosperent_Api(array(
  1253. 'imageSize' => '60x30',
  1254. 'limit' => '100',
  1255. 'page' => $n3u_inputVars['p'],
  1256. ));
  1257. if(!$prosperentApi){die($n3u_lang['Prosperent_NoConnect']);} // Check for connection error
  1258. return @$prosperentApi->fetchMerchants($val); // Fetch search data, Notices are suppressed with @
  1259. }
  1260. /**
  1261. n3u_FetchSearch() is used to fetch all search information.
  1262. **/
  1263. function n3u_FetchSearch($endpoint='USA'){
  1264. global $n3u_configVars;
  1265. global $n3u_inputVars;
  1266. global $n3u_lang;
  1267. global $prosperentApi;
  1268. global $n3u_extendedSort;
  1269. global $n3u_results;
  1270. global $n3u_brands;
  1271. global $n3u_merchants;
  1272. @$prosperentApi = new Prosperent_Api(array(
  1273. // 'accessKey' => $n3u_configVars['accessKey'],
  1274. 'api_key' => $n3u_configVars['api_key'],
  1275. 'cacheBackend' => $n3u_configVars['cacheBackend'],
  1276. // 'cacheBackend' => 'Memcache',
  1277. 'cacheOptions' => array(
  1278. 'cache_dir' => $n3u_configVars['cache_dir'],
  1279. 'caching' => $n3u_configVars['caching'],
  1280. 'cache_id_prefix' => 's_',
  1281. 'lifetime' => $n3u_configVars['lifetime'],
  1282. // 'servers' => array(
  1283. // array(
  1284. // 'host' => $n3u_configVars['cache_host'], // '127.0.0.1',
  1285. // 'persistent' => $n3u_configVars['cache_persistent'], // TRUE,
  1286. // 'port' => $n3u_configVars['cache_port'], // '11211',
  1287. // 'retry_interval' => $n3u_configVars['cache_retry_interval'], // 15,
  1288. // 'status' => $n3u_configVars['cache_status'], // TRUE
  1289. // 'timeout' => $n3u_configVars['cache_timeout'], // 5,
  1290. // 'weight' => $n3u_configVars['cache_weight'], // 1,
  1291. // ),
  1292. // ),
  1293. ),
  1294. // 'categoryID' => $n3u_configVars['channel_id'],
  1295. // 'channel_id' => $n3u_configVars['channel_id'],
  1296. 'debugMode' => $n3u_configVars['debug'],
  1297. 'enableCoupons' => $n3u_configVars['enableCoupons'],
  1298. 'enableJsonCompression' => $n3u_configVars['enableJsonCompression'],
  1299. 'enableFacets' => $n3u_configVars['enableFacets'],
  1300. 'enableFullData' => TRUE,
  1301. 'enableQuerySuggestion' => $n3u_configVars['enableQuerySuggestion'],
  1302. 'extendedQuery' => '@keyword '.$n3u_inputVars['q'] . ' !('.$n3u_configVars['CategoryFilters'].')',
  1303. // 'extendedSortMode' => '@relevance DESC, price '.$n3u_inputVars['sort'].', @id '.$n3u_inputVars['sort'],
  1304. 'extendedSortMode' => $n3u_extendedSort,
  1305. 'filterBrand' => $n3u_inputVars['b'],
  1306. 'filterCatalogId' => $n3u_inputVars['item'],
  1307. 'filterMerchant' => $n3u_inputVars['m'],
  1308. // 'filterProductId' => $n3u_inputVars['compare'],
  1309. 'imageSize' => $n3u_configVars['img_size'],
  1310. 'limit' => $n3u_configVars['limit'],
  1311. 'location' => $n3u_configVars['location'],
  1312. 'logging' => $n3u_configVars['logging'],
  1313. // 'maxPrice' => $n3u_inputVars['price_max'],
  1314. // 'maxPriceSale' => $n3u_inputVars['price_max'],
  1315. // 'minPrice' => $n3u_inputVars['price_min'],
  1316. // 'minPriceSale' => $n3u_inputVars['price_min'],
  1317. 'page' => $n3u_inputVars['p'],
  1318. // 'query' => $n3u_inputVars['q'],
  1319. 'referrer' => $n3u_configVars['referrer'],
  1320. 'sid' => $n3u_configVars['sid'],
  1321. 'sortPrice' => $n3u_inputVars['sort'],
  1322. // 'sortPriceSale' => $n3u_inputVars['sort'],
  1323. 'userAgent' => $n3u_configVars['userAgent'],
  1324. 'visitor_ip' => $n3u_configVars['visitor_ip']
  1325. ));
  1326. try{
  1327. if(!$prosperentApi){die($n3u_lang['Prosperent_NoConnect']);} // Check for connection error
  1328. if($endpoint == 'CA'){ // Fetch products for Canada
  1329. @$prosperentApi->fetchCaProducts();
  1330. }elseif($endpoint == 'UK'){ // Fetch products for United Kingdom
  1331. @$prosperentApi->fetchUkProducts();
  1332. }else{ // defaults to USA, keeps compatible
  1333. @$prosperentApi->fetchProducts(); // Fetch search data, Notices are suppressed with @
  1334. }
  1335. $n3u_results = @$prosperentApi->getData('all');
  1336. }catch(Exception $prosperentApi){
  1337. die('Caught exception: '. $prosperentApi->getMessage());
  1338. }
  1339. return $n3u_results;
  1340. }
  1341. function n3u_GPL_Credits(){
  1342. $GPL_Credits = "\t\t" . 'n3u Niche Store - Custom Niche PHP Script' . PHP_EOL
  1343. . "\t\t" . 'Copyright (C) 2012-2014 n3u.com' . PHP_EOL . PHP_EOL
  1344. . "\t\t" . 'This program is free software: you can redistribute it and/or modify' . PHP_EOL
  1345. . "\t\t" . 'it under the terms of the GNU General Public License as published by' . PHP_EOL
  1346. . "\t\t" . 'the Free Software Foundation, either version 3 of the License, or' . PHP_EOL
  1347. . "\t\t" . '(at your option) any later version.' . PHP_EOL . PHP_EOL
  1348. . "\t\t" . 'This program is distributed in the hope that it will be useful,' . PHP_EOL
  1349. . "\t\t" . 'but WITHOUT ANY WARRANTY; without even the implied warranty of' . PHP_EOL
  1350. . "\t\t" . 'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the' . PHP_EOL
  1351. . "\t\t" . 'GNU General Public License for more details.' . PHP_EOL . PHP_EOL
  1352. . "\t\t" . 'You should have received a copy of the GNU General Public License' . PHP_EOL
  1353. . "\t\t" . 'along with this program. If not, see <http://www.gnu.org/licenses/>' . PHP_EOL . PHP_EOL;
  1354. return $GPL_Credits;
  1355. }
  1356. function n3u_HTTP_Host(){
  1357. global $n3u_ServerVars;
  1358. $url = parse_url($n3u_ServerVars['HTTP_HOST']);
  1359. if(!isset($url['host']) || $url['host'] == NULL){
  1360. if(isset($url['path']) && $url['path'] != NULL){ // Check to see if host is classified as path
  1361. $url['host'] = $url['path'];
  1362. }elseif(isset($n3u_ServerVars['HTTP_ZONE_NAME']) && $n3u_ServerVars['HTTP_ZONE_NAME'] != NULL){ // Use HTTP_ZONE_NAME
  1363. $url['host'] = $n3u_ServerVars['HTTP_ZONE_NAME'];
  1364. }else{ // Use SiteURL
  1365. $url['host'] = strtolower(str_replace('http://','',$n3u_configVars['SiteURL']));
  1366. }
  1367. }
  1368. return $url;
  1369. }
  1370. /**
  1371. n3u_IdCleaner() is used to clean data in id tags
  1372. **/
  1373. function n3u_IdCleaner($string){
  1374. // used on $row['keyword'] in ids
  1375. $id_entities = array('%','?','+', '%21', '%22', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');
  1376. $id_replacements = array('','','_', '', '*', "'", "(", ")", ";", ":", "@", "&amp;", "=", '+', "$", ",", "/", "?", "%", "#", "[", "]");
  1377. return str_replace($id_entities, $id_replacements, urlencode($string));
  1378. }
  1379. /**
  1380. n3u_Input() assigns all input varibles into $n3u_inputVars array.
  1381. **/
  1382. function n3u_Input(){
  1383. global $n3u_inputVars;
  1384. $n3u_inputArgs = array(
  1385. 'b' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  1386. 'm' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  1387. 'p' => FILTER_SANITIZE_NUMBER_INT,
  1388. 'page' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  1389. 'q' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  1390. 'x' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  1391. 'clearcache' => array('filter' => FILTER_VALIDATE_BOOLEAN),
  1392. 'clearimgs' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  1393. // 'compare' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  1394. 'error' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  1395. 'item' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  1396. 'lang' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  1397. // 'url' => FILTER_SANITIZE_URL,
  1398. 'url' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH),
  1399. // 'price_min' => array('filter' => FILTER_SANITIZE_NUMBER_FLOAT,'flags' => FILTER_FLAG_ALLOW_FRACTION),
  1400. // 'price_min' => array('filter' => FILTER_UNSAFE_RAW,'flags' => FILTER_FLAG_STRIP_HIGH),
  1401. // 'price_max' => array('filter' => FILTER_SANITIZE_NUMBER_FLOAT,'flags' => FILTER_FLAG_ALLOW_FRACTION),
  1402. 'sort' => array('filter' => FILTER_SANITIZE_STRING,'flags' => FILTER_FLAG_STRIP_HIGH)
  1403. );
  1404. $n3u_inputVars = filter_input_array(INPUT_GET, $n3u_inputArgs);
  1405. $n3u_inputVars['b'] = str_replace('&#44;','',@$n3u_inputVars['b']); // strip ,
  1406. $n3u_inputVars['q'] = str_replace('&#39;','',@$n3u_inputVars['q']); // strip '
  1407. $n3u_inputVars['page'] = preg_replace(array("'\s+'","/[^a-z0-9._\-]/i"),'',strtolower(@$n3u_inputVars['page'])); // Force lowercase
  1408. $n3u_inputVars['x'] = strtolower(@$n3u_inputVars['x']); // Force lowercase
  1409. // $n3u_inputVars = array_filter($n3u_inputVars);
  1410. // var_dump($n3u_inputVars);
  1411. }
  1412. function n3u_Messages($msg_limit = NULL){ // Returns array of Messages
  1413. global $n3u_configVars;
  1414. $n3u_Messages = array();
  1415. $i = 1;
  1416. // preprint_r($n3u_configVars['msg_dir']);
  1417. $n3u_MessageFiles = glob($n3u_configVars['msg_dir'] . "msg_*.php");
  1418. usort($n3u_MessageFiles, function($a, $b){
  1419. return filemtime($a) < filemtime($b);
  1420. });
  1421. foreach($n3u_MessageFiles as $n3u_message){
  1422. // preprint_r($n3u_message);
  1423. if($msg_limit == NULL){ // limits pages returned if specified
  1424. $n3u_Messages[$i]['id'] = str_replace($n3u_configVars['msg_dir'] . 'msg_','',str_replace('.php','',$n3u_message));
  1425. $n3u_Messages[$i]['date'] = date("F d Y H:i:s", filemtime($n3u_message));
  1426. $n3u_Messages[$i]['url'] = $n3u_message;
  1427. $i++;
  1428. }elseif($msg_limit >= $i){
  1429. $n3u_Messages[$i]['id'] = str_replace($n3u_configVars['msg_dir'] . 'msg_','',str_replace('.php','',$n3u_message));
  1430. $n3u_Messages[$i]['date'] = date("F d Y H:i:s", filemtime($n3u_message));
  1431. $n3u_Messages[$i]['url'] = $n3u_message;
  1432. $i++;
  1433. }
  1434. } // finds all messages in inc directory, builds array
  1435. // preprint_r($n3u_Messages);
  1436. unset($msg_limit,$n3u_message,$i);
  1437. return $n3u_Messages;
  1438. }
  1439. /**
  1440. n3u_Pagination() is used to paginate search results.
  1441. **/
  1442. function n3u_Pagination(){ // Pagination
  1443. global $prosperentApi;
  1444. global $n3u_inputVars;
  1445. global $n3u_configVars;
  1446. global $n3u_lang;
  1447. $n3u_totalItems = @$prosperentApi->gettotalRecordsFound(); // get total records
  1448. if($n3u_totalItems > 1000){$n3u_totalItems = 1000;} // force stop at 1000 results (current prosperent limit)
  1449. $n3u_totalPageCount = ($n3u_totalItems / $n3u_configVars['limit']); // figure out pages needed
  1450. $n3u_totalPages = ceil($n3u_totalPageCount); // round above up a page to ensure total pages is correct for data
  1451. $n3u_prevPage = $n3u_inputVars['p'] -1; // set Previous page as current page number minus 1
  1452. $n3u_nextPage = $n3u_inputVars['p'] +1; // set Next Page as current page number plus 1
  1453. $n3u_resultStart = ($n3u_configVars['limit'] * $n3u_prevPage + 1); // Figure out first result being displayed for current page
  1454. $n3u_resultEnd = ($n3u_configVars['limit'] * $n3u_inputVars['p']); // Figure out last result being displayed
  1455. $n3u_pageZone = 3; // Set the desired page first/last gap here.
  1456. if($n3u_resultEnd > $n3u_totalItems){$n3u_resultEnd = $n3u_totalItems;} // if $n3u_resultEnd is more than $n3u_totalItems, show $n3u_resultEnd as $n3u_totalRecords
  1457. if($n3u_inputVars['p'] > $n3u_totalPages){echo "\t\t\t\t\t" . $n3u_lang['No_Results'] . PHP_EOL;} // n3u_Error(404);exit;
  1458. echo "\t\t\t\t\t" . '<div class="Pagination">' . PHP_EOL;
  1459. $i=1;
  1460. if($n3u_inputVars['p'] <= $n3u_totalPages){ // Display statistics
  1461. echo "\t\t\t\t\t\t" . '<span>Displaying <span class="number">' . $n3u_resultStart .'</span>-<span class="number">'. $n3u_resultEnd . '</span> of <span class="number">' . $n3u_totalItems . '</span> total results found. Displaying page <span class="number">' . $n3u_inputVars['p'] . '</span> of <span class="number">' . $n3u_totalPages . '</span>.</span><br />' . PHP_EOL;
  1462. }
  1463. if($n3u_configVars['CleanUrls'] == TRUE){
  1464. if(($n3u_prevPage > $n3u_pageZone) && ($n3u_inputVars['p'] <= $n3u_totalPages)){ // Display First Link
  1465. echo "\t\t\t\t\t\t" . '<a class="link" href="' . $n3u_inputVars['x'] . '_' . $n3u_inputVars['m'] . '_' . $n3u_inputVars['b'] . '_' . urlencode($n3u_inputVars['q']) . '-' . $n3u_inputVars['sort'] . '-' . '1.htm" rel="prefetch">First</a>&nbsp;' . PHP_EOL;
  1466. }
  1467. if(($n3u_inputVars['p'] > 1) && ($n3u_inputVars['p'] <= $n3u_totalPages)){ // Display Previous Link
  1468. echo "\t\t\t\t\t\t" . '<a class="link" href="' . $n3u_inputVars['x'] . '_' . $n3u_inputVars['m'] . '_' . $n3u_inputVars['b'] . '_' . urlencode($n3u_inputVars['q']) . '-' . $n3u_inputVars['sort'] . '-' . $n3u_prevPage . '.htm" rel="prev">Previous</a>&nbsp;' . PHP_EOL;
  1469. if($n3u_prevPage > $n3u_pageZone){echo "\t\t\t\t\t\t" . ' ... ' . PHP_EOL;}
  1470. }
  1471. while(($i <= $n3u_totalPages) && ($n3u_totalPages > 1) && ($n3u_inputVars['p'] <= $n3u_totalPages)){ // while less than total pages AND total pages is greater than 1 AND current page is equal to or less than total pages
  1472. if($i == $n3u_inputVars['p']){echo "\t\t\t\t\t\t" . '<span class="current">' . $i . '</span>&nbsp;' . PHP_EOL;} // if current page display no link
  1473. if($i < ($n3u_nextPage + $n3u_pageZone) && ($i > $n3u_prevPage - $n3u_pageZone) && $i !=$n3u_inputVars['p']){
  1474. echo "\t\t\t\t\t\t" . '<a class="link" href="' . $n3u_inputVars['x'] . '_' . $n3u_inputVars['m'] . '_' . $n3u_inputVars['b'] . '_' . urlencode($n3u_inputVars['q']) . '-' . $n3u_inputVars['sort'] . '-' . $i . '.htm" rel="prefetch">' . $i . '</a>&nbsp;' . PHP_EOL;
  1475. }
  1476. $i++;
  1477. }
  1478. if(($n3u_inputVars['p'] >= 1) && ($n3u_inputVars['p'] < $n3u_totalPages)){ // Display Next Link
  1479. if($n3u_totalPages - $n3u_pageZone > $n3u_inputVars['p']){echo "\t\t\t\t\t\t\t" . ' ... ' . PHP_EOL;}
  1480. echo "\t\t\t\t\t\t" . '<a href="' . $n3u_inputVars['x'] . '_' . $n3u_inputVars['m'] . '_' . $n3u_inputVars['b'] . '_' . urlencode($n3u_inputVars['q']) . '-' . $n3u_inputVars['sort'] . '-' . $n3u_nextPage . '.htm" class="next" rel="next">Next</a>&nbsp;' . PHP_EOL;
  1481. }
  1482. if(($n3u_nextPage < $n3u_totalPages) && ($n3u_totalPages > (1 + $n3u_pageZone))){ // Display Last Link
  1483. echo "\t\t\t\t\t\t" . '<a class="link" href="' . $n3u_inputVars['x'] . '_' . $n3u_inputVars['m'] . '_' . $n3u_inputVars['b'] . '_' . urlencode($n3u_inputVars['q']) . '-' . $n3u_inputVars['sort'] . '-' . $n3u_totalPages . '.htm" rel="prefetch">Last</a>' . PHP_EOL;
  1484. }
  1485. }else{
  1486. if(($n3u_prevPage > $n3u_pageZone) && ($n3u_inputVars['p'] <= $n3u_totalPages)){ // Display First Link
  1487. echo "\t\t\t\t\t\t" . '<a class="link" href="' . $n3u_configVars['self'] . '?x=' . $n3u_inputVars['x'] . '&amp;b=' . $n3u_inputVars['b'] . '&amp;m=' . $n3u_inputVars['m'] . '&amp;p=1&amp;q=' . urlencode($n3u_inputVars['q']) . '&amp;sort=' . $n3u_inputVars['sort'] . '" rel="prefetch">First</a>&nbsp;' . PHP_EOL;
  1488. }
  1489. if(($n3u_inputVars['p'] > 1) && ($n3u_inputVars['p'] <= $n3u_totalPages)){ // Display Previous Link
  1490. echo "\t\t\t\t\t\t" . '<a class="link" href="' . $n3u_configVars['self'] . '?x=' . $n3u_inputVars['x'] . '&amp;b=' . $n3u_inputVars['b'] . '&amp;m=' . $n3u_inputVars['m'] . '&amp;p=' . $n3u_prevPage . '&amp;q=' . urlencode($n3u_inputVars['q']) . '&amp;sort=' . $n3u_inputVars['sort'] . '" rel="prev">Previous</a>&nbsp;' . PHP_EOL;
  1491. if($n3u_prevPage > $n3u_pageZone){echo "\t\t\t\t\t\t" . ' ... ' . PHP_EOL;}
  1492. }
  1493. while(($i <= $n3u_totalPages) && ($n3u_totalPages > 1) && ($n3u_inputVars['p'] <= $n3u_totalPages)){ // while less than total pages AND total pages is greater than 1 AND current page is equal to or less than total pages
  1494. if($i == $n3u_inputVars['p']){echo "\t\t\t\t\t\t" . '<span class="current">' . $i . '</span>&nbsp;' . PHP_EOL;} // if current page display no link
  1495. if($i < ($n3u_nextPage + $n3u_pageZone) && ($i > $n3u_prevPage - $n3u_pageZone) && $i !=$n3u_inputVars['p']){
  1496. echo "\t\t\t\t\t\t" . '<a class="link" href="' . $n3u_configVars['self'] . '?x=' . $n3u_inputVars['x'] . '&amp;b=' . $n3u_inputVars['b'] . '&amp;m=' . $n3u_inputVars['m'] . '&amp;p=' . $i . '&amp;q=' . urlencode($n3u_inputVars['q']) . '&amp;sort=' . $n3u_inputVars['sort'] . '" rel="prefetch">' . $i . '</a>&nbsp;' . PHP_EOL;
  1497. }
  1498. $i++;
  1499. }
  1500. if(($n3u_inputVars['p'] >= 1) && ($n3u_inputVars['p'] < $n3u_totalPages)){ // Display Next Link
  1501. if($n3u_totalPages - $n3u_pageZone > $n3u_inputVars['p']){echo "\t\t\t\t\t\t" . ' ... ' . PHP_EOL; }
  1502. echo "\t\t\t\t\t\t" . '<a class="link" href="' . $n3u_configVars['self'] . '?x=' . $n3u_inputVars['x'] . '&amp;b=' . $n3u_inputVars['b'] . '&amp;m=' . $n3u_inputVars['m'] . '&amp;p=' . $n3u_nextPage . '&amp;q=' . urlencode($n3u_inputVars['q']) . '&amp;sort=' . $n3u_inputVars['sort'] . '" class="next" rel="next">Next</a>&nbsp;' . PHP_EOL;
  1503. }
  1504. if(($n3u_nextPage < $n3u_totalPages) && ($n3u_totalPages > (1 + $n3u_pageZone))){ // Display Last Link
  1505. echo "\t\t\t\t\t\t" . '<a class="link" href="' . $n3u_configVars['self'] . '?x=' . $n3u_inputVars['x'] . '&amp;b=' . $n3u_inputVars['b'] . '&amp;m=' . $n3u_inputVars['m'] . '&amp;p=' . $n3u_totalPages . '&amp;q=' . urlencode($n3u_inputVars['q']) . '&amp;sort=' . $n3u_inputVars['sort'] . '" rel="prefetch">Last</a>' . PHP_EOL;
  1506. }
  1507. }
  1508. echo "\t\t\t\t\t" . '</div>' . PHP_EOL; // div Pagination
  1509. unset($n3u_totalItems,$n3u_totalPageCount,$n3u_totalPages,$n3u_prevPage,$n3u_nextPage,$n3u_resultStart,$n3u_resultEnd,$n3u_pageZone,$i);
  1510. }
  1511. /**
  1512. n3u_return() may be redundant and needs to be checked.
  1513. function n3u_return($val = null){
  1514. global $prosperentApi;
  1515. return @$prosperentApi->getAllData($val); // get data
  1516. } **/
  1517. /**
  1518. n3u_swap() is redundant and behaves as preg_replace, will be phased out.
  1519. function n3u_swap($pattern,$replacement,$string){
  1520. $string = preg_replace($pattern, $replacement, $string);
  1521. return $string;
  1522. }**/
  1523. /**
  1524. n3u_ReturnPriceSymbol() is used to return correct Price Symbol. Used
  1525. with $n3u_result['currency'] to detect Price symbol that should be displayed.
  1526. Used on Search & Item Pages.
  1527. **/
  1528. function n3u_ReturnPriceSymbol($currency='USD'){
  1529. if($currency == 'GBP'){$n3u_price_symbol = '&pound;';}else{$n3u_price_symbol = '$';}
  1530. return $n3u_price_symbol;
  1531. }
  1532. /**
  1533. n3u_TitleCleaner() is used to clean data in title tags
  1534. **/
  1535. function n3u_TitleCleaner($string){ // used on $row['keyword'] in titles
  1536. $title_entities = array('+', '%21', '%22','%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');
  1537. $title_replacements = array(' ', '!', 'in', '*', '', '(', ')', ';', ':', '@', '&amp;', '=', ' ', '$', ',', '/', '?', '%', '#', '[', ']');
  1538. return str_replace($title_entities, $title_replacements, urlencode($string));
  1539. }
  1540. /**
  1541. n3u_UrlEncode()
  1542. **/
  1543. function n3u_UrlEncode($string){ // used on urls
  1544. $url_entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');
  1545. $url_replacements = array('!', '*', '\'', '(', ')', ';', ':', '@', '&amp;', '=', '+', '$', ',', '/', '?', '%', '#', '[', ']');
  1546. return str_replace($url_entities, $url_replacements, urlencode($string));
  1547. }
  1548. /**
  1549. n3u_VersionChecker() does a domain text record lookup on n3u.com and
  1550. finds whatever is listed as the current version and compares this
  1551. against the active script version.
  1552. **/
  1553. function n3u_VersionChecker(){
  1554. global $n3u_configVars;
  1555. if($n3u_configVars['caching'] == FALSE){
  1556. $txt_records = dns_get_record('n3u.com', DNS_TXT); // get all txt records
  1557. foreach($txt_records as $txt_record => $txt_values){ // Parse records
  1558. $txt_string = strstr($txt_values['txt'],'=', TRUE); // Set anything before the equal sign as the string to search
  1559. if($txt_string == 'nns-version'){ // Was the string returned nns-version?
  1560. return(str_replace('nns-version=','',$txt_values['txt'])); // Version only is returned
  1561. }
  1562. }
  1563. }else{
  1564. $n3u_cacheFilePath = $n3u_configVars['cache_dir'] . 'version.txt'; // Set cache file name
  1565. if(file_exists($n3u_cacheFilePath) && time() - $n3u_configVars['lifetime'] < filemtime($n3u_cacheFilePath)){ // else if a recent cache file exist
  1566. // Already exist and is recent so do nothing.
  1567. return file_get_contents($n3u_cacheFilePath);
  1568. }else{
  1569. // Not recent or does not exist
  1570. ob_start();
  1571. $txt_records = dns_get_record('n3u.com', DNS_TXT); // get all txt records
  1572. foreach($txt_records as $txt_record => $txt_values){ // Parse records
  1573. $txt_string = strstr($txt_values['txt'],'=', TRUE); // Set anything before the equal sign as the string to search
  1574. if($txt_string == 'nns-version'){ // Was the string returned nns-version?
  1575. echo str_replace('nns-version=','',$txt_values['txt']);
  1576. }
  1577. }
  1578. $n3u_cacheFileDump = ob_get_contents(); // Dump contents as var
  1579. ob_end_clean(); // Stop & flush buffer
  1580. $n3u_cacheFileName = fopen($n3u_cacheFilePath, 'w'); // open as writeable
  1581. fwrite($n3u_cacheFileName, $n3u_cacheFileDump); // Write the file from var
  1582. fclose($n3u_cacheFileName); // Close file
  1583. unset($n3u_cacheFileName,$txt_records);
  1584. return $n3u_cacheFileDump;
  1585. }
  1586. }
  1587. }
  1588. /**
  1589. n3u_WriteConfig() is used to write the current settings of
  1590. $n3u_configVars to a domain-based configuration file.
  1591. **/
  1592. function n3u_WriteConfig($domainConfig = NULL){
  1593. global $n3u_configArgs;
  1594. global $n3u_configVars;
  1595. $n3u_configVars = filter_var_array($n3u_configVars, $n3u_configArgs);
  1596. $url = n3u_HTTP_Host();
  1597. if(!isset($domainConfig) || $domainConfig == NULL){
  1598. $domainConfig = $url['host'].'_config.php';
  1599. }
  1600. $configFile = '<?php ' . PHP_EOL
  1601. . "\t" . '/**' . PHP_EOL
  1602. . n3u_GPL_Credits()
  1603. . "\t\t" . 'n3u Niche Store - '.$url['host'].'_config.php' . PHP_EOL
  1604. . "\t\t\t" . 'To customize n3u Niche Store, a little configuration is required.' . PHP_EOL
  1605. . "\t\t\t" . 'It\'s best to configure n3u Niche Store from the Admin Panel' . PHP_EOL
  1606. . "\t\t\t" . 'However, Each option below is usually self explanatory.' . PHP_EOL
  1607. . "\t\t\t" . 'Don\'t use a \' character in any option unless you know how to escape properly.' . PHP_EOL
  1608. . "\t\t\t" . 'Again it\'s best to use Admin Panel which is safer.' . PHP_EOL
  1609. . "\t" . '**/' . PHP_EOL
  1610. . "\t" . 'if(!defined(\'n3u\')){die(\'Direct access is not permitted.\');} // Is n3u defined?' . PHP_EOL
  1611. . "\t" . '$n3u_configData = array( // Visit the admin section at ?x=admin to change settings.' . PHP_EOL
  1612. . "\t\t" . '\'accessKey\' => \'' . $n3u_configVars['accessKey'] . '\',' . PHP_EOL
  1613. . "\t\t" . '\'api_key\' => \'' . $n3u_configVars['api_key'] . '\',' . PHP_EOL
  1614. . "\t\t" . '\'blocks_dir\' => \'' . str_replace('//','/',$n3u_configVars['blocks_dir']) . '\',' . PHP_EOL
  1615. . "\t\t" . '\'cache_dir\' => \'' . str_replace('//','/',$n3u_configVars['cache_dir']) . '\',' . PHP_EOL
  1616. . "\t\t" . '\'cacheImgs\' => \'' . ($n3u_configVars['cacheImgs'] ? 'true' : 'false') . '\',' . PHP_EOL
  1617. . "\t\t" . '\'caching\' => \'' . ($n3u_configVars['caching'] ? 'true' : 'false') . '\',' . PHP_EOL
  1618. . "\t\t" . '\'Categories\' => \'' . $n3u_configVars['Categories'] . '\',' . PHP_EOL
  1619. . "\t\t" . '\'CategoryFilters\' => \'' . str_replace(array(' , ',', ',',',' | ','| ',' '),array('|','|','|','|','|','%20'),$n3u_configVars['CategoryFilters']) . '\',' . PHP_EOL
  1620. . "\t\t" . '\'CleanUrls\' => \'' . ($n3u_configVars['CleanUrls'] ? 'true' : 'false') . '\',' . PHP_EOL
  1621. . "\t\t" . '\'ClearCacheFreq\' => \'' . $n3u_configVars['ClearCacheFreq'] . '\',' . PHP_EOL
  1622. . "\t\t" . '\'ClearImgCacheFreq\' => \'' . $n3u_configVars['ClearImgCacheFreq'] . '\',' . PHP_EOL
  1623. . "\t\t" . '\'commissionDateMonths\' => \'' . $n3u_configVars['commissionDateMonths'] . '\',' . PHP_EOL
  1624. . "\t\t" . '\'debug\' => \'' . ($n3u_configVars['debug'] ? 'true' : 'false') . '\',' . PHP_EOL
  1625. . "\t\t" . '\'defaultKeyword\' => \'' . $n3u_configVars['defaultKeyword'] . '\',' . PHP_EOL
  1626. . "\t\t" . '\'defaultLanguage\' => \'' . $n3u_configVars['defaultLanguage'] . '\',' . PHP_EOL
  1627. . "\t\t" . '\'enableCoupons\' => \'' . ($n3u_configVars['enableCoupons'] ? 'true' : 'false') . '\',' . PHP_EOL
  1628. . "\t\t" . '\'enableFacets\' => \'' . ($n3u_configVars['enableFacets'] ? 'true' : 'false') . '\',' . PHP_EOL
  1629. . "\t\t" . '\'enableJsonCompression\' => \'' . ($n3u_configVars['enableJsonCompression'] ? 'true' : 'false') . '\',' . PHP_EOL
  1630. . "\t\t" . '\'enableQuerySuggestion\' => \'' . ($n3u_configVars['enableQuerySuggestion'] ? 'true' : 'false') . '\',' . PHP_EOL
  1631. . "\t\t" . '\'img_dir\' => \'' . str_replace('//','/',$n3u_configVars['img_dir']) . '\',' . PHP_EOL
  1632. . "\t\t" . '\'img_size\' => \'' . $n3u_configVars['img_size'] . '\',' . PHP_EOL
  1633. . "\t\t" . '\'include_dir\' => \'' . str_replace('//','/',$n3u_configVars['include_dir']) . '\',' . PHP_EOL
  1634. . "\t\t" . '\'jScroll\' => \'' . ($n3u_configVars['jScroll'] ? 'true' : 'false') . '\',' . PHP_EOL
  1635. . "\t\t" . '\'language_dir\' => \'' . str_replace('//','/',$n3u_configVars['language_dir']) . '\',' . PHP_EOL
  1636. . "\t\t" . '\'lifetime\' => \'' . $n3u_configVars['lifetime'] . '\',' . PHP_EOL
  1637. . "\t\t" . '\'logging\' => \'' . $n3u_configVars['logging'] . '\',' . PHP_EOL
  1638. . "\t\t" . '\'limit\' => \'' . $n3u_configVars['limit'] . '\',' . PHP_EOL
  1639. . "\t\t" . '\'msg_dir\' => \'' . str_replace('//','/',$n3u_configVars['msg_dir']) . '\',' . PHP_EOL
  1640. . "\t\t" . '\'password\' => \'' . $n3u_configVars['password'] . '\',' . PHP_EOL
  1641. . "\t\t" . '\'Prosperent_Endpoint\' => \'' . $n3u_configVars['Prosperent_Endpoint'] . '\',' . PHP_EOL
  1642. . "\t\t" . '\'Prosperent_UserID\' => \'' . $n3u_configVars['Prosperent_UserID'] . '\',' . PHP_EOL
  1643. . "\t\t" . '\'reCaptcha_privKey\' => \'' . $n3u_configVars['reCaptcha_privKey'] . '\',' . PHP_EOL
  1644. . "\t\t" . '\'reCaptcha_pubKey\' => \'' . $n3u_configVars['reCaptcha_pubKey'] . '\',' . PHP_EOL
  1645. . "\t\t" . '\'SiteEmail\' => \'' . $n3u_configVars['SiteEmail'] . '\',' . PHP_EOL
  1646. . "\t\t" . '\'SiteIndex\' => \'' . $n3u_configVars['SiteIndex'] . '\',' . PHP_EOL
  1647. . "\t\t" . '\'SiteName\' => \'' . $n3u_configVars['SiteName'] . '\',' . PHP_EOL
  1648. . "\t\t" . '\'SiteURL\' => \'' . $n3u_configVars['SiteURL'] . '\',' . PHP_EOL
  1649. . "\t\t" . '\'Supporter\' => \'' . $n3u_configVars['Supporter'] . '\',' . PHP_EOL
  1650. . "\t\t" . '\'Template_Dir\' => \'' . str_replace('//','/',$n3u_configVars['Template_Dir']) . '\',' . PHP_EOL
  1651. . "\t\t" . '\'Template_Name\' => \'' . $n3u_configVars['Template_Name'] . '\',' . PHP_EOL
  1652. . "\t\t" . '\'username\' => \'' . $n3u_configVars['username'] . '\',' . PHP_EOL
  1653. . "\t" . '); // n3u Niche Store is brought to you by n3u.com' . PHP_EOL
  1654. . '?>';
  1655. $fp = fopen($domainConfig, "w");
  1656. fwrite($fp, $configFile);
  1657. fclose($fp);
  1658. unset($configFile,$url);
  1659. }
  1660. /**
  1661. n3u_WriteLanguage() is used to write language changes to a custom
  1662. language file.
  1663. **/
  1664. function n3u_WriteLanguage($n3u_lang_changes = NULL,$resetlang = FALSE){
  1665. global $n3u_inputVars;
  1666. global $n3u_configVars;
  1667. if(isset($n3u_lang_changes) && $n3u_lang_changes != NULL){
  1668. $langFile = '<?php ' . PHP_EOL
  1669. . "\t" . '/**' . PHP_EOL
  1670. . n3u_GPL_Credits()
  1671. . "\t\t" . 'Custom Language File' . PHP_EOL
  1672. . "\t\t\t" . 'When you edit a language value in the admin panel, it\'s written to' . PHP_EOL
  1673. . "\t\t\t" . 'a custom file. This retains your changes as n3u Niche Store is updated.' . PHP_EOL
  1674. . "\t\t\t" . 'Don\'t use a \' character in any option unless you know how to escape properly.' . PHP_EOL
  1675. . "\t\t\t" . 'Again it\'s best to use Admin Panel which is safer.' . PHP_EOL
  1676. . "\t" . '**/' . PHP_EOL
  1677. . "\t" . 'if(!defined(\'n3u\')){die(\'Direct access is not permitted.\');} // Is n3u defined?' . PHP_EOL
  1678. . "\t" . '$n3u_lang_custom = array( // Visit the admin section at ?x=admin&page=language to change settings.' . PHP_EOL;
  1679. foreach($n3u_lang_changes as $n3u_lang_change_key => $n3u_lang_change_value){
  1680. if($n3u_lang_change_key != 'lang'){
  1681. $langFile .= "\t\t" . '\'' . $n3u_lang_change_key . '\' => \'' . addslashes($n3u_lang_change_value) . '\',' . PHP_EOL;
  1682. }
  1683. }
  1684. $langFile .= "\t" . '); // n3u Niche Store is brought to you by n3u.com' . PHP_EOL
  1685. . '?>';
  1686. $fp = fopen($n3u_configVars['language_dir'] . $n3u_inputVars['lang'] . '/' . $n3u_inputVars['lang'] . '_custom.php', "w");
  1687. fwrite($fp, $langFile);
  1688. fclose($fp);
  1689. unset($langFile);
  1690. }elseif(isset($resetlang) && $resetlang == TRUE){
  1691. unlink($n3u_configVars['language_dir'] . $n3u_inputVars['lang'] . '/' . $n3u_inputVars['lang'] . '_custom.php');
  1692. unset($resetlang);
  1693. }
  1694. }
  1695. function n3u_WriteMessage($name = NULL, $email = NULL, $subject = NULL,$description = NULL){
  1696. global $n3u_inputVars;
  1697. global $n3u_configVars;
  1698. $langFile = '<?php ' . PHP_EOL
  1699. . "\t" . '/**' . PHP_EOL
  1700. . n3u_GPL_Credits()
  1701. . "\t\t" . 'Message' . PHP_EOL
  1702. . "\t\t\t" . 'When a visitor attempts to contact you, this message is created' . PHP_EOL
  1703. . "\t\t\t" . 'so that you can view it later from the Administration Panel.' . PHP_EOL
  1704. . "\t\t\t" . 'Should you wish to delete it, you may do so there or simply by' . PHP_EOL
  1705. . "\t\t\t" . 'deleting this file directly. The message details are encrypted' . PHP_EOL
  1706. . "\t\t\t" . 'with base64 encoding and are automatically decoded from the' . PHP_EOL
  1707. . "\t\t\t" . 'Administration Panel.' . PHP_EOL
  1708. . "\t" . '**/' . PHP_EOL
  1709. . "\t" . 'if(!defined(\'n3u\')){die(\'Direct access is not permitted.\');} // Is n3u defined?' . PHP_EOL
  1710. . "\t" . '$n3u_Messages = array( // Visit the admin section at ?x=admin&page=messages to read messages.' . PHP_EOL
  1711. . "\t\t" . '\'Name\' => \'' . addslashes(base64_encode($name)) . '\',' . PHP_EOL
  1712. . "\t\t" . '\'Email\' => \'' . addslashes(base64_encode($email)) . '\',' . PHP_EOL
  1713. . "\t\t" . '\'Subject\' => \'' . addslashes($subject) . '\',' . PHP_EOL
  1714. . "\t\t" . '\'Description\' => \'' . addslashes(base64_encode($description)) . '\',' . PHP_EOL
  1715. . "\t\t" . '\'SiteURL\' => \'' . addslashes($n3u_configVars['SiteURL']) . '\',' . PHP_EOL
  1716. . "\t\t" . '\'VisitorIP\' => \'' . $n3u_configVars['visitor_ip'] . '\',' . PHP_EOL;
  1717. $langFile .= "\t" . '); // n3u Niche Store is brought to you by n3u.com' . PHP_EOL
  1718. . '?>';
  1719. $fp = fopen($n3u_configVars['msg_dir'] . 'msg_' .md5($name.$email.$description). '.php', "w");
  1720. fwrite($fp, $langFile);
  1721. fclose($fp);
  1722. unset($langFile);
  1723. }
  1724. /**
  1725. n3u_WriteCustomPage() is used to create blank custom pages.
  1726. **/
  1727. function n3u_WriteCustomPage($customPage_name = NULL,$admin_only = FALSE){
  1728. global $n3u_inputVars;
  1729. global $n3u_configVars;
  1730. if(isset($customPage_name) && $customPage_name != NULL){
  1731. $url = n3u_HTTP_Host();
  1732. $customPage = '<?php ' . PHP_EOL
  1733. . "\t" . '/**' . PHP_EOL
  1734. . n3u_GPL_Credits()
  1735. . "\t\t" . 'Custom Page' . PHP_EOL
  1736. . "\t\t\t" . 'Custom pages are loaded within the content div. This means they' . PHP_EOL
  1737. . "\t\t\t" . 'share the header, sides and footer with other pages. Simply edit' . PHP_EOL
  1738. . "\t\t\t" . 'this page template to customise and build your page. You have' . PHP_EOL
  1739. . "\t\t\t" . 'access to many functions found in n3u.php as well as the' . PHP_EOL
  1740. . "\t\t\t" . '$n3u_configVars $n3u_inputVars and $n3u_lang arrays. You may' . PHP_EOL
  1741. . "\t\t\t" . 'enable debugging to see the data provided by those arrays.' . PHP_EOL
  1742. . "\t\t\t" . '' . PHP_EOL
  1743. . "\t" . '**/' . PHP_EOL
  1744. . "\t" . 'if(!defined(\'n3u\')){die(\'Direct access is not permitted.\');} // Is n3u defined?' . PHP_EOL;
  1745. if(isset($admin_only) && $admin_only == TRUE){
  1746. $customPage .= "\t" . 'if(!defined(\'admin\')){ // if not admin, forbidden error' . PHP_EOL
  1747. . "\t\t" . 'n3u_Error(403,\'Not logged into admin.\'); // returns forbidden error and headers with formatting, plus gives a custom message' . PHP_EOL
  1748. . "\t" . '}else{ // if admin, do below . PHP_EOL;' . PHP_EOL
  1749. . "\t\t" . 'echo "\t\t\t\t" . \'<div id="\' . n3u_IdCleaner($customPage_name) . \'">\' . PHP_EOL' . PHP_EOL
  1750. . "\t\t" . '. "\t\t\t\t\t" . \'<h3>\' . $customPage_name . \'</h3>\' . PHP_EOL' . PHP_EOL
  1751. . "\t\t" . '. "\t\t\t\t\t" . \'<hr />\' . PHP_EOL' . PHP_EOL
  1752. . "\t\t" . '. "\t\t\t\t\t" . \'<p>Your custom content would begin here.</p>\' . PHP_EOL' . PHP_EOL
  1753. . "\t\t" . '. "\t\t\t\t" . \'</div>\' . PHP_EOL; // div' . PHP_EOL
  1754. . "\t" . '} . PHP_EOL' . PHP_EOL
  1755. . "\t" . '// n3u Niche Store is brought to you by n3u.com' . PHP_EOL
  1756. . '?>' . PHP_EOL;
  1757. }else{
  1758. $customPage .= "\t" . 'echo "\t\t\t\t" . \'<div id="' . n3u_IdCleaner($customPage_name) . '">\' . PHP_EOL' . PHP_EOL
  1759. . "\t" . '. "\t\t\t\t\t" . \'<h3>' . $customPage_name . '</h3>\' . PHP_EOL' . PHP_EOL
  1760. . "\t" . '. "\t\t\t\t\t" . \'<hr />\' . PHP_EOL' . PHP_EOL
  1761. . "\t" . '. "\t\t\t\t\t" . \'<p>Your custom content would begin here.</p>\' . PHP_EOL' . PHP_EOL
  1762. . "\t" . '. "\t\t\t\t" . \'</div>\' . PHP_EOL; // div' . PHP_EOL
  1763. . "\t" . '// n3u Niche Store is brought to you by n3u.com' . PHP_EOL
  1764. . '?>' . PHP_EOL;
  1765. }
  1766. $fp = fopen($n3u_configVars['include_dir'] . 'custom/'.$url['host'].'_' . $customPage_name . '.php', "w");
  1767. fwrite($fp, $customPage);
  1768. fclose($fp);
  1769. unset($langFile,$url);
  1770. }
  1771. }
  1772. /**
  1773. preprint_r($val) simply wraps the results of print_r() in <pre> tags.
  1774. **/
  1775. function preprint_r($val){
  1776. echo '<pre>' . PHP_EOL;
  1777. print_r($val) . PHP_EOL;
  1778. echo '</pre>' . PHP_EOL;
  1779. }
  1780. if(is_file($n3u_configVars['include_dir'] . 'custom_functions.php')){
  1781. require_once($n3u_configVars['include_dir'] . 'custom_functions.php'); // Get user-defined functions
  1782. }else{
  1783. $CustomFile =
  1784. '<?php ' . PHP_EOL
  1785. . "\t" . '/**' . PHP_EOL
  1786. . n3u_GPL_Credits()
  1787. . "\t\t" . 'NOTES:' . PHP_EOL
  1788. . "\t\t\t" . 'Custom functions file' . PHP_EOL
  1789. . "\t\t\t\t" . 'Keep functions seperate to reduce upgrade headaches' . PHP_EOL
  1790. . "\t\t\t\t" . 'Block-based functions should be stored within corresponding block files' . PHP_EOL . PHP_EOL
  1791. . "\t\t\t" . 'You may interact with the following arrays by calling them globally:' . PHP_EOL
  1792. . "\t\t\t\t" . '$n3u_configVars - Stores all configuration related options' . PHP_EOL
  1793. . "\t\t\t\t" . '$n3u_inputVars - Stores all form input related options' . PHP_EOL
  1794. . "\t\t\t\t" . '$n3u_lang - Stores all language related options' . PHP_EOL
  1795. . "\t\t\t\t" . '$n3u_results - Stores all result information (search page)' . PHP_EOL
  1796. . "\t" . '**/' . PHP_EOL
  1797. . "\t" . 'if(!defined(\'n3u\')){die(\'Direct access is not permitted.\');} // Is n3u defined?' . PHP_EOL
  1798. . "\t" . '// Enter any custom functions you have below the line.' . PHP_EOL . PHP_EOL
  1799. . '?>';
  1800. $fp = fopen($n3u_configVars['include_dir'] . 'custom_functions.php', "w");
  1801. fwrite($fp, $CustomFile);
  1802. fclose($fp);
  1803. unset($CustomFile);
  1804. }
  1805. ?>