Beispiel #1
0
function explnum_add_from_url($f_notice_id, $f_bulletin_id, $f_nom, $f_url, $overwrite = true, $source_id = 0, $filename = '', $f_path = '', $f_statut)
{
    global $dbh, $base_path;
    if ($f_bulletin_id) {
        $f_notice_id = 0;
    } else {
        if (!$f_bulletin_id && $f_notice_id) {
            $f_bulletin_id = 0;
        }
    }
    if (!$overwrite) {
        $sql_find = "SELECT count(*) FROM explnum WHERE explnum_notice = " . $f_notice_id . " AND explnum_nom = '" . addslashes($f_nom) . "'";
        $res = pmb_mysql_query($sql_find, $dbh);
        $count = pmb_mysql_result($res, 0, 0);
        if ($count) {
            return;
        }
    }
    $sql_delete = "DELETE FROM explnum WHERE explnum_notice = " . $f_notice_id . " AND explnum_nom = '" . addslashes($f_nom) . "' ";
    pmb_mysql_query($sql_delete, $dbh);
    $original_filename = basename($f_url);
    $extension = substr($original_filename, strripos($original_filename, '.') * 1 + 1);
    $tmp_filename = explnum::static_rename($extension);
    if ($filename) {
        $new_filename = $filename;
    } else {
        $new_filename = $tmp_filename;
    }
    //copie en répertoire temporaire
    $r = false;
    if (file_exists($f_url) && filesize($f_url)) {
        //document en repertoire
        $r = copy($f_url, $base_path . '/temp/' . $tmp_filename);
    } else {
        //url
        $aCurl = new Curl();
        $content = $aCurl->get($f_url);
        $content = $content->body;
        $r = file_put_contents("{$base_path}/temp/" . $tmp_filename, $content);
    }
    if ($r) {
        //construction vignette
        $vignette = construire_vignette('', $tmp_filename);
        create_tableau_mimetype();
        $mimetype = trouve_mimetype("{$base_path}/temp/" . $tmp_filename, $extension);
        //si la source du connecteur est précisée, on regarde si on a pas un répertoire associé
        $rep_upload = 0;
        if ($source_id) {
            $check_rep = "select rep_upload from connectors_sources where source_id = " . $source_id;
            $res = pmb_mysql_query($check_rep);
            if (pmb_mysql_num_rows($res)) {
                $rep_upload = pmb_mysql_result($res, 0, 0);
            }
        }
        if ($rep_upload != 0) {
            $upload_folder = new upload_folder($rep_upload);
            $rep_path = $upload_folder->get_path($new_filename);
            if ($f_path && file_exists($rep_path . $f_path)) {
                $rep_path = $rep_path . $f_path . '/';
            }
            if (file_exists($upload_folder->encoder_chaine($rep_path . $new_filename))) {
                $suffix = 1;
                $ext = extension_fichier($new_filename);
                $file = str_replace("." . $ext, "", basename($new_filename));
                while (file_exists($upload_folder->encoder_chaine($rep_path . $file . "_" . $suffix . "." . $ext))) {
                    $suffix++;
                }
                $new_filename = $file . "_" . $suffix . "." . $ext;
            }
            rename("{$base_path}/temp/" . $tmp_filename, $upload_folder->encoder_chaine($rep_path . $new_filename));
            $path = $upload_folder->formate_path_to_save($upload_folder->formate_path_to_nom($rep_path));
            $insert_sql = "INSERT INTO explnum (explnum_notice, explnum_bulletin, explnum_nom, explnum_nomfichier, explnum_mimetype, explnum_extfichier, explnum_vignette, explnum_repertoire, explnum_path, explnum_docnum_statut) VALUES (";
            $insert_sql .= $f_notice_id . ",";
            $insert_sql .= $f_bulletin_id . ",";
            $insert_sql .= "'" . addslashes($f_nom) . "',";
            $insert_sql .= "'" . addslashes($new_filename) . "',";
            $insert_sql .= "'" . addslashes($mimetype) . "',";
            $insert_sql .= "'" . addslashes($extension) . "',";
            $insert_sql .= "'" . addslashes($vignette) . "',";
            $insert_sql .= "'" . addslashes($rep_upload) . "',";
            $insert_sql .= "'" . addslashes($path) . "',";
            $insert_sql .= $f_statut;
            $insert_sql .= ")";
        } else {
            $insert_sql = "INSERT INTO explnum (explnum_notice, explnum_bulletin, explnum_nom, explnum_nomfichier, explnum_mimetype, explnum_extfichier, explnum_data, explnum_vignette, explnum_docnum_statut) VALUES (";
            $insert_sql .= $f_notice_id . ",";
            $insert_sql .= $f_bulletin_id . ",";
            $insert_sql .= "'" . addslashes($f_nom) . "',";
            $insert_sql .= "'" . addslashes($new_filename) . "',";
            $insert_sql .= "'" . addslashes($mimetype) . "',";
            $insert_sql .= "'" . addslashes($extension) . "',";
            $insert_sql .= "'" . addslashes($content) . "',";
            $insert_sql .= "'" . addslashes($vignette) . "',";
            $insert_sql .= $f_statut;
            $insert_sql .= ")";
        }
        if (pmb_mysql_query($insert_sql, $dbh)) {
            $docnum_id = pmb_mysql_insert_id($dbh);
            if ($docnum_id) {
                $index = new indexation_docnum($docnum_id);
                $index->indexer();
            }
        }
        /*
        		$aCurl = new Curl();
        		$content = $aCurl->get($f_url);
        		$content = $content->body;
        		
        		$origine=str_replace(" ","",microtime());
        		$origine=str_replace("0.","",$origine);
        		$original_filename = basename($f_url);
        		if( $filename != "") $afilename = $filename;
        		else $afilename = $origine.$original_filename;
        		if (!$original_filename)
        			$original_filename = $afilename;
        			
        		file_put_contents("$base_path/temp/".$afilename, $content);
        */
        /*		
        		$vignette = construire_vignette('', $afilename);
        		create_tableau_mimetype();
        		$afilename_ext=extension_fichier($afilename);
        		$mimetype = trouve_mimetype("$base_path/temp/".$afilename, $afilename_ext);
        		$extension = strrchr($afilename, '.');
        		
        		//si la source du connecteur est précisée, on regarde si on a pas un répertoire associé
        		if ($source_id){
        			$check_rep = "select rep_upload from connectors_sources where source_id = ".$source_id;
        			$res = pmb_mysql_query($check_rep);
        			if(pmb_mysql_num_rows($res)){
        				$rep_upload = pmb_mysql_result($res,0,0);
        			}
        		}
        */
        /*
        			if($rep_upload != 0){
        			$upload_folder = new upload_folder($rep_upload);
        			$rep_path = $upload_folder->get_path($afilename);
        			if ($f_path && file_exists($rep_path.$f_path)) {
        				$rep_path=$rep_path.$f_path.'/';
        			}
        			
        			copy("$base_path/temp/".$afilename,$rep_path.$afilename);
        			$path =$upload_folder->formate_path_to_save($upload_folder->formate_path_to_nom($rep_path));
        			$insert_sql = "INSERT INTO explnum (explnum_notice, explnum_nom, explnum_nomfichier, explnum_mimetype, explnum_extfichier, explnum_vignette, explnum_repertoire, explnum_path) VALUES (";
        			$insert_sql .= $f_notice_id.",";
        			$insert_sql .= "'".addslashes($f_nom)."',";
        			$insert_sql .= "'".addslashes($afilename)."',";
        			$insert_sql .= "'".addslashes($mimetype)."',";
        			$insert_sql .= "'".addslashes($extension)."',";
        			$insert_sql .= "'".addslashes($vignette)."',";
        			$insert_sql .= "'".addslashes($rep_upload)."',";
        			$insert_sql .= "'".addslashes($path)."'";
        			$insert_sql .= ")";		
        		}else{
        			$insert_sql = "INSERT INTO explnum (explnum_notice, explnum_nom, explnum_nomfichier, explnum_mimetype, explnum_extfichier, explnum_data, explnum_vignette) VALUES (";
        			$insert_sql .= $f_notice_id.",";
        			$insert_sql .= "'".addslashes($f_nom)."',";
        			$insert_sql .= "'".addslashes($afilename)."',";
        			$insert_sql .= "'".addslashes($mimetype)."',";
        			$insert_sql .= "'".addslashes($extension)."',";
        			$insert_sql .= "'".addslashes($content)."',";
        			$insert_sql .= "'".addslashes($vignette)."'";
        			$insert_sql .= ")";
        		}
        		pmb_mysql_query($insert_sql, $dbh);
        		
        		unlink("$base_path/temp/".$afilename);	
        */
    }
}