PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/code/opentape_common.php

https://github.com/ahassan/opentape
PHP | 783 lines | 764 code | 11 blank | 8 comment | 3 complexity | 3518d64f768b8247ea991ee3439df8f3 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. // --- CONFIGURABLE ADVANCED SETTINGS
  3. // Be sure to have an ending /. These are relative to the dir
  4. // where you installed opentape.
  5. define("SETTINGS_PATH", "settings/");
  6. define("SONGS_PATH", "songs/");
  7. define("DEFAULT_COLOR", "EC660F");
  8. // --- END OF CONFIGURABLE ADVANCED SETTINGS ---- //
  9. require_once ('JSON.php');
  10. ini_set("track_errors","on");
  11. global $REL_PATH;
  12. $REL_PATH = preg_replace('|settings/[^/]*?$|', '', $_SERVER['REQUEST_URI']);
  13. $REL_PATH = preg_replace('|songs/[^/]*?$|', '', $REL_PATH);
  14. $REL_PATH = preg_replace('|code/[^/]*?$|', '', $REL_PATH);
  15. $REL_PATH = preg_replace('|res/[^/]*?$|', '', $REL_PATH);
  16. $REL_PATH = preg_replace('|/[^/]+?$|', '/', $REL_PATH);
  17. $REL_PATH = preg_replace('|/+|', '/', $REL_PATH);
  18. define("VERSION", "0.12");
  19. define("VERSION_CHECK_URL", "http://opentape.fm/public/latest_version.php");
  20. define("ANNOUNCE_SONGS_URL", "http://opentape.fm/public/announce_songs.php");
  21. define("ANNOUNCE_JS_URL", "http://opentape.fm/public/announce.js");
  22. // this may fix certain win32 issues, thanks fusen
  23. define("GETID3_HELPERAPPSDIR", "/");
  24. // Change dir to the main install dir for consistency
  25. $cwd = getcwd();
  26. if (preg_match('/code\/?$/', $cwd) || preg_match('|' . constant("SETTINGS_PATH") . '?$|', $cwd) || preg_match('/res\/?$/', $cwd)) {
  27. chdir('..');
  28. }
  29. //This function transforms the php.ini notation numbers (like '2M') to an integer (2*1024*1024 in this case)
  30. function let_to_num($v){
  31. $l = substr($v, -1);
  32. $ret = substr($v, 0, -1);
  33. switch(strtoupper($l)){
  34. case 'P':
  35. $ret *= 1024;
  36. case 'T':
  37. $ret *= 1024;
  38. case 'G':
  39. $ret *= 1024;
  40. case 'M':
  41. $ret *= 1024;
  42. case 'K':
  43. $ret *= 1024;
  44. break;
  45. }
  46. return $ret;
  47. }
  48. function get_max_upload_mb () {
  49. $max_upload_size = min(let_to_num(ini_get('post_max_size')), let_to_num(ini_get('upload_max_filesize')));
  50. return round(($max_upload_size / (1024*1024)),2);
  51. }
  52. function get_max_upload_bytes () {
  53. $max_upload_size = min(let_to_num(ini_get('post_max_size')), let_to_num(ini_get('upload_max_filesize')));
  54. return $max_upload_size;
  55. }
  56. // returns microtime as a long number
  57. function microtime_float()
  58. {
  59. list($usec, $sec) = explode(" ", microtime());
  60. return ((float)$usec + (float)$sec);
  61. }
  62. function escape_for_inputs($string) {
  63. return preg_replace("/'/", "&#39;", $string);
  64. }
  65. function escape_for_json($string) {
  66. return preg_replace('/"/', '\"', $string);
  67. }
  68. function clean_titles($string) {
  69. return preg_replace('/\\000/', '', $string);
  70. }
  71. function get_base_url() {
  72. global $REL_PATH;
  73. //if ( ($_SERVER['SERVER_PORT']==80 && (empty($_SERVER['HTTPS']) || !strcasecmp($_SERVER['HTTPS'],"off"))) ) {
  74. return 'http://' . $_SERVER['HTTP_HOST'] . $REL_PATH;
  75. /*
  76. } elseif ( ($_SERVER['SERVER_PORT']!=80 && (empty($_SERVER['HTTPS']) || !strcasecmp($_SERVER['HTTPS'],"off"))) ) {
  77. return 'http://' . $_SERVER['HTTP_HOST'] . ":" . $_SERVER['SERVER_PORT'] . $REL_PATH;
  78. } elseif ($_SERVER['SERVER_PORT']==443 && (!empty($_SERVER['HTTPS']) || strcasecmp($_SERVER['HTTPS'],"off"))) {
  79. return 'https://' . $_SERVER['HTTP_HOST'] . $REL_PATH;
  80. } else {
  81. return 'https://' . $_SERVER['HTTP_HOST'] . ":" . $_SERVER['SERVER_PORT'] . $REL_PATH;
  82. }*/
  83. }
  84. function is_logged_in() {
  85. $session_struct = get_session_struct();
  86. if (is_array($session_struct)) {
  87. foreach ($session_struct as $pos => $item) {
  88. if(!strcmp($_COOKIE["opentape_session"], $item['key'])) {
  89. return true;
  90. }
  91. }
  92. }
  93. return false;
  94. }
  95. function check_password($password) {
  96. /*
  97. $password_struct = array();
  98. if (file_exists(constant("SETTINGS_PATH") . ".opentape_password.array")) {
  99. $password_struct_data = file_get_contents( constant("SETTINGS_PATH") . ".opentape_password.array" );
  100. if(empty($password_struct_data) || $password_struct_data===false) {
  101. return -1;
  102. }
  103. $password_struct = unserialize($password_struct_data);
  104. if(empty($password_struct_data) || $password_struct_data===false) {
  105. return -1;
  106. }
  107. } else {
  108. return -1;
  109. }
  110. */
  111. $password_struct = get_password_struct();
  112. if (!strcmp( md5("MIXTAPESFORLIFE" . $password), $password_struct['hash']) ) {
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. function is_password_set() {
  119. $password_struct = get_password_struct();
  120. if (is_array($password_struct) && !empty($password_struct) && $password_struct !== false) {
  121. return true;
  122. } else {
  123. return false;
  124. }
  125. /*
  126. if (file_exists(constant("SETTINGS_PATH") . ".opentape_password.array")) {
  127. $password_struct_data = file_get_contents( constant("SETTINGS_PATH") . ".opentape_password.array" );
  128. if(empty($password_struct_data)) {
  129. return false;
  130. } elseif ($password_struct_data===false) {
  131. return -1; // this is a read error
  132. } else {
  133. return true; // password exists
  134. }
  135. } else {
  136. return false;
  137. }
  138. */
  139. }
  140. function set_password($password) {
  141. $password_struct = array();
  142. $password_struct['hash'] = md5("MIXTAPESFORLIFE" . $password);
  143. return write_password_struct($password_struct);
  144. /*
  145. $bytes_written = file_put_contents( constant("SETTINGS_PATH") . ".opentape_password.array", serialize($password_struct));
  146. if ($bytes_written===false) {
  147. return false;
  148. } else {
  149. return true;
  150. }
  151. */
  152. }
  153. // Here we give all users cookies, then we add/remove these session id's
  154. // into the session file
  155. function check_cookie() {
  156. if (!isset($_COOKIE["opentape_session"])) {
  157. if (!strcasecmp($_SERVER['HTTP_HOST'],"localhost")) {
  158. setcookie("opentape_session", md5("MIXTAPESFORLIFE" . microtime_float()), (time() + (86400*365)), "/", "");
  159. } elseif (preg_match('/:\d+$/', $_SERVER['HTTP_HOST'])) {
  160. setcookie("opentape_session", md5("MIXTAPESFORLIFE" . microtime_float()), (time() + (86400*365)), "/", "");
  161. } else {
  162. setcookie("opentape_session", md5("MIXTAPESFORLIFE" . microtime_float()), (time() + (86400*365)), "/", $_SERVER['HTTP_HOST']);
  163. }
  164. }
  165. }
  166. function create_session() {
  167. $session_struct = get_session_struct();
  168. $session_item = array();
  169. if($session_struct===false) { $session_struct=array(); }
  170. if (isset($_COOKIE["opentape_session"])) {
  171. $session_item['key'] = $_COOKIE["opentape_session"];
  172. $session_item['ts'] = time();
  173. array_push($session_struct, $session_item);
  174. return write_session_struct($session_struct);
  175. } else {
  176. return false;
  177. }
  178. }
  179. function remove_session() {
  180. $session_struct = get_session_struct();
  181. $session_struct_new = array();
  182. $session_item = array();
  183. if (is_array($session_struct)) {
  184. // rewrite the session struct without the one we are removing
  185. foreach ($session_struct as $pos => $item) {
  186. if(strcmp($_COOKIE["opentape_session"], $item['key'])) {
  187. array_push($session_struct_new,$session_item);
  188. }
  189. }
  190. return write_session_struct($session_struct_new);
  191. } else {
  192. // if the session file doesn't exist, there's nothing to remove the session from
  193. // so we just say ok.
  194. }
  195. return true;
  196. }
  197. function scan_songs() {
  198. require_once('getid3.php');
  199. $dir_handle = opendir(constant("SONGS_PATH"));
  200. // Initialize getID3 engine
  201. $getID3 = new getID3;
  202. $songlist_struct = get_songlist_struct();
  203. $songlist_struct_original = $songlist_struct;
  204. $songlist_new_items = array();
  205. // List all the files
  206. while (false !== ($file = readdir($dir_handle)) ) {
  207. if ( strcmp($file, ".") && strcmp($file, "..") && !strcasecmp(end(explode(".", $file)), "mp3")) {
  208. // error_log("Analyzing: " . constant("SONGS_PATH") . $file . " file_exists=" . file_exists(constant("SONGS_PATH") . $file));
  209. // error_log("id3_structure: " . print_r($id3_info,1) . "\nID3v2:" . $id3_info['id3v2']['comments']['artist'][0] . " - " . $id3_info['id3v2']['comments']['title'][0]);
  210. // error_log("ID3v1:" . $id3_info['id3v1']['artist'] . " - " . $id3_info['id3v1']['title']);
  211. if ( !isset($songlist_struct[ base64_encode(rawurlencode($file)) ]) ) {
  212. $id3_info = $getID3->analyze(constant("SONGS_PATH") . $file);
  213. $song_item = array();
  214. // Check id3 v2 tags,
  215. if (!empty($id3_info['id3v2']['comments']['artist'][0])) {
  216. $song_item['artist'] = clean_titles($id3_info['id3v2']['comments']['artist'][0]);
  217. } elseif (!empty($id3_info['id3v1']['artist'])) {
  218. $song_item['artist'] = clean_titles($id3_info['id3v1']['artist']);
  219. } /*else {
  220. $song_item['artist'] = "Unknown artist";
  221. } */
  222. if (!empty($id3_info['id3v2']['comments']['title'][0])) {
  223. $song_item['title'] = clean_titles($id3_info['id3v2']['comments']['title'][0]);
  224. } elseif (!empty($id3_info['id3v1']['title'])) {
  225. $song_item['title'] = clean_titles($id3_info['id3v1']['title']);
  226. } /*else {
  227. $song_item['title'] = "Unknown title";
  228. } */
  229. // if we are missing tags, set the title to the filename, sans ".mp3"
  230. if (!isset($song_item['artist']) && !isset($song_item['title'])) {
  231. $song_item['artist'] = "";
  232. $song_item['title'] = preg_replace('/\.mp3$/i', '', $id_info['filename']);
  233. } elseif (!isset($song_item['artist'])) { // fill in some of the blanks otherwise
  234. $song_item['artist'] = "Unknown artist";
  235. } elseif (!isset($song_item['title'])) {
  236. $song_item['title'] = "Unknown track";
  237. }
  238. $song_item['filename'] = $id3_info['filename'];
  239. $song_item['playtime_seconds'] = $id3_info['playtime_seconds'];
  240. $song_item['playtime_string'] = $id3_info['playtime_string'];
  241. $song_item['mtime'] = filemtime(constant("SONGS_PATH") . $file);
  242. $song_item['size'] = filesize(constant("SONGS_PATH") . $file);
  243. $songlist_new_items[ base64_encode(rawurlencode($id3_info['filename'])) ] = $song_item;
  244. }
  245. }
  246. }
  247. // if changed, save it
  248. if (!empty($songlist_new_items)) {
  249. $songlist_struct = array_merge($songlist_new_items, $songlist_struct);
  250. announce_songs($songlist_struct);
  251. write_songlist_struct($songlist_struct);
  252. }
  253. return $songlist_struct;
  254. }
  255. // Renames songs in the songlist structure
  256. function rename_song($song_key, $artist, $title) {
  257. if (empty($song_key)) {
  258. error_log("rename_song called with insufficient arguments: song_key=$song_key, artist=$artist, title=$title");
  259. return false;
  260. }
  261. $songlist_struct = get_songlist_struct();
  262. $songlist_struct[$song_key]['opentape_artist'] = $artist;
  263. $songlist_struct[$song_key]['opentape_title'] = $title;
  264. return write_songlist_struct($songlist_struct);
  265. }
  266. // Reorders songs in the songlist structure
  267. function reorder_songs($args) {
  268. if (empty($args)) {
  269. error_log("reorder_songs called with insufficient arguments: args=$args");
  270. return false;
  271. }
  272. $songlist_struct = get_songlist_struct();
  273. $songlist_struct_new = array();
  274. foreach ($args as $pos => $row) {
  275. $songlist_struct_new[$row] = $songlist_struct[$row];
  276. }
  277. return write_songlist_struct($songlist_struct_new);
  278. }
  279. // Deletes song from the disk and the songlist struct
  280. function delete_song($song_key) {
  281. if (empty($song_key)) {
  282. error_log("delete_song called with insufficient arguments: song_key=$song_key");
  283. return false;
  284. }
  285. $songlist_struct = get_songlist_struct();
  286. if (unlink(constant("SONGS_PATH") . $songlist_struct[$song_key]['filename'])) {
  287. unset($songlist_struct[$song_key]);
  288. } else {
  289. return false;
  290. }
  291. return write_songlist_struct($songlist_struct);
  292. }
  293. // Get total tape runtime from songlist_struct in seconds
  294. function get_total_runtime_seconds() {
  295. $songlist_struct = get_songlist_struct();
  296. $total_secs = 0;
  297. foreach ($songlist_struct as $pos => $row) {
  298. $total_secs += $row['playtime_seconds'];
  299. }
  300. return $total_secs;
  301. }
  302. // Return pretty and formatted runtime
  303. function get_total_runtime_string() {
  304. $seconds = get_total_runtime_seconds();
  305. $string = "";
  306. $mins = round($seconds / 60);
  307. $secs = $seconds % 60;
  308. if ($mins==1) { $string .= "$mins min"; }
  309. else { $string .= "$mins mins"; }
  310. if ($secs==1) { $string .= " $secs sec"; }
  311. else { $string .= " $secs secs"; }
  312. return $string;
  313. }
  314. // Retrieves songlist struct from the disk file
  315. function get_songlist_struct() {
  316. $songlist_struct = array();
  317. $filename_base = ".opentape_songlist";
  318. if (file_exists( constant("SETTINGS_PATH") . $filename_base . ".php" ) &&
  319. is_readable( constant("SETTINGS_PATH") . $filename_base . ".php" ) ) {
  320. // this is the more secure way of storing this data, as
  321. // the web users can't fetch it
  322. include( constant("SETTINGS_PATH") . $filename_base . ".php" );
  323. $songlist_struct = unserialize(base64_decode($songlist_struct_data));
  324. } elseif (file_exists( constant("SETTINGS_PATH") . $filename_base . ".array" ) &&
  325. is_readable( constant("SETTINGS_PATH") . $filename_base . ".array" ) ) {
  326. $songlist_struct_data = file_get_contents ( constant("SETTINGS_PATH") . $filename_base .".array" );
  327. $songlist_struct = unserialize($songlist_struct_data);
  328. if ($songlist_struct === false || !is_array($songlist_struct)) {
  329. error_log ("Songlist currently empty");
  330. $songlist_struct = array();
  331. } else {
  332. // upgrade to the new method of storing the data quietly
  333. write_songlist_struct($songlist_struct);
  334. }
  335. }
  336. return $songlist_struct;
  337. }
  338. // Saves the songlist data to disk. Checks if all items in list are actualyl accessible
  339. function write_songlist_struct($songlist_struct) {
  340. // before we write, lets verify that all the files in here are really working files, that they exist at least...
  341. foreach ($songlist_struct as $pos => $row) {
  342. if (! is_file(constant("SONGS_PATH") . $row['filename']) ) {
  343. error_log($row['filename'] . " is not accessible, removing it from the songlist");
  344. unset($songlist_struct[$pos]);
  345. }
  346. }
  347. $songlist_struct_data = '<?php $songlist_struct_data = "' . base64_encode(serialize($songlist_struct)) . '"; ?>';
  348. $bytes_written = file_put_contents( constant("SETTINGS_PATH") . ".opentape_songlist.php", $songlist_struct_data);
  349. if ($bytes_written===false) {
  350. error_log("Unable to write songlist array");
  351. return false;
  352. } else {
  353. return true;
  354. }
  355. }
  356. function get_opentape_prefs() {
  357. $prefs_struct = array();
  358. $filename_base = ".opentape_prefs";
  359. if (file_exists( constant("SETTINGS_PATH") . $filename_base . ".php" ) &&
  360. is_readable( constant("SETTINGS_PATH") . $filename_base . ".php" ) ) {
  361. // this is the more secure way of storing this data, as
  362. // the web users can't fetch it
  363. include( constant("SETTINGS_PATH") . $filename_base . ".php" );
  364. $prefs_struct = unserialize(base64_decode($prefs_struct_data));
  365. } elseif (file_exists( constant("SETTINGS_PATH") . $filename_base . ".array" ) &&
  366. is_readable( constant("SETTINGS_PATH") . $filename_base . ".array" ) ) {
  367. $prefs_struct_data = file_get_contents ( constant("SETTINGS_PATH") . $filename_base . ".array" );
  368. $prefs_struct = unserialize($prefs_struct_data);
  369. if ($prefs_struct === false || !is_array($prefs_struct)) {
  370. error_log ("prefs file currently empty");
  371. $prefs_struct = array();
  372. } else {
  373. // we need to upgrade the file type to the new version by writing it over again here.
  374. write_opentape_prefs($prefs_struct);
  375. }
  376. }
  377. return $prefs_struct;
  378. }
  379. function write_opentape_prefs($prefs_struct) {
  380. $prefs_struct_data = '<?php $prefs_struct_data = "' . base64_encode(serialize($prefs_struct)) . '"; ?>';
  381. $bytes_written = file_put_contents( constant("SETTINGS_PATH") . ".opentape_prefs.php", $prefs_struct_data);
  382. if ($bytes_written===false) {
  383. error_log("Unable to write prefs php data in " . constant("SETTINGS_PATH") . ".opentape_prefs.php");
  384. return false;
  385. } else {
  386. return true;
  387. }
  388. }
  389. function get_password_struct() {
  390. $password_struct = array();
  391. $filename_base = ".opentape_password";
  392. if (file_exists( constant("SETTINGS_PATH") . $filename_base . ".php" ) &&
  393. is_readable( constant("SETTINGS_PATH") . $filename_base .".php" ) ) {
  394. // this is the more secure way of storing this data, as
  395. // the web users can't fetch it
  396. include( constant("SETTINGS_PATH") . $filename_base . ".php" );
  397. $password_struct = unserialize(base64_decode($password_struct_data));
  398. } elseif (file_exists( constant("SETTINGS_PATH") . $filename_base . ".array" ) &&
  399. is_readable( constant("SETTINGS_PATH") . $filename_base . ".array" ) ) {
  400. $password_struct_data = file_get_contents ( constant("SETTINGS_PATH") . $filename_base . ".array" );
  401. $password_struct = unserialize($password_struct_data);
  402. if ($password_struct === false || !is_array($password_struct)) {
  403. error_log ("password file currently empty");
  404. $password_struct = array();
  405. } else {
  406. // we need to upgrade the file type to the new version by writing it over again here.
  407. write_password_struct($password_struct);
  408. }
  409. } else {
  410. return false;
  411. }
  412. return $password_struct;
  413. }
  414. function write_password_struct($password_struct) {
  415. $password_struct_data = '<?php $password_struct_data = "' . base64_encode(serialize($password_struct)) . '"; ?>';
  416. $bytes_written = file_put_contents( constant("SETTINGS_PATH") . ".opentape_password.php", $password_struct_data);
  417. if ($bytes_written===false) {
  418. error_log("Unable to write password php data in " . constant("SETTINGS_PATH") . ".opentape_password.php");
  419. return false;
  420. } else {
  421. return true;
  422. }
  423. }
  424. function get_session_struct() {
  425. $session_struct = array();
  426. $filename_base = ".opentape_session";
  427. if (file_exists( constant("SETTINGS_PATH") . $filename_base . ".php" ) &&
  428. is_readable( constant("SETTINGS_PATH") . $filename_base . ".php" ) ) {
  429. // this is the more secure way of storing this data, as
  430. // the web users can't fetch it
  431. include( constant("SETTINGS_PATH") . $filename_base . ".php" );
  432. $session_struct = unserialize(base64_decode($session_struct_data));
  433. } elseif (file_exists( constant("SETTINGS_PATH") . $filename_base . ".array" ) &&
  434. is_readable( constant("SETTINGS_PATH") . $filename_base . ".array" ) ) {
  435. $session_struct_data = file_get_contents ( constant("SETTINGS_PATH") . $filename_base . ".array" );
  436. $session_struct = unserialize($session_struct_data);
  437. if ($session_struct === false || !is_array($session_struct)) {
  438. error_log ("password file currently empty");
  439. $session_struct = array();
  440. } else {
  441. // we need to upgrade the file type to the new version by writing it over again here.
  442. write_session_struct($session_struct);
  443. }
  444. } else {
  445. return false;
  446. }
  447. return $session_struct;
  448. }
  449. function write_session_struct($session_struct) {
  450. $session_struct_data = '<?php $session_struct_data = "' . base64_encode(serialize($session_struct)) . '"; ?>';
  451. $bytes_written = file_put_contents( constant("SETTINGS_PATH") . ".opentape_session.php", $session_struct_data);
  452. if ($bytes_written===false) {
  453. error_log("Unable to write session php data in " . constant("SETTINGS_PATH") . ".opentape_session.php");
  454. return false;
  455. } else {
  456. return true;
  457. }
  458. }
  459. function get_version() {
  460. return constant('VERSION');
  461. }
  462. function get_version_banner() {
  463. echo 'This is <a href="http://opentape.fm">Opentape ' . constant('VERSION') . '</a>.';
  464. }
  465. function check_for_update() {
  466. $ts = time();
  467. $prefs_struct = get_opentape_prefs();
  468. $result = do_post_request(constant("VERSION_CHECK_URL"), http_build_query(array('version'=>constant("VERSION"))), null);
  469. $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
  470. $version_struct = $json->decode(stripslashes($result));
  471. if ($version_struct!==false && is_array($version_struct)) {
  472. $prefs_struct['latest_opentape'] = $version_struct['version'];
  473. $prefs_struct['latest_opentape_update'] = $version_struct['ts'];
  474. $prefs_struct['last_update_check'] = $ts;
  475. //error_log($result);
  476. } else {
  477. error_log("Version check returned incorrect result");
  478. $prefs_struct['latest_opentape'] = -1;
  479. $prefs_struct['latest_opentape_update'] = -1;
  480. $prefs_struct['last_update_check'] = $ts;
  481. }
  482. if(write_opentape_prefs($prefs_struct)) {
  483. return $prefs_struct;
  484. } else {
  485. false;
  486. }
  487. }
  488. function announce_songs($songlist_struct) {
  489. global $REL_PATH;
  490. $ts = time();
  491. $prefs_struct = get_opentape_prefs();
  492. $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
  493. $version_struct = $json->encode($songlist_struct);
  494. $data = http_build_query(
  495. array(
  496. 'version' => constant("VERSION"),
  497. 'url' => get_base_url(),
  498. 'songs' => $version_struct
  499. )
  500. );
  501. $result = do_post_request(constant("ANNOUNCE_SONGS_URL"), $data, null);
  502. if (!strcmp($result,"OK")) {
  503. } else {
  504. error_log("Failed to announce songs to " . constant("ANNOUNCE_SONGS_URL") . " result was: " . $result);
  505. }
  506. $prefs_struct['last_announce_songs'] = $ts;
  507. if(write_opentape_prefs($prefs_struct)) {
  508. return $prefs_struct;
  509. } else {
  510. false;
  511. }
  512. }
  513. function do_post_request($url, $data, $optional_headers = null) {
  514. // People with curl use curl
  515. if (extension_loaded("curl")) {
  516. $curl_handle = curl_init();
  517. curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
  518. curl_setopt($curl_handle, CURLOPT_URL, "$url");
  519. curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 10);
  520. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
  521. curl_setopt($curl_handle, CURLOPT_USERAGENT, "Opentape " . constant("VERSION"));
  522. curl_setopt($curl_handle, CURLOPT_POST, 1);
  523. curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);
  524. $buffer = curl_exec($curl_handle);
  525. curl_close($curl_handle);
  526. // check for success or failure
  527. if (empty($buffer)) {
  528. return false;
  529. } else {
  530. return $buffer;
  531. }
  532. // everyone else is via the stream
  533. // from here: http://netevil.org/blog/2006/nov/http-post-from-php-without-curl
  534. } else {
  535. $params = array('http' => array(
  536. 'method' => 'POST',
  537. 'content' => $data
  538. ));
  539. if ($optional_headers !== null) {
  540. array_push($optional_headers, "User-Agent: Opentape " . constant("VERSION"));
  541. } else {
  542. $optional_headers = array();
  543. array_push($optional_headers, "User-Agent: Opentape " . constant("VERSION"));
  544. }
  545. $params['http']['header'] = $optional_headers;
  546. $ctx = stream_context_create($params);
  547. $fp = @fopen($url, 'rb', false, $ctx);
  548. if (!$fp) {
  549. return false;
  550. //throw new Exception("Problem with $url, $php_errormsg");
  551. }
  552. $response = @stream_get_contents($fp);
  553. if ($response === false) {
  554. return false;
  555. //throw new Exception("Problem reading data from $url, $php_errormsg");
  556. }
  557. return $response;
  558. }
  559. }
  560. ?>