Dotclear

Vous n'êtes pas identifié(e).

Annonce

13 février 2024 Sortie de Dotclear 2.29

#1 2020-05-06 17:06:30

nanart
Membre
Lieu : Roubaix
Inscription : 2007-08-19

aide pour behavior theme 404

Bonjour,
J'ai comme projet d'implanter un behavior dans un theme enfant de Berlin.

Son objet est de proposer une nouvelle page 404 affichant :
- le referer - la page appelée qui a généré l'erreur
- éventuellement d'autres infos tels qu'IP, Nom d'hôte (si existe) et agent

Quelques pistes pour pouvoir le générer ?
Merci


Dernière version stable Dotclear sur wampserver et chez ovh
Versions testing & unstable en local
https
php: 7.4  - 8 +

Hors ligne

#2 2020-05-06 18:13:40

Philippe
Stagiaire
Lieu : Toulon
Inscription : 2004-06-13
Site Web

Re : aide pour behavior theme 404

Pour personnaliser la page d'erreur, il suffit d'ajouter dans le thème courant un fichier 404.html.

Si tu veux afficher le referrer ou l'IP, le nom d'hôte, etc. je pense qu'il faut ajouter un fichier _public.php pour les récupérer en PHP avec une balise de template personnalisée, comme par exemple avec HTTP_REFERER qui est une variable d'exécution, par exemple

echo $_SERVER['HTTP_REFERER'];

Dernière modification par Philippe (2020-05-06 18:17:40)

Hors ligne

#3 2020-05-06 20:18:28

nanart
Membre
Lieu : Roubaix
Inscription : 2007-08-19

Re : aide pour behavior theme 404

Certes, mais ce que je cherche c'est l'écriture de la fonction du  behavior qui va générer le contenu de la balise tpl.

un truc du genre, si c'est possible

public static function public_urlHandler() //args ?
{
// récupération $urlHandler

//echo $urlHandler / value

}

Dernière version stable Dotclear sur wampserver et chez ovh
Versions testing & unstable en local
https
php: 7.4  - 8 +

Hors ligne

#4 2020-05-07 06:44:27

Philippe
Stagiaire
Lieu : Toulon
Inscription : 2004-06-13
Site Web

Re : aide pour behavior theme 404

En suivant l'exemple de la doc, ajouter dans le fichier _public.php du thème :

$core->tpl->addValue('myReferer',array('tplMyMoreTpl','myReferer'));

class tplMyMoreTpl
{
	public static function myReferer()
	{
		return
		'<?php echo $_SERVER[\'HTTP_REFERER\']; ?>';
	}
}

et dans le fichier 404.html

{{tpl:myReferer}}

Pour tester, dans n'importe quel billet, fais un lien vers un billet qui n'existe pas. Tu devrais tomber sur la page d'erreur, et là où tu as mis la balise de template tu devrais avoir l'url du billet d'origine du lien.

Hors ligne

#5 2020-05-07 22:46:17

nanart
Membre
Lieu : Roubaix
Inscription : 2007-08-19

Re : aide pour behavior theme 404

ok, c'est plus ou moins ce que j'avais pensé pour un truc simple

Mais j'essaie de me servir de urlHandler...

En cherchant un peu, j'ai réussi à trouver le "titre" (base url?)
- que je pourrais éventuellement tester dans une recherche
par ex :

public static function beforeGetData($_ctx)
	{
			$referer = $_ctx->url->args; //

Mébon, j'ai beau creuser, je n'arrive pas à trouver le "type" d'erreur que je voudrais tester
du genre

if ($error=404...

EDIT, trouvé:

$error = $_ctx->url->type

Dernière modification par nanart (2020-05-07 23:38:40)


Dernière version stable Dotclear sur wampserver et chez ovh
Versions testing & unstable en local
https
php: 7.4  - 8 +

Hors ligne

#6 2020-05-08 07:56:02

Philippe
Stagiaire
Lieu : Toulon
Inscription : 2004-06-13
Site Web

Re : aide pour behavior theme 404

Bonjour

Je ne sais pas exactement ce que tu essaies de faire (?).

À tout hasard, je te colle ici un exemple de code (© pep) qui permet de servir un template différent par catégorie (category-1.html, category-2.html, etc.), et enregistre donc plusieurs URLHandlers selon le contexte (post, category ou preview) :

$core->url->register('category', 'category', '^category/(.+)$', array('myURLHandlers','category'));
$core->url->register('post', 'post', '^post/(.+)$', array('myURLHandlers','post'));
$core->url->register('preview', 'preview', '^preview/(.+)$', array('myURLHandlers','preview'));

class myURLHandlers extends dcUrlHandlers
{
    public static function category($args)
    {
        $_ctx =& $GLOBALS['_ctx'];
        $core =& $GLOBALS['core'];
        
        $n = self::getPageNumber($args);
        
        if ($args == '' && !$n) {
            self::p404();
        }
        
        $params['cat_url'] = $args;
        $params['post_type'] = 'post';
        
        $_ctx->categories = $core->blog->getCategories($params);
        
        if ($_ctx->categories->isEmpty()) {
            self::p404();
        } else {
            if ($n) {
                $GLOBALS['_page_number'] = $n;
            }
            $tpl = 'category-'.$_ctx->categories->cat_id.'.html';
            if (!$core->tpl->getFilePath($tpl)) {
                $tpl = 'category.html';
            }
            self::serveDocument($tpl);
            exit;
        }
    }
    
    public static function post($args)
    {
        if ($args == '') {
            self::p404();
        }
        
        $_ctx =& $GLOBALS['_ctx'];
        $core =& $GLOBALS['core'];
        
        $core->blog->withoutPassword(false);
        
        $params = new ArrayObject();
        $params['post_url'] = $args;
        
        $_ctx->posts = $core->blog->getPosts($params);
        
        $_ctx->comment_preview = new ArrayObject();
        $_ctx->comment_preview['content'] = '';
        $_ctx->comment_preview['rawcontent'] = '';
        $_ctx->comment_preview['name'] = '';
        $_ctx->comment_preview['mail'] = '';
        $_ctx->comment_preview['site'] = '';
        $_ctx->comment_preview['preview'] = false;
        $_ctx->comment_preview['remember'] = false;
        
        $core->blog->withoutPassword(true);
        
        
        if ($_ctx->posts->isEmpty()) {
            # No entry
            self::p404();
        }
        
        $post_id = $_ctx->posts->post_id;
        $post_password = $_ctx->posts->post_password;
        
        # Password protected entry
        if ($post_password != '') {
            # Get passwords cookie
            if (isset($_COOKIE['dc_passwd'])) {
                $pwd_cookie = unserialize($_COOKIE['dc_passwd']);
            } else {
                $pwd_cookie = array();
            }
            
            # Check for match
            if ((!empty($_POST['password']) && $_POST['password'] == $post_password)
            || (isset($pwd_cookie[$post_id]) && $pwd_cookie[$post_id] == $post_password)) {
                $pwd_cookie[$post_id] = $post_password;
                setcookie('dc_passwd', serialize($pwd_cookie), 0, '/');
            } else {
                self::serveDocument('password-form.html', 'text/html', false);
                exit;
            }
        }
        
        $post_comment =
            isset($_POST['c_name']) && isset($_POST['c_mail']) &&
            isset($_POST['c_site']) && isset($_POST['c_content']) &&
            $_ctx->posts->commentsActive();
        
        # Posting a comment
        if ($post_comment) {
            # Spam trap
            if (!empty($_POST['f_mail'])) {
                http::head(412, 'Precondition Failed');
                header('Content-Type: text/plain');
                echo "So Long, and Thanks For All the Fish";
                exit;
            }
            
            $name = $_POST['c_name'];
            $mail = $_POST['c_mail'];
            $site = $_POST['c_site'];
            $content = $_POST['c_content'];
            $preview = !empty($_POST['preview']);
            
            if ($content != '') {
                if ($core->blog->settings->wiki_comments) {
                    $core->initWikiComment();
                } else {
                    $core->initWikiSimpleComment();
                }
                $content = $core->wikiTransform($content);
                $content = $core->HTMLfilter($content);
            }
            
            $_ctx->comment_preview['content'] = $content;
            $_ctx->comment_preview['rawcontent'] = $_POST['c_content'];
            $_ctx->comment_preview['name'] = $name;
            $_ctx->comment_preview['mail'] = $mail;
            $_ctx->comment_preview['site'] = $site;
            
            if ($preview) {
                $_ctx->comment_preview['preview'] = true;
            } else {
                # Post the comment
                $cur = $core->con->openCursor($core->prefix.'comment');
                $cur->comment_author = $name;
                $cur->comment_site = html::clean($site);
                $cur->comment_email = html::clean($mail);
                $cur->comment_content = $content;
                $cur->post_id = $_ctx->posts->post_id;
                $cur->comment_status = $core->blog->settings->comments_pub ? 1 : -1;
                $cur->comment_ip = http::realIP();
                
                $redir = $_ctx->posts->getURL();
                $redir .= strpos($redir, '?') !== false ? '&' : '?';
                
                try {
                    if (!text::isEmail($cur->comment_email)) {
                        throw new Exception(__('You must provide a valid email address.'));
                    }

                    # --BEHAVIOR-- publicBeforeCommentCreate
                    $core->callBehavior('publicBeforeCommentCreate', $cur);
                    if ($cur->post_id) {
                        $comment_id = $core->blog->addComment($cur);
                    
                        # --BEHAVIOR-- publicAfterCommentCreate
                        $core->callBehavior('publicAfterCommentCreate', $cur, $comment_id);
                    }
                    
                    if ($cur->comment_status == 1) {
                        $redir_arg = 'pub=1';
                    } else {
                        $redir_arg = 'pub=0';
                    }
                    
                    header('Location: '.$redir.$redir_arg);
                    exit;
                } catch (Exception $e) {
                    $_ctx->form_error = $e->getMessage();
                    $_ctx->form_error;
                }
            }
        }
        
        # The entry
        $tpl = 'post.html';
        if ($_ctx->posts->cat_id) {
            $alt_tpl = 'post-cat'.strtolower($_ctx->posts->cat_id).'.html';
            if ($core->tpl->getFilePath($alt_tpl)) {
                $tpl = $alt_tpl;
            }
        }
        self::serveDocument($tpl);
        exit;
    }
    
    public static function preview($args)
    {
        $core = $GLOBALS['core'];
        if (!preg_match('#^(.+?)/([0-9a-z]{40})/(.+?)$#', $args, $m)) {
            self::p404();
        }
        $user_id = $m[1];
        $user_key = $m[2];
        $post_url = $m[3];
        if (!$core->auth->checkUser($user_id, null, $user_key)) {
            self::p404();
        }
        
        self::post($post_url);
        exit;
    }
}

En espérant te mettre sur la bonne piste ;)

Hors ligne

Vous n'êtes pas identifié(e).

Pied de page des forums

Sites map