Modifying new code to support PHP5.3 - php

I'm using Roots.io wordpress starter theme. It's coded for PHP 5.5 but the server I'm posting the site on is running PHP 5.3
I need to change this code to be supported by the PHP on the server, but don't know how.
function asset_path($filename) {
$dist_path = get_template_directory_uri() . DIST_DIR;
$directory = dirname($filename) . '/';
$file = basename($filename);
static $manifest;
if (empty($manifest)) {
$manifest_path = get_template_directory() . DIST_DIR . 'assets.json';
$manifest = new JsonManifest($manifest_path);
}
if (WP_ENV !== 'development' && array_key_exists($file, $manifest->get())) {
return $dist_path . $directory . $manifest->get()[$file];
} else {
return $dist_path . $directory . $file;
}
}
The issue is in this line:
return $dist_path . $directory . $manifest->get()[$file];
The [$file] is confusing PHP I think but no idea how to modify this. Any tips? If more code is needed please let me know.

You'll need to split that return as requesting an index from a method call I think started getting supported in 5.4.
Try splitting it out.
$val = $manifest->get();
return $dist_path . $directory . $val[$file];
For reference this is know as array dereferencing. You can find more information about it here.

Related

Getting error on server due to anonymous functions issue in PHP 5.2

I'm using "google-api-php-client" library which is working fine on local system but it's giving following error on server as it's version is 5.2!
syntax error, unexpected T_FUNCTION, expecting ')'
So I have two questions here, if we can fix this error by doing some changes in code to make it work with this function? Below is the code of autoload.php
spl_autoload_register(
function ($className) {
$classPath = explode('_', $className);
if ($classPath[0] != 'Google') {
return;
}
// Drop 'Google', and maximum class file path depth in this project is 3.
$classPath = array_slice($classPath, 1, 2);
$filePath = dirname(__FILE__) . '/' . implode('/', $classPath) . '.php';
if (file_exists($filePath)) {
require_once($filePath);
}
}
);
but I'm not sure how to change the above to solve this issue and also is there any library which can run on php version 5.2? As if I use this, it might be possible that it start giving error on some other functionality. Thanks!
It seems your php version not knows about anonymous functions or closures. Try to use named one:
function autoloadGoogleApi($className) {
$classPath = explode('_', $className);
if ($classPath[0] != 'Google') {
return;
}
// Drop 'Google', and maximum class file path depth in this project is 3.
$classPath = array_slice($classPath, 1, 2);
$filePath = dirname(__FILE__) . '/' . implode('/', $classPath) . '.php';
if (file_exists($filePath)) {
require_once($filePath);
}
}
spl_autoload_register('autoloadGoogleApi');
Still, I'm also want to point out, that php version you specifying is very old, so I'm suggesting to really consider option of upgrading.
UPD: 3v4l test

File exists issues

I'm attempting to try and debug the following code with the file_exists function. I've ran a var_dump on the avatar directory and it always returns as bool(false). I'm not sure why. I tested the code below and it gets to the file exists but it proves the if statement false everytime. Any thoughts? I have looked and the image is in the directory correctly.
$default_avatar = 'default.jpg';
$avatar_directory = base_url() . 'assets/globals/images/avatars/';
if (!is_null($user_data->avatar))
{
$avatar = $avatar_directory . $user_data->avatar;
if (file_exists($avatar))
{
$user_data->avatar = $avatar_directory . $user_data->avatar;
}
else
{
$user_data->avatar = $avatar_directory . $default_avatar;
}
}
else
{
$user_data->avatar = $default_avatar;
}
$default_avatar = 'default.jpg';
$avatar_directory = 'assets/globals/images/avatars/';
if (!is_null($user_data->avatar))
{
$avatar = $avatar_directory . $user_data->avatar;
if (file_exists(FCPATH . $avatar))
{
$user_data->avatar = base_url() . $avatar_directory . $user_data->avatar;
}
else
{
$user_data->avatar = base_url() . $avatar_directory . $default_avatar;
}
}
else
{
$user_data->avatar = $default_avatar;
}
from the name base_url seems like a function that will get a url like http://www.mysite.com, which will not work for doing local directory functions.
you need something like getcwd, or a full path
getcwd will get the current working directory (the directory where the initial script was executed from):
//If say script.php was exectued from /home/mysite/www
$avatar_directory = getcwd() . '/assets/globals/images/avatars/';
//$avatar_directory would be
/home/mysite/www/assets/globals/images/avatars/
Well this works both CLI and via Apache etc...:
$avatar_directory = substr(str_replace(pathinfo(__FILE__, PATHINFO_BASENAME), '', __FILE__), 0, -1) . '/assets/globals/images/avatars/'
The did returned is the one that the php file itself is in, not the root.
assuming you meant base_url() to point to the root of your project -
$file = __DIR__ . "/path/to/file.ext";
if (file_exists($file)) {
//...
}
Or some variation thereof. This also works:
__DIR__ . "/.."
it resolves to the parent directory of __DIR__.
see PHP's magic constants:
http://php.net/manual/en/language.constants.predefined.php
If you are looking for a remote resource - a file not located on your local filesystem - you have to change your php.ini to permit that. And it's probably not a good idea, this is not usually considered safe or secure. At all.
http://php.net/manual/en/features.remote-files.php
And note:
"This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir."
-- from http://php.net/manual/en/function.file-exists.php
-- edited to add relevant information based on a comment from OP.

Why isn't wordnik's php api loading the required classes?

I'm getting the following error with wordnik's php api
Fatal error: Class 'Example' not found in Swagger.php on line 212
I've edited Swagger.php to get it to work, below is the original function
function swagger_autoloader($className) {
$currentDir = substr(__FILE__, 0, strrpos(__FILE__, '/'));
if (file_exists($currentDir . '/' . $className . '.php')) {
include $currentDir . '/' . $className . '.php';
} elseif (file_exists($currentDir . '/models/' . $className . '.php')) {
include $currentDir . '/models/' . $className . '.php';
}
}
The working code I changed to is
function swagger_autoloader($className)
{
include $currentDir . '/models/' . $className . '.php';
}
MORE INFO
My file structure is as follows.
WORDNIK (contains start.php)>>>WORDNIK (contains Swagger.php)>>MODELS (contains Example.php)
I'm using wampserver 2.2 with php 5.4.3
UPDATE/EDIT
Using the following code
function swagger_autoloader($className) {
echo "dirname(__FILE__)= ",dirname(__FILE__);
$currentDir = substr(__FILE__, 0, strrpos(__FILE__, '/'));
echo "currentDir=".$currentDir."</br>";
echo "className=".$className."</br>";
echo "__FILE__=",__FILE__."<br/>";
die();
I get the results
dirname(__FILE__)=D:\wamp\www\wordnik\wordnik
currentDir=
className=Example
__FILE__=D:\wamp\www\wordnik\wordnik\Swagger.php
As suggested by vcampitelli, using either __DIR__ or dirname(__FILE__) works.
MY QUESTION
Why doesn't the original function work?
UNIMPORTANT INFO
For people struggling through the examples, here is my start.php file
<?php
require('./wordnik/Swagger.php');
require('./wordnik/WordApi.php');
require('./wordnik/AccountApi.php');
require('./wordnik/WordsApi.php');
require('./wordnik/WordListApi.php');
require('./wordnik/WordListsApi.php');
$myAPIKey = 'replace_this_with_your_real_api';
$client = new APIClient($myAPIKey, 'http://api.wordnik.com/v4');
$wordApi = new WordApi($client);
$example = $wordApi->getTopExample('irony');
print $example->text;
?>
What does $currentDir return? Have you tried using __DIR__ or dirname(__FILE__) (they are the same) instead of that substr?
If you are using the second example just as you posted (without declaring $currentDir), so that is the problem at your original code: $currentDir is not returning the right folder!
With the original code, which file is being included? Because, actually, I think no one is! Use echo inside those if statements to check that!
The original function doesn't work because it is looking for the position of a UNIX-style forward slash '/', whereas you are on Windows and have backslashes '\'. That's a bug! I'll fix the lib to use dirname(FILE) as you do. Thanks for pointing out the error.

Zend Framework PHP -> realpath()

im trying to create a Zend Framework custom Provider and here:
if($module == 'default'){
$modelsPath = dirname(__FILE__) . '/../application/models';
$filePath = realpath($modelsPath) . '/' . $name . '.php';
} else
if(!$module == ''){
$modelsPath = dirname(__FILE__) . '/../application/' . $module . '/models';
$filePath = realpath($modelsPath) . '/' . $name . '.php';
} else {
die('Please enter a module!');
}
when i im trying to create a path to the file and when its default module everything is ok, but when for ex. module is other word no matter what the realpath returns false?! where is the error?
try using the APPLICATION_PATH constant.
if($module == 'default'){
$modelsPath = APPLICATION_PATH . '/models';
$filePath = realpath($modelsPath) . '/' . $name . '.php';
} else
if(!$module == ''){
$modelsPath = APPLICATION_PATH . '/' . $module . '/models';
$filePath = realpath($modelsPath) . '/' . $name . '.php';
} else {
die('Please enter a module!');
}
Taken out of the php manual:
realpath() returns FALSE on failure, e.g. if the file does not exist.
Note:
The running script must have executable permissions on all directories in the hierarchy, otherwise realpath() will return FALSE.
So the problem most probably is with folder permission or the path simply is wrong!
Double check that the paths exist by simple echoing $filePath. If this checks out to be okay then ensure that the 'user' (apache, www-data, for example) have enough privileges. If you are not sure who your server is operating as you can simply debug by echoing whoami

Change file contents

We have a script, /scripts/ourscript.php and a file, /texts/elvis.txt.
How can we change contents of this file, when we run ourscript.php?
Use file_put_contents() method to set the contents of a file.
If you need just to save new data, you can do:
$elvis = 'Contents here';
$fileName = '..' . PATH_SEPARATOR . 'texts' . PATH_SEPARATOR . 'elvis.txt';
if (file_put_contents($fileName, $elvis) === false)
{
// Handle error here.
}
If, instead of saving data, you need to change existing data, do:
$fileName = '..' . PATH_SEPARATOR . 'texts' . PATH_SEPARATOR . 'elvis.txt';
$elvis = file_get_contents($fileName);
// Do changes to $elvis here.
if (file_put_contents($fileName, $elvis) === false)
{
// Handle error here.
}
Finally, if you need to append something new to existing contents, use:
$elvis = PHP_EOL . 'Contents to append to existing stuff here';
$fileName = '..' . PATH_SEPARATOR . 'texts' . PATH_SEPARATOR . 'elvis.txt';
// Noticed FILE_APPEND as third argument?
if (file_put_contents($fileName, $elvis, FILE_APPEND) === false)
{
// Handle error here.
}
While MainMa has given you a direct answer, I'll point you to:
http://php.net/manual/en/function.file.php
Since it seems that you might have more of these questions, which could have been easily answered by looking at the documentation.
Also by figuring things out with the help of the documentation you'll learn how to solve such problems on your own, you know independence is a nice thing to have :)

Categories