Last active
December 23, 2015 12:09
-
-
Save fastcodecoq/6633351 to your computer and use it in GitHub Desktop.
Code de la clase Gomosoft I/O, para gestionar los files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once($_SERVER["DOCUMENT_ROOT"] . "/config.inc.php"); | |
| class files{ | |
| private $con; | |
| private $bd; | |
| private $col; | |
| private $files; | |
| private $errors; | |
| public function __construct(){ | |
| $this->con = new MongoClient(); | |
| $this->bd = $this->con->selectDB(database); | |
| $this->col = $this->bd->files; | |
| try{ | |
| $rs = $this->col->find(); | |
| $rs = iterator_to_array($rs); | |
| if(count($rs) === 0) | |
| throw new Exception("BD no initialized", 164); | |
| $this->files = $this->find(); | |
| }catch(Exception $e){ | |
| $this->files = $this->iniBD(); | |
| } | |
| } | |
| public function getFiles(){ | |
| return $this->files; | |
| } | |
| public function registerView($name){ | |
| try{ | |
| $query = array( "name" => $name); | |
| if($rs = $this->find($query)) | |
| { | |
| $set = array( | |
| "views" => $rs["views"] + 1 | |
| ); | |
| $this->col->update($query , array('$set' => $set)); | |
| echo "<script> window.location = \"/files/{$name}\" </script>"; | |
| } | |
| else | |
| throw new Exception("The file requested not exists", 138); | |
| }catch(Exception $e){ | |
| $this->errors[] = $e; | |
| return false; | |
| } | |
| } | |
| public function find( $query = NULL){ | |
| try{ | |
| if($query === NULL) | |
| { | |
| $rs = $this->col->find(); | |
| $rs = $rs->sort( array("views" => -1) ); | |
| $rs = iterator_to_array($rs); | |
| if(count($rs) === 0) | |
| return false; | |
| else | |
| return $rs; | |
| } | |
| else | |
| { | |
| $rs = $this->col->findOne($query); | |
| if(count($rs) === 0) | |
| return false; | |
| else | |
| return $rs; | |
| } | |
| }catch(Exception $e){ | |
| $this->errors[] = $e; | |
| return false; | |
| } | |
| } | |
| private function iniBD(){ | |
| $files = $this->getFilesFromFolder(); | |
| foreach ($files as $f) { | |
| try{ | |
| $insert = array( | |
| "name" => $f, | |
| "views" => 0 | |
| ); | |
| $this->col->insert($insert); | |
| }catch(Exception $e){ | |
| $this->errors[] = $e; | |
| exit; | |
| } | |
| } | |
| return iterator_to_array($this->col->find()); | |
| } | |
| private function getFilesFromFolder( $dir = "/files"){ | |
| $directorio=opendir(dirname(__FILE__) . $dir); | |
| $struct = array(); | |
| while ($archivo = readdir($directorio)) { | |
| if($archivo == '.') | |
| {//$struct[] = $archivo; | |
| } | |
| elseif($archivo == '..'){ | |
| if($dir != '.'){ | |
| $carpetas = split("/",$dir); | |
| array_pop($carpetas); | |
| $dir2 = join("/",$carpetas); | |
| //$struct[] = $dir2; | |
| } | |
| } | |
| elseif(is_dir("$dir/$archivo")) | |
| echo "<a href=\"?dir=$dir/$archivo\"> $archivo</a><br>"; | |
| else if(strlen($archivo) > 3) | |
| $struct[] = $archivo; | |
| } | |
| closedir($directorio); | |
| return $struct; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment