PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ModuleInstall/PackageManager/PackageController.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 373 lines | 257 code | 31 blank | 85 comment | 37 complexity | af8c746cbb6ba5142568d34e535d0a94 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. /*********************************************************************************
  3. * SugarCRM Community Edition is a customer relationship management program developed by
  4. * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  24. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  25. *
  26. * The interactive user interfaces in modified source and object code versions
  27. * of this program must display Appropriate Legal Notices, as required under
  28. * Section 5 of the GNU Affero General Public License version 3.
  29. *
  30. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  31. * these Appropriate Legal Notices must retain the display of the "Powered by
  32. * SugarCRM" logo. If the display of the logo is not reasonably feasible for
  33. * technical reasons, the Appropriate Legal Notices must display the words
  34. * "Powered by SugarCRM".
  35. ********************************************************************************/
  36. require_once('ModuleInstall/PackageManager/PackageManagerDisplay.php');
  37. require_once('ModuleInstall/PackageManager/PackageManager.php');
  38. class PackageController{
  39. var $_pm;
  40. /**
  41. * Constructor: this class is called from the the ajax call and handles invoking the correct
  42. * functionality on the server.
  43. */
  44. function PackageController(){
  45. $this->_pm = new PackageManager();
  46. }
  47. function performBasicSearch(){
  48. $json = getJSONobj();
  49. $search_term = '';
  50. $node_id = '';
  51. if(isset($_REQUEST['search_term'])) {
  52. $search_term = nl2br($_REQUEST['search_term']);
  53. }
  54. if(isset($_REQUEST['node_id'])) {
  55. $node_id = nl2br($_REQUEST['node_id']);
  56. }
  57. $xml = PackageManager::getPackages($node_id);
  58. echo 'result = ' . $json->encode(array('packages' => $xml));
  59. }
  60. /**
  61. * Retrieve a list of packages which belong to the corresponding category
  62. *
  63. * @param category_id this is passed via POST and is the category id of packages
  64. * we wish to retrieve
  65. * @return packages xml string consisting of the packages and releases which belong to
  66. * the category
  67. */
  68. function getPackages(){
  69. $json = getJSONobj();
  70. $category_id = '';
  71. if(isset($_REQUEST['category_id'])) {
  72. $category_id = nl2br($_REQUEST['category_id']);
  73. }
  74. $xml = PackageManager::getPackages($category_id);
  75. echo 'result = ' . $json->encode(array('package_output' => $xml));
  76. }
  77. /**
  78. * Obtain a list of releases from the server. This function is currently used for generating the patches/langpacks for upgrade wizard
  79. * as well as during installation
  80. */
  81. function getReleases(){
  82. $json = getJSONobj();
  83. $category_id = '';
  84. $package_id = '';
  85. $types = '';
  86. if(isset($_REQUEST['category_id'])) {
  87. $category_id = nl2br($_REQUEST['category_id']);
  88. }
  89. if(isset($_REQUEST['package_id'])) {
  90. $package_id = nl2br($_REQUEST['package_id']);
  91. }
  92. if(isset($_REQUEST['types'])) {
  93. $types = nl2br($_REQUEST['types']);
  94. }
  95. $types = explode(',', $types);
  96. $filter = array();
  97. $count = count($types);
  98. $index = 1;
  99. $type_str = '';
  100. foreach($types as $type){
  101. $type_str .= "'".$type."'";
  102. if($index < $count)
  103. $type_str .= ",";
  104. $index++;
  105. }
  106. $filter = array('type' => $type_str);
  107. $filter = PackageManager::toNameValueList($filter);
  108. $releases = PackageManager::getReleases($category_id, $package_id, $filter);
  109. $nodes = array();
  110. $release_map = array();
  111. foreach($releases['packages'] as $release){
  112. $release = PackageManager::fromNameValueList($release);
  113. $nodes[] = array('description' => $release['description'], 'version' => $release['version'], 'build_number' => $release['build_number'], 'id' => $release['id']);
  114. $release_map[$release['id']] = array('package_id' => $release['package_id'], 'category_id' => $release['category_id']);
  115. }
  116. $_SESSION['ML_PATCHES'] = $release_map;
  117. echo 'result = ' . $json->encode(array('releases' => $nodes));
  118. }
  119. /**
  120. * Obtain a promotion from the depot
  121. */
  122. function getPromotion(){
  123. $json = getJSONobj();
  124. $header = PackageManager::getPromotion();
  125. echo 'result = ' . $json->encode(array('promotion' => $header));
  126. }
  127. /**
  128. * Download the given release
  129. *
  130. * @param category_id this is passed via POST and is the category id of the release we wish to download
  131. * @param package_id this is passed via POST and is the package id of the release we wish to download
  132. * @param release_id this is passed via POST and is the release id of the release we wish to download
  133. * @return bool true is successful in downloading, false otherwise
  134. */
  135. function download(){
  136. global $sugar_config;
  137. $json = getJSONobj();
  138. $package_id = '';
  139. $category_id = '';
  140. $release_id = '';
  141. if(isset($_REQUEST['package_id'])) {
  142. $package_id = nl2br($_REQUEST['package_id']);
  143. }
  144. if(isset($_REQUEST['category_id'])) {
  145. $category_id = nl2br($_REQUEST['category_id']);
  146. }
  147. if(isset($_REQUEST['release_id'])) {
  148. $release_id = nl2br($_REQUEST['release_id']);
  149. }
  150. $GLOBALS['log']->debug("PACKAGE ID: ".$package_id);
  151. $GLOBALS['log']->debug("CATEGORY ID: ".$category_id);
  152. $GLOBALS['log']->debug("RELEASE ID: ".$release_id);
  153. $result = $this->_pm->download($category_id, $package_id, $release_id);
  154. $GLOBALS['log']->debug("RESULT: ".print_r($result,true));
  155. $success = 'false';
  156. if($result != null){
  157. $GLOBALS['log']->debug("Performing Setup");
  158. $this->_pm->performSetup($result, 'module', false);
  159. $GLOBALS['log']->debug("Complete Setup");
  160. $success = 'true';
  161. }
  162. echo 'result = ' . $json->encode(array('success' => $success));
  163. }
  164. /**
  165. * Retrieve a list of categories that are subcategories to the selected category
  166. *
  167. * @param id - the id of the parent_category, -1 if this is the root
  168. * @return array - a list of categories/nodes which are underneath this node
  169. */
  170. function getCategories(){
  171. $json = getJSONobj();
  172. $node_id = '';
  173. if(isset($_REQUEST['category_id'])) {
  174. $node_id = nl2br($_REQUEST['category_id']);
  175. }
  176. $GLOBALS['log']->debug("NODE ID: ".$node_id);
  177. $nodes = PackageManager::getCategories($node_id);
  178. echo 'result = ' . $json->encode(array('nodes' => $nodes));
  179. }
  180. function getNodes(){
  181. $json = getJSONobj();
  182. $category_id = '';
  183. if(isset($_REQUEST['category_id'])) {
  184. $category_id = nl2br($_REQUEST['category_id']);
  185. }
  186. $GLOBALS['log']->debug("CATEGORY ID: ".$category_id);
  187. $nodes = PackageManager::getModuleLoaderCategoryPackages($category_id);
  188. $GLOBALS['log']->debug(var_export($nodes, true));
  189. echo 'result = ' . $json->encode(array('nodes' => $nodes));
  190. }
  191. /**
  192. * Check the SugarDepot for updates for the given type as passed in via POST
  193. * @param type the type to check for
  194. * @return array return an array of releases for each given installed object if an update is found
  195. */
  196. function checkForUpdates(){
  197. $json = getJSONobj();
  198. $type = '';
  199. if(isset($_REQUEST['type'])) {
  200. $type = nl2br($_REQUEST['type']);
  201. }
  202. $pm = new PackageManager();
  203. $updates = $pm->checkForUpdates();
  204. $nodes = array();
  205. $release_map = array();
  206. if(!empty($updates)){
  207. foreach($updates as $update){
  208. $update = PackageManager::fromNameValueList($update);
  209. $nodes[] = array('label' => $update['name'], 'description' => $update['description'], 'version' => $update['version'], 'build_number' => $update['build_number'], 'id' => $update['id'], 'type' => $update['type']);
  210. $release_map[$update['id']] = array('package_id' => $update['package_id'], 'category_id' => $update['category_id'], 'type' => $update['type']);
  211. }
  212. }
  213. //patches
  214. $filter = array(array('name' => 'type', 'value' => "'patch'"));
  215. $releases = $pm->getReleases('', '', $filter);
  216. if(!empty($releases['packages'])){
  217. foreach($releases['packages'] as $update){
  218. $update = PackageManager::fromNameValueList($update);
  219. $nodes[] = array('label' => $update['name'], 'description' => $update['description'], 'version' => $update['version'], 'build_number' => $update['build_number'], 'id' => $update['id'], 'type' => $update['type']);
  220. $release_map[$update['id']] = array('package_id' => $update['package_id'], 'category_id' => $update['category_id'], 'type' => $update['type']);
  221. }
  222. }
  223. $_SESSION['ML_PATCHES'] = $release_map;
  224. echo 'result = ' . $json->encode(array('updates' => $nodes));
  225. }
  226. function getLicenseText(){
  227. $json = getJSONobj();
  228. $file = '';
  229. if(isset($_REQUEST['file'])) {
  230. $file = hashToFile($_REQUEST['file']);
  231. }
  232. $GLOBALS['log']->debug("FILE : ".$file);
  233. echo 'result = ' . $json->encode(array('license_display' => PackageManagerDisplay::buildLicenseOutput($file)));
  234. }
  235. /**
  236. * build the list of modules that are currently in the staging area waiting to be installed
  237. */
  238. function getPackagesInStaging(){
  239. $packages = $this->_pm->getPackagesInStaging('module');
  240. $json = getJSONobj();
  241. echo 'result = ' . $json->encode(array('packages' => $packages));
  242. }
  243. /**
  244. * build the list of modules that are currently in the staging area waiting to be installed
  245. */
  246. function performInstall(){
  247. $file = '';
  248. if(isset($_REQUEST['file'])) {
  249. $file = hashToFile($_REQUEST['file']);
  250. }
  251. if(!empty($file)){
  252. $this->_pm->performInstall($file);
  253. }
  254. $json = getJSONobj();
  255. echo 'result = ' . $json->encode(array('result' => 'success'));
  256. }
  257. function authenticate(){
  258. $json = getJSONobj();
  259. $username = '';
  260. $password = '';
  261. $servername = '';
  262. $terms_checked = '';
  263. if(isset($_REQUEST['username'])) {
  264. $username = nl2br($_REQUEST['username']);
  265. }
  266. if(isset($_REQUEST['password'])) {
  267. $password = nl2br($_REQUEST['password']);
  268. }
  269. if(isset($_REQUEST['servername'])) {
  270. $servername = $_REQUEST['servername'];
  271. }
  272. if(isset($_REQUEST['terms_checked'])) {
  273. $terms_checked = $_REQUEST['terms_checked'];
  274. if($terms_checked == 'on')
  275. $terms_checked = true;
  276. }
  277. if(!empty($username) && !empty($password)){
  278. $password = md5($password);
  279. $result = PackageManager::authenticate($username, $password, $servername, $terms_checked);
  280. if(!is_array($result) && $result == true)
  281. $status = 'success';
  282. else
  283. $status = $result['faultstring'];
  284. }else{
  285. $status = 'failed';
  286. }
  287. echo 'result = ' . $json->encode(array('status' => $status));
  288. }
  289. function getDocumentation(){
  290. $json = getJSONobj();
  291. $package_id = '';
  292. $release_id = '';
  293. if(isset($_REQUEST['package_id'])) {
  294. $package_id = nl2br($_REQUEST['package_id']);
  295. }
  296. if(isset($_REQUEST['release_id'])) {
  297. $release_id = nl2br($_REQUEST['release_id']);
  298. }
  299. $documents = PackageManager::getDocumentation($package_id, $release_id);
  300. $GLOBALS['log']->debug("DOCUMENTS: ".var_export($documents, true));
  301. echo 'result = ' . $json->encode(array('documents' => $documents));
  302. }
  303. function downloadedDocumentation(){
  304. $json = getJSONobj();
  305. $document_id = '';
  306. if(isset($_REQUEST['document_id'])) {
  307. $document_id = nl2br($_REQUEST['document_id']);
  308. }
  309. $GLOBALS['log']->debug("Downloading Document: ".$document_id);
  310. PackageManagerComm::downloadedDocumentation($document_id);
  311. echo 'result = ' . $json->encode(array('result' => 'true'));
  312. }
  313. /**
  314. * Remove metadata files such as foo-manifest
  315. * Enter description here ...
  316. * @param unknown_type $file
  317. * @param unknown_type $meta
  318. */
  319. protected function rmMetaFile($file, $meta)
  320. {
  321. $metafile = pathinfo($file, PATHINFO_DIRNAME)."/". pathinfo($file, PATHINFO_FILENAME)."-$meta.php";
  322. if(file_exists($metafile)) {
  323. unlink($metafile);
  324. }
  325. }
  326. function remove(){
  327. $json = getJSONobj();
  328. $file = '';
  329. if(isset($_REQUEST['file'])) {
  330. $file = urldecode(hashToFile($_REQUEST['file']));
  331. }
  332. $GLOBALS['log']->debug("FILE TO REMOVE: ".$file);
  333. if(!empty($file)){
  334. unlink($file);
  335. foreach(array("manifest", "icon") as $meta) {
  336. $this->rmMetaFile($file, $meta);
  337. }
  338. }
  339. echo 'result = ' . $json->encode(array('result' => 'true'));
  340. }
  341. }
  342. ?>