functions.php
author Dan
Fri, 12 Jun 2009 13:48:22 -0400
changeset 76 487a16c7117c
parent 75 2f39cb7f54c4
permissions -rw-r--r--
Considerably improved reliability of reboot code. Still producing respawn errors so currently disabled.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     1
<?php
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     2
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     3
/**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     4
 * Utility functions
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     5
 * 
6
5f35ebc4f9bb First release version. Renamed to Greyhound and readme/license files added.
Dan
parents: 5
diff changeset
     6
 * Greyhound - real web management for Amarok
5
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
     7
 * Copyright (C) 2008 Dan Fuhry
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     8
 *
5
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
     9
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
    10
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
    11
 *
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
    13
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    14
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    15
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    16
/**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    17
 * Utility/reporting functions
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    18
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    19
 
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    20
/**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    21
 * Report a fatal error and exit
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    22
 * @param string Error message
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    23
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    24
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    25
function burnout($msg)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    26
{
34
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    27
  global $use_colors;
9
63a257541313 Moved all Greyhound output to stderr so it will show up in AmaroK error messages; corrected some HTML bugs in the readme
Dan
parents: 8
diff changeset
    28
  $h = @fopen('php://stderr', 'w');
34
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    29
  if ( $use_colors )
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    30
  {
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    31
    fwrite($h, "\x1B[31;1m[Greyhound] fatal: \x1B[37;1m");
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    32
    fwrite($h, "$msg\x1B[0m\n");
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    33
  }
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    34
  else
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    35
  {
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    36
    fwrite($h, "[Greyhound] fatal: $msg\n");
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    37
  }
9
63a257541313 Moved all Greyhound output to stderr so it will show up in AmaroK error messages; corrected some HTML bugs in the readme
Dan
parents: 8
diff changeset
    38
  fclose($h);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    39
  exit(1);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    40
}
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    41
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    42
/**
34
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    43
 * Print a stylized status message, compatible with Linux consoles. Falls back to no control characters if on unsupported terminal.
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    44
 * @param string Status message
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    45
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    46
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    47
function status($msg)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    48
{
34
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    49
  global $use_colors;
9
63a257541313 Moved all Greyhound output to stderr so it will show up in AmaroK error messages; corrected some HTML bugs in the readme
Dan
parents: 8
diff changeset
    50
  $h = @fopen('php://stderr', 'w');
16
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
    51
  $label = ( defined('HTTPD_WS_CHILD') ) ? 'Child ' . substr(strval(getmypid()), -3) : 'Greyhound';
34
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    52
  if ( $use_colors )
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    53
  {
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    54
    fwrite($h, "\x1B[32;1m[$label] \x1B[32;0m$msg\x1B[0m\n");
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    55
  }
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    56
  else
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    57
  {
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    58
    fwrite($h, "[$label] $msg\n");
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    59
  }
9
63a257541313 Moved all Greyhound output to stderr so it will show up in AmaroK error messages; corrected some HTML bugs in the readme
Dan
parents: 8
diff changeset
    60
  fclose($h);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    61
}
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    62
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    63
/**
13
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 9
diff changeset
    64
 * Print a stylized warning message, compatible with Linux consoles
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 9
diff changeset
    65
 * @param string message message
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 9
diff changeset
    66
 */
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 9
diff changeset
    67
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 9
diff changeset
    68
function warning($msg)
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 9
diff changeset
    69
{
34
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    70
  global $use_colors;
13
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 9
diff changeset
    71
  $h = @fopen('php://stderr', 'w');
34
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    72
  if ( $use_colors )
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    73
  {
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    74
    fwrite($h, "\x1B[33;1m[Greyhound] \x1B[0m\x1B[33mWarning:\x1B[0m $msg\n");
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    75
  }
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    76
  else
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    77
  {
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    78
    fwrite($h, "[Greyhound] Warning: $msg\n");
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 33
diff changeset
    79
  }
13
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 9
diff changeset
    80
  fclose($h);
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 9
diff changeset
    81
}
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 9
diff changeset
    82
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 9
diff changeset
    83
/**
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    84
 * Performs an action with DCOP.
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    85
 * @param string DCOP component, e.g. player, playlist, playlistbrowser, ...
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    86
 * @param string Action to perform, e.g. stop, play, ...
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    87
 * @param string additional parameters... (NOT IMPLEMENTED)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    88
 * @return mixed output of DCOP command
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    89
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    90
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    91
function dcop_action($component, $action)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    92
{
69
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
    93
  file_put_contents('php://stderr', "[dcop] start...");
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
    94
  if ( function_exists('proc_open') )
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
    95
  {
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
    96
    $descriptorspec = array(
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
    97
        0 => array('pipe', 'r'),
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
    98
        1 => array('pipe', 'w'),
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
    99
        2 => array('pipe', 'w')
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   100
      );
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   101
    $dcop_command = "dcop amarok $component $action";
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   102
    $dcop_process = proc_open($dcop_command, $descriptorspec, $dcop_pipes);
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   103
    do
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   104
    {
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   105
      $status = proc_get_status($dcop_process);
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   106
      usleep(2000);
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   107
    } while ( $status['running'] );
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   108
    pcntl_waitpid($status['pid'], $status);
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   109
    $output = fgets($dcop_pipes[1], 1024);
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   110
    proc_close($dcop_process);
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   111
  }
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   112
  else
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   113
  {
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   114
    $tmpfile = tempnam('amaweb', '');
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   115
    if ( !$tmpfile )
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   116
      burnout('tempnam() failed us');
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   117
    
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   118
    // This is freezing up for some reason.
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   119
    system("dcop amarok $component $action > $tmpfile");
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   120
    $output = @file_get_contents($tmpfile);
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   121
    @unlink($tmpfile);
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   122
  }
73780a159e15 DCOP: added safeguards for mysterious zombie behavior
Dan
parents: 64
diff changeset
   123
  file_put_contents('php://stderr', "got it...\r");
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   124
  $output = trim($output);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   125
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   126
  // detect type of output
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   127
  if ( $output == 'true' )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   128
    return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   129
  else if ( $output == 'false' )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   130
    return false;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   131
  else if ( preg_match('/^-?[0-9]+/', $output) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   132
    return intval($output);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   133
  else
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   134
    return $output;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   135
}
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   136
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   137
/**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   138
 * Rebuilds the copy of the playlist in RAM
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   139
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   140
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   141
$playlist_last_md5 = '';
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   142
75
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   143
function rebuild_playlist($force = false, $from_ipc = false)
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   144
{
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   145
  // import what we need
33
3b4aef1efff6 Removed requirement for detecting home directory; artwork fetcher relies on return from saveCurrentPlaylist now
Dan
parents: 32
diff changeset
   146
  global $playlist, $amarok_home;
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   147
  // sync and load the playlist file
75
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   148
  // if we're coming from IPC, don't call DCOP, just load the existing file
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   149
  // this prevents the file from being generated 20 times and thus sometimes
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   150
  // causing simplexml to read a half-written file
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   151
  if ( !$from_ipc )
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   152
  {
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   153
    $playlist_file = dcop_action('playlist', 'saveCurrentPlaylist');
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   154
  }
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   155
  else
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   156
  {
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   157
    $playlist_file = $from_ipc;
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   158
  }
33
3b4aef1efff6 Removed requirement for detecting home directory; artwork fetcher relies on return from saveCurrentPlaylist now
Dan
parents: 32
diff changeset
   159
  // do we have amarok's home?
3b4aef1efff6 Removed requirement for detecting home directory; artwork fetcher relies on return from saveCurrentPlaylist now
Dan
parents: 32
diff changeset
   160
  if ( !$amarok_home )
3b4aef1efff6 Removed requirement for detecting home directory; artwork fetcher relies on return from saveCurrentPlaylist now
Dan
parents: 32
diff changeset
   161
    $amarok_home = dirname($playlist_file);
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   162
  // check MD5 - if it's not changed, exit to save CPU cycles
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   163
  global $playlist_last_md5;
5
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   164
  $effective_md5 = md5_playlist_file($playlist_file);
75
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   165
  if ( $playlist_last_md5 == $effective_md5 && !$force )
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   166
  {
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   167
    return true;
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   168
  }
14
7a1573676cc4 Added jump-to-current function in iphone theme; fixed md5('') call if unique IDs empty in playlist XML file
Dan
parents: 13
diff changeset
   169
  status('Rebuilding playlist cache');
5
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   170
  $playlist_last_md5 = $effective_md5;
75
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   171
  $originalplaylist = $playlist_file;
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   172
  $xml = false;
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   173
  $unlinkplaylist = false;
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   174
  while ( !$xml )
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   175
  {
75
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   176
    // start XML parser
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   177
    try
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   178
    {
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   179
      $xml = @simplexml_load_file($playlist_file);
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   180
      if ( $xml )
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   181
        break;
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   182
      
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   183
      // if we can't load it, just wait - another process might have it loaded
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   184
      warning('Could not load playlist file; maybe another Greyhound thread has it loaded? Waiting half a second.');
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   185
      usleep(500000);
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   186
    }
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   187
    catch ( Exception $e )
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   188
    {
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   189
      burnout("Caught exception trying to load playlist file:\n$e");
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   190
    }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   191
  }
75
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   192
  
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   193
  $attribs = $xml->attributes();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   194
  if ( @$attribs['product'] != 'Amarok' )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   195
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   196
    burnout('Playlist is not in Amarok format');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   197
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   198
  $playlist = array();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   199
  foreach ( $xml->children() as $child )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   200
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   201
    $attribs = $child->attributes();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   202
    $item = array(
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   203
        'uri'    => $attribs['uri'],
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   204
        'title'  => strval($child->Title),
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   205
        'artist' => strval($child->Artist),
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   206
        'album'  => strval($child->Album),
2
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   207
        'length' => seconds_to_str(intval($child->Length)),
860ba7141641 Should be nearly finished now - includes volume control, length measurement, and seems pretty stable
Dan
parents: 0
diff changeset
   208
        'length_int' => intval($child->Length)
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   209
      );
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   210
    $playlist[] = $item;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   211
  }
75
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   212
  if ( $unlinkplaylist )
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   213
  {
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   214
    unlink($playlist_file);
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   215
  }
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   216
  // tell all other worker threads to reload the playlist as well
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   217
  global $httpd;
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   218
  if ( is_object($httpd) && !$from_ipc )
32
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 16
diff changeset
   219
  {
75
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   220
    $httpd->threader->ipc_send(array(
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   221
        'action' => 'reloadplaylist',
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   222
        'propagate' => true,
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   223
        'playlist_file' => $playlist_file
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   224
      ));
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   225
  }
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   226
}
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   227
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   228
function rebuild_playlist_ipc($command, $threader)
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   229
{
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   230
  status('IPC playlist rebuild received');
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   231
  if ( isset($command['playlist']) )
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   232
  {
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   233
    status('IPC playlist rebuild: using playlist sent in IPC packet; length: ' . strlen($command['playlist']));
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   234
    $GLOBALS['playlist'] = $command['playlist'];
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   235
  }
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   236
  else
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   237
  {
2f39cb7f54c4 Playlist rebuild should work right now!
Dan
parents: 74
diff changeset
   238
    rebuild_playlist(true, $command['playlist_file']);
32
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 16
diff changeset
   239
  }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   240
}
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   241
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   242
/**
5
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   243
 * Builds the correct MD5 check for the specified playlist XML file. This is designed to base on the list of actual tracks, disregarding
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   244
 * the rest of the text in the XML file.
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   245
 * @param string Path to playlist
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   246
 * @return string hash
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   247
 */
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   248
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   249
function md5_playlist_file($file)
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   250
{
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   251
  $contents = @file_get_contents($file);
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   252
  if ( empty($contents) )
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   253
    return false;
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   254
  $count = preg_match_all('/uniqueid="([a-fA-F0-9]+?)"/', $contents, $matches);
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   255
  $matches = implode("", $matches[1]);
14
7a1573676cc4 Added jump-to-current function in iphone theme; fixed md5('') call if unique IDs empty in playlist XML file
Dan
parents: 13
diff changeset
   256
  if ( empty($matches) )
7a1573676cc4 Added jump-to-current function in iphone theme; fixed md5('') call if unique IDs empty in playlist XML file
Dan
parents: 13
diff changeset
   257
  {
7a1573676cc4 Added jump-to-current function in iphone theme; fixed md5('') call if unique IDs empty in playlist XML file
Dan
parents: 13
diff changeset
   258
    // sometimes current.xml has blank unique IDs
7a1573676cc4 Added jump-to-current function in iphone theme; fixed md5('') call if unique IDs empty in playlist XML file
Dan
parents: 13
diff changeset
   259
    $count = preg_match_all('/url="([^"]+?)"/', $contents, $matches);
7a1573676cc4 Added jump-to-current function in iphone theme; fixed md5('') call if unique IDs empty in playlist XML file
Dan
parents: 13
diff changeset
   260
    $matches = implode("", $matches[1]);
7a1573676cc4 Added jump-to-current function in iphone theme; fixed md5('') call if unique IDs empty in playlist XML file
Dan
parents: 13
diff changeset
   261
  }
5
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   262
  return md5($matches);
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   263
}
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   264
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 4
diff changeset
   265
/**
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   266
 * Converts a number to minute:second format
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   267
 * @param int Seconds
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   268
 * @return string format: mm:ss
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   269
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   270
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   271
function seconds_to_str($secs)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   272
{
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   273
  $seconds = $secs % 60;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   274
  $minutes = ( $secs - $seconds ) / 60;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   275
  $seconds = strval($seconds);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   276
  $minutes = strval($minutes);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   277
  if ( strlen($seconds) < 2 )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   278
    $seconds = "0$seconds";
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   279
  if ( strlen($minutes) < 2 )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   280
    $minutes = "0$minutes";
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   281
  return "$minutes:$seconds";
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   282
}
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   283
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   284
/**
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   285
 * Loads the specified theme into Smarty
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   286
 * @param string Theme ID
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   287
 * @return object Smarty object
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   288
 */
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   289
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   290
function load_theme($theme_id)
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   291
{
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   292
  global $httpd;
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   293
  static $smarty = array();
50
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   294
  if ( $theme_id === '__free__' )
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   295
  {
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   296
    $smarty = array();
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   297
    return false;
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   298
  }
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   299
  if ( !isset($smarty[$theme_id]) )
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   300
  {
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   301
    $smarty[$theme_id] = new Smarty();
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 6
diff changeset
   302
    $smarty[$theme_id]->template_dir = GREY_ROOT . "/themes/$theme_id";
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 6
diff changeset
   303
    if ( !is_dir("./compiled/$theme_id") )
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 6
diff changeset
   304
      @mkdir("./compiled/$theme_id");
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 6
diff changeset
   305
    $smarty[$theme_id]->compile_dir  = "./compiled/$theme_id";
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   306
    $smarty[$theme_id]->config_dir = "./config";
8
a8d108f37363 Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents: 6
diff changeset
   307
    $httpd->add_handler("themes/$theme_id", 'dir', GREY_ROOT . "/themes/$theme_id");
4
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   308
  }
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   309
  return $smarty[$theme_id];
cde92f6ec29f Should be completely iPhone/iPod Touch-friendly now :)
Dan
parents: 2
diff changeset
   310
}
50
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   311
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   312
/**
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   313
 * Implementation of the "which" command in native PHP.
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   314
 * @param string command
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   315
 * @return string path to executable, or false on failure
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   316
 */
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   317
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   318
function which($executable)
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   319
{
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   320
  $path = ( isset($_ENV['PATH']) ) ? $_ENV['PATH'] : ( isset($_SERVER['PATH']) ? $_SERVER['PATH'] : false );
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   321
  if ( !$path )
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   322
    // couldn't get OS's PATH
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   323
    return false;
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   324
    
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   325
  $win32 = ( PHP_OS == 'WINNT' || PHP_OS == 'WIN32' );
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   326
  $extensions = $win32 ? array('.exe', '.com', '.bat') : array('');
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   327
  $separator = $win32 ? ';' : ':';
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   328
  $paths = explode($separator, $path);
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   329
  foreach ( $paths as $dir )
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   330
  {
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   331
    foreach ( $extensions as $ext )
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   332
    {
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   333
      $fullpath = "$dir/{$executable}{$ext}";
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   334
      if ( file_exists($fullpath) && is_executable($fullpath) )
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   335
      {
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   336
        return $fullpath;
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   337
      }
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   338
    }
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   339
  }
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   340
  return false;
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   341
}
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   342
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   343
/**
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   344
 * Reload the config.
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   345
 */
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   346
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   347
function grey_reload_config()
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   348
{
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   349
  global $httpd;
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   350
  status('reloading the config');
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   351
  
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   352
  if ( file_exists('./greyhound-config.php') )
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   353
  {
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   354
    require('./greyhound-config.php');
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   355
  }
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   356
  else
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   357
  {
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   358
    // ignore this, it allows using a different config file when a Mercurial repository
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   359
    // exists in Greyhound's root directory (to allow the devs to have their own config
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   360
    // separate from the default)
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   361
    
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   362
    if ( @is_dir(GREY_ROOT . '/.hg') )
64
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 57
diff changeset
   363
    {
50
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   364
      require(GREY_ROOT . '/config.dev.php');
64
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 57
diff changeset
   365
    }
50
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   366
    else
64
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 57
diff changeset
   367
    {
50
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   368
      require(GREY_ROOT . '/config.php');
64
ee64bb096f56 A few miscellaneous fixes including modifying WebServer to write data in chunks (improved performance and reliability on a slow connection)
Dan
parents: 57
diff changeset
   369
    }
50
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   370
  }
52
ab3541465382 Fixed globals from config file not being properly loaded from grey_reload_config()
Dan
parents: 50
diff changeset
   371
  
57
31ce64a3ff6c Some backend changes to webserver to allow IPv6 binding
Dan
parents: 52
diff changeset
   372
  foreach ( array('public', 'enable_ipv4', 'enable_ipv6', 'allowcontrol', 'theme', 'allow_fork', 'use_auth', 'auth_data', 'configpass') as $var )
52
ab3541465382 Fixed globals from config file not being properly loaded from grey_reload_config()
Dan
parents: 50
diff changeset
   373
  {
ab3541465382 Fixed globals from config file not being properly loaded from grey_reload_config()
Dan
parents: 50
diff changeset
   374
    if ( isset($$var) )
ab3541465382 Fixed globals from config file not being properly loaded from grey_reload_config()
Dan
parents: 50
diff changeset
   375
    {
ab3541465382 Fixed globals from config file not being properly loaded from grey_reload_config()
Dan
parents: 50
diff changeset
   376
      $GLOBALS[$var] = $$var;
ab3541465382 Fixed globals from config file not being properly loaded from grey_reload_config()
Dan
parents: 50
diff changeset
   377
    }
ab3541465382 Fixed globals from config file not being properly loaded from grey_reload_config()
Dan
parents: 50
diff changeset
   378
  }
50
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   379
}
1b4288399b1f Added graphical configuration, at this point only for the grey theme but others will follow soon. (This has been nearly done for two weeks or more but was on hold due to the bugs with multithreading)
Dan
parents: 34
diff changeset
   380
74
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   381
/**
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   382
 * Decodes a hex string.
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   383
 * @param string $hex The hex code to decode
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   384
 * @return string
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   385
 */
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   386
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   387
function hexdecode($hex)
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   388
{
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   389
  $hex = str_split($hex, 2);
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   390
  $bin_key = '';
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   391
  foreach($hex as $nibble)
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   392
  {
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   393
    $byte = chr(hexdec($nibble));
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   394
    $bin_key .= $byte;
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   395
  }
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   396
  return $bin_key;
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   397
}
7719085707d8 Security: Sessions are now based on HMAC-MD5 salts instead of concatenation
Dan
parents: 69
diff changeset
   398