Joomla 3 replace text plugin using preg replace not working - plugins

I am following a tutorial about joomla 3 extension development. I am using Joomla 3.2.4
I have a plugin name clicktocall, which to make all phone number text displayed as a link.
Phone number format is XXXX-XXXX or XXXX XXXX, X is digit. and I want display any phone number as ">
The method is using pattern as replace any text match the pattern by link tag
I installed, enabled the plugin
I do it after a tutorial in an ebook, in the book everything are so smoothly, but in my site, after I view an article which have phone number text, there are nothing happen. The plugin not working.
My code:
clicktocall.php
defined('_JEXEC') or die;
jimport('joomla.plugin.plugin');
class plgContentClicktocall extends JPlugin {
function plgContentClicktocall(&$subject, $params) {
parent::__construct($subject, $params);
}
public function onContentPrepare($context, &$row, &$params, $page = 0) {
//don't run this when the content is indexing
if ($context == 'com_finder.indexer') {
return true;
}
if (is_object($row)) {
echo $row->text;
return $this->clickToCall($row->text, $params);
}
return $this->clickToCall($row, $params);
}
protected function clickToCall(&$text, &$params) {
// matches 4 numbers followed by an optional hyphen or space,
// then followed by 4 numbers.
// phone number is in the form XXXX-XXXX or XXXX XXXX
$pattern = '/(\W[0-9]{4})-? ?(\W[0-9]{4})/';
$replacement = '$1$2';
$text = preg_replace($pattern, $replacement, $text);
return true;
}
}
clicktocall.xml
<?xml version="1.0" encoding="UTF-8"?>
<extension
version="3.0"
type="plugin"
group="content"
method="upgrade">
<name>Content - Click To Call</name>
<author>Tim Plummer</author>
<creationDate>April 2013</creationDate>
<copyright>Copyright (C) 2013 Packt Publishing. All rights
reserved.</copyright>
<license> http://www.gnu.org/licenses/gpl-3.0.html</license>
<authorEmail>example#packtpub.com</authorEmail>
<authorUrl>http://packtpub.com</authorUrl>
<version>1.0.0</version>
<description>This plugin will replace phone numbers with click
to call links. Requires Joomla! 3.0 or greater.
Don't forget to publish this plugin!
</description>
<files>
<filename plugin="clicktocall">clicktocall.php</filename>
<filename>index.html</filename>
</files>
</extension>
index.html : blank tags only
Sorry for the XML, I try for 10 minutes to make it pre-formatted but seem to be useless, but I confirm it's OK, included all files in my plugin

I believe the issue is you are returning the value from your click2Call() method inside your onContentPrepare() method. Try reformatting like so:
public function onContentPrepare($context, &$row, &$params, $page = 0) {
//don't run this when the content is indexing
if ($context == 'com_finder.indexer') {
return true;
}
if (is_object($row)) {
echo $row->text;
$this->clickToCall($row->text, $params);
} else {
$this->clickToCall($row, $params);
}
return true;
}
Since the row variable is referenced, any changes you make to the row data you're making to the actual data. Therefor, no need to return any data outside of the the return true at the end of the method.

Related

Custom sitemap handler causing site to fail to load (Sitecore)

I'm trying to add a custom handler to my site to redirect to the appropriate Sitemap for the current language, i.e. mysite.com/sitemap.xml --> sitemap-en.xml, mysite.es/sitemap.xml --> sitemap-es.xml, etc.
This is my setup:
handler trigger patch:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<customHandlers>
<handler trigger="sitemap.xml" handler="sitemap.ashx" />
</customHandlers>
</sitecore>
</configuration>
web.config:
<add path="sitemap.ashx" verb="*" type="MySite.CustomSitecore.Handlers.SitemapHandler,MySite" name="SitemapXml" />
handler:
namespace MySite.CustomSitecore.Handlers
{
public class SitemapHandler : IHttpHandler
{
public bool IsReusable
{
get
{
throw new NotImplementedException();
}
}
public void ProcessRequest(HttpContext context)
{
try
{
var curSite = Sitecore.Context.Site;
var m_Sites = SitemapManagerConfiguration.GetSites();
foreach (DictionaryEntry site in m_Sites)
{
if (site.Key.ToString().Equals(curSite.Name))
{
var filepath = site.Value;
HttpContext.Current.Response.Redirect("/" + filepath, false);
return;
}
}
return;
}
catch (Exception ex)
{
Log.Info("Error in SitemapHandler: " + ex, this);
}
}
}
}
It's not working though, when I try to go to mysite.com/sitemap.xml and debug it steps through the redirect process as expected and looks as if it should be successfully redirecting to /sitemap-en.xml, but the page just spins and displays browser error saying that the page can't be loaded.
I have tried a couple different redirect methods, but nothing has worked. I tried this as well:
HttpContext.Current.Response.StatusCode = 200;
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.AddHeader("Location", "/" + filepath);
HttpContext.Current.Response.End();
return;
Not sure if this is the issue, But out of the box Sitecore don't allow .xml files. Security hardening.
You can use somethings like this in the config, to add the .xml extension
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<preprocessRequest help="Processors should derive from Sitecore.Pipelines.PreprocessRequest.PreprocessRequestProcessor">
<processor type="Sitecore.Pipelines.PreprocessRequest.FilterUrlExtensions, Sitecore.Kernel">
<param desc="Allowed extensions (comma separated)">aspx, ashx, asmx, xml</param>
</processor>
</preprocessRequest>
</pipelines>
</sitecore>
</configuration>
But I think a better solution, use the default and take security seriously.
Tell the search engine what the url of the sitemap is by using a robots.txt
User-agent: *
Sitemap: /sitemap.ashx

How to write Monolog logs into a file AND remote database

I have a Symfony2 -project and it writes the logs into different files beautifully, but I would like it to write the logs into a remote database(mongodb) as well. I would like to keep the actual log files in the servers as a backup in case something goes wrong with the database connection.
Question 1:
Is it even possible to save the same logs into two different places at the same time?
Question 2:
How do I save the logs into the mongodb? I don't necessarily need specific mongodb-instructions, but some guidelines on how to write into a remote db with monologger. The mongodb-specific instructions are also welcome if available. ;)
Question 3(OPTIONAL):
Can I get a full error stack into the logs somehow? Where could one find a full list of what data the Monolog can actually write and how to write?
There was a very good Blogpost sometime back for logging to a mysql database with monolog and doctrine. I can't find it anymore so i will just add the neccessary Files here and you can adjust it.
The whole logic is done in the DatabaseHandler so you can just change from
mysql inserts to a handling for your mongodb.
This code is not mine if anyone knows the original post please comment.
BacktraceLoggerListener.php
namespace UtilsBundle\EventListener;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
class BacktraceLoggerListener{
private $_logger;
public function __construct(LoggerInterface $logger = null)
{
$this->_logger = $logger;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
$this->_logger->addError($event->getException());
}
}
DatabaseHandler.php
namespace UtilsBundle\Logger;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
/**
* Stores to database
*
*/
class DatabaseHandler extends AbstractProcessingHandler{
protected $_container;
/**
* #param string $stream
* #param integer $level The minimum logging level at which this handler will be triggered
* #param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct($level = Logger::DEBUG, $bubble = true)
{
parent::__construct($level, $bubble);
}
/**
*
* #param type $container
*/
public function setContainer($container)
{
$this->_container = $container;
}
/**
* {#inheritdoc}
*/
protected function write(array $record)
{
// Ensure the doctrine channel is ignored (unless its greater than a warning error), otherwise you will create an infinite loop, as doctrine like to log.. a lot..
if( 'doctrine' == $record['channel'] ) {
if( (int)$record['level'] >= Logger::WARNING ) {
error_log($record['message']);
}
return;
}
// Only log errors greater than a warning
// TODO - you could ideally add this into configuration variable
if( (int)$record['level'] >= Logger::NOTICE ) {
try
{
// Logs are inserted as separate SQL statements, separate to the current transactions that may exist within the entity manager.
$em = $this->_container->get('doctrine')->getManager();
$conn = $em->getConnection();
$created = date('Y-m-d H:i:s');
$serverData = ""; //$record['extra']['server_data'];
$referer = "";
if (isset($_SERVER['HTTP_REFERER'])){
$referer= $_SERVER['HTTP_REFERER'];
}
$stmt = $em->getConnection()->prepare('INSERT INTO system_log(log, level, server_data, modified, created)
VALUES(' . $conn->quote($record['message']) . ', \'' . $record['level'] . '\', ' . $conn->quote($referer) . ', \'' . $created . '\', \'' . $created . '\');');
$stmt->execute();
} catch( \Exception $e ) {
// Fallback to just writing to php error logs if something really bad happens
error_log($record['message']);
error_log($e->getMessage());
}
}
}
}
We used xml here but this can be done in
services.yml too
services.xml
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="utils.database.logger" class="UtilsBundle\Logger\DatabaseHandler">
<call method="setContainer">
<argument type="service" id="service_container" />
</call>
</service>
<service id="utils.backtrace.logger.listener" class="UtilsBundle\EventListener\BacktraceLoggerListener">
<argument type="service" id="logger" />
<tag name="monolog.logger" channel="backtrace" />
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
</service>
</services>
And lastly add the handler to your monolog config in
config_**.yml so here for production for example
config_prod.yml
monolog:
handlers:
main:
type: rotating_file
action_level: error
max_files: 10
handler: nested
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console
database:
type: service
level: notice
id: utils.database.logger
channels: ["!translation"]
Hope that helps
Hope I can some things up for you:
Question 1: Yes its possible. E.G. you can do smt. like:
$this->logger->pushHandler(new StreamHandler('/path/to/logs/123_info.log', Logger::INFO));
$this->logger->pushHandler(new StreamHandler('/path/to/logs/456_warning.log', Logger::INFO));
So if $this->logger->addInfo("testinfo"); this is getting logged in both streams.
Question 2: There is a MongoDBHandler as according to the StreamHandler. You should be able do configure it and pass it along to the pushHandler method or if you want to have it in your services look at MongoDBConfiguration.
Question 3:
This should help: Configure Monolog
Hope that helps.

Zend Error: Registry is already initialized

So here's the situation:
-I have a forum software, XenForo for customers to frolick about in
-I have a membership software, aMember, to handle customer payments and deliver digital products. (Both on the same website)
aMember has a template system that allows you to (ideally) easily customize the script to look like it's naturally part of your website.
XenForo has a script addon that lets you use the customized XenForo Header and footer using a PHP "include" function.
So essentially, I can take a regular php file, call the XenForo header and footer using php include, and make that page look like it's part of the forum software (almost like a wordpress header/footer). So far, everything I've mentioned is tested and working outside the aMember system, so I currently have an index.php file that calls the XenForo header and footer using include and it works great.
Here's where it gets nasty, I tried to use the PHP include script inside the aMember template system. I got it mostly working, but then the following Error is thrown with the resulting fun batch of code afterwards:
Error: Registry is already initialized
Exception Zend_Exception
Zend_Registry::setClassName [ /home/content/p/p/o/ppowers/html/forum/library/XenForo/Application.php : 244 ]
XenForo_Application::initialize [ /home/content/p/p/o/ppowers/html/forum/library/Dark/Kotomi/KotomiHeader.php : 5 ]
include_once [ /home/content/p/p/o/ppowers/html/header.php : 6 ]
include_once [ library/Am/View.php : 419 ]
Am_View->printLayoutHead [ application/default/themes/sample/layout.phtml : 8 ]
include [ library/Am/View.php : 352 ]
Am_View->_run [ library/Zend/View/Abstract.php : 888 ]
Zend_View_Abstract->render [ library/Am/View.php : 326 ]
Am_View->display [ application/default/controllers/IndexController.php : 7 ]
IndexController->indexAction [ library/Am/Controller.php : 139 ]
Am_Controller->_runAction [ library/Am/Controller.php : 116 ]
Am_Controller->dispatch [ library/Zend/Controller/Dispatcher/Standard.php : 295 ]
Zend_Controller_Dispatcher_Standard->dispatch [ library/Zend/Controller/Front.php : 954 ]
Zend_Controller_Front->dispatch [ library/Am/App.php : 1372 ]
Am_App->run [ index.php : 41 ]
From what I can tell and my limited programming knowledge, it looks like aMember and XenForo are having a fight over who gets to use the Zend Registry.
Is there anyway I can make them play well together without hiring a full time programmer for 6 months? Thanks so much!
........................................................Response to comment:
The add comment didn't have enough charecters, so here's some of the code.
From what I can tell XenForo uses it as it's primary..well...everything, here's the START of Application.php, part of XenForo's source code. This file is over 1,000 lines, all of which make up the class that start's at the top... This seems to be the only file that uses the Zend_Registry that isn't part of the Zend source itself.
class XenForo_Application extends Zend_Registry
{
const URL_ID_DELIMITER = '.';
public static $version = '1.1.0';
public static $versionId = 1010070; // abbccde = a.b.c d (alpha: 1, beta: 3, RC: 5, stable: 7, PL: 9) e
public static $jsVersion = '';
public static $jQueryVersion = '1.5.2';
protected $_configDir = '.';
protected $_rootDir = '.';
protected $_initialized = false;
protected $_lazyLoaders = array();
protected static $_handlePhpError = true;
protected static $_debug;
protected static $_randomData = '';
protected static $_classCache = array();
public static $time = 0;
public static $host = 'localhost';
aMember uses it across several files, here are a few examples:
This is inside form.php...
public function findRuleMessage(HTML_QuickForm2_Rule $rule, HTML_QuickForm2_Node $el)
{
$strings = array(
'rule.required' => ___('This is a required field'),
);
$type = lcfirst(preg_replace('/^.+rule_/i', '', get_class($rule)));
$tr = Zend_Registry::get('Zend_Translate');
$fuzzy = sprintf('rule.%s', $type);
if (array_key_exists($fuzzy, $strings))
return $strings[$fuzzy];
}
And this is inside app.php....
function amDate($string) {
if ($string == null) return '';
return date(Zend_Registry::get('Am_Locale')->getDateFormat(), amstrtotime($string));
}
function amDatetime($string) {
if ($string == null) return '';
return date(Zend_Registry::get('Am_Locale')->getDateTimeFormat(), amstrtotime($string));
}
function amTime($string) {
if ($string == null) return '';
return date(Zend_Registry::get('Am_Locale')->getTimeFormat(), amstrtotime($string));
}
Alright, I better not post any more source code or they'll send the men in black after me.
It looks like it might be easier to program it out of aMember, but this is quickly looking like an insurmountable task, especially at my (lack of) skill level.
Additional Info:
public static function initialize($configDir = '.', $rootDir = '.', $loadDefaultData = true)
{
(244)self::setClassName(__CLASS__);
self::getInstance()->beginApplication($configDir, $rootDir, $loadDefaultData);
}
Commenting out Line 244 produced the following error:
Fatal error: Call to undefined method Zend_Registry::beginApplication() in /home/content/p/p/o/ppowers/html/forum/library/XenForo/Application.php on line 245
And adding the code you suggested into the aMember index.php file produced this error:
Fatal error: Class 'XenForo_Application' not found in /home/content/p/p/o/ppowers/html/amember/index.php on line 40
What is on line 244 of /home/content/p/p/o/ppowers/html/forum/library/XenForo/Application.php? If it's just Zend_Registry which is the default class name and if you are not afraid of modifying the sources, just comment out the call on line 244. But this is not advised as you would have problems if you wanted to update XenForo in the future.
Check if whatever is passed to setClassName() on line 244 can be configured somehow. Paste some more code. Get some more answers.
UPDATE
Fighting with XenForo would probably require quite a lot of coding, so I'd suggest a rather different approach. Since aMemeber seems to use vanilla Zend_Registry, you can try to make sure XenForo's extended version of Zend_Registry instantiates first. In your index.php (probably located in public directory) locate the line with $application->bootstrap(); or similar, and before this line add something like this:
XenForo_Application::setClassName("XenForo_Application");
And comment out line 244 of /home/content/p/p/o/ppowers/html/forum/library/XenForo/Application.php.
If this works, remember to comment on the change, cross-referencing both modified files.
I build my website I am using Zend framework v1.5: http://www.panpic.vn (**) |
Forum using xenforo: http://www.panpic.vn/forum (*)
at homepage (**) I am Authentication User Xenforo
My Coding:
define('XF_ROOT', '/home/www/lighttpd/my_web/forum'); // set this (absolute path)!
define('STARTTIME', microtime(true) );
define('SESSION_BYPASS', false); // if true: logged in user info and sessions are not needed
require_once(XF_ROOT . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader(XF_ROOT . '/library');
XenForo_Application::initialize(XF_ROOT . '/library', XF_ROOT);
XenForo_Application::set('page_start_time', STARTTIME );
XenForo_Application::setDebugMode(false);
if (!SESSION_BYPASS)
{
$dependencies = new XenForo_Dependencies_Public();
$dependencies->preLoadData();
$session = XenForo_Session::startPublicSession(new Zend_Controller_Request_Http);
XenForo_Visitor::setup($session->get('user_id'));
$visitor = XenForo_Visitor::getInstance();
if ($visitor->getUserId())
{
$userModel = XenForo_Model::create('XenForo_Model_User');
$userinfo = $userModel->getFullUserById($visitor->getUserId());
}
}
Error: Registry is already initialized
Could you how do I fix ?

Magento: Email template crashes new template form

I have a module that I'm trying to use with an email template I created. I created the .phtml template directly (not through the new template form on the backend) into the locale > en_US > template > email folder. The template seems to work as the variables passed to it work and the email gets sent fine. My only problem is that now when I go into the management > Transactional Emails > New Template, the page crashes. The dropdown is empty and everything after it does't get rendered.
I think it might have something to do with the way I'm loading the template in the modules config.xml. When I remove the reference to the template the problem goes away. Put the reference back in and the form crashes..
Config.xml
<?xml version="1.0"?>
<config>
<modules>
<Optimise_Requestcallback>
<version>0.1.9</version>
</Optimise_Requestcallback>
</modules>
<frontend>
<routers>
<requestcallback>
<use>standard</use>
<args>
<module>Optimise_Requestcallback</module>
<frontName>request-callback</frontName>
</args>
</requestcallback>
</routers>
<layout>
<updates>
<requestcallback>
<file>optimise.xml</file>
</requestcallback>
</updates>
</layout>
</frontend>
<global>
<template>
<email>
<requestcallback_template translate="label" module="requestcallback">
<label>Optimise RequestCallback</label>
<file>requestcallback_template.html</file>
<type>html</type>
</requestcallback_template>
</email>
</template>
</global>
</config>
Here is how I send the email:
public function sendemailAction() {
$emailTemplate = Mage::getModel('core/email_template')
->loadDefault('requestcallback_template');
$emailTemplateVariables = array();
//Fetch submited params
$params = $this->getRequest()->getParams();
$subjectOfMail = "Request a Callback from the Puji Website<br /><br />Product = " . $params['product'] . "<br />Name = " . $params['name'] . "<br />Email = " . $params['email'] . "<br />Telephone = " . $params['telephone'] . "<br />Message = " . $params['comment'];
$emailTemplateVariables['body'] = $subjectOfMail;
$emailTemplate->setSenderName($params['name']);
$emailTemplate->setSenderEmail($params['email']);
try {
$emailTemplate->send('billy#optimiseweb.co.uk', 'Sales', $emailTemplateVariables);
Mage::getSingleton('core/session')->addSuccess('Thank you! We will contact you very soon.');
} catch (Exception $ex) {
$translate->setTranslateInline(true);
Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
$this->_redirect('*/*/');
return;
}
//Redirect back to index action of (this) activecodeline-simplecontact controller
$this->_redirect('request-callback/');
}
And the template itself probably couldn't be any simpler!
<!--#subject Request a Callback from the Puji Website #-->
{{var body}}
Can anyone see an issue here that would cause the New template form to crash?
There can be various reasons, but first check config.xml file for each custom module, one-by-one.
There must be one module, where you will find code like:
module="[some-module-name-here]"
Try removing this code one-by-one and reload the transactional e-mail template form again.
I am sure, it will solve the problem.

Nant - Get Newest Folder

Is there a relatively simple way in nant, without writing a custom task, to get the name of the newest folder in a certain directory? Recursion is not needed. I have been trying to do it with directory::get-creation-time and a foreach loop and if statements, yada yada. It's too complex, and I'm about to create a custom task instead. However, I suspect there is some simpler way to do it via existing nant features.
I believe you're correct in stating that doing this in a pure nant fashion might pose to be messy, especially the way properties work in nant. If you don't want to write a custom task, you can always use the script task. For example:
<?xml version="1.0"?>
<project name="testing" basedir=".">
<script language="C#" prefix="test" >
<code>
<![CDATA[
[Function("find-newest-dir")]
public static string FindNewestDir( string startDir ) {
string theNewestDir = string.Empty;
DateTime theCreateTime = new DateTime();
DateTime theLastCreateTime = new DateTime();
string[] theDirs = Directory.GetDirectories( startDir );
for ( int theCurrentIdx = 0; theCurrentIdx < theDirs.Length; ++theCurrentIdx )
{
if ( theCurrentIdx != 0 )
{
DateTime theCurrentDirCreateTime = Directory.GetCreationTime( theDirs[ theCurrentIdx ] );
if ( theCurrentDirCreateTime >= theCreateTime )
{
theNewestDir = theDirs[ theCurrentIdx ];
theCreateTime = theCurrentDirCreateTime;
}
}
else
{
theNewestDir = theDirs[ theCurrentIdx ];
theCreateTime = Directory.GetCreationTime( theDirs[ theCurrentIdx ] );
}
}
return theNewestDir;
}
]]>
</code>
</script>
<property name="dir" value="" overwrite="false"/>
<echo message="The newest directory is: ${test::find-newest-dir( dir )}"/>
</project>
With this, one should be able to call the function to get the newest directory. The implementation of the actual function could be changed to be anything (optimized a bit more or whatever), but I've included a quick one for reference on how to use the script task. It produces output like the following:
nant -D:dir=c:\
NAnt 0.85 (Build 0.85.2478.0; release; 10/14/2006)
Copyright (C) 2001-2006 Gerry Shaw
http://nant.sourceforge.net
Buildfile: file:///C:/tmp/NAnt.build
Target framework: Microsoft .NET Framework 2.0
[script] Scanning assembly "jdrgmbuy" for extensions.
[echo] The newest directory is: C:\tmp
BUILD SUCCEEDED
Total time: 0.3 seconds.