[fix] __autoload for api from alban + fixing variables & bad preg
This commit is contained in:
		
							parent
							
								
									5eb6ae6d47
								
							
						
					
					
						commit
						3058ed4cbe
					
				|  | @ -21,7 +21,6 @@ $root=ALTERNC_PANEL."/"; | ||||||
| 
 | 
 | ||||||
| require_once($root."class/db_mysql.php"); | require_once($root."class/db_mysql.php"); | ||||||
| require_once($root."class/functions.php"); | require_once($root."class/functions.php"); | ||||||
| require_once($root."class/variables.php"); |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| global $L_MYSQL_HOST,$L_MYSQL_DATABASE,$L_MYSQL_LOGIN,$L_MYSQL_PWD; | global $L_MYSQL_HOST,$L_MYSQL_DATABASE,$L_MYSQL_LOGIN,$L_MYSQL_PWD; | ||||||
|  | @ -71,6 +70,7 @@ closedir($c); | ||||||
| /* Language */ | /* Language */ | ||||||
| //include_once("../../class/lang_env.php");
 | //include_once("../../class/lang_env.php");
 | ||||||
| 
 | 
 | ||||||
|  | $variables=new m_variables(); | ||||||
| $mem=new m_mem(); | $mem=new m_mem(); | ||||||
| $err=new m_err(); | $err=new m_err(); | ||||||
| $authip=new m_authip(); | $authip=new m_authip(); | ||||||
|  |  | ||||||
|  | @ -30,7 +30,72 @@ define("API_CALL_POST",      2 ); | ||||||
| define("API_CALL_POST_REST", 3 ); | define("API_CALL_POST_REST", 3 ); | ||||||
| define("API_CALL_GET_REST",  4 ); | define("API_CALL_GET_REST",  4 ); | ||||||
| 
 | 
 | ||||||
| // TODO : __autoload of classes ? 
 | /** | ||||||
|  |  * Attempts to load a class in multiple path, the PSR-0 or old style way | ||||||
|  |  *  | ||||||
|  |  * @staticvar array $srcPathList | ||||||
|  |  * @staticvar boolean $init | ||||||
|  |  * @param string $class_name | ||||||
|  |  * @return boolean | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | function __autoload($class_name) | ||||||
|  | { | ||||||
|  |     // Contains (Namespace) => directory
 | ||||||
|  |     static $srcPathList                 = array(); | ||||||
|  |     static $init=null; | ||||||
|  | 
 | ||||||
|  |     // Attempts to set include path and directories once
 | ||||||
|  |     if( is_null( $init )){ | ||||||
|  | 
 | ||||||
|  |         // Sets init flag
 | ||||||
|  |         $init                           = true; | ||||||
|  |          | ||||||
|  |         // Sets a contextual directory
 | ||||||
|  |         $srcPathList["standard"]        = "/usr/share/php"; | ||||||
|  | 
 | ||||||
|  |         // Updates include_path according to this list
 | ||||||
|  |         $includePathList                = explode(PATH_SEPARATOR, get_include_path());  | ||||||
|  | 
 | ||||||
|  |         foreach($srcPathList as $path){ | ||||||
|  |             if ( !in_array($path, $includePathList)){ | ||||||
|  |                 $includePathList[]      = $path; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         // Reverses the path for search efficiency
 | ||||||
|  |         $finalIncludePathList           = array_reverse($includePathList); | ||||||
|  |          | ||||||
|  |         // Sets the updated include_path
 | ||||||
|  |         set_include_path(implode(PATH_SEPARATOR, $finalIncludePathList)); | ||||||
|  | 	 | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     // Accepts old Foo_Bar namespacing
 | ||||||
|  |     if(preg_match("/_/", $class_name)){ | ||||||
|  |         $file_name                      = str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php'; | ||||||
|  |          | ||||||
|  |     // Accepts 5.3 Foo\Bar PSR-0 namespacing 
 | ||||||
|  |     } else if(preg_match("/\\/", $class_name)){ | ||||||
|  |         $file_name                      = str_replace('\\', DIRECTORY_SEPARATOR, ltrim($class_name,'\\')) . '.php'; | ||||||
|  |          | ||||||
|  |     // Accepts non namespaced classes
 | ||||||
|  |     } else { | ||||||
|  |         $file_name                      = $class_name . '.php';         | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // Attempts to find file in namespace
 | ||||||
|  |     foreach($srcPathList as $namespace => $path ){ | ||||||
|  |         $file_path                      = $path.DIRECTORY_SEPARATOR.$file_name; | ||||||
|  |         if(is_file($file_path) && is_readable($file_path)){ | ||||||
|  |             require $file_path; | ||||||
|  |             return true; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     // Failed to find file
 | ||||||
|  |     return false; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| function apicall($data,$token,$mode) { | function apicall($data,$token,$mode) { | ||||||
|   global $dbh; |   global $dbh; | ||||||
|  | @ -85,7 +150,7 @@ function apiauth($data,$mode) { | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| // Authentication 
 | // Authentication 
 | ||||||
| if (preg_match("#^/api/auth/([^/]*)/?#$",$_SERVER["REQUEST_URI"],$mat)) { | if (preg_match("#^/api/auth/([^/\?]*)[/\?]?#",$_SERVER["REQUEST_URI"],$mat)) { | ||||||
|   if ($_SERVER["REQUEST_METHOD"]=="POST") { |   if ($_SERVER["REQUEST_METHOD"]=="POST") { | ||||||
|     $data=array("options" => $_POST, |     $data=array("options" => $_POST, | ||||||
| 		"method" => $mat[1]); | 		"method" => $mat[1]); | ||||||
|  | @ -121,7 +186,7 @@ if ($_SERVER["REQUEST_URI"]=="/api/post") { | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| if (preg_match("#^/api/rest/([^/]*)/([^/]*)/?#$",$_SERVER["REQUEST_URI"],$mat)) { | if (preg_match("#^/api/rest/([^/]*)/([^/\?]*)[/\?]?$#",$_SERVER["REQUEST_URI"],$mat)) { | ||||||
|   if ($_SERVER["REQUEST_METHOD"]=="POST") { |   if ($_SERVER["REQUEST_METHOD"]=="POST") { | ||||||
|     $data=array("options" => $_POST,  |     $data=array("options" => $_POST,  | ||||||
| 		"object" => $mat[1], | 		"object" => $mat[1], | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	 Benjamin Sonntag
						Benjamin Sonntag