PageRenderTime 27ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/3.0/modules/moduleupdates/controllers/admin_moduleupdates.php

https://github.com/tbleher/gallery3-contrib
PHP | 364 lines | 237 code | 49 blank | 78 comment | 71 complexity | 46083a96d119afe25a886f74e5560657 MD5 | raw file
  1. <?php defined("SYSPATH") or die("No direct script access.");/**
  2. * Gallery - a web based photo album viewer and editor
  3. * Copyright (C) 2000-2013 Bharat Mediratta
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. class Admin_Moduleupdates_Controller extends Admin_Controller {
  20. /**
  21. * Builds the backend information for the Module Updates page.
  22. *
  23. * Builds an array of data containing the needed information about an installed copy of Gallery3
  24. * to determine if versions stored in the GitHub are newer.
  25. *
  26. * List ID: The name of the folder the module resides in (obtained from module::available)
  27. * name: The given name of the module (obtained from module::available)
  28. * locked: If the module is considered locked by Gallery (obtained from module::available)
  29. * code_version: The version of the module in the modules directory (obtained from module::available)
  30. * active: If the module is installed and enabled (obtained from module::available)
  31. * version: The version installed and running (obtained from module::available)
  32. * description: The description of the module (obtained from module::available)
  33. * remote_version: The version of the code on GitHub (obtained from get_remote_module_version)
  34. * remote_server: The server the remote version is on (obtained from get_remote_module_version)
  35. * font_color: The color to display the update in depending on its status
  36. *
  37. * @author brentil <forums@inner-ninja.com>
  38. */
  39. protected $gm_ini;
  40. protected $gm_core_ini;
  41. public function __construct() {
  42. parent::__construct();
  43. $this->gm_ini = TMPPATH.'/gm.ini';
  44. $this->gm_core_ini = TMPPATH.'/gm_core.ini';
  45. }
  46. public function index() {
  47. //Start execution timer
  48. $bgtime=time();
  49. $view = new Admin_View("admin.html");
  50. $view->page_title = t("Gallery 3 :: Manage Module Updates");
  51. $view->content = new View("admin_moduleupdates.html");
  52. $view->content->mu_version = module::get_version("moduleupdates");
  53. $refreshCache = false;
  54. $cache = unserialize(Cache::instance()->get("moduleupdates_cache"));
  55. $cache_updates = unserialize(Cache::instance()->get("moduleupdates_cache_updates"));
  56. //if someone pressed the button to refresh now
  57. if (request::method() == "post") {
  58. access::verify_csrf();
  59. $cache = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
  60. $cache_updates = array("date" => "", "updates" => 0);
  61. $refreshCache = true;
  62. }else if(count($cache) < 1 or $cache_updates['date'] == ""){
  63. //if there are no items in the cache array or the update date is "" refresh the data
  64. $cache = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
  65. $cache_updates = array("date" => "", "updates" => 0);
  66. $refreshCache = true;
  67. }
  68. //Check the ability to access the Gallery3 GitHub
  69. $GitHub = null;
  70. try {
  71. $GitHub = fopen ("http://github.com", "r");
  72. if ($GitHub != null) {
  73. $GitHub = '<font color=green>Online</font>';
  74. }else{
  75. $GitHub = '<font color=red>Offline</font>';
  76. }
  77. }
  78. catch (Exception $e) {
  79. }
  80. //Check the ability to access the Google
  81. $Google = null;
  82. try {
  83. $Google = fopen ("http://google.com", "r");
  84. if ($Google != null) {
  85. $Google = '<font color=green>Online</font>';
  86. }else{
  87. $Google = '<font color=red>Offline</font>';
  88. }
  89. }
  90. catch (Exception $e) {
  91. }
  92. $update_count = 0;
  93. if($refreshCache == true){
  94. // Only poll GalleryModules.com once for the ini file.
  95. $fp = fopen($this->gm_ini, 'w');
  96. $fp2 = fopen($this->gm_core_ini,'w');
  97. if(function_exists("curl_init")) {
  98. $cp = curl_init("http://www.gallerymodules.com/gallerymodules.ini");
  99. curl_setopt($cp, CURLOPT_FILE, $fp);
  100. $buffer = curl_exec($cp);
  101. curl_close($cp);
  102. fclose($fp);
  103. $cp = curl_init("http://www.gallerymodules.com/core.ini");
  104. curl_setopt($cp, CURLOPT_FILE, $fp2);
  105. $buffer = curl_exec($cp);
  106. curl_close($cp);
  107. fclose($fp2);
  108. } else {
  109. fwrite($fp,file_get_contents("http://www.gallerymodules.com/gallerymodules.ini"));
  110. fclose($fp);
  111. fwrite($fp2,file_get_contents("http://www.gallerymodules.com/core.ini"));
  112. fclose($fp2);
  113. }
  114. foreach (module::available() as $this_module_name => $module_info) {
  115. $font_color_local = "black";
  116. $core_version = '';
  117. $core_server = '';
  118. $core_dlink = '';
  119. $font_color_core = "black";
  120. $contrib_version = '';
  121. $contrib_server = '';
  122. $contrib_dlink = '';
  123. $font_color_contrib = "black";
  124. $gh_version = '';
  125. $gh_server = '';
  126. $gh_dlink = '';
  127. $font_color_gh = "black";
  128. $font_color_local = $this->get_local_module_version_color ($module_info->version, $module_info->code_version);
  129. list ($core_version, $core_server) = $this->get_remote_module_version($this_module_name, "CORE");
  130. $font_color_core = $this->get_module_version_color ($module_info->version, $module_info->code_version, $core_version);
  131. if(!is_numeric($core_version)) {
  132. list ($gh_version, $gh_server) = $this->get_remote_module_version($this_module_name, "GH");
  133. $font_color_gh = $this->get_module_version_color ($module_info->version, $module_info->code_version, $gh_version);
  134. }
  135. if($font_color_core == "red" or $font_color_contrib == "red" or $font_color_gh == "red"){
  136. $update_count++;
  137. }
  138. $module_info->name = "<a href=\"http://codex.gallery2.org/Gallery3:Modules:".$this_module_name."\" target=\"_new\">".$module_info->name."</a>";
  139. if (is_numeric($core_version)) {
  140. if($core_version > $module_info->version) {
  141. $core_dlink = "http://github.com/gallery/gallery3/tree/master/modules/".$this_module_name;
  142. }
  143. }
  144. if (is_numeric($gh_version)) {
  145. if($gh_version > $module_info->version) {
  146. $this_gm_repo = str_replace(".","",substr_replace(gallery::VERSION,"",strpos(gallery::VERSION," ")));
  147. if($this_gm_repo == "30"){
  148. $gh_dlink = "http://www.gallerymodules.com/update/".$this_module_name;
  149. } else {
  150. $gh_dlink = "http://www.gallerymodules.com/update".$this_gm_repo."/".$this_module_name;
  151. }
  152. }
  153. }
  154. //populate the list fo modules and their data
  155. $cache->$this_module_name = array ("name" => $module_info->name, "locked" => $module_info->locked,
  156. "code_version" => $module_info->code_version, "active" => $module_info->active,
  157. "version" => $module_info->version,"description" => $module_info->description,
  158. "core_version" => $core_version, "core_server" => $core_server, "font_color_core" => $font_color_core,
  159. "contrib_version" => $contrib_version, "contrib_server" => $contrib_server, "font_color_contrib" => $font_color_contrib,
  160. "gh_version" => $gh_version, "gh_server" => $gh_server, "font_color_gh" => $font_color_gh,
  161. "font_color_local" => $font_color_local, "core_dlink" => $core_dlink, "contrib_dlink" => $contrib_dlink,
  162. "gh_dlink" => $gh_dlink);
  163. }
  164. //Define right now as YYYY.MM.DD HH:MM with the # of updates that are out of date
  165. $cache_updates = array("date" => date("Y.m.d - H:i"), "updates" => $update_count);
  166. //Write out the new data to cache with a 30 day expiration & 0 for update data so it's always present
  167. Cache::instance()->set("moduleupdates_cache", serialize($cache), array("ModuleUpdates"), 30*86400);
  168. Cache::instance()->set("moduleupdates_cache_updates", serialize($cache_updates), array("ModuleUpdates"), null);
  169. log::success("moduleupdates", t("Completed checking remote GitHub for modules updates."));
  170. }
  171. if(is_file($this->gm_ini))
  172. unlink($this->gm_ini);
  173. if(is_file($this->gm_core_ini))
  174. unlink($this->gm_core_ini);
  175. $view->content->vars = $cache;
  176. $view->content->update_time = $cache_updates['date'];
  177. $view->content->csrf = access::csrf_token();
  178. $view->content->Google = $Google;
  179. $view->content->GitHub = $GitHub;
  180. $view->content->Gallery_Version = substr_replace(gallery::VERSION,"",strpos(gallery::VERSION," "));
  181. //End execution timer
  182. $ExecutionTime = (time()-$bgtime);
  183. if ($ExecutionTime < 1) {
  184. $ExecutionTime = '<font color=green>1</font>';
  185. }else if ($ExecutionTime <= 30){
  186. $ExecutionTime = '<font color=green>' . $ExecutionTime . '</font>';
  187. }else if ($ExecutionTime <= 60){
  188. $ExecutionTime = '<font color=orange>' . $ExecutionTime . '</font>';
  189. }else{
  190. $ExecutionTime = '<font color=red>' . $ExecutionTime . '</font>';
  191. }
  192. $view->content->ExecutionTime = $ExecutionTime;
  193. print $view;
  194. }
  195. /**
  196. *
  197. **/
  198. private function get_module_version_color ($version, $code_version, $remote_version) {
  199. $font_color = "black";
  200. //BLACK - no module version detected
  201. if ($remote_version == "") {
  202. $font_color = "black";
  203. //BLUE - DNE: Does Not Exist, this module was not found
  204. } else if ($remote_version == "DNE") {
  205. $font_color = "blue";
  206. //GREEN - Your version is newer than the GitHub
  207. } else if ($remote_version < $code_version or ($version != ''
  208. and $remote_version < $version)) {
  209. $font_color = "green";
  210. //RED - Your version is older than the GitHub
  211. } else if ($remote_version > $code_version or ($version != ''
  212. and $remote_version > $version)) {
  213. $font_color = "red";
  214. }
  215. return $font_color;
  216. }
  217. /**
  218. *
  219. **/
  220. private function get_local_module_version_color ($version, $code_version) {
  221. $font_color = "black";
  222. //PINK - Your installed version is newer than file version
  223. if ($version != '' and $code_version < $version) {
  224. $font_color = "pink";
  225. //ORANGE - Your file version is newer than the installed version
  226. } else if ($version != '' and $code_version > $version) {
  227. $font_color = "orange";
  228. }
  229. return $font_color;
  230. }
  231. /**
  232. * Parses the known GitHub repositories for new versions of modules.
  233. *
  234. * Searches the remote GitHub repositories for a module with a like filename to that of the ones
  235. * installed in the running Gallery isntall. Reads the remote modules module.info file to
  236. * gather the version information. Uses the following locations;
  237. *
  238. * http://github.com/gallery/gallery3
  239. * http://www.gallerymodules.com
  240. *
  241. * @author brentil <forums@inner-ninja.com>
  242. * @param String - The folder name of the module to search for on the remote GitHub server
  243. * @param String - The remote server to check against
  244. * @return Array - An array with the remote module version and the server it was found on.
  245. */
  246. private function get_remote_module_version ($module_name, $server_location) {
  247. $version = '';
  248. $server = '';
  249. $file = null;
  250. switch ($server_location) {
  251. case "CORE":
  252. //Check the main Gallery3 GitHub
  253. if ($file == null) {
  254. try {
  255. if(file_exists($this->gm_core_ini)) {
  256. $file = 1;
  257. }
  258. if ($file != null) {
  259. $gm_core_array = parse_ini_file($this->gm_core_ini,true);
  260. $server = '(G)';
  261. }
  262. }
  263. catch (Exception $e) {
  264. }
  265. }
  266. break;
  267. case "GH":
  268. //Parse ini file from GalleryModules.com
  269. try {
  270. if(file_exists($this->gm_ini)) {
  271. $file = 1;
  272. }
  273. if ($file != null) {
  274. $gm_array = parse_ini_file($this->gm_ini,true);
  275. $server = '(GH)';
  276. }
  277. }
  278. catch (Exception $e) {
  279. echo $e;
  280. }
  281. break;
  282. }
  283. if ($file != null) {
  284. //Search in the GM listing
  285. if ($server_location == "GH"){
  286. //Search if this is a Gallery 3.0 module
  287. if(array_key_exists($module_name,$gm_array)){
  288. if(array_key_exists('g3',$gm_array[$module_name])){
  289. $version = $gm_array[$module_name]['g3'];
  290. }
  291. if($version == ''){
  292. if(array_key_exists('g31',$gm_array[$module_name])){
  293. $version = $gm_array[$module_name]['g31'];
  294. }
  295. }
  296. if($version == ''){
  297. if(array_key_exists('codex',$gm_array[$module_name])){
  298. $version = $gm_array[$module_name]['codex'];
  299. }
  300. }
  301. } else { //Module not found
  302. $version = '';
  303. }
  304. //Search in the Core listing
  305. } else {
  306. if(array_key_exists($module_name,$gm_core_array)){
  307. $version = $gm_core_array[$module_name]['version'];
  308. } else { //Module not found
  309. $version = '';
  310. }
  311. }
  312. }
  313. return array ($version, $server);
  314. }
  315. }