PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/api/includes/ringside/api/AbstractRest.php

https://github.com/jkinner/ringside
PHP | 415 lines | 191 code | 48 blank | 176 comment | 30 complexity | 734dc86dd03aadfe7908376e5870d7ff MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /*******************************************************************************
  3. * Ringside Networks, Harnessing the power of social networks.
  4. *
  5. * Copyright 2008 Ringside Networks, Inc., and individual contributors as indicated
  6. * by the @authors tag or express copyright attribution
  7. * statements applied by the authors. All third-party contributions are
  8. * distributed under license by Ringside Networks, Inc.
  9. *
  10. * This is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU Lesser General Public License as
  12. * published by the Free Software Foundation; either version 2.1 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This software is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this software; if not, write to the Free
  22. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  23. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  24. ******************************************************************************/
  25. require_once ('ringside/api/db/RingsideApiDbDatabase.php');
  26. require_once ('ringside/api/bo/App.php');
  27. require_once ('ringside/api/OpenFBAPIException.php');
  28. require_once( "ringside/api/ServiceFactory.php" );
  29. abstract class Api_AbstractRest
  30. {
  31. // Following need to move out to a SESSION wrapper.
  32. const SESSION_ID = 'session_id';
  33. const SESSION_API_KEY = 'api_key';
  34. const SESSION_APP_ID = 'app_id';
  35. const SESSION_USER_ID = 'uid';
  36. const SESSION_NETWORK_ID = 'network_key';
  37. const SESSION_EXPIRES = 'expires';
  38. const SESSION_EXPIRES_VALUE_NEVER = 'never';
  39. const SESSION_CALL_ID = 'call_id';
  40. const SESSION_INFINITE = 'infinite';
  41. // Auth related session values.
  42. const SESSION_APPROVED = 'approved';
  43. const SESSION_TYPE = 'type';
  44. const SESSION_TYPE_VALUE_AUTH = 'auth_token';
  45. const SESSION_TYPE_VALUE_SESS = 'session_key';
  46. /** The paramters for this API. */
  47. private $m_apiParams = array();
  48. /** Hold onto session object for this request */
  49. private $m_session = null;
  50. /** Hold onto the request context */
  51. private $m_context = null;
  52. /** Hold onto the SERVER object */
  53. private $m_server = null;
  54. /**
  55. * Constructor
  56. */
  57. public function __construct()
  58. {
  59. }
  60. public function getAppId()
  61. {
  62. return $this->getSessionValue(self::SESSION_APP_ID);
  63. }
  64. /**
  65. * Get the user id.
  66. *
  67. * @return unknown The user id.
  68. */
  69. public function getUserId()
  70. {
  71. return $this->getSessionValue(self::SESSION_USER_ID);
  72. }
  73. /**
  74. * Get the network id.
  75. *
  76. * @return unknown The user id.
  77. */
  78. public function getNetworkId()
  79. {
  80. $nid = $this->getSessionValue(self::SESSION_NETWORK_ID);
  81. if($nid == null)
  82. {
  83. $nid = $this->m_context->getNetworkKey();
  84. }
  85. return $nid;
  86. }
  87. /**
  88. * Lifecyle method to inject the context into the REST handler
  89. *
  90. * @param unknown_type $context
  91. */
  92. public function _setContext(Api_RequestContext &$context)
  93. {
  94. $this->m_context = &$context;
  95. $this->m_apiParams = &$context->getParameters();
  96. }
  97. public function &getContext()
  98. {
  99. return $this->m_context;
  100. }
  101. /**
  102. * Lifecyle method to inject session into this object.
  103. *
  104. * @param array $session
  105. */
  106. public function _setSession(&$session)
  107. {
  108. $this->m_session = &$session;
  109. }
  110. /**
  111. * Return the session object if needed.
  112. *
  113. * @return SESSION
  114. */
  115. public function &getSession()
  116. {
  117. return $this->m_session;
  118. }
  119. /**
  120. * Lifecyle initialization to setup the SERVER object.
  121. *
  122. * @param Api_Server $server
  123. */
  124. public function _setServer(OpenFBServer &$server)
  125. {
  126. $this->m_server = $server;
  127. }
  128. /**
  129. * Return access to the server object.
  130. *
  131. * @return Api_Server
  132. */
  133. public function &getServer()
  134. {
  135. return $this->m_server;
  136. }
  137. /**
  138. * Get the value of a session key for this request, null if session not in context or value not in context;
  139. *
  140. * @param string $key to look for
  141. * @return null if session[key] not available. value otherwise.
  142. * @throws OpenFBAPIException if session is not in context
  143. */
  144. public function getSessionValue($key)
  145. {
  146. if(! isset($this->m_session))
  147. {
  148. return null;
  149. // throw new OpenFBAPIException(FB_ERROR_MSG_BUSTED_SESSION, FB_ERROR_CODE_UNKNOWN_ERROR);
  150. }else if(isset($this->m_session[$key]))
  151. {
  152. return $this->m_session[$key];
  153. }else
  154. {
  155. return null;
  156. }
  157. }
  158. /**
  159. * Set a value in session, if session is in context.
  160. *
  161. * @param string $key to set
  162. * @param mixed $value to set
  163. * @return old value if in session already.
  164. * @throws OpenFBAPIException is session not in context
  165. */
  166. public function setSessionValue($key, $value)
  167. {
  168. $oldValue = $this->getSessionValue($key);
  169. $this->m_session[$key] = $value;
  170. return $oldValue;
  171. }
  172. /**
  173. * Get the paramters for this API.
  174. * @return unknown The paramters for this API.
  175. */
  176. public function &getApiParams()
  177. {
  178. return $this->m_apiParams;
  179. }
  180. /**
  181. * Return a specific param key.
  182. *
  183. * @param unknown_type $key
  184. * @return unknown
  185. */
  186. public function getApiParam($key, $default = null)
  187. {
  188. $value = $default;
  189. if ( isset($this->m_apiParams[$key]) && !$this->isEmpty($this->m_apiParams[$key]))
  190. {
  191. $value = $this->m_apiParams[$key];
  192. }
  193. return $value;
  194. }
  195. public function getRequiredApiParam($key)
  196. {
  197. $this->checkRequiredParam($key);
  198. return $this->getApiParam($key);
  199. }
  200. /**
  201. * Load the session from the session key.
  202. */
  203. abstract public function loadSession();
  204. /**
  205. * Specific point by which delegation can occur.
  206. */
  207. abstract public function delegateRequest();
  208. /**
  209. * Validate the session is correct and not expired.
  210. */
  211. abstract public function validateSession();
  212. /**
  213. * Validate the API Key ([TODO] and load basic APP data?).
  214. */
  215. abstract public function validateApiKey();
  216. /**
  217. * Validate the SIGNATURE by build MD5 checksum.
  218. */
  219. abstract public function validateSig();
  220. /**
  221. * Validate the version matches.
  222. */
  223. abstract public function validateVersion();
  224. /**
  225. * Validate the call_is being incremented on each and every call.
  226. */
  227. abstract public function validateCallId();
  228. /**
  229. * Validate the request has what it needs
  230. */
  231. abstract public function validateRequest();
  232. /**
  233. * Execute the REST api.
  234. */
  235. abstract public function execute();
  236. /**
  237. * Validate that the list of required parameters are defined in the api parameters passed in on
  238. * the constructor.
  239. *
  240. * @param array $requiredParams The list of required parameters
  241. *
  242. * @throws OpenFBAPIException if any of the required paramters are not set in the
  243. * api parameters passed in on the constructor.
  244. */
  245. public function checkRequiredParams($requiredParams)
  246. {
  247. foreach($requiredParams as $param)
  248. {
  249. $this->checkRequiredParam($param);
  250. }
  251. }
  252. /**
  253. * Validate a given parameter is avialable and not empty.
  254. *
  255. * @param string $parameter
  256. * @throws OpenFBAPIException if any of the required paramters are not set in the
  257. * api parameters passed in on the constructor.
  258. */
  259. public function checkRequiredParam($parameter)
  260. {
  261. if(! isset($this->m_apiParams[$parameter]) || $this->isEmpty($this->m_apiParams[$parameter]))
  262. {
  263. throw new OpenFBAPIException("The " . $parameter . " must be specified.", FB_ERROR_CODE_PARAMETER_MISSING);
  264. }
  265. }
  266. /**
  267. * Validate that at least one of the requiredParamSet options is set.
  268. *
  269. * @param array $requiredParamSet the set of parameters, of which at least one must be provided.
  270. * @return boolean whether at least one of the parameters is set; will only return true
  271. * @throws OpenFBApiException if none of the required parameters is set.
  272. */
  273. public function checkOneOfRequiredParams($requiredParamSet)
  274. {
  275. foreach ( $requiredParamSet as $param )
  276. {
  277. if ( isset($this->m_apiParams[$param]) && ! $this->isEmpty($this->m_apiParams[$param]))
  278. {
  279. return true;
  280. }
  281. }
  282. throw new OpenFBAPIException("At least one of '" . join("', '", $requiredParamSet) . "' must be specified.", FB_ERROR_CODE_PARAMETER_MISSING);
  283. }
  284. /**
  285. * If this is a cross app call, one app trying to execute something on another application
  286. * validate that the calling application is a DEFAULT enabled application as they should be the
  287. * only ones allowed to do this.
  288. *
  289. * @param integer $aid
  290. * @return true
  291. * @throws OpenFBApiException if the calling app is not allowed to make the call.
  292. */
  293. public function checkDefaultApp($aid = null)
  294. {
  295. // TODO: SECURITY: This disables security on app-to-app requests!
  296. return;
  297. /*
  298. * You can only cross check application information if
  299. * the calling application is a default application
  300. */
  301. $tad = $this->getAppId();
  302. error_log("Invoking API as $tad against application $aid");
  303. if(($aid == null) || ($aid != $tad))
  304. {
  305. $appService = Api_ServiceFactory::create('AppService');
  306. if ( null != $tad ) {
  307. // If a domain is calling this method, it should work
  308. $callingApp = $appService->getApp($tad);
  309. if (($callingApp == NULL) || (empty($callingApp)))
  310. {
  311. throw new OpenFBAPIException("Can not load calling application information ($aid,{ $tad })", FB_ERROR_CODE_UNKNOWN_ERROR);
  312. }
  313. $isDefault = $callingApp['isdefault'];
  314. if($isDefault == 0)
  315. {
  316. error_log("Application $tad cannot get information on application $aid");
  317. throw new OpenFBAPIException(FB_ERROR_MSG_GRAPH_EXCEPTION, FB_ERROR_CODE_GRAPH_EXCEPTION);
  318. }
  319. }
  320. }
  321. return true;
  322. }
  323. /**
  324. * Start the session object.
  325. * TODO: Think about where this really should live.
  326. *
  327. * @param id The ID of the product.
  328. */
  329. public function startSession($id = null)
  330. {
  331. if(! empty($id))
  332. {
  333. session_id($id);
  334. }
  335. session_start();
  336. // error_log("Session is as follows:");
  337. // error_log(var_export($_SESSION, true));
  338. $this->m_session = &$_SESSION;
  339. }
  340. /**
  341. * PHP empty function causes '0', 0, Array(), and FALSE to return true. These really are not empty
  342. * to us, so instead we have our own isEmpty function that only returns true if the variable is
  343. * ""
  344. * null
  345. * !isset($var); such as var $var; declared, but not value associated with it.
  346. *
  347. * @param mixed $var
  348. * @return bool
  349. */
  350. public static function isEmpty($var)
  351. {
  352. if(! isset($var) || is_null($var))
  353. {
  354. return true;
  355. }
  356. if(is_string($var) && strlen(rtrim($var)) == 0)
  357. {
  358. return true;
  359. }
  360. if(is_array($var) && count($var) == 0)
  361. {
  362. return true;
  363. }
  364. return false;
  365. }
  366. }
  367. ?>