mirror of
https://github.com/EGroupware/egroupware.git
synced 2024-11-07 16:44:07 +01:00
235 lines
7.5 KiB
PHP
235 lines
7.5 KiB
PHP
|
#!/usr/bin/env php -qC
|
||
|
<?php
|
||
|
/**
|
||
|
* EGroupware - check namespace usage in converted code
|
||
|
*
|
||
|
* @link http://www.egroupware.org
|
||
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
||
|
* @author Ralf Becker <rb@stylite.de>
|
||
|
* @copyright 2016 by Ralf Becker <rb@stylite.de>
|
||
|
* @version $Id$
|
||
|
*/
|
||
|
|
||
|
if (php_sapi_name() !== 'cli') // security precaution: forbit calling as web-page
|
||
|
{
|
||
|
die('<h1>fix_api.php must NOT be called as web-page --> exiting !!!</h1>');
|
||
|
}
|
||
|
|
||
|
// raw replacements
|
||
|
$replace = array(
|
||
|
"#use EGroupware\\Api;#" => '', // remove evtl. use EGroupware\Api, as we add it again below
|
||
|
"#<\?php\n/\*\*.*\*/#msU" => "\$0\n\nuse EGroupware\\Api;",
|
||
|
'#egw_framework::csp_connect_src_attrs\(#' => "Api\\Header\\ContentSecurityPolicy::add('connect-src', ",
|
||
|
'#egw_framework::csp_frame_src_attrs\(#' => "Api\\Header\\ContentSecurityPolicy::add('frame-src', ",
|
||
|
'#egw_framework::csp_script_src_attrs\(#' => "Api\\Header\\ContentSecurityPolicy::add('script-src', ",
|
||
|
'#egw_framework::csp_style_src_attrs\(#' => "Api\\Header\\ContentSecurityPolicy::add('style-src', ",
|
||
|
"#egw_session::appsession\(([^,]+),\s*('[^']+')\)#" => 'Api\\Cache::getSession($2, $1)',
|
||
|
"#egw_session::appsession\(([^,]+),\s*('[^']+'),\s*#" => 'Api\\Cache::setSession($2, $1, ',
|
||
|
"#\\\$GLOBALS\['egw'\]->session->appsession\(([^,]+),\s*('[^']+')\)#" => 'Api\\Cache::getSession($2, $1)',
|
||
|
"#\\\$GLOBALS\['egw'\]->session->appsession\(([^,]+),\s*('[^']+'),\s*#" => 'Api\\Cache::setSession($2, $1, ',
|
||
|
"#\\\$GLOBALS\['egw'\]->common->#" => 'common::',
|
||
|
);
|
||
|
// enclose class-names and static methods with some syntax check
|
||
|
$class_start = '#([\[\s,;(])';
|
||
|
$class_end = '(::|\\(|;|\?|:|\\s|$)#';
|
||
|
foreach(array(
|
||
|
'accounts' => 'Api\\Accounts',
|
||
|
//'acl' => 'Api\Acl',
|
||
|
//'applications' => 'Api\Applications',
|
||
|
//'asyncservice' => 'Api\AsyncService',
|
||
|
'auth' => 'Api\\Auth',
|
||
|
'categories' => 'Api\\Categories',
|
||
|
'config::get_customfields' => 'Api\\Storage\\Customfields::get',
|
||
|
'config' => 'Api\\Config',
|
||
|
'common::image' => 'Api\\Image::find',
|
||
|
'common::egw_exit' => 'exit',
|
||
|
//'common' list single methods like
|
||
|
//'country' => 'Api\\Country',
|
||
|
//'egw' =>
|
||
|
'egw_cache' => 'Api\\Cache',
|
||
|
'egw_ckeditor_config' => 'Api\\Html\\CkEditorConfig',
|
||
|
'egw_customfields' => 'Api\\Storage\\Customfields',
|
||
|
'egw_db' => 'Api\\Db',
|
||
|
'egw_digest_auth' => 'Api\\Header\\Authentication',
|
||
|
'egw_exception' => 'Api\\Exception',
|
||
|
'egw_exception_no_permission' => 'Api\\Exception\\NoPermission',
|
||
|
'egw_exception_no_permission_app' => 'Api\\Exception\\NoPermission\\App',
|
||
|
'egw_exception_no_permission_admin' => 'Api\\Exception\\NoPermission\\Admin',
|
||
|
'egw_exception_no_permission_record' => 'Api\\Exception\\NoPermission\\Record',
|
||
|
'egw_exception_not_found' => 'Api\\Exception\\NotFound',
|
||
|
'egw_exception_assertion_failed' => 'Api\\Exception\\AssertionFailed',
|
||
|
'egw_exception_wrong_parameter' => 'Api\\Exception\\WrongParameter',
|
||
|
'egw_exception_wrong_userinput' => 'Api\\Exception\\WrongUserinput',
|
||
|
'egw_exception_db' => 'Api\\Db\\Exception',
|
||
|
'egw_exception_db_invalid_sql' => 'Api\\Db\\Exception\\InvalidSql',
|
||
|
'egw_exception_redirect' => 'Api\\Exception\\Redirect',
|
||
|
//'egw_favorites' =>
|
||
|
//'egw_framework' =>
|
||
|
//'egw_ical_iterator' =>
|
||
|
'egw_json_request' => 'Api\\Json\\Request',
|
||
|
'egw_json_response' => 'Api\\Json\\Response',
|
||
|
'egw_link' => 'Api\\Link',
|
||
|
//'egw_mailer' =>
|
||
|
'egw_session' => 'Api\\Session',
|
||
|
//'egw_tail' =>
|
||
|
'egw_time' => 'Api\\DateTime',
|
||
|
'egw_vfs' => 'Api\\Vfs',
|
||
|
//'groupdav*' =>
|
||
|
'historylog' => 'Api\\Storage\\History',
|
||
|
//'hooks' =>
|
||
|
'html::\$user_agent' => 'Api\\Header\\UserAgent::type()',
|
||
|
'html::\$ua_version' => 'Api\\Header\\UserAgent::version()',
|
||
|
'html::\$ua_mobile' => 'Api\\Header\\UserAgent::mobile()',
|
||
|
'html::safe_content_header' => 'Api\\Header\\Content::safe',
|
||
|
'html::content_header' => 'Api\\Header\\Content::type',
|
||
|
'html::content_disposition_header' => 'Api\\Header\\Content::disposition',
|
||
|
'html' => 'Api\\Html',
|
||
|
'iface_stream_wrapper' => 'Api\\Vfs\\StreamWrapperIface',
|
||
|
'mime_magic' => 'Api\\MimeMagic',
|
||
|
'preferences' => 'Api\\Preferences',
|
||
|
'solink' => 'Api\\Link\\Storage',
|
||
|
'sqlfs_stream_wrapper' => 'Api\\Vfs\\Sqlfs\\StreamWrapper',
|
||
|
'sqlfs_utils' => 'Api\\Vfs\\Sqlfs\\Utils',
|
||
|
// todo translation mail specific stuff
|
||
|
'translation' => 'Api\\Translation',
|
||
|
// etemplate2
|
||
|
'etemplate_new' => 'Api\\Etemplate',
|
||
|
'etemplate_widget' => 'Api\\Etemplate\\Widget',
|
||
|
'etemplate_widget_entry' => 'Api\\Etemplate\\Widget\\Entry',
|
||
|
'etemplate_widget_tree' => 'Api\\Etemplate\\Widget\\Tree',
|
||
|
'etemplate_widget_select' => 'Api\\Etemplate\\Widget\\Select',
|
||
|
'etemplate_widget_link' => 'Api\\Etemplate\\Widget\\Link',
|
||
|
'etemplate_widget_nextmatch' => 'Api\\Etemplate\\Widget\\Nextmatch',
|
||
|
'etemplate_widget_taglist' => 'Api\\Etemplate\\Widget\\Taglist',
|
||
|
'etemplate_widget_file' => 'Api\\Etemplate\\Widget\\File',
|
||
|
'etemplate_widget_vfs' => 'Api\\Etemplate\\Widget\\Vfs',
|
||
|
'etemplate_requst' => 'Api\\Etemplate\\Request',
|
||
|
'nextmatch_widget::category_action' => 'Api\\Etemplate\\Widget\\Nextmatch::category_action',
|
||
|
// so_sql and friends
|
||
|
'so_sql' => 'Api\\Storage\\Base',
|
||
|
'so_sql_cf' => 'Api\\Storage',
|
||
|
'so_sql2' => 'Api\\Storage\\Base2',
|
||
|
'bo_tracking' => 'Api\\Storage\\Tracking',
|
||
|
//'bo_merge' =>
|
||
|
// addressbook backend
|
||
|
'addressbook_bo' => 'Api\\Contacts',
|
||
|
'addressbook_so' => 'Api\\Contacts\\Storage',
|
||
|
) as $from => $to)
|
||
|
{
|
||
|
$replace[$class_start.$from.$class_end] = '$1'.$to.'$2';
|
||
|
}
|
||
|
//print_r($replace);
|
||
|
|
||
|
/**
|
||
|
* Check namespace usage in converted code
|
||
|
*
|
||
|
* @param string $file filename
|
||
|
* @return boolean false on error
|
||
|
*/
|
||
|
function fix_api($file)
|
||
|
{
|
||
|
global $prog, $replace;
|
||
|
if (basename($file) == $prog) return true; // dont fix ourself ;-)
|
||
|
|
||
|
if (($content = file_get_contents($file)) === false) return false;
|
||
|
|
||
|
if (!preg_match("|<\?php\n/\*\*.*\*/|msU", $content))
|
||
|
{
|
||
|
error_log("No file-level phpDoc block found in $file to add 'use EGroupware\\Api;'\n");
|
||
|
return;
|
||
|
}
|
||
|
$content = preg_replace(array_keys($replace), array_values($replace), $content);
|
||
|
|
||
|
// shorten some classes, if used, with further use declarations
|
||
|
foreach(array('Api\\Etemplate', 'Api\\Vfs') as $namespace)
|
||
|
{
|
||
|
if (strpos($content, $namespace) !== false && strpos($content, 'use EGroupware\\'.$namespace) === false)
|
||
|
{
|
||
|
$content = strtr($content, array(
|
||
|
$namespace => str_replace('Api\\', '', $namespace),
|
||
|
"use EGroupware\\Api;" => "use EGroupware\\Api;\nuse EGroupware\\$namespace;"
|
||
|
));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
die($content);
|
||
|
|
||
|
//print_r($use);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Loop recursive through directory and call fix_api for each php file
|
||
|
*
|
||
|
* @param string $dir
|
||
|
* @return boolean false on error
|
||
|
*/
|
||
|
function fix_api_recursive($dir)
|
||
|
{
|
||
|
if (!is_dir($dir)) return false;
|
||
|
|
||
|
foreach(scandir($dir) as $file)
|
||
|
{
|
||
|
if ($file == '.' || $file == '..') continue;
|
||
|
|
||
|
if (is_dir($dir.'/'.$file))
|
||
|
{
|
||
|
fix_api_recursive($dir.'/'.$file);
|
||
|
}
|
||
|
elseif(substr($file,-4) == '.php')
|
||
|
{
|
||
|
echo "\r".str_repeat(' ',100)."\r".$dir.'/'.$file.': ';
|
||
|
fix_api($dir.'/'.$file);
|
||
|
}
|
||
|
}
|
||
|
echo "\r".str_repeat(' ',100)."\r";
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Give usage
|
||
|
*
|
||
|
* @param string $error =null
|
||
|
*/
|
||
|
function usage($error=null)
|
||
|
{
|
||
|
global $prog;
|
||
|
echo "Usage: $prog [-h|--help] file or dir\n\n";
|
||
|
if ($error) echo $error."\n\n";
|
||
|
exit($error ? 1 : 0);
|
||
|
}
|
||
|
|
||
|
$args = $_SERVER['argv'];
|
||
|
$prog = basename(array_shift($args));
|
||
|
|
||
|
if (!$args) usage();
|
||
|
|
||
|
$replace_file = false;
|
||
|
while(($arg = array_shift($args)))
|
||
|
{
|
||
|
switch($arg)
|
||
|
{
|
||
|
case '-h':
|
||
|
case '--help':
|
||
|
usage();
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
if ($args) // not last argument
|
||
|
{
|
||
|
usage("Unknown argument '$arg'!");
|
||
|
}
|
||
|
break 2;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!file_exists($arg)) usage("Error: $arg not found!");
|
||
|
|
||
|
if (!is_dir($arg))
|
||
|
{
|
||
|
fix_api($arg,$replace_file);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
fix_api_recursive($arg,$replace_file);
|
||
|
}
|