PageRenderTime 23ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/_plugins_/ddUpload/action/joindre.php

https://bitbucket.org/pombredanne/spip-zone-treemap
PHP | 306 lines | 283 code | 10 blank | 13 comment | 6 complexity | fbffb1a1e9da19989dda3d50e6f6cf8b MD5 | raw file
  1. <?php
  2. /***************************************************************************\
  3. * SPIP, Systeme de publication pour l'internet *
  4. * *
  5. * Copyright (c) 2001-2010 *
  6. * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
  7. * *
  8. * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
  9. * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
  10. \***************************************************************************/
  11. if (!defined("_ECRIRE_INC_VERSION")) return;
  12. include_spip('inc/charsets'); # pour le nom de fichier
  13. include_spip('inc/actions');
  14. // http://doc.spip.org/@action_joindre_dist
  15. function action_joindre_dist()
  16. {
  17. global $redirect;
  18. $securiser_action = charger_fonction('securiser_action', 'inc');
  19. $arg = $securiser_action();
  20. if (!preg_match(',^(-?\d+)\D(\d+)\D(\w+)/(\w+)$,',$arg,$r)) {
  21. spip_log("action_joindre_dist incompris: " . $arg);
  22. $redirect = urldecode(_request('redirect'));
  23. return;
  24. }
  25. list(, $id, $id_document, $mode, $type) = $r;
  26. $actifs = array();
  27. $redirect = action_joindre_sous_action($id, $id_document, $mode, $type, $actifs);
  28. }
  29. // http://doc.spip.org/@action_joindre_sous_action
  30. function action_joindre_sous_action($id, $id_document, $mode, $type, &$documents_actifs)
  31. {
  32. $hash = _request('hash');
  33. $url = _request('url');
  34. $chemin = _request('chemin');
  35. $ancre = _request('ancre');
  36. $sousaction1 = _request('sousaction1');
  37. $sousaction2 = _request('sousaction2');
  38. $sousaction3 = _request('sousaction3');
  39. $sousaction4 = _request('sousaction4');
  40. $sousaction5 = _request('sousaction5'); // decompacter un zip
  41. $redirect = _request('redirect');
  42. $iframe_redirect = _request('iframe_redirect');
  43. // pas terrible, mais c'est le pb du bouton Submit qui retourne son texte,
  44. // et son transcodage est couteux et perilleux
  45. $sousaction =
  46. ($sousaction1 ? 1 :
  47. ($sousaction2 ? 2 :
  48. ($sousaction3 ? 3 :
  49. ($sousaction4 ? 4 :
  50. $sousaction5 ))));
  51. if (isset($_SERVER['HTTP_X_FILE_NAME']) && isset($_SERVER['CONTENT_LENGTH'])) {
  52. if($_SERVER['CONTENT_LENGTH']>0) {
  53. $handle = fopen("php://input","rb");
  54. $block = fread($handle,4096);
  55. $blocklen = strlen($block);
  56. $base64 = false;
  57. $dest = tempnam(_DIR_TMP, 'tmp_upload');
  58. $handle_dest = fopen($dest,"ab");
  59. if (preg_match("/^data:[^;]+(;charset=\"[^\"]+\")?(;base64)?,/",$block,$m)) {
  60. //data uri
  61. $base64 = $m[2];
  62. $block = substr($block,strlen($m[0]));
  63. if($base64) {
  64. $blocklen -= $m[0];
  65. $blockleft = $blocklen % 4;
  66. if($blockleft) {
  67. $block .= fread($handle,$blockleft);
  68. }
  69. $block = base64_decode($block);
  70. }
  71. }
  72. fwrite($handle_dest,$block);
  73. while($block = fread($handle,4096)) {
  74. if($base64)
  75. $block = base64_decode($block);
  76. fwrite($handle_dest,$block);
  77. }
  78. fclose($handle);
  79. fclose($handle_dest);
  80. $path = array(
  81. array("name" => $_SERVER['HTTP_X_FILE_NAME'], "tmp_name" => $dest, "error" => 0)
  82. );
  83. } else {
  84. spip_log("file upload error");
  85. $path = array(array("error" => 4));
  86. }
  87. } else {
  88. $path = ($sousaction1 ? ($_FILES ? $_FILES : $GLOBALS['HTTP_POST_FILES']) :
  89. ($sousaction2 ? $url : $chemin));
  90. }
  91. $sousaction = charger_fonction('joindre' . $sousaction, 'inc');
  92. $type_image = $sousaction($path, $mode, $type, $id, $id_document,
  93. $hash, $redirect, $documents_actifs, $iframe_redirect);
  94. if($dest)
  95. @unlink($dest);
  96. $redirect = urldecode($redirect);
  97. if ($documents_actifs) {
  98. $redirect = parametre_url($redirect,'show_docs',join(',',$documents_actifs),'&');
  99. }
  100. if (!$ancre) {
  101. if ($mode=='image')
  102. $ancre = 'images';
  103. else if ($type_image)
  104. $ancre = 'portfolio';
  105. else
  106. $ancre = 'documents';
  107. }
  108. $redirect .= '#' . $ancre;
  109. if ($type == 'rubrique') {
  110. include_spip('inc/rubriques');
  111. calculer_rubriques_if($id, array('statut' => 'publie'));
  112. }
  113. if(_request("iframe") == 'iframe') {
  114. $redirect = parametre_url(urldecode($iframe_redirect),"show_docs",join(',',$documents_actifs),'&')."&iframe=iframe&var_noajax=1";
  115. }
  116. return $redirect;
  117. }
  118. // Cas d'un document distant reference sur internet
  119. // http://doc.spip.org/@inc_joindre2_dist
  120. function inc_joindre2_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs, $iframe_redirect)
  121. {
  122. return joindre_documents(array(
  123. array('name' => basename($path),
  124. 'tmp_name' => $path)
  125. ), 'distant', $type, $id, $id_document,
  126. $hash, $redirect, $actifs, $iframe_redirect);
  127. }
  128. // Cas d'un fichier transmis
  129. // http://doc.spip.org/@inc_joindre1_dist
  130. function inc_joindre1_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs, $iframe_redirect)
  131. {
  132. $files = array();
  133. if (is_array($path))
  134. foreach ($path as $file) {
  135. //UPLOAD_ERR_NO_FILE
  136. if (!($file['error'] == 4) )
  137. $files[]=$file;
  138. }
  139. return joindre_documents($files, $mode, $type, $id, $id_document,
  140. $hash, $redirect, $actifs, $iframe_redirect);
  141. }
  142. // copie de tout ou partie du repertoire upload
  143. // http://doc.spip.org/@inc_joindre3_dist
  144. function inc_joindre3_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs, $iframe_redirect)
  145. {
  146. if (!$path || strstr($path, '..')) return;
  147. $upload = determine_upload();
  148. if ($path != '/' AND $path != './') $upload .= $path;
  149. if (!is_dir($upload))
  150. // seul un fichier est demande
  151. $files = array(array ('name' => basename($upload),
  152. 'tmp_name' => $upload)
  153. );
  154. else {
  155. include_spip('inc/documents');
  156. $files = array();
  157. foreach (preg_files($upload) as $fichier) {
  158. $files[]= array (
  159. 'name' => basename($fichier),
  160. 'tmp_name' => $fichier
  161. );
  162. }
  163. }
  164. return joindre_documents($files, $mode, $type, $id, $id_document, $hash, $redirect, $actifs, $iframe_redirect);
  165. }
  166. //
  167. // Charger la fonction surchargeable receptionnant un fichier
  168. // et l'appliquer sur celui ou ceux indiques.
  169. // http://doc.spip.org/@joindre_documents
  170. function joindre_documents($files, $mode, $type, $id, $id_document, $hash, $redirect, &$actifs, $iframe_redirect)
  171. {
  172. $ajouter_documents = charger_fonction('ajouter_documents', 'inc');
  173. if (function_exists('gzopen')
  174. AND !($mode == 'distant')
  175. AND (count($files) == 1)
  176. AND (preg_match('/\.zip$/i', $files[0]['name'])
  177. OR ($files[0]['type'] == 'application/zip'))) {
  178. // on pose le fichier dans le repertoire zip
  179. // (nota : copier_document n'ecrase pas un fichier avec lui-meme
  180. // ca autorise a boucler)
  181. $desc = $files[0];
  182. $zip = copier_document("zip",
  183. $desc['name'],
  184. $desc['tmp_name']
  185. );
  186. // Est-ce qu'on sait le lire ?
  187. include_spip('inc/pclzip');
  188. $archive = $zip ? new PclZip($zip) : '';
  189. if ($archive) {
  190. $valables = verifier_compactes($archive);
  191. if ($valables) {
  192. if (rename($zip, $tmp = _DIR_TMP.basename($zip))) {
  193. echo $ajouter_documents($valables, $tmp, $type, $id, $mode, $id_document, $actifs, $hash, $redirect, $iframe_redirect);
  194. // a tout de suite en joindre4, joindre5, ou joindre6
  195. exit;
  196. }
  197. }
  198. }
  199. }
  200. foreach ($files as $arg) {
  201. // verifier l'extension du fichier en fonction de son type mime
  202. list($extension,$arg['name']) = fixer_extension_document($arg);
  203. check_upload_error($arg['error']);
  204. $x = $ajouter_documents($arg['tmp_name'], $arg['name'],
  205. $type, $id, $mode, $id_document, $actifs);
  206. }
  207. // un invalideur a la hussarde qui doit marcher au moins pour article, breve, rubrique
  208. include_spip('inc/invalideur');
  209. suivre_invalideur("id='id_$type/$id'");
  210. return $x;
  211. }
  212. #-----------------------------------------------------------------------
  213. // sous-actions suite a l'envoi d'un Zip:
  214. // la fonction joindre_documents ci-dessus a construit un formulaire
  215. // qui renvoie sur une des 3 sous-actions qui suivent.
  216. // Zip avec confirmation "tel quel"
  217. // http://doc.spip.org/@inc_joindre5_dist
  218. function inc_joindre5_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs)
  219. {
  220. $ajouter_documents = charger_fonction('ajouter_documents', 'inc');
  221. return $ajouter_documents($path, basename($path), $type, $id, $mode, $id_document, $actifs);
  222. }
  223. // Zip a deballer.
  224. // http://doc.spip.org/@inc_joindre6_dist
  225. function inc_joindre6_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs, $iframe_redirect)
  226. {
  227. $x = joindre_deballes($path, $mode, $type, $id, $id_document,$hash, $redirect, $actifs);
  228. // suppression de l'archive en zip
  229. spip_unlink($path);
  230. return $x;
  231. }
  232. // Zip avec les 2 options a la fois
  233. // http://doc.spip.org/@inc_joindre4_dist
  234. function inc_joindre4_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs, $iframe_redirect)
  235. {
  236. joindre_deballes($path, $mode, $type, $id, $id_document,$hash, $redirect, $actifs);
  237. return inc_joindre5_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, $actifs);
  238. }
  239. // http://doc.spip.org/@joindre_deballes
  240. function joindre_deballes($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs)
  241. {
  242. $ajouter_documents = charger_fonction('ajouter_documents', 'inc');
  243. define('_tmp_dir', creer_repertoire_documents($hash));
  244. if (_tmp_dir == _DIR_IMG)
  245. {include_spip('inc/minipres');
  246. echo minipres(_T('avis_operation_impossible'));
  247. exit;
  248. }
  249. include_spip('inc/pclzip');
  250. $archive = new PclZip($path);
  251. $archive->extract(
  252. PCLZIP_OPT_PATH, _tmp_dir,
  253. PCLZIP_CB_PRE_EXTRACT, 'callback_deballe_fichier'
  254. );
  255. $contenu = verifier_compactes($archive);
  256. $titrer = _request('titrer') == 'on';
  257. foreach ($contenu as $fichier => $size) {
  258. $f = basename($fichier);
  259. $x = $ajouter_documents(_tmp_dir. $f, $f,
  260. $type, $id, $mode, $id_document, $actifs, $titrer);
  261. }
  262. effacer_repertoire_temporaire(_tmp_dir);
  263. return $x;
  264. }
  265. ?>