[PLUGINS] ~maj des plugins
[lhc/web/www.git] / www / plugins / crayons / inc / crayons.php
index 6bf5b4b..ed40b6e 100644 (file)
@@ -1,4 +1,10 @@
 <?php\r
+/**\r
+ * Crayons \r
+ * plugin for spip \r
+ * (c) Fil, toggg 2006-2013\r
+ * licence GPL\r
+ */\r
 \r
 if (!defined("_ECRIRE_INC_VERSION")) return;\r
 \r
@@ -12,14 +18,14 @@ if ($GLOBALS['spip_version_code'] < '1.93' AND $f = charger_fonction('compat_cra
 // Par defaut : oui (pour les admins complets, si autoriser_defaut_dist()) ;\r
 // mettre a false en cas de mutualisation par prefixe de table,\r
 // sinon on ne peut pas garantir que les sites sont hermetiques\r
-define('_CRAYONS_TABLES_EXTERNES', true);\r
+if(!defined('_CRAYONS_TABLES_EXTERNES'))\r
+       define('_CRAYONS_TABLES_EXTERNES', true);\r
 \r
 // Autorisations non prevues par le core\r
 include_spip('inc/autoriser');\r
 \r
 include_spip('inc/crayons-json');\r
 \r
-\r
 if (!function_exists('autoriser_meta_modifier_dist')) {\r
 /**\r
  * Autorisation d'éditer les configurations dans spip_meta\r
@@ -62,20 +68,20 @@ if (!function_exists('autoriser_message_modifier_dist')) {
 //compat 192 documents\r
 if ($GLOBALS['spip_version_code'] < '1.93'){\r
        if (!function_exists('get_spip_doc')){\r
-                       function get_spip_doc($fichier) {\r
-                                       // fichier distant\r
-                                       if (preg_match(',^\w+://,', $fichier))\r
-                                                       return $fichier;\r
-\r
-                                       // gestion d'erreurs, fichier=''\r
-                                       if (!strlen($fichier))\r
-                                                       return false;\r
-\r
-                                       // fichier normal\r
-                                       return (strpos($fichier, _DIR_IMG) === false)\r
-                                       ? _DIR_IMG . $fichier\r
-                                       : $fichier;\r
-                  }\r
+               function get_spip_doc($fichier) {\r
+                       // fichier distant\r
+                       if (preg_match(',^\w+://,', $fichier))\r
+                                       return $fichier;\r
+\r
+                       // gestion d'erreurs, fichier=''\r
+                       if (!strlen($fichier))\r
+                                       return false;\r
+\r
+                       // fichier normal\r
+                       return (strpos($fichier, _DIR_IMG) === false)\r
+                       ? _DIR_IMG . $fichier\r
+                       : $fichier;\r
+               }\r
        }\r
 }\r
 \r
@@ -140,7 +146,6 @@ function valeur_champ_vignette($table, $id, $champ) {
 // en reference : le nom du widget, pour aller chercher d'autres donnees\r
 // (ex: supprimer)\r
 function logo_revision($id, $file, $type, $ref) {\r
-\r
        $chercher_logo = charger_fonction('chercher_logo', 'inc');\r
        $_id_objet = id_table_objet($type);\r
 \r
@@ -168,7 +173,6 @@ function logo_revision($id, $file, $type, $ref) {
                        @unlink($on[0]);\r
        }\r
 \r
-\r
        // Reduire le logo ?\r
        if (is_array($cfg = @unserialize($GLOBALS['meta']['crayons']))\r
        AND $max = intval($cfg['reduire_logo'])) {\r
@@ -194,7 +198,7 @@ function logo_revision($id, $file, $type, $ref) {
 // cette fonction de revision recoit le fichier upload a passer en document\r
 function document_fichier_revision($id, $data, $type, $ref) {\r
 \r
-       $s = spip_query("SELECT * FROM spip_documents WHERE id_document="._q($id));\r
+       $s = spip_query("SELECT * FROM spip_documents WHERE id_document=".intval($id));\r
        if (!$t = sql_fetch($s))\r
                return false;\r
 \r
@@ -216,64 +220,80 @@ function document_fichier_revision($id, $data, $type, $ref) {
        // Chargement d'un nouveau doc ?\r
        if ($data['document']) {\r
 \r
-               $ajouter_documents = charger_fonction('ajouter_documents', 'inc');\r
                $arg = $data['document'];\r
-               check_upload_error($arg['error']);\r
-               $x = $ajouter_documents($arg['tmp_name'], $arg['name'],\r
-                       'article', 0, 'document', null, $actifs);\r
-\r
-               // $actifs contient l'id_document nouvellement cree\r
-               // on recopie les donnees interessantes dans l'ancien\r
-               $extension=", extension ";\r
-               //compat 192\r
-               if ($GLOBALS['spip_version_code'] < '1.93')\r
-                       $extension="";\r
-\r
-               if ($id_new = array_pop($actifs)\r
-               AND $s = spip_query("SELECT fichier, taille, largeur, hauteur $extension, distant FROM spip_documents\r
-                       WHERE id_document="._q($id_new))\r
-               AND $new = sql_fetch($s)) {\r
-                       define('FILE_UPLOAD', true); // message pour crayons_json_export :(\r
-\r
-                       // Une vignette doit rester une image\r
-                       if ($t['mode'] == 'vignette'\r
-                       AND !in_array($new['extension'], array('jpg', 'gif', 'png')))\r
+               \r
+               /** \r
+                * Méthode >= SPIP 3.0 \r
+                * ou SPIP 2.x + Mediathèque\r
+                */ \r
+               if($ajouter_documents = charger_fonction('ajouter_documents','action',true)){ \r
+                       $actifs = $ajouter_documents($id,array($arg),'', 0,$t['mode']);\r
+                       $x = reset($actifs);\r
+                       if(is_numeric($x))\r
+                               return true;\r
+                       else\r
                                return false;\r
-\r
-                       // Maintenant on est bon, on recopie les nouvelles donnees\r
-                       // dans l'ancienne ligne spip_documents\r
-                       include_spip('inc/modifier');\r
-                       modifier_contenu('document', $id,\r
-                               # 'champs' inutile a partir de SPIP 11348\r
-                               array('champs' => array_keys($new)),\r
-                               $new);\r
-\r
-                       // supprimer l'ancien document (sauf s'il etait distant)\r
-                       if ($t['distant'] != 'oui'\r
-                       AND file_exists(get_spip_doc($t['fichier'])))\r
-                               supprimer_fichier(get_spip_doc($t['fichier']));\r
-\r
-                       // Effacer la ligne temporaire de spip_document\r
-                       spip_query("DELETE FROM spip_documents WHERE id_document="._q($id_new));\r
-\r
-                       // oublier id_document temporaire (ca marche chez moi, sinon bof)\r
-                       spip_query("ALTER TABLE spip_documents AUTO_INCREMENT="._q($id_new));\r
-\r
-                       return true;\r
+               }\r
+               /**\r
+                * Méthode SPIP < 3.0\r
+                */\r
+               else if($ajouter_documents = charger_fonction('ajouter_documents','inc',true)){ \r
+                       check_upload_error($arg['error']);\r
+                       $x = $ajouter_documents($arg['tmp_name'], $arg['name'],\r
+                                       'article', 0, 'document', null, $actifs);\r
+                       // $actifs contient l'id_document nouvellement cree\r
+                       // on recopie les donnees interessantes dans l'ancien\r
+                       $extension=", extension ";\r
+                       //compat 192\r
+                       if ($GLOBALS['spip_version_code'] < '1.93')\r
+                               $extension="";\r
+\r
+                       if ($id_new = array_pop($actifs)\r
+                       AND $s = spip_query("SELECT fichier, taille, largeur, hauteur $extension, distant FROM spip_documents\r
+                               WHERE id_document="._q($id_new))\r
+                       AND $new = sql_fetch($s)) {\r
+                               define('FILE_UPLOAD', true); // message pour crayons_json_export :(\r
+\r
+                               // Une vignette doit rester une image\r
+                               if ($t['mode'] == 'vignette'\r
+                               AND !in_array($new['extension'], array('jpg', 'gif', 'png')))\r
+                                       return false;\r
+\r
+                               // Maintenant on est bon, on recopie les nouvelles donnees\r
+                               // dans l'ancienne ligne spip_documents\r
+                               include_spip('inc/modifier');\r
+                               modifier_contenu('document', $id,\r
+                                       # 'champs' inutile a partir de SPIP 11348\r
+                                       array('champs' => array_keys($new)),\r
+                                       $new);\r
+\r
+                               // supprimer l'ancien document (sauf s'il etait distant)\r
+                               if ($t['distant'] != 'oui'\r
+                               AND file_exists(get_spip_doc($t['fichier'])))\r
+                                       supprimer_fichier(get_spip_doc($t['fichier']));\r
+\r
+                               // Effacer la ligne temporaire de spip_document\r
+                               spip_query("DELETE FROM spip_documents WHERE id_document="._q($id_new));\r
+\r
+                               // oublier id_document temporaire (ca marche chez moi, sinon bof)\r
+                               spip_query("ALTER TABLE spip_documents AUTO_INCREMENT="._q($id_new));\r
+\r
+                               return true;\r
+                       }\r
                }\r
        }\r
-\r
 }\r
 \r
 // cette fonction de revision soit supprime la vignette d'un document,\r
 // soit recoit le fichier upload a passer ou remplacer la vignette du document\r
 function vignette_revision($id, $data, $type, $ref) {\r
-       $s = sql_fetsel("*","spip_documents","id_document=".intval($id));\r
+       $s = sql_fetsel("id_document,id_vignette","spip_documents","id_document=".intval($id));\r
        if (!is_array($s))\r
                return false;\r
 \r
        include_spip('inc/modifier');\r
        include_spip('inc/documents');\r
+       include_spip('action/editer_document');//pour revision_document\r
        // Chargement d'un nouveau doc ?\r
        if ($data['vignette']) {\r
                define('FILE_UPLOAD', true);\r
@@ -286,7 +306,7 @@ function vignette_revision($id, $data, $type, $ref) {
                                supprimer_fichier($f); \r
                        }\r
                        sql_delete('spip_documents', 'id_document='.intval($s['id_vignette']));\r
-                       sql_delete('spip_documents_liens',  'id_document='.intval($s['id_vignette']));\r
+                       sql_delete('spip_documents_liens', 'id_document='.intval($s['id_vignette']));\r
 \r
                        pipeline('post_edition',\r
                                array(\r
@@ -298,15 +318,34 @@ function vignette_revision($id, $data, $type, $ref) {
                                        'data' => null\r
                                )\r
                        );\r
-                       \r
-                       // On remet l'id_vignette a 0\r
-                       revision_document($s['id_document'], array('id_vignette'=>0));\r
+                       $id_vignette = 0;\r
                }\r
-               // Ajout du document comme vignette\r
-               $ajouter_documents = charger_fonction('ajouter_documents', 'inc');\r
+\r
                $arg = $data['vignette'];\r
                check_upload_error($arg['error']);\r
-               $x = $ajouter_documents($arg['tmp_name'], $arg['name'],'','', 'vignette', $id, $actifs);\r
+               // Ajout du document comme vignette\r
+\r
+               /**\r
+                * Méthode >= SPIP 3.0 \r
+                * ou SPIP 2.x + Mediatheque\r
+                */\r
+               if($ajouter_documents = charger_fonction('ajouter_documents','action',true)){\r
+                       $x = $ajouter_documents(null,array($arg),'', 0, 'vignette');\r
+                       $vignette = reset($x);\r
+                       if(intval($vignette))\r
+                               document_modifier($id, array('id_vignette'=>$vignette));\r
+                       else if($id_vignette)\r
+                               document_modifier($id, array('id_vignette'=>$id_vignette));\r
+               }\r
+               /**\r
+                * Méthode < SPIP 3.0\r
+                */\r
+               else if($ajouter_documents = charger_fonction('ajouter_documents','inc',true)){\r
+                       // On remet l'id_vignette a 0 si on l'a supprimé\r
+                       if($id_vignette) revision_document($s['id_document'], array('id_vignette'=>0));\r
+                       $x = $ajouter_documents($arg['tmp_name'], $arg['name'],'','', 'vignette', $id, $actifs);\r
+               }\r
+               \r
        }else\r
                // Suppression de la vignette ?\r
                if ($wid = array_pop($ref)\r
@@ -320,7 +359,7 @@ function vignette_revision($id, $data, $type, $ref) {
                                }\r
                                sql_delete('spip_documents', 'id_document='.intval($s['id_vignette']));\r
                                sql_delete('spip_documents_liens',  'id_document='.intval($s['id_vignette']));\r
-                               \r
+\r
                                pipeline('post_edition',\r
                                        array(\r
                                                'args' => array(\r
@@ -331,7 +370,7 @@ function vignette_revision($id, $data, $type, $ref) {
                                                'data' => null\r
                                        )\r
                                );\r
-                               \r
+\r
                                // On remet l'id_vignette a 0\r
                                revision_document($s['id_document'], array('id_vignette'=>0));\r
                        }\r
@@ -339,11 +378,14 @@ function vignette_revision($id, $data, $type, $ref) {
        return true;\r
 }\r
 \r
+\r
 function colonne_table($type, $col) {\r
        list($distant,$table) = distant_table($type);\r
        $nom_table = '';\r
-       if (!(($tabref = &crayons_get_table($table, $nom_table)) && ($brut = $tabref['field'][$col]))) {\r
-               return false;\r
+       if (!(($tabref = &crayons_get_table($table, $nom_table))\r
+               && isset($tabref['field'][$col])\r
+               && ($brut = $tabref['field'][$col]))) {\r
+                       return false;\r
        }\r
        $ana = explode(' ', $brut);\r
        $sta = 0;\r
@@ -400,20 +442,50 @@ function colonne_table($type, $col) {
        }\r
        return $ret;\r
 }\r
-//     var_dump(colonne_table('forum', 'id_syndic')); die();\r
 \r
-function table_where($type, $id, $where_en_tableau = false)\r
-{\r
-       list($distant,$table) = distant_table($type);\r
+\r
+/**\r
+ * Obtient le nom de la table ainsi que sa ou ses clés primaires\r
+ *\r
+ * @param string $type\r
+ *     Table sur laquelle s'applique le crayon.\r
+ *     Ce type peut contenir le nom d'un connecteur distant tel que `{connect}__{table}`\r
+ *\r
+ * @return array|bool\r
+ *     - false si on ne trouve pas de table ou de table ayant de clé primaire\r
+ *     - liste :\r
+ *     - - nom de la table sql\r
+ *     - - tableau des noms de clés primaires\r
+**/\r
+function crayons_get_table_name_and_primary($type) {\r
+       static $types = array();\r
+       if (isset($types[$type])) {\r
+               return $types[$type];\r
+       }\r
+\r
        $nom_table = '';\r
-       if (!(($tabref = &crayons_get_table($type, $nom_table))\r
-                       && ($tabid = explode(',', $tabref['key']['PRIMARY KEY'])))) {\r
-               spip_log('crayons: table ' . $table . ' inconnue');\r
+       if ($tabref = &crayons_get_table($type, $nom_table)\r
+         and ($tabid = explode(',', $tabref['key']['PRIMARY KEY'])))\r
+       {\r
+               return $types[$type] = array($nom_table, $tabid);\r
+       }\r
+       spip_log('crayons: table ' . $type . ' inconnue');\r
+       return $types[$type] = false;\r
+}\r
+\r
+\r
+function table_where($type, $id, $where_en_tableau = false) {\r
+\r
+\r
+       if (!$infos = crayons_get_table_name_and_primary($type)) {\r
                return array(false, false);\r
        }\r
-       if (is_scalar($id)) {\r
+\r
+       list($nom_table, $tabid) = $infos;\r
+\r
+\r
+       if (is_scalar($id))\r
                $id = explode('-', $id);\r
-       }\r
        // sortie tableau pour sql_updateq\r
        if ($where_en_tableau) {\r
                $where = array();\r
@@ -422,6 +494,7 @@ function table_where($type, $id, $where_en_tableau = false)
                }\r
        // sinon sortie texte pour sql_query\r
        } else {\r
+\r
                $where = $and = '';\r
                foreach ($id as $idcol => $idval) {\r
                        $where .= $and . '`' . (is_int($idcol) ? trim($tabid[$idcol]) : $idcol) . '`=' . _q($idval);\r
@@ -433,11 +506,12 @@ function table_where($type, $id, $where_en_tableau = false)
 //     var_dump(colonne_table('forum', 'id_syndic')); die();\r
 \r
 function valeur_colonne_table_dist($type, $col, $id) {\r
-       list($distant,$table) = distant_table($type);\r
-       list($nom_table, $where) = table_where($type, $id);\r
 \r
-       if (!$nom_table)\r
+       // Table introuvable ou sans clé primaire\r
+       if (!$infos = crayons_get_table_name_and_primary($type)) {\r
                return false;\r
+       }\r
+       $table = reset($infos);\r
 \r
        $r = array();\r
 \r
@@ -450,12 +524,17 @@ function valeur_colonne_table_dist($type, $col, $id) {
        }\r
 \r
        // valeurs SQL\r
-       if (count($col)\r
-       AND $s = spip_query(\r
-                       'SELECT `' . implode($col, '`, `') .\r
-                       '` FROM ' . $nom_table . ' WHERE ' . $where, $distant)\r
-       AND $t = sql_fetch($s))\r
-               $r = array_merge($r, $t);\r
+       if (count($col)) {\r
+               list($distant, $table)   = distant_table($type);\r
+               list($nom_table, $where) = table_where($type, $id);\r
+\r
+               if ($s = spip_query(\r
+                               'SELECT `' . implode($col, '`, `') .\r
+                               '` FROM ' . $nom_table . ' WHERE ' . $where, $distant)\r
+                       AND $t = sql_fetch($s)){\r
+                               $r = array_merge($r, $t);\r
+               }\r
+       }\r
 \r
        return $r;\r
 }\r
@@ -521,13 +600,19 @@ function return_log($var) {
        die(crayons_json_export(array('$erreur'=> var_export($var,true))));\r
 }\r
 \r
-function _U($texte, $params=array())\r
-{\r
-    include_spip('inc/charsets');\r
-    return unicode2charset(html2unicode(_T($texte, $params)));\r
+function _U($texte, $params=array()) {\r
+       include_spip('inc/charsets');\r
+       return unicode2charset(html2unicode(_T($texte, $params)));\r
 }\r
 \r
-// wdgcfg = widget config :-)\r
+/**\r
+ * Obtenir la configuration des crayons\r
+ *\r
+ * @note wdgcfg = widget config :-)\r
+ * \r
+ * @return array\r
+ *     Couples : attribut => valeur\r
+**/\r
 function wdgcfg() {\r
        $php = function_exists('crayons_config') ? crayons_config() : array();\r
        include_spip('inc/meta');\r
@@ -572,8 +657,7 @@ function &crayons_get_table($type, &$nom_table) {
                        include_spip('base/serial');\r
                        include_spip('base/auxiliaires');\r
                        include_spip('public/parametrer');\r
-                       foreach(array('tables_principales', 'tables_auxiliaires') as $categ)\r
-                       {\r
+                       foreach(array('tables_principales', 'tables_auxiliaires') as $categ) {\r
                                foreach ($try as $nom) {\r
                                        if (isset($GLOBALS[$categ][$nom])) {\r
                                                $noms[$table] = $nom;\r
@@ -583,7 +667,6 @@ function &crayons_get_table($type, &$nom_table) {
                                }\r
                        }\r
                }\r
-\r
        }\r
 \r
        $nom_table = $noms[$table];\r