PageRenderTime 55ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_virtuemart/classes/ps_function.php

https://github.com/Shigaru/shigaru
PHP | 342 lines | 212 code | 36 blank | 94 comment | 35 complexity | 0a852f892ee0a29bbc333c07ddecd286 MD5 | raw file
  1. <?php
  2. if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
  3. /**
  4. *
  5. * @version $Id: ps_function.php 1674 2009-03-04 02:07:57Z tkahl $
  6. * @package VirtueMart
  7. * @subpackage classes
  8. * @copyright Copyright (C) 2004-2007 soeren - All rights reserved.
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  10. * VirtueMart is free software. This version may have been modified pursuant
  11. * to the GNU General Public License, and as distributed it includes or
  12. * is derivative of works licensed under the GNU General Public License or
  13. * other free or open source software licenses.
  14. * See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
  15. *
  16. * http://virtuemart.net
  17. */
  18. /**
  19. * This class is is used to manage the function register.
  20. *
  21. */
  22. class vm_ps_function extends vmAbstractObject {
  23. var $_table_name = "#__{vm}_function";
  24. var $_key = 'function_id';
  25. function vm_ps_function() {
  26. $this->addRequiredField( array('function_name', 'module_id', 'function_class', 'function_method', 'function_perms') );
  27. $this->addUniqueField( 'function_name' );
  28. }
  29. /**
  30. * Validates adding a function to a module.
  31. *
  32. * @param array $d
  33. * @return boolean
  34. */
  35. function validate_add( &$d ) {
  36. return $this->validate( $d );
  37. }
  38. /**
  39. * Validates updating a module function
  40. *
  41. * @param array $d
  42. * @return boolean
  43. */
  44. function validate_update($d) {
  45. return $this->validate( $d );
  46. }
  47. /**
  48. * Validates deleting a function record
  49. *
  50. * @param array $d
  51. * @return boolean
  52. */
  53. function validate_delete($d) {
  54. global $perm, $vmLogger, $VM_LANG;
  55. if (empty($d["function_id"])) {
  56. $vmLogger->err( $VM_LANG->_('VM_FUNCTION_ERR_DELETE_SELECT') );
  57. return False;
  58. }
  59. else {
  60. $db = new ps_DB();
  61. if( is_array( $d["function_id"] )) {
  62. foreach( $d["function_id"] as $function ) {
  63. $db->query( 'SELECT module_perms, function_perms FROM `#__{vm}_function` f, `#__{vm}_module` m
  64. WHERE `function_id` = '.(int)$function
  65. . ' AND `f`.`module_id` = `m`.`module_id`' );
  66. $db->next_record();
  67. $module_perms = explode(',', $db->f('module_perms') );
  68. $function_perms = explode(',', $db->f('function_perms') );
  69. foreach( $module_perms as $permisson ) {
  70. if( !$perm->hashigherPerms( $permisson )) {
  71. $err_msg = $VM_LANG->_('VM_FUNCTION_ERR_DELETE_NOTALLOWED_MOD');
  72. $err_msg = str_replace('{module_perms}',$db->f('module_perms'),$err_msg);
  73. $err_msg = str_replace('{perms}',$_SESSION['auth']['perms'],$err_msg);
  74. $vmLogger->err( $err_msg );
  75. return false;
  76. }
  77. }
  78. foreach( $function_perms as $permisson ) {
  79. if( !$perm->hashigherPerms( $permisson )) {
  80. $err_msg = $VM_LANG->_('VM_FUNCTION_ERR_DELETE_NOTALLOWED_FUNC');
  81. $err_msg = str_replace('{function_perms}',$db->f('function_perms'),$err_msg);
  82. $err_msg = str_replace('{perms}',$_SESSION['auth']['perms'],$err_msg);
  83. $vmLogger->err( $err_msg );
  84. return false;
  85. }
  86. }
  87. }
  88. } else {
  89. $db->query( 'SELECT module_perms, function_perms FROM `#__{vm}_function` f, `#__{vm}_module` m
  90. WHERE `function_id` = '.(int)$d["function_id"]
  91. . ' AND `f`.`module_id` = `m`.`module_id`' );
  92. $db->next_record();
  93. $module_perms = explode(',', $db->f('module_perms') );
  94. $function_perms = explode(',', $db->f('function_perms') );
  95. foreach( $module_perms as $permisson ) {
  96. if( !$perm->hashigherPerms( $permisson )) {
  97. $err_msg = $VM_LANG->_('VM_FUNCTION_ERR_DELETE_NOTALLOWED_MOD');
  98. $err_msg = str_replace('{module_perms}',$db->f('module_perms'),$err_msg);
  99. $err_msg = str_replace('{perms}',$_SESSION['auth']['perms'],$err_msg);
  100. $vmLogger->err( $err_msg );
  101. return false;
  102. }
  103. }
  104. foreach( $function_perms as $permisson ) {
  105. if( !$perm->hashigherPerms( $permisson )) {
  106. $err_msg = $VM_LANG->_('VM_FUNCTION_ERR_DELETE_NOTALLOWED_FUNC');
  107. $err_msg = str_replace('{function_perms}',$db->f('function_perms'),$err_msg);
  108. $err_msg = str_replace('{perms}',$_SESSION['auth']['perms'],$err_msg);
  109. $vmLogger->err( $err_msg );
  110. return false;
  111. }
  112. }
  113. }
  114. }
  115. return true;
  116. }
  117. /**
  118. * Creates a new function record
  119. * @author pablo, soeren
  120. *
  121. * @param array $d
  122. * @return boolean
  123. */
  124. function add(&$d) {
  125. $db = new ps_DB;
  126. $timestamp = time();
  127. if (!$this->validate_add($d)) {
  128. return False;
  129. }
  130. if( is_array( $d[ 'function_perms' ] )) {
  131. $d[ 'function_perms' ] = implode( ',', $d[ 'function_perms' ] );
  132. }
  133. $fields = array( 'function_name' => vmGet( $d, 'function_name' ),
  134. 'function_class'=> vmGet( $d, 'function_class' ),
  135. 'function_method' => vmGet( $d, 'function_method' ),
  136. 'function_perms' => vmGet( $d, 'function_perms' ),
  137. 'module_id' => vmRequest::getInt('module_id'),
  138. 'function_description'=> vmGet( $d, 'function_description' ) );
  139. $db->buildQuery( 'INSERT', '#__{vm}_function', $fields );
  140. $db->query();
  141. $_REQUEST['function_id'] = $db->last_insert_id();
  142. return True;
  143. }
  144. /**
  145. * updates function information
  146. * @author pablo, soeren
  147. *
  148. * @param array $d
  149. * @return boolean
  150. */
  151. function update(&$d) {
  152. $db = new ps_DB;
  153. $timestamp = time();
  154. if (!$this->validate_update($d)) {
  155. return False;
  156. }
  157. if( is_array( $d[ 'function_perms' ] )) {
  158. $d[ 'function_perms' ] = implode( ',', $d[ 'function_perms' ] );
  159. }
  160. $fields = array( 'function_name' => vmGet( $d, 'function_name' ),
  161. 'function_class'=> vmGet( $d, 'function_class' ),
  162. 'function_method' => vmGet( $d, 'function_method' ),
  163. 'function_perms' => vmGet( $d, 'function_perms' ),
  164. 'function_description'=> vmGet( $d, 'function_description' ) );
  165. $db->buildQuery( 'UPDATE', '#__{vm}_function', $fields, 'WHERE function_id='.(int)$d["function_id"] );
  166. $db->query();
  167. return True;
  168. }
  169. /**
  170. * Delete a function, but check permissions before
  171. *
  172. * @param array $d
  173. * @return boolean
  174. */
  175. function delete(&$d) {
  176. $db = new ps_DB;
  177. if (!$this->validate_delete($d)) {
  178. return False;
  179. }
  180. $record_id = $d["function_id"];
  181. if( is_array( $record_id)) {
  182. foreach( $record_id as $record) {
  183. if( !$this->delete_record( (int)$record, $d ))
  184. return false;
  185. }
  186. return true;
  187. }
  188. else {
  189. return $this->delete_record( (int)$record_id, $d );
  190. }
  191. }
  192. /**
  193. * Deletes one Record.
  194. */
  195. function delete_record( $record_id, &$d ) {
  196. global $db;
  197. $q = 'DELETE from #__{vm}_function where function_id='.(int)$record_id;
  198. return $db->query($q);
  199. }
  200. /**
  201. * Returns an information array about the function $func
  202. *
  203. * @param string $func
  204. * @return mixed
  205. */
  206. function get_function($func) {
  207. $db = new ps_DB;
  208. $result = array();
  209. $q = "SELECT `function_perms`, `function_class`, `function_method`
  210. FROM `#__{vm}_function`
  211. WHERE LOWER(`function_name`)='".$db->getEscaped(strtolower($func))."'";
  212. $db->query( $q );
  213. if ($db->next_record()) {
  214. $result["perms"] = $db->f("function_perms");
  215. $result["class"] = $db->f("function_class");
  216. $result["method"] = $db->f("function_method");
  217. return $result;
  218. }
  219. else {
  220. return False;
  221. }
  222. }
  223. /**
  224. * Check Function Permissions
  225. * returns true if the function $func is registered
  226. * and user has permission to run it
  227. * Displays error if function is not registered
  228. *
  229. * @param string $func the function name
  230. * @return mixed
  231. */
  232. function getFuncPermissions( $func ) {
  233. global $page, $perm, $VM_LANG, $vmLogger;
  234. if (!empty($func)) {
  235. $funcParams = $this->get_function($func);
  236. if ($funcParams) {
  237. if ($perm->check($funcParams["perms"])) {
  238. return $funcParams;
  239. }
  240. else {
  241. $error = $VM_LANG->_('PHPSHOP_PAGE_403').'. ';
  242. $error .= $VM_LANG->_('PHPSHOP_FUNC_NO_EXEC') . $func;
  243. $vmLogger->err( $error );
  244. return false;
  245. }
  246. }
  247. else {
  248. $error = $VM_LANG->_('PHPSHOP_FUNC_NOT_REG').'. ';
  249. $error .= $func . $VM_LANG->_('PHPSHOP_FUNC_ISNO_REG') ;
  250. $vmLogger->err( $error );
  251. return false;
  252. }
  253. }
  254. return true;
  255. }
  256. /**
  257. * Checks if the currently logged in user is allowed to execute the function specified by $func
  258. *
  259. * @param string $func
  260. * @return boolean
  261. */
  262. function userCanExecuteFunc($func) {
  263. global $perm;
  264. if (!empty($func)) {
  265. // Retrieve the function attributes
  266. $funcParams = $this->get_function($func);
  267. if (is_array($funcParams) && $perm->check($funcParams["perms"])) {
  268. return true;
  269. }
  270. }
  271. return false;
  272. }
  273. /**
  274. * Updates the function permissions for all functions given
  275. *
  276. * @param array $d
  277. * @return boolean
  278. */
  279. function update_permissions( &$d ) {
  280. $db = new ps_DB;
  281. $i = 0;
  282. foreach( $d['function_perms'] as $function ) {
  283. $functions = implode(',', array_keys($function) );
  284. $function_id=(int)$d['function_id'][$i];
  285. $db->buildQuery('UPDATE', '#__{vm}_function', array('function_perms' => $functions ), 'WHERE function_id='.$function_id );
  286. $db->query();
  287. $i++;
  288. }
  289. return true;
  290. }
  291. }
  292. // Check if there is an extended class in the Themes and if it is allowed to use them
  293. // If the class is called outside Virtuemart, we have to make sure to load the settings
  294. // Thomas Kahl - Feb. 2009
  295. if (!defined('VM_ALLOW_EXTENDED_CLASSES') && file_exists(dirname(__FILE__).'/../virtuemart.cfg.php')) {
  296. include_once(dirname(__FILE__).'/../virtuemart.cfg.php');
  297. }
  298. // If settings are loaded, extended Classes are allowed and the class exisits...
  299. if (defined('VM_ALLOW_EXTENDED_CLASSES') && defined('VM_THEMEPATH') && VM_ALLOW_EXTENDED_CLASSES && file_exists(VM_THEMEPATH.'user_class/'.basename(__FILE__))) {
  300. // Load the theme-user_class as extended
  301. include_once(VM_THEMEPATH.'user_class/'.basename(__FILE__));
  302. } else {
  303. // Otherwise we have to use the original classname to extend the core-class
  304. class ps_function extends vm_ps_function {}
  305. }
  306. ?>