Trying to add custom background image to Gregwar Captcha - laravel

Here's my Utility/Captcha.php
i'm trying to add a custom background image to the captcha.
https://github.com/Gregwar/Captcha
Under API
setBackgroundImages(array($imagepath1, $imagePath2)), Sets custom background images to be used as captcha background. It is recommended to disable image effects when passing custom images for background (ignore_all_effects). A random image is selected from the list passed, the full paths to the image files must be passed.
I don't know how to set the file path to point to /img/captchabackground.jpg
What am i doing wrong?
<?php
namespace App\Site\Utility;
use Gregwar\Captcha\CaptchaBuilder;
/**
* Creating and Verifying Captcha
*/
class Captcha
{
public static function Build()
{
$width = 300;
$height = 50;
$char_number = 8;
$builder = new CaptchaBuilder($char_number);
$builder->build($width,$height);
session()->put('captcha',$builder->getPhrase());
return $builder->inline();
}
public static function Verify($input)
{
if (!session()->has('captcha')) {
return false;
}
if (session()->get('captcha') !== $input) {
return false;
}
return true;
}
}
<?php
namespace App\Site\Utility;
use Gregwar\Captcha\CaptchaBuilder;
/**
* Creating and Verifying Captcha
*/
class Captcha
{
public static function Build()
{
$width = 300;
$height = 50;
$char_number = 8;
$builder = new CaptchaBuilder($char_number);
$builder->build($width,$height);
setBackgroundImages(array($imagepath1, $imagePath2)),
session()->put('captcha',$builder->getPhrase());
return $builder->inline();
}
public static function Verify($input)
{
if (!session()->has('captcha')) {
return false;
}
if (session()->get('captcha') !== $input) {
return false;
}
return true;
}
}

Related

Laravel application freezes doesn't respond anymore

I have a laravel application that uses a while loop to process a txt file(6000 lines) line by line together with various helpers to manipulate each string. At the end of the manipulation it stores a record in db for each line. Everything works fine, the application process the entire file in a minute and returns the view indicated. The problem is that the application than freeze(not sure if this is the appropriate term), it keeps running and the browser ask repeatedly to wait or close the application.
I noticed also this message in the laravel server if it may help
[Wed Apr 21 10:28:26 2021] 127.0.0.1:50897 Closed without sending a request; it was probably just an unused speculative preconnection
Thanks to anyone who could help me.
This is the process:
$log = new UserActivityHelper;
$log->create(Config::get('constants.user_activities.FTP_FILE_PROCESSED_START'), 'test');
$file = fopen(Storage::path("1-2021.txt"), 'r');
while(!feof($file))
{
$line = fgets($file);
//call the method that validate the string
$string = new ValidateStringHelper($line); //FIRST HELPER
$string->validate();
//check the result of the validation
if($string->validated == false){
dd("out");
} elseif ($string->empty == true){
continue;
}else{
//save the validated array of substrings as variable
$my_arr = $string->validatedChars;
//check if the province exists or create it
$province = new ProvinceExistsHelper($my_arr['district']); //SECOND HELPER
$province->check_if_exists_or_create();
if($province->new_province == true) {
$log = new UserActivityHelper;
$log->create(Config::get('constants.ftp_process.FTP_PROVINCE_CREATED'), "Creata nuova provincia con id {$province->district_id}");
self::message('yellow', "Attenzione. Nell'elaborazione sono state create nuove provincie, controllare le impostazioni.");
}
//manipolation of name and lastname
$name = ucwords(strtolower($my_arr['name_lastname']));
//check if the person already exists or create it (passing fiscal code, name and district_id)
$person = new PersonExistsHelper($my_arr['fiscal_code'], $name, $province->district_id); //THIRD HELPER
$person->check_if_exists_or_create();
$newMovement = new Movement;
$newMovement->person_id = $person->person_id;
if(array_key_exists('level', $my_arr)){
$newMovement->level = $my_arr['level'];
}
...
try {
$newMovement->save();
} catch (\Throwable $exception) {
report($exception);
return;
}
}
}
fclose($file);
$log = new UserActivityHelper;
$log->create(Config::get('constants.user_activities.FTP_FILE_PROCESSED_END'), 'test');
return view('ftp.test');
Helpers called:
class ValidateStringHelper
{
public $line;
public $empty = false;
public $validated = false;
public $validationErrors = array();
public $validatedChars = array();
public function __construct($line){
$this->line = $line;
}
public function validate(){
if ($this->line!="")
{
if(strlen($this->line) >= 186)
{
//substrings
$district = trim(substr($this->line, 0, 2));
$trade_union_tax_code = trim(substr($this->line, 2, 3));
...
//validation
//check if validated
$this->validatedChars['district'] = $district;
$this->validatedChars['trade_union_tax_code'] = $trade_union_tax_code;
...
//filter all non empty string values and not all zero strings
$filter_arr = array_filter($this->validatedChars, fn($value) => $value !== '' && preg_filter('/^(?!0*$).*$/', '$0', $value));
$this->validatedChars = $filter_arr;
$this->validated = true;
return $this->validated;
//return $this->validatedChars;
//eturn $this->validated;
} else {
$this->validationErrors[] = "string_length";
return $this->validated;
}
} else {
$this->empty = true;
return $this->empty;
}
}
class PersonExistsHelper
{
public $name_lastname;
public $fiscal_code;
public $district_id;
public $new_person = false;
public $person_id;
public function __construct(string $fiscal_code, string $name_lastname, string $district_id){
$this->fiscal_code = $fiscal_code;
$this->name_lastname = $name_lastname;
$this->district_id = $district_id;
}
public function check_if_exists_or_create()
{
$person = Person::where('fiscal_code', '=', $this->fiscal_code)->first();
if($person == NULL)
{
$this->new_person = true;
$newPerson = new Person;
$newPerson->name_lastname = $this->name_lastname;
$newPerson->district_id = $this->district_id;
$newPerson->fiscal_code = $this->fiscal_code;
$newPerson->created_by = Config::get('constants.people_creation.FTP_PROCESS');
$newPerson->save();
$this->person_id = $newPerson->id;
return $this->person_id;
} else{
$this->person_id = $person->id;
return $this->person_id;
}
}
}
class ProvinceExistsHelper
{
public $district_id;
public $district_code;
public $new_province = false;
public function __construct($district_code){
$this->district_code = $district_code;
}
public function check_if_exists_or_create()
{
//check if the province is in the Provinces table
$province = Province::where('code', '=', $this->district_code)->first();
if(!isset($province))
{
$this->new_province = true;
$newProvince = new Province;
$newProvince->name = $this->district_code;
$newProvince->code = $this->district_code;
$newProvince->save();
$this->district_id = $newProvince->id;
return $this->district_id;
} else {
//if yes, just return the id of the province
$this->district_id = $province->id;
return $this->district_id;
}
}
}

Hide header and footer in custom pdf Prestashop

I made method to print custom pdf in my module in Prestashop 1.7.4.1. Everything works fine, but it print header with shop logo and footer with information about electronic invoice on every page. How can I hide them that my template take all size of the printed page?
I've tried to add to my pdf object code from tcpdf example but it seems I don't use TCPDF in presta:
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
This is my class:
class HTMLTemplateCustomPdf extends HTMLTemplate
{
public $custom_model;
public function __construct($custom_object, $smarty)
{
$this->custom_model = $custom_object;
$this->smarty = $smarty;
}
/**
* Returns the template's HTML content
* #return string HTML content
*/
public function getContent()
{
//here I get content
return $this->smarty->fetch(_PS_MODULE_DIR_ . 'ps_first_module/views/templates/hook/pdf.tpl');
}
/**
* Returns the template filename
* #return string filename
*/
public function getFilename()
{
return 'custom_pdf.pdf';
}
/**
* Returns the template filename when using bulk rendering
* #return string filename
*/
public function getBulkFilename()
{
return 'custom_pdf.pdf';
}
This is where I create pdf object:
if (Tools::isSubmit('print')) {
if (Shop::getContext() != Shop::CONTEXT_GROUP && Shop::getContext() != Shop::CONTEXT_ALL) {
require_once _PS_MODULE_DIR_ . 'ps_first_module/HTMLTemplateCustomPdf.php';
$orientation = 'L';
$pdf = new PDF($custom_object, 'CustomPdf', Context::getContext()->smarty, $orientation);
$pdf->render();
}
}
EDIT:
This is my PDFGenerator.php override. Should I put this in root/override/classes/pdf or my_module/override/classes/pdf?
<?php
class PDFGenerator extends PDFGeneratorCore
{
/**
* #param bool $use_cache
* #param string $orientation
*/
public function __construct($use_cache = false, $orientation = 'L')
{
parent::__construct($orientation, 'mm', 'A4', true, 'UTF-8', $use_cache, false);
$this->setRTL(Context::getContext()->language->is_rtl);
}
/**
* Write a PDF page
*/
public function writePage()
{
if(!$this->header){
$this->SetHeaderMargin(0);
}
else{
$this->SetHeaderMargin(5);
}
if(!$this->footer){
$this->SetFooterMargin(0);
}
else{
$this->SetFooterMargin(21);
}
$this->setMargins(10, 10, 10);
$this->AddPage();
$this->writeHTML($this->content, true, false, true, false, '');
}
}
I've tried in version 1.7.2 and file properties mention Producer: TCPDF 6.2.12 (http://www.tcpdf.org). Also, class Pdf in function render() as:
$this->pdf_renderer->createHeader($template->getHeader());
$this->pdf_renderer->createFooter($template->getFooter());
So, the best way would be your class HTMLTemplateCustomPdf to include the functions getHeader() and getFooter() to return false (or empty) otherwise it will use those from HTMLTemplateCore.
In the override for PDFGenerator, you can do something like:
public function writePage()
{
if(!$this->header){
$this->SetHeaderMargin(0);
}
else{
$this->SetHeaderMargin(5);
}
if(!$this->footer){
$this->SetFooterMargin(0);
}
else{
$this->SetFooterMargin(21);
}
$this->setMargins(10, 40, 10);
$this->AddPage();
$this->writeHTML($this->content, true, false, true, false, '');
}
You can also set different margins if needed in $this->setMargins(10, 40, 10);

Magento not navigating to search result page

Any idea why my magento search is not navigating to the search result page at all. it just refreshes the page every time submit is clicked.
heres the link to the website http://remas.com.au
Thanks in advance!
Your form action is http://remas.com.au/catalogsearch/result/?q=neoguri (when query is neoguri). When the form is submitted, a GET request is made to the indexAction of Mage_CatalogSearch_ResultController:
/**
* Display search result
*/
public function indexAction()
{
$query = Mage::helper('catalogsearch')->getQuery();
/* #var $query Mage_CatalogSearch_Model_Query */
$query->setStoreId(Mage::app()->getStore()->getId());
if ($query->getQueryText() != '') {
if (Mage::helper('catalogsearch')->isMinQueryLength()) {
$query->setId(0)
->setIsActive(1)
->setIsProcessed(1);
}
else {
if ($query->getId()) {
$query->setPopularity($query->getPopularity()+1);
}
else {
$query->setPopularity(1);
}
if ($query->getRedirect()){
$query->save();
$this->getResponse()->setRedirect($query->getRedirect());
return;
}
else {
$query->prepare();
}
}
Mage::helper('catalogsearch')->checkNotes();
$this->loadLayout();
$this->_initLayoutMessages('catalog/session');
$this->_initLayoutMessages('checkout/session');
$this->renderLayout();
if (!Mage::helper('catalogsearch')->isMinQueryLength()) {
$query->save();
}
}
else {
$this->_redirectReferer();
}
}
It looks like $query->getQueryText() is null or empty
if ($query->getQueryText() != '') {
...
}
else {
$this->_redirectReferer();
}
so the action redirects back to the page it was submitted from. If you have a debugger, set a breakpoint at the top of the method
$query = Mage::helper('catalogsearch')->getQuery();
and inspect the $query object. You can also dump the object. If this doesn't tell you anything, continue digging. This is the getQuery method of the catalogsearch helper (Mage_CatalogSearch_Helper_Data):
/**
* Retrieve query model object
*
* #return Mage_CatalogSearch_Model_Query
*/
public function getQuery()
{
if (!$this->_query) {
$this->_query = Mage::getModel('catalogsearch/query')
->loadByQuery($this->getQueryText());
if (!$this->_query->getId()) {
$this->_query->setQueryText($this->getQueryText());
}
}
return $this->_query;
}
and the getQueryText method:
/**
* Retrieve search query text
*
* #return string
*/
public function getQueryText()
{
if (!isset($this->_queryText)) {
$this->_queryText = $this->_getRequest()->getParam($this->getQueryParamName());
if ($this->_queryText === null) {
$this->_queryText = '';
} else {
/* #var $stringHelper Mage_Core_Helper_String */
$stringHelper = Mage::helper('core/string');
$this->_queryText = is_array($this->_queryText) ? ''
: $stringHelper->cleanString(trim($this->_queryText));
$maxQueryLength = $this->getMaxQueryLength();
if ($maxQueryLength !== '' && $stringHelper->strlen($this->_queryText) > $maxQueryLength) {
$this->_queryText = $stringHelper->substr($this->_queryText, 0, $maxQueryLength);
$this->_isMaxLength = true;
}
}
}
return $this->_queryText;
}
In one of these two methods, some how the query text is set empty.

Varien_Data_Collection pagination not working

I have been created a Varien_Data_Collection from scratch:
$myCollection = new Varien_Data_Collection();
after add some items into it, $myCollection->addItem($fooItem);, I tried to paginate it with:
$myCollection->setPageSize(3)->setCurPage(1);
but when I display the collection, it shows all items in the collection instead the first, second o N page.
$myItems = $myCollection->getItems();
foreach ($myItems as $item) {
echo $item->getData('bar');
}
The (non-abstract) base-class Varien_Data_Collection - although it does have the setPageSize and setCurPage methods, those are not reflected within the aggregation of the Iterator:
class Varien_Data_Collection implements IteratorAggregate, Countable
{
...
/**
* Implementation of IteratorAggregate::getIterator()
*/
public function getIterator()
{
$this->load();
return new ArrayIterator($this->_items);
}
...
It will in any case return an ArrayIteator that has all objects. The load method doesn't change a thing here btw:
...
/**
* Load data
*
* #return Varien_Data_Collection
*/
public function loadData($printQuery = false, $logQuery = false)
{
return $this;
}
...
/**
* Load data
*
* #return Varien_Data_Collection
*/
public function load($printQuery = false, $logQuery = false)
{
return $this->loadData($printQuery, $logQuery);
}
...
I'd say this qualifies as a bug, as the public interface makes one assume that the magent collection object Varien_Data_Collection provides pagination while it does not.
I have not searched for a bug-report regarding this issue. The solution is to use another iterator for pagination, for example like outlined in an answer to How to Paginate lines in a foreach loop with PHP with PHP's LimitIterator:
/**
* Class Varien_Data_Collection_Pagination
*
* Aggregates missing Pagination on Collection on getting the Iterator
*
* #author hakre <http://hakre.wordpress.com/>
*/
class Varien_Data_Collection_Pagination implements IteratorAggregate
{
/**
* #var Varien_Data_Collection
*/
private $collection;
public function __construct(Varien_Data_Collection $collection)
{
$this->collection = $collection;
}
public function getIterator()
{
$collection = $this->collection;
if (FALSE === $size = $collection->getPageSize()) {
return $collection;
}
$page = $collection->getCurPage();
if ($page < 1) {
return $collection;
}
$offset = $size * $page - $size;
return new LimitIterator(new IteratorIterator($collection), $offset, $size);
}
}
Usage Example:
$collection = new Varien_Data_Collection();
$collectionIterator = new Varien_Data_Collection_Pagination($collection);
# [...] fill collection
# set pagination:
$collection->setPageSize(3);
$collection->setCurPage(1);
echo iterator_count($collectionIterator); # 3 (int)
Keep in mind that this is mainly an example. IMHO this should be fixed upstream within the Magento codebase, so it's perhaps worth you do some search work in the Magento Bug-Tracker.
As explained in previous post Varien_Data_collection does not support pagination. One solution would be to extend this class and overwrite its loadData() method. Here is practical example of paginating array of stdClass objects. Can be used with any array of data
class YourNamespace_YourModule_Model_History_Collection extends Varien_Data_Collection{
//array of stdClass objects. Can be array of any objects/subarrays
protected $_history = array();
public function loadData($printQuery = false, $logQuery = false){
if ($this->isLoaded()) {
return $this;
}
$this->_totalRecords = count($this->_history);
$this->_setIsLoaded();
// paginate and add items
$from = ($this->getCurPage() - 1) * $this->getPageSize();
$to = $from + $this->getPageSize() ;
$isPaginated = $this->getPageSize() > 0;
$cnt = 0;
$sub_history = array_slice($this->_history, $from, $to-$from);
foreach ( $sub_history as $entry) {
$cnt++;
$item = new $this->_itemObjectClass();
//build here your Varien Object
$item->setDate($entry->Date);
//.......
$this->addItem($item);
if (!$item->hasId()) {
$item->setId($cnt);
}
}
return $this;
}
public function setRecords($records){
if(is_array($records)){
$this->_history = $records;
}
return;
}
}
//Your block class
class YourNamespace_YourModule_Block_History extends Mage_Core_Block_Template{
public function __construct(){
$collection_prepared = new YourNamespace_YourModule_Model_History_Collection();
//get your data here
//$records is array of stdClass objects
$collection_prepared->setRecords($records);
$this->setCollection($collection_prepared);
}
protected function _prepareLayout(){
parent::_prepareLayout();
$pager = $this->getLayout()->createBlock('page/html_pager', 'history.pager');
$pager->setAvailableLimit(array(20=>20,40=>40,80=>80,'all'=>'all'));
$pager->setCollection($this->getCollection());
$this->setChild('pager', $pager);
$this->getCollection()->load();
return $this;
}
public function getPagerHtml()
{
return $this->getChildHtml('pager');
}
}
Hope this helps!
class Pageable_Varien_Data_Collection extends Varien_Data_Collection
{
/**
* Load data
*
* #param bool $printQuery
* #param bool $logQuery
*
* #return Pageable_Varien_Data_Collection
*/
public function load($printQuery = false, $logQuery = false)
{
if ($this->isLoaded()) {
return $this;
}
$this->_renderLimit();
$this->_setIsLoaded();
return $this;
}
/**
* #return Pageable_Varien_Data_Collection
*/
protected function _renderLimit()
{
if ($this->_pageSize) {
$currentPage = $this->getCurPage();
$pageSize = $this->_pageSize;
$firstItem = (($currentPage - 1) * $pageSize + 1);
$lastItem = $firstItem + $pageSize;
$iterator = 1;
foreach ($this->getItems() as $key => $item) {
$pos = $iterator;
$iterator++;
if ($pos >= $firstItem && $pos <= $lastItem) {
continue;
}
$this->removeItemByKey($key);
}
}
return $this;
}
/**
* Retrieve collection all items count
*
* #return int
*/
public function getSize()
{
if (is_null($this->_totalRecords)) {
$this->_totalRecords = count($this->getItems());
}
return intval($this->_totalRecords);
}
/**
* Retrieve collection items
*
* #return array
*/
public function getItems()
{
return $this->_items;
}
}

CodeIgniter and friendly urls

I currently have a set of urls which are derived from their controller names but they're not very url friendly.
For example, is there anyway I change:
example.com/admin/news_manager/add_article
to
example.com/admin/news-manager/add-article
Any help would be greatly appreciated! Thank you.
If its just that one function you can open applications/config/routes.php and add a line something like this:
$route['^admin/news-manager/add-article'] = $route['admin/news_manager/add_article'];
depending on what your other urls are you could come up with a more generic rule.
Here's what I have, put this in your application/core folder as MY_Router.php
It will convert all of the -'s in your url to _'s.
<?php
class MY_Router extends CI_Router {
function set_class($class)
{
$this->class = $this->replace_underscores($class);
}
function set_method($method)
{
$this->method = $this->replace_underscores($method);
}
private function replace_underscores($string){
return str_replace('-', '_', $string);
}
function _validate_request($segments)
{
$this->segments = $segments;
if(empty($this->segments) || $this->controller_is_in_root_folder())
return $this->segments;
if ($this->controller_is_in_a_subfolder())
{
$this->segments = $this->set_directory_and_remove_from_array();
if ($this->at_least_one_segment() > 0 && !$this->default_controller_is_in_subfolder())
{
show_404($this->fetch_directory().$this->segments[0]);
}
else
{
$this->set_class($this->default_controller);
$this->set_method('index');
if (!$this->default_controller_is_in_subfolder())
{
$this->directory = '';
return array();
}
}
return $this->segments;
}
else
show_404($this->segments[0]);
}
private function at_least_one_segment(){
return count($this->segments) >= 1;
}
private function controller_is_in_a_subfolder(){
return is_dir(APPPATH.'controllers/'.$this->segments[0]);
}
private function controller_is_in_root_folder(){
return file_exists(APPPATH.'controllers/'.str_replace('-', '_', $this->segments[0]).EXT);
}
private function set_directory_and_remove_from_array(){
$this->set_directory($this->segments[0]);
return array_slice($this->segments, 1);
}
private function default_controller_is_in_subfolder(){
return file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $this->segments[0]).EXT);
}
}
You can use URI routing. See the URI routing page in the CodeIgniter docs.

Resources