PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_kaprayertimes/models/uploaddata.php

https://github.com/khawaib/KA-Prayer-Times
PHP | 281 lines | 229 code | 15 blank | 37 comment | 25 complexity | 3d97f713030214617b0c8ba0b2371770 MD5 | raw file
  1. <?php
  2. /**
  3. * @package KA Prayer Times
  4. * @author Khawaib Ahmed http://www.khawaib.co.uk
  5. * @copyright Copyright (C) 2007 - 2010 Khawaib Ahmed - http://www.khawaib.co.uk
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. // no direct access
  9. defined('_JEXEC') or die('Restricted access');
  10. // Import Joomla! libraries
  11. jimport('joomla.application.component.model');
  12. class KaprayertimesModelUploaddata extends JModel {
  13. function __construct() {
  14. parent::__construct();
  15. $array = JRequest::getVar('cid', 0, '', 'array');
  16. $this->setId((int)$array[0]);
  17. }
  18. function setId($id) {
  19. $this->_id = $id;
  20. $this->_data = null;
  21. }
  22. function &getData() {
  23. if (empty( $this->_data )) {
  24. $query = $this->_buildQuery();
  25. $this->_db->setQuery( $query );
  26. $this->_data = $this->_db->loadObject();
  27. }
  28. if (!$this->_data) {
  29. $this->_data = new stdClass();
  30. $this->_data->id = 0;
  31. $this->_data->quran_id = 0;
  32. $this->_data->published = 1;
  33. $this->_data->filename = null;
  34. $this->_data->size = null;
  35. $this->_data->ordering = null;
  36. $this->_data->createdate = null;
  37. $this->_data->language = null;
  38. }
  39. return $this->_data;
  40. }
  41. function _buildQuery() {
  42. $query = ' SELECT *'
  43. . ' FROM #__kaprayertimes'
  44. . ' WHERE id = '.$this->_id;
  45. return $query;
  46. }
  47. function store() {
  48. $row =& $this->getTable();
  49. $data = JRequest::get( 'post' );
  50. $file = JRequest::getVar('file', null, 'files', 'array' );
  51. $filename = $file['name'];
  52. if ($this->upload($data)) {
  53. // Bind the form fields to the table
  54. if (!$row->bind($data)) {
  55. $this->setError($this->_db->getErrorMsg());
  56. return false;
  57. }
  58. // Make sure the record is valid
  59. if (!$row->check()) {
  60. $this->setError($this->_db->getErrorMsg());
  61. return false;
  62. }
  63. // Store the table to the database
  64. if (!$row->store()) {
  65. $this->setError($this->_db->getErrorMsg());
  66. return false;
  67. }
  68. return true;
  69. }
  70. }
  71. // function get_string_between($string, $start, $end) {
  72. // $string = " ".$string;
  73. // $ini = strpos($string,$start);
  74. // if ($ini == 0) return "";
  75. // $ini += strlen($start);
  76. // $len = strpos($string,$end,$ini) - $ini;
  77. // return substr($string,$ini,$len);
  78. // }
  79. function upload($data) {
  80. $app = JFactory::getApplication();
  81. $option = JRequest::getVar('option');
  82. set_time_limit(0);
  83. $database =& JFactory::getDBO();
  84. $file = JRequest::getVar('file', null, 'files', 'array' );
  85. $filename = strtolower($file['name']);
  86. //$uploadPath = JPATH_SITE.DS.'administrator'.DS.'components'.DS.$option.DS.'uploads'.DS;
  87. $uploadPath = JPATH_COMPONENT . DS . 'uploads' . DS;
  88. if (!$filename) {
  89. $app->redirect("index.php?option=$option&view=uploaddata", JText::_('PLEASE_SELECT_A_FILE'));
  90. return;
  91. }
  92. if (!is_writable($uploadPath)){
  93. @chmod($uploadPath,'0755');
  94. if(!is_writable($uploadPath)) {
  95. JError::raiseWarning( 500, JText::_('UPLOAD_FOLDER_NOT_WRITEABLE') . $uploadPath, '');
  96. $app->redirect("index.php?option=$option&view=uploaddata", '');
  97. //$app->redirect("index.php?option=$option", "Upload folder is not writeable: $uploadPath");
  98. }
  99. }
  100. // if (!eregi("(\.(sql|gz))$",$filename)) {
  101. if ( !eregi( "(\.(csv))$", $filename ) ) {
  102. JError::raiseWarning( 500, JText::_('FILE_TYPE_NOT_ALLOWED'), '');
  103. $app->redirect("index.php?option=$option&view=uploaddata", '');
  104. return;
  105. }
  106. if (isset($file) && is_array($file) && $file['name'] != '') {
  107. $pathAndfile = $uploadPath . $filename;
  108. //$pathAndfile = $uploadPath.$filename;
  109. jimport('joomla.filesystem.file');
  110. // if (JFile::exists($pathandfile)) {
  111. // $app->redirect("index.php?option=$option", "File already exists!");
  112. // return;
  113. // }
  114. if (!JFile::upload($file['tmp_name'], $pathAndfile)) {
  115. JError::raiseWarning( 500, JText::_('UPLOAD_FILE_SIZE_RESTRICTION'));
  116. $app->redirect("index.php?option=$option&view=uploaddata", '');
  117. return;
  118. }
  119. if (!$this->parseFile($uploadPath, $filename, $data)) {
  120. JError::raiseWarning( 500, JText::_('CSV_IMPORT_FAILED'), '');
  121. $app->redirect("index.php?option=$option&view=uploaddata", '');
  122. return;
  123. }
  124. }
  125. return true;
  126. }
  127. function parseFile($uploadPath, $filename, $data) {
  128. $app = JFactory::getApplication();
  129. $option = JRequest::getVar('option');
  130. $delimiter=trim(JRequest::getVar('delimiter',','));
  131. if (eregi("(\.(csv))$",$filename)) {
  132. // Initialize variables
  133. $queries = array();
  134. $database =& JFactory::getDBO();
  135. $databaseDriver = strtolower($database->name);
  136. if ($databaseDriver == 'mysqli') {
  137. $databaseDriver = 'mysql';
  138. }
  139. $databaseCharset = ($database->hasUTF()) ? 'utf8' : '';
  140. $fCharset = 'utf8';
  141. $fDriver = 'mysql';
  142. if ( $fCharset == $databaseCharset && $fDriver == $databaseDriver) {
  143. // Get the name of the sql file to process
  144. $csvfile = $uploadPath.$filename;
  145. // Check that sql files exists before reading. Otherwise raise error for rollback
  146. if ( !file_exists( $csvfile ) ) {
  147. JError::raiseWarning( 500, JText::_("FILE_DOES_NOT_EXIST") . $csvfile );
  148. $app->redirect("index.php?option=$option&view=uploaddata", '');
  149. return;
  150. }
  151. /* $buffer = file_get_contents($csvfile);
  152. // Graceful exit and rollback if read not successful
  153. if ( $buffer === false ) {
  154. $app->redirect("index.php?option=$option", "Could not open and read CSV file. Path: $csvfile");
  155. return false;
  156. }*/
  157. // $resFileHandler = fopen ( $csvfile, 'r' );
  158. $arrData = array();
  159. $resFileHandler = fopen ( $csvfile, 'r' );
  160. $count = 0;
  161. while ( $arrData = fgetcsv ( $resFileHandler, 1000, ",","\"" )) {
  162. if ( preg_match( '`^\d{1,2}/\d{1,2}/\d{4}$`', $arrData[1] )) {
  163. $dateArray = explode('/', $arrData[1]);
  164. $date = $dateArray[2]."-".$dateArray[1]."-".$dateArray[0];
  165. // $query = $data['overwriteexisting'] ? 'REPLACE' : 'INSERT IGNORE';
  166. $query = 'INSERT INTO `#__kaprayertimes` SET ';
  167. $query .= sprintf( '`date` = "%s",
  168. `fajr_begins` = "%s",
  169. `fajr_jamaat` = "%s",
  170. `sunrise` = "%s",
  171. `zuhr_begins` = "%s",
  172. `zuhr_jamaat` = "%s",
  173. `asr_begins` = "%s",
  174. `asr_jamaat` = "%s",
  175. `maghrib_begins` = "%s",
  176. `maghrib_jamaat` = "%s",
  177. `isha_begins` = "%s",
  178. `isha_jamaat` = "%s"
  179. ',
  180. date("Y-m-d", strtotime($date)),
  181. mysql_real_escape_string($arrData[2]),
  182. mysql_real_escape_string($arrData[3]),
  183. mysql_real_escape_string($arrData[4]),
  184. mysql_real_escape_string($arrData[5]),
  185. mysql_real_escape_string($arrData[6]),
  186. mysql_real_escape_string($arrData[7]),
  187. mysql_real_escape_string($arrData[8]),
  188. mysql_real_escape_string($arrData[9]),
  189. mysql_real_escape_string($arrData[10]),
  190. mysql_real_escape_string($arrData[11]),
  191. mysql_real_escape_string($arrData[12])
  192. );
  193. $query = trim($query);
  194. /*
  195. * TODO: Improve function to check if prayer times already exisit for a date and overwrite them or skip instead of creating new entry.
  196. */
  197. if ($query != '') {
  198. $database->setQuery($query);
  199. if (!$database->query()) {
  200. // JError::raiseWarning( 500, JText::_('SQL Error')." ".$database->stderr(true));
  201. $app->redirect("index.php?option=$option&view=uploaddata", JText::_('SQL Error')." ".$database->stderr(true));
  202. return false;
  203. }
  204. }
  205. }
  206. $count++;
  207. }
  208. /* // Create an array of queries from the sql file
  209. jimport('joomla.installer.helper');
  210. $queries = JInstallerHelper::splitSql($buffer);
  211. if (count($queries) == 0) {
  212. $app->redirect("index.php?option=$option", "No queries to process. Path: $csvfile");
  213. return;
  214. }
  215. // Process each query in the $queries array (split out of sql file).
  216. foreach ($queries as $query) {
  217. $query = trim($query);
  218. if ($query != '' && $query{0} != '#') {
  219. $database->setQuery($query);
  220. if (!$database->query()) {
  221. JError::raiseWarning(1, 'JInstaller::install: '.JText::_('SQL Error')." ".$database->stderr(true));
  222. return false;
  223. }
  224. }
  225. }*/
  226. }
  227. return true;
  228. // return (int) count($queries);
  229. }
  230. if (eregi("(\.(gz))$",$filename)) {
  231. $csv_insert_table = ''; // Destination table for CSV files
  232. $ajax = false; // AJAX mode: import will be done without refreshing the website
  233. $linespersession = 7000; // Lines to be executed per one import session
  234. $delaypersession = 0; // You can specify a sleep time in milliseconds after each session
  235. // Works only if JavaScript is activated. Use to reduce server overrun
  236. //$file = false;
  237. $error = false;
  238. $gzipmode = true;
  239. $file = $uploadPath.$filename;
  240. $curfilename = $file;
  241. if ((!$gzipmode && !$file=@fopen($curfilename,"rt")) || ($gzipmode && !$file=@gzopen($curfilename,"rt"))) {
  242. $app->redirect("index.php?option=$option", "Can't open ".$filename." for import.");
  243. return;
  244. } else if ((!$gzipmode && @fseek($file, 0, SEEK_END)==0) || ($gzipmode && @gzseek($file, 0)==0)) {
  245. if (!$gzipmode) $filesize = ftell($file);
  246. else $filesize = gztell($filesize);
  247. }
  248. $_REQUEST["start"] = 1;
  249. $_REQUEST["foffset"] = 0;
  250. $_REQUEST["totalqueries"] = 0;
  251. $query = "";
  252. $queries = 0;
  253. $totalqueries = $_REQUEST["totalqueries"];
  254. $linenumber = $_REQUEST["start"];
  255. $querylines = 0;
  256. $inparents = false;
  257. }
  258. }
  259. }
  260. ?>