PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/baselibs/DataFeed.php

https://github.com/neek-syndk8/OpenBH-Cash-Generator
PHP | 232 lines | 177 code | 14 blank | 41 comment | 49 complexity | 0d487f9305338ed810feac52fa79e954 MD5 | raw file
  1. <?php
  2. /******** Syndk8's OpenBH *********
  3. *
  4. * This program is free software
  5. * licensed under the GPLv2 license.
  6. * You may redistribute it and/or
  7. * modify it under the terms of
  8. * the GPLv2 license (see license.txt)
  9. *
  10. * Warning:
  11. * OpenBH is for educational use
  12. * Use OpenBH at your own risk !
  13. *
  14. * Credits:
  15. * https://www.syndk8.com/openbh/people.html
  16. *
  17. ********************************/
  18. /**
  19. * baselibs/DataFeed.php
  20. * The DataFeed class helps us map keywords, urls etc
  21. * from DataFeeds like the ones from various Affiliate
  22. * Networks either in CSV Format or XML
  23. * For the fmap array check the config/config.php file
  24. *
  25. * @author Neek
  26. * @todo Finalize XML support
  27. * @return obj
  28. */
  29. class DataFeed
  30. {
  31. private $feed = array();
  32. private $fmap = array();
  33. private $xmlobj;
  34. function __construct() {
  35. /* we should not always load the feed ... */
  36. if(OpenBHConf::get('xmlfeed')==true) {
  37. if(!function_exists('simplexml_load_file')){
  38. writeLog("SimpleXML support missing");
  39. return false;
  40. }
  41. $this->fmap = OpenBHConf::get('xmlfeedmapping');
  42. } else {
  43. $this->fmap = OpenBHConf::get('feedmapping');
  44. }
  45. if(!array_key_exists('keyword',$this->fmap)) {
  46. writeLog("Missing 'keyword' in 'feedmapping' (see config..php)");
  47. return false; // ... missing keyword mapping
  48. }
  49. }
  50. private function Load() {
  51. if(OpenBHConf::get('xmlfeed')==true) {
  52. $this->xmlobj = simplexml_load_file('config/kw/open.txt');
  53. } else {
  54. if (($handle = fopen('config/kw/open.txt', 'r')) !== FALSE) {
  55. while(!feof($handle)) {
  56. if (strnatcmp(phpversion(),'5.3.0') >= 0) {
  57. $data = fgetcsv($handle, 10000, OpenBHConf::get('csv_delimiter'), OpenBHConf::get('csv_enclosure'),OpenBHConf::get('csv_escape'));
  58. } else {
  59. $data = fgetcsv($handle, 10000, OpenBHConf::get('csv_delimiter'), OpenBHConf::get('csv_enclosure'));
  60. }
  61. array_push($this->feed,$data);
  62. }
  63. fclose($handle);
  64. }
  65. }
  66. }
  67. private function LoadOnly($amount) {
  68. if(OpenBHConf::get('xmlfeed')==true) {
  69. $this->Load();
  70. } else {
  71. $cnt = 0;
  72. if (($handle = fopen("config/kw/open.txt", "r")) !== FALSE) {
  73. while(!feof($handle)) {
  74. if (strnatcmp(phpversion(),'5.3.0') >= 0) {
  75. $data = fgetcsv($handle, 10000, OpenBHConf::get('csv_delimiter'), OpenBHConf::get('csv_enclosure'),OpenBHConf::get('csv_escape'));
  76. } else {
  77. $data = fgetcsv($handle, 10000, OpenBHConf::get('csv_delimiter'), OpenBHConf::get('csv_enclosure'));
  78. }
  79. if($cnt==$amount) {
  80. break;
  81. }
  82. array_push($this->feed,$data);
  83. $cnt++;
  84. }
  85. fclose($handle);
  86. }
  87. }
  88. }
  89. public function ReturnFirstKeywords($amount) {
  90. $kwArr = array();
  91. $this->LoadOnly($amount);
  92. if(OpenBHConf::get('xmlfeed')==true) {
  93. $cnt = 0;
  94. foreach($this->xmlobj->xpath(sprintf('/%s',$this->fmap['keyword'])) as $kw) {
  95. array_push($kwArr,$kw);
  96. if($cnt++==$amount) {
  97. break;
  98. }
  99. }
  100. } else {
  101. foreach($this->feed as $line) {
  102. if(count($line)==1) {
  103. array_push($kwArr,$line[0]); // we assume that this must be the keyword since this feed only contains one column..
  104. continue;
  105. }
  106. array_push($kwArr,$line[$this->fmap['keyword']]);
  107. }
  108. }
  109. return $kwArr;
  110. }
  111. public function ReturnPrevKw($kw) {
  112. if(count($this->feed)==0 && !is_object($this->xmlobj)) {
  113. $this->Load();
  114. }
  115. if(OpenBHConf::get('xmlfeed')==true) {
  116. /* previous keyword .. */
  117. return false;
  118. } else {
  119. $key = array_search($kw,$this->feed);
  120. if($key==0 || $key==false) {
  121. return false;
  122. }
  123. if(count($this->feed[$key-1])==1) {
  124. return $this->feed[$key-1][0]; // we assume that this must be the keyword since this feed only contains one column..
  125. }
  126. return $this->feed[$key-1][$this->fmap['keyword']];
  127. }
  128. }
  129. public function ReturnNextKw($kw) {
  130. if(count($this->feed)==0 && !is_object($this->xmlobj)) {
  131. $this->Load();
  132. }
  133. if(OpenBHConf::get('xmlfeed')==true) {
  134. /* next keyword .. */
  135. return false;
  136. } else {
  137. $key = array_search($kw,$this->feed);
  138. if($key==(count($this->feed)-1) || $key==false) {
  139. return false;
  140. }
  141. if(count($this->feed[$key+1])==1) {
  142. return $this->feed[$key+1][0]; // we assume that this must be the keyword since this feed only contains one column..
  143. }
  144. return $this->feed[$key+1][$this->fmap['keyword']];
  145. }
  146. }
  147. public function ReturnRandomEntries($num) {
  148. if(count($this->feed)==0) {
  149. $this->Load();
  150. }
  151. $cnt = count($this->feed);
  152. if($num>$cnt) {
  153. $num = $cnt;
  154. }
  155. $ret = array();
  156. while(count($ret)<$num) {
  157. $rnd = rand(0,$cnt);
  158. if(count($this->feed[$rnd])==1) {
  159. if(!is_numeric(array_search($this->feed[$rnd][0], $ret))) {
  160. array_push($ret,$this->feed[$rnd][0]);
  161. }
  162. continue;
  163. }
  164. if(!is_numeric(array_search($this->feed[$rnd][$this->fmap['keyword']], $ret))) {
  165. array_push($ret,$this->feed[$rnd][$this->fmap['keyword']]);
  166. }
  167. }
  168. return $ret;
  169. }
  170. public function ParseFeed($kw) {
  171. /*
  172. * this needs to be configured inside the config
  173. * column to var mapping
  174. * separator
  175. *
  176. * this needs to be an associative array like this:
  177. * the key will be used as token so you can map anything you like ;)
  178. *
  179. * array('prodname'=>12,'keyword'=>2,'url',3);
  180. */
  181. $row = 1;
  182. $result = null;
  183. if (($handle = fopen("config/kw/open.txt", "r")) !== FALSE) {
  184. while(!feof($handle)) {
  185. if (strnatcmp(phpversion(),'5.3.0') >= 0) {
  186. $data = fgetcsv($handle, 10000, OpenBHConf::get('csv_delimiter'), OpenBHConf::get('csv_enclosure'),OpenBHConf::get('csv_escape'));
  187. } else {
  188. $data = fgetcsv($handle, 10000, OpenBHConf::get('csv_delimiter'), OpenBHConf::get('csv_enclosure'));
  189. }
  190. if(count($data)==1) { // we assume there is only the keyword since we have only one column in the feed
  191. if(str_replace('-',' ',strtolower($data[0]))!=strtolower($kw)) {
  192. continue;
  193. }
  194. } else if(str_replace('-',' ',strtolower($data[$this->fmap['keyword']]))!=strtolower($kw)) {
  195. continue;
  196. }
  197. $result = $data;
  198. break; // $data match
  199. }
  200. fclose($handle);
  201. }
  202. // map col<>map
  203. $linemap = null;
  204. if(count($result)==1) {
  205. $linemap['keyword'] = $result;
  206. } else if(is_array($result)) {
  207. $lineMap = array();
  208. foreach($this->fmap as $k=>$v) {
  209. if(count($data)<$v) {
  210. continue;
  211. }
  212. $lineMap[$k] = $data[$v];
  213. }
  214. }
  215. return $lineMap;
  216. }
  217. }
  218. ?>