CakePHP - Miles Uploader Plugin - Isset Error - cakephp

I have tried several CakePHP file uploaders, but have not been successful' in setting up and making it work. I've settled down with Miles Uploader Plugin and I am doing everything I can to make it work. Installation instructions are excellent and I have followed them to the letter, but I still cant get it to work.
When I post an Article I get the following error:
Warning (2): Illegal offset type in isset or empty [APP/plugins/uploader/controllers/components/uploader.php, line 1104]
}
if (isset($this->_data[$file])) {
I am making the following call in my add articles view
...
echo $form->input('main_image', array('type' => 'file'));
...
And I am using the following in my add action but it is returning false
...
if (!empty($this->data)) {
//This call is not successful which skips to the }else{ display the message below
if ($data = $this->Uploader->upload('main_image')) {
// Upload successful, do whatever
}else{
$this->setFlash('Uploader Error',true);
}
}
...
Can anyone please shed some light on what could possibly be wrong.
** EDIT **
I am not able to try this out right now, but do I need to do the following to make it work:
if ($data = $this->Uploader->upload(data['Article']['main_image'])) {
Thanks,

Spent many hours trying to figure out why it does not work. Finally I gave up last night trying and settled with trying to configure CakePHP's Media Plugin. That now works great. Setup was a breeze.

Related

CakePhp 4.x basic Authentication

I am following the CakePHP 4.x tutorial to the letter (as far as I can see) until chapter "CMS Tutorial - Authentication".
Half way through "Now, on every request, the AuthenticationMiddleware will inspect the request session to look for an authenticated user. If we are loading the /users/login page, it will also inspect the posted form data (if any) to extract the credentials."
When I try to access articles or users I get an error:
( ! ) Fatal error: Interface
'Authentication\AuthenticationServiceProviderInterface' not found in
C:\wamp64\www\cake\src\Application.php on line 41
I have tried to figure out why this would be, but I cannot find it. I have tried looking up the same problem on the internet, no dice. Not even a mention that this could be security related (I found a mention about strict brower settings earlier but it was related to another problem).
I have uploaded my code on Github here: https://github.com/plafeber/cakephp-tutorial
I would greatly appreciate any feedback. I was under the assumption that if I create the full code set from the tutorial, given of course I run CakePHP 4.1.5 and follow the related Cake 4.x manual, that it would work. However, I already found out that I have to change the line about the use of DefaultPasswordHasher compared to what was in the code. So I can imagine the Tutorial page is not exactly as it should be.
This would be hte correct line about the use of the DefaultPasswordHasher in User.php;
//the use line
use Cake\Auth\DefaultPasswordHasher as AuthDefaultPasswordHasher;
//and the function
protected function _setPassword(string $password) : ?string
{
if (strlen($password) > 0) {
$hasher = new AuthDefaultPasswordHasher();
return $hasher->hash($password);
}
}
The solution to this was to navigate to the Cake install dir (containing the src and config folder and so on), then running the Composer call again. This apparently placed the filed in the right directories and then the error no longer appeared.

Using DomPDF in Cakephp Delivers an error Cannot redeclare class Dompdf\View\PdfView

I have a problem printing a PDF using CakePHP and DomPDF as soon I want to fetch some data from the Database before printing the pdf. Without fetching the Data, it works like a charme. My function is the following:
public function tourpdf($tourid = null){
$contain = ['Deliveries','Deliveries.Articletransactions','Deliveries.Orders','Deliveries.Orders.Customers',
'Deliveries.Articletransactions.Orderarticles','Deliveries.Articletransactions.Orderarticles.Articles'];
$tour = $this->Tours->get($tourid)
->contain($contain);
$this->viewBuilder()
->className('Dompdf.Pdf')
->layout('Dompdf.default')
->options(['config' => [
'filename' => $filename,
'render' => 'browser',
'size' => 'A4',
'orientation' => 'landscape'
]]);
$this->set('Test', 'Hallo');
$this->set('Tour',$tour);
}
As soon as I set the Data from the last line, that I fetched above, I get the error Message "Fatal error: Cannot redeclare class Dompdf\View\PdfView in .../src/View/PdfView.php on line 66".
However commenting the last line out, the first set with Test is working and the PDF is generated. How can I set data retrieving from a query to the pdf file?
I got a similar error, but with Zend Framework 2. All of a sudden DOMPDF would give an error "Cannot redeclare (previously declared in ...)".
I had PHP functions in some templates and when I removed them I was getting another error "No block-level parent found. Not good.". I noticed this started happening when I upgraded libxml2 from 2.9.4 to 2.9.5 or later.
The solution to both of the problems was to instantiate DOMPDF class like this:
use Dompdf\Options;
$options = new Options();
$options->set('enable_html5_parser', true);
$dompdf = new Dompdf($options);
With enable_html5_parser all the problems went away. Here is some more information on this issue.
Amazing!
Had this issue on Mac 10.12.6
PHP Version 5.6.30
libxml Version 2.9.4
Using the regex to remove whitespace between the tags fixed the issue.
$html = preg_replace('/>\s+</', '><', $html);

CakePHP Cakemenu plugin fails after global error due to incorrect string encoding

I am using CakePHP 2.1.2 with PHP 5.3.5 and a plugin called 'Cakemenu' which normally works fine. The plugin stores menus in a db table with the menu link stored as text like
array('plugin'=>null,'controller'=>'assets','action'=>'index');
The helper in the plugin gets those values, then executes this code to convert that text to an array:
//Try to evaluate the link (if starts with array)
if (eregi('^array', $value['Menu']['link'])) {
$code = "\$parse = " . $value['Menu']['link'] . ";";
$result = eval($code);
if (is_array($parse)) {
$value['Menu']['link'] = $parse;
}
}
Everything works fine unless CakePHP is handling an error. For example if I mistype the name of a controller in the browser I should get a menu and then the missing controller message. Instead I get a page full "Parse error: syntax error, unexpected $end in..." messages pointing to the line with the eval statement. If I printout the variable that is getting eval'ed I see that it has been (incorrectly) encoded with Html entities when it normally does not.
Good string to be eval'ed:
$parse = array('plugin'=>null,'controller'=>'assets','action'=>'index');
Bad string to be eval'ed:
$parse = array('plugin'=>null,'controller'=>'Parts','action'=>'add');
To temporarily fix the problem I added two statements to just replace the offending characters
$value['Menu']['link'] = str_replace( ''','\'',$value['Menu']['link']);
$value['Menu']['link'] = str_replace( '>','>',$value['Menu']['link']);
and everything works great again. Some other pieces of information that might be helpful is that the array of data used to generate the menu is read during the beforeFilter of the app and saved in a view variable and then the menu is generated as an element in the view.
I'm thinking that the error causes CakePHP (or PHP) to skip some loading or configuration process and that causes the string to be mishandled. Any help would be appreciated, thanks
Your beforeFilter() method won't be executed on error pages. You'll have to handle your errors yourself and manually call beforeFilter(). I wrote a blog post on how to use custom error pages - pay close attention to the Controller Callbacks section.

CakePHP redirecting on error

I'm receiving a strange error from my call to query() the database. This normally wouldn't be a problem, except that it seems that the query call is redirecting me to the 500 server error page and not executing the rest of my code.
Regardless of the error, this PHP is executed during an AJAX call, meaning that I'm unable to return a properly formatted json string containing the error because CakePHP is redirecting.
A workaround would be to just parse the big HTML response from the server if there is an error, and use javascript to figure out what went wrong and display it properly, but I would much rather return a little json string from the backend that plays nicely with the code I already have in place.
Here is my call:
$this->FileUpload->query('exec sproc_runjob_ProcessTests', false);
Any help would be much appreciated. Thanks in advance!
As bfavaretto mentions above, the correct answer is to surround the query line in a try catch.
Here is the code I used:
try{
$this->dbParent->FileUpload->query('exec sproc_runjob_ProcessTests', false);
} catch(PDOException $e){
if($e['errorInfo'][0] == 42000 && $e['errorInfo'][1] == 22022)
return array('error' => 'sproc_blocked');
}
return array('success'=>true);

Integrating a file browser into ckeditor & CakePHP

recently found an article in http://hashtwo.com/blog/integrating-a-file-browser-into-ckeditor-cakephp to integrate a file browser into ckeditor (file manager of core five labs )
I followed the steps but i get an error when I click the browse server button,
Missing Controller:
Error: CkeditorController could not be found.
I had the same error at some point during my setup, but I don't seem to recall what was it and how I fixed it. Let me know if my next solution does not fix your problem and I will investigate a little more:
First I followed the following tutorial which worked well, but missed the Authentication part: http://labs.corefive.com/projects/filemanager/
Then I used the tutorial you referenced, except for the last section of code provided. Instead I added the following code directly to my view page: admin_add.ctp
<?php echo $form->textarea('body', array(/*'class'=>'ckeditor', */'id' => 'body', 'class' => 'body')) ?>
<?php
include_once 'ckeditor/ckeditor.php' ;
//require_once 'ckfinder/ckfinder.php' ;
//$initialValue = 'Default value goes here' ;
$ckeditor = new CKEditor() ;
$ckeditor->basePath = '/ckeditor/' ;
$ckeditor->config['filebrowserBrowseUrl'] = '/ckeditor/filemanager/index.html';
$ckeditor->config['filebrowserUploadUrl'] = '/ckeditor/filemanager/connectors/php/filemanager.php';
$ckeditor->config['filebrowserImageBrowseUrl'] = '/ckeditor/filemanager/index.html?type=Images';
$ckeditor->config['filebrowserImageUploadUrl'] = '/ckeditor/filemanager/connectors/php/filemanager.php?command=QuickUpload&type;=Images';
$ckeditor->config['filebrowserWindowWidth'] = '800';
//CKFinder::SetupCKEditor( $ckeditor,'/ckfinder/' ) ;
// This will replace CakePHP textarea listed above.
$ckeditor->replace('body');
?>
Also, for the Auth Section of his tutorial I changed it to match my requirements:
function auth() {
session_name("CAKEPHP");
session_start();
if(isset($_SESSION['Auth']['User']) )
{
//Since CKEditor for me is only used in the admin section
//I make sure only admins can access it, group_id=1
if($_SESSION['Auth']['User']['group_id'] == 1)
{
return true;
}
}
return false;
}
If it tries to find a controller this means it can not access the editors file or folder and instead the dispatcher of CakePHP gets the request and tries to load a fckeditor controller.
This means you'll need to place the editor in the right directory (for example app/webroot/fckedtor/) or use the correct url to access it.

Resources