Example #1
0
 public function Create($post_name, $user_id, $id_word = null, $id_rewrite = false)
 {
     $user_id = (int) $user_id;
     if (!POSTGood($post_name, self::$formats)) {
         return 1;
     }
     if ($id_word and !preg_match("/^[a-zA-Z0-9._-]+\$/", $id_word)) {
         return 3;
     }
     $new_file_info = POSTSafeMove($post_name, $this->base_dir);
     if (!$new_file_info) {
         return 2;
     }
     $way = $this->base_dir . $new_file_info['tmp_name'];
     $hash = md5_file($this->base_dir . $new_file_info['tmp_name']);
     $sql_part = $id_word ? " OR `id_word`=:id_word" : '';
     $data = $id_word ? array('id_word' => $id_word) : false;
     $line = getDB()->fetchRow("SELECT `id` FROM `{$this->db}` " . "WHERE `hash`='" . $hash . "'" . $sql_part, $data, 'num');
     if ($line) {
         $file_similar = new File($line[0]);
         $similar_info = $file_similar->getInfo();
         if ($similar_info['hash'] == $hash) {
             if (file_exists($way)) {
                 unlink($way);
             }
             $this->id = $similar_info['id'];
             $this->user_id = $similar_info['user_id'];
             $this->id_word = $similar_info['id_word'];
             $this->name = $similar_info['name'];
             $this->size = $similar_info['size'];
             $this->hash = $similar_info['hash'];
             $this->downloads = $similar_info['downloads'];
             $this->way = $file_similar->getWay();
             return 7;
         } else {
             if (!$id_rewrite) {
                 if (file_exists($way)) {
                     unlink($way);
                 }
                 return 4;
             } else {
                 if (!$file_similar->Delete()) {
                     return 6;
                 }
                 unset($file_similar);
             }
         }
     }
     $sql = "INSERT INTO {$this->db} (id_word, user_id, way, name, size, hash) " . "VALUES (:id_word, :user_id, :fway, :fname, :fsize, '{$hash}')";
     $result = getDB()->ask($sql, array('id_word' => $id_word ? $id_word : '', 'user_id' => $user_id, 'fway' => $new_file_info['tmp_name'], 'fname' => $new_file_info['name'], 'fsize' => $new_file_info['size_mb']));
     if ($result) {
         $this->id = getDB()->lastInsertId();
         $this->user_id = $user_id;
         $this->id_word = $id_word ? $id_word : '';
         $this->way = $way;
         $this->name = $new_file_info['name'];
         $this->size = $new_file_info['size_mb'];
         $this->hash = $hash;
         $this->downloads = 0;
     } else {
         if (file_exists($way)) {
             unlink($way);
         }
         return 5;
     }
     return 0;
 }