Advertisement
Guest User

Webui_shell magnet link support

a guest
Oct 31st, 2012
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 30.82 KB | None | 0 0
  1. <?php
  2. /*
  3.  * WEBUI-SHELL for use with utorrent and its webui - http://www.utorrent.com
  4.  *
  5.  * Version 0.7.0
  6.  *
  7.  * Copyright (C) 2008 Tjores Maes
  8.  *
  9.  *  This program is free software: you can redistribute it and/or modify
  10.  *  it under the terms of the GNU General Public License as published by
  11.  *  the Free Software Foundation, either version 3 of the License, or
  12.  *  (at your option) any later version.
  13.  *
  14.  *  This program is distributed in the hope that it will be useful,
  15.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *  GNU General Public License for more details.
  18.  *
  19.  *  You should have received a copy of the GNU General Public License
  20.  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21.  *
  22.  * File Name: webui.php
  23.  *  File containing the webui interaction script
  24.  *
  25.  * File Author:
  26.  *      Tjores Maes (lordalderaan@gmail.com)
  27.  *
  28.  * Contributors:
  29.  *      asrael...
  30.  */
  31.  
  32. // Logged in as user: get needed variables
  33. $options=$database->getoptions($_SESSION['userid']);
  34. $actions=$database->getactions($_SESSION['userid']);
  35. $instance=$database->getinstances($_SESSION['userid'],'userid');
  36. if ( empty($instance) )
  37. {
  38.     $database->fail('Configuration error: This user has no valid instance.');
  39. }
  40. $instanceid=$instance['instanceid'];
  41. if ($options['User_Disabled'] === '1')
  42. {
  43.     setcookie('shell_clogin','',1);
  44.     setcookie('shell_cpw','',1);
  45.     $_SESSION=Array('userid'=>$_SESSION['userid']);
  46.     $_SESSION['recentlogout']=time();
  47.     $database->fail('This user has been disabled.');
  48. }
  49. if ($options['Show_Unclaimed_Torrents'] === '1')
  50. {
  51.     $torrents=$database->gettorrents($_SESSION['userid'],$instanceid,true);
  52. }
  53. else
  54. {
  55.     $torrents=$database->gettorrents($_SESSION['userid'],$instanceid);
  56. }
  57. if ( $options['Remember_Last_IP'] === '1' )
  58. {
  59.     $database->upd('users',Array('ip'=>$_SERVER['REMOTE_ADDR']),Array('userid'=>$_SESSION['userid']));
  60. }
  61. $url='http://'.$instance['domain'].':'.$instance['port'].'/gui/'.$file;
  62. // Check for multiple identical query fields (ie: s=a&s=b)
  63. // If so it returns array with all fields, otherwise returns false
  64. $fullrequest=query_array($_SERVER['QUERY_STRING']);
  65. // Filter request before they are forwarded
  66. if (!empty($_REQUEST['hash']))
  67. {
  68.     hashallowed();
  69. }
  70. if (!empty($_REQUEST['action']))
  71. {
  72.     foreach ($fullrequest['action'] as $action_i => $action)
  73.     {
  74.         switch ($action['value'])
  75.         {
  76.             case 'setsetting':
  77.                 if ($_REQUEST['s']=='webui.cookie')
  78.                 {
  79.                     // Webui cookie is always saved alone so no need to check $fullrequest
  80.                     // Webui cookie saved to database (per user) instead of utorrent
  81.                     /*
  82.                     $_SESSION['webui.cookie']=$_REQUEST['v'];
  83.                     $database->upd('users',Array('cookie'=>$_REQUEST['v']),Array('userid'=>$_SESSION['userid']));
  84.                     */
  85.                     header('Content-type: text/plain; charset=utf-8');
  86.                     die(json_encode(Array('build'=>$_SESSION['build'])));
  87.                 }
  88.                 if ( $options['Change_Settings'] !== '1' )
  89.                 {
  90.                     $database->fail('Changing settings not allowed for this user.');
  91.                 }
  92.             break;
  93.             case 'setprops':
  94.                 if ( $options['Set_Torrent_Properties'] !== '1' )
  95.                 {
  96.                     $database->fail('Changing Torrent Properties not allowed for this user.');
  97.                 }
  98.             break;
  99.             case 'start':
  100.             case 'stop':
  101.             case 'pause':
  102.             case 'unpause':
  103.                 if ( $options['Start_Stop_Pause_Unpause'] !== '1' )
  104.                 {
  105.                     $database->fail('Starting, Stopping and Pauzing torrents not allowed for this user.');
  106.                 }
  107.             break;
  108.             case 'forcestart':
  109.                 if ( $options['Force_Start'] !== '1' )
  110.                 {
  111.                     $database->fail('Force starting torrents not allowed for this user.');
  112.                 }
  113.             break;
  114.             case 'recheck':
  115.                 if ( $options['Recheck'] !== '1' )
  116.                 {
  117.                     $database->fail('Rechecking not allowed for this user.');
  118.                 }
  119.             break;
  120.             case 'remove':
  121.                 if ( $options['Remove'] !== '1' )
  122.                 {
  123.                     $database->fail('Removing torrents not allowed for this user.');
  124.                 }
  125.                 $_SESSION['fulllist']=-1;
  126.                 if ( $options['Unassign_on_Remove'] === '1' )
  127.                 {
  128.                     unassign();
  129.                 }
  130.             break;
  131.             case 'removedata':
  132.                 if ( $options['Remove_Data'] !== '1' )
  133.                 {
  134.                     $database->fail('Removing torrents and its data not allowed for this user.');
  135.                 }
  136.                 $_SESSION['fulllist']=-1;
  137.                 if ( $options['Unassign_on_Remove'] === '1' )
  138.                 {
  139.                     unassign();
  140.                 }
  141.             break;
  142.             case 'removetorrent':
  143.                 // webapi 3.0
  144.                 if ( $options['Remove_Data'] !== '1' )
  145.                 {
  146.                     $database->fail('Removing torrents and its data not allowed for this user.');
  147.                 }
  148.                 $_SESSION['fulllist']=-1;
  149.                 if ( $options['Unassign_on_Remove'] === '1' )
  150.                 {
  151.                     unassign();
  152.                 }
  153.             break;
  154.             case 'removedatatorrent':
  155.                 // webapi 3.0
  156.                 if ( $options['Remove_Data'] !== '1' )
  157.                 {
  158.                     $database->fail('Removing torrents and its data not allowed for this user.');
  159.                 }
  160.                 $_SESSION['fulllist']=-1;
  161.                 if ( $options['Unassign_on_Remove'] === '1' )
  162.                 {
  163.                     unassign();
  164.                 }
  165.             break;
  166.             case 'setprio':
  167.                 if ( $options['Set_File_Priority'] !== '1' )
  168.                 {
  169.                     $database->fail('Changing file priority not allowed for this user.');
  170.                 }
  171.             break;
  172.             case 'add-url':
  173.                 // Download add-url torrent and add as file.
  174.                 if ( $options['Add_Torrents'] !== '1' )
  175.                 {
  176.                     $database->fail('Adding torrents not allowed for this user.');
  177.                 }
  178.                 $req_url=false;
  179.                 foreach ($fullrequest['s'] as $s_i => $s)
  180.                 {
  181.                     if ( $s['order'] == $action['order'] + 1 )
  182.                     {
  183.                         $req_url=$s['value'];
  184.                         break;
  185.                     }
  186.                 }
  187.                 if ( $req_url === false )
  188.                 {
  189.                     $database->fail('No torrent url found.');
  190.                 }
  191.                 // If it's a magnet link we want to store and continue.
  192.                 if (!strncmp($req_url, 'magnet:?', 8))
  193.                 {
  194.                     $magnetlink=$req_url;
  195.                     break;
  196.                 }
  197.                 if (stripos($req_url,':COOKIE:') === false)
  198.                 {
  199.                     $add_url=$req_url;
  200.                     $add_cookie='';
  201.                 }
  202.                 else
  203.                 {
  204.                     $add_string=explode(':COOKIE:',$req_url);
  205.                     $add_url=$add_string[0];
  206.                     $add_cookie=$add_string[1];
  207.                 }
  208.                 $curl_headers=array(
  209.                     "Accept-Language: en-us,en;q=0.5",
  210.                     "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
  211.                     "Keep-Alive: 300",
  212.                     "Connection: keep-alive"
  213.                 );
  214.                 $curl_opts=Array(
  215.                     CURLOPT_FRESH_CONNECT => 1,
  216.                     CURLOPT_FORBID_REUSE => 1,
  217.                     CURLOPT_HEADER => 0,
  218.                     CURLOPT_RETURNTRANSFER => 1,
  219.                     CURLOPT_URL => str_replace(' ','%20',trim($add_url)),
  220.                     CURLOPT_SSL_VERIFYPEER => FALSE,
  221.                     CURLOPT_FOLLOWLOCATION => 1,
  222.                     CURLOPT_USERAGENT => 'BTuTWebuiShell',
  223.                     CURLOPT_ENCODING => ''
  224.                 );
  225.                 if (!empty($add_cookie))
  226.                 {
  227.                     $curl_opts['CURLOPT_COOKIE']=$add_cookie;
  228.                 }
  229.                 $ch = curl_init();
  230.                 curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers);
  231.                 curl_setopt_array($ch, $curl_opts);
  232.                 $results = curl_exec ($ch);
  233.                 if ( curl_errno($ch) !== 0 )
  234.                 {
  235.                     $database->fail('cURL error: '.curl_error($ch).' ('.curl_errno($ch).')');
  236.                 }
  237.                 if (curl_getinfo($ch,CURLINFO_HTTP_CODE) !== 200)
  238.                 {
  239.                     $database->fail('Adding this torrent failed. The file could not be retrieved from the url.');
  240.                 }
  241.                 curl_close ($ch);
  242.                 unset($ch);
  243.                 $torrentfile=$cfg["settings_dir"].'temp.torrent';
  244.                 if (file_put_contents($torrentfile,$results) === false)
  245.                 {
  246.                     $database->fail('Adding this torrent failed. Configuration Error: No read/write access to settings folder.');
  247.                 }
  248.                 // Remove add-url and use add-file instead.
  249.                 unset($fullrequest['action'][$action_i]);
  250.                 unset($fullrequest['s'][$s_i]);
  251.                 $fullrequest['action'][]=Array('value'=>'add-file','order'=>'9');
  252.             break;
  253.             case 'add-file':
  254.                 if ( $options['Add_Torrents'] !== '1' )
  255.                 {
  256.                     $database->fail('Adding torrents not allowed for this user.');
  257.                 }
  258.                 if ( !empty($_FILES) && !empty($_FILES['torrent_file']) && file_exists($_FILES['torrent_file']['tmp_name']) )
  259.                 {
  260.                     $torrentfile=$_FILES['torrent_file']['tmp_name'];
  261.                 }
  262.                 else
  263.                 {
  264.                     $database->fail('Adding this torrent failed. Uploaded file unavailable.');
  265.                 }
  266.             break;
  267.             case 'getprops':
  268.                 if ( $options['View_Torrent_Properties'] !== '1' )
  269.                 {
  270.                     $database->fail('Viewing Torrent Properties not allowed for this user.');
  271.                 }
  272.             break;
  273.             case 'queuebottom':
  274.             case 'queuedown':
  275.             case 'queuetop':
  276.             case 'queueup':
  277.                 if ( $options['Allow_Manage_Queue'] !== '1' )
  278.                 {
  279.                     $database->fail('Changing queue order not allowed for this user.');
  280.                 }
  281.             break;
  282.             case 'getxferhist':
  283.                 // api 3.0
  284.                 if ( $options['Allow_DLUL_Statistics'] !== '1' )
  285.                 {
  286.                     $database->fail('Viewing Statistics not allowed for this user.');
  287.                 }
  288.             break;
  289.             case 'resetxferhist':
  290.                 // api 3.0
  291.                 if ( $options['Change_Settings'] !== '1' )
  292.                 {
  293.                     $database->fail('Changing (xferhist) settings not allowed for this user.');
  294.                 }
  295.             break;
  296.             case 'rss-remove':
  297.             case 'rss-update':
  298.             case 'filter-remove':
  299.             case 'filter-update':
  300.                 // api 3.0
  301.                 // Per user RSS settings are planned for 0.8.0
  302.                 // Allow or refuse based on Change_Settings for now.
  303.                 if ( $options['Change_Settings'] !== '1' )
  304.                 {
  305.                     $database->fail('Changing (RSS) settings not allowed for this user.');
  306.                 }
  307.             break;
  308.             case 'list-dirs':
  309.                 // api 3.0
  310.             case 'getversion': 
  311.                 // api 3.0
  312.             case 'getsettings':
  313.             case 'getfiles':
  314.                 // These actions are forwarded without interference.
  315.             break;
  316.             default:
  317.                 // The remaining actions are Unknown. Checking against Unknown actions settings.
  318.                 if ( array_key_exists($action['value'],$actions) )
  319.                 {
  320.                     if ( $actions[$action['value']] !== '1' )
  321.                     {
  322.                         $database->fail('Unknown action \''.$action['value'].'\' specifically not allowed for this user.');
  323.                     }
  324.                 }
  325.                 elseif ( $options['Allow_Unknown_Actions'] !== '1' )
  326.                 {
  327.                     $database->fail('Doing Unknown actions not allowed for this user.');
  328.                 }
  329.             break;
  330.         }
  331.     }
  332.     if ( isset($torrentfile) || isset($magnetlink) )
  333.     {
  334.         if (isset($torrentfile))
  335.         {
  336.             // If there is a torrentfile from add-torrent or add-url check against Existing torrents settings
  337.             // This system is not yet waterproof.
  338.             $bdecode=BDecode(file_get_contents($torrentfile));
  339.             if (!is_array($bdecode) || !isset($bdecode['info']) || !isset($bdecode['info']['name']))
  340.             {
  341.                 $database->fail('Not a valid torrent file.');
  342.             }
  343.             $hash=strtoupper(sha1(BEncode($bdecode['info'])));
  344.  
  345.             // Get torrent size
  346.             if (array_key_exists('files',$bdecode['info']))
  347.             {
  348.                 $size=0;
  349.                 foreach ($bdecode['info']['files'] as $item)
  350.                 {
  351.                     $size+=$item['length'];
  352.                 }
  353.             }
  354.             else
  355.             {
  356.                 $size=$bdecode['info']['length'];
  357.             }
  358.             $name=$bdecode['info']['name'];
  359.         }
  360.         else
  361.         {
  362.             // First we want to extract the hash for reference.
  363.             $size=strpos($magnetlink,'urn:btih:',8)+9;
  364.             if ($size > (strlen($magnetlink)-32))
  365.             {
  366.                 $database->fail('Cannot locate (or incomplete) BitTorrent Info Hash (btih) in magnet link.');
  367.             }
  368.             $hash=substr($magnetlink, $size, 32);
  369.             // Now we want to extract the name.
  370.             $size=strpos($magnetlink,'dn=',8)+3;
  371.             $endnamepos=strpos($magnetlink,'&',$size);
  372.             if ($endnamepos == false)
  373.             {
  374.                 $endnamepos=strlen($magnetlink);
  375.             }
  376.             $name=substr($magnetlink, $size, $endnamepos);
  377.             $name=urldecode($name);
  378.             // Finally, we can't actually determine the size at this point, so we ignore.
  379.             // This means that we can't meaningfully do any quota filtering.
  380.             $size=0;
  381.         }
  382.  
  383.         // Get all torrents
  384.         $lasttorrents=$database->gettorrents(0,$instanceid);
  385.         if ( !is_array($lasttorrents) || count($lasttorrents) == 0 )
  386.         {
  387.             if ( $_SESSION['fulllist'] < ( time() - 1800 ) )
  388.             {
  389.                 $database->fail('Adding this torrent failed. There is no list of current torrents. Refresh the webui (Press Esc) and try again.');
  390.             }
  391.         }
  392.         // Check Quota Max Torrents
  393.         if ( is_numeric($options['Quota_Max_Torrents']) && $options['Quota_Max_Torrents'] > 0 )
  394.         {
  395.             $quota=0;
  396.             if ( $options['Show_All_Torrents'] === '1' )
  397.             {
  398.                 $quota=count($lasttorrents);
  399.             }
  400.             elseif ($options['Show_Unclaimed_Torrents'] === '1')
  401.             {
  402.                 foreach($torrents as $torrent)
  403.                 {
  404.                     if ($torrent['userid'] !== '0')
  405.                     {
  406.                         $quota++;
  407.                     }
  408.                 }
  409.             }
  410.             else
  411.             {
  412.                 $quota=count($torrents);
  413.             }
  414.             if ( $quota >= $options['Quota_Max_Torrents'] )
  415.             {
  416.                 $database->fail('No more then '.$options['Quota_Max_Torrents'].' torrents allowed for this user.'.$quota);
  417.             }
  418.         }
  419.         // Check Quota Max Combinbedsize
  420.         if ( is_numeric($options['Quota_Max_Combinedsize']) && $options['Quota_Max_Combinedsize'] > 0 )
  421.         {
  422.             $quota=0;
  423.             if ( $options['Show_All_Torrents'] === '1' )
  424.             {
  425.                 foreach($lasttorrents as $torrent)
  426.                 {
  427.                     $quota+=$torrent['size'];
  428.                 }
  429.             }
  430.             elseif ($options['Show_Unclaimed_Torrents'] === '1')
  431.             {
  432.                 foreach($torrents as $torrent)
  433.                 {
  434.                     if ($torrent['userid'] !== '0')
  435.                     {
  436.                         $quota+=$torrent['size'];
  437.                     }
  438.                 }
  439.             }
  440.             else
  441.             {
  442.                 foreach($torrents as $torrent)
  443.                 {
  444.                     $quota+=$torrent['size'];
  445.                 }
  446.             }
  447.             if ( $quota + $size > $options['Quota_Max_Combinedsize'] )
  448.             {
  449.                 $database->fail('No more then '.bytes($options['Quota_Max_Combinedsize']).' allowed. Loaded '.bytes($quota).' + new '.bytes($size).' = '.bytes($quota+$size).' total.');
  450.             }
  451.         }
  452.         // Check for existing torrent.
  453.         if ( array_key_exists($hash,$lasttorrents) )
  454.         {
  455.             if ( $options['Show_All_Torrents'] === '1' || array_key_exists($hash,$torrents) )
  456.             {
  457.                 $database->fail('Adding this torrent failed. This torrent is already loaded.');
  458.             }
  459.             elseif ( $options['Allow_Existing_Torrents'] === '1' )
  460.             {
  461.                 $database->upd('torrents',Array('userid'=>$_SESSION['userid'],'instanceid'=>$instanceid,'hash'=>$hash,'name'=>$name,'size'=>$size));
  462.                 $_SESSION['fulllist']=-1;
  463.                 header('Content-type: text/plain; charset=utf-8');
  464.                 die(json_encode(Array('build'=>$_SESSION['build'])));
  465.             }
  466.             else
  467.             {
  468.                 $database->fail('Adding this torrent failed. This torrent already exists. Adding existing torrents not allowed for this user.');
  469.             }
  470.         }
  471.         $database->upd('torrents',Array('userid'=>$_SESSION['userid'],'instanceid'=>$instanceid,'hash'=>$hash,'name'=>$name,'size'=>$size));
  472.         $_SESSION['fulllist']=-1;
  473.     }
  474. }
  475.  
  476. $cachedrequest=false;
  477. // $_REQUEST cannot handle multiple identical query fields so use the custom query array instead
  478. if ( $fullrequest !== false && count($fullrequest) > 0 )
  479. {
  480.     $newarray=Array();
  481.     foreach ($fullrequest as $key => $items)
  482.     {
  483.         if ( $key != session_name() && substr($key,0,6) != 'shell_' && $key !== 'Language' && $key != 't')
  484.         {
  485.             if ( $key == 'cid' )
  486.             {
  487.                 if ( $items[0]['value'] == $_SESSION['cid'] )
  488.                 {
  489.                     $cachedrequest=true;
  490.                 }
  491.             }
  492.             else
  493.             {
  494.                 $newarray[$key]=$items;
  495.             }
  496.         }
  497.     }
  498.     if ( empty($newarray['token']) && !empty($_SESSION['token']) )
  499.     {
  500.         $newarray['token'][]=Array('value'=>$_SESSION['token'],'order'=>100);
  501.     }
  502.     $url.='?'.query_string($newarray);
  503. }
  504. // Quick cleanup of old cookiejars
  505. if ( !file_exists($cfg["settings_dir"].'cookiejar.'.session_id().'.tmp'))
  506. {
  507.     if($cjdir = opendir($cfg["settings_dir"]))
  508.     {
  509.         while($cjfile = readdir($cjdir))
  510.         {
  511.             if( substr($cjfile,0,10) == 'cookiejar.' && substr($cjfile,-4) == '.tmp' )
  512.             {
  513.                 if ( filemtime($cfg["settings_dir"].$cjfile) < time() - 86400 )
  514.                 {
  515.                     unlink($cfg["settings_dir"].$cjfile);
  516.                 }
  517.             }
  518.         }
  519.         closedir($cjdir);
  520.     }
  521. }
  522. // Forward to webui
  523. $curl_opts=Array(
  524.     CURLOPT_FRESH_CONNECT => 0,
  525.     CURLOPT_FORBID_REUSE => 0,
  526.     CURLOPT_USERPWD => $instance['username'].':'.$instance['password'],
  527.     CURLOPT_HEADER => 0,
  528.     CURLOPT_RETURNTRANSFER => 1,
  529.     CURLOPT_URL => $url,
  530.     CURLOPT_SSL_VERIFYPEER => FALSE,
  531.     CURLOPT_FOLLOWLOCATION => 1,
  532.     CURLOPT_COOKIEFILE => $cfg["settings_dir"].'cookiejar.'.session_id().'.tmp',
  533.     CURLOPT_COOKIEJAR => $cfg["settings_dir"].'cookiejar.'.session_id().'.tmp'
  534. );
  535. if ( !empty($torrentfile) )
  536. {
  537.     $curl_opts[CURLOPT_POSTFIELDS]['torrent_file']='@'.$torrentfile;
  538. }
  539. $ch = curl_init();
  540. curl_setopt_array($ch, $curl_opts);
  541. $results = curl_exec ($ch);
  542. if ( curl_errno($ch) !== 0 )
  543. {
  544.     if ( curl_errno($ch) === 52 )
  545.     {
  546.         $database->fail('Configuration error: Blocked by ipfilter.dat OR cURL error: '.curl_error($ch).' ('.curl_errno($ch).')');
  547.     }
  548.     else
  549.     {
  550.         $database->fail('cURL error: '.curl_error($ch).' ('.curl_errno($ch).')');
  551.     }
  552. }
  553. if (curl_getinfo($ch,CURLINFO_HTTP_CODE) == 400)
  554. {
  555.     // Linux server invalid request issue, simply try agian. See http://forum.utorrent.com/viewtopic.php?id=89538
  556.     $results = curl_exec ($ch);
  557. }
  558. switch (curl_getinfo($ch,CURLINFO_HTTP_CODE))
  559. {
  560. case 404:
  561.     $database->fail('File not found.');
  562.     break;
  563. case 401:
  564.     $database->fail('Configuration Error: Wrong login details.');
  565.     break;
  566. case 300:
  567. case 400:
  568.     $database->fail('Configuration Error: Invalid request.');
  569.     break;
  570. case 200:
  571.     // Valid result from webui.
  572.     // Adapt contenttype from result
  573.     header('Content-type: '.curl_getinfo($ch,CURLINFO_CONTENT_TYPE).'; charset=utf-8');
  574.     $json=Array('build'=>$_SESSION['build'],'torrents'=>Array(),'settings'=>Array());
  575.     if ( curl_getinfo($ch,CURLINFO_CONTENT_TYPE) == 'text/html' )
  576.     {
  577.         // Grab token if present
  578.         preg_match("/<div[^>]*id=[\"']token[\"'][^>]*>([^<]*)<\/div>/",$results,$matches);
  579.         if (isset($matches[1]))
  580.         {
  581.             $_SESSION['token']=$matches[1];
  582.         }
  583.     }
  584.     elseif ( substr($file,-3) == '.js' )
  585.     {
  586.         // Replace /gui/ with Webui-Shell path
  587.         $results=str_replace('/gui',substr($cfg['altname'],0,-1),$results);
  588.     }
  589.     elseif ( curl_getinfo($ch,CURLINFO_CONTENT_TYPE) == 'text/plain' )
  590.     {
  591.         // Process plain text
  592.         // Grab JSON
  593.         $json=json_decode($results,true);
  594.         $_SESSION['build']=$json['build'];
  595.         // Process webui Errors
  596.         if (isset($json['error']))
  597.         {
  598.             $database->fail('WEBUI: '.$json['error']);
  599.         }
  600.         // The webui_shell does not use the Webui API cache. So torrentp, torrentm etc are unexpected.
  601.         if ( isset($json['torrentp']) || isset($json['torrentm']) || isset($json['rssfeedp']) || isset($json['rssfeedm']) || isset($json['rssfilterp']) || isset($json['rssfilterm']) )
  602.         {
  603.             $database->fail('Unexpected torrentp and torrentm. Please contact the Webui-Shell devs.');
  604.         }
  605.         // At this time RSS feeds could contain passkeys and are filtered until more options are provided.
  606.         if (isset($json['rssfeeds']))
  607.         {
  608.             $json['rssfeeds']=Array();
  609.         }
  610.         if (isset($json['rssfilters']))
  611.         {
  612.             $json['rssfilters']=Array();
  613.         }
  614.         // Files list
  615.         if (isset($json['files']))
  616.         {
  617.             // let it pass unmodified
  618.         }
  619.         // Props list
  620.         if (isset($json['props']))
  621.         {
  622.             if ( $options['View_Torrent_Properties'] !== '1' )
  623.             {
  624.                 $json['props']=Array();
  625.             }
  626.         }
  627.         // Transfer history
  628.         if (isset($json['transfer_history']))
  629.         {
  630.             if (  $options['Allow_DLUL_Statistics'] !== '1' )
  631.             {
  632.                 $json['transfer_history']=Array();
  633.             }
  634.         }
  635.         // Download dirs
  636.         if (isset($json['download-dirs']))
  637.         {
  638.             // api 3.0
  639.             // Unfiltered, need direct access to utorrent to manage anyways, admin decides what users see or not.
  640.             // Per user download dirs are planned for 0.8.0
  641.         }
  642.         // Process the torrent list.
  643.         if (isset($json['torrents']))
  644.         {
  645.             // Save full torrentlist to database for later use (Admin Panel, Existing torrent, Show unclaimed)
  646.             // On: First Load, New Torrent, Removed Torrent, 30 minutes old
  647.             if ( $_SESSION['fulllist'] < ( time() - 1800 ) )
  648.             {
  649.                 $database->deltorrent(0,$instanceid,-1);
  650.                 foreach ( $json['torrents'] as $item )
  651.                 {
  652.                     $database->upd('torrents',Array('userid'=>0,'instanceid'=>$instanceid,'hash'=>strtoupper($item[0]),'name'=>$item[2],'size'=>$item[3]));
  653.                 }
  654.                 if ($options['Show_All_Torrents'] !== '1' || ( is_numeric($options['Quota_Max_Torrents']) && $options['Quota_Max_Torrents'] > 0 ) || ( is_numeric($options['Quota_Max_Combinedsize']) && $options['Quota_Max_Combinedsize'] > 0 ) )
  655.                 {
  656.                     $database->cleantorrents($_SESSION['userid']);
  657.                     if ($options['Show_Unclaimed_Torrents'] === '1')
  658.                     {
  659.                         $database->checkclaimed($instanceid); // Check claimed torrents.
  660.                         $torrents=$database->gettorrents($_SESSION['userid'],$instanceid,true); // Reload Torrents in case of changes to claimed torrents.
  661.                     }
  662.                 }
  663.                 $_SESSION['fulllist']=time();
  664.             }
  665.             // Filter on allowed:
  666.             if ( $options['Show_All_Torrents'] === '1' )
  667.             {
  668.                 $t_filtered=$json['torrents'];
  669.             }
  670.             else
  671.             {
  672.                 $t_filtered=Array();
  673.                 $t_label=Array();
  674.                 foreach ($json['torrents'] as $t_item )
  675.                 {
  676.                     if ( array_key_exists($t_item[0],$torrents) )
  677.                     {
  678.                         $t_filtered[]=$t_item;
  679.                         if (!empty($t_item[11]))
  680.                         {
  681.                             if (array_key_exists($t_item[11],$t_label))
  682.                             {
  683.                                 $t_label[$t_item[11]]++;
  684.                             }
  685.                             else
  686.                             {
  687.                                 $t_label[$t_item[11]]=1;
  688.                             }
  689.                         }
  690.                     }
  691.                 }
  692.                 $json['label']=Array();
  693.                 foreach($t_label as $label => $count)
  694.                 {
  695.                     $json['label'][]=Array($label,$count);
  696.                 }
  697.             }
  698.             // Store labels in session:
  699.             $_SESSION['labels']=$json['label'];
  700.             // Compare to cache:
  701.             if ($cachedrequest)
  702.             {
  703.                 unset($json['torrents']);
  704.                 $t_newcache=Array();
  705.                 $json['torrentp']=Array();
  706.                 foreach ($t_filtered as $t_item)
  707.                 {
  708.                     $t_newcache[$t_item[0]]=$t_item;
  709.                     if ( array_key_exists($t_item[0],$_SESSION['torrentcache']) )
  710.                     {
  711.                         if ( count(array_diff_assoc($t_item,$_SESSION['torrentcache'][$t_item[0]])) > 0 )
  712.                         {
  713.                             $json['torrentp'][]=$t_item;
  714.                         }
  715.                     }
  716.                     else
  717.                     {
  718.                         $json['torrentp'][]=$t_item;
  719.                     }
  720.                 }
  721.                 $json['torrentm']=Array();
  722.                 foreach ($_SESSION['torrentcache'] as $t_hash => $t_item)
  723.                 {
  724.                     if ( !array_key_exists($t_hash,$t_newcache) )
  725.                     {
  726.                         $json['torrentm'][]=$t_hash;
  727.                     }
  728.                 }
  729.                 $_SESSION['torrentcache']=$t_newcache;
  730.             }
  731.             else
  732.             {
  733.                 $json['torrents']=$t_filtered;
  734.                 $_SESSION['torrentcache']=Array();
  735.                 foreach($t_filtered as $t_item)
  736.                 {
  737.                     $_SESSION['torrentcache'][$t_item[0]]=$t_item;
  738.                 }
  739.             }
  740.             // Set random torrentc:
  741.             $_SESSION['cid']=rand(1000000000,9999999999);
  742.             $json['torrentc']=$_SESSION['cid'];
  743.         }
  744.         // Process settings file.
  745.         if (isset($json['settings']))
  746.         {
  747.             foreach ($json['settings'] as &$item)
  748.             {
  749.                 if ($item[0] == 'webui.cookie' )
  750.                 {
  751.                     /*
  752.                     // Misssing 2.1 webui.cookie fix part 1
  753.                     $temp_cookiefound=true;
  754.                     // End of fix part 1
  755.                     //Replace webui.cookie with user/session cookie (unless that is empty, then take default cookie from the settings)
  756.                     if (empty($_SESSION['webui.cookie']))
  757.                     {
  758.                         $_SESSION['webui.cookie']=$item[2];
  759.                     }
  760.                     else
  761.                     {
  762.                         $item[2]=$_SESSION['webui.cookie'];
  763.                     }
  764.                     if (!empty($_SESSION['newlang']))
  765.                     {
  766.                         $tempcookie=json_decode($_SESSION['webui.cookie'],true);
  767.                         $tempcookie['lang']=$_SESSION['newlang'];
  768.                         $_SESSION['webui.cookie']=json_encode($tempcookie);
  769.                         $item[2]=$_SESSION['webui.cookie'];
  770.                         $database->upd('users',Array('cookie'=>$_SESSION['webui.cookie']),Array('userid'=>$_SESSION['userid']));
  771.                         unset($_SESSION['newlang']);
  772.                     }
  773.                     */
  774.                 }
  775.                 elseif ($item[0] == 'webui.enable' || $item[0] == 'gui.graphic_progress' )
  776.                 {
  777.                     //Let these settings through regardless of authorization.
  778.                 }
  779.                 elseif ($item[0] == 'webui.port' || $item[0] == 'bind_port')
  780.                 {
  781.                     if ($options['Change_Settings'] !== '1')
  782.                     {
  783.                         //Replace with -1 instead of 0.
  784.                         $item[2]=-1;
  785.                     }
  786.                 }
  787.                 else
  788.                 {
  789.                     if ($options['Change_Settings'] !== '1')
  790.                     {
  791.                     //Replace with 0, false or empty string depending on type.
  792.                         switch ($item[1])
  793.                         {
  794.                         case 0:
  795.                             $item[2]=0;
  796.                             break;
  797.                         case 1:
  798.                             $item[2]='false';
  799.                             break;
  800.                         case 2:
  801.                             $item[2]='';
  802.                             break;
  803.                         }
  804.                     }
  805.                 }
  806.             }
  807.             /*
  808.             // Misssing 2.1 webui.cookie fix part 2
  809.             if (!isset($temp_cookiefound))
  810.             {
  811.                 $json['settings'][]=Array('webui.cookie',2,$_SESSION['webui.cookie']);
  812.             }
  813.             // End of fix part 2
  814.             */
  815.         }
  816.         $results=json_encode($json);
  817.     }
  818.     if ( substr($file,0,5) == 'lang/' )
  819.     {
  820.         // Process language file or toolbar image based on users options
  821.         if(preg_match("/lang\s*=\s*(\[.*\])[;|,]/ms",$results,$matches)>0)
  822.         {
  823.             $langstrings=json_decode($matches[1]);
  824.             $to_clean=Array();
  825.             $to_disable=Array();
  826.             if ( count($langstrings) == 253 )
  827.             {
  828.                 // webui 0.370
  829.                 $disablestring=substr($langstrings[227],0,strpos($langstrings[227],'||'));
  830.                 if ($options['Change_Settings'] !== '1')
  831.                 {
  832.                     $to_clean=array_merge($to_clean,Array(89,90,91,92,93,94,95,96,216,217,218,219,220,221,222,223,224,225,226));
  833.                     $to_disable=array_merge($to_disable,Array(4));
  834.                 }              
  835.                 if ( $options['View_Torrent_Properties'] !== '1' )
  836.                 {
  837.                     $to_disable=array_merge($to_disable,Array(161));
  838.                     // Properties
  839.                 }              
  840.                 if ( $options['Set_Torrent_Properties'] !== '1' )
  841.                 {
  842.                     $to_disable=array_merge($to_disable,Array(159,177,202,203,204,205));
  843.                     // Torrent Properties, Label stuff
  844.                 }
  845.                 if ( $options['Start_Stop_Pause_Unpause'] !== '1' )
  846.                 {
  847.                     $to_disable=array_merge($to_disable,Array(160,164,165,208,211,212));
  848.                     // Pause, Start, Stop, Pause, Start, Stop
  849.                 }
  850.                 if ( $options['Force_Start'] !== '1' )
  851.                 {
  852.                     $to_disable=array_merge($to_disable,Array(158));
  853.                     // Force Start
  854.                 }
  855.                 if ( $options['Recheck'] !== '1' )
  856.                 {
  857.                     $to_disable=array_merge($to_disable,Array(157));
  858.                     // Force Re-check
  859.                 }
  860.                 if ( $options['Remove'] !== '1' )
  861.                 {
  862.                     $to_disable=array_merge($to_disable,Array(162,210));
  863.                     // Remove, Remove
  864.                 }
  865.                 if ( $options['Remove_Data'] !== '1' )
  866.                 {
  867.                     $to_disable=array_merge($to_disable,Array(163,156));
  868.                     // Remove And, Delete Data
  869.                 }
  870.                 if ( $options['Set_File_Priority'] !== '1' )
  871.                 {
  872.                     $to_disable=array_merge($to_disable,Array(152,153,154,155));
  873.                     // Priorities
  874.                 }
  875.                 if ( $options['Add_Torrents'] !== '1' )
  876.                 {
  877.                     $to_disable=array_merge($to_disable,Array(207));
  878.                     // Add Torrent, Add Torrent from url
  879.                 }              
  880.             }
  881.             elseif ( count($langstrings) == 333 )
  882.             {
  883.                 // Webui 0.380
  884.                 $disablestring=substr($langstrings[307],0,strpos($langstrings[307],'||'));
  885.                 if ($options['Change_Settings'] !== '1')
  886.                 {
  887.                     $to_clean=array_merge($to_clean,Array(114,115,116,117,118,119,120,121,292,293,294,295,296,297,298,299,300,301,302,303,304,305));
  888.                     $to_disable=array_merge($to_disable,Array(4));
  889.                 }              
  890.                 if ( $options['View_Torrent_Properties'] !== '1' )
  891.                 {
  892.                     $to_disable=array_merge($to_disable,Array(223));
  893.                     // Properties
  894.                 }              
  895.                 if ( $options['Set_Torrent_Properties'] !== '1' )
  896.                 {
  897.                     $to_disable=array_merge($to_disable,Array(151,221,266,267,268,269));
  898.                     // Torrent Properties, Label stuff
  899.                 }
  900.                 if ( $options['Start_Stop_Pause_Unpause'] !== '1' )
  901.                 {
  902.                     $to_disable=array_merge($to_disable,Array(273,278,279,222,228,229));
  903.                     // Pause, Start, Stop, Pause, Start, Stop
  904.                 }
  905.                 if ( $options['Force_Start'] !== '1' )
  906.                 {
  907.                     $to_disable=array_merge($to_disable,Array(220));
  908.                     // Force Start
  909.                 }
  910.                 if ( $options['Recheck'] !== '1' )
  911.                 {
  912.                     $to_disable=array_merge($to_disable,Array(219));
  913.                     // Force Re-check
  914.                 }
  915.                 if ( $options['Remove'] !== '1' )
  916.                 {
  917.                     $to_disable=array_merge($to_disable,Array(226,277));
  918.                     // Remove, Remove
  919.                 }
  920.                 if ( $options['Remove_Data'] !== '1' )
  921.                 {
  922.                     $to_disable=array_merge($to_disable,Array(227,216,217,218));
  923.                     // Remove And, Delete Data
  924.                 }
  925.                 if ( $options['Set_File_Priority'] !== '1' )
  926.                 {
  927.                     $to_disable=array_merge($to_disable,Array(212,213,214,215));
  928.                     // Priorities
  929.                 }
  930.                 if ( $options['Add_Torrents'] !== '1' )
  931.                 {
  932.                     $to_disable=array_merge($to_disable,Array(271,272));
  933.                     // Add Torrent, Add Torrent from url
  934.                 }              
  935.                 if ( $options['Allow_Manage_Queue'] !== '1' )
  936.                 {
  937.                     $to_disable=array_merge($to_disable,Array(224,225,275,276));
  938.                     // Move Down Queue, Move Up Queue, Move Down Queue, Move Up Queue
  939.                 }
  940.             }
  941.             elseif ( count($langstrings) == 274 )
  942.             {
  943.                 // webui linux
  944.                 $disablestring=substr($langstrings[231],0,strpos($langstrings[231],'||'));
  945.                 if ($options['Change_Settings'] !== '1')
  946.                 {
  947.                     $to_clean=array_merge($to_clean,Array(90,91,92,93,94,95,96,220,221,222,223,224,225,226,227,228,229,230));
  948.                     $to_disable=array_merge($to_disable,Array(4));
  949.                 }              
  950.                 if ( $options['View_Torrent_Properties'] !== '1' )
  951.                 {
  952.                     $to_disable=array_merge($to_disable,Array(161));
  953.                     // Properties
  954.                 }              
  955.                 if ( $options['Set_Torrent_Properties'] !== '1' )
  956.                 {
  957.                     $to_disable=array_merge($to_disable,Array(114,159,204,205,206,207));
  958.                     // Torrent Properties, Label stuff
  959.                 }
  960.                 if ( $options['Start_Stop_Pause_Unpause'] !== '1' )
  961.                 {
  962.                     $to_disable=array_merge($to_disable,Array(160,166,167,210,215,216));
  963.                     // Pause, Start, Stop, Pause, Start, Stop
  964.                 }
  965.                 if ( $options['Force_Start'] !== '1' )
  966.                 {
  967.                     $to_disable=array_merge($to_disable,Array(158));
  968.                     // Force Start
  969.                 }
  970.                 if ( $options['Recheck'] !== '1' )
  971.                 {
  972.                     $to_disable=array_merge($to_disable,Array(157));
  973.                     // Force Re-check
  974.                 }
  975.                 if ( $options['Remove'] !== '1' )
  976.                 {
  977.                     $to_disable=array_merge($to_disable,Array(164,214));
  978.                     // Remove, Remove
  979.                 }
  980.                 if ( $options['Remove_Data'] !== '1' )
  981.                 {
  982.                     $to_disable=array_merge($to_disable,Array(165,156));
  983.                     // Remove And, Delete Data
  984.                 }
  985.                 if ( $options['Set_File_Priority'] !== '1' )
  986.                 {
  987.                     $to_disable=array_merge($to_disable,Array(152,153,154,155));
  988.                     // Priorities
  989.                 }
  990.                 if ( $options['Add_Torrents'] !== '1' )
  991.                 {
  992.                     $to_disable=array_merge($to_disable,Array(209));
  993.                     // Add Torrent, Add Torrent from url
  994.                 }              
  995.                 if ( $options['Allow_Manage_Queue'] !== '1' )
  996.                 {
  997.                     $to_disable=array_merge($to_disable,Array(162,163,212,213));
  998.                     // Move Down Queue, Move Up Queue, Move Down Queue, Move Up Queue
  999.                 }
  1000.             }
  1001.             if ( count($to_clean)>0 || count($to_disable)>0 )
  1002.             {
  1003.                 if ($cfg["disablelook"] == 1)
  1004.                 {
  1005.                     $disablestring='';
  1006.                 }
  1007.                 foreach  ($to_clean as $value)
  1008.                 {
  1009.                     $langstrings[$value]='';
  1010.                 }
  1011.                 foreach  ($to_disable as $value)
  1012.                 {
  1013.                     $langstrings[$value]=$disablestring;
  1014.                 }
  1015.                 $results=preg_replace("/lang\s*=\s*(\[.*\])([;|,])/ms",'lang='.json_encode($langstrings).'$2',$results);
  1016.             }
  1017.         }
  1018.     }
  1019.     elseif ( $file == 'images/toolbar.png' )
  1020.     {
  1021.         // Process toolbar image based on users options
  1022.         if ( $options['Start_Stop_Pause_Unpause'] !== '1' )
  1023.         {
  1024.             edittoolbar(Array(4,5,6));
  1025.         }
  1026.         if ( $options['Remove'] !== '1' )
  1027.         {
  1028.             edittoolbar(Array(3));
  1029.         }
  1030.         if ( $options['Add_Torrents'] !== '1' )
  1031.         {
  1032.             edittoolbar(Array(0,1));
  1033.         }
  1034.         if ( $options['Set_File_Priority'] !== '1' )
  1035.         {
  1036.             edittoolbar(Array(7,8));
  1037.         }
  1038.     }
  1039.     // return processed result or modified image
  1040.     if ( empty($img) )
  1041.     {
  1042.         echo $results;
  1043.     }
  1044.     else
  1045.     {
  1046.         imagepng($img);
  1047.     }
  1048.     break;
  1049. default:
  1050.     $database->fail('Invalid response code '.curl_getinfo($ch,CURLINFO_HTTP_CODE));
  1051.     break;
  1052. }
  1053. curl_close ($ch);
  1054. unset($ch);
  1055. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement