PageRenderTime 103ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/controller/lib/tools.php

https://gitlab.com/maxupunk/OCdownload
PHP | 256 lines | 216 code | 29 blank | 11 comment | 33 complexity | f93bfafb7260540714ac1f3e376219bc MD5 | raw file
  1. <?php
  2. /**
  3. * ownCloud - ocDownloader
  4. *
  5. * This file is licensed under the Creative Commons BY-SA License version 3 or
  6. * later. See the COPYING file.
  7. *
  8. * @author Xavier Beurois <www.sgc-univ.net>
  9. * @copyright Xavier Beurois 2015
  10. */
  11. namespace OCA\ocDownloader\Controller\Lib;
  12. use OCA\ocDownloader\Controller\Lib\Aria2;
  13. class Tools
  14. {
  15. public static function CheckURL ($URL)
  16. {
  17. $URLPattern = '%^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?$%iu';
  18. preg_match ($URLPattern, $URL, $Matches);
  19. if (count ($Matches) == 1)
  20. {
  21. return true;
  22. }
  23. return false;
  24. }
  25. public static function CheckFilepath ($FP)
  26. {
  27. if (\OC\Files\Filesystem::file_exists ($FP))
  28. {
  29. return true;
  30. }
  31. return false;
  32. }
  33. public static function GetProgressString ($Completed, $Total, $Progress)
  34. {
  35. $CompletedStr = self::FormatSizeUnits ($Completed);
  36. $TotalStr = self::FormatSizeUnits ($Total);
  37. if ($Progress < 1 && $Progress > 0)
  38. {
  39. return $CompletedStr . ' / ' . $TotalStr . ' (' . round ((($Completed / $Total) * 100), 2) . '%)';
  40. }
  41. elseif ($Progress >= 1)
  42. {
  43. return $TotalStr . ' (' . round ((($Completed / $Total) * 100), 2) . '%)';
  44. }
  45. return null;
  46. }
  47. public static function FormatSizeUnits ($Bytes)
  48. {
  49. if ($Bytes >= 1073741824)
  50. {
  51. $Bytes = number_format ($Bytes / 1073741824, 2) . ' GB';
  52. }
  53. elseif ($Bytes >= 1048576)
  54. {
  55. $Bytes = number_format ($Bytes / 1048576, 2) . ' MB';
  56. }
  57. elseif ($Bytes >= 1024)
  58. {
  59. $Bytes = number_format ($Bytes / 1024, 2) . ' KB';
  60. }
  61. else
  62. {
  63. $Bytes = $Bytes . ' B';
  64. }
  65. return $Bytes;
  66. }
  67. public static function CheckBinary ($Binary)
  68. {
  69. exec ('which ' . $Binary, $Output, $Return);
  70. if ($Return == 0)
  71. {
  72. return true;
  73. }
  74. return false;
  75. }
  76. public static function CleanString ($Text)
  77. {
  78. $UTF8 = Array
  79. (
  80. '/[áàâãªä]/u' => 'a',
  81. '/[ÁÀÂÃÄ]/u' => 'A',
  82. '/[ÍÌÎÏ]/u' => 'I',
  83. '/[íìîï]/u' => 'i',
  84. '/[éèêë]/u' => 'e',
  85. '/[ÉÈÊË]/u' => 'E',
  86. '/[óòôõºö]/u' => 'o',
  87. '/[ÓÒÔÕÖ]/u' => 'O',
  88. '/[úùûü]/u' => 'u',
  89. '/[ÚÙÛÜ]/u' => 'U',
  90. '/ç/' => 'c',
  91. '/Ç/' => 'C',
  92. '/ñ/' => 'n',
  93. '/Ñ/' => 'N',
  94. '/–/' => '-', // UTF-8 hyphen to "normal" hyphen
  95. '/[’‘‹›‚]/u' => '', // Literally a single quote
  96. '/[“”«»„]/u' => '', // Double quote
  97. '/ /' => '_', // nonbreaking space (equiv. to 0x160)
  98. );
  99. return preg_replace (array_keys ($UTF8), array_values ($UTF8), $Text);
  100. }
  101. public static function GetDownloadStatusID ($Status)
  102. {
  103. switch (strtolower ($Status))
  104. {
  105. case 'complete':
  106. $DLStatus = 0;
  107. break;
  108. case 'active':
  109. $DLStatus = 1;
  110. break;
  111. case 'waiting':
  112. $DLStatus = 2;
  113. break;
  114. case 'paused':
  115. $DLStatus = 3;
  116. break;
  117. case 'removed':
  118. $DLStatus = 4;
  119. break;
  120. default:
  121. $DLStatus = 5;
  122. break;
  123. }
  124. return $DLStatus;
  125. }
  126. public static function GetCounters ($DbType, $UID)
  127. {
  128. $SQL = 'SELECT (SELECT COUNT(*) FROM `*PREFIX*ocdownloader_queue` WHERE `STATUS` < ? AND `UID` = ?) as `ALL`,' .
  129. '(SELECT COUNT(*) FROM `*PREFIX*ocdownloader_queue` WHERE `STATUS` = ? AND `UID` = ?) as `COMPLETES`,' .
  130. '(SELECT COUNT(*) FROM `*PREFIX*ocdownloader_queue` WHERE `STATUS` = ? AND `UID` = ?) as `ACTIVES`,' .
  131. '(SELECT COUNT(*) FROM `*PREFIX*ocdownloader_queue` WHERE `STATUS` = ? AND `UID` = ?) as `WAITINGS`,' .
  132. '(SELECT COUNT(*) FROM `*PREFIX*ocdownloader_queue` WHERE `STATUS` = ? AND `UID` = ?) as `STOPPED`,' .
  133. '(SELECT COUNT(*) FROM `*PREFIX*ocdownloader_queue` WHERE `STATUS` = ? AND `UID` = ?) as `REMOVED`';
  134. if ($DbType == 1)
  135. {
  136. $SQL = 'SELECT (SELECT COUNT(*) FROM *PREFIX*ocdownloader_queue WHERE "STATUS" < ? AND "UID" = ?) as "ALL",' .
  137. '(SELECT COUNT(*) FROM *PREFIX*ocdownloader_queue WHERE "STATUS" = ? AND "UID" = ?) as "COMPLETES",' .
  138. '(SELECT COUNT(*) FROM *PREFIX*ocdownloader_queue WHERE "STATUS" = ? AND "UID" = ?) as "ACTIVES",' .
  139. '(SELECT COUNT(*) FROM *PREFIX*ocdownloader_queue WHERE "STATUS" = ? AND "UID" = ?) as "WAITINGS",' .
  140. '(SELECT COUNT(*) FROM *PREFIX*ocdownloader_queue WHERE "STATUS" = ? AND "UID" = ?) as "STOPPED",' .
  141. '(SELECT COUNT(*) FROM *PREFIX*ocdownloader_queue WHERE "STATUS" = ? AND "UID" = ?) as "REMOVED"';
  142. }
  143. $Query = \OCP\DB::prepare ($SQL);
  144. $Request = $Query->execute (Array (5, $UID, 0, $UID, 1, $UID, 2, $UID, 3, $UID, 4, $UID));
  145. return $Request->fetchRow ();
  146. }
  147. public static function StartsWith ($Haystack, $Needle)
  148. {
  149. return $Needle === "" || strrpos ($Haystack, $Needle, -strlen ($Haystack)) !== FALSE;
  150. }
  151. public static function EndsWith ($Haystack, $Needle)
  152. {
  153. return $Needle === "" || (($Temp = strlen ($Haystack) - strlen ($Needle)) >= 0 && strpos ($Haystack, $Needle, $Temp) !== FALSE);
  154. }
  155. public static function GetLastVersionNumber ()
  156. {
  157. $CH = curl_init ('https://raw.githubusercontent.com/DjazzLab/ocdownloader/master/appinfo/version');
  158. curl_setopt_array ($CH, Array (
  159. CURLOPT_RETURNTRANSFER => true,
  160. CURLOPT_TIMEOUT => 10,
  161. CURLOPT_CONNECTTIMEOUT => 10,
  162. CURLOPT_RETURNTRANSFER => true,
  163. CURLOPT_FOLLOWLOCATION => true,
  164. CURLOPT_MAXREDIRS => 10
  165. ));
  166. $Data = curl_exec ($CH);
  167. curl_close ($CH);
  168. return $Data;
  169. }
  170. public static function CanCheckForUpdate ()
  171. {
  172. // Is the user in the admin group ?
  173. if (\OC_User::isAdminUser (\OC_User::getUser ()))
  174. {
  175. // Is the ocdownloader option to automatically check is enable ?
  176. $Settings = new Settings ();
  177. $Settings->SetKey ('CheckForUpdates');
  178. $CheckForUpdates = $Settings->GetValue ();
  179. if (strcmp ($CheckForUpdates, 'Y') == 0 || is_null ($CheckForUpdates))
  180. {
  181. return true;
  182. }
  183. }
  184. return false;
  185. }
  186. public static function ResetAria2 ($DbType)
  187. {
  188. $SQL = 'SELECT * FROM `*PREFIX*ocdownloader_queue`';
  189. if ($DbType == 1)
  190. {
  191. $SQL = 'SELECT * FROM *PREFIX*ocdownloader_queue';
  192. }
  193. $Query = \OCP\DB::prepare ($SQL);
  194. $Request = $Query->execute ();
  195. while ($Row = $Request->fetchRow ())
  196. {
  197. $Status = Aria2::TellStatus ($GID);
  198. if (!isset ($Status['error']) && strcmp ($Status['result']['status'], 'error') != 0 && strcmp ($Status['result']['status'], 'complete') != 0)
  199. {
  200. Aria2::Remove ($GID);
  201. }
  202. }
  203. $Purge = Aria2::PurgeDownloadResult ();
  204. if (isset ($Purge['result']) && strcmp ($Purge['result'], 'OK') == 0)
  205. {
  206. $SQL = 'TRUNCATE TABLE `*PREFIX*ocdownloader_queue`';
  207. if ($DbType == 1)
  208. {
  209. $SQL = 'TRUNCATE TABLE *PREFIX*ocdownloader_queue';
  210. }
  211. $Query = \OCP\DB::prepare ($SQL);
  212. $Request = $Query->execute ();
  213. }
  214. }
  215. public static function GetMinutes ($Number, $UnitLetter)
  216. {
  217. if (strcmp ($UnitLetter, 'i') == 0)
  218. {
  219. return $Number;
  220. }
  221. $Units = array ('h' => 'hour', 'd' => 'day', 'w' => 'week', 'm' => 'month', 'y' => 'year');
  222. $To = strtotime ('+' . $Number . ' ' . $Units[$UnitLetter] . ($Number > 1 ? 's' : ''));
  223. $From = strtotime ('now');
  224. return round (abs ($To - $From) / 60,2);
  225. }
  226. }
  227. ?>