JApplicationCli Cronjob - joomla

I have the following script:
// Initialize Joomla framework
const _JEXEC = 1;
// Load system defines
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}
// Get the framework.
require_once JPATH_LIBRARIES . '/import.legacy.php';
// Bootstrap the CMS libraries.
require_once JPATH_LIBRARIES . '/cms.php';
/**
* Cron job to trash expired cache data.
*
* #since 2.5
*/
class DoCron extends JApplicationCli
{
public function doExecute()
{
echo 'ok3';
$this->out('Fetching updates...');
}
}
JApplicationCli::getInstance('DoCron')->execute();
I have added this in CPanel with a Cronjob and get the results of excecution by e-mail.
Now I hoped for an e-mail with 'ok3' or 'Fetching updates...' but none of that all. I do get an e-mail but it is an reference to php excecution.
When I add an 'echo ok' tag right before:
JApplicationCli::getInstance('DoCron')->execute();
I get that 'ok' as a result in the e-mail.
Any thoughts on what goes wrong here? The script is based on general scripts coming with joomla 3.6.5. Those scripts also give no result.

I had the same problem. It turns out that the default php binary for most hosts is the PHP CGI. Running under PHP CGI results in no stdout, stderr and stdin which is why you are not seeing the correct output.
Instead, check your CPanel documentation and look for the CLI version of PHP. It is most likely called php-cli. For example, I had to run a cron job for a Joomla CLI app and found the following to work:
/usr/bin/php-cli /path/to/joomla/cli/my-cli -a b -c d --verbose

Related

Code coverage for laravel dusk

Is there any way to get code coverage when running Laravel Dusk?
I know it runs browser tests so it's not scrutinizing code, but is there a way to add a listener to check what code is covered? I did not see anything on this subject now.
Conceptually, you need to bootstrap all your requests w/ PHP Unit's code coverage tools.
You can do this with phpunit libraries directly, or via xdebug's coverage tools (which use phpunit).
From this sample gist that I found, you can start coverage tools based on a couple of _GET parameters passed via the Dusk test.
public function testBasicExample()
{
$this->browse(function (Browser $browser) {
$browser->visit(route('test', [
'test_name' => 'testBasicExample',
'coverage_dir' => '/app/Http'
]))->assertSee('test');
});
}
The code that does the work is two parts
1. Start collecting based on parameters:
$test_name = $_GET['test_name'];
require __DIR__ . '/../vendor/autoload.php';
$current_dir = __DIR__;
$coverage = new SebastianBergmann\CodeCoverage\CodeCoverage;
$filter = $coverage->filter();
$filter->addDirectoryToWhitelist(
$current_dir . '/..' . ((isset($_GET['coverage_dir']) && $_GET['coverage_dir'])
? $_GET['coverage_dir']
: '/app')
);
$coverage->start($test_name);
And 2 end collecting and output:
function end_coverage()
{
global $test_name;
global $coverage;
global $filter;
global $current_dir;
$coverageName = $current_dir . '/coverages/coverage-' . $test_name . '-' . microtime(true);
try {
$coverage->stop();
$writer = new \SebastianBergmann\CodeCoverage\Report\Html\Facade;
$writer->process($coverage, $current_dir . '/../public/report/' . $test_name);
$writer = new SebastianBergmann\CodeCoverage\Report\PHP();
} catch (Exception $ex) {
file_put_contents($coverageName . '.ex', $ex);
}
}
The end collection is called using a clever little trick where the class coverage_dumper has just a destructor, which gets called automatically when php ends the process.
The code itself can use a bit of tidy up as far as output paths, and variables go, but from a concept, it should work.
Dusk is using Browsers to run tests and the Browser can't see the PHP code that's being executed.
The only way that I see to achieve code coverage with Dusk is to create a option in php artisan serve that would be possible to count and create the coverage file.

cron scheduler in laravel 4

I am trying to set cron for a command or controller action but it seemed to be not working for me. Please see below what I have tried
I have been trying to set up scheduler as per your instructions with no result.
When I try:
1. /usr/local/bin/php /home/mysite/public_html/protected/app/start/artisan cron:run it gives error
Could not open input file: /home/mysite/public_html/protected/app/start/artisan
2. /usr/local/bin/php /home/mysite/public_html/protected/app/controllers/TestController.php
it gives error Fatal error: Class 'BaseController' not found in
3. /usr/local/bin/php -q /home/mysite/public_html/protected/app/start/artisan cron:run
Error-Could not open input file:
4. php /var/www/com/mysite.com/artisan cron:run
Status: 404 Not Found No input file specified.
/usr/local/bin/php home/opendesk/public_html/protected/app/start/artisan.php and in artisan.php I do like Artisan::add(new CronRunCommand);
Error Fatal error: Class 'Artisan' not found
/usr/local/bin/php /home/opendesk/public_html/protected/app/start/artisan.php
when in artisan.php I change it to $artisan->add(new CronRunCommand);
Error Fatal error: Call to a member function add() on a non-object
None of it seems to work. I have been read many SO and google posts but cant find a solution to this. hoping to get some help here
At first. Task Scheduling not available in laravel 4, you need use newest version.
At second. What a path /home/mysite/public_html/protected/app/start/artisan? Can you show application folder structure?
However artisan its file in application root folder in default installation. For sample - my application placed in \var\www\myaplication then artisan placed in \var\www\myaplication\artisan. And i call this php \var\www\myaplication\artisan or better, change current dir to cd \var\www\myaplication and run php artisan.
At trith artisan cron can not prepare controller method. You need create a cron task in file App\Console\Kernel
<?php
namespace App\Console;
use DB;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
\App\Console\Commands\Inspire::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
DB::table('recent_users')->delete();
})->daily();
}
}
For more details read documenation this very useful!

How to correctly upload Laravel 5 application to server?

I am new to Laravel 5 and have realized that a lot has changed, I am more familiar with Laravel 4. I just tried uploading my site to a live VPS, I managed to change the URLs in index.php and server.php but I keep getting these errors:
Which makes me believe there was something else I was supposed to change that I did not because these files are indeed there and my application works just fine on my localhost.
Also with the exception of my home page, the rest of the pages say not found when I click on their links.
This is my document structure:
index.php
<?php
require __DIR__.'/../*********/bootstrap/autoload.php';
$app = require_once __DIR__.'/../*********/bootstrap/app.php';
$kernel = $app->make('Illuminate\Contracts\Http\Kernel');
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
server.php
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylorotwell#gmail.com>
*/
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' and file_exists(__DIR__.'/../public_html'.$uri))
{
return false;
}
require_once __DIR__.'/../public_html/index.php';
Following step you have must followed to move local to live server in Laravel.
Move all public folder data into root directory
In index.php change path to
require DIR.'/../bootstrap/autoload.php'; to require DIR.'/bootstrap/autoload.php';
AND
$app = require_once DIR.'/../bootstrap/app.php'; to $app = require_once DIR.'/bootstrap/app.php';
and set file permission to 744
In .htaccess file add following code after RewriteEngine On.
RewriteBase /
Change parameter in .env file.

How to disable TFA (Two factor authentication)

I enabled TFA in Joomla 3.2 and it worked fine, but my smartphone is unaccessible.
Then I cannot go in backend and I tried to disable the plugin plg_twofactorauth_totp in database but it stay enabled.
Disabling by rename the folder hide Secret Key input, but I wasn't able to login.
Go to your MySQL database for joomla, go to the users table. Clear the value of otpKey. you should be able to login without a key now.
https://gist.github.com/medigeek/28a047be0d0d527a95769130a6faf559
This code will disable two-factor auth plugins and clear keys for Joomla! Super Users.
This script disables Joomla!'s two factor authentication plugin and clears the otpKey and otep values for Super Users. It allows you to login when you aren't able to use Google authenticator for any reason.
Usage:
Place it in the Joomla! 3.x root dir (where configuration.php and index.php are) and run it. Then login and leave the security key field empty.
Warning: Use with caution. Backup before use!
Snapshot of the code
<?php
/* This script disables Joomla!'s two factor authentication
* plugin and clears the otpKey and otep values for Super
* Users. It allows you to login when you aren't able to
* use Google authenticator for any reason.
* Usage:
* Place it in the Joomla! 3.x root dir (where configuration.php
* and index.php are) and run it. Then login and leave the
* security key field empty.
* Warning: Use with caution. Backup before use.
*/
define('_JEXEC', 1);
define('JPATH_BASE', __DIR__);
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Load system defines
if (file_exists(JPATH_BASE . '/defines.php')) { require_once JPATH_BASE . '/defines.php'; }
if (!defined('_JDEFINES')) { require_once JPATH_BASE . '/includes/defines.php'; }
require_once JPATH_LIBRARIES . '/import.legacy.php'; // Get the framework.
require_once JPATH_LIBRARIES . '/cms.php'; // Bootstrap the CMS libraries.
class Reset2FA extends JApplicationCli
{
public function execute()
{
$this->out('Initialising');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query2 = $db->getQuery(true);
//get users by group: (array of integers)
$sadminids = JAccess::getUsersByGroup(8); // 8 = Super Users
$strsadminids = implode(',', $sadminids);
$this->out(sprintf('Super User IDs: %s', $strsadminids));
$this->out('Disabling twofactorauth plugin (totp and yubikey)');
// Fields to update.
$fields = array(sprintf('%s = 0', $db->quoteName('enabled')));
// Conditions for which records should be updated.
// plg_twofactorauth_totp
// plg_twofactorauth_yubikey
$conditions = array(sprintf('%s LIKE %s', $db->quoteName('name'), $db->quote('plg_twofactorauth_%')));
$query->update($db->quoteName('#__extensions'))->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->execute();
$this->out('Disabling/clearing otpKey and otep for all Super Users');
// UPDATE 2
$fields2 = array(
$db->quoteName('otpKey') . " = ''",
$db->quoteName('otep') . " = ''",
);
// Conditions for which records should be updated.
// otpKey
// otep
$conditions2 = array(
$db->quoteName('otpKey') . " != ''",
$db->quoteName('otep') . " != ''",
sprintf('%s IN (%s)', $db->quoteName('id'), $strsadminids)
);
$query2->update($db->quoteName('#__users'))->set($fields2)->where($conditions2);
$db->setQuery($query2);
$result2 = $db->execute();
$this->out('Done');
}
}
JApplicationCli::getInstance('Reset2FA')->execute();
?>

Symfony 1.4: Override doctrine:build-schema command

Is it possible to override a Symfony 1.4 CLI command?
More specifically, I was wondering if it's possible to override this command:
php symfony doctrine:build-schema
What I want to do is to add a new option in the database.yml file for each connection I find in it.
The option I want to add is a package option
So, an hypothetical connection could be:
all:
doctrine:
class: sfDoctrineDatabase
package: myPackageOption
param:
dsn: 'mysql:host=localhost;dbname=my_db_name'
username: db_user
password: db_password
If it would be possible, where can i find the code to override?
I suggest you to use some shell script that pre-generate the databses.yml and then auto-invoque the php symfony doctrine:build-schema. Something like:
build.sh, in project root folder:
#!/bin/bash
cp config/databases_1.yml config/databases.yml
php symfony doctrine:build
then, type ./build.sh (after added execution permissions) in your console.
The copy/replace of multiple databases_xxx.yml it's the easiest example. But you can do any processing you want.
If you don't know about shell scripting, you can do the file modification even with a php script, so your build.sh should looks like:
#!/bin/bash
php pregenerate_databases.php
php symfony doctrine:build
I'm trying to override the task but I can't make it work, but:
You can create your own task that inherits the doctrine task and do your stuff:
in lib/task add sfDoctrineBuildSchemaCustomTask.class.php:
class sfDoctrineBuildSchemaCustomTask extends sfDoctrineBuildSchemaTask
{
/**
* #see sfTask
*/
protected function configure()
{
$this->addOptions(array(
new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true),
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
));
$this->namespace = 'doctrine';
$this->name = 'build-schema-custom';
$this->briefDescription = 'Creates a schema from an existing database';
$this->detailedDescription = <<<EOF
The [doctrine:build-schema|INFO] task introspects a database to create a schema:
[./symfony doctrine:build-schema|INFO]
The task creates a yml file in [config/doctrine|COMMENT]
EOF;
}
/**
* #see sfTask
*/
protected function execute($arguments = array(), $options = array())
{
// do your stuff before original call
parent::execute($arguments,$options);
// do your stuff after original call
}
}
Then, you can call php symfony doctrine:build-schema-custom, and go!
Or, maybe, you can edit the original task located in lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineBuildSchemaTask.class.php

Resources