A string replace with array - plugins

I am trying to create a simple image gallery for joomla 3. I have managed to get the code working. The way it works is that, it detects the string {dGalley}stories{/dGallery} from article. I have done this so far. It works but it only prints one image. Any suggestion? Thanks in advance.
<?php
defined('_JEXEC') or die;
class PlgContentgallery extends JPlugin
{
public function onContentPrepare($content, $article, $params, $limit){
preg_match_all('/{dGallery}(.*?){\/dGallery}/is', $article->text, $matches);
$i=0;
foreach ($matches[1] as $value)
{
$result = array();
$dir = '/home/juniorwe/public_html/images/'.$matches[1][$i];
$cdir = scandir($dir);
foreach ($cdir as $key => $value)
{
if (!in_array($value,array(".","..")))
{
if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
{
$result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
}
else
{
$article->text = str_replace('{dGallery}'.$matches[1][$i].'{/dGallery}','<img src="../images/'.$matches[1][$i].'/'.$value.'"/>
', $article->text);
}
}
}
$i++;
}
}
}
Updated code. It works but a bit dirty solution:
<?php
defined('_JEXEC') or die;
class PlgContentgallery extends JPlugin
{
public function onContentPrepare($content, $article, $params, $limit)
{
$document = JFactory::getDocument();
$document->addScript(JURI::base(). "plugins/content/gallery/jquery.colorbox.js");
$document->addScript(JURI::base(). "plugins/content/gallery/colorboxCall.js");
$document->addStyleSheet(JURI::base(). "plugins/content/gallery/colorbox.css");
preg_match_all('/{dGallery}(.*?){\/dGallery}/is', $article->text, $matches);
$i=0;
foreach ($matches[1] as $value)
{
$result .= '';
$dir = '/home/juniorwe/public_html/images/'.$matches[1][$i];
$cdir = scandir($dir);
foreach ($cdir as $key => $value)
{
if (!in_array($value,array(".","..")))
{
if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
{
$result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
}else{
$result .='<a class="group4" href="../images/'.$matches[1][$i].'/'.$value.'" title=""><img src="../images/'.$matches[1][$i].'/'.$value.'" class="gallery"/></a>';
}
}
}
$article->text = str_replace('{dGallery}'.$matches[1][$i].'{/dGallery}',$result, $article->text);
$i++;
}
}
}

You want something more like this
public function onContentPrepare($content, $article, $params, $limit)
{
preg_match_all('/{dGallery}(.*?){\/dGallery}/is', $article->text, $matches);
foreach ($matches[0] as $folder)
{
$result = array();
$dir = '/home/juniorwe/public_html/images/' . $folder; // You might want to get the stored value for image path instead of hard coding
if (is_dir($dir))
{
$cdir = JFilesystem::files($dir);
foreach ($cdir as $value)
{
$article->text = str_replace('{dGallery}' . $folder .'{/dGallery}','<img src="/images/' . $folder . '/' . $value. ' "/>', $article->text);
}
}
}
}
Is it that you are trying to get the images from the sub folders also?

Related

codeigniter : $shows->result() not returning all records

I do not know why this method is showing error, the number rows returned on $shows when count is two, but when i try to display it using
#
foreach ($values as $keys => $val) {
$itemsz = $shows->result();
}
It will return 1 record
######################################
#
foreach ($values as $keys => $val) {
$itemsz[] = $shows->result();
}
It will show error
#
Please i do not know where am making the mistake, i want this query to return as many records as possible.
public function dist_order_cart_detail($ord_no, $prd_id)
{
$logged_id = 4;
$this->db->select('*');
$this->db->from('message');
$this->db->where('ord_no', $ord_no);
$this->db->where('to_id', $logged_id);
$show = $this->db->get();
$values = $show->result();
$items = array();
foreach ($values as $keys => $val) {
$items[] = $val->product_id;
}
$itemsz = array();
foreach ($items as $val) {
$this->db->select('*');
$this->db->from('order_details');
$this->db->where('order_details.ord_det_order_number', $order_no);
$this->db->where_in('order_details.ord_det_item_id',$val);
$shows = $this->db->get();
$values = $shows->result();
foreach ($values as $keys => $val) {
$itemsz[] = $shows->result();
}
}
return $itemsz;
}
observe that you are invoking $itemsz[] = $shows->result(); within a for loop? is this intended? appears to be a source of bug.

cakephp3 plugin not load my translation file

my cakephp3 plugin with name "AdminTheme" has a localisation file.
AdminTheme
/src
/Locale
/de
AdminTheme.po
The locale change perfectly. In my templates I use the function
__d('AdminTheme', 'Sign In')
The function
\Cake\I18n\MessagesFileLoader::__invoke()
says in line 122 (is_file()): File not found.
pathToMyCake/plugins/AdminTheme/src/Locale/de/AdminTheme.po
public function __invoke()
{
$package = new Package('default');
$folders = $this->translationsFolders();
$ext = $this->_extension;
$file = false;
$fileName = $this->_name;
$pos = strpos($fileName, '/');
if ($pos !== false) {
$fileName = substr($fileName, $pos + 1);
}
foreach ($folders as $folder) {
$path = $folder . $fileName . ".$ext";
if (is_file($path)) {
$file = $path;
break;
}
}
if (!$file) {
return $package;
}
$name = ucfirst($ext);
$class = App::classname($name, 'I18n\Parser', 'FileParser');
if (!$class) {
throw new RuntimeException(sprintf('Could not find class %s', "{$name}FileParser"));
}
$messages = (new $class)->parse($file);
$package->setMessages($messages);
return $package;
}
Why?
The file is correctly?
Please help me. Very thank's.
The debugger screens
I'm confused.

How to pass a variable in a URL using Perl?

I'm trying to pass parameters in a URL. I don't know what's missing, I tried to see how the URL looks after executing this script.
my $request3 = HTTP::Request->new(GET => $sql_activation);
my $useragent = LWP::UserAgent->new();
$useragent->timeout(10);
my $response2 = $useragent->request($request3);
if ($response2->is_success) {
my $res2 = $response2->content;
if ($res =~ m/[#](.*):(.*)[#]/g) {
my ($key, $username) = ($1, $2);
print "[+] $username:$key \n\n";
}
else {
print "[-] Error \n\n";
}
}
my $link =
"http://localhost/wordpress/wp-login.php?action=rp&key="
. $key
. "&login="
. $username;
sub post_url {
my ($link, $formref) = #_;
my $ua = new LWP::UserAgent(timeout => 300);
$ua->agent('perlproc/1.0');
my $get = $ua->post($link, $formref);
if ($get->is_success) {
print "worked \n";
}
else {
print "Failed \n";
}
}
After executing the script the URL is like this
site/wordpress/wp-login.php?action=rp&key=&login=
Perl has block level scope. You define $key and $username in the block following an if statement. They don't live beyond that.
You need to create them (with my) before that block.
# HERE
my ( $key, $username );
if ( $response2->is_success ) {
my $res2 = $response2->content;
if ( $res =~ m/[#](.*):(.*)[#]/g ) {
# Don't say my again
( $key, $username ) = ( $1, $2 );
}
else { print "[-] Error \n\n"; }
}

Reference to a string as a class variable

I'm trying to save a reference to a string in a class variable.
I wish to access this variable by dereferencing it.
For example in the routine getHeaders instead of using:
my $fileContentsRef = $this->getFileContent;
my $fileContentsRef1 = $$fileContentsRef;
$fileContentsRef1 =~ /Spaltenname.*?Datentyp.*?---\n(.*?)\n\n/gsmi;
I would like to use:
my $fileContentsRef = $this->getFileContent;
$$fileContentsRef =~ /Spaltenname.*?Datentyp.*?---\n(.*?)\n\n/gsmi;
For more details you should see the code at the end.
My problem is, that the program doesn't work when I don't work with the copy( i.e when I don't use $fileContentsRef1). What am I doing / getting wrong? Is it possible to reach the goal in the way I described? Could some give me clues how?
open FILE, "a1.bad";
$file_contents .= do { local $/; <FILE> };
close FILE;
my $log = auswerter->new(\$file_contents);
#-----------------------------------------------------------------
# Subs
#-----------------------------------------------------------------
# CONSTRUCTOR
sub new
{
my $fileRef = $_[1];
my $self = {};
bless $self;
$self->initialize();
if($fileRef) { $self->{fileRef} = $fileRef; }
return $self;
}
sub initialize
{
#-----------------------------------------------------------------
# Configuration
#-----------------------------------------------------------------
my $this = shift;
}
sub setFile {
my $this = shift;
$this->{file} = shift;
}
sub getFileContent
{
my $this = shift;
return $this->{fileRef};
}
sub getHeaders
{
print "HEADERS...\n";
my $this = shift;
my #headers = ();
my $fileContentsRef = $this->getFileContent;
my $fileContentsRef1 = $$fileContentsRef;
$fileContentsRef1 =~ /Spaltenname.*?Datentyp.*?---\n(.*?)\n\n/gsmi;
#headers = split ("\n", $1 );
foreach (#headers)
{
$_ =~ s/^(.*?)\s.*/$1/;
}
return \#headers;
}
sub getErrList
{
print "ERR LIST...\n";
my $this = shift;
my #errors = ();
my $fileContentsRef = $this->getFileContent;
my $fileContentsRef1 = $$fileContentsRef;
$fileContentsRef1 =~ /Spaltenname.*?(Satz.*)ORA.*?^Tabelle/gsmi;
return \#errors if !$1;
#errors = split ("\n\n", $1 );
foreach (#errors)
{
$_ =~ s/.*Spalte (.*?)\..*/$1/msgi;
}
return \#errors;
}
sub getEntries
{
my $this = shift;
my #entries = ();
my $fileContentsRef = $this->getFileContent;
my $fileContentsRef1 = $$fileContentsRef;
$fileContentsRef1 =~ /.*==\n(.*)/gsmi;
#entries = split ("\n", $1 );
return \#entries;
}
sub sqlldrAnalyze
{
my $this = shift;
my $token = shift;
my $errRef =$this->getErrList();
return "" if $#$errRef < 0 ;
my $headersRef = $this->getHeaders();
my $entriesRef = $this->getEntries();
my $i = 0;
my $str = "";
$str = "<html>";
$str .= "<table rules=\"all\">";
$str .= "<tr>";
foreach ( #$headersRef)
{
$str .= "<th>".$_."</th>";
}
$str .= "</tr>";
foreach ( #$entriesRef)
{
my #errOffset = grep { $headersRef->[$_] =~ $errRef->[$i] }0..$#$headersRef ;
my #entries = split($token, $_);
$str .= "<tr>";
foreach (my $j =0; $j <= $#entries;$j++)
{
$str .= "<td nowrap";
$str .= " style=\"background-color: red\"" if $j == $errOffset[0];;
$str .= ">";
$str .= "<b>" if $j == $errOffset[0];
$str .= $entries[$j];
$str .= "</b>" if $j == $errOffset[0];
$str .= "</td>";
}
$str .= "</tr>\n";
$i++;
}
$str .= "</table>";
$str .= "</html>";
return $str;
}
return 1;
When you call your class->new(...) constructor with a filename argument, the new subroutine gets the class name as the first argument, and the filename as the second argument.
In your constructor, you are simply copying the value of $_[1] (the filename) into $self->{FileRef}, but that value is not a reference.
So when you access it, there is no need to use a doubled sigil to dereference the value.
You should run all of your code with the following two lines at the top, which will catch many errors for you (including trying to use strings as references when they are not references):
use strict;
use warnings;
These two lines basically move Perl out of quick one-liner mode, and into a mode more suitable for large development (improved type safety, static variable name checking, and others).
Per the update: If the code you have is working properly when copying the string, but not when dereferencing it directly, it sounds like you may be running into an issue of the string reference preserving the last match position (the g flag).
Try running the following:
my $fileContentsRef = $this->getFileContent;
pos($$fileContentsRef) = 0; # reset the match position
$$fileContentsRef =~ /Spaltenname.*?Datentyp.*?---\n(.*?)\n\n/gsmi;

How can I find the full URL from a relative URL in Perl?

I'm new to perl but was wondering if anyone know of a script that was similar to the following PHP version which works great!
private function resolve_href ( $base, $href ) {
if (!$href)
return $base;
$rel_parsed = parse_url($href);
if (array_key_exists('scheme', $rel_parsed))
return $href;
$base_parsed = parse_url("$base ");
if (!array_key_exists('path', $base_parsed))
$base_parsed = parse_url("$base/ ");
if ($href{0} === "/")
$path = $href;
else
$path = dirname($base_parsed['path']) . "/$href";
$path = preg_replace('~/\./~', '/', $path);
$parts = array();
foreach ( explode('/', preg_replace('~/+~', '/', $path)) as $part ) {
if ($part === "..")
array_pop($parts);
elseif ($part!="")
$parts[] = $part;
}
$dir = ( ( array_key_exists('scheme', $base_parsed)) ? $base_parsed['scheme'] . '://' . $base_parsed['host'] : "" ) . "/" . implode("/", $parts);
return str_replace( "\/", '', $dir );
}
Any help is much appreciated
See URI:
#!/usr/bin/perl
use strict; use warnings;
use URI;
my $u = URI->new_abs('../foobar', 'http://foo.com/bar/poo/');
print $u->canonical;
Output:
http://foo.com/bar/foobar