Advertisement
Faguss

OFP_ADD_DOWN - script for webservers

Apr 7th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.20 KB | None | 0 0
  1. <?php
  2. // Example of a PHP script communicating between the server and the OFP LoadMod.sqs script
  3. // Compliant with the standard described here:
  4. //      http://ofp-faguss.com/files/OFPAD_metadatastandard.pdf
  5. echo "_version=0.5;";
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. // ====================================================================================
  13. // Evaluate request type and return data
  14. // ====================================================================================
  15.  
  16. // Return address list for other addon databases --------------------------------------
  17. if (isset($_GET[othersites]))
  18. {
  19.     // Array with urls
  20.     // This is an example. You could store this in a database instead.
  21.     $list = array
  22.     (
  23.         // "http://www.address.example/for/ofp/download",                                   //CUSTOMIZE
  24.     );
  25.    
  26.     // Format it to an OFP array and return
  27.     echo "_url=[";
  28.     sort($list);
  29.     foreach ($list as $item) echo "]+[".formatString($item);
  30.     echo "];true";
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. // Search database --------------------------------------------------------------------
  39. else if (isset($_GET[search]))
  40. {
  41.     // Assign input variables
  42.     $search = $_GET[search];
  43.     $page = $_GET[page];
  44.     $calcTotalPages = !isset($_GET[page]);
  45.  
  46.     // Check input validity
  47.     if ($calcTotalPages || $page<1) $page=1;
  48.     if (get_magic_quotes_gpc()) $search=stripslashes($search);
  49.     if ($search==NULL||ctype_space($search)) die("_errorMSG=\"Search subject is empty\";false");
  50.  
  51.     // Determine starting record based on page number
  52.     $rowLimit = 10;                                                                         //CUSTOMIZE
  53.     $startRow = --$page * $rowLimit;
  54.  
  55.  
  56.     // Connect to the database
  57.     $link = mysqli_connect("", "", "", "");                                                 //CUSTOMIZE
  58.     if (!$link) die("_errorMSG=\"Couldn't connect to the database\";false");
  59.     $table = "ofpaddon";
  60.     mysqli_select_db($link, $table);
  61.     $search = mysqli_real_escape_string($link, $search);
  62.  
  63.     // Build query string
  64.     // In this example file size (in KB) is stored in the database
  65.     // but you could measure it on the fly
  66.     $query = "SELECT ";
  67.     if ($calcTotalPages) $query.="SQL_CALC_FOUND_ROWS ";
  68.     $query .=                                                                               //CUSTOMIZE
  69.     "
  70.         DISTINCT(filename), url, filesize
  71.         FROM {$table}
  72.         WHERE
  73.             pboname LIKE '%{$search}%'
  74.             OR
  75.             filename LIKE '%{$search}%'
  76.         LIMIT {$startRow}, {$rowLimit}
  77.     ";
  78.  
  79.     // Send query
  80.     // If page number was not specified then send 2nd query for total number of results
  81.     $result = mysqli_query($link, $query);
  82.     $result2 = true;
  83.     if ($calcTotalPages) $result2=mysqli_query($link, "SELECT FOUND_ROWS()");
  84.     if (!$result || !$result2)
  85.         die("_errorMSG=\"Invalid query:\\n".str_replace("\"","\"\"",mysqli_error($link))."\";false");
  86.  
  87.  
  88.     // If page number was not specified then calculate total number of pages
  89.     if ($calcTotalPages)
  90.     {
  91.         $temp = mysqli_fetch_row($result2);
  92.         $total_records = $temp[0];
  93.         $total_pages = ceil($total_records / $rowLimit);
  94.         echo "_pages=$total_pages;_results=$total_records;";
  95.     };
  96.  
  97.     // Put query results to an OFP array and then return it
  98.     echo "_files=[";
  99.     while($row = mysqli_fetch_assoc($result))
  100.     {
  101.         echo "]+[["                                                                         //CUSTOMIZE
  102.                 . formatTitle($row["filename"]) . ","
  103.                 . formatFileSize($row["filesize"]) . ","
  104.                 . formatString($row["url"]) . "]";
  105.     };
  106.     echo "];true";
  107.  
  108.    
  109.     // Free memory
  110.     mysqli_free_result($result);
  111.     mysqli_close($link);
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. // Predetermined list of files --------------------------------------------------------
  120. else if (isset($_GET[quickaccess]))
  121. {
  122.     // Array with file info
  123.     // This is an example. You could store this in a database instead.
  124.     $list = array
  125.     (
  126.         // array("Editor Addon 1.11","63","kegetys.net/ofp/KegetysEditorAddon111.zip"),     //CUSTOMIZE
  127.     );
  128.  
  129.     // Associate keys in the array
  130.     function renameKeys($subarray)
  131.     {
  132.         return array('filename'=>$subarray[0], 'filesize'=>$subarray[1], 'url'=>$subarray[2]);
  133.     };
  134.     $list = array_map("renameKeys", $list);
  135.    
  136.     // Put array contents to an OFP array and then return it
  137.     echo "_files=[";
  138.     foreach($list as $row)
  139.     {
  140.         echo "]+[["
  141.                 . formatTitle($row["filename"]) . ","
  142.                 . formatFileSize($row["filesize"]) . ","
  143.                 . formatString($row["url"]) . "]";
  144.     };
  145.     echo "];true";
  146. }
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153. // Unknown request --------------------------------------------------------------------
  154. else echo "_errorMSG=\"Request not supported\";false";
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. // ====================================================================================
  174. // Function inventory
  175. // ====================================================================================
  176.  
  177. // Format file size number
  178. function formatFileSize($size)                                                              //CUSTOMIZE
  179. {
  180.     if (ctype_space($size)) return "\"\"";
  181.    
  182.     if ($size > 1024)
  183.         {$size=intval($size/1024); $size.=" MB";}
  184.     else
  185.         $size.=" KB";
  186.  
  187.     return "\"".$size."\"";
  188. };
  189.  
  190.  
  191.  
  192. // Split string if it's too long - don't modify this function
  193. function formatString($string)
  194. {
  195.     if (strlen($string)<=122)
  196.         return "\"" . str_replace("\"","\"\"",$string) . "\"";
  197.    
  198.     $array = str_split($string, 122);
  199.     $string = "[";
  200.     foreach ($array as $part)
  201.         $string .= "]+[\"" . str_replace("\"","\"\"",$part) . "\"";
  202.        
  203.     return $string."]";
  204. };
  205.  
  206.  
  207. // Double quot marks
  208. function formatTitle($string)
  209. {
  210.     return "\"" . str_replace("\"", "\"\"", $string) . "\"";
  211. };
  212. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement