A tumblelog CMS built on AJAX, PHP and MySQL.

functions.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /* ===========================
  3. gelato CMS - A PHP based tumblelog CMS
  4. development version
  5. http://www.gelatocms.com/
  6. gelato CMS is a free software licensed under GPL (General public license)
  7. =========================== */
  8. ?>
  9. <?php
  10. function version() {
  11. return "0.85";
  12. }
  13. function beginsWith($str, $sub) {
  14. return (strpos($str, $sub) === 0);
  15. }
  16. function endsWith($str, $sub) {
  17. return (substr($str, strlen($str) - strlen($sub)) == $sub);
  18. }
  19. function getFileName($fileUrl) {
  20. $path = explode('/', $fileUrl);
  21. return $path[count($path)-1];
  22. }
  23. function isMP3($fileUrl) {
  24. if (endsWith($fileUrl, ".mp3")) {
  25. return true;
  26. } else {
  27. return false;
  28. }
  29. }
  30. function getMP3File($remoteFileName) {
  31. if (isMP3($remoteFileName)) {
  32. if (getFile($remoteFileName)) {
  33. return true;
  34. } else {
  35. return false;
  36. }
  37. } elseif (isGoEar($remoteFileName)) {
  38. return true;
  39. } else {
  40. return false;
  41. }
  42. }
  43. function isGoEar($songUrl) {
  44. if (beginsWith($songUrl, "http://www.goear.com/listen.php?v="))
  45. return true;
  46. else
  47. return false;
  48. }
  49. function isImageFile($photoUrl) {
  50. if (endsWith($photoUrl, ".jpg")) { return true; }
  51. elseif (endsWith($photoUrl, ".gif")) { return true; }
  52. elseif (endsWith($photoUrl, ".png")) { return true; }
  53. else { return false; }
  54. }
  55. function getPhotoFile($remoteFileName) {
  56. if (isImageFile($remoteFileName)) {
  57. if (getFile($remoteFileName)) {
  58. return true;
  59. } else {
  60. return false;
  61. }
  62. } else {
  63. return false;
  64. }
  65. }
  66. function getFile($remoteFileName) {
  67. $fileName = "../uploads/".getFileName($remoteFileName);
  68. $str = _file_get_contents($remoteFileName);
  69. if (!$handle = fopen($fileName, 'w')) {
  70. return false;
  71. }
  72. if (fwrite($handle, $str) === FALSE) {
  73. return false;
  74. }
  75. fclose($handle);
  76. return true;
  77. }
  78. function isVimeoVideo($videoUrl) {
  79. if (beginsWith($videoUrl, "http://vimeo.com/clip:") || beginsWith($videoUrl, "http://www.vimeo.com/clip:"))
  80. return true;
  81. else
  82. return false;
  83. }
  84. function getVimeoVideoUrl($videoUrl) {
  85. return array_pop(explode("clip:",$videoUrl));
  86. }
  87. function isYoutubeVideo($videoUrl) {
  88. if (beginsWith($videoUrl, "http://youtube.com/watch?v=") || beginsWith($videoUrl, "http://www.youtube.com/watch?v="))
  89. return true;
  90. else
  91. return false;
  92. }
  93. function getYoutubeVideoUrl($videoUrl) {
  94. $params = explode("?v=", $videoUrl);
  95. $params2 = explode("&",$params[1]);
  96. return $params2[0];
  97. }
  98. function isVideo($url) {
  99. if (isYoutubeVideo($url)) { return true; }
  100. elseif (isVimeoVideo($url)) { return true; }
  101. else { return false; }
  102. }
  103. function sendMail($to, $title, $body, $from) {
  104. $rp = trim($from);
  105. $org = "gelato CMS";
  106. $mailer = "gelato CMS Mailer";
  107. $head = '';
  108. $head .= "Content-Type: text/html \r\n";
  109. $head .= "Date: ". date('r'). " \r\n";
  110. $head .= "Return-Path: $rp \r\n";
  111. $head .= "From: $from \r\n";
  112. $head .= "Sender: $from \r\n";
  113. $head .= "Reply-To: $from \r\n";
  114. $head .= "Organization: $org \r\n";
  115. $head .= "X-Sender: $from \r\n";
  116. $head .= "X-Priority: 3 \r\n";
  117. $head .= "X-Mailer: $mailer \r\n";
  118. $body = str_replace("\r\n", "\n", $body);
  119. $body = str_replace("\n", "\r\n", $body);
  120. return @mail($to, $title, $body, $head);
  121. }
  122. function getThemes() {
  123. $themes_dir = "themes";
  124. $dirs = array();
  125. $path = getcwd();
  126. $dir = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$themes_dir : $path."/".$themes_dir;
  127. $dir = str_replace("admin\\", "", $dir);
  128. $dir = str_replace("admin/", "", $dir);
  129. $handle = opendir($dir);
  130. $i=0;
  131. while($filename = readdir($handle)) {
  132. if($filename != "." && $filename != "..") {
  133. $dirs[$i]=trim($filename);
  134. $i++;
  135. }
  136. }
  137. closedir($handle);
  138. return $dirs;
  139. }
  140. function _file_get_contents($path) {
  141. // Modified function from:
  142. // http://work.dokoku.net/Anieto2k/_file_get_contents.phps
  143. // http://www.anieto2k.com/2007/02/09/file_get_contents-y-dreamhost/
  144. if (!preg_match('/^http/i', $path)) {
  145. if ($fp = fopen($path, 'r')) {
  146. return fread($fp, 1024);
  147. } else {
  148. return false;
  149. }
  150. } else {
  151. if (extension_loaded('curl') && version_compare(get_curl_version(), '7.10.5', '>=')) {
  152. $ch = curl_init();
  153. $timeout = 5;
  154. curl_setopt ($ch, CURLOPT_URL, $path);
  155. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  156. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  157. $file_contents = curl_exec($ch);
  158. curl_close($ch);
  159. if (is_string($file_contents)) {
  160. return $file_contents;
  161. } else {
  162. return false;
  163. }
  164. } else {
  165. $data = parse_url($path);
  166. if (!$data['host'] || $data['scheme'] != "http") {
  167. return false;
  168. }
  169. $f = fsockopen($data['host'], ($data['port']) ? $data['port'] : 80, $e1, $e2, 3);
  170. if (!$f) {
  171. return false;
  172. }
  173. $q = "GET " . $data['path'] . "?" . $data['query'] . " HTTP/1.1\r\n";
  174. $q .= "Host: " . $data['host'] . "\r\n";
  175. $q .= "Connection: close\r\n";
  176. $q .= "Referer: http://www.gelatocms.com/\r\n\r\n";
  177. $recv = "";
  178. fwrite($f, $q);
  179. while (!feof($f)) {
  180. $recv .= fread($f, 1024);
  181. }
  182. $request = $q;
  183. $response = substr($recv, 0, strpos($recv, "\r\n\r\n"));
  184. $body = substr($recv, strpos($recv, "\r\n\r\n") + 4);
  185. if (preg_match('/http\/1\\.[0|1] ([0-9]{3})/i', $response, $res)) {
  186. if ($res[1][0] != "2") {
  187. return false;
  188. }
  189. } else {
  190. return false;
  191. }
  192. if (preg_match('/transfer-encoding:\s*chunked/i', $response)) {
  193. $tmp_body = $body;
  194. $new = "";
  195. $exit = false;
  196. while (!$exit) {
  197. if (preg_match('/^([0-9a-f]+).*?\r\n/i', $tmp_body, $res)) {
  198. $len = hexdec($res[1]);
  199. if ($len == "0") {
  200. $exit = true;
  201. break;
  202. }
  203. $new .= substr($tmp_body, strlen($res[0]), $len);
  204. $tmp_body = substr($tmp_body, strlen($res[0]) + $len + strlen("\r\n"));
  205. } else {
  206. $exit = true;
  207. }
  208. }
  209. $body = $new;
  210. }
  211. return $body;
  212. }
  213. }
  214. }
  215. function get_curl_version() {
  216. $curl = 0;
  217. if (is_array(curl_version())) {
  218. $curl = curl_version();
  219. $curl = $curl['version'];
  220. } else {
  221. $curl = curl_version();
  222. $curl = explode(' ', $curl);
  223. $curl = explode('/', $curl[0]);
  224. $curl = $curl[1];
  225. }
  226. return $curl;
  227. }
  228. ?>