PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/base/include/adminsettings_include.php

https://gitlab.com/nexxuz/phpBMS
PHP | 401 lines | 192 code | 103 blank | 106 comment | 29 complexity | 069e54cfc0e8b5154c6ce02d6fe22bc5 MD5 | raw file
  1. <?php
  2. /*
  3. $Rev$ | $LastChangedBy$
  4. $LastChangedDate$
  5. +-------------------------------------------------------------------------+
  6. | Copyright (c) 2004 - 2010, Kreotek LLC |
  7. | All rights reserved. |
  8. +-------------------------------------------------------------------------+
  9. | |
  10. | Redistribution and use in source and binary forms, with or without |
  11. | modification, are permitted provided that the following conditions are |
  12. | met: |
  13. | |
  14. | - Redistributions of source code must retain the above copyright |
  15. | notice, this list of conditions and the following disclaimer. |
  16. | |
  17. | - Redistributions in binary form must reproduce the above copyright |
  18. | notice, this list of conditions and the following disclaimer in the |
  19. | documentation and/or other materials provided with the distribution. |
  20. | |
  21. | - Neither the name of Kreotek LLC nor the names of its contributore may |
  22. | be used to endorse or promote products derived from this software |
  23. | without specific prior written permission. |
  24. | |
  25. | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  26. | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  27. | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
  28. | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
  29. | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
  30. | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
  31. | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
  32. | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
  33. | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
  34. | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
  35. | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
  36. | |
  37. +-------------------------------------------------------------------------+
  38. */
  39. /**
  40. * Handles saving of settings for phpBMS
  41. *
  42. * Processes and retrieves settings from the settings table.
  43. * @author Brian Rieb <brieb@kreotek.com>
  44. */
  45. class settings{
  46. /**
  47. * $db
  48. *
  49. * @var object phpBMS database object
  50. */
  51. var $db;
  52. /**
  53. * $updateErrors
  54. *
  55. * @var string Error message to be displayed upon errored update.
  56. */
  57. var $updateErrorMessage = "";
  58. /**
  59. * Constructor sets up {@link $db}
  60. */
  61. function settings($db){
  62. $this->db = $db;
  63. }//end function constructor
  64. /**
  65. * Retrieves settings from database and populates array with them
  66. *
  67. */
  68. function getSettings(){
  69. $therecord = array();
  70. $querystatement = "
  71. SELECT
  72. `name`,
  73. `value`
  74. FROM
  75. `settings`";
  76. $queryresult = $this->db->query($querystatement);
  77. while($setting = $this->db->fetchArray($queryresult))
  78. $therecord[$setting["name"]] = $setting["value"];
  79. return $therecord;
  80. }//end function getSettings
  81. /**
  82. * Updates phpBMS settings
  83. *
  84. * Updates the phpBMS settings, including the logo graphic
  85. *
  86. * @param array $variables variables passed from the form
  87. * @global object phpbms
  88. */
  89. function updateSettings($variables){
  90. global $phpbms;
  91. if(!isset($variables["persistent_login"]))
  92. $variables["persistent_login"] = 0;
  93. if(!isset($variables["auto_check_update"]))
  94. $variables["auto_check_update"] = 0;
  95. if(!isset($variables["send_metrics"]))
  96. $variables["send_metrics"] = 0;
  97. //include any procesing that needs to be done by modules
  98. foreach($phpbms->modules as $module => $moduleinfo){
  99. if($module != "base"){
  100. if(class_exists($module."Update")){
  101. $class = $module."Update";
  102. $extraUpdate = new $class($this->db);
  103. $variables = $extraUpdate->updateSettings($variables);
  104. if(isset($extraUpdate->updateErrorMessage))
  105. if($extraUpdate->updateErrorMessage)
  106. $this->updateErrorMessage = $extraUpdate->updateErrorMessage;
  107. }//end if
  108. }//end if
  109. }//end foreach
  110. // Update the settings records
  111. foreach($variables as $settingname => $settingvalue){
  112. if(defined(strtoupper($settingname))){
  113. if(constant(strtoupper($settingname)) != $settingvalue){
  114. $updatestatement = "
  115. UPDATE
  116. settings
  117. SET
  118. value ='".$settingvalue."'
  119. WHERE
  120. name='".mysql_real_escape_string($settingname)."'";
  121. $updateresult = $this->db->query($updatestatement);
  122. if(!$this->db->affectedRows()){
  123. //check to see why the update did not work
  124. $querystatement = "
  125. SELECT
  126. name
  127. FROM
  128. settings
  129. WHERE
  130. name = '".mysql_real_escape_string($settingname)."'";
  131. $queryresult = $this->db->query($querystatement);
  132. if(!$this->db->numRows($queryresult)){
  133. //insert the setting if need be
  134. $insertstatement ="
  135. INSERT INTO
  136. settings (
  137. `value`,
  138. `name`,
  139. ) VALUES (
  140. '".$settingvalue."',
  141. '".mysql_real_escape_string($settingname)."'
  142. )";
  143. $this->db-query($insertstatement);
  144. }//end if
  145. }//end if
  146. }//end if
  147. }//endif
  148. }//end foreach
  149. // deal with logo graphic.
  150. if(isset($_FILES["printedlogo"])){
  151. $validFileTypes = array(
  152. "image/png",
  153. "image/x-png",
  154. "image/jpg",
  155. "image/jpeg",
  156. "imagep/jpeg",
  157. );
  158. if(in_array($_FILES["printedlogo"]["type"], $validFileTypes)){
  159. if (function_exists('file_get_contents')) {
  160. $file = mysql_real_escape_string(file_get_contents($_FILES['printedlogo']['tmp_name']));
  161. } else {
  162. // If using PHP < 4.3.0 use the following:
  163. $file = mysql_real_escape_string(fread(fopen($_FILES['printedlogo']['tmp_name'], 'r'), filesize($_FILES['printedlogo']['tmp_name'])));
  164. }//endif
  165. if($_FILES["printedlogo"]["type"] == "image/jpeg")
  166. $name = "logo.jpg";
  167. else
  168. $name = "logo.png";
  169. $updatestatement = "
  170. UPDATE
  171. `files`
  172. SET
  173. `file` = '".$file."',
  174. `type` = '".$_FILES["printedlogo"]["type"]."',
  175. `name`='".$name."'
  176. WHERE
  177. id = 1";
  178. $this->db->query($updatestatement);
  179. }//endif file types
  180. }//endif file exists
  181. return true;
  182. }//end function updateSettings
  183. /**
  184. * Updates the password encryption seed and the password for the current user
  185. *
  186. * Updates the encryption seed, and also updates the encryption seed for the
  187. * given user (current)
  188. *
  189. * @param string $newseed the new encryptions seed
  190. * @param string $currpassword the user's current password
  191. * @param integer $userid the user's id
  192. */
  193. function updateEncryptionSeed($newseed, $currpassword, $userid){
  194. $userid = (int) $userid;
  195. //first let's make sure the password matches
  196. $querystatement="
  197. SELECT
  198. id
  199. FROM
  200. users
  201. WHERE
  202. id = ".$userid."
  203. AND password = ENCODE('".$currpassword."','".ENCRYPTION_SEED."')";
  204. $queryresult = $this->db->query($querystatement);
  205. if(!$this->db->numRows($queryresult))
  206. return "Encryption Seed not Updated: Invalid Current Password";
  207. //let's update the encryption seed then
  208. $querystatement = "
  209. UPDATE
  210. `settings`
  211. SET
  212. `value` = '".$newseed."'
  213. WHERE
  214. `name` = 'encryption_seed'";
  215. $queryresult = $this->db->query($querystatement);
  216. //last, reencode the current password
  217. $querystatement = "
  218. UPDATE
  219. users
  220. SET
  221. password = ENCODE('".$currpassword."','".$newseed."')
  222. WHERE
  223. id = ".$userid;
  224. $queryresult=$this->db->query($querystatement);
  225. //rencode all other passwords
  226. $querystatement = "
  227. UPDATE
  228. users
  229. SET
  230. password = ENCODE(DECODE(password,'".ENCRYPTION_SEED."'),'".$newseed."')
  231. WHERE
  232. id !=".$userid;
  233. $queryresult=$this->db->query($querystatement);
  234. return "Encryption Seed Updated.";
  235. }//end function updateEncryptionSeed
  236. /**
  237. * processes settings form
  238. *
  239. * Processes the form that updates the settings, or the encryption seed
  240. *
  241. * @param array $variables variables array passed from the $_POST
  242. */
  243. function processForm($variables){
  244. $variables = addSlashesToArray($variables);
  245. switch($variables["command"]){
  246. case "save":
  247. if($this->updateSettings($variables))
  248. if(!$this->updateErrorMessage)
  249. $statusmessage = "Settings Updated";
  250. else
  251. $statusmessage = "ERROR: ".$this->updateErrorMessage;
  252. break;
  253. case "encryption seed":
  254. if(isset($variables["changeseed"]))
  255. $statusmessage = $this->updateEncryptionSeed($variables["encryption_seed"],$variables["currentpassword"],$_SESSION["userinfo"]["id"]);
  256. break;
  257. }//endswitch
  258. return $statusmessage;
  259. }//end function processForm
  260. /**
  261. * displays options values for stylesheet select
  262. *
  263. * Displays the option tags for the stylesheet select
  264. *
  265. * @param string $stylesheet the current stylesheet
  266. */
  267. function displayStylesheets($stylesheet){
  268. $thedir="../../common/stylesheet";
  269. $thedir_stream = @opendir($thedir);
  270. while($entry = @ readdir($thedir_stream)){
  271. if ($entry!="." and $entry!=".." and is_dir($thedir."/".$entry) && $entry != ".svn") {
  272. ?><option value="<?php echo $entry?>" <?php if($entry = $stylesheet) echo 'selected="selected"'; ?>><?php echo $entry?></option><?php
  273. }//endif
  274. }//endwhile
  275. }//end function displayStyleSheets
  276. /**
  277. * Check to see if the scheduler has ever run
  278. */
  279. function checkForSchedulerRunning(){
  280. //first, if this is within the first day of the installation, we skip the check
  281. $querystatement = "SELECT creationdate FROM users WHERE id = 1";
  282. $queryresult = $this->db->query($querystatement);
  283. $therecord = $this->db->fetchArray($queryresult);
  284. if(stringToDate($therecord["creationdate"], "SQL") < strtotime("yesterday")){
  285. $querystatement = "
  286. SELECT
  287. MAX(lastrun) AS lastrun
  288. FROM
  289. scheduler";
  290. $queryresult = $this->db->query($querystatement);
  291. $therecord = $this->db->fetchArray($queryresult);
  292. if(!$therecord["lastrun"])
  293. return false;
  294. }//endif
  295. return true;
  296. }//end function checkForSchedulerRunning
  297. }//end class settings
  298. ?>