require_once in kohana3.3 showing the Server error - php

In kohana 3.3 i need to include one of the controller file in the another controller
I used require_once( realpath(dirname(FILE) . '/Twitteroauth.php' ) );
Its showing the server error.
Thanks in advance.

You should do this:
require_once( realpath(dirname(__FILE__) . '/Twitteroauth.php' ) );

Function realpath returns false when file does not exist, so you should maybe check result before require:
$path = realpath(dirname(__FILE__) . '/Twitteroauth.php' );
if(!empty($path))
require_once($path);
else
die("$path not found!");

Related

Joomla 3.x set User to a specific group with PHP

I just want to set a user to a specific group. I have tried many things but not helped. One solutiuon is worked for me, but I think it is deprecated because in the Joomla's admin page I see other groups.
My code:
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$user =JFactory::getUser(joe); //joe is the username
$GroupJoomla = array('14'=>14); //I want to change it's group to 14
$user->set('groups',$GroupJoomla); //Method one, which I have found on documentations
print_r ( $user->get('groups')); //Succesfully sets, BUT if I check on the Joomla's admin page, the user is still on other group. Maybe this is a deprecated function and the Joomla didnt use it now.
$user->groups = $GroupJoomla;
$user->save(); //Method two. Unfortunatelly this is always returns to false for me, dont know why. There is no error message. (Apache error log also empty)
print_r ($user->getAuthorisedGroups()); //This is the good to get the user's groups. This is correct.
if ($user->save() === false)
{
throw new Exception($user->getError(), 500); // This gives me this error: Error displaying the error page: Application Instantiation Error: Application Instantiation Error
}
What am I doing wrong? :/
I havent got Joomfish, so it isnt why the $user->save() returns false.
Update:
This code is also returns false on $user->save().
$uid = 1748;
$user =JFactory::getUser($uid); //joe is the username
$GroupJoomla = array(14); //I want to change it's group to 14
$user->set('groups',$GroupJoomla);
$user->groups = $GroupJoomla;
$user->save();
jimport( 'joomla.access.access' );//Call the Access Class
/*If the below code is not there it wont save.*/
if ($user->save() === false){ //This is gives me false somehow
throw new Exception($user->getError(), 500);
}
$groups = JAccess::getGroupsByUser($uid, false);
print_r($groups); exit; //Receives Group id 15 (Not 14)
With both code samples I have got the good user object. $user->get('username'); This gives me the good username. (Joomla 3.6.5)
As #itoctopus said you need to enter id instead of username but also there are some other mistakes that needs to be taken care of like initiating the group variable. It should be
$GroupJoomla = array(14);
if more than one group
$GroupJoomla = array(13,14);
then to get the groups you can use
$groups = JAccess::getGroupsByUser($id, false);
So finally your code will be
<?php
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', 'C:\xampp\htdocs\joomla' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$uid = 404;
$user =JFactory::getUser($uid); //joe is the username
$GroupJoomla = array(4,6); //I want to change it's group to 14
$user->set('groups',$GroupJoomla);
$user->groups = $GroupJoomla;
$user->save();
jimport( 'joomla.access.access' );//Call the Access Class
/*If the below code is not there it wont save.*/
if ($user->save() === false){
throw new Exception($user->getError(), 500);
}
$groups = JAccess::getGroupsByUser($uid, false);
print_r($groups); exit;
A quick glimpse at your code, and I think this line:
$user =JFactory::getUser(joe);
Should be:
$user =JFactory::getUser(5);
Where 5 is the id of the user with username "joe". You cannot get a user by username by using the function JFactory::getUser.
Alternatively, if you really want to get a user by username, then you should use:
$userId = JUserHelper::getUserId('joe'); //note that there are quotes around joe (in your code you omitted the quote)
$user =JFactory::getUser($userId);
Hope this helps.

After wordpress upgrade I'm getting wp_user not found in pluggable.php

I am calling a php script from an app that checks the credentials of a wp user. This worked fine until I upgraded wordpress from 4.3 to 4.4 (and 4.5). It's really time I got this sorted but I can't think why wp-user is no longer available as it is in the include list.
error: wp_user not found in pluggable.php
Please see the code below..
define( 'SHORTINIT', TRUE );
require_once $abspath . '/wp-load.php';
require_once $abspath . '/wp-includes/user.php';
require_once $abspath . '/wp-includes/pluggable.php';
require_once $abspath . '/wp-includes/formatting.php';
require_once $abspath . '/wp-includes/capabilities.php';
require_once $abspath . '/wp-includes/kses.php';
require_once $abspath . '/wp-includes/meta.php';
require_once $abspath . '/wp-includes/l10n.php';
require_once $abspath . '/wp-includes/class-wp-error.php';
require_once $abspath . '/wp-includes/general-template.php';
require_once $abspath . '/wp-includes/link-template.php';
$the_authenticate = wp_authenticate_username_password('null',$user_name,$user_password);
if( is_wp_error( $the_authenticate ) ) {
echo '{"error":"The username was not recognised"}';
}
else
{
$the_user_authenticate_id = $the_authenticate->ID;
$the_user = get_user_by('login', $user_name);
$the_user_id = $the_user->ID;
if ( !$the_user )
{
//echo "{'error':'The username was not recognised'}";
}
I have found the solution is that I now need to include the following..
require_once $abspath . '/wp-includes/class-wp-roles.php';
require_once $abspath . '/wp-includes/class-wp-user.php';
require_once $abspath . '/wp-includes/class-wp-role.php';
Please can anyone explain why these extra includes are suddenly necessary in wordpress 4.4 and whether my solution makes sense?
Download the updated wordpress version from wordpress.org and replace your current wp-admin and wp-includes folders with updated wordpress folders.
Hope it will work.

PHP - JPath Moving Multiple files to correct directory

See my last question as this links to it: PHP - Moving multiple files with different files names to own directory
So i decided to using the Joomla API however, the documentation was for 1.5 and 2.5 system only but i'm using 3.0. I have a number of files that look like this:
"2005532-JoePharnel.pdf"
and
"1205121-HarryCollins.pdf"
Basically I want to create a PHP code that when someone ftp uploads those files to the upload folder that it will 1) Create a directory if it doesn't exist using there name 2) Move the files to the correct directory (E.g. JoePharnel to the JoePharnel Directory ignoring the number at the beginning)
Updated: 23/10/14 - 14:05:
My new code creates the folder but won't move the file in the upload into that new folder, code is below:
<?php
define( '_JEXEC', 1);
define('JPATH', dirname(__FILE__) );
if (!defined('DS')){
define( 'DS', DIRECTORY_SEPARATOR );
$parts = explode( DS, JPATH );
$script_root = implode( DS, $parts ) ;
// check path
$x = array_search ( 'administrator', $parts );
if (!$x) exit;
$path = '';
for ($i=0; $i < $x; $i++){
$path = $path.$parts[$i].DS;
}
// remove last DS
$path = substr($path, 0, -1);
if (!defined('JPATH_BASE')){
define('JPATH_BASE', $path );
}
if (!defined('JPATH_SITE')){
define('JPATH_SITE', $path );
}
/* Required Files */
require_once ( JPATH_SITE . DS . 'includes' . DS . 'defines.php' );
require_once ( JPATH_SITE . DS . 'includes' . DS . 'framework.php' );
require_once ( JPATH_SITE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
//Import filesystem libraries. Perhaps not necessary, but does not hurt
jimport('joomla.filesystem.path');
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.user.user');
//First we set up parameters
$searchpath = JPATH_BASE . DS . "upload";
//Then we create the subfolder called png
if ( !JFolder::create($searchpath . DS ."Images") ) {
//Throw error message and stop script
}
//Now we read all png files and put them in an array.
$png_files = JFolder::files($searchpath,'.png');
//Now we need some stuff from the JFile:: class to move all files into the new folder
foreach ($png_files as $file) {
JFile::move($searchpath. DS . ".png" . $file, $searchpath . DS. "Images" . $file);
}
//Lastly, we are moving the complete subdir to the root of the component.
if (JFolder::move($searchpath . DS. "Images",JPATH_COMPONENT) ) {
//Redirect with perhaps a happy message
} else {
//Throw an error
}
}
?>
Only error i get is Notice: Use of undefined constant JPATH_COMPONENT - assumed 'JPATH_COMPONENT' in /upload.php on line 70. But doesn't stop it working, am so close on this any help is greatly appreciated. I want to know where its taking the image, i think i have worked out the "DS" now.
Thanks

PHP - Class 'HbaseClient' not found even though it is included

Been scratching my head for a while on this one. I'm just getting started with using PHP/Thrift to communicate with HBase (I can do it fine w/Python). For some reason the code below is generating class 'HbaseClient' not found on the $client = new line:
$GLOBALS['THRIFT_ROOT'] = 'thrift';
require_once( $GLOBALS['THRIFT_ROOT'] . '/Thrift.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/transport/TSocket.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/transport/TBufferedTransport.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/protocol/TBinaryProtocol.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/Hbase/Hbase.php' );
try
{
$socket = new TSocket('127.0.0.1', 9090);
$transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocolAccelerated($transport);
$client = new HbaseClient( $protocol );
$transport->open();
}
catch (Exception $e)
{
echo "Exception: %e\r\n";
}
I have literally no idea why. In the Hbase.php include file the client is defined as such:
class HbaseClient implements \Hbase\HbaseIf {
Am I missing something glaringly simple here? (Full HBase.php here: http://pastebin.com/6kd9r2Se )
Thanks in advance!
I believe this is a namespace issue. Try putting :
namespace Hbase;
in the file instantiating the object or use the fully qualified name:
$client = new Hbase\HbaseClient( $protocol );
Are you sure you have placed the HBase folder under THRIFT_ROOT directory or outside?

Define absolute path for files

I have the following include files..
require_once ("config.php");
require_once ("functions.php");
require_once ("session.php");
I want to define absolute paths for my include files. I have tried with the following code and no luck..
can you please help to define an appropriate absolute path, so that require_once as expected.
defined('DS') ? null : define('DS',DIRECTORY_SEPARATOR);
defined('SITE_ROOT') ? null: define('SITE_ROOT',DS.'PHP_Files'. DS . 'phpsandbox'. DS.'my_mat'. DS.'my_test');
defined('LIB_PATH')?null:define('LIB_PATH',SITE_ROOT.DS.'includes');
require_once(LIB_PATH.DS."config.php");
require_once(LIB_PATH.DS."functions.php");
require_once(LIB_PATH.DS."session.php");
These 3 include files in my system are stored in J:\PHP_Files\phpsandbox\my_mat\my_test\includes.
Thanks in advance!
Ok, so I think what you are looking for is the actual system file path. To get that you can echo
dirname( __FILE__ );
You can do this in any file that you want and it will display the system file path relative to your file. For me it's something like this:
/home2/myusername/public_html/project_name/includes/config.php
so if you are interested in the "project_name" folder you should have something like this:
defined("SITE_ROOT") ? null : define("SITE_ROOT", DS . "home2" . DS . "myusername" . DS . "public_html" . DS . "project_name" );
Then if you are looking for the "includes" folder which will be your library you should have something like this:
defined("LIB_PATH") ? null : define("LIB_PATH", SITE_ROOT . DS . "includes" );
Hope this helps. I had the exact same problem and this worked for me.
Cheers, Mihai Popa
Try including your LIB_PATH in your include path.
set_include_path(LIB_PATH . DS . PATH_SEPARATOR . get_include_path());
require_once(dirname(__FILE__).DS."config.php");
require_once(dirname(__FILE__).DS."functions.php");
require_once(dirname(__FILE__).DS."session.php");
check it out , i think it's good
You need realpath function. Also, you can get all files included by get_included_files that returns array of absolute paths of files you've included at the moment function's got called.
defined('DS') or define('DS',DIRECTORY_SEPARATOR);
$disk_label = '';
if (PHP_OS == 'WINNT') {
if (FALSE !== ($pos = strpos(__FILE__, ':'))) {
$disk_label = substr(__FILE__, 0,$pos+1);
}
}
defined('SITE_ROOT') or define('SITE_ROOT', $disk_label.DS.'PHP_Files'. DS . 'phpsandbox'. DS.'my_mat'. DS.'my_test');
defined('LIB_PATH') or define('LIB_PATH',SITE_ROOT.DS.'includes');
require_once(LIB_PATH.DS."config.php");
require_once(LIB_PATH.DS."functions.php");
require_once(LIB_PATH.DS."session.php");

Categories