PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/admin/helpers/project/template/helper.php

https://github.com/elkuku/EasyCreator
PHP | 404 lines | 237 code | 78 blank | 89 comment | 38 complexity | d696e91cf6193c083026da584560cef5 MD5 | raw file
  1. <?php defined('_JEXEC') || die('=;)');
  2. /**
  3. * @package EasyCreator
  4. * @subpackage Helpers
  5. * @author Nikolai Plath
  6. * @author Created on 17-Oct-2009
  7. * @license GNU/GPL, see JROOT/LICENSE.php
  8. */
  9. /**
  10. * EasyCreator template helper.
  11. *
  12. * @package EasyCreator
  13. */
  14. class EcrProjectTemplateHelper
  15. {
  16. /**
  17. * Gets a list of installed templates.
  18. *
  19. * @return array Indexed array with template type as key ans folder name as value
  20. */
  21. public static function getTemplateList()
  22. {
  23. static $list = array();
  24. if(count($list))
  25. return $list;
  26. $types = JFolder::folders(ECRPATH_EXTENSIONTEMPLATES);
  27. foreach($types as $tplType)
  28. {
  29. if($tplType == 'parts'
  30. || $tplType == 'std'
  31. || $tplType == 'autocodes'
  32. )
  33. continue;
  34. $templates = JFolder::folders(ECRPATH_EXTENSIONTEMPLATES.DS.$tplType);
  35. foreach($templates as $tplName)
  36. {
  37. $info = self::getTemplateInfo($tplType, $tplName);
  38. if(!$info)
  39. {
  40. continue;
  41. }
  42. $list[$tplType][$info->folder] = $info;
  43. }
  44. }
  45. return $list;
  46. }
  47. /**
  48. * Gets Information about a specific template.
  49. *
  50. * @param string $tplType Template type
  51. * @param string $tplName Template name
  52. * @param string $basePath
  53. *
  54. * @since 0.0.1
  55. *
  56. * @return object|boolean stdClass Template info false on invalid template.
  57. */
  58. public static function getTemplateInfo($tplType, $tplName, $basePath = ECRPATH_EXTENSIONTEMPLATES)
  59. {
  60. if(false == JFile::exists($basePath.DS.$tplType.DS.$tplName.DS.'manifest.xml'))
  61. {
  62. return false;
  63. }
  64. $xml = EcrProjectHelper::getXML($basePath.DS.$tplType.DS.$tplName.DS.'manifest.xml');
  65. $info = new stdClass;
  66. $info->folder = $tplName;
  67. $info->name = (string)$xml->name;
  68. $info->description = jgettext((string)$xml->description);
  69. $info->version = (string)$xml->version;
  70. $info->jVersion = (string)$xml->jVersion;
  71. $info->phpVersion = (string)$xml->phpVersion;
  72. $info->dbTables = (string)$xml->dbTables;
  73. $info->author = (string)$xml->author;
  74. $info->authorUrl = (string)$xml->authorUrl;
  75. $info->complements = array();
  76. if(isset($xml->complements->complement))
  77. {
  78. foreach($xml->complements->complement as $complement)
  79. {
  80. $c = new stdClass;
  81. $c->folder = (string)$complement->folder;
  82. $c->version = (string)$complement->version;
  83. $c->targetDir = (string)$complement->targetDir;
  84. $info->complements[] = $c;
  85. }
  86. }
  87. $info->info = '';
  88. $info->info .= jgettext(ucfirst($tplType)).' '.$info->name.' '.$info->version.'::'.$info->description;
  89. $info->info .= ($info->author) ? '<br /><span style=\'color: blue;\'>Author:</span> '.$info->author : '';
  90. $info->info .= '<br /><strong>Joomla!:</strong> '.$info->jVersion;
  91. $info->info .= '<br /><strong>PHP:</strong> '.$info->phpVersion;
  92. $info->info .= ($info->dbTables)
  93. ? '<br /><span style=\'color: orange;\'>dbTables:</span> '.$info->dbTables
  94. : '';
  95. $info->info .= '<br />ECR Folder: '.$info->folder;
  96. return $info;
  97. }
  98. /**
  99. * Install templates.
  100. *
  101. * @since 0.0.1
  102. *
  103. * @throws Exception
  104. *
  105. * @return array with installs and errors.
  106. */
  107. public static function installPackage(array $package)
  108. {
  109. if($package['type'] != 'ecrextensiontemplate')
  110. {
  111. throw new Exception(jgettext('This is not an EasyCreator Extension Template'));
  112. }
  113. $result = array(
  114. 'installs' => array(),
  115. 'errors' => array(),
  116. );
  117. $types = (JFolder::folders($package['extractdir']));
  118. foreach($types as $type)
  119. {
  120. JFolder::create(ECRPATH_EXTENSIONTEMPLATES.DS.$type);
  121. $templates = JFolder::folders($package['extractdir'].DS.$type);
  122. foreach($templates as $template)
  123. {
  124. //-- Check for previous install - no upgrade yet..
  125. if(JFolder::exists(ECRPATH_EXTENSIONTEMPLATES.DS.$type.DS.$template))
  126. {
  127. $compare = self::compareVersions($type, $template, $package['extractdir']);
  128. switch ($compare) {
  129. case -1:
  130. // Installed is lower
  131. $result['installs'][] = "Updated: $type - $template";
  132. break;
  133. case 0:
  134. // Same version
  135. $result['errors'][] = "Same version: $type - $template";
  136. continue 2;
  137. break;
  138. case 1:
  139. // Uploaded is lower
  140. $result['errors'][] = "Installed is newer: $type - $template";
  141. continue 2;
  142. break;
  143. }
  144. }
  145. else
  146. {
  147. $result['installs'][] = "Installed: $type - $template";
  148. }
  149. //-- Create template dir
  150. JFolder::create(ECRPATH_EXTENSIONTEMPLATES.DS.$type.DS.$template);
  151. //-- Create the folders
  152. $folders = JFolder::folders($package['extractdir'].DS.$type.DS.$template, '.', true, true);
  153. foreach($folders as $folder)
  154. {
  155. $f = str_replace('/', DIRECTORY_SEPARATOR, $folder);
  156. $s = str_replace($package['extractdir'].DS.$type.DS.$template.DS, '', $f);
  157. if(false == JFolder::create(ECRPATH_EXTENSIONTEMPLATES.DS.$type.DS.$template.DS.$s))
  158. {
  159. throw new Exception(sprintf(jgettext('Can not create folder %s'), $folder));
  160. }
  161. }
  162. //-- Copy the files
  163. $files = JFolder::files($package['extractdir'].DS.$type.DS.$template, '.', true, true);
  164. foreach($files as $file)
  165. {
  166. $f = str_replace('/', DIRECTORY_SEPARATOR, $file);
  167. $s = str_replace($package['extractdir'].DS.$type.DS.$template.DS, '', $f);
  168. if(false == JFile::copy($file, ECRPATH_EXTENSIONTEMPLATES.DS.$type.DS.$template.DS.$s))
  169. {
  170. throw new Exception(sprintf(jgettext('Can not copy file %s'), $s));
  171. }
  172. }
  173. }
  174. }
  175. return $result;
  176. }
  177. /**
  178. * Export templates to a tar.gz package.
  179. *
  180. * @param array $exports Index array of templates to export
  181. * @param string $zipName
  182. *
  183. * @return string The name of the archive created.
  184. * @throws Exception
  185. * @since 0.0.1
  186. *
  187. */
  188. public static function exportTemplates($exports, $zipName = '')
  189. {
  190. $tempDir = JFactory::getConfig()->get('tmp_path').DS.uniqid('templateexport');
  191. $files = array();
  192. foreach($exports as $type => $folders)
  193. {
  194. foreach($folders as $folder)
  195. {
  196. $fileList = JFolder::files(ECRPATH_EXTENSIONTEMPLATES.DS.$type.DS.$folder, '.', true, true);
  197. foreach($fileList as $path)
  198. {
  199. $path = str_replace(ECRPATH_EXTENSIONTEMPLATES.DS, '', $path);
  200. if(false == JFolder::exists(dirname($tempDir.DS.$path)))
  201. JFolder::create(dirname($tempDir.DS.$path));
  202. if(false == JFile::copy(ECRPATH_EXTENSIONTEMPLATES.DS.$path, $tempDir.DS.$path))
  203. throw new Exception(sprintf(jgettext('Unable to copy the file %s to %s')
  204. , ECRPATH_EXTENSIONTEMPLATES.DS.$path, $tempDir.DS.$path));
  205. $files[] = $tempDir.DS.$path;
  206. }
  207. }
  208. }
  209. $xml = new SimpleXMLElement('<extension type="ecrextensiontemplate" version="'.ECR_VERSION.'"/>');
  210. $doc = new DOMDocument('1.0', 'utf-8');
  211. $doc->formatOutput = true;
  212. $domnode = dom_import_simplexml($xml);
  213. $domnode = $doc->importNode($domnode, true);
  214. $domnode = $doc->appendChild($domnode);
  215. $result = $doc->saveXML();
  216. if(false == JFile::write($tempDir.DS.'manifest.xml', $result))
  217. throw new Exception(sprintf(jgettext('Unable to write file %s'), $tempDir.DS.'manifest.xml'));
  218. $files[] = $tempDir.DS.'manifest.xml';
  219. $fileName = $zipName ? : 'ecr_extension_templates'.date('Ymd_His');
  220. $fileName .= '.zip';
  221. if( ! JFolder::create(ECRPATH_EXPORTS.DS.'templates'))
  222. throw new Exception(sprintf(jgettext('Unable to create the folder %s'), ECRPATH_EXPORTS.DS.'templates'));
  223. $result = EcrArchive::createZip(ECRPATH_EXPORTS.DS.'templates'.DS.$fileName, $files, $tempDir);
  224. //-- This means error
  225. if( ! $result->listContent())
  226. throw new Exception(jgettext('Error creating archive'));
  227. return $fileName;
  228. }
  229. /**
  230. * Upload and unpack a package file.
  231. *
  232. * @since 0.0.1
  233. *
  234. * @throws Exception
  235. * @return mixed array the package on success | boolean false on error
  236. */
  237. public static function installPackageFromUpload()
  238. {
  239. //-- Get the uploaded file information
  240. $userfile = JFactory::getApplication()->input->files->get('install_package', null, 'raw');
  241. //-- If there is no uploaded file, we have a problem...
  242. if(false == is_array($userfile))
  243. throw new Exception(jgettext('No file selected'));
  244. //-- Check if there was a problem uploading the file.
  245. if($userfile['error'] || $userfile['size'] < 1)
  246. throw new Exception(jgettext('Invalid package'));
  247. //-- Build the appropriate paths
  248. $tmp_src = $userfile['tmp_name'];
  249. $tmp_dest = JFactory::getConfig()->get('tmp_path').DS.$userfile['name'];
  250. //-- Move uploaded file
  251. JFile::upload($tmp_src, $tmp_dest, false, true);
  252. //-- Unpack the downloaded package file
  253. $package = JInstallerHelper::unpack($tmp_dest);
  254. if(false == $package)
  255. throw new Exception(jgettext('Unable to find install package'));
  256. return self::installPackage($package);
  257. }
  258. /**
  259. * Install a package from a WEB repository.
  260. *
  261. * @param $url
  262. *
  263. * @since 0.0.25.6
  264. *
  265. * @throws Exception
  266. *
  267. * @return array
  268. */
  269. public static function installPackageFromWeb($url)
  270. {
  271. $ch = curl_init();
  272. curl_setopt($ch, CURLOPT_URL, $url);
  273. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  274. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  275. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  276. $data = curl_exec ($ch);
  277. $error = curl_error($ch);
  278. curl_close ($ch);
  279. if ($error)
  280. {
  281. throw new Exception($error);
  282. }
  283. $destination = JPATH_ROOT . '/tmp/' . substr($url, strrpos($url, '/') + 1, strlen($url));
  284. $file = fopen($destination, "w+");
  285. fputs($file, $data);
  286. fclose($file);
  287. //-- Unpack the downloaded package file
  288. $package = JInstallerHelper::unpack($destination);
  289. if(false == $package)
  290. {
  291. throw new Exception(jgettext('Unable to find install package'));
  292. }
  293. return self::installPackage($package);
  294. }
  295. /**
  296. * Get extended replacement information.
  297. *
  298. * @since 0.0.1
  299. *
  300. * @return array
  301. */
  302. public static function getReplacementInfo()
  303. {
  304. $reflector = new ReflectionClass('EcrProjectReplacement');
  305. $info = array();
  306. $blacks = array('customs', 'priorities');
  307. foreach($reflector->getProperties() as $property)
  308. {
  309. if(in_array((string)$property->getName(), $blacks))
  310. continue;
  311. $comment = $property->getDocComment();
  312. $comment = str_replace('@var string', '', $comment);
  313. $comment = trim($comment, '/*\n ');
  314. $comment = trim($comment);
  315. $comment = trim($comment, '* ');
  316. $info[$property->getName()] = $comment;
  317. }
  318. return $info;
  319. }
  320. private static function compareVersions($type, $template, $basePath2)
  321. {
  322. $info1 = self::getTemplateInfo($type, $template);
  323. $info2 = self::getTemplateInfo($type, $template, $basePath2);
  324. return version_compare($info1->version, $info2->version);
  325. }
  326. }