1 | <?php |
---|
2 | |
---|
3 | if (!defined("_ECRIRE_INC_VERSION")) return; |
---|
4 | |
---|
5 | function zippeur_chemin_dossier_local(){ |
---|
6 | |
---|
7 | if (!isset($chemin)) { |
---|
8 | static $chemin = ''; |
---|
9 | if (defined('_DIR_SITE')) { |
---|
10 | $chemin = _DIR_SITE._NOM_TEMPORAIRES_ACCESSIBLES; |
---|
11 | } |
---|
12 | else{ |
---|
13 | $chemin = _DIR_RACINE._NOM_TEMPORAIRES_ACCESSIBLES; |
---|
14 | } |
---|
15 | } |
---|
16 | return $chemin; |
---|
17 | } |
---|
18 | |
---|
19 | include_spip('inc/zippeur_dynamique'); |
---|
20 | function zippeur_dynamique($dossier,$date, $cmd,$dynamiques=array(),$statiques=array(),$sanspath=array(),$delai=0, $extension='zip'){ |
---|
21 | if ($date == '') { |
---|
22 | $date = date("Y-m-d H:i:s",time()); |
---|
23 | } |
---|
24 | $chemin = zippeur_chemin_dossier_local().$dossier; |
---|
25 | function_exists('supprimer_repertoire') ? supprimer_repertoire($chemin) : spip_log("Version de SPIP < 3, possibilité de mélange dans un repertoire dynamique",'zippeur'); |
---|
26 | sous_repertoire($chemin); |
---|
27 | |
---|
28 | // création des fichiers dynamiques |
---|
29 | if (is_array($dynamiques)) { |
---|
30 | foreach ($dynamiques as $dyn){ |
---|
31 | if ($dyn[1]==''){ // si le 2 argument est vide, alors pas de souci, on prend le chemin tel quel |
---|
32 | $dyn[1] = $dyn[0]; |
---|
33 | } |
---|
34 | zippeur_creer_fichier($dyn[0],$dossier.'/'.$dyn[1],$dyn[2]); |
---|
35 | } |
---|
36 | } |
---|
37 | // Les fichiers statiques |
---|
38 | if (is_array($statiques)) { |
---|
39 | foreach ($statiques as $stat){ |
---|
40 | if ($stat[1]==''){ // si le 2 argument est vide, alors pas de souci, on prend le chemin tel quel |
---|
41 | $stat[1] = $stat[0]; |
---|
42 | } |
---|
43 | |
---|
44 | if (is_dir(find_in_path($stat[0]))) |
---|
45 | zippeur_copier_dossier($stat[0],$dossier.'/'.$stat[1]); |
---|
46 | else |
---|
47 | zippeur_copier_fichier($stat[0],$dossier.'/'.$stat[1]); |
---|
48 | } |
---|
49 | } |
---|
50 | // Et ceux où la notion de chemin ne s'applique pas |
---|
51 | if (is_array($sanspath)) { |
---|
52 | foreach ($sanspath as $sp){ |
---|
53 | defined('_DIR_SITE') ? $base = _DIR_SITE: $base = _DIR_RACINE; |
---|
54 | if (stripos($sp[0],'http://') === 0 or stripos($sp[0],'https://')) { // On peut passer une url |
---|
55 | include_spip('inc/distant'); |
---|
56 | $url = str_replace('&','&',$sp[0]); |
---|
57 | if ($sp[1]){ |
---|
58 | |
---|
59 | $chemin_fichier_recup = zippeur_chemin_dossier_local().$dossier.'/'.$sp[1]; |
---|
60 | zippeur_creer_arbo($dossier.'/'.$sp[1],'oui'); |
---|
61 | copie_locale($url,'force',$chemin_fichier_recup); |
---|
62 | } |
---|
63 | |
---|
64 | } |
---|
65 | else {// pas url ? |
---|
66 | if (stripos($sp[0],$base) === false){//vérifier que la personne n'a pas passé le chemin complet avant de modifier $sp[0] |
---|
67 | $sp[0] = $base.$sp[0]; |
---|
68 | } |
---|
69 | $p = $sp[0]; |
---|
70 | if ($sp[1]==''){ // si le 2 argument est vide, alors pas de souci, on prend le chemin tel quel |
---|
71 | $sp[1] = $sp[0]; |
---|
72 | } |
---|
73 | zippeur_copier_fichier($p, $dossier.'/'.$sp[1],false); |
---|
74 | } |
---|
75 | } |
---|
76 | } |
---|
77 | return zippeur(array($chemin),$date,$cmd,$dossier,zippeur_chemin_dossier_local().$dossier,$delai,$extension); |
---|
78 | } |
---|
79 | |
---|
80 | function zippeur($array,$date='',$cmd='',$nom='',$plat='oui',$delai='0',$extension='zip'){ |
---|
81 | if ($date == '') { |
---|
82 | $date = date("Y-m-d H:i:s",time()); |
---|
83 | } |
---|
84 | $delai = valeur_numerique($delai); |
---|
85 | $nom == '' ? $nom = md5(serialize($array)) : $nom = $nom; |
---|
86 | $cmd =='' ? $cmd = lire_config('zippeur/zippeur_cmd'):$cmd=$cmd; |
---|
87 | |
---|
88 | $chemin = zippeur_chemin_dossier_local().'cache-zip/'.$nom."$extension" ; |
---|
89 | include_spip('inc/flock'); |
---|
90 | $enbase = sql_fetsel('id_zip,fichiers,date_modif','spip_zippeur',"`nom`='$nom' and `extension`='$extension'"); |
---|
91 | /* On vérifie si le zip existe*/ |
---|
92 | if (count(preg_files($chemin))==0 or!$enbase['id_zip'] or $enbase['date_modif']!=$date or count($array)!=$enbase['fichiers'] or (defined('_NO_CACHE') and _NO_CACHE!=0 and !defined('_NO_CACHE_SAUF_ZIPPEUR'))){ |
---|
93 | |
---|
94 | if(zippeur_zipper($chemin,$array,$cmd,$plat)) |
---|
95 | { |
---|
96 | spip_log("Zippage de $nom.$extension avec cmd=$cmd","zippeur"); |
---|
97 | if ($enbase['id_zip']){ |
---|
98 | sql_updateq("spip_zippeur",array("delai_suppression"=>$delai,"date_modif"=>$date,'date_zip'=>date('Y-m-d H-i-s'),'fichiers'=>count($array)),"id_zip=".$enbase['id_zip']); |
---|
99 | } |
---|
100 | else{ |
---|
101 | sql_insertq("spip_zippeur",array("delai_suppression"=>$delai,"nom"=>$nom,'extension' => $extension,"date_modif"=>$date,'date_zip'=>date('Y-m-d H-i-s'),'fichiers'=>count($array))); |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | ; |
---|
106 | |
---|
107 | return $chemin; |
---|
108 | } |
---|
109 | |
---|
110 | function zippeur_zipper($chemin,$array,$cmd,$plat){ |
---|
111 | $temps_un=explode(" ",microtime()); |
---|
112 | if($cmd=='PclZip'){include_spip('inc/pclzip');} |
---|
113 | sous_repertoire(zippeur_chemin_dossier_local(),'cache-zip'); |
---|
114 | supprimer_fichier($chemin); |
---|
115 | $fichiers = 0; |
---|
116 | if($cmd=='PclZip') |
---|
117 | { |
---|
118 | $zip = new PclZip($chemin); |
---|
119 | $i = 0; |
---|
120 | foreach ($array as $fichier){ |
---|
121 | if (test_espace_prive()){ |
---|
122 | $array[$i] = '../'.$fichier; |
---|
123 | } |
---|
124 | $i++; |
---|
125 | } |
---|
126 | if ($plat=='oui') |
---|
127 | $erreur = $zip->add($array,PCLZIP_OPT_REMOVE_ALL_PATH); |
---|
128 | else |
---|
129 | $erreur = $zip->add($array,PCLZIP_OPT_REMOVE_PATH, $plat); |
---|
130 | if ($erreur == 0){ |
---|
131 | spip_log("$chemin".$zip->errorInfo(true),"zippeur_erreur"._LOG_ERREUR); |
---|
132 | |
---|
133 | } |
---|
134 | $fichiers =count($array) ; |
---|
135 | }elseif($cmd=='7zip') |
---|
136 | { |
---|
137 | foreach ($array as $fichier){ |
---|
138 | if (test_espace_prive()){ |
---|
139 | $fichier_liste .= ' ../'.$fichier; |
---|
140 | }else{ |
---|
141 | $fichier_liste .= ' '.$fichier; |
---|
142 | } |
---|
143 | $fichiers++; |
---|
144 | } |
---|
145 | passthru("7za a -tzip ".$chemin." ".$fichier_liste." -mx5 >/dev/null",$result); |
---|
146 | if($result!=0) |
---|
147 | { |
---|
148 | spip_log($fichier_liste." -- code d'erreur 7z: ".$result,"zippeur_erreur"._LOG_ERREUR); |
---|
149 | } |
---|
150 | else{ |
---|
151 | //$fichiers++; |
---|
152 | } |
---|
153 | }elseif($cmd=='zip') |
---|
154 | { |
---|
155 | foreach ($array as $fichier){ |
---|
156 | if (test_espace_prive()){ |
---|
157 | $fichier_liste .= ' ../'.$fichier; |
---|
158 | }else{ |
---|
159 | $fichier_liste .= ' '.$fichier; |
---|
160 | } |
---|
161 | $fichiers++; |
---|
162 | } |
---|
163 | passthru("zip -jq9 ".$chemin." ".$fichier_liste." >/dev/null",$result); |
---|
164 | if($result!=0) |
---|
165 | { |
---|
166 | spip_log($fichier_liste." -- code d'erreur zip: ".$result,"zippeur_erreur"._LOG_ERREUR); |
---|
167 | } |
---|
168 | else{ |
---|
169 | //$fichiers++; |
---|
170 | } |
---|
171 | } |
---|
172 | if ($fichiers !=count($array)){ |
---|
173 | spip_log("$chemin : $fichiers fichiers présents mais ".count($array)." prévus",'zippeur_erreur'._LOG_ERREUR); |
---|
174 | return false; |
---|
175 | }else{ |
---|
176 | $temps_deux=explode(" ",microtime()); |
---|
177 | spip_log('zipper en '.($temps_deux[1]-$temps_un[1]).'sec avec '.$cmd,'zippeur'); |
---|
178 | return true; |
---|
179 | } |
---|
180 | } |
---|