Code beim Speichern ausführen und AMR URL

Gesperrt
Mario_m
Beiträge: 90
Registriert: Sa 24. Okt 2009, 22:57
Kontaktdaten:

Code beim Speichern ausführen und AMR URL

Beitrag von Mario_m » Sa 5. Jun 2010, 21:44

Ich suche nach einer Möglichkeit php code beim Speichern eines Artikels auszuführen. Hat jemand eine Idee wo ich da eingreifen kann?
Als nächstes möchte ich an dieser Stelle den vom AMR generierten URL String wissen oder mir irgendwie zusammenbauen.
Als drittes muss ich an dieser Stelle noch abfragen, ob der Artikel online ist.

Hintergrund ist, dass ich hier beim Speichern des jeweiligen Artikels, das Ganze als statische Datei in einem Cache Verzeichnise speichern möchte. Den Code dazu hab ich schon. Das Ganze funktioniert mit der Snoopy Klasse. Leider geht das Ganze derzeit nur mit Artikeltext und nicht mit Bildern. Aber reicht mir schon.

Code: Alles auswählen

<?php		
	include "Snoopy.class.php";
	
	$url="http://www.domain.de/directory/index.html";
	fetch($url);

/*
 *    Function to fetch a webpage in the given subdirectory
 *    @param string $url
 */
function fetch($url){	
	$snoopy = new Snoopy;
	$url2= parse_url($url, PHP_URL_PATH);
	$pathTrimmed = trim($url2, '/');	
	$mkdirpath = substr($pathTrimmed, 0, -11);   
	
	if($snoopy->fetch($url))
	{								
		if(!dir_exists($mkdirpath))
		{
			mkdir($mkdirpath,0777,TRUE);
		}		
			$fh = fopen($pathTrimmed, 'w') or die("can't open file");		
			fwrite($fh, $snoopy->results);		
			fclose($fh);		
	}
	else
	{
		echo "error fetching document: ".$snoopy->error."\n";		
	}
}

/*
 *    Function to check recursively if dirname is exists in directory's tree
 *    @param string $dir_name
 *    @param string [$path]
 *    @return bool
 */
function dir_exists($dir_name = false, $path = './') {
    if(!$dir_name) return false;
   
    if(is_dir($path.$dir_name)) return true;
   
    $tree = glob($path.'*', GLOB_ONLYDIR);
    if($tree && count($tree)>0) {
        foreach($tree as $dir)
            if(dir_exists($dir_name, $dir.'/'))
                return true;
    }   
    return false;
}
?>

thepoet
Beiträge: 55
Registriert: Mo 7. Jul 2003, 11:39
Wohnort: Vilsbiburg
Kontaktdaten:

Re: Code beim Speichern ausführen und AMR URL

Beitrag von thepoet » Mo 7. Jun 2010, 17:11

Es gibt eine Chain Contenido.Content.SaveContentEntry die aus der conSaveContentEntry heraus mit den Parametern ($idartlang, $type, $typeid, $value) aufgerufen wird.

D.h. Du solltest dich mit einem Codeschnippsel wie dem folgenden in der contenido/includes/config.local.php (neu anlegen und Leserechte für den Webserver-User vergeben falls sie noch nicht existiert, dabei PHP-Tags nicht vergessen und sicher stellen dass keine Leerzeichen am Ende der Datei sind) darauf hängen können:

Code: Alles auswählen

function cecSaveCtEntry_createCachedHtml($idartlang, $type, $typeid, $value) {
  // Code aus der Seite via $idartlang auslesen und in den Cache speichern, d.h.
  // deine Snoopy-Routine mit cms/front_content.php?idartlang=$idartlang
  // aufrufen.
}

$_cecRegistry->addChainFunction("Contenido.Content.SaveContentEntry", "cecSaveCtEntry_createCachedHtml");

Mario_m
Beiträge: 90
Registriert: Sa 24. Okt 2009, 22:57
Kontaktdaten:

Re: Code beim Speichern ausführen und AMR URL

Beitrag von Mario_m » Mo 7. Jun 2010, 20:16

Vielen Dank für die Antwort. Leider kommt noch ein Fehler
Beim Aufruf des Editors eines Artikekels kommt:
Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in ........./contenido/includes/include.con_editcontent.php(638) : eval()'d code on line 509
Meine config.local.php sieht nun so aus. Die Snoopy Klasse liegt auch in dem includes Verzeichnis

Code: Alles auswählen

<?
include "Snoopy.class.php";	
$url="http://domain.de/cms/front_content.php?idartlang=$idartlang";
	
function cecSaveCtEntry_createCachedHtml($idartlang, $type, $typeid, $value) {	
	fetch($url);  
}

$_cecRegistry->addChainFunction("Contenido.Content.SaveContentEntry", "cecSaveCtEntry_createCachedHtml");

/*
 *    Function to fetch a webpage in the given subdirectory
 *    @param string $url
 */
function fetch($url){	
	$snoopy = new Snoopy;
	$url2= parse_url($url, PHP_URL_PATH);
	$pathTrimmed = trim($url2, '/');	
	$mkdirpath = substr($pathTrimmed, 0, -11);   
	
	if($snoopy->fetch($url))
	{								
		if(!dir_exists($mkdirpath))
		{
			mkdir($mkdirpath,0777,TRUE);
		}		
			$fh = fopen($pathTrimmed, 'w') or die("can't open file");		
			fwrite($fh, $snoopy->results);		
			fclose($fh);		
	}
	else
	{
		echo "error fetching document: ".$snoopy->error."\n";		
	}
}

/*
 *    Function to check recursively if dirname is exists in directory's tree
 *    @param string $dir_name
 *    @param string [$path]
 *    @return bool
 */
function dir_exists($dir_name = false, $path = './') {
    if(!$dir_name) return false;
   
    if(is_dir($path.$dir_name)) return true;
   
    $tree = glob($path.'*', GLOB_ONLYDIR);
    if($tree && count($tree)>0) {
        foreach($tree as $dir)
            if(dir_exists($dir_name, $dir.'/'))
                return true;
    }   
    return false;
	}
?>

Mario_m
Beiträge: 90
Registriert: Sa 24. Okt 2009, 22:57
Kontaktdaten:

Re: Code beim Speichern ausführen und AMR URL

Beitrag von Mario_m » Mi 9. Jun 2010, 22:52

Ich bin jetzt hier weitergekommen. Scheint auch soweit zu funktionieren.
Jetzt stehe ich vor dem Problem wie ich in der config.local.php (im Mandantenverzeichnis), Mandantenspezifische Systemvariablen auslesen kann. $cfgClient geht offenbar nicht. und $cfg liefert mir keine Mandanteninfo.

Oder weiss hier jemand wie ich den Server Pfad des Mandanten rausbekomme?

Desweiteren ist mir immer noch nicht klar, wie ich die vom AMR generierten URLS rausbekomme. Die brauche ich zwingend, weil dies die Grundlage
für meine Cache Verzeichnisstruktur darstellt.

Mario_m
Beiträge: 90
Registriert: Sa 24. Okt 2009, 22:57
Kontaktdaten:

Re: Code beim Speichern ausführen und AMR URL

Beitrag von Mario_m » Do 10. Jun 2010, 07:37

Ok, das Pfadproblem ist gelöst. Lese einfach getcwd aus. Funktioniert natürlich nur, wenn config.local.php im Mandantenverzeichnis liegt.
Fehlt noch die URL. Der Code sieht nun folgendermaßen aus:

Code: Alles auswählen

<?
include "Snoopy.class.php";	

/**
 * Userdefined function, which will be added to Chain "Contenido.Content.SaveContentEntry", 
 * processes contents of Contenido CMS_HTML types.
 *
 * @param   int     $idartlang  idartlang (con_art_lang.idartlang)
 * @param   string  $type       CMS Typ (con_type.type)
 * @param   int     $typeid     Position of CMS-Type, e. g. 5 for CMS_HTML[5]
 * @param   string  $value      The value (HTML-Code)
 * @return  string  Processed value
 */
function cecSaveCtEntry_createCachedHtml($idartlang, $type, $typeid, $value) {			
	$value=fetch("http://domain.de/directory/index.html");  /*Der URL String muss dynamisch erzeugt werden.*/
	return $value;
}

/*
 *    Function to fetch a webpage in the given subdirectory
 *    @param string $url
 */
function fetch($url){				
	$serverpath=getcwd()."/cache/";
	$snoopy = new Snoopy;
	$url2= parse_url($url, PHP_URL_PATH);
	$pathTrimmed = trim($url2, '/');	
	$mkdirpath = substr($serverpath.$pathTrimmed, 0, -11);   
	
	if($snoopy->fetch($url))
	{								
		if(!is_dir($mkdirpath))
		{
			mkdir($mkdirpath,0777,TRUE);
		}		
			$fh = fopen($serverpath.$pathTrimmed, 'w') or die("Kann Datei nicht lesen");		
			fwrite($fh, $snoopy->results);		
			fclose($fh);		
	}
	else
	{
		echo "error fetching document: ".$snoopy->error."\n";		
	}
}
	
// get cec registry instance
$_cecRegistry = cApiCECRegistry::getInstance();

// add a function to Contenido Extension Chainer
$_cecRegistry->addChainFunction("Contenido.Content.SaveContentEntry", "cecSaveCtEntry_createCachedHtml");
?>

Mario_m
Beiträge: 90
Registriert: Sa 24. Okt 2009, 22:57
Kontaktdaten:

Re: Code beim Speichern ausführen und AMR URL

Beitrag von Mario_m » Sa 19. Jun 2010, 21:58

Habe nun mit dem URL Builder und dem AMR rumgespielt.

Folgender Code funktioniert zwar

Code: Alles auswählen

if ($cfg['url_builder']['name'] == 'front_content' || $cfg['url_builder']['name'] == 'MR')
{	
	$aParams = array ('idart' => $idart, 'idcat' => $idcat, 'lang' => $lang);
	$url = Contenido_Url::getInstance()->build($aParams);	
	echo $url;
}
liefert aber in der Vorschau und dem Editor immer noch die front_content Version. Kann es sein, dass ich die Rewrite Regeln hier erweitern muss?

Gesperrt