PageRenderTime 23ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/magmi/plugins/base/datasources/csv/magmi_csvdatasource.php

https://bitbucket.org/jit_bec/shopifine
PHP | 247 lines | 28 code | 9 blank | 210 comment | 1 complexity | 752dba075c02d21dba24e8dca4c70a14 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. require_once("magmi_csvreader.php");
  3. class Magmi_CSVDataSource extends Magmi_Datasource
  4. {
  5. protected $_csvreader;
  6. public function initialize($params)
  7. {
  8. $this->_csvreader=new Magmi_CSVReader();
  9. $this->_csvreader->bind($this);
  10. $this->_csvreader->initialize();
  11. }
  12. public function getAbsPath($path)
  13. {
  14. return abspath($path,$this->getScanDir());
  15. }
  16. public function getScanDir($resolve=true)
  17. {
  18. $scandir=$this->getParam("CSV:basedir","var/import");
  19. if(!isabspath($scandir))
  20. {
  21. $scandir=abspath($scandir,Magmi_Config::getInstance()->getMagentoDir(),$resolve);
  22. }
  23. return $scandir;
  24. }
  25. public function getCSVList()
  26. {
  27. $scandir=$this->getScanDir();
  28. $files=glob("$scandir/*.csv");
  29. return $files;
  30. }
  31. public function getPluginParams($params)
  32. {
  33. $pp=array();
  34. foreach($params as $k=>$v)
  35. {
  36. if(preg_match("/^CSV:.*$/",$k))
  37. {
  38. $pp[$k]=$v;
  39. }
  40. }
  41. return $pp;
  42. }
  43. public function getPluginInfo()
  44. {
  45. return array("name"=>"CSV Datasource",
  46. "author"=>"Dweeves",
  47. "version"=>"1.2");
  48. }
  49. public function getRecordsCount()
  50. {
  51. return $this->_csvreader->getLinesCount();
  52. }
  53. public function getAttributeList()
  54. {
  55. }
  56. public function getRemoteFile($url,$creds=null,$authmode=null)
  57. {
  58. $ch = curl_init($url);
  59. $this->log("Fetching CSV: $url","startup");
  60. //output filename (current dir+remote filename)
  61. $csvdldir=dirname(__FILE__)."/downloads";
  62. if(!file_exists($csvdldir))
  63. {
  64. @mkdir($csvdldir);
  65. @chmod($csvdldir, Magmi_Config::getInstance()->getDirMask());
  66. }
  67. $outname=$csvdldir."/".basename($url);
  68. //open file for writing
  69. if(file_exists($outname))
  70. {
  71. unlink($outname);
  72. }
  73. $fp = fopen($outname, "w");
  74. if($fp==false)
  75. {
  76. throw new Exception("Cannot write file:$outname");
  77. }
  78. if(substr($url,0,4)=="http")
  79. {
  80. $lookup=1;
  81. $lookup_opts= array(CURLOPT_RETURNTRANSFER=>true,
  82. CURLOPT_HEADER=>true,
  83. CURLOPT_NOBODY=>true,
  84. CURLOPT_FOLLOWLOCATION=>true,
  85. CURLOPT_FILETIME=>true,
  86. CURLOPT_CUSTOMREQUEST=>"HEAD");
  87. $dl_opts=array(CURLOPT_FILE=>$fp,
  88. CURLOPT_CUSTOMREQUEST=>"GET",
  89. CURLOPT_HEADER=>false,
  90. CURLOPT_NOBODY=>false,
  91. CURLOPT_FOLLOWLOCATION=>true,
  92. CURLOPT_UNRESTRICTED_AUTH=>true,
  93. CURLOPT_HTTPHEADER=> array('Expect:'));
  94. }
  95. else
  96. {
  97. if(substr($url,0,3)=="ftp")
  98. {
  99. $lookup=0;
  100. $dl_opts=array(CURLOPT_FILE=>$fp);
  101. }
  102. }
  103. if($creds!="")
  104. {
  105. if($lookup!=0)
  106. {
  107. if(substr($url,0,4)=="http")
  108. {
  109. $lookup_opts[CURLOPT_HTTPAUTH]=CURLAUTH_ANY;
  110. $lookup_opts[CURLOPT_UNRESTRICTED_AUTH]=true;
  111. }
  112. $lookup_opts[CURLOPT_USERPWD]="$creds";
  113. }
  114. if(substr($url,0,4)=="http")
  115. {
  116. $dl_opts[CURLOPT_HTTPAUTH]=CURLAUTH_ANY;
  117. $dl_opts[CURLOPT_UNRESTRICTED_AUTH]=true;
  118. }
  119. $dl_opts[CURLOPT_USERPWD]="$creds";
  120. }
  121. if($lookup)
  122. {
  123. //lookup , using HEAD request
  124. $ok=curl_setopt_array($ch,$lookup_opts);
  125. $res=curl_exec($ch);
  126. if($res!==false)
  127. {
  128. $lm=curl_getinfo($ch);
  129. if(curl_getinfo($ch,CURLINFO_HTTP_CODE)!=200)
  130. {
  131. $resp = explode("\n\r\n", $res);
  132. $this->log("http header:<pre>".$resp[0]."</pre>","error");
  133. throw new Exception("Cannot fetch $url");
  134. }
  135. }
  136. else
  137. {
  138. throw Exception("Cannot fetch $url");
  139. }
  140. }
  141. $res=array("should_dl"=>true,"reason"=>"");
  142. if($res["should_dl"])
  143. {
  144. //clear url options
  145. $ok=curl_setopt_array($ch, array());
  146. //Download the file , force expect to nothing to avoid buffer save problem
  147. curl_setopt_array($ch,$dl_opts);
  148. curl_exec($ch);
  149. if(curl_error($ch)!="")
  150. {
  151. $this->log(curl_error($ch),"error");
  152. throw new Exception("Cannot fetch $url");
  153. }
  154. else
  155. {
  156. $lm=curl_getinfo($ch);
  157. $this->log("CSV Fetched in ".$lm['total_time']. "secs","startup");
  158. }
  159. curl_close($ch);
  160. fclose($fp);
  161. }
  162. else
  163. {
  164. curl_close($ch);
  165. //bad file or bad hour, no download this time
  166. $this->log("No dowload , ".$res["reason"],"info");
  167. }
  168. //return the csv filename
  169. return $outname;
  170. }
  171. public function beforeImport()
  172. {
  173. if($this->getParam("CSV:importmode","local")=="remote")
  174. {
  175. $url=$this->getParam("CSV:remoteurl","");
  176. $creds="";
  177. $authmode="";
  178. if($this->getParam("CSV:remoteauth",false)==true)
  179. {
  180. $user=$this->getParam("CSV:remoteuser");
  181. $pass=$this->getParam("CSV:remotepass");
  182. $authmode=$this->getParam("CSV:authmode");
  183. $creds="$user:$pass";
  184. }
  185. $outname=$this->getRemoteFile($url,$creds,$authmode);
  186. $this->setParam("CSV:filename", $outname);
  187. $this->_csvreader->initialize();
  188. }
  189. return $this->_csvreader->checkCSV();
  190. }
  191. public function afterImport()
  192. {
  193. }
  194. public function startImport()
  195. {
  196. $this->_csvreader->openCSV();
  197. }
  198. public function getColumnNames($prescan=false)
  199. {
  200. return $this->_csvreader->getColumnNames($prescan);
  201. }
  202. public function endImport()
  203. {
  204. $this->_csvreader->closeCSV();
  205. }
  206. public function getNextRecord()
  207. {
  208. return $this->_csvreader->getNextRecord();
  209. }
  210. }