PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/skeleditor/spip_200/skeleditor_fonctions.php

https://bitbucket.org/pombredanne/spip-zone-treemap
PHP | 318 lines | 211 code | 35 blank | 72 comment | 50 complexity | d5ca6b44b20f1a8215a3b3ee9a4303b8 MD5 | raw file
  1. <?php
  2. // variante repliee de la fonction de l'affichage de l'arbre des repertoires
  3. // http://doc.spip.org/@tree_open_close_dir
  4. function skeleditor_tree_open_close_dir(&$current,$target,$current_file){
  5. if ($current == $target) return "";
  6. $tcur = explode("/",$current);
  7. $ttarg = explode("/",$target);
  8. $tcom = array();
  9. $output = "";
  10. // la partie commune
  11. while (reset($tcur)==reset($ttarg)){
  12. $tcom[] = array_shift($tcur);
  13. array_shift($ttarg);
  14. }
  15. // fermer les repertoires courant jusqu'au point de fork
  16. while($close = array_pop($tcur)){
  17. $output .= fin_block();
  18. }
  19. $chemin = implode("/",$tcom)."/";
  20. // ouvrir les repertoires jusqu'a la cible
  21. while($open = array_shift($ttarg)){
  22. $chemin .= $open . "/";
  23. if(!strstr($current_file,$chemin)){
  24. $output .= bouton_block_depliable("<img src='"._DIR_PLUGIN_SKELEDITOR."spip_200/img_pack/folder.png' alt='directory' style='float:left;' /> $open",false,md5($chemin));
  25. }
  26. else {
  27. $output .= bouton_block_depliable("<img src='"._DIR_PLUGIN_SKELEDITOR."spip_200/img_pack/folder.png' alt='directory' style='float:left;' /> $open",false,md5($chemin));
  28. }
  29. $output .= "<div style='line-height: 12px;border:1px solid #ededed;padding:4px;margin:4px 0;display:none;' id='".md5($chemin)."'>\n";
  30. }
  31. $current = $target;
  32. return $output;
  33. }
  34. function skeleditor_afficher_dir_skel($file_list,$current_file,$img_extension) {
  35. $output = "<div style='line-height: 12px;border:1px solid #ededed;padding:4px;margin:4px 0'>\n";
  36. $init_dir = $current_dir = dirname(reset($file_list));
  37. foreach($file_list as $file){
  38. $dir = dirname($file);
  39. if ($dir != $current_dir)
  40. $output .= skeleditor_tree_open_close_dir($current_dir,$dir,$current_file);
  41. if (!is_writable($dir))
  42. $output .= "<div style='background:#3ff'>";
  43. else
  44. $output .= "<div>";
  45. if ($file==$current_file)
  46. $expose=" style='background:#ff6'";
  47. else
  48. $expose="";
  49. $path_parts = pathinfo($file);
  50. $extension = $path_parts['extension'];
  51. $base = $path_parts['basename'];
  52. if (in_array($extension,$img_extension)) {
  53. $output .= "<img src='"._DIR_PLUGIN_SKELEDITOR."spip_200/img_pack/img.png' alt='image' /> ";
  54. $output .= "<a href='".generer_url_ecrire('skeleditor','f='.urlencode($file))."'$expose>$base</a>";
  55. } else {
  56. $output .= "<img src='"._DIR_PLUGIN_SKELEDITOR."spip_200/img_pack/file.png' alt='file' /> ";
  57. $output .= "<a href='".generer_url_ecrire('skeleditor','f='.urlencode($file))."'$expose>$base</a>";
  58. }
  59. $output .= "</div>\n";
  60. }
  61. $output .= skeleditor_tree_open_close_dir($current_dir,$init_dir,$current_file);
  62. $output .= "</div>\n";
  63. return $output;
  64. }
  65. // build select menu to choose directory
  66. function editor_form_directory($path,$depth="") {
  67. $output = "";
  68. foreach($path as $dir){
  69. $tdir = explode('/',$dir);
  70. $myfile = array_pop($tdir);
  71. $depth = "";
  72. while(array_pop($tdir)) $depth.="&nbsp;&nbsp;";
  73. $output .= "<option value='$dir'>$depth$myfile</option>\n";
  74. }
  75. return $output;
  76. }
  77. // add file form
  78. function skeleditor_addfile($path_list) {
  79. //$output = bouton_block_invisible('editor_newfile');
  80. $output .= "<img src='"._DIR_PLUGIN_SKELEDITOR."spip_200/img_pack/action_add.png' alt='new' />"._T("skeleditor:fichier_nouveau");
  81. //$output .= debut_block_invisible('editor_newfile');
  82. $output .= "<form method='get'>\n";
  83. $output .= "<input type='hidden' name='exec' value='skeleditor' />";
  84. $output .= "<input type='hidden' name='operation' value='new' />";
  85. //$output .= "nom du fichier:<br />\n";
  86. $output .= "<input type='text' name='f' value='untitled.html' onfocus=\"this.value=''\" />";
  87. $output .= _T("skeleditor:target")."<br />\n";
  88. $output .= "<select name='target'><br />\n";
  89. $output .= editor_form_directory($path_list);
  90. $output .= "</select><br /><input type='submit' name='sub' value='"._T("skeleditor:creer")."' />";
  91. $output .= "</form>\n";
  92. $output .= fin_block();
  93. return $output;
  94. }
  95. // upload file form
  96. function skeleditor_uploadfile($path_list) {
  97. //$output = "<br />".bouton_block_invisible('editor_uploadfile');
  98. $output .= "<img src='"._DIR_PLUGIN_SKELEDITOR."spip_200/img_pack/action_add.png' alt='new' />"._T("skeleditor:fichier_upload");
  99. //$output .= debut_block_invisible('editor_uploadfile');
  100. $output .= "<form method='post' enctype='multipart/form-data' >\n";
  101. $output .= "<input type='hidden' name='exec' value='skeleditor' />";
  102. $output .= "<input type='hidden' name='operation' value='upload' />";
  103. $output .= "<input type='hidden' name='MAX_FILE_SIZE' value='200000' />";
  104. $output .= "<input type='file' name='upf'/>";
  105. $output .= _T("skeleditor:target")."<br />\n";
  106. $output .= "<select name='target'><br />\n";
  107. $output .= editor_form_directory($path_list);
  108. $output .= "</select><br /><input type='submit' name='sub' value='"._T("skeleditor:upload")."' />";
  109. $output .= "</form>\n";
  110. $output .= fin_block();
  111. return $output;
  112. }
  113. // skeleton parsing (more details: public/phrase_html)
  114. function skel_parser($skel_str) {
  115. include_spip("public/interfaces");
  116. include_spip("public/phraser_html");
  117. //include_spip("public/debug");
  118. //$output .= _T('skeleditor:parseur_titre');
  119. $output .= "<div style='background: #eef; border:1px solid #eee;padding:10px;font-size:0.82em;font-family:Verdana'>";
  120. $boucles = array();
  121. $b = public_phraser_html($skel_str, 0, $boucles, 'skel_editor');
  122. $boucles = array_reverse($boucles,TRUE);
  123. /* parse outside boucles */
  124. //$output .= bouton_block_invisible("hors_boucle")._T("skeleditor:parseur_horsboucle");
  125. //$output .= debut_block_invisible("hors_boucle");
  126. $output .= "<div style='background: #fff;padding:10px;'>";
  127. foreach($b as $k=>$val) {
  128. if ($val->type == "champ") $output .= "<span style='color:#c30;background:#eee'>#".$val->nom_champ."</span>";
  129. else if ($val->type == "texte") $output .="<pre style='background:#ddd;margin:0;display:inline;'>&nbsp;".htmlspecialchars($val->texte)."</pre>";
  130. else if ($val->type == "include") $output .= "<span style='color:#30c;background:#cff;'>(INCLURE)</span>";
  131. }
  132. $output .= "</div>\n";
  133. $output .= fin_block()."<br />";
  134. /* parse boucles */
  135. foreach($boucles as $k=>$val) {
  136. /* version gentle */
  137. //$output .= bouton_block_invisible("skel_parser_$k")." BOUCLE$k";
  138. $output .= " <span style='color:#888'>(".strtoupper($val->type_requete).")</span>";
  139. //$output .= debut_block_invisible("skel_parser_$k");
  140. $output .= "<div style='background: #fff;padding:10px;'>";
  141. if ($val->id_parent) $output .= "<strong>id_parent:</strong> BOUCLE$val->id_parent<br />";
  142. if ($val->param) $output .= "<strong>"._T('skeleditor:parseur_param')."</strong>".skel_parser_param($val->param)."<br />";
  143. $output .= "<strong>"._T('skeleditor:parseur_contenu')."</strong><br />";
  144. $output .= skel_parser_affiche( _T('skeleditor:parseur_avant'),$val->avant, '#cc9');
  145. $output .= skel_parser_affiche( _T('skeleditor:parseur_milieu'),$val->milieu, '#fc6');
  146. $output .= skel_parser_affiche( _T('skeleditor:parseur_apres'),$val->apres, '#fcc');
  147. $output .= skel_parser_affiche( _T('skeleditor:parseur_altern'),$val->altern, '#cfc');
  148. $output .= "</div>\n";
  149. $output .= fin_block()."<br />";
  150. /* version brute */
  151. /*
  152. $output .= "<strong>BOUCLE$k</strong><br />\n";
  153. foreach (get_object_vars($val) as $prop => $val2) {
  154. $output .= "\t<br />$prop = $val2\n";
  155. if (is_array($val2)) {
  156. foreach($val2 as $k3=>$val3) {
  157. $output .= "\t\t<br>........................$k3 = $val3\n";
  158. if (is_object($val3)) {
  159. foreach (get_object_vars($val3) as $prop4 => $val4) {
  160. $output .= "\t\t<br>+++........................( $prop4 = $val4 )\n";
  161. if (is_array($val4 )) {
  162. foreach($val4 as $k5=>$val5) {
  163. $output .= "\t\t<br>++++++...............$k5 = $val5 )\n";
  164. foreach($val5 as $k6=>$val6) {
  165. $output .= "\t\t<br>+++++++++++.........$k6 = $val6 )\n";
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }*/
  174. }
  175. $output .= "</div>";
  176. return $output;
  177. }
  178. // affiche le code pour le parseur
  179. function skel_parser_affiche($titre, $content, $bgcolor = '#fc6') {
  180. $output = "";
  181. $output .= "<div style='background:$bgcolor'>$titre</div>";
  182. foreach ($content as $k => $str) {
  183. if ($str->type == "champ") $output .= "<span style='color:#c30;background:#eee'>#".$str->nom_champ."</span>";
  184. else if ($str->type == "texte") $output .="<pre style='background:#ddd;margin:0;display:inline;'>&nbsp;".htmlspecialchars($str->texte)."</pre>";
  185. else if ($str->type == "include") $output .= "<span style='color:#30c;background:#cff;'>(INCLUDE)</span>";
  186. }
  187. return $output;
  188. }
  189. // parse param value
  190. function skel_parser_param($str,$output='') {
  191. $output = "";
  192. if (is_array($str)) {
  193. foreach($str as $k2=>$val2) {
  194. //$output .= ".....$k2=>$val2 ($c)<br />";
  195. $output .= skel_parser_param($val2,$output); // recursive
  196. }
  197. } else if (is_object($str)) {
  198. $output .= " {".$str->texte."} ";
  199. /*foreach (get_object_vars($str) as $prop4 => $val4) {
  200. $output .= "\t\t<br>...........( $prop4 = $val4 )\n";
  201. } */
  202. }
  203. return $output;
  204. }
  205. // security
  206. function check_file_allowed($file,$files_editable,$new = false) {
  207. if (in_array($file,$files_editable)) return true; // known file
  208. else if ($new){ // new file ?
  209. if (in_array(dirname($file),array_map('dirname',$files_editable))) return true; // known directory
  210. }
  211. return false;
  212. }
  213. // recupere le chemin du squelette a editer: dist ? plugin squelette ou squelettes ?
  214. function get_spip_path(){
  215. static $path_a = array();
  216. static $c = '';
  217. // on calcule le chemin si le nombre de plugins a change
  218. if ($c != count($GLOBALS['plugins']).$GLOBALS['dossier_squelettes']) {
  219. $c = count($GLOBALS['plugins']).$GLOBALS['dossier_squelettes'];
  220. // Chemin standard depuis l'espace public
  221. $path = defined('_SPIP_PATH') ? _SPIP_PATH :
  222. _DIR_RACINE.':'.
  223. _DIR_RACINE.'squelettes-dist/:'.
  224. _DIR_RACINE.'formulaires/:'.
  225. _DIR_RESTREINT;
  226. // Ajouter dist/
  227. $path = _DIR_RACINE.'squelettes-dist/:' . $path;
  228. // Ajouter les repertoires des plugins
  229. /* solution trop globale: il faut ajouter seulement les plugins de type "squelettes"
  230. if ($GLOBALS['plugins'])
  231. $path = _DIR_PLUGINS
  232. . join(':'._DIR_PLUGINS, $GLOBALS['plugins'])
  233. . ':' . $path;
  234. */
  235. if (count(get_plugin_squelette())>0)
  236. $path = join(':', get_plugin_squelette()).':'.$path;
  237. // Ajouter squelettes/
  238. if (@is_dir(_DIR_RACINE.'squelettes'))
  239. $path = _DIR_RACINE.'squelettes/:' . $path;
  240. // Et le(s) dossier(s) des squelettes nommes
  241. if ($GLOBALS['dossier_squelettes'])
  242. foreach (explode(':', $GLOBALS['dossier_squelettes']) as $d)
  243. $path =
  244. ($d[0] == '/' ? '' : _DIR_RACINE) . $d . '/:' . $path;
  245. // nettoyer les / du path
  246. $path_a = array();
  247. foreach (explode(':', $path) as $dir) {
  248. if (strlen($dir) AND substr($dir,-1) != '/')
  249. $dir .= "/";
  250. $path_a[] = $dir;
  251. }
  252. }
  253. return $path_a;
  254. }
  255. // recupere les plugins de type squelette
  256. function get_plugin_squelette() {
  257. // alternative 1: liste des plugins squelettes manuelle (blip, sarka, ...?)
  258. // alternative 2: on scanne les plugins: si article.html et sommaire.html present ? sans doute un plugin squelette
  259. $plugin_squelette = array();
  260. if ($GLOBALS['plugins']) {
  261. foreach($GLOBALS['plugins'] as $k) {
  262. if (@is_file(_DIR_PLUGINS."$k/article.html")&&@is_file(_DIR_PLUGINS."$k/sommaire.html"))
  263. $plugin_squelette[] = _DIR_PLUGINS.$k."/";
  264. }
  265. }
  266. return $plugin_squelette;
  267. }
  268. function parse_path($dir,$extensions){
  269. $pattern = "\.(".implode("|",$extensions).")$";
  270. $liste = preg_files($dir, $pattern);
  271. return $liste;
  272. }
  273. // tri la liste des fichiers en placant ceux a la racine en premier
  274. function sort_directory_first($files,$root) {
  275. $files_root = array();
  276. $files_directory = array();
  277. foreach($files as $file) {
  278. if (dirname($file)."/" != $root) $files_directory[] = $file;
  279. else $files_root[] = $file;
  280. }
  281. return array_merge($files_root,$files_directory);
  282. }
  283. ?>