PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/skeleditor/spip_210/prive/exec/skeleditor_fonctions.php

https://bitbucket.org/pombredanne/spip-zone-treemap
PHP | 334 lines | 228 code | 34 blank | 72 comment | 30 complexity | 480a293aed2456ad548045af73c8c7ba MD5 | raw file
  1. <?php
  2. /**
  3. * Plugin SkelEditor
  4. * Editeur de squelette en ligne
  5. * (c) 2007-2010 erational
  6. * Licence GPL-v3
  7. *
  8. */
  9. function skeleditor_dossier(){
  10. include_spip('inc/skeleditor');
  11. return skeleditor_path_editable();
  12. }
  13. /**
  14. * Afficher l'arborescence du dossier squelette
  15. *
  16. * @param string $path_base
  17. * @param string $current_file
  18. * @return string
  19. */
  20. function skeleditor_afficher_dir_skel($path_base,$current_file) {
  21. include_spip('inc/skeleditor');
  22. include_spip('inc/presentation');
  23. $file_list = skeleditor_files_editables($path_base);
  24. $current_file = substr($current_file,strlen($path_base));
  25. $output = "<div id='arbo'><div class='dir'>\n";
  26. $init_dir = $current_dir = "";
  27. foreach($file_list as $file){
  28. $dir = substr(dirname($file),strlen($path_base));
  29. $file = substr($file,strlen($path_base));
  30. if ($dir != $current_dir)
  31. $output .= skeleditor_tree_open_close_dir($current_dir,$dir,$current_file);
  32. $class="fichier";
  33. if (!is_writable($path_base.$dir))
  34. $class .= " readonly";
  35. $class .= ($file==$current_file?" on":'');
  36. $icon = "file";
  37. if (preg_match(',('._SE_EXTENSIONS_IMG.')$,',$file))
  38. $icon = "image";
  39. $output .= "<a href='".generer_url_ecrire('skeleditor','f='.urlencode($path_base.$file))."' class='$class'>"
  40. . "<img src='"._DIR_PLUGIN_SKELEDITOR."spip_210/img_pack/se-$icon-16.png' alt='$icon' /> "
  41. .basename($file)."</a>";
  42. }
  43. $output .= skeleditor_tree_open_close_dir($current_dir,$init_dir,$current_file);
  44. $output .= "</div></div>\n";
  45. return $output;
  46. }
  47. // build select menu to choose directory
  48. function editor_form_directory($path,$depth="") {
  49. $output = "";
  50. foreach($path as $dir){
  51. $tdir = explode('/',$dir);
  52. $myfile = array_pop($tdir);
  53. $depth = "";
  54. while(array_pop($tdir)) $depth.="&nbsp;&nbsp;";
  55. $output .= "<option value='$dir'>$depth$myfile</option>\n";
  56. }
  57. return $output;
  58. }
  59. // add file form
  60. function skeleditor_addfile($path_list) {
  61. //$output = bouton_block_invisible('editor_newfile');
  62. $output .= "<img src='"._DIR_PLUGIN_SKELEDITOR."spip_210/img_pack/action_add.png' alt='new' />"._T("skeleditor:fichier_nouveau");
  63. //$output .= debut_block_invisible('editor_newfile');
  64. $output .= "<form method='get'>\n";
  65. $output .= "<input type='hidden' name='exec' value='skeleditor' />";
  66. $output .= "<input type='hidden' name='operation' value='new' />";
  67. //$output .= "nom du fichier:<br />\n";
  68. $output .= "<input type='text' name='f' value='untitled.html' onfocus=\"this.value=''\" />";
  69. $output .= _T("skeleditor:target")."<br />\n";
  70. $output .= "<select name='target'><br />\n";
  71. $output .= editor_form_directory($path_list);
  72. $output .= "</select><br /><input type='submit' name='sub' value='"._T("skeleditor:creer")."' />";
  73. $output .= "</form>\n";
  74. $output .= fin_block();
  75. return $output;
  76. }
  77. // upload file form
  78. function skeleditor_uploadfile($path_list) {
  79. //$output = "<br />".bouton_block_invisible('editor_uploadfile');
  80. $output .= "<img src='"._DIR_PLUGIN_SKELEDITOR."spip_210/img_pack/action_add.png' alt='new' />"._T("skeleditor:fichier_upload");
  81. //$output .= debut_block_invisible('editor_uploadfile');
  82. $output .= "<form method='post' enctype='multipart/form-data' >\n";
  83. $output .= "<input type='hidden' name='exec' value='skeleditor' />";
  84. $output .= "<input type='hidden' name='operation' value='upload' />";
  85. $output .= "<input type='hidden' name='MAX_FILE_SIZE' value='200000' />";
  86. $output .= "<input type='file' name='upf'/>";
  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:upload")."' />";
  91. $output .= "</form>\n";
  92. $output .= fin_block();
  93. return $output;
  94. }
  95. // skeleton parsing (more details: public/phrase_html)
  96. function skel_parser($skel_str) {
  97. include_spip("public/interfaces");
  98. include_spip("public/phraser_html");
  99. //include_spip("public/debug");
  100. //$output .= _T('skeleditor:parseur_titre');
  101. $output .= "<div style='background: #eef; border:1px solid #eee;padding:10px;font-size:0.82em;font-family:Verdana'>";
  102. $boucles = array();
  103. $b = public_phraser_html($skel_str, 0, $boucles, 'skel_editor');
  104. $boucles = array_reverse($boucles,TRUE);
  105. /* parse outside boucles */
  106. //$output .= bouton_block_invisible("hors_boucle")._T("skeleditor:parseur_horsboucle");
  107. //$output .= debut_block_invisible("hors_boucle");
  108. $output .= "<div style='background: #fff;padding:10px;'>";
  109. foreach($b as $k=>$val) {
  110. if ($val->type == "champ") $output .= "<span style='color:#c30;background:#eee'>#".$val->nom_champ."</span>";
  111. else if ($val->type == "texte") $output .="<pre style='background:#ddd;margin:0;display:inline;'>&nbsp;".htmlspecialchars($val->texte)."</pre>";
  112. else if ($val->type == "include") $output .= "<span style='color:#30c;background:#cff;'>(INCLURE)</span>";
  113. }
  114. $output .= "</div>\n";
  115. $output .= fin_block()."<br />";
  116. /* parse boucles */
  117. foreach($boucles as $k=>$val) {
  118. /* version gentle */
  119. //$output .= bouton_block_invisible("skel_parser_$k")." BOUCLE$k";
  120. $output .= " <span style='color:#888'>(".strtoupper($val->type_requete).")</span>";
  121. //$output .= debut_block_invisible("skel_parser_$k");
  122. $output .= "<div style='background: #fff;padding:10px;'>";
  123. if ($val->id_parent) $output .= "<strong>id_parent:</strong> BOUCLE$val->id_parent<br />";
  124. if ($val->param) $output .= "<strong>"._T('skeleditor:parseur_param')."</strong>".skel_parser_param($val->param)."<br />";
  125. $output .= "<strong>"._T('skeleditor:parseur_contenu')."</strong><br />";
  126. $output .= skel_parser_affiche( _T('skeleditor:parseur_avant'),$val->avant, '#cc9');
  127. $output .= skel_parser_affiche( _T('skeleditor:parseur_milieu'),$val->milieu, '#fc6');
  128. $output .= skel_parser_affiche( _T('skeleditor:parseur_apres'),$val->apres, '#fcc');
  129. $output .= skel_parser_affiche( _T('skeleditor:parseur_altern'),$val->altern, '#cfc');
  130. $output .= "</div>\n";
  131. $output .= fin_block()."<br />";
  132. /* version brute */
  133. /*
  134. $output .= "<strong>BOUCLE$k</strong><br />\n";
  135. foreach (get_object_vars($val) as $prop => $val2) {
  136. $output .= "\t<br />$prop = $val2\n";
  137. if (is_array($val2)) {
  138. foreach($val2 as $k3=>$val3) {
  139. $output .= "\t\t<br>........................$k3 = $val3\n";
  140. if (is_object($val3)) {
  141. foreach (get_object_vars($val3) as $prop4 => $val4) {
  142. $output .= "\t\t<br>+++........................( $prop4 = $val4 )\n";
  143. if (is_array($val4 )) {
  144. foreach($val4 as $k5=>$val5) {
  145. $output .= "\t\t<br>++++++...............$k5 = $val5 )\n";
  146. foreach($val5 as $k6=>$val6) {
  147. $output .= "\t\t<br>+++++++++++.........$k6 = $val6 )\n";
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }*/
  156. }
  157. $output .= "</div>";
  158. return $output;
  159. }
  160. // affiche le code pour le parseur
  161. function skel_parser_affiche($titre, $content, $bgcolor = '#fc6') {
  162. $output = "";
  163. $output .= "<div style='background:$bgcolor'>$titre</div>";
  164. foreach ($content as $k => $str) {
  165. if ($str->type == "champ") $output .= "<span style='color:#c30;background:#eee'>#".$str->nom_champ."</span>";
  166. else if ($str->type == "texte") $output .="<pre style='background:#ddd;margin:0;display:inline;'>&nbsp;".htmlspecialchars($str->texte)."</pre>";
  167. else if ($str->type == "include") $output .= "<span style='color:#30c;background:#cff;'>(INCLUDE)</span>";
  168. }
  169. return $output;
  170. }
  171. // parse param value
  172. function skel_parser_param($str,$output='') {
  173. $output = "";
  174. if (is_array($str)) {
  175. foreach($str as $k2=>$val2) {
  176. //$output .= ".....$k2=>$val2 ($c)<br />";
  177. $output .= skel_parser_param($val2,$output); // recursive
  178. }
  179. } else if (is_object($str)) {
  180. $output .= " {".$str->texte."} ";
  181. /*foreach (get_object_vars($str) as $prop4 => $val4) {
  182. $output .= "\t\t<br>...........( $prop4 = $val4 )\n";
  183. } */
  184. }
  185. return $output;
  186. }
  187. // recupere les plugins de type squelette
  188. function get_plugin_squelette() {
  189. // alternative 1: liste des plugins squelettes manuelle (blip, sarka, ...?)
  190. // alternative 2: on scanne les plugins: si article.html et sommaire.html present ? sans doute un plugin squelette
  191. $plugin_squelette = array();
  192. if ($GLOBALS['plugins']) {
  193. foreach($GLOBALS['plugins'] as $k) {
  194. if (@is_file(_DIR_PLUGINS."$k/article.html")&&@is_file(_DIR_PLUGINS."$k/sommaire.html"))
  195. $plugin_squelette[] = _DIR_PLUGINS.$k."/";
  196. }
  197. }
  198. return $plugin_squelette;
  199. }
  200. function skeleditor_codemirror($filename,$editable=true){
  201. if (!$filename)
  202. return "";
  203. $infos = pathinfo($filename);
  204. switch($infos['extension']){
  205. case 'sh':
  206. case 'txt':
  207. case 'nfo':
  208. case 'log':
  209. case 'csv':
  210. $parsers = null;
  211. break;
  212. case 'as':
  213. case 'js':
  214. $parsers = array("tokenizejavascript.js", "parsejavascript.js");
  215. $css = array("css/jscolors.css");
  216. // autoMatchParens: true
  217. break;
  218. case 'css':
  219. $parsers = array("parsecss.js");
  220. $css = array("css/csscolors.css");
  221. break;
  222. case 'xml':
  223. case 'svg':
  224. case 'rdf':
  225. $parsers = array("parsexml.js");
  226. $css = array("css/xmlcolors.css");
  227. #continuousScanning: 500,
  228. break;
  229. case 'sql':
  230. $parsers = array("../contrib/sql/js/parsesql.js");
  231. $css = array("css/sqlcolors.css");
  232. #textWrapping: false,
  233. break;
  234. case 'py':
  235. $parsers = array("../contrib/python/js/parsepython.js");
  236. $css = array("css/pythoncolors.css");
  237. # lineNumbers: true,
  238. # textWrapping: false,
  239. # indentUnit: 4,
  240. # parserConfig: {'pythonVersion': 2, 'strictErrors': true}
  241. break;
  242. case 'php':
  243. case 'html':
  244. case 'htm':
  245. default:
  246. $parsers = array("parsexml.js", "parsecss.js", "tokenizejavascript.js", "parsejavascript.js",
  247. "../contrib/php/js/tokenizephp.js", "../contrib/php/js/parsephp.js",
  248. "../contrib/php/js/parsephphtmlmixed.js");
  249. $css = array("css/xmlcolors.css", "css/jscolors.css", "css/csscolors.css", "contrib/php/css/phpcolors.css");
  250. break;
  251. }
  252. if (is_null($parsers))
  253. return "";
  254. $readonly = "";
  255. if (!$editable)
  256. $readonly = "readonly: true,\n";
  257. $dir = _DIR_PLUGIN_SKELEDITOR.'spip_210/codemirror/';
  258. $dirjs = _DIR_PLUGIN_SKELEDITOR.'spip_210/codemirror/js/';
  259. $script = '<script src="'._DIR_PLUGIN_SKELEDITOR.'spip_210/codemirror/js/codemirror.js" type="text/javascript"></script>
  260. <script type="text/javascript">
  261. var editor;
  262. function init_code_edit(){
  263. if (jQuery("#code").is(":visible")){
  264. editor = CodeMirror.fromTextArea(\'code\', {
  265. height: "550px",
  266. parserfile: ["'.implode('", "',$parsers).'"],
  267. stylesheet: ["'.$dir.implode('", "'.$dir,$css).'"],
  268. path: "'._DIR_PLUGIN_SKELEDITOR.'spip_210/codemirror/js/",
  269. continuousScanning: 500,
  270. textWrapping: false,
  271. readOnly: jQuery("#code").attr("readonly"),
  272. lineNumbers: true
  273. });
  274. }
  275. }
  276. init_code_edit();
  277. </script>
  278. <style type="text/css">
  279. .codewrap {border:1px solid #333;background:#fff;font-size:11px;clear:both;}
  280. .codewrap.readonly {background:#ddd;}
  281. .CodeMirror-line-numbers {
  282. width: 2.2em;
  283. color: #aaa;
  284. background-color: #eee;
  285. text-align: right;
  286. padding-right: .3em;
  287. font-size: 10pt;
  288. font-family: monospace !important;
  289. padding-top: .4em;
  290. line-height:15px;
  291. }
  292. .CodeMirror-line-numbers *{
  293. font-family: monospace !important;
  294. }
  295. </style>
  296. ';
  297. return $script;
  298. }
  299. ?>