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

/applications/dashboard/controllers/class.utilitycontroller.php

https://github.com/abhishekmica/Garden
PHP | 278 lines | 209 code | 28 blank | 41 comment | 53 complexity | 90193bf9e59204a8491e073f6a093844 MD5 | raw file
  1. <?php if (!defined('APPLICATION')) exit();
  2. /*
  3. Copyright 2008, 2009 Vanilla Forums Inc.
  4. This file is part of Garden.
  5. Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  6. Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
  8. Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
  9. */
  10. class UtilityController extends DashboardController {
  11. public $Uses = array('Form');
  12. public function Sort() {
  13. $Session = Gdn::Session();
  14. $TransientKey = GetPostValue('TransientKey', '');
  15. $Target = GetPostValue('Target', '');
  16. if ($Session->ValidateTransientKey($TransientKey)) {
  17. $TableID = GetPostValue('TableID', FALSE);
  18. if ($TableID) {
  19. $Rows = GetPostValue($TableID, FALSE);
  20. if (is_array($Rows)) {
  21. try {
  22. $Table = str_replace('Table', '', $TableID);
  23. $TableModel = new Gdn_Model($Table);
  24. foreach ($Rows as $Sort => $ID) {
  25. $TableModel->Update(array('Sort' => $Sort), array($Table.'ID' => $ID));
  26. }
  27. } catch (Exception $ex) {
  28. $this->Form->AddError($ex->getMessage());
  29. }
  30. }
  31. }
  32. }
  33. if ($this->DeliveryType() != DELIVERY_TYPE_BOOL)
  34. Redirect($Target);
  35. $this->Render();
  36. }
  37. /**
  38. * Allows the setting of data into one of two serialized data columns on the
  39. * user table: Preferences and Attributes. The method expects "Name" &
  40. * "Value" to be in the $_POST collection. This method always saves to the
  41. * row of the user id performing this action (ie. $Session->UserID). The
  42. * type of property column being saved should be specified in the url:
  43. * ie. /dashboard/utility/set/preference/name/value/transientKey
  44. * or /dashboard/utility/set/attribute/name/value/transientKey
  45. *
  46. * @param string The type of value being saved: preference or attribute.
  47. * @param string The name of the property being saved.
  48. * @param string The value of the property being saved.
  49. * @param string A unique transient key to authenticate that the user intended to perform this action.
  50. */
  51. public function Set($UserPropertyColumn = '', $Name = '', $Value = '', $TransientKey = '') {
  52. $this->_DeliveryType = DELIVERY_TYPE_BOOL;
  53. $Session = Gdn::Session();
  54. $Success = FALSE;
  55. if (
  56. in_array($UserPropertyColumn, array('preference', 'attribute'))
  57. && $Name != ''
  58. && $Value != ''
  59. && $Session->UserID > 0
  60. && $Session->ValidateTransientKey($TransientKey)
  61. ) {
  62. $UserModel = Gdn::Factory("UserModel");
  63. $Method = $UserPropertyColumn == 'preference' ? 'SavePreference' : 'SaveAttribute';
  64. $Success = $UserModel->$Method($Session->UserID, $Name, $Value) ? 'TRUE' : 'FALSE';
  65. }
  66. if (!$Success)
  67. $this->Form->AddError('ErrorBool');
  68. // Redirect back where the user came from if necessary
  69. if ($this->_DeliveryType == DELIVERY_TYPE_ALL)
  70. Redirect($_SERVER['HTTP_REFERER']);
  71. else
  72. $this->Render();
  73. }
  74. public function Structure($AppName = 'all', $CaptureOnly = '1', $Drop = '0', $Explicit = '0') {
  75. $this->Permission('Garden.Settings.Manage');
  76. $Files = array();
  77. $AppName = $AppName == '' ? 'all': $AppName;
  78. if ($AppName == 'all') {
  79. // Load all application structure files.
  80. $ApplicationManager = new Gdn_ApplicationManager();
  81. $Apps = $ApplicationManager->EnabledApplications();
  82. $AppNames = ConsolidateArrayValuesByKey($Apps, 'Folder');
  83. foreach ($AppNames as $AppName) {
  84. $Files[] = CombinePaths(array(PATH_APPLICATIONS, $AppName, 'settings', 'structure.php'), DS);
  85. }
  86. $AppName = 'all';
  87. } else {
  88. // Load that specific application structure file.
  89. $Files[] = CombinePaths(array(PATH_APPLICATIONS, $AppName, 'settings', 'structure.php'), DS);
  90. }
  91. $Validation = new Gdn_Validation();
  92. $Database = Gdn::Database();
  93. $Drop = $Drop == '0' ? FALSE : TRUE;
  94. $Explicit = $Explicit == '0' ? FALSE : TRUE;
  95. $CaptureOnly = !($CaptureOnly == '0');
  96. $Structure = Gdn::Structure();
  97. $Structure->CaptureOnly = $CaptureOnly;
  98. $SQL = Gdn::SQL();
  99. $SQL->CaptureModifications = $CaptureOnly;
  100. $this->SetData('CaptureOnly', $Structure->CaptureOnly);
  101. $this->SetData('Drop', $Drop);
  102. $this->SetData('Explicit', $Explicit);
  103. $this->SetData('ApplicationName', $AppName);
  104. $this->SetData('Status', '');
  105. $FoundStructureFile = FALSE;
  106. foreach ($Files as $File) {
  107. if (file_exists($File)) {
  108. $FoundStructureFile = TRUE;
  109. try {
  110. include($File);
  111. } catch (Exception $Ex) {
  112. $this->Form->AddError($Ex);
  113. }
  114. }
  115. }
  116. // Run the structure of all of the plugins.
  117. $Plugins = Gdn::PluginManager()->EnabledPlugins();
  118. foreach ($Plugins as $PluginKey => $Plugin) {
  119. $PluginInstance = Gdn::PluginManager()->GetPluginInstance($PluginKey, Gdn_PluginManager::ACCESS_PLUGINNAME);
  120. if (method_exists($PluginInstance, 'Structure'))
  121. $PluginInstance->Structure();
  122. }
  123. if (property_exists($Structure->Database, 'CapturedSql'))
  124. $this->SetData('CapturedSql', (array)$Structure->Database->CapturedSql);
  125. else
  126. $this->SetData('CapturedSql', array());
  127. if ($this->Form->ErrorCount() == 0 && !$CaptureOnly && $FoundStructureFile)
  128. $this->SetData('Status', 'The structure was successfully executed.');
  129. $this->AddSideMenu('dashboard/settings/configure');
  130. $this->AddCssFile('admin.css');
  131. $this->SetData('Title', T('Database Structure Upgrades'));
  132. $this->Render();
  133. }
  134. public function Update() {
  135. // Check for permission or flood control.
  136. // These settings are loaded/saved to the database because we don't want the config file storing non/config information.
  137. $Now = time();
  138. $LastTime = Gdn::Get('Garden.Update.LastTimestamp', 0);
  139. if ($LastTime + (60 * 60 * 24) > $Now) {
  140. // Check for flood control.
  141. $Count = Gdn::Get('Garden.Update.Count', 0) + 1;
  142. if ($Count > 5) {
  143. if (!Gdn::Session()->CheckPermission('Garden.Settings.Manage')) {
  144. // We are only allowing an update of 5 times every 24 hours.
  145. throw PermissionException();
  146. }
  147. }
  148. } else {
  149. $Count = 1;
  150. }
  151. Gdn::Set('Garden.Update.LastTimestamp', $Now);
  152. Gdn::Set('Garden.Update.Count', $Count);
  153. // Run the structure.
  154. $UpdateModel = new UpdateModel();
  155. $UpdateModel->RunStructure();
  156. $this->SetData('Success', TRUE);
  157. $this->MasterView = 'none';
  158. $this->Render();
  159. }
  160. // Because you cannot send xmlhttprequests across domains, we need to use
  161. // a proxy to check for updates.
  162. public function UpdateProxy() {
  163. $Fields = $_POST;
  164. foreach ($Fields as $Field => $Value) {
  165. if (get_magic_quotes_gpc()) {
  166. if (is_array($Value)) {
  167. $Count = count($Value);
  168. for ($i = 0; $i < $Count; ++$i) {
  169. $Value[$i] = stripslashes($Value[$i]);
  170. }
  171. } else {
  172. $Value = stripslashes($Value);
  173. }
  174. $Fields[$Field] = $Value;
  175. }
  176. }
  177. $UpdateCheckUrl = C('Garden.UpdateCheckUrl', 'http://vanillaforums.org/addons/update');
  178. echo ProxyRequest($UpdateCheckUrl.'?'.http_build_query($Fields));
  179. $Database = Gdn::Database();
  180. $Database->CloseConnection();
  181. }
  182. public function UpdateResponse() {
  183. // Get the message, response, and transientkey
  184. $Messages = TrueStripSlashes(GetValue('Messages', $_POST));
  185. $Response = TrueStripSlashes(GetValue('Response', $_POST));
  186. $TransientKey = GetIncomingValue('TransientKey', '');
  187. // If the key validates
  188. $Session = Gdn::Session();
  189. if ($Session->ValidateTransientKey($TransientKey)) {
  190. // If messages wasn't empty
  191. if ($Messages != '') {
  192. // Unserialize them & save them if necessary
  193. $Messages = Gdn_Format::Unserialize($Messages);
  194. if (is_array($Messages)) {
  195. $MessageModel = new MessageModel();
  196. foreach ($Messages as $Message) {
  197. // Check to see if it already exists, and if not, add it.
  198. if (is_object($Message))
  199. $Message = Gdn_Format::ObjectAsArray($Message);
  200. $Content = ArrayValue('Content', $Message, '');
  201. if ($Content != '') {
  202. $Data = $MessageModel->GetWhere(array('Content' => $Content));
  203. if ($Data->NumRows() == 0) {
  204. $MessageModel->Save(array(
  205. 'Content' => $Content,
  206. 'AllowDismiss' => ArrayValue('AllowDismiss', $Message, '1'),
  207. 'Enabled' => ArrayValue('Enabled', $Message, '1'),
  208. 'Application' => ArrayValue('Application', $Message, 'Dashboard'),
  209. 'Controller' => ArrayValue('Controller', $Message, 'Settings'),
  210. 'Method' => ArrayValue('Method', $Message, ''),
  211. 'AssetTarget' => ArrayValue('AssetTarget', $Message, 'Content'),
  212. 'CssClass' => ArrayValue('CssClass', $Message, '')
  213. ));
  214. }
  215. }
  216. }
  217. }
  218. }
  219. // Save some info to the configuration file
  220. $Save = array();
  221. // If the response wasn't empty, save it in the config
  222. if ($Response != '')
  223. $Save['Garden.RequiredUpdates'] = Gdn_Format::Unserialize($Response);
  224. // Record the current update check time in the config.
  225. $Save['Garden.UpdateCheckDate'] = time();
  226. SaveToConfig($Save);
  227. }
  228. }
  229. public function SetClientHour($ClientHour = '', $TransientKey = '') {
  230. $this->_DeliveryType = DELIVERY_TYPE_BOOL;
  231. $Session = Gdn::Session();
  232. $Success = FALSE;
  233. if (
  234. is_numeric($ClientHour)
  235. && $Session->UserID > 0
  236. && $Session->ValidateTransientKey($TransientKey)
  237. ) {
  238. $UserModel = Gdn::UserModel();
  239. $HourOffset = $ClientHour - date('G', time());
  240. $UserModel->Update(array('HourOffset' => $HourOffset), array('UserID' => $Session->UserID));
  241. $Success = TRUE;
  242. }
  243. $this->Render();
  244. }
  245. public function GetFeed($Type = 'news', $Length = 5, $FeedFormat = 'normal') {
  246. echo file_get_contents('http://vanillaforums.org/vforg/home/getfeed/'.$Type.'/'.$Length.'/'.$FeedFormat.'/?DeliveryType=VIEW');
  247. $this->DeliveryType(DELIVERY_TYPE_NONE);
  248. $this->Render();
  249. }
  250. }