default
[ class tree: default ] [ index: default ] [ all elements ]

Source for file RSFile.php

Documentation is available at RSFile.php

  1. <?php
  2.  
  3. /**
  4.  * 
  5.  * Simple rapidshare file checker class.
  6.  * Usage:
  7.  *  $files = RSFile::getInstance($id1, $name1);
  8.  *  $files = RSFile::getInstance($id2, $name2);
  9.  *  $files->processFiles();
  10.  *  if ($files->getStatus() === RSFile::STATUS_FILE_OK) {
  11.  *      echo $files->getSize(RSFile::SIZE_MB);
  12.  *      echo $files->getDownloadMirror();
  13.  *  }
  14.  * 
  15.  * @version v1.0
  16.  * @author Alex
  17.  *
  18.  */
  19. class RSFile implements Iterator {
  20.     
  21.     /**
  22.      * The file was not found on the rapidshare servers.
  23.      * @var integer 
  24.      */
  25.     const STATUS_FILE_NOT_FOUND 0;
  26.     
  27.     /**
  28.      * File was found and everything is OK.
  29.      * @var integer 
  30.      */
  31.     const STATUS_FILE_OK        1;
  32.     
  33.     /**
  34.      * The rapidshare server is down.
  35.      * @var integer 
  36.      */
  37.     const STATUS_SERVER_DOWN    3;
  38.     
  39.     /**
  40.      * The file was marked as illegal.
  41.      * @var integer 
  42.      */
  43.     const STATUS_ILLEGAL_FILE   4;
  44.     
  45.     /**
  46.      * Format file size to bytes (by default).
  47.      * @var string 
  48.      */
  49.     const SIZE_B  'b';
  50.     
  51.     /**
  52.      * Format file size to kilobytes
  53.      * @var string 
  54.      */
  55.     const SIZE_KB 'kb';
  56.     
  57.     /**
  58.      * Format file size to megabytes
  59.      * @var string 
  60.      */
  61.     const SIZE_MB 'mb';
  62.     
  63.     /**
  64.      * Format file size to gigabytes
  65.      * @var string 
  66.      */
  67.     const SIZE_GB 'gb';
  68.     
  69.     /**
  70.      * The maximum number of calls to the rapidshare servers.
  71.      * @var integer 
  72.      */
  73.     const MAX_SERVER_CALLS 3;
  74.     
  75.     /**
  76.      * Stores the latest error (if any)
  77.      * @var string 
  78.      */
  79.     private $_error = null;
  80.     
  81.     /**
  82.      * Stores the latest error number (if any)
  83.      * @var string 
  84.      */
  85.     private $_errno = null;
  86.     
  87.     /**
  88.      * Stores the current file id
  89.      * @var string 
  90.      */
  91.     private $_id;
  92.     
  93.     /**
  94.      * Stores the current file name
  95.      * @var string 
  96.      */
  97.     private $_name;
  98.     
  99.     /**
  100.      * Stores the current file size in bytes
  101.      * @var integer 
  102.      */
  103.     private $_size;
  104.     
  105.     /**
  106.      * Stores the server id for the current file
  107.      * @var integer 
  108.      */
  109.     private $_serverId;
  110.     
  111.     /**
  112.      * Stores the status for the current file
  113.      * @var integer 
  114.      */
  115.     private $_status;
  116.     
  117.     /**
  118.      * Stores the short host for the current file
  119.      * @var string 
  120.      */
  121.     private $_shortHost;
  122.     
  123.     /**
  124.      * Stores the md5sum of the current file
  125.      * @var string 
  126.      */
  127.     private $_md5sum;
  128.  
  129.     /**
  130.      * We use this simple variable to know if more than MAX_SERVER_CALLS calls
  131.      * are made to the rapidshare server, you should never make more than one
  132.      * call to processFiles() function that connects to the rapidshare servers.
  133.      * @var integer 
  134.      */
  135.     private static $calls 0;
  136.  
  137.     /**
  138.      * The list of instances of this class
  139.      * @var object 
  140.      */
  141.     private static $instances array();
  142.         
  143.     /**
  144.      * Private constructor
  145.      */
  146.     private function  __construct($id$name{
  147.         $this->_id   = $id;
  148.         $this->_name = $name;
  149.     }
  150.     
  151.     /**
  152.      * Returns only one instance of this class per id/name
  153.      *
  154.      * @param string $id File id on rapidshare servers
  155.      * @param string $name File name on rapidshare servers
  156.      *
  157.      * @return object 
  158.      */
  159.     public static function getInstance($id$name{
  160.         $instance = (string) $id.$name;
  161.         if (!empty($instance)) {
  162.             if (!isset(self::$instances[$instance]|| !self::$instances[$instanceinstanceof self)
  163.                 self::$instances[$instancenew self($id$name);
  164.     
  165.             return self::$instances[$instance];
  166.         }
  167.         trigger_error("Empty instance id!"E_USER_ERROR);
  168.     }
  169.     
  170.     /**
  171.      * Removes the instance.
  172.      * @param string $id 
  173.      */
  174.     public function unsetInstance($id$name{
  175.         $instance = (string) $id.$name;
  176.         if (!empty($instance)) {
  177.             if (isset(self::$instances[$instance]))
  178.                 unset(self::$instances[$instance]);
  179.             return true;
  180.         }
  181.         trigger_error("Empty instance id!"E_USER_ERROR);
  182.     }
  183.     
  184.     /**
  185.      * Returns the id for the current file
  186.      * @return string 
  187.      */
  188.     public function getFileId({
  189.         return $this->_id;
  190.     }
  191.     
  192.     /**
  193.      * Returns the name of the current file
  194.      * @return string 
  195.      */
  196.     public function getFileName({
  197.         return $this->_name;
  198.     }
  199.     
  200.     /**
  201.      * Returns the size of the current file
  202.      * @param string $format Use one of the defined SIZE_* constants
  203.      * @param int $precision Precision for round function
  204.      * 
  205.      * @return float The file size changed in the desired format.
  206.      */
  207.     public function getFileSize($format self::SIZE_B$precision 2{
  208.         
  209.         if (!is_int($precision))
  210.             $precision = (int) $precision;
  211.         
  212.         switch ($format{
  213.             case self::SIZE_GB:
  214.                 return round($this->_size / (1024 1024 1024)$precision);
  215.                 
  216.             case self::SIZE_MB:
  217.                 return round($this->_size / (1024 1024)$precision);
  218.                         
  219.             case self::SIZE_KB:
  220.                 return round($this->_size / 1024$precision);
  221.             
  222.             default:
  223.             case self::SIZE_B:
  224.                 return $this->_size;
  225.         }    
  226.     }
  227.     
  228.     /**
  229.      * Returns the server id where the file is hosted.
  230.      * @return integer 
  231.      */
  232.     public function getServerId({
  233.         return $this->_serverId;
  234.     }
  235.     
  236.     /**
  237.      * Returns the status of the current file
  238.      * @returns integer.
  239.      */
  240.     public function getFileStatus({
  241.         return $this->_status;
  242.     }
  243.     
  244.     /**
  245.      * Returns the short host for the current file, is used to get the best
  246.      * download mirror.
  247.      * @return string. 
  248.      */
  249.     public function getShortHost({
  250.         return $this->_shortHost;
  251.     }
  252.     
  253.     /**
  254.      * Returns the best download mirror for the current file or null if the
  255.      * file status is not equal to 1
  256.      * 
  257.      * @return mixed 
  258.      */
  259.     public function getDownloadMirror({
  260.         // The status of the file MUST be ok or there is no file.
  261.         if ($this->getFileStatus(!== self::STATUS_FILE_OK)
  262.             return null;
  263.         
  264.         // http://rs$serverid$shorthost.rapidshare.com/files/$fileid/$filename
  265.         $url  'http://rs';
  266.         $url .= $this->getServerId();
  267.         $url .= $this->getShortHost();
  268.         $url .= '.rapidshare.com/files/';
  269.         $url .= $this->getFileId();
  270.         $url .= '/';
  271.         $url .= $this->getFileName();
  272.         return $url;
  273.     }
  274.     
  275.     /**
  276.      * Returns the md5sum of the current file
  277.      * @return string 
  278.      */
  279.     public function getMd5sum({
  280.         return $this->_md5sum;
  281.     }
  282.  
  283.     /**
  284.      * Simple implode function but works with multiton objects.
  285.      *
  286.      * @param string $glue String separator
  287.      * @param object $objs Instance of class implementing Iterator interface
  288.      * @param string $callback Object method used to get the string for
  289.      *  concatenation
  290.      */
  291.     public function implodeObjs($glue$objs$callback{
  292.     
  293.         $returns '';
  294.         foreach ($objs as $obj{
  295.             $returns .= $obj->$callback();
  296.             $returns .= $glue;
  297.         }
  298.     
  299.         /*
  300.          * If the separator is not an empty string then remove it from the end 
  301.          * of the final string.
  302.          */
  303.         if (isset($glue[0]))
  304.             $returns substr($returns0(count($glue* -1));
  305.     
  306.         return $returns;
  307.     }
  308.     
  309.     /**
  310.      * This function must be called only after every file is already created.
  311.      * e.g. $files = RSFile::getInstance(id1, name1)->getInstance(id2, name2)...
  312.      * $files->processFiles();
  313.      * foreach($files as $file)
  314.      *     $file->getFileSize() ...
  315.      *     
  316.      * @return boolean true on success and false otherwise.
  317.      */
  318.     public function processFiles({
  319.         
  320.         // Make sure we dont call to many times the rapidshare api.
  321.         if (self::$calls >= self::MAX_SERVER_CALLS{
  322.             $this->_error = 'You can NOT call this method more than 
  323.                     MAX_SERVER_CALLS times!';
  324.             $this->_errno = '0001';
  325.             return false;
  326.         }
  327.         // Get file ids separated by comma
  328.         $files     $this->implodeObjs(','$this'getFileId');
  329.         // Get file names separated by comma
  330.         $filenames $this->implodeObjs(','$this'getFileName');
  331.         
  332.         // Some checking before getting any error from the server.
  333.         if (count($files3000 || count($filenames30000{
  334.             $this->_error = 'There is a server based limitation. Files are limited
  335.                      to 3000 characters and filenames to 30.000';
  336.             $this->_errno = '0002';
  337.             return false;
  338.         }
  339.         
  340.         // Create all POST fields
  341.         $chPostFields  'sub=checkfiles';
  342.         $chPostFields .= '&files=';
  343.         $chPostFields .= $files;
  344.         $chPostFields .= '&filenames=';
  345.         $chPostFields .= $filenames;
  346.         
  347.         // Initiate cURL
  348.         $ch curl_init('http://api.rapidshare.com/cgi-bin/rsapi.cgi');
  349.         curl_setopt($chCURLOPT_POST1);
  350.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  351.         curl_setopt($chCURLOPT_POSTFIELDS$chPostFields);
  352.  
  353.         // Get all files from the response
  354.         $data curl_exec($ch);
  355.         if ($data === false{
  356.             // There was an error. Take note of that...
  357.             $this->_error = curl_error($ch);
  358.             $this->_errno = curl_errno($ch);
  359.             
  360.             // Close the connection
  361.             @curl_close($ch);
  362.             
  363.             // Return false so we know ther is an error.
  364.             return false;
  365.         }
  366.         // Explode the results
  367.         $files explode("\n"$data);
  368.         
  369.         // Remove the last item
  370.         array_pop($files);
  371.         foreach ($files as $file{
  372.             list($id$name$size$serverId$status$shortHost$md5sum)
  373.                 = explode(','$file);
  374.             // Set all fields.
  375.             self::$instances[$id.$name]->_size      = (int) $size;
  376.             self::$instances[$id.$name]->_serverId  = (int) $serverId;
  377.             self::$instances[$id.$name]->_status    = (int) $status;
  378.             self::$instances[$id.$name]->_shortHost $shortHost;
  379.             self::$instances[$id.$name]->_md5sum    $md5sum;
  380.         }
  381.         
  382.         curl_close($ch);
  383.         self::$calls++;
  384.         
  385.         return true;
  386.     }
  387.     
  388.     /**
  389.      * Returns the last error description.
  390.      * @return string 
  391.      */
  392.     public function error({
  393.         return $this->_error;
  394.     }
  395.     
  396.     /**
  397.      * Returns the last error number.
  398.      * @return string 
  399.      */
  400.     public function errno({
  401.         return $this->_errno;
  402.     }
  403.     
  404.     /**
  405.      * No serialization allowed
  406.      */
  407.     public function __sleep({
  408.         trigger_error("No serialization allowed!"E_USER_ERROR);
  409.     }
  410.     
  411.     /**
  412.      * No cloning allowed
  413.      */
  414.     public function __clone({
  415.         trigger_error("No cloning allowed!"E_USER_ERROR);
  416.     }
  417.  
  418.     /**
  419.      * Implementing Iterator
  420.      */
  421.     public function current({
  422.         return current(self::$instances);
  423.     }
  424.     
  425.     public function key({
  426.         return key(self::$instances);
  427.     }
  428.     
  429.     public function next({
  430.         return next(self::$instances);
  431.     }
  432.     
  433.     public function rewind({
  434.         reset(self::$instances);
  435.     }
  436.     
  437.     public function valid({
  438.         return current(self::$instances!== false;
  439.     }
  440. }

Documentation generated on Wed, 22 Feb 2012 12:18:08 +0100 by phpDocumentor 1.4.3