Changeset 6165 for sandbox/webservice/api
- Timestamp:
- 05/11/12 10:38:02 (12 years ago)
- Location:
- sandbox/webservice/api
- Files:
-
- 3 added
- 9 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
sandbox/webservice/api/adapters/ExpressoAdapter.php
r6149 r6165 100 100 if($this->getResult()) 101 101 $body['result'] = $this->getResult(); 102 else 103 throw new ResponseException("",Errors::E_UNKNOWN_ERROR); 102 else { 103 Errors::runException("E_UNKNOWN_ERROR"); 104 } 105 104 106 105 107 $response->body = json_encode($body); … … 115 117 } 116 118 else{ 117 throw new ResponseException("",Errors::LOGIN_AUTH_INVALID);119 Errors::runException("LOGIN_AUTH_INVALID"); 118 120 } 119 121 } 120 122 else{ 121 throw new ResponseException("",Errors::LOGIN_NOT_LOGGED_IN);123 Errors::runException("LOGIN_NOT_LOGGED_IN"); 122 124 } 123 125 } -
sandbox/webservice/api/library/utils/Errors.php
r6150 r6165 3 3 final class Errors { 4 4 5 // Errors from -32000 to -32099 => Server error :Reserved for implementation-defined server-errors. 6 const E_PARSE_ERROR = "-32700"; 7 const E_INVALID_REQUEST = "-32600"; 8 const E_METHOD_NOT_FOUND = "-32601"; 9 const E_INVALID_PARAMS = "-32602"; 10 const E_INTERNAL_ERROR = "-32603"; 11 12 const E_HTTP_NOT_FOUND = "404"; 13 const E_UNKNOWN_ERROR = "500"; 5 private static $sInstance; 6 private $errors; 14 7 15 16 // Errors inherited from Expresso Application. 17 const LOGIN_SUCCESS_LOGOUT = "1"; 18 const LOGIN_SESSION_EXPIRED = "2"; 19 const LOGIN_NOT_LOGGED_IN = "3"; 20 const LOGIN_COOKIES_REQUIRED = "4"; 21 const LOGIN_BADLOGIN = "5"; 22 const LOGIN_PASSWORD_EXPIRED = "6"; 23 const LOGIN_AUTH_INVALID = "7"; 24 const LOGIN_SESSION_NOT_VERIFIED = "10"; 25 const LOGIN_ACCOUNT_EXPIRED = "98"; 26 const LOGIN_LOGIN_BLOCKED = "99"; 27 const LOGIN_INVALID_LOGIN = "200"; 28 29 30 const CATALOG_MIN_ARGUMENT_SEARCH = "1001"; 31 const MAIL_NOT_SENT = "1002"; 32 const MAIL_TRASH_NOT_CLEANED = "1003"; 33 const MAIL_MESSAGE_NOT_FOUND = "1004"; 34 35 static private $reservedErrors = array ( 36 self::E_PARSE_ERROR => "Parse error", 37 self::E_INVALID_REQUEST => "Invalid Request", 38 self::E_METHOD_NOT_FOUND => "Method not found", 39 self::E_INVALID_PARAMS => "Invalid params", 40 self::E_INTERNAL_ERROR => "Internal error", 41 self::E_HTTP_NOT_FOUND => 'A resource matching URI "%1" was not found', 42 self::E_UNKNOWN_ERROR => "HTTP Unknown error" 43 ); 44 45 46 // Errors inherited from Expresso Application. 47 static private $applicationErrors = array( 48 49 self::LOGIN_SUCCESS_LOGOUT => "You have been successfully logged out", 50 self::LOGIN_SESSION_EXPIRED => "Sorry, your login has expired", 51 self::LOGIN_NOT_LOGGED_IN => "You are not logged in", 52 self::LOGIN_COOKIES_REQUIRED => "Cookies are required to login to this site.", 53 self::LOGIN_BADLOGIN => "bad login or password", 54 self::LOGIN_PASSWORD_EXPIRED => "Your password has expired, and you do not have access to change it", 55 self::LOGIN_AUTH_INVALID => "Your auth is invalid", 56 self::LOGIN_SESSION_NOT_VERIFIED => "Your session could not be verified.", 57 self::LOGIN_ACCOUNT_EXPIRED => "Account is expired", 58 self::LOGIN_LOGIN_BLOCKED => "Blocked, too many attempts!", 59 self::LOGIN_INVALID_LOGIN => "Bad login or password", 60 self::CATALOG_MIN_ARGUMENT_SEARCH => "Your search argument must be longer than %1 characters.", 61 self::MAIL_NOT_SENT => "Your mail could not be sent.", 62 self::MAIL_TRASH_NOT_CLEANED => "Your trash folder could not be cleaned.", 63 self::MAIL_MESSAGE_NOT_FOUND => "Message not found in folder %1." 64 ); 65 66 static public function set($code, $message){ 67 return array ("code" => $code, "message" => $message); 68 } 69 70 static public function get($code, $argument = false){ 71 $errors = self::$reservedErrors + self::$applicationErrors; 72 $message = $errors[$code]; 73 if(!$message) 74 $message = $errors[self::E_UNKNOWN_ERROR]; 75 if($argument) 76 $message = str_replace("%1", $argument, $message); 77 78 return array ("code" => $code, "message" => $message); 8 public static function getInstance(){ 9 if (!self::$sInstance) { 10 self::$sInstance = new Errors(); 11 } 12 return self::$sInstance; 79 13 } 80 14 81 static public function getResponse($request, $responseException){82 $response = new Response($request);83 $response->code = Response::OK;84 $response->addHeader('content-type', 'application/json');85 if($request->id)86 $body['id'] = $request->id;87 88 $code = $responseException->getCode();89 $error = Errors::get($code);90 $message = ($responseException->getMessage() ? $responseException->getMessage() : $error['message']);91 $body['error'] = array("code" => $code, "message" => $message);92 $response->body = json_encode($body);93 return $response;15 function __construct(){ 16 $this->errors = array(); 17 if($handle = fopen(__DIR__."/../../config/Errors.tsv", "r")){ 18 while (!feof($handle)) { 19 $line = trim(fgets($handle,1024)); 20 if($line == null || $line[0] == "#") 21 continue; 22 $error = preg_split("/[\t]+/", $line); 23 if(is_array($error)&& count($error) == 3) { 24 $this->errors[] = array("code" => $error[0], "key" => $error[1], "message" => $error[2]); 25 } 26 } 27 } 94 28 } 29 30 static public function runException($needle, $argument = false){ 31 $error = false; 32 $type = (is_int($needle) ? "code" : "key"); 33 foreach( Errors::getInstance()->errors as $value ){ 34 if($value[$type] == $needle){ 35 $error = $value; 36 break; 37 } 38 } 39 40 if($error['message'] && $argument){ 41 $error['message'] = str_replace("%1", $argument, $error['message']); 42 } 43 if(!$error){ 44 Errors::getInstance()->runException("E_UNKNOWN_ERROR"); 45 } 46 47 throw new ResponseException($error['message'], $error['code']); 48 } 95 49 96 50 } -
sandbox/webservice/api/rest/catalog/ContactsResource.php
r6148 r6165 24 24 return $this->getGlobalContacts($search, $this->getParam('contactID')); 25 25 else{ 26 $error = Errors::get(Errors::CATALOG_MIN_ARGUMENT_SEARCH, $this-> getMinArgumentSearch()); 27 throw new ResponseException($error['message'], $error['code']); 26 Errors::runException("CATALOG_MIN_ARGUMENT_SEARCH", $this-> getMinArgumentSearch()); 28 27 } 29 28 } -
sandbox/webservice/api/rest/core/LoginResource.php
r6149 r6165 35 35 else 36 36 { 37 throw new ResponseException("",$GLOBALS['phpgw']->session->cd_reason);37 Errors::runException($GLOBALS['phpgw']->session->cd_reason); 38 38 } 39 39 return $this->getResponse(); -
sandbox/webservice/api/rest/dispatch.php
r6155 r6165 2 2 3 3 // load libraries 4 require_once __DIR__.'/../library/tonic/lib/tonic.php';5 require_once(__DIR__. "/../Errors.php");4 require_once(__DIR__.'/../library/tonic/lib/tonic.php'); 5 require_once(__DIR__.'/../library/utils/Errors.php'); 6 6 7 7 // load adapters … … 54 54 break; 55 55 default: 56 $response = Errors::getResponse($request, $e); 56 57 $response = new Response($request); 58 $response->code = Response::OK; 59 $response->addHeader('content-type', 'application/json'); 60 if($request->id) 61 $body['id'] = $request->id; 62 63 $body['error'] = array("code" => "".$e->getCode(), "message" => $e->getMessage()); 64 $response->body = json_encode($body); 57 65 } 58 66 } -
sandbox/webservice/api/rest/mail/AddFolderResource.php
r6155 r6165 13 13 14 14 if(empty($new_name) || preg_match('/[\/\\\!\@\#\$\%\&\*\(\)]/', $new_name)) 15 throw new ResponseException("",Errors::MAIL_INVALID_NEW_FOLDER_NAME);15 Errors::runException("MAIL_INVALID_NEW_FOLDER_NAME"); 16 16 17 17 $new_id = $parent_id . $this->getImap()->imap_delimiter . $new_name; … … 21 21 $result = $this->getImap()->create_mailbox($params); 22 22 if($result != 'Ok') 23 throw new ResponseException("",Errors::MAIL_FOLDER_NOT_ADDED);23 Errors::runException("MAIL_FOLDER_NOT_ADDED"); 24 24 } 25 25 -
sandbox/webservice/api/rest/mail/CleanTrashResource.php
r6148 r6165 10 10 $params['clean_folder'] = 'imapDefaultTrashFolder'; 11 11 if(!$this -> getImap() -> empty_folder($params)) 12 throw new ResponseException("",Errors::MAIL_TRASH_NOT_CLEANED);12 Errors::runException("MAIL_TRASH_NOT_CLEANED"); 13 13 } 14 14 -
sandbox/webservice/api/rest/mail/DelFolderResource.php
r6155 r6165 11 11 12 12 if(!$this->getImap()->folder_exists($folder_id)) 13 throw new ResponseException("",Errors::MAIL_INVALID_FOLDER);13 Errors::runException("MAIL_INVALID_FOLDER"); 14 14 15 15 $default_folders = array_keys($this->defaultFolders); 16 16 if(in_array($folder_id, $default_folders)) 17 throw new ResponseException("",Errors::MAIL_CANNOT_DEL_DEFAULT_FOLDER);17 Errors::runException("MAIL_CANNOT_DEL_DEFAULT_FOLDER"); 18 18 19 19 $personal_folders = $this->getImap()->get_folders_list(array('noSharedFolders' => true, 'folderType' => 'personal')); … … 23 23 foreach($personal_folders AS $personal_folder){ 24 24 if($personal_folder['folder_id'] == $folder_id && $personal_folder['folder_hasChildren']) 25 throw new ResponseException("",Errors::MAIL_FOLDER_NOT_EMPTY);25 Errors::runException("MAIL_FOLDER_NOT_EMPTY"); 26 26 } 27 27 28 28 if($this->getImap()->get_num_msgs(array('folder' => $folder_id)) > 0) 29 throw new ResponseException("",Errors::MAIL_FOLDER_NOT_EMPTY);29 Errors::runException("MAIL_FOLDER_NOT_EMPTY"); 30 30 31 31 $result = $this->getImap()->delete_mailbox($params); 32 32 if($result != 'Ok') 33 throw new ResponseException("",Errors::MAIL_FOLDER_NOT_DELETED);33 Errors::runException("MAIL_FOLDER_NOT_DELETED"); 34 34 } 35 35 -
sandbox/webservice/api/rest/mail/RenameFolderResource.php
r6155 r6165 12 12 13 13 if(!$this->getImap()->folder_exists($old_id)) 14 throw new ResponseException("",Errors::MAIL_INVALID_OLD_FOLDER);14 Errors::runException("MAIL_INVALID_OLD_FOLDER"); 15 15 16 16 $default_folders = array_keys($this->defaultFolders); 17 17 if(in_array($old_id, $default_folders)) 18 throw new ResponseException("",Errors::MAIL_INVALID_OLD_FOLDER);18 Errors::runException("MAIL_INVALID_OLD_FOLDER"); 19 19 20 20 if(empty($new_name) || preg_match('/[\/\\\!\@\#\$\%\&\*\(\)]/', $new_name)) 21 throw new ResponseException("",Errors::MAIL_INVALID_NEW_FOLDER_NAME);21 Errors::runException("MAIL_INVALID_NEW_FOLDER_NAME"); 22 22 23 23 $old_id_arr = explode($this->getImap()->imap_delimiter, $old_id); … … 30 30 $result = $this->getImap()->ren_mailbox($params); 31 31 if($result != 'Ok') 32 throw new ResponseException("",Errors::MAIL_FOLDER_NOT_RENAMED);32 Errors::runException("MAIL_FOLDER_NOT_RENAMED"); 33 33 } 34 34 -
sandbox/webservice/api/rest/mail/SendSupportFeedbackResource.php
r6148 r6165 29 29 30 30 if (!$returncode || !(is_array($returncode) && $returncode['success'] == true)) 31 throw new ResponseException("",Errors::MAIL_NOT_SENT);31 Errors::runException("MAIL_NOT_SENT"); 32 32 } 33 33
Note: See TracChangeset
for help on using the changeset viewer.