PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_jfusionplugins/magento/admin.php

http://jfusion.googlecode.com/
PHP | 434 lines | 314 code | 66 blank | 54 comment | 43 complexity | e3408e3225cfe7d4232eb257f9815956 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * @package JFusion_magento
  4. * @author JFusion development team
  5. * @copyright Copyright (C) 2008 JFusion. All rights reserved.
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  7. */
  8. // no direct access
  9. defined('_JEXEC' ) or die('Restricted access' );
  10. /**
  11. * load the factory
  12. */
  13. //require_once dirname(__FILE__) . DS . 'factory.php';
  14. /**
  15. * JFusion Admin class for Magento 1.1
  16. * For detailed descriptions on these functions please check the model.abstractadmin.php
  17. * @package JFusion_Magento
  18. */
  19. class JFusionAdmin_magento extends JFusionAdmin{
  20. function getJname(){
  21. return 'magento';
  22. }
  23. function getTablename(){
  24. return 'admin_user';
  25. }
  26. function setupFromPath($forumPath){
  27. //check for trailing slash and generate file path
  28. if (substr($forumPath, -1) != DS) {
  29. $forumpath = $forumPath.DS;
  30. }
  31. $xmlfile = $forumpath.'app'.DS.'etc'.DS.'local.xml';
  32. if (file_exists($xmlfile)) {
  33. $xml = JFactory::getXMLParser('Simple');
  34. if (!$xml->loadFile($xmlfile)) {
  35. unset($xml);
  36. JError::raiseWarning(500,JText::_('WIZARD_FAILURE'). " $xmlfile " . JText::_('WIZARD_MANUAL'));
  37. $result = false;
  38. return $result;
  39. }
  40. //save the parameters into array
  41. $params = array();
  42. $params['database_host'] = (string)$xml->document->global[0]->resources[0]->default_setup[0]->connection[0]->host[0]->data();
  43. $params['database_name'] = (string)$xml->document->global[0]->resources[0]->default_setup[0]->connection[0]->dbname[0]->data();
  44. $params['database_user'] = (string)$xml->document->global[0]->resources[0]->default_setup[0]->connection[0]->username[0]->data();
  45. $params['database_password']= (string)$xml->document->global[0]->resources[0]->default_setup[0]->connection[0]->password[0]->data();
  46. $params['database_prefix'] = (string)$xml->document->global[0]->resources[0]->db[0]->table_prefix[0]->data();
  47. $params['database_type'] = "mysql";
  48. $params['source_path'] = $forumpath;
  49. unset($xml);
  50. return $params;
  51. } else {
  52. JError::raiseWarning(500,JText::_('WIZARD_FAILURE'). " $xmlfile " . JText::_('WIZARD_MANUAL'));
  53. $result = false;
  54. return $result;
  55. }
  56. }
  57. function getUserList($start = 0, $count = '')
  58. {
  59. //getting the connection to the db
  60. $db = JFusionFactory::getDataBase($this->getJname());
  61. $query = 'SELECT email as username, email from #__customer_entity';
  62. if(!empty($count)){
  63. $query .= ' LIMIT ' . $start . ', ' .$count;
  64. }
  65. $db->setQuery($query );
  66. //getting the results
  67. $userlist = $db->loadObjectList();
  68. return $userlist;
  69. }
  70. function getUserCount(){
  71. //getting the connection to the db
  72. $db = JFusionFactory::getDataBase($this->getJname());
  73. $query = 'SELECT count(*) from #__customer_entity';
  74. $db->setQuery($query );
  75. //getting the results
  76. $no_users = $db->loadResult();
  77. return $no_users;
  78. }
  79. function getUsergroupList(){
  80. //get the connection to the db
  81. $db = JFusionFactory::getDataBase($this->getJname());
  82. $query = 'SELECT customer_group_id as id, customer_group_code as name from #__customer_group;';
  83. $db->setQuery($query );
  84. //getting the results
  85. return $db->loadObjectList();
  86. }
  87. function getDefaultUsergroup(){
  88. $params = JFusionFactory::getParams($this->getJname());
  89. $usergroup_id = $params->get('usergroup');
  90. //we want to output the usergroup name
  91. $db = JFusionFactory::getDatabase($this->getJname());
  92. $query = 'SELECT customer_group_code from #__customer_group WHERE customer_group_id = ' . $usergroup_id;
  93. $db->setQuery($query );
  94. return $db->loadResult();
  95. }
  96. function allow_empty_cookie_path(){
  97. return true;
  98. }
  99. function allow_empty_cookie_domain() {
  100. return true;
  101. }
  102. function debugConfigExtra(){
  103. // see if we have an api user in Magento
  104. $jname = $this->getJname();
  105. $db = JFusionFactory::getDataBase($this->getJname());
  106. $query = 'SELECT count(*) from #__api_user';
  107. $db->setQuery($query );
  108. $no_users = $db->loadResult();
  109. if ($no_users <= 0){
  110. JError::raiseWarning(0, $jname . ': ' . JText::_('MAGENTO_NEED_API_USER'));
  111. } else{
  112. // check if we have valid parameters for apiuser and api key
  113. $params = JFusionFactory::getParams($this->getJname());
  114. $apiuser = $params->get('apiuser');
  115. $apikey = $params->get('apikey');
  116. if (!$apiuser || !$apikey ){
  117. JError::raiseWarning(0, $jname . '-plugin: ' . JText::_('MAGENTO_NO_API_DATA'));
  118. } else {
  119. //finally check if the apiuser and apikey are valid
  120. $query = 'SELECT api_key FROM #__api_user WHERE username = ' . $db->Quote($apiuser);
  121. $db->setQuery($query);
  122. $api_key = $db->loadResult();
  123. $hashArr = explode(':', $api_key);
  124. $api_key = $hashArr[0];
  125. $api_salt = $hashArr[1];
  126. if($api_salt) {
  127. $params_hash = md5($api_salt.$apikey);
  128. } else {
  129. $params_hash = md5($apikey);
  130. }
  131. if ($params_hash != $api_key){
  132. JError::raiseWarning(0, $jname . '-plugin: ' . JText::_('MAGENTO_WRONG_APIUSER_APIKEY_COMBINATION'));
  133. }
  134. }
  135. }
  136. // check the user_remote_addr security settings
  137. $query = "SELECT value FROM #__core_config_data WHERE path = 'web/session/use_remote_addr'";
  138. $db->setQuery($query );
  139. if ($db->_errorNum == 0){
  140. $value = $db->loadResult();
  141. if ($value) {
  142. JError::raiseWarning(0, $jname . ': ' . JText::_('MAGENTO_USE_REMOTE_ADDRESS_NOT_DISABLED'));
  143. }
  144. }
  145. // we need to have the curl library installed
  146. if (!extension_loaded('curl')) {
  147. JError::raiseWarning(0, $jname . ': ' . JText::_('CURL_NOTINSTALLED'));
  148. }
  149. }
  150. function allowRegistration(){
  151. $result = true;
  152. $params = JFusionFactory::getParams($this->getJname());
  153. $registration_disabled = $params->get('disabled_registration');
  154. if ($registration_disabled){$result = false;}
  155. return $result;
  156. }
  157. /**
  158. * Put specific javascript script
  159. */
  160. public function outputJavascript(){
  161. $js = <<< EOD
  162. <script language="javascript" type="text/javascript">
  163. <!--
  164. function module(action) {
  165. var form = document.adminForm;
  166. form.customcommand.value = action;
  167. form.action.value = 'apply';
  168. submitform('saveconfig');
  169. return;
  170. }
  171. //-->
  172. </script>
  173. EOD;
  174. return $js;
  175. }
  176. public function moduleInstallation() {
  177. $jname = $this->getJname() ;
  178. $params = & JFusionFactory::getParams ( $jname );
  179. $db = & JFusionFactory::getDatabase ( $jname );
  180. if (! JError::isError ( $db ) && ! empty ( $db )) {
  181. $source_path = $params->get('source_path', '');
  182. if(!file_exists($source_path . DS . 'app'. DS .'Mage.php')){
  183. return JText::_('MAGE_CONFIG_SOURCE_PATH');
  184. }
  185. $js = $this->outputJavascript();
  186. $document = &JFactory::getDocument();
  187. $document->addCustomTag($js);
  188. $mod_exists = false;
  189. if(file_exists($source_path . DS .'app'. DS .'etc'. DS .'modules'. DS .'Jfusion_All.xml')){
  190. $mod_exists = true;
  191. }
  192. $html = '<div class="button2-left"><div class="blank"><a href="javascript:void(0);" onclick="return module(\''.(($mod_exists)?'uninstallModule':'installModule').'\');">' .((!$mod_exists)?JText::_ ( 'MODULE_UNINSTALL_BUTTON' ):JText::_('MODULE_INSTALL_BUTTON')) . '</a></div></div>' . "\n";
  193. if ($mod_exists ) {
  194. $src = "components/com_jfusion/images/tick.png";
  195. } else {
  196. $src = "components/com_jfusion/images/cross.png";
  197. }
  198. $html .= "<img src='$src' style='margin-left:10px;' id='usergroups_img'/>";
  199. return $html;
  200. } else {
  201. return JText::_ ( 'MAGE_CONFIG_FIRST' );
  202. }
  203. }
  204. public function installModule() {
  205. $jname = $this->getJname ();
  206. $db = JFusionFactory::getDatabase($jname);
  207. $params = JFusionFactory::getParams ( $jname );
  208. $source_path = $params->get ( 'source_path' );
  209. jimport ( 'joomla.filesystem.archive' );
  210. jimport ( 'joomla.filesystem.file' );
  211. jimport ( 'pear.archive_tar.Archive_Tar' );
  212. $archive_filename = 'magento_module_jfusion.tar.gz';
  213. $old_chdir = getcwd();
  214. $src_archive = $src_path = realpath ( dirname ( __FILE__ ) ) . DS . 'install_module';
  215. $src_code = $src_archive . DS . 'source';
  216. $dest = $source_path;
  217. // Create an archive to facilitate the installation into the Magento installation while extracting
  218. chdir($src_code);
  219. $tar = new Archive_Tar( $archive_filename, 'gz' );
  220. $tar->setErrorHandling(PEAR_ERROR_PRINT);
  221. $tar->createModify( 'app' , '', '' );
  222. chdir($old_chdir);
  223. $ret = JArchive::extract ( $src_code . DS . $archive_filename, $dest );
  224. JFile::delete($src_code . DS . $archive_filename);
  225. // Initialize default data config in Magento database
  226. $joomla = JFusionFactory::getParams('joomla_int');
  227. $joomla_baseurl = $joomla->get('source_url');
  228. $joomla_secret = $joomla->get('secret');
  229. $query = "REPLACE INTO #__core_config_data SET path = 'joomla/joomlaconfig/baseurl', value = '".$joomla_baseurl."';";
  230. $db->BeginTrans();
  231. $db->Execute($query);
  232. if ($db->_errorNum != 0) {
  233. $db->RollbackTrans();
  234. $status['error'] = $db->stderr ();
  235. return $status;
  236. }
  237. $query = "REPLACE INTO #__core_config_data SET path = 'joomla/joomlaconfig/installationpath', value = '".JPATH_SITE."';";
  238. $db->BeginTrans();
  239. $db->Execute($query);
  240. if ($db->_errorNum != 0) {
  241. $db->RollbackTrans();
  242. $status['error'] = $db->stderr ();
  243. return $status;
  244. }
  245. $query = "REPLACE INTO #__core_config_data SET path = 'joomla/joomlaconfig/secret_key', value = '".$joomla_secret."';";
  246. $db->BeginTrans();
  247. $db->Execute($query);
  248. if ($db->_errorNum != 0) {
  249. $db->RollbackTrans();
  250. $status['error'] = $db->stderr ();
  251. return $status;
  252. }
  253. $status = array();
  254. if ($ret !== true) {
  255. $status['error'] = $jname . ': ' . JText::sprintf('INSTALL_MODULE_ERROR', $src_archive, $dest);
  256. }else{
  257. $status['message'] = $jname .': ' . JText::_('INSTALL_MODULE_SUCCESS');
  258. }
  259. return $status;
  260. }
  261. public function uninstallModule(){
  262. jimport ( 'joomla.filesystem.file' );
  263. jimport ( 'joomla.filesystem.folder' );
  264. $jname = $this->getJname ();
  265. $db = JFusionFactory::getDatabase($jname);
  266. $params = JFusionFactory::getParams ( $jname );
  267. $source_path = $params->get ( 'source_path' );
  268. $xmlfile = realpath ( dirname ( __FILE__ ) ) . DS . 'install_module' . DS . 'source' . DS . 'listfiles.xml';
  269. $listfiles = JFactory::getXMLParser('simple');
  270. $listfiles->loadFile($xmlfile);
  271. $files = $listfiles->document->file;
  272. foreach($files as $file){
  273. $file = $file->data();
  274. $file = preg_replace('#/#', DS, $file);
  275. @chmod($source_path . DS . $file, 0777);
  276. if(!is_dir($source_path . DS . $file)){
  277. JFile::delete($source_path . DS . $file);
  278. }else{
  279. JFolder::delete($source_path . DS . $file);
  280. }
  281. }
  282. $paths = array();
  283. $paths[] = 'joomla/joomlaconfig/baseurl';
  284. $paths[] = 'joomla/joomlaconfig/installationpath';
  285. $paths[] = 'joomla/joomlaconfig/secret_key';
  286. foreach($paths as $path)
  287. {
  288. $query = "DELETE FROM #__core_config_data WHERE path = " . $db->Quote($path);
  289. $db->BeginTrans ();
  290. $db->Execute ( $query );
  291. if ($db->_errorNum != 0) {
  292. $db->RollbackTrans ();
  293. $status ['error'] = $db->stderr ();
  294. return $status;
  295. }
  296. }
  297. /*$query = "DELETE FROM #__core_config_data WHERE path = 'joomla/joomlaconfig/installationpath'";
  298. $db->BeginTrans();
  299. $db->Execute($query);
  300. if ($db->_errorNum != 0) {
  301. $db->RollbackTrans();
  302. $status['error'] = $db->stderr ();
  303. return $status;
  304. }
  305. $query = "DELETE FROM #__core_config_data WHERE path = 'joomla/joomlaconfig/secret_key'";
  306. $db->BeginTrans();
  307. $db->Execute($query);
  308. if ($db->_errorNum != 0) {
  309. $db->RollbackTrans();
  310. $status['error'] = $db->stderr ();
  311. return $status;
  312. }*/
  313. $status = array();
  314. if ($ret !== true) {
  315. $status['error'] = $jname . ': ' . JText::sprintf('UNINSTALL_MODULE_ERROR', $src_archive, $dest);
  316. }else{
  317. $status['message'] = $jname .': ' . JText::_('UNINSTALL_MODULE_SUCCESS');
  318. }
  319. }
  320. public function moduleActivation() {
  321. $jname = $this->getJname ();
  322. $params = JFusionFactory::getParams ( $jname );
  323. $source_path = $params->get ( 'source_path' );
  324. $jfusion_mod_xml = $source_path . DS .'app'. DS .'etc'. DS .'modules'. DS .'Jfusion_All.xml';
  325. if(file_exists($jfusion_mod_xml)){
  326. $xml = JFactory::getXMLParser ( 'simple' );
  327. $xml->loadfile ( $jfusion_mod_xml );
  328. $modules = $xml->document->getElementByPath ( 'modules/jfusion_joomla' );
  329. $activated = $modules->active [0]->data ();
  330. if($activated == 'false'){
  331. $activated = 0;
  332. }else{
  333. $activated = 1;
  334. }
  335. $js = $this->outputJavascript();
  336. $document = &JFactory::getDocument();
  337. $document->addCustomTag($js);
  338. $html = '<div class="button2-left"><div class="blank"><a href="javascript:void(0);" onclick="return module(\'activateModule\');">' . ((!$activated)?JText::_ ( 'MODULE_DEACTIVATION_BUTTON' ):JText::_ ( 'MODULE_ACTIVATION_BUTTON' )) . '</a></div></div>' . "\n";
  339. $html .= '<input type="hidden" name="activation" id="activation" value="'.(($activated)?0:1).'"/>';
  340. if ($activated ) {
  341. $src = "components/com_jfusion/images/tick.png";
  342. } else {
  343. $src = "components/com_jfusion/images/cross.png";
  344. }
  345. $html .= "<img src='$src' style='margin-left:10px;'/>";
  346. return $html;
  347. } else {
  348. return JText::_ ( 'MAGE_CONFIG_FIRST' );
  349. }
  350. }
  351. public function activateModule(){
  352. jimport ( 'joomla.filesystem.file' );
  353. $activation = ((JRequest::getVar('activation', 1))?'true':'false');
  354. $jname = $this->getJname ();
  355. $params = JFusionFactory::getParams ( $jname );
  356. $source_path = $params->get ( 'source_path' );
  357. $jfusion_mod_xml = $source_path . DS .'app'. DS .'etc'. DS .'modules'. DS .'Jfusion_All.xml';
  358. $xml = JFactory::getXMLParser ( 'simple' );
  359. $xml->loadfile ( $jfusion_mod_xml );
  360. $module = $xml->document->getElementByPath ( 'modules/jfusion_joomla' );
  361. //$xml->document->modules->jfusion_joomla->active[0]->setData('false');
  362. $module->active[0]->setData($activation);
  363. $buffer = '<?xml version="1.0"?'.'>';
  364. $buffer .= $xml->document->toString();
  365. JFile::write($jfusion_mod_xml, $buffer);
  366. }
  367. }