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

/hardware/admin/dbfuncs.php

https://bitbucket.org/JaKXz/photoflo2
PHP | 399 lines | 301 code | 52 blank | 46 comment | 31 complexity | 68f144e95a4353971f884c8f1e59fa4d MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * This file was developed as part of the Concerto digital signage project
  4. * at RPI.
  5. *
  6. * Copyright (C) 2009 Rensselaer Polytechnic Institute
  7. * (Student Senate Web Technologies Group)
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details. You should have received a copy
  18. * of the GNU General Public License along with this program.
  19. *
  20. * @package Concerto
  21. * @author Web Technologies Group, $Author$
  22. * @copyright Rensselaer Polytechnic Institute
  23. * @license GPLv2, see www.gnu.org/licenses/gpl-2.0.html
  24. * @version $Revision$
  25. */
  26. require("config.php");
  27. require_once("signature.php");
  28. function init_db( ) {
  29. mysql_pconnect(MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD)
  30. or die("MySQL connect failed! error = " . mysql_error( ));
  31. mysql_select_db(MYSQL_DATABASE)
  32. or die("MySQL select database failed: " . mysql_error( ));
  33. }
  34. function validate_mac($mac) {
  35. $mac = str_replace(':','',$mac); # remove any colons from the mac address
  36. if (preg_match('/^[0-9A-Fa-f]{12}$/', $mac)) {
  37. return $mac;
  38. } else {
  39. return 0;
  40. }
  41. }
  42. class FlashFile {
  43. private $id, $name, $md5, $sig, $url;
  44. public function create_new($name, $location, $url) {
  45. # compute md5 sum of the data file
  46. $md5 = md5_file($location);
  47. # sign the md5 hash
  48. $sig = generate_signature($md5);
  49. # insert into database
  50. $name = mysql_escape_string($name);
  51. mysql_query(
  52. "insert into file (name, md5, sig, url) ".
  53. "values(\"$name\", \"$md5\", \"$sig\", \"$url\")"
  54. ) or die ("query to insert new file failed: " . mysql_error( ));
  55. # return the object
  56. $new_id = mysql_insert_id( );
  57. return FlashFile::load_from_id($new_id);
  58. }
  59. public function load_from_id($id) {
  60. $obj = new FlashFile( );
  61. if (!is_numeric($id)) {
  62. die("passed non-numeric ID to load_from_id");
  63. }
  64. $result = mysql_query("select name, md5, sig, url from file where file_id=$id")
  65. or die("query to load file object from DB failed: " . mysql_error( ));
  66. if (!($row = mysql_fetch_row($result))) {
  67. die("attempt to load file object with nonexistent ID\n");
  68. } else {
  69. $obj->id = $id;
  70. $obj->name = $row[0];
  71. $obj->md5 = $row[1];
  72. $obj->sig = $row[2];
  73. $obj->url = $row[3];
  74. }
  75. }
  76. public function get_name( ) {
  77. return $this->name;
  78. }
  79. public function get_url( ) {
  80. return $this->url;
  81. }
  82. public function get_md5( ) {
  83. return $this->md5;
  84. }
  85. public function get_sig( ) {
  86. return $this->sig;
  87. }
  88. public function get_id( ) {
  89. return $this->id;
  90. }
  91. public function delete( ) {
  92. $id = $this->id;
  93. mysql_query("delete from file where file_id=$id");
  94. mysql_query("delete from file_map where file_id=$id");
  95. }
  96. }
  97. class HardwareClass {
  98. public function create_new($name) {
  99. // create a new hardware class and return it.
  100. // escape the string first in case it contains special chars
  101. $name = mysql_escape_string($name);
  102. mysql_query("insert into class (name) values(\"$name\")")
  103. or die("query to create new class failed: " . mysql_error( ));
  104. $new_id = mysql_insert_id( );
  105. return HardwareClass::load_from_id($new_id);
  106. }
  107. public function load_all_from_db( ) {
  108. // load all classes from the database
  109. $result = mysql_query("select class_id from class")
  110. or die("query to load class list failed: " . mysql_error( ));
  111. $objs = array( );
  112. while ($row = mysql_fetch_row($result)) {
  113. array_push($objs, HardwareClass::load_from_id($row[0]));
  114. }
  115. return $objs;
  116. }
  117. public function load_from_id($id) {
  118. // load a hardware class from the database given its ID
  119. $obj = new HardwareClass( );
  120. if (!is_numeric($id)) {
  121. die("passed non-numeric ID to load_from_id");
  122. }
  123. $result = mysql_query("select name from class where class_id=$id")
  124. or die("query to load ID failed: " . mysql_error( ));
  125. if (!($row = mysql_fetch_row($result))) {
  126. die("attempt to load nonexistent class $id");
  127. } else {
  128. $obj->id = $id;
  129. $obj->name = $row[0];
  130. return $obj;
  131. }
  132. }
  133. public function find_from_mac($mac) {
  134. if (($mac = validate_mac($mac)) === 0) {
  135. die("invalid MAC address passed");
  136. }
  137. $mac = mysql_escape_string($mac);
  138. $result = mysql_query("select class_id from class_map where mac='$mac'")
  139. or die("query to get class from MAC failed: " . mysql_error( ));
  140. if ($row = mysql_fetch_row($result)) {
  141. return HardwareClass::load_from_id($row[0]);
  142. } else {
  143. return 0;
  144. }
  145. }
  146. public function get_member_list( ) {
  147. // load the list of member MAC addresses from the database
  148. // returns an array of MAC strings
  149. $id = $this->id;
  150. $result = mysql_query("select mac from class_map where class_id=$id")
  151. or die("query to load member list failed: " . mysql_error( ));
  152. $ret = array( );
  153. while ($row = mysql_fetch_row($result)) {
  154. array_push($ret, $row[0]);
  155. }
  156. return $ret;
  157. }
  158. public function add_member($mac) {
  159. // add the machine specified by $mac to the database
  160. // also this will remove it from any other class
  161. $id = $this->id;
  162. # match against a regex
  163. if (($mac = validate_mac($mac)) === 0) {
  164. die("Error: input is not a valid MAC address");
  165. }
  166. $mac = mysql_escape_string($mac);
  167. mysql_query("delete from class_map where mac=\"$mac\"")
  168. or die("query to remove class member failed: " . mysql_error( ));
  169. mysql_query("insert into class_map(class_id, mac) values($id, \"$mac\")")
  170. or die("query to add class member failed: " . mysql_error( ));
  171. }
  172. public function rename($new_name) {
  173. // rename the class
  174. $id = $this->id;
  175. $new_name = mysql_escape_string($new_name);
  176. mysql_query("update class set name=\"$new_name\" where class_id=$id")
  177. or die("query to rename class failed: " . mysql_error( ));
  178. $this->name = $new_name;
  179. }
  180. public function remove( ) {
  181. // remove the class (all machines will go to the default class 1)
  182. if ($this->id == 1) {
  183. die("attempt to remove the default class (class 1)!");
  184. }
  185. $id = $this->id;
  186. // move everyone to class 1
  187. mysql_query("update class_map set class_id=1 where class_id=$id")
  188. or die("query to move to class 1 failed: " . mysql_error( ));
  189. mysql_query("delete from class where class_id=$id")
  190. or die("query to delete class $id failed: " . mysql_error( ));
  191. }
  192. public function remove_member($mac) {
  193. $id = $this->id;
  194. if (($mac = validate_mac($mac)) === 0) {
  195. die("invalid MAC passed to HardwareClass::remove_member");
  196. }
  197. $mac = mysql_escape_string($mac);
  198. mysql_query(
  199. "delete from class_map where class_id=$id and mac='$mac'"
  200. ) or die(
  201. "query to delete member $mac from class $id failed:"
  202. . mysql_error( )
  203. );
  204. }
  205. public function get_id( ) {
  206. return $this->id;
  207. }
  208. public function get_name( ) {
  209. return $this->name;
  210. }
  211. public function add_override($path) {
  212. // Add a new configuration file override for this class.
  213. // This override will replace the file at the given path.
  214. // Any existing override will be left alone.
  215. $path = mysql_escape_string($path);
  216. $id = $this->id;
  217. $result = mysql_query(
  218. "select count(class_id) from config_override ".
  219. " where class_id=$id and file_path='$path'"
  220. ) or die(
  221. "failed to query for number of overrides: " . mysql_error( )
  222. );
  223. $row = mysql_fetch_row($result);
  224. if ($row[0] == 1) {
  225. return 0;
  226. }
  227. mysql_query(
  228. "insert into config_override (class_id, file_path)" .
  229. " values($id, '$path')"
  230. ) or die(
  231. "failed to insert into config_override: " . mysql_error( )
  232. );
  233. return 1;
  234. }
  235. public function remove_override($path) {
  236. $path = mysql_escape_string($path);
  237. $id = $this->id;
  238. mysql_query(
  239. "delete from config_override ".
  240. "where class_id=$id and file_path=\"$path\""
  241. ) or die(
  242. "failed to delete from config_override: " . mysql_error( )
  243. );
  244. }
  245. public function edit_override($path, $new_text) {
  246. $path = mysql_escape_string($path);
  247. $sig = generate_signature($new_text);
  248. $new_text = mysql_escape_string($new_text);
  249. $id = $this->id;
  250. mysql_query(
  251. "update config_override " .
  252. "set data=\"$new_text\", " .
  253. "sig=\"$sig\"" .
  254. "where class_id=$id and file_path='$path'"
  255. ) or die(
  256. "failed to update override for $id:$path: " . mysql_error( )
  257. );
  258. }
  259. public function get_override($path) {
  260. $path = mysql_escape_string($path);
  261. $id = $this->id;
  262. $result = mysql_query(
  263. "select data from config_override ".
  264. "where class_id=$id && file_path=\"$path\""
  265. ) or die(
  266. "failed to query override table for $id:$path: " . mysql_error( )
  267. );
  268. if ($row = mysql_fetch_row($result)) {
  269. return $row[0];
  270. }
  271. return 0;
  272. }
  273. public function get_override_sig($path) {
  274. $path = mysql_escape_string($path);
  275. $id = $this->id;
  276. $result = mysql_query(
  277. "select sig from config_override ".
  278. "where class_id=$id && file_path=\"$path\""
  279. ) or die(
  280. "failed to query override table for $id:$path: " . mysql_error( )
  281. );
  282. if ($row = mysql_fetch_row($result)) {
  283. return $row[0];
  284. }
  285. return 0;
  286. }
  287. public function list_overrides( ) {
  288. $id = $this->id;
  289. $result = mysql_query(
  290. "select file_path from config_override where class_id=$id"
  291. ) or die(
  292. "failed to query override table for $id: " . mysql_error( )
  293. );
  294. $ret = array( );
  295. while ($row = mysql_fetch_row($result)) {
  296. $ret[ ] = $row[0];
  297. }
  298. return $ret;
  299. }
  300. public function add_file($filename, $target_path) {
  301. $id = $this->id;
  302. # get the md5 hash of the file and sign it
  303. $path = BASE_DIR."/flash/".$filename;
  304. $md5 = md5_file($path);
  305. #$tmp = split('= ', exec("openssl md5 $path"));
  306. #$md5 = trim($tmp[1]);
  307. print "-->".$md5."<--";
  308. $sig = generate_signature($md5);
  309. $filename = basename($filename);
  310. if (!file_exists(BASE_DIR."/flash/$filename")) {
  311. die("File does not exist!");
  312. }
  313. $url = BASE_URL."/flash/$filename";
  314. $filename=mysql_escape_string($filename);
  315. $url=mysql_escape_string($url);
  316. $target_path=mysql_escape_string($target_path);
  317. $md5=mysql_escape_string($md5);
  318. $sig=mysql_escape_string($sig);
  319. mysql_query("insert into file_map (class_id, filename, url, output_path, md5, sig) " .
  320. "values($id, \"$filename\", \"$url\", \"$target_path\", \"$md5\", \"$sig\")")
  321. or die("failure to add file to class: ". mysql_error( ));
  322. }
  323. public function remove_file($path) {
  324. $path=mysql_escape_string($path);
  325. $id=$this->id;
  326. mysql_query("delete from file_map where class_id=$id and output_path=\"$path\"")
  327. or die("failed to delete file...");
  328. }
  329. public function list_files( ) {
  330. $id=$this->id;
  331. $result = mysql_query("select filename, url, output_path, md5, sig from file_map where class_id=$id") or die("failed to list files: " . mysql_error( ));
  332. $output = array();
  333. while ($row = mysql_fetch_row($result)) {
  334. $output[] = array(
  335. name => $row[0],
  336. path => $row[2],
  337. url => $row[1],
  338. md5 => $row[3],
  339. sig => $row[4]
  340. );
  341. }
  342. return $output;
  343. }
  344. private $id;
  345. private $name;
  346. }
  347. ?>