[SPIP] +2.1.12
[velocampus/web/www.git] / www / plugins / auto / verifier / inc / verifier.php
1 <?php
2
3 // Sécurité
4 if (!defined("_ECRIRE_INC_VERSION")) return;
5
6 /**
7 * Fonction de base de l'API de vérification.
8 * @param mixed $valeur La valeur a verifier.
9 * @param string $type Le type de verification a appliquer.
10 * @param array $options Un eventuel tableau d'options suivant le type.
11 * @return string Retourne une chaine vide si c'est valide, sinon une chaine expliquant l'erreur.
12 */
13 function inc_verifier_dist($valeur, $type, $options=null){
14
15 // On vérifie que les options sont bien un tableau
16 if (!is_array($options))
17 $options = array();
18
19 // Si la valeur est vide, il n'y a rien a verifier donc c'est bon
20 if (is_null($valeur) or (is_string($valeur) and $valeur == '')) return '';
21
22 // On cherche si une fonction correspondant au type existe
23 if ($verifier = charger_fonction($type, 'verifier',true)){
24 $erreur = $verifier($valeur, $options);
25 }
26
27 // On passe le tout dans le pipeline du meme nom
28 $erreur = pipeline(
29 'verifier',
30 array(
31 'args' => array(
32 'valeur' => $valeur,
33 'type' => $type,
34 'options' => $options
35 ),
36 'data' => $erreur
37 )
38 );
39
40 return $erreur;
41 }
42
43 /**
44 * Liste toutes les vérifications possibles
45 *
46 * @return Retourne un tableau listant les vérifications et leurs options
47 */
48 function verifier_lister_disponibles(){
49 static $verifications = null;
50
51 if (is_null($verifications)){
52 $verifications = array();
53 $liste = find_all_in_path('verifier/', '.+[.]yaml$');
54
55 if (count($liste)){
56 foreach ($liste as $fichier=>$chemin){
57 $type_verif = preg_replace(',[.]yaml$,i', '', $fichier);
58 $dossier = str_replace($fichier, '', $chemin);
59 // On ne garde que les vérifications qui ont bien la fonction !
60 if (charger_fonction($type_verif, 'verifier', true)
61 and (
62 is_array($verif = verifier_charger_infos($type_verif))
63 )
64 ){
65 $verifications[$type_verif] = $verif;
66 }
67 }
68 }
69 }
70
71 return $verifications;
72 }
73
74 /**
75 * Charger les informations contenues dans le yaml d'une vérification
76 *
77 * @param string $type_verif Le type de la vérification
78 * @return array Un tableau contenant le YAML décodé
79 */
80 function verifier_charger_infos($type_verif){
81 include_spip('inc/yaml');
82 $fichier = find_in_path("verifier/$type_verif.yaml");
83 $verif = yaml_decode_file($fichier);
84 if (is_array($verif)){
85 $verif['titre'] = $verif['titre'] ? _T_ou_typo($verif['titre']) : $type_verif;
86 $verif['description'] = $verif['description'] ? _T_ou_typo($verif['description']) : '';
87 $verif['icone'] = $verif['icone'] ? find_in_path($verif['icone']) : '';
88 }
89 return $verif;
90 }
91
92 ?>