source: trunk/spip/esqueleto-redcta/plugins/crayons/inc/crayons.php @ 69

Last change on this file since 69 was 69, checked in by guille, 16 years ago

Se agrego Plugin Crayon y se actualizaron los esqueletos para que funcione

File size: 11.3 KB
Line 
1<?php
2
3if (!defined("_ECRIRE_INC_VERSION")) return;
4
5define('_PREG_CRAYON', ',crayon\b[^<>\'"]+?\b((\w+)-(\w+)-(\d+(?:-\w+)?))\b,');
6
7// Compatibilite pour 1.92 : on a besoin de sql_fetch
8if ($GLOBALS['spip_version_code'] < '1.93' AND $f = charger_fonction('compat_crayons', 'inc'))
9        $f('sql_fetch');
10
11
12// Si un logo est demande, on renvoie la date dudit logo (permettra de gerer
13// un "modifie par ailleurs" si la date a change, rien de plus)
14function valeur_champ_logo($table, $id, $champ) {
15        $chercher_logo = charger_fonction('chercher_logo', 'inc');
16        $on = $chercher_logo($id, id_table_objet($table), 'on');
17        return $on ? filemtime($on[0]) : false;
18}
19
20// Idem : si un doc est demande, on renvoie la date du doc
21function valeur_champ_document($table, $id, $champ) {
22        $s = spip_query("SELECT date FROM spip_documents WHERE id_document="._q($id));
23        if ($t = sql_fetch($s))
24                return $t['date'];
25}
26
27// cette fonction de revision recoit le fichier upload a passer en logo
28// en reference : le nom du widget, pour aller chercher d'autres donnees
29// (ex: supprimer)
30function logo_revision($id, $file, $type, $ref) {
31
32        $chercher_logo = charger_fonction('chercher_logo', 'inc');
33        $_id_objet = id_table_objet($type);
34
35        // Chargement d'un nouveau logo ?
36        if ($file['logo']) {
37                define('FILE_UPLOAD', true); // message pour json_export :(
38
39                // supprimer l'ancien logo
40                $on = $chercher_logo($id, $_id_objet, 'on');
41                if ($on) @unlink($on[0]);
42
43                // ajouter le nouveau
44                include_spip('action/iconifier');
45                action_spip_image_ajouter_dist(
46                        type_du_logo($_id_objet).'on'.$id, false, false
47                ); // beurk
48        }
49
50        else
51
52        // Suppression du logo ?
53        if ($wid = array_pop($ref)
54        AND $_POST['content_'.$wid.'_logo_supprimer'] == 'on') {
55                if ($on = $chercher_logo($id, $_id_objet, 'on'))
56                        @unlink($on[0]);
57        }
58
59
60        // Reduire le logo ?
61        if (is_array($cfg = @unserialize($GLOBALS['meta']['crayons']))
62        AND $max = intval($cfg['reduire_logo'])) {
63                $on = $chercher_logo($id, $_id_objet, 'on');
64                include_spip('inc/filtres');
65                @copy($on[0], $temp = _DIR_VAR.'tmp'.rand(0,999).'.'.$on[3]);
66                $img1 = filtrer('image_reduire', $temp, $max);
67                $img2 = preg_replace(',[?].*,', '', extraire_attribut($img1, 'src'));
68                if (@file_exists($img2)
69                AND $img2 !=  $temp) {
70                        @unlink($on[0]);
71                        $dest = $on[1].$on[2].'.'
72                                .preg_replace(',^.*\.(gif|jpg|png)$,', '\1', $img2);
73                        @rename($img2,$dest);
74                }
75                @unlink($temp);
76        }
77
78        return true;
79}
80
81
82// cette fonction de revision recoit le fichier upload a passer en document
83function document_fichier_revision($id, $data, $type, $ref) {
84
85        $s = spip_query("SELECT * FROM spip_documents WHERE id_document="._q($id));
86        if (!$t = sql_fetch($s))
87                return false;
88
89        /*
90        // Envoi d'une URL de document distant ?
91        // TODO: verifier l'extension distante, sinon tout explose
92        if ($data['fichier']
93        AND preg_match(',^(https?|ftp)://.+,', $data['fichier'])) {
94                include_spip('inc/modifier');
95                modifier_contenu('document', $id,
96                        array('champs' => array('fichier', 'distant')),
97                        array('fichier' => $data['fichier'], 'distant' => 'oui')
98                );
99                return true;
100        }
101        else
102        */
103
104        // Chargement d'un nouveau doc ?
105        if ($data['document']) {
106
107                $ajouter_documents = charger_fonction('ajouter_documents', 'inc');
108                $arg = $data['document'];
109                check_upload_error($arg['error']);
110                $x = $ajouter_documents($arg['tmp_name'], $arg['name'], 
111                        'article', 0, 'document', null, $actifs);
112
113                // $actifs contient l'id_document nouvellement cree
114                // on recopie les donnees interessantes dans l'ancien
115                if ($id_new = array_pop($actifs)
116                AND $s = spip_query("SELECT fichier, taille, largeur, hauteur, extension, distant FROM spip_documents
117                        WHERE id_document="._q($id_new))
118                AND $new = sql_fetch($s)) {
119                        define('FILE_UPLOAD', true); // message pour json_export :(
120
121                        // Une vignette doit rester une image
122                        if ($t['mode'] == 'vignette'
123                        AND !in_array($new['extension'], array('jpg', 'gif', 'png')))
124                                return false;
125
126                        // Maintenant on est bon, on recopie les nouvelles donnees
127                        // dans l'ancienne ligne spip_documents
128                        include_spip('inc/modifier');
129                        modifier_contenu('document', $id,
130                                array('champs' => array_keys($new)),
131                                $new);
132
133                        // supprimer l'ancien document (sauf s'il etait distant)
134                        if ($t['distant'] != 'oui'
135                        AND file_exists(get_spip_doc($t['fichier'])))
136                                supprimer_fichier(get_spip_doc($t['fichier']));
137
138                        // Effacer la ligne temporaire de spip_document
139                        spip_query("DELETE FROM spip_documents WHERE id_document="._q($id_new));
140
141                        // oublier id_document temporaire (ca marche chez moi, sinon bof)
142                        spip_query("ALTER TABLE spip_documents AUTO_INCREMENT="._q($id_new));
143
144                        return true;
145                }
146        }
147
148}
149
150
151function colonne_table($table, $col) {
152        $nom_table = '';
153        if (!(($tabref = &crayons_get_table($table, $nom_table)) && ($brut = $tabref['field'][$col]))) {
154                return false;
155        }
156        $ana = explode(' ', $brut);
157        $sta = 0;
158        $sep = '';
159        $ret = array('brut' => $brut,
160                'type' => '', 'notnull' => false, 'long' => 0, 'def' => '');
161        foreach ($ana as $mot) {
162                switch ($sta) {
163                        case 0: $ret['type'] = ($mot = strtolower($mot));
164                        case 1: if ($mot[strlen($mot) - 1] == ')') {
165                                        $pos = strpos($mot, '(');
166                                        $ret['type'] = strtolower(substr($mot, 0, $pos++));
167                                        $vir = explode(',', substr($mot, $pos, -1));
168                                        if ($ret['type'] == 'enum') {
169                                                $ret['enum'] = $vir;
170                                        } elseif (count($vir) > 1) {
171                                                $ret['long'] = $vir;
172                                        } else {
173                                                $ret['long'] = $vir[0];
174                                        }
175                                        $sta = 1;
176                                        continue;
177                                }
178                                if (!$sta) {
179                                        $sta = 1;
180                                        continue;
181                                }
182                        case 2: switch (strtolower($mot)) {
183                                case 'not':
184                                        $sta = 3;
185                                        continue;
186                                case 'default':
187                                        $sta = 4;
188                                        continue;
189                                }
190                                continue;
191                        case 3:         $ret['notnull'] = strtolower($mot) == 'null';
192                                $sta = 2;
193                                continue;
194                        case 4: $df1 = strpos('"\'', $mot[0]) !== false? $mot[0] : '';
195                                $sta = 5;
196                        case 5: $ret['def'] .= $sep . $mot;
197                                if (!$df1) {
198                                        $sta = 2;
199                                        continue;
200                                }
201                                if ($df1 == $mot[strlen($mot) - 1]) {
202                                        $ret['def'] = substr($ret['def'], 1, -1);
203                                        $sta = 2;
204                                }
205                                $sep = ' ';
206                                continue;
207                }
208        }
209        return $ret;
210}
211//      var_dump(colonne_table('forum', 'id_syndic')); die();
212
213function table_where($table, $id)
214{
215        $nom_table = '';
216        if (!(($tabref = &crayons_get_table($table, $nom_table))
217                        && ($tabid = explode(',', $tabref['key']['PRIMARY KEY'])))) {
218                spip_log('crayons: table ' . $table . ' inconnue');
219                return array(false, false);
220        }
221        if (is_scalar($id)) {
222                $id = explode('-', $id);
223        }
224        $where = $and = '';
225        foreach ($id as $idcol => $idval) {
226                $where .= $and . '`' . (is_int($idcol) ? trim($tabid[$idcol]) : $idcol) . '`=' . _q($idval);
227                $and = ' AND ';
228        }
229        return array($nom_table, $where);
230}
231//      var_dump(colonne_table('forum', 'id_syndic')); die();
232
233function valeur_colonne_table_dist($table, $col, $id) {
234        list($nom_table, $where) = table_where($table, $id);
235        if (!$nom_table)
236                return false;
237
238        $r = array();
239
240        // valeurs non SQL
241        foreach ($col as $champ) {
242                if (function_exists($f = 'valeur_champ_'.$table.'_'.$champ) OR function_exists($f = 'valeur_champ_'.$champ)) {
243                        $r[$champ] = $f($table, $id, $champ);
244                        $col = array_diff($col, array($champ));
245                }
246        }
247
248        // valeurs SQL
249        if (count($col)
250        AND $s = spip_query(
251                        'SELECT `' . implode($col, '`, `') .
252                        '` FROM ' . $nom_table . ' WHERE ' . $where)
253        AND $t = sql_fetch($s))
254                $r = array_merge($r, $t);
255
256        return $r;
257}
258
259function valeur_colonne_table($table, $col, $id) {
260        if (!is_array($col))
261                $col = array($col);
262
263        if (function_exists($f = $table.'_valeur_colonne_table_dist')
264        OR function_exists($f = $table.'_valeur_colonne_table')
265        OR $f = 'valeur_colonne_table_dist')
266                return $f($table, $col, $id);
267}
268
269/**
270    * Transform a variable into its javascript equivalent (recursive)
271    * @access private
272    * @param mixed the variable
273    * @return string js script | boolean false if error
274    */
275function var2js($var) {
276    $asso = false;
277    switch (true) {
278        case is_null($var) :
279            return 'null';
280        case is_string($var) :
281            return '"' . str_replace('&', '\x26', addcslashes($var, "\"\\\n\r")) . '"';
282        case is_bool($var) :
283            return $var ? 'true' : 'false';
284        case is_scalar($var) :
285            return $var;
286        case is_object( $var) :
287            $var = get_object_vars($var);
288            $asso = true;
289        case is_array($var) :
290            $keys = array_keys($var);
291            $ikey = count($keys);
292            while (!$asso && $ikey--) {
293                $asso = $ikey !== $keys[$ikey];
294            }
295            $sep = '';
296            if ($asso) {
297                $ret = '{';
298                foreach ($var as $key => $elt) {
299                    $ret .= $sep . '"' . $key . '":' . var2js($elt);
300                    $sep = ',';
301                }
302                return $ret ."}\n";
303            } else {
304                $ret = '[';
305                foreach ($var as $elt) {
306                    $ret .= $sep . var2js($elt);
307                    $sep = ',';
308                }
309                return $ret ."]\n";
310            }
311    }
312    return false;
313}
314
315function json_export($var) {
316        $var = var2js($var);
317
318        // flag indiquant qu'on est en iframe et qu'il faut proteger nos
319        // donnees dans un <textarea> ; attention $_FILES a ete vide par array_pop
320        if (defined('FILE_UPLOAD'))
321                return "<textarea>".htmlspecialchars($var)."</textarea>";
322        else
323                return $var;
324}
325
326function return_log($var) {
327        die(json_export(array('$erreur'=> var_export($var,true))));
328}
329
330function _U($texte)
331{
332    include_spip('inc/charsets');
333    return unicode2charset(html2unicode(_T($texte)));
334}
335
336function wdgcfg() {
337        $php = function_exists('crayons_config') ? crayons_config() : array();
338        include_spip('inc/meta');
339        lire_metas();
340        global $meta;
341        $metacrayons = empty($meta['crayons']) ? array() : unserialize($meta['crayons']);
342        $wdgcfg = array();
343        foreach (array(
344                'msgNoChange' => false,
345                'msgAbandon' => false,  /* etait: true */
346                'filet' => false,
347                'yellow_fade' => false,
348                'clickhide' => false /* etait: true */
349        )
350        as $cfgi => $def) {
351                $wdgcfg[$cfgi] = isset($php[$cfgi]) ? $php[$cfgi] :
352                        isset($metacrayons[$cfgi]) ? $metacrayons[$cfgi] : $def;
353        }
354        return $wdgcfg;
355}
356
357function &crayons_get_table($table, &$nom_table) {
358        static $catab = array('tables_principales',     'tables_auxiliaires');
359        static $return = array();
360        static $noms = array();
361        if (!isset($return[$table])) {
362                $return[$table] = $noms[$table] = '';
363                include_spip('base/serial');
364                include_spip('base/auxiliaires');
365                include_spip('public/parametrer');
366                $try = array('spip_'.table_objet($table), 'spip_' . $table . 's', $table . 's', 'spip_' . $table, $table);
367                foreach ($catab as $i=>$categ) {
368                        // 1ere possibilite : c'est connu de SPIP
369                        foreach ($try as $nom) {
370                                if (isset($GLOBALS[$categ][$nom])) {
371                                        $noms[$table] = $nom;
372                                        $return[$table] = & $GLOBALS[$categ][$nom];
373                                        break 2;
374                                }
375                        }
376                        // seconde possibilite : regarder directement la base
377                        if (function_exists('sql_showtable'))
378                        foreach ($try as $nom) {
379                                if ($q = sql_showtable($nom)) {
380                                        $noms[$table] = $nom;
381                                        $return[$table] = $q;
382                                }
383                        }
384                }
385        }
386
387        $nom_table = $noms[$table];
388        return $return[$table];
389}
390
391?>
Note: See TracBrowser for help on using the repository browser.