/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/security/package.module.security.php

https://github.com/livinglab/openlab · PHP · 485 lines · 433 code · 0 blank · 52 comment · 46 complexity · e53af897e7dbc93d5c4951d4d990a9ed MD5 · raw file

  1. <?php
  2. /**
  3. * Class A_Security_Factory
  4. * @mixin C_Component_Factory
  5. * @adapts I_Component_Factory
  6. */
  7. class A_Security_Factory extends Mixin
  8. {
  9. function wordpress_security_manager($context = FALSE)
  10. {
  11. return new C_WordPress_Security_Manager($context);
  12. }
  13. function security_manager($context = FALSE)
  14. {
  15. return $this->object->wordpress_security_manager($context);
  16. }
  17. function wordpress_security_actor($context = FALSE)
  18. {
  19. return new C_WordPress_Security_Actor($context);
  20. }
  21. function wordpress_security_token($context = FALSE)
  22. {
  23. return new C_Wordpress_Security_Token($context);
  24. }
  25. function security_token($context)
  26. {
  27. return $this->object->wordpress_security_token($context);
  28. }
  29. }
  30. class Mixin_Security_Actor extends Mixin
  31. {
  32. function add_capability($capability_name)
  33. {
  34. return false;
  35. }
  36. function remove_capability($capability_name)
  37. {
  38. return false;
  39. }
  40. function is_allowed($capability_name, $args = null)
  41. {
  42. return false;
  43. }
  44. function is_user()
  45. {
  46. return false;
  47. }
  48. }
  49. class Mixin_Security_Actor_Entity extends Mixin
  50. {
  51. var $entity_object = null;
  52. var $entity_props = null;
  53. // Note, an Actor with null $entity is considered a "Guest", i.e. no privileges
  54. function set_entity($entity, $entity_props = null)
  55. {
  56. $this->object->entity_object = $entity;
  57. $this->object->entity_props = $entity_props;
  58. }
  59. function get_entity($entity = null)
  60. {
  61. if ($entity == null) {
  62. $entity = $this->object->entity_object;
  63. }
  64. if ($entity != null && $entity == $this->object->entity_object) {
  65. return $entity;
  66. }
  67. return null;
  68. }
  69. function get_entity_id($entity = null)
  70. {
  71. $entity = $this->object->get_entity($entity);
  72. if ($entity != null) {
  73. $entity_props = $this->object->entity_props;
  74. if (isset($entity_props['id'])) {
  75. return $entity_props['id'];
  76. }
  77. }
  78. return null;
  79. }
  80. function get_entity_type($entity = null)
  81. {
  82. $entity = $this->object->get_entity($entity);
  83. if ($entity != null) {
  84. $entity_props = $this->object->entity_props;
  85. if (isset($entity_props['type'])) {
  86. return $entity_props['type'];
  87. }
  88. }
  89. return null;
  90. }
  91. }
  92. /**
  93. * Class C_Security_Actor
  94. * @mixin Mixin_Security_Actor
  95. * @mixin Mixin_Security_Actor_Entity
  96. * @implements I_Security_Actor
  97. */
  98. class C_Security_Actor extends C_Component
  99. {
  100. function define($context = FALSE)
  101. {
  102. parent::define($context);
  103. $this->implement('I_Security_Actor');
  104. $this->add_mixin('Mixin_Security_Actor');
  105. $this->add_mixin('Mixin_Security_Actor_Entity');
  106. }
  107. }
  108. class Mixin_Security_Manager extends Mixin
  109. {
  110. function is_allowed($capability_name, $args = null)
  111. {
  112. $actor = $this->object->get_current_actor();
  113. if ($actor != null) {
  114. return $actor->is_allowed($capability_name, $args);
  115. }
  116. return false;
  117. }
  118. function get_actor($actor_id, $actor_type = null, $args = null)
  119. {
  120. return null;
  121. }
  122. function get_current_actor()
  123. {
  124. return null;
  125. }
  126. }
  127. class Mixin_Security_Manager_Request extends Mixin
  128. {
  129. function get_request_token($action_name, $args = null)
  130. {
  131. return null;
  132. }
  133. }
  134. /**
  135. * Class C_Security_Manager
  136. * @mixin Mixin_Security_Manager
  137. * @mixin Mixin_Security_Manager_Request
  138. * @implements I_Security_Manager
  139. */
  140. class C_Security_Manager extends C_Component
  141. {
  142. static $_instances = array();
  143. function define($context = FALSE)
  144. {
  145. parent::define($context);
  146. $this->implement('I_Security_Manager');
  147. $this->add_mixin('Mixin_Security_Manager');
  148. $this->add_mixin('Mixin_Security_Manager_Request');
  149. }
  150. /**
  151. * @param bool|string $context
  152. * @return C_Security_Manager
  153. */
  154. static function get_instance($context = False)
  155. {
  156. if (!isset(self::$_instances[$context])) {
  157. self::$_instances[$context] = new C_Security_Manager($context);
  158. }
  159. return self::$_instances[$context];
  160. }
  161. }
  162. class Mixin_Security_Token extends Mixin
  163. {
  164. function get_request_list($args = null)
  165. {
  166. return array();
  167. }
  168. function get_form_html($args = null)
  169. {
  170. return null;
  171. }
  172. function check_request($request_values)
  173. {
  174. return false;
  175. }
  176. function check_current_request()
  177. {
  178. return $this->object->check_request($_REQUEST);
  179. }
  180. }
  181. class Mixin_Security_Token_Property extends Mixin
  182. {
  183. var $_action_name;
  184. var $_args;
  185. function init_token($action_name, $args = null)
  186. {
  187. $this->object->_action_name = $action_name;
  188. $this->object->_args = $args;
  189. }
  190. function get_action_name()
  191. {
  192. return $this->object->_action_name;
  193. }
  194. function get_property($name)
  195. {
  196. if (isset($this->object->_args[$name])) {
  197. return $this->object->_args[$name];
  198. }
  199. return null;
  200. }
  201. function get_property_list()
  202. {
  203. return array_keys((array) $this->object->_args);
  204. }
  205. }
  206. /**
  207. * Class C_Security_Token
  208. * @mixin Mixin_Security_Token
  209. * @mixin Mixin_Security_Token_Property
  210. * @implements I_Security_Token
  211. */
  212. class C_Security_Token extends C_Component
  213. {
  214. function define($context = FALSE)
  215. {
  216. parent::define($context);
  217. $this->implement('I_Security_Token');
  218. $this->add_mixin('Mixin_Security_Token');
  219. $this->add_mixin('Mixin_Security_Token_Property');
  220. }
  221. }
  222. class Mixin_WordPress_Security_Actor extends Mixin
  223. {
  224. function add_capability($capability_name)
  225. {
  226. $entity = $this->object->get_entity();
  227. if ($entity != null) {
  228. $capability_name = $this->object->get_native_action($capability_name);
  229. $entity->add_cap($capability_name);
  230. return true;
  231. }
  232. return false;
  233. }
  234. function remove_capability($capability_name)
  235. {
  236. $entity = $this->object->get_entity();
  237. if ($entity != null && $this->object->is_allowed($capability_name)) {
  238. $capability_name = $this->object->get_native_action($capability_name);
  239. $entity->remove_cap($capability_name);
  240. return true;
  241. }
  242. return false;
  243. }
  244. function is_allowed($capability_name, $args = null)
  245. {
  246. $entity = $this->object->get_entity();
  247. if ($entity != null) {
  248. $capability_name = $this->object->get_native_action($capability_name, $args);
  249. return $entity->has_cap($capability_name);
  250. }
  251. return false;
  252. }
  253. function is_user()
  254. {
  255. return $this->object->get_entity_type() == 'user';
  256. }
  257. function get_native_action($capability_name, $args = null)
  258. {
  259. return $capability_name;
  260. }
  261. }
  262. class Mixin_WordPress_Security_Action_Converter extends Mixin
  263. {
  264. function get_native_action($capability_name, $args = null)
  265. {
  266. switch ($capability_name) {
  267. case 'nextgen_edit_settings':
  268. $capability_name = 'NextGEN Change options';
  269. break;
  270. case 'nextgen_edit_style':
  271. $capability_name = 'NextGEN Change style';
  272. break;
  273. case 'nextgen_edit_display_settings':
  274. $capability_name = 'NextGEN Change options';
  275. break;
  276. case 'nextgen_edit_displayed_gallery':
  277. $capability_name = 'NextGEN Attach Interface';
  278. break;
  279. case 'nextgen_edit_gallery':
  280. $capability_name = 'NextGEN Manage gallery';
  281. break;
  282. case 'nextgen_edit_gallery_unowned':
  283. $capability_name = 'NextGEN Manage others gallery';
  284. break;
  285. case 'nextgen_upload_image':
  286. $capability_name = 'NextGEN Upload images';
  287. break;
  288. case 'nextgen_edit_album_settings':
  289. $capability_name = 'NextGEN Edit album settings';
  290. break;
  291. case 'nextgen_edit_album':
  292. $capability_name = 'NextGEN Edit album';
  293. break;
  294. }
  295. return $capability_name;
  296. }
  297. }
  298. /**
  299. * Class C_WordPress_Security_Actor
  300. * @mixin Mixin_WordPress_Security_Actor
  301. * @mixin Mixin_WordPress_Security_Action_Converter
  302. */
  303. class C_WordPress_Security_Actor extends C_Security_Actor
  304. {
  305. function define($context = FALSE)
  306. {
  307. parent::define($context);
  308. $this->add_mixin('Mixin_WordPress_Security_Actor');
  309. $this->add_mixin('Mixin_WordPress_Security_Action_Converter');
  310. }
  311. }
  312. class Mixin_WordPress_Security_Manager extends Mixin
  313. {
  314. function get_actor($actor_id, $actor_type = null, $args = null)
  315. {
  316. if ($actor_type == null) {
  317. $actor_type = 'user';
  318. }
  319. $object = null;
  320. if ($actor_id != null) {
  321. switch ($actor_type) {
  322. case 'user':
  323. $object = get_userdata($actor_id);
  324. if ($object == false) {
  325. $object = null;
  326. }
  327. break;
  328. case 'role':
  329. $object = get_role($actor_id);
  330. if ($object == false) {
  331. $object = null;
  332. }
  333. break;
  334. }
  335. }
  336. if ($object != null) {
  337. $factory = C_Component_Factory::get_instance();
  338. $actor = $factory->create('wordpress_security_actor', $actor_type);
  339. $entity_props = array('type' => $actor_type, 'id' => $actor_id);
  340. $actor->set_entity($object, $entity_props);
  341. return $actor;
  342. }
  343. return $this->object->get_guest_actor();
  344. }
  345. function get_current_actor()
  346. {
  347. // If the current_user has an id of 0, then perhaps something went wrong
  348. // with trying to parse the cookie. In that case, we'll force WordPress to try
  349. // again
  350. global $current_user;
  351. if ($current_user->ID == 0) {
  352. if (isset($GLOBALS['HTTP_COOKIE_VARS']) && isset($GLOBALS['_COOKIE'])) {
  353. $current_user = NULL;
  354. foreach ($GLOBALS['HTTP_COOKIE_VARS'] as $key => $value) {
  355. if (!isset($_COOKIE[$key])) {
  356. $_COOKIE[$key] = $value;
  357. }
  358. }
  359. }
  360. }
  361. return $this->object->get_actor(get_current_user_id(), 'user');
  362. }
  363. function get_guest_actor()
  364. {
  365. $factory = C_Component_Factory::get_instance();
  366. $actor = $factory->create('wordpress_security_actor', 'user');
  367. $entity_props = array('type' => 'user');
  368. $actor->set_entity(null, $entity_props);
  369. return $actor;
  370. }
  371. }
  372. class Mixin_WordPress_Security_Manager_Request extends Mixin
  373. {
  374. function get_request_token($action_name, $args = null)
  375. {
  376. $factory = C_Component_Factory::get_instance();
  377. $token = $factory->create('wordpress_security_token');
  378. $token->init_token($action_name, $args);
  379. return $token;
  380. }
  381. }
  382. /**
  383. * Class C_WordPress_Security_Manager
  384. * @mixin Mixin_WordPress_Security_Manager
  385. * @mixin Mixin_WordPress_Security_Manager_Request
  386. */
  387. class C_WordPress_Security_Manager extends C_Security_Manager
  388. {
  389. static $_instances = array();
  390. function define($context = FALSE)
  391. {
  392. parent::define($context);
  393. $this->add_mixin('Mixin_WordPress_Security_Manager');
  394. $this->add_mixin('Mixin_WordPress_Security_Manager_Request');
  395. }
  396. /**
  397. * @param bool|string $context
  398. * @return C_WordPress_Security_Manager
  399. */
  400. static function get_instance($context = False)
  401. {
  402. if (!isset(self::$_instances[$context])) {
  403. $klass = get_class();
  404. self::$_instances[$context] = new $klass($context);
  405. }
  406. return self::$_instances[$context];
  407. }
  408. }
  409. class Mixin_Wordpress_Security_Token extends Mixin
  410. {
  411. function get_request_list($args = null)
  412. {
  413. $prefix = isset($args['prefix']) ? $args['prefix'] : null;
  414. $action_name = $this->object->get_action_name();
  415. $list = array();
  416. if ($prefix != null) {
  417. $list[$action_name . '_prefix'] = $prefix;
  418. }
  419. $action = $this->object->get_nonce_name();
  420. $list[$prefix . $action_name . '_sec'] = wp_create_nonce($action);
  421. return $list;
  422. }
  423. function get_form_html($args = null)
  424. {
  425. $list = $this->object->get_request_list($args);
  426. $out = null;
  427. foreach ($list as $name => $value) {
  428. $out .= '<input type="hidden" name="' . esc_attr($name) . '" value="' . esc_attr($value) . '" />';
  429. }
  430. return $out;
  431. }
  432. function get_json($args = null)
  433. {
  434. $list = $this->object->get_request_list($args);
  435. return json_encode($list);
  436. }
  437. function check_request($request_values)
  438. {
  439. $action_name = $this->object->get_action_name();
  440. $action = $this->object->get_nonce_name();
  441. $prefix = isset($request_values[$action_name . '_prefix']) ? $request_values[$action_name . '_prefix'] : null;
  442. if (isset($request_values[$prefix . $action_name . '_sec'])) {
  443. $nonce = $request_values[$prefix . $action_name . '_sec'];
  444. $result = wp_verify_nonce($nonce, $action);
  445. if ($result) {
  446. return true;
  447. }
  448. }
  449. return false;
  450. }
  451. function get_nonce_name()
  452. {
  453. $action_name = $this->object->get_action_name();
  454. $prop_list = $this->object->get_property_list();
  455. $action = $action_name;
  456. foreach ($prop_list as $prop_name) {
  457. $property = $this->object->get_property($prop_name);
  458. $action .= '_' . strval($property);
  459. }
  460. return $action;
  461. }
  462. }
  463. class Mixin_Wordpress_Security_Token_MVC extends Mixin
  464. {
  465. function check_request($request_values)
  466. {
  467. // XXX check URL parameters passed with the MVC module
  468. //
  469. return $this->call_parent('check_request', $request_values);
  470. }
  471. }
  472. /**
  473. * Class C_Wordpress_Security_Token
  474. * @mixin Mixin_Wordpress_Security_Token
  475. * @mixin Mixin_Wordpress_Security_Token_MVC
  476. */
  477. class C_Wordpress_Security_Token extends C_Security_Token
  478. {
  479. function define($context = FALSE)
  480. {
  481. parent::define($context);
  482. $this->add_mixin('Mixin_Wordpress_Security_Token');
  483. $this->add_mixin('Mixin_Wordpress_Security_Token_MVC');
  484. }
  485. }