A tumblelog CMS built on AJAX, PHP and MySQL.

functions.php 6.3KB

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