Calcetines Extreme

Calcetines Extreme
Take care of you using the best socks

Friday, December 25, 2020

How to text spin using a wordpress shorcut

Problem:
You need to create random text but with sense, for example, you have a web page that have thousand of pages and you would like to create text automatically using text spin technique.

Solution:
1.- Create a new function inside your Wordpress "Snippets" plugin.

function fun_textspin() {
class Spintax
{
public function process($text)
{
return preg_replace_callback(
'/\{(((?>[^\{\}]+)|(?R))*?)\}/x',
array($this, 'replace'),
$text
);
}
public function replace($text)
{
$text = $this->process($text[1]);
$parts = explode('|', $text);
return $parts[array_rand($parts)];
}
}
$spintax = new Spintax();
$string = '{Hello|Howdy|Hola} to you, {Mr.|Mrs.|Ms.} Smith|Williams|Davis}!';
return $spintax->process($string);
}

add_shortcode( 'sc_textspin','fun_textspin' );


 

2.- add the following Shorcut to your wordpress page.

<!-- wp:paragraph -->

<p>[sc_textspin]</p>

<!-- /wp:paragraph -->


Source:
https://gist.github.com/irazasyed/11256369 


Thursday, December 3, 2020

How to connect to Wordpress SQL using shortcuts

Problem:

Yo need to access to any database table connected to your wordpress or maybe an external one to extract any information and process it.

Solution:

Use this TestSQLand add is to your wordpress functions.php of "Code Snipper" plugin:

function TestSQL_Function( $atts ) {
$link = mysqli_connect("localhost", "user", "****", "DATABASENAME");
$sql = "SELECT * FROM table";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        while($row = mysqli_fetch_array($result)){
            $line = $line.$row['fielname1']."(".$row['fieldname2'].")";
        }
return $line;
        // Free result set
        mysqli_free_result($result);
    } else{
        return "No records matching your query were found.";
    }
} else{
    return "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
}
add_shortcode( 'TestSQL', 'TestSQL_Function' );

Now, you only need to add the wordpress shortcut [TestSQL] in your wordpres post or page.

Monday, November 16, 2020

Protect your CMS Backoffice login without plugins

Problem:

You are working with a Wordpress or Joomla CMS and you need to protect your back-office login to avoid massive attacks or hacking, but you are not sure to use a plugin to do that, you are looking to know what are the page in the corresponding CMS to edit to add your own script.

Solution:

It's quite easy to protect your CMS back office, it depend of the CMS that you are working on, you can do this using a little script to avoid any access to the login screen.

The script is something like this one:

<?php
$vip = $_SERVER['REMOTE_ADDR'];
if ( ($vip!='92.239.116.139')  ) {
  echo "<div id='errmessage'>Forbbiden to:".$vip."</div>";
} 
?>

Now, you only need to add this lines of code to the following files, it depend of the site and/or page that you would like to protect.

With this script, only connections to your site from the choose IP will be able to access to your site, if your IP is not static, you will need to update this script or maybe use another one that use other check rather than the remote ip connected to the site.

Joomla:

  • Frontoffice:  
    /httpdocs/templates/<yourtemplate>/index.php
  • BackOffice:  
    /httpdocs/administrator/templates/<yourtemplate>/index.php

Wordpress:
  • Login:  /httpdocs/wp-login.php


Sunday, November 15, 2020

Can't access to wordpress multi-site wp-admin

Problem:

You have setup the standard Wordpress multi site configuration in Wordpress ( using "directories" not subdomains ) and create one or more sub sites; the problems is that when you try to access to the sub-site web view or sub-site wp-admin you get a 404 error and it's not possible to view the template styles files or login into the wp-admin to manage the sub site.

Solution:

you have to apply a different configuration in the .httpaccess file, just apply the next configuration:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]

Source:

index.php in wordpress friendly url

 Problem:

After the automatic installation using a word press template installation wizard, you see that the permanent links have the "index.php" in the friendly url; you need to remove index.php from friendly url.

Solution:

To fix this problem you have to go to settings, permanent links, custom structure, there you will se something like this:

index.php/%year%/%month%/%day%/%postmark%/

You only need to change it like this:

/%year%/%month%/%day%/%postmark%/

Tuesday, October 27, 2020

How to scrap elements from external website

Problem:

You need to pic information from a public website and process it internally to do some staff.

Solution:

You can use PHP Simple HTML DOM parse as the easy way, follow the next steps:

  1. Download the sources from the oficial web site
  2. copy all files into your web server
  3. create a new file example.php
  4. use this code:

include_once('simple_html_dom.php');

$context = stream_context_create();

stream_context_set_params($context, array(

            'ignore_errors' => true, 

            'max_redirects' => 3)

            );

$html = file_get_html('https://www.elmundo.es', 0, $context);

$articles_titles = $html->find(".ue-c-cover-content__link");

foreach($articles_titles as $article_title) {

    echo "<BR>\t\t".$article_title->plaintext."\n";

}

$html->clear();

unset($html);

echo "<BR>";echo "<BR>";echo "<BR>";

echo "<BR>OK";

Information That's it, now you only need to analyse the information that you get as html and process it in your function or program, take care about your objective, the server that you use to extract the information can ban your IP; to avoid that it's possible to use a proxy as is indicated int he source link.

Source:

https://www.fernandezsansalvador.es/hacer-web-scraping-con-php-simple-html-dom-parser/


Monday, October 19, 2020

How to setup PHP mvc minimun model without any framework

 Problem:

You need to create a minimum web site or web app using php model but it's preferred to use any framework to avoid load the system without any unnecessary code.

Solution:

Create the following folder and fields structure in the server.
\
\\controllers\sample_controler.php
\\db\connection.php
\\models\sample_model.php
\\views\sample_view.html
\\index.php

Use the next code in each file:

\\controllers\sample_controler.php
<?php
require_once("models/sample_model.php");
$samp=new sample_model();
$data=$samp->get_samplequery();
require_once("views/sample_view.html");
?>
\\db\connection.php
<?php
class Connect{
    public static function connection(){
        $connection=new mysqli("localhost", "root", "pwd", "mvc");
        $connection->query("SET * FROM table 'UTF8' ");
        return $connection;
    }
}
?>

\\models\sample_model.php
<?php
class sample_model{
    private $DB;
    private $sample;
    public function __construct(){
        $this->DB=Connect::connection();
        $this->sample=array();
    }
    public function get_samplequery(){
        $query=$this->db->query("select * from table;");
         ...
        return $this->query;
    }
}
?>

\\views\sample_view.html
<html lang="es">
    <head>
        <meta charset="UTF8" />
        <title>Sample</title>
    </head>
        <?php echo "put your database query here"        ?>
</html>

\index.php
<?php
require_once("db/connection.php");
require_once("controllers/sample_controler.php");
?>