mosDBTable('#__share_categorys', 'id', $db); } } class shareFilesTable extends mosDBTable { var $id = null; var $users_id=null; var $share_categorys_id=null; var $title = null; var $description = null; var $name = null; var $published = null; var $credit = null; var $credit_upload = null; var $date = null; function shareFilesTable(&$db) { $this->mosDBTable('#__share_files', 'id', $db); } } class shareLogsTable extends mosDBTable { var $id = null; var $users_id = null; var $share_files_id = null; var $date = null; var $operation = null; var $credit = null; function shareLogsTable(&$db) { $this->mosDBTable('#__share_logs', 'id', $db); } } class shareUsersTable extends mosDBTable { var $id = null; var $users_id = null; var $credit = null; function shareUsersTable(&$db) { $this->mosDBTable('#__share_users', 'id', $db); } } class usersTable extends mosDBTable { var $id = null; var $name = null; var $username = null; var $email = null; var $usertype = null; //var $ = null; function usersTable(&$db) { $this->mosDBTable('#__users', 'id', $db); } } class Download_File { function download($file, $newName) { // Decode URL sent from download page //$file = "$BASEPATH/".urldecode($_GET['file']); //$file = "upload/7271b39fed21136ad67824b272cd0f10.pdf"; // Transfer file in chunks to preserve memory on the server //$fn = basename($file); $fn = $newName; // Fix [3164] while (@ob_end_clean()); // required for IE, otherwise Content-disposition is ignored if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); } header("Pragma: public"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Expires: 0"); header("Content-Transfer-Encoding: binary"); header('Content-Type: application/octet-stream'); header("Content-Disposition: attachment; filename=\"$fn\""); header('Content-Length: '.filesize($file)); $this->readfile_chunked($file, false); //die; } function readfile_chunked($filename,$retbytes=true) { $chunksize = 1*(512*1024); // how many bytes per chunk $buffer = ''; $cnt =0; $handle = fopen($filename, 'rb'); if ($handle === false) { return false; } while (!feof($handle)) { $buffer = fread($handle, $chunksize); echo $buffer; flush(); if ($retbytes) { $cnt += strlen($buffer); } } $status = fclose($handle); if ($retbytes && $status) { return $cnt; // return num. bytes delivered like readfile() does. } return $status; } } function uploadFile($file,$new_name,$dir) { $target_path =$dir; if (!is_dir($target_path) && strlen($target_path)>0) mkdir($target_path, 0755, true); $fileName=$file['name']; $ext=strtolower(end(explode('.',$fileName))); $saved_file_path = $target_path.$new_name.'.'.$ext; if(move_uploaded_file($file['tmp_name'], ($saved_file_path))) { return $saved_file_path; } else return null; } //if user not exist creatre on function checkIfUserExist($id) { global $database; $database->setQuery("SELECT * FROM #__share_users WHERE users_id=".$id); $users = $database->loadObjectList(); if(!count($users)>0) { $user = new shareUsersTable($database); $user->users_id=$id; $user->credit=0; $user -> store(); } } function updateCreditsAfterDownload($id_file) { global $database, $my; $database->setQuery("UPDATE #__share_users as t1,#__share_files as t2, #__users as t3 SET t1.credit=t1.credit-t2.credit WHERE t1.users_id =t3.id AND t3.id=$my->id AND t2.id = $id_file"); $database->query(); $database->setQuery("UPDATE #__share_users as t1,#__share_files as t2, #__users as t3 SET t1.credit=t1.credit+t2.credit WHERE t1.users_id =t3.id AND t3.id=t2.users_id AND t2.id = $id_file"); $database->query(); } function hasSubCategorys($id) { global $database; $database->setQuery("SELECT * FROM #__share_categorys WHERE id_parent=$id"); $subCategorys = $database->loadObjectList(); if(count($subCategorys)>0) { return true; } return false; } function getCategorys() { global $database; $database->setQuery("SELECT * FROM #__share_categorys"); $categorys = $database->loadObjectList(); return $categorys; } function countSubcategory($id) { global $database; $database->setQuery("SELECT * FROM #__share_categorys WHERE id_parent=$id"); $subCategorys = $database->loadObjectList(); return count($subCategorys); } function countFilesCategory($id) { global $database; $database->setQuery("SELECT * FROM #__share_files WHERE share_categorys_id = $id"); $files = $database->loadObjectList(); return count($files); } function countLastPublishedFilesCategory($id, $days) { global $database; $query = "SELECT * FROM #__share_files WHERE published=1 AND share_categorys_id IN($id ".getSubCategorys($id).") AND HOUR(TIMEDIFF(NOW(), date))/24<=$days "; //echo $query."
"; $database->setQuery($query); $files = $database->loadObjectList(); $count = count($files); /*$database->setQuery("SELECT * FROM #__share_categorys WHERE id_parent = $id"); //echo ("SELECT * FROM #__share_categorys WHERE id_parent = $id
"); $categorys = $database->loadObjectList(); foreach($categorys as $category) { $count += countLastPublishedFilesCategory($category->id, $days); }*/ return $count; } function countPublishedFilesCategory($id) { global $database; $database->setQuery("SELECT * FROM #__share_files WHERE published=1 AND share_categorys_id = $id"); $files = $database->loadObjectList(); $count = count($files); $database->setQuery("SELECT * FROM #__share_categorys WHERE id_parent = $id"); //echo ("SELECT * FROM #__share_categorys WHERE id_parent = $id
"); $categorys = $database->loadObjectList(); foreach($categorys as $category) { $count += countPublishedFilesCategory($category->id); } return $count; } function getSubCategorys($id) { global $database; $subcategorys = ""; $database->setQuery("SELECT * FROM #__share_categorys WHERE id_parent = $id"); //echo ("SELECT * FROM #__share_categorys WHERE id_parent = $id
"); $categorys = $database->loadObjectList(); if(count($categorys) >0) { foreach($categorys as $category) { $subcategorys .= ", ".$category->id; $subcategorys .= getSubCategorys($category->id); } } return $subcategorys; } function getLeafCategorys() { global $database; $database->setQuery("SELECT * FROM #__share_categorys WHERE id NOT IN (SELECT DISTINCT id_parent FROM #__share_categorys t1 WHERE id_parent!=0)"); $leafCategorys = $database->loadObjectList(); return $leafCategorys; } function payFile($id) { global $database, $my; $file = new shareFilesTable($database); $file->load($id); checkIfUserExist($file->users_id); $database->setQuery("SELECT * FROM #__share_logs WHERE share_files_id=$id AND operation='pay' ORDER BY id DESC"); $pays = $database->loadObjectList(); if(count($pays)==0) { $database->setQuery("UPDATE #__share_users t1, #__share_files t2 SET t1.credit=t1.credit+t2.credit_upload WHERE t1.users_id=t2.users_id AND t2.id=$id"); $database->query(); $date = date("Y-m-d H:i:s"); $log = new shareLogsTable($database); $log->users_id = $my->id; $log->share_files_id = $id; $log->date = $date; $log->operation = 'pay'; $log->credit = $file->credit_upload; // store it in the db $log -> store(); } else { $pay = $pays[0]; if($pay->credit!=$file->credit_upload) { $database->setQuery("UPDATE #__share_users SET credit=(credit+$file->credit_upload-$pay->credit) WHERE users_id=$file->users_id"); $database->query(); $date = date("Y-m-d H:i:s"); $log = new shareLogsTable($database); $log->users_id = $my->id; $log->share_files_id = $id; $log->date = $date; $log->operation = 'pay'; $log->credit = $file->credit_upload; // store it in the db $log -> store(); } } } function getCredits() { global $database, $my; if($my->id) { $database->setQuery("SELECT * FROM #__share_users WHERE users_id = $my->id"); $users = $database->loadObjectList(); if(count($users)>0) { return $users[0]->credit; } else { return 0; } } else { return -1; } } function getUser($id=NULL) { global $database, $my; if($id==NULL) { $id= $my->$id; } $user=new usersTable($database); $user->load($id); return $user; } ?>