[SPIP] v3.2.1-->v3.2.3
[lhc/web/www.git] / www / plugins-dist / textwheel / inc / textwheel.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2019 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 if (!defined("_ECRIRE_INC_VERSION")) {
14 return;
15 }
16
17 include_spip('engine/textwheel');
18 // si une regle change et rend son cache non valide
19 // incrementer ce define au numero de commit concerne
20 // (inconsistence entre la wheel et l'inclusion php)
21 if (!defined('_WHEELS_VERSION')) {
22 define('_WHEELS_VERSION', 68672);
23 }
24
25 //
26 // Definition des principales wheels de SPIP
27 //
28 if (!isset($GLOBALS['spip_wheels'])) {
29 $GLOBALS['spip_wheels'] = array();
30 }
31
32 // Si le tableau des raccourcis existe déjà
33 if (!isset($GLOBALS['spip_wheels']['raccourcis']) or !is_array($GLOBALS['spip_wheels']['raccourcis'])) {
34 $GLOBALS['spip_wheels']['raccourcis'] = array(
35 'spip/spip.yaml',
36 'spip/spip-paragrapher.yaml'
37 );
38 } else {
39 $GLOBALS['spip_wheels']['raccourcis'] = array_merge(
40 array(
41 'spip/spip.yaml',
42 'spip/spip-paragrapher.yaml'
43 ),
44 $GLOBALS['spip_wheels']['raccourcis']
45 );
46 }
47
48 if (test_espace_prive()) {
49 $GLOBALS['spip_wheels']['raccourcis'][] = 'spip/ecrire.yaml';
50 }
51
52 $GLOBALS['spip_wheels']['interdire_scripts'] = array(
53 'spip/interdire-scripts.yaml'
54 );
55
56 $GLOBALS['spip_wheels']['echappe_js'] = array(
57 'spip/echappe-js.yaml'
58 );
59
60 $GLOBALS['spip_wheels']['paragrapher'] = array(
61 'spip/spip-paragrapher.yaml'
62 );
63
64 $GLOBALS['spip_wheels']['listes'] = array(
65 'spip/spip-listes.yaml'
66 );
67
68 //
69 // Methode de chargement d'une wheel SPIP
70 //
71
72 class SPIPTextWheelRuleset extends TextWheelRuleSet {
73 protected function findFile(&$file, $path = '') {
74 static $default_path;
75
76 // absolute file path?
77 if (file_exists($file)) {
78 return $file;
79 }
80
81 // file include with texwheels, relative to calling ruleset
82 if ($path and file_exists($f = $path . $file)) {
83 return $f;
84 }
85
86 return find_in_path($file, 'wheels/');
87 }
88
89 public static function &loader($ruleset, $callback = '', $class = 'SPIPTextWheelRuleset') {
90
91 # memoization
92 # attention : le ruleset peut contenir apres loading des chemins relatifs
93 # il faut donc que le cache depende du chemin courant vers la racine de SPIP
94 $key = "";
95 if ($callback) {
96 $key = $callback($key);
97 }
98 $key = 'tw-' . md5(_WHEELS_VERSION . "-" . serialize($ruleset) . $key . $class . _DIR_RACINE);
99
100 # lecture du cache
101 if ((!defined('_VAR_MODE') or _VAR_MODE != 'recalcul')
102 and $cacheruleset = tw_cache_get($key)
103 ) {
104 return $cacheruleset;
105 }
106
107 # calcul de la wheel
108 $ruleset = parent::loader($ruleset, $callback, $class);
109
110 # ecriture du cache
111 tw_cache_set($key, $ruleset);
112
113 return $ruleset;
114 }
115 }
116
117
118 function tw_trig_purger($quoi) {
119 if ($quoi == 'cache') {
120 purger_repertoire(_DIR_CACHE . "wheels");
121 }
122
123 return $quoi;
124 }
125
126
127 /**
128 * Lire une valeur en cache
129 * memoization minimale
130 * (utilise le plugin memoization si disponible)
131 *
132 * @param string $key
133 * @return mixed
134 */
135 function tw_cache_get($key) {
136 if (function_exists('cache_get')) {
137 return cache_get($key);
138 }
139
140 return @unserialize(file_get_contents(_DIR_CACHE . "wheels/" . $key . ".txt"));
141 }
142
143 /**
144 * Ecrire une valeur en cache
145 * memoization minimale
146 * (utilise le plugin memoization si disponible)
147 *
148 * @param string $key
149 * @param mixed $value
150 * @param int|null $ttl
151 * @return bool
152 */
153 function tw_cache_set($key, $value, $ttl = null) {
154 if (function_exists('cache_set')) {
155 return cache_set($key, $value, $ttl);
156 }
157 $dir = sous_repertoire(_DIR_CACHE, "wheels/");
158
159 return ecrire_fichier($dir . $key . ".txt", serialize($value));
160 }